-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
154 lines (123 loc) · 3.64 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding: utf-8 -*-
import os
import sys
import pytest
from importlib import import_module
from uuid import uuid4
# Insert current directory to the top in os.path for relative paths.
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import django
django.setup()
from django.test.client import Client
from django.conf import settings
from django.apps import apps
from django.contrib.auth.models import AnonymousUser
from django.contrib.auth import login
try:
from django.contrib.auth import get_user_model
except ImportError:
from django.contrib.auth.models import User
get_user_model = lambda: User
pytestmark = pytest.mark.django_db
def django_app(name):
def wrapper():
if name not in sys.modules:
import_module(name)
res = sys.modules[name]
return res
return wrapper
def django_model(model):
def wrapper():
return model
return wrapper
for app_name in set(settings.INSTALLED_APPS):
pytest.fixture(scope='session')(django_app(app_name))
for model in apps.get_models():
pytest.fixture(scope='session')(django_model(model))
@pytest.fixture
def client():
"""A Django test client instance"""
return Client()
@pytest.fixture
def anonymous_user():
""" AnonymousUser instance"""
return AnonymousUser()
@pytest.fixture
def test_user():
"""User instance"""
kls = get_user_model()
try:
user = kls.objects.get(username='test')
except kls.DoesNotExist:
user = kls.objects.create(
username='test',
email='test@example.com',
password='test',
first_name='John',
last_name='Doe',
)
return user
@pytest.fixture
def staff_user():
"""Staff User instance"""
kls = get_user_model()
try:
staff = kls.objects.get(username='staff')
except kls.DoesNotExist:
staff = kls.objects.create(
username='staff',
email='staff@example.com',
password='staff',
first_name='Alice',
last_name='Grace',
is_staff=True
)
return staff
@pytest.fixture
def admin_user():
"""SuperUser instance"""
kls = get_user_model()
try:
admin = kls.objects.get(username='admin')
except kls.DoesNotExist:
admin = kls.objects.create_superuser(
username='admin',
email='admin@example.com',
password='admin',
is_superuser=True
)
return admin
@pytest.fixture
def uclient(client, test_user, rf):
"""Client instance with logged in user"""
test_user.backend = 'django.contrib.auth.backends.ModelBackend'
if client.session:
rf.session = client.session
else:
engine = import_module(settings.SESSION_ENGINE)
rf.session = engine.SessionStore()
login(rf, test_user)
# Save the session values.
rf.session.save()
# Set the cookie to represent the session.
session_cookie = settings.SESSION_COOKIE_NAME
client.cookies[session_cookie] = rf.session.session_key
cookie_data = {
'max-age': None,
'path': '/',
'domain': settings.SESSION_COOKIE_DOMAIN,
'secure': settings.SESSION_COOKIE_SECURE or None,
'expires': None
}
client.cookies[session_cookie].update(cookie_data)
client.user = test_user
return client
@pytest.fixture
def aclient(client, admin_user, rf):
"""Client instance with logged in admin"""
return uclient(client, admin_user, rf)
# @pytest.fixture
# def elastic_client():
# es = settings.ES_CLIENT
# yield es
# es.indices.delete(index=settings.ES_INDEX, ignore=[400, 404])