-
Notifications
You must be signed in to change notification settings - Fork 520
Expand file tree
/
Copy pathtest_unit_custom_auth_views.py
More file actions
122 lines (101 loc) · 3.79 KB
/
test_unit_custom_auth_views.py
File metadata and controls
122 lines (101 loc) · 3.79 KB
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
import json
from typing import Any
import pytest
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient
from users.models import FFAdminUser
def test_get_current_user(staff_user: FFAdminUser, staff_client: APIClient) -> None:
# Given
url = reverse("api-v1:custom_auth:ffadminuser-me")
# When
response = staff_client.get(url)
# Then
assert response.status_code == status.HTTP_200_OK
response_json = response.json()
assert response_json["email"] == staff_user.email
assert response_json["first_name"] == staff_user.first_name
assert response_json["last_name"] == staff_user.last_name
assert response_json["uuid"] == str(staff_user.uuid)
def test_get_me_should_return_onboarding_object(db: None) -> None:
# Given
onboarding = {
"tasks": [{"name": "task-1"}],
"tools": {"completed": True, "integrations": ["integration-1"]},
}
onboarding_serialized = json.dumps(onboarding)
new_user = FFAdminUser.objects.create(
email="testuser@mail.com",
onboarding_data=onboarding_serialized,
)
new_user.save()
client = APIClient()
client.force_authenticate(user=new_user)
url = reverse("api-v1:custom_auth:ffadminuser-me")
# When
response = client.get(url)
# Then
assert response.status_code == status.HTTP_200_OK
response_json = response.json()
assert response_json["onboarding"] is not None
assert response_json["onboarding"].get("tools", {}).get("completed") is True
assert response_json["onboarding"].get("tools", {}).get("integrations") == [
"integration-1"
]
assert response_json["onboarding"].get("tasks") is not None
assert response_json["onboarding"].get("tasks", [])[0].get("name") == "task-1"
@pytest.mark.parametrize(
"data,expected_keys",
[
(
{"tasks": [{"name": "task-1", "completed_at": "2024-01-01T12:00:00Z"}]},
{"tasks"},
),
({"tools": {"completed": True, "integrations": ["integration-1"]}}, {"tools"}),
(
{
"tasks": [{"name": "task-1", "completed_at": "2024-01-01T12:00:00Z"}],
"tools": {"completed": True, "integrations": ["integration-1"]},
},
{"tasks", "tools"},
),
],
)
def test_patch_user_onboarding_updates_only_nested_objects_if_provided(
staff_user: FFAdminUser,
staff_client: APIClient,
data: dict[str, Any],
expected_keys: set[str],
) -> None:
# Given
url = reverse("api-v1:custom_auth:ffadminuser-patch-onboarding")
# When
response = staff_client.patch(url, data=data, format="json")
# Then
staff_user.refresh_from_db()
assert response.status_code == status.HTTP_204_NO_CONTENT
onboarding_json = json.loads(staff_user.onboarding_data or "{}")
assert onboarding_json is not None
if "tasks" in expected_keys:
assert onboarding_json.get("tasks", [])[0]
assert onboarding_json.get("tasks", [])[0].get("name") == data.get("tasks", [])[
0
].get("name")
if "tools" in expected_keys:
assert onboarding_json.get("tools", {}).get("completed") is True
assert onboarding_json.get("tools", {}).get("integrations") == data.get(
"tools", {}
).get("integrations")
def test_patch_user_onboarding_returns_error_if_tasks_and_tools_are_missing(
staff_user: FFAdminUser,
staff_client: APIClient,
) -> None:
# Given
url = reverse("api-v1:custom_auth:ffadminuser-patch-onboarding")
# When
response = staff_client.patch(url, data={}, format="json")
# Then
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json() == {
"non_field_errors": ["At least one of 'tasks' or 'tools' must be provided."]
}