test some management commands (#24866)

This commit is contained in:
Emmanuel Cazenave 2018-07-10 18:44:45 +02:00
parent 82f4dc6cdc
commit a6e24b6855
2 changed files with 124 additions and 0 deletions

View File

@ -0,0 +1,27 @@
{
"jwks_uri": "https://a2test.publik/idp/oidc/certs/",
"frontchannel_logout_session_supported": true,
"subject_types_supported": [
"public",
"pairwise"
],
"token_endpoint_auth_methods_supported": [
"clien_secret_post",
"client_secret_basic"
],
"id_token_signing_alg_values_supported": [
"RS256",
"HS256"
],
"issuer": "https://a2test.publik/",
"userinfo_endpoint": "https://a2test.publik/idp/oidc/user_info/",
"frontchannel_logout_supported": true,
"token_endpoint": "https://a2test.publik/idp/oidc/token/",
"response_types_supported": [
"code",
"token",
"token id_token"
],
"end_session_endpoint": "https://a2test.publik/idp/oidc/logout/",
"authorization_endpoint": "https://a2test.publik/idp/oidc/authorize/"
}

97
tests/test_commands.py Normal file
View File

@ -0,0 +1,97 @@
import datetime
import importlib
import json
from django.core import management
import py
from authentic2.models import DeletedUser
from authentic2_auth_oidc.models import OIDCProvider
from django_rbac.utils import get_ou_model
def test_changepassword(db, simple_user, monkeypatch):
import getpass
def _getpass(*args, **kwargs):
return 'pass'
monkeypatch.setattr(getpass, 'getpass', _getpass)
management.call_command('changepassword', 'user')
old_pass = simple_user.password
simple_user.refresh_from_db()
assert old_pass != simple_user.password
def test_clean_unused_account(simple_user):
simple_user.last_login = datetime.datetime.now() - datetime.timedelta(days=2)
simple_user.save()
management.call_command('clean-unused-accounts', '1')
assert DeletedUser.objects.get(user=simple_user)
def test_cleanupauthentic(db):
management.call_command('cleanupauthentic')
def test_load_ldif(db, monkeypatch, tmpdir):
ldif = tmpdir.join('some.ldif')
ldif.ensure()
class MockPArser(object):
def __init__(self, *args, **kwargs):
self.users = []
assert len(args) == 1
assert isinstance(args[0], file)
assert kwargs['options']['extra_attribute'] == {'ldap_attr': 'first_name'}
assert kwargs['options']['result'] == 'result'
def parse(self):
pass
oidc_cmd = importlib.import_module(
'authentic2.management.commands.load-ldif')
monkeypatch.setattr(oidc_cmd, 'DjangoUserLDIFParser', MockPArser)
management.call_command(
'load-ldif', ldif.strpath, result='result', extra_attribute={'ldap_attr': 'first_name'})
def test_oidc_register_issuer(db, tmpdir, monkeypatch):
oidc_conf_f = py.path.local(__file__).dirpath('openid_configuration.json')
with oidc_conf_f.open() as f:
oidc_conf = json.load(f)
def register_issuer(
name, issuer=None, openid_configuration=None, verify=True, timeout=None,
ou=None):
OU = get_ou_model()
ou = OU.objects.get(default=True)
return OIDCProvider.objects.create(
name=name, ou=ou, issuer=issuer, strategy='create',
authorization_endpoint=openid_configuration['authorization_endpoint'],
token_endpoint=openid_configuration['token_endpoint'],
userinfo_endpoint=openid_configuration['userinfo_endpoint'],
end_session_endpoint=openid_configuration['end_session_endpoint'])
oidc_cmd = importlib.import_module(
'authentic2_auth_oidc.management.commands.oidc-register-issuer')
monkeypatch.setattr(oidc_cmd, 'register_issuer', register_issuer)
oidc_conf = py.path.local(__file__).dirpath('openid_configuration.json').strpath
management.call_command(
'oidc-register-issuer', 'somename', openid_configuration=oidc_conf, issuer='issuer')
provider = OIDCProvider.objects.get(name='somename')
assert provider.issuer == 'issuer'
def test_resetpassword(simple_user):
management.call_command('resetpassword', 'user')
old_pass = simple_user.password
simple_user.refresh_from_db()
assert old_pass != simple_user.password
def test_sync_metadata(db):
test_file = py.path.local(__file__).dirpath('metadata.xml').strpath
management.call_command('sync-metadata', test_file)