-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
65 lines (50 loc) · 1.6 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
"""
/conftest.py
~~~~~~~~~~~~
Defines project-scope Pytest fixtures.
"""
import pytest
from django.urls import reverse
from rest_framework.test import APIClient
from rest_framework_simplejwt.tokens import RefreshToken
from athletes.models import Athlete
DUMMY_USER = {"email": "[email protected]", "name": "dummy user"}
DUMMY_USER_PASSWD = "testpasswd123."
SUPER_USER = {
"email": "[email protected]",
"name": "super user",
"is_active": True,
"is_superuser": True,
"is_staff": True,
"password": DUMMY_USER_PASSWD,
}
@pytest.fixture
def dummy_user(db):
user = Athlete.objects.create_user(**DUMMY_USER, password=DUMMY_USER_PASSWD)
return user
@pytest.fixture
def auth_client(db):
user = Athlete.objects.create_user(
**DUMMY_USER, is_superuser=False, is_staff=False, password=DUMMY_USER_PASSWD
)
client = APIClient()
res = client.post(
reverse("token_obtain_pair"), {"email": user.email, "password": DUMMY_USER_PASSWD}
)
assert res.status_code == 200
refresh = RefreshToken.for_user(user)
client.credentials(HTTP_AUTHORIZATION=f"Bearer {refresh.access_token}")
return client
@pytest.fixture
def admin_auth_client(db):
superuser = Athlete.objects.create_user(**SUPER_USER)
client = APIClient()
res = client.post(
reverse("token_obtain_pair"),
{"email": superuser.email, "password": DUMMY_USER_PASSWD},
)
assert res.status_code == 200
# Obtain token for user
refresh = RefreshToken.for_user(superuser)
client.credentials(HTTP_AUTHORIZATION=f"Bearer {refresh.access_token}")
return client