authentic2-cut/tests/conftest.py

81 lines
2.0 KiB
Python

import copy
import pytest
import django_webtest
from collections import namedtuple
from django.core.management import call_command
from django_rbac.utils import get_ou_model
OU = get_ou_model()
@pytest.fixture
def app(request, db, settings, tmpdir):
wtm = django_webtest.WebTestMixin()
wtm._patch_settings()
request.addfinalizer(wtm._unpatch_settings)
settings.MEDIA_DIR = str(tmpdir.mkdir('media'))
call_command('loaddata', 'cut_attributes.json')
return django_webtest.DjangoTestApp(extra_environ={'HTTP_HOST': 'localhost'})
@pytest.fixture
def partner_ou(db):
return OU.objects.create(name='partner', slug='ou')
@pytest.fixture
def glc(app, partner_ou, db):
from authentic2_idp_oidc.models import OIDCClient
oidc_client = OIDCClient.objects.create(
name='Client 1',
slug='client1',
ou=partner_ou,
client_id='client1',
client_secret='client1',
# IMPORTANT !
has_api_access=True,
identifier_policy=OIDCClient.POLICY_PAIRWISE_REVERSIBLE,
)
GLC = namedtuple('GLC', ['oidc_client'])
return GLC(oidc_client=oidc_client)
@pytest.fixture
def glc_app(app, glc):
app = copy.copy(app)
app.authorization = ('Basic', (glc.oidc_client.client_id, glc.oidc_client.client_secret))
return app
class AllHook(object):
def __init__(self):
self.calls = {}
from authentic2 import hooks
hooks.get_hooks.cache.clear()
def __call__(self, hook_name, *args, **kwargs):
calls = self.calls.setdefault(hook_name, [])
calls.append({'args': args, 'kwargs': kwargs})
def __getattr__(self, name):
return self.calls.get(name, [])
def clear(self):
self.calls = {}
@pytest.fixture
def hooks(settings):
if hasattr(settings, 'A2_HOOKS'):
hooks = settings.A2_HOOKS
else:
hooks = settings.A2_HOOKS = {}
hook = hooks['__all__'] = AllHook()
yield hook
hook.clear()
del settings.A2_HOOKS['__all__']