0002-tests-initiate-module-for-later-unit-testing-impleme.patch
tests/conftest.py | ||
---|---|---|
1 |
import django_webtest |
|
2 |
import pytest |
|
3 | ||
4 |
try: |
|
5 |
import pathlib |
|
6 |
except ImportError: |
|
7 |
import pathlib2 as pathlib |
|
8 | ||
9 |
from django.contrib.auth import get_user_model |
|
10 |
from django_rbac.utils import get_ou_model |
|
11 | ||
12 |
User = get_user_model() |
|
13 |
TEST_DIR = pathlib.Path(__file__).parent |
|
14 | ||
15 | ||
16 |
@pytest.fixture |
|
17 |
def app(request, db, settings, tmpdir): |
|
18 |
wtm = django_webtest.WebTestMixin() |
|
19 |
wtm._patch_settings() |
|
20 |
request.addfinalizer(wtm._unpatch_settings) |
|
21 |
settings.MEDIA_DIR = str(tmpdir.mkdir('media')) |
|
22 |
return django_webtest.DjangoTestApp(extra_environ={'HTTP_HOST': 'localhost'}) |
|
23 | ||
24 | ||
25 |
class AllHook: |
|
26 |
def __init__(self): |
|
27 |
self.calls = {} |
|
28 |
from authentic2 import hooks |
|
29 | ||
30 |
hooks.get_hooks.cache.clear() |
|
31 | ||
32 |
def __call__(self, hook_name, *args, **kwargs): |
|
33 |
calls = self.calls.setdefault(hook_name, []) |
|
34 |
calls.append({'args': args, 'kwargs': kwargs}) |
|
35 | ||
36 |
def __getattr__(self, name): |
|
37 |
return self.calls.get(name, []) |
|
38 | ||
39 |
def clear(self): |
|
40 |
self.calls = {} |
|
41 | ||
42 | ||
43 |
@pytest.fixture |
|
44 |
def user(db): |
|
45 |
user = User.objects.create( |
|
46 |
username='john.doe', |
|
47 |
email='john.doe@example.net', |
|
48 |
first_name='John', |
|
49 |
last_name='Doe', |
|
50 |
email_verified=True, |
|
51 |
) |
|
52 |
user.set_password('john.doe') |
|
53 |
return user |
|
54 | ||
55 | ||
56 |
@pytest.fixture |
|
57 |
def hooks(settings): |
|
58 |
if hasattr(settings, 'A2_HOOKS'): |
|
59 |
hooks = settings.A2_HOOKS |
|
60 |
else: |
|
61 |
hooks = settings.A2_HOOKS = {} |
|
62 |
hook = hooks['__all__'] = AllHook() |
|
63 |
yield hook |
|
64 |
hook.clear() |
|
65 |
del settings.A2_HOOKS['__all__'] |
|
66 | ||
67 | ||
68 |
@pytest.fixture |
|
69 |
def admin(db): |
|
70 |
user = User(username='admin', email='admin@example.net', is_superuser=True, is_staff=True) |
|
71 |
user.set_password('admin') |
|
72 |
user.save() |
|
73 |
return user |
|
74 | ||
75 | ||
76 |
@pytest.fixture(autouse=True) |
|
77 |
def clean_caches(): |
|
78 |
from authentic2.apps.journal.models import event_type_cache |
|
79 | ||
80 |
event_type_cache.cache.clear() |
tests/settings.py | ||
---|---|---|
1 |
import os |
|
2 | ||
3 |
ALLOWED_HOSTS = ['localhost'] |
|
4 | ||
5 |
DATABASES = { |
|
6 |
'default': { |
|
7 |
'ENGINE': 'django.db.backends.postgresql_psycopg2', |
|
8 |
'NAME': 'authentic2-auth-fedict', |
|
9 |
} |
|
10 |
} |
|
11 | ||
12 |
if 'postgres' in DATABASES['default']['ENGINE']: |
|
13 |
for key in ('PGPORT', 'PGHOST', 'PGUSER', 'PGPASSWORD'): |
|
14 |
if key in os.environ: |
|
15 |
DATABASES['default'][key[2:]] = os.environ[key] |
|
16 | ||
17 |
LANGUAGE_CODE = 'en' |
|
18 |
A2_FC_CLIENT_ID = '' |
|
19 |
A2_FC_CLIENT_SECRET = '' |
|
20 | ||
21 |
# test hook handlers |
|
22 |
A2_HOOKS_PROPAGATE_EXCEPTIONS = True |
tests/test_all.py | ||
---|---|---|
1 |
import pytest |
|
2 |
from django.contrib.auth import get_user_model |
|
3 |
from utils import login |
|
4 | ||
5 |
User = get_user_model() |
|
6 | ||
7 |
pytestmark = pytest.mark.django_db |
|
8 | ||
9 | ||
10 |
def test_dummy(app): |
|
11 |
assert 1 |
tests/utils.py | ||
---|---|---|
1 |
from authentic2.utils.misc import make_url |
|
2 |
from django.urls import reverse |
|
3 | ||
4 | ||
5 |
def login(app, user, path=None, password=None, remember_me=None): |
|
6 |
if path: |
|
7 |
real_path = make_url(path) |
|
8 |
login_page = app.get(real_path, status=302).maybe_follow() |
|
9 |
else: |
|
10 |
login_page = app.get(reverse('auth_login')) |
|
11 |
assert login_page.request.path == reverse('auth_login') |
|
12 |
form = login_page.form |
|
13 |
form.set('username', user.username if hasattr(user, 'username') else user) |
|
14 |
# password is supposed to be the same as username |
|
15 |
form.set('password', password or user.username) |
|
16 |
if remember_me is not None: |
|
17 |
form.set('remember_me', bool(remember_me)) |
|
18 |
response = form.submit(name='login-password-submit').follow() |
|
19 |
if path: |
|
20 |
assert response.request.path == real_path |
|
21 |
else: |
|
22 |
assert response.request.path == reverse('auth_homepage') |
|
23 |
assert '_auth_user_id' in app.session |
|
24 |
return response |
|
0 |
- |