-
Notifications
You must be signed in to change notification settings - Fork 522
Expand file tree
/
Copy pathtest_unit_custom_auth_views.py
More file actions
238 lines (205 loc) · 7.63 KB
/
test_unit_custom_auth_views.py
File metadata and controls
238 lines (205 loc) · 7.63 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import json
from typing import Any
from unittest.mock import MagicMock
import pytest
from django.urls import reverse
from freezegun import freeze_time
from pytest_django.fixtures import SettingsWrapper
from rest_framework import status
from rest_framework.test import APIClient
from users.models import FFAdminUser, HubspotTracker
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)
@pytest.mark.parametrize(
"onboarding_data, expected_response",
[
(None, None),
(
{"tasks": [{"name": "task-1"}]},
{"tasks": [{"name": "task-1", "completed_at": "2025-01-01T12:00:00Z"}]},
),
(
{"tools": {"completed": True, "integrations": ["integration-1"]}},
{
"tasks": [],
"tools": {"completed": True, "integrations": ["integration-1"]},
},
),
(
{
"tasks": [{"name": "task-1"}],
"tools": {"completed": True, "integrations": ["integration-1"]},
},
{
"tasks": [{"name": "task-1", "completed_at": "2025-01-01T12:00:00Z"}],
"tools": {"completed": True, "integrations": ["integration-1"]},
},
),
(
{
"tasks": [{"name": "task-1"}],
"tools": {"completed": True, "integrations": ["integration-1"]},
"hosting_preferences": ["hosting-preference-1, hosting-preference-2"],
},
{
"tasks": [{"name": "task-1", "completed_at": "2025-01-01T12:00:00Z"}],
"tools": {"completed": True, "integrations": ["integration-1"]},
"hosting_preferences": ["hosting-preference-1, hosting-preference-2"],
},
),
],
)
@freeze_time("2025-01-01T12:00:00Z")
def test_get_me_should_return_onboarding_object(
db: None, onboarding_data: dict[str, Any], expected_response: dict[str, Any]
) -> None:
# Given
onboarding_serialized = json.dumps(onboarding_data)
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"] == expected_response
@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"},
),
(
{
"tasks": [{"name": "task-1", "completed_at": "2024-01-01T12:00:00Z"}],
"tools": {"completed": True, "integrations": ["integration-1"]},
"hosting_preferences": ["hosting-preference-1, hosting-preference-2"],
},
{"tasks", "tools", "hosting_preferences"},
),
],
)
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_preferences_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' or 'hosting_preferences' must be provided."
]
}
def test_create_user_calls_hubspot_tracking_and_creates_hubspot_contact(
mocker: MagicMock,
db: None,
settings: SettingsWrapper,
staff_client: APIClient,
) -> None:
# Given
hubspot_cookie = "1234567890"
settings.ENABLE_HUBSPOT_LEAD_TRACKING = True
data = {
"first_name": "new",
"last_name": "user",
"email": "test@exemple.fr",
"password": "password123456!=&",
"sign_up_type": "NO_INVITE",
"hubspotutk": hubspot_cookie,
}
mock_create_hubspot_contact_for_user = mocker.patch(
"integrations.lead_tracking.hubspot.services.create_hubspot_contact_for_user"
)
url = reverse("api-v1:custom_auth:ffadminuser-list")
# When
response = staff_client.post(url, data=data, format="json")
user = FFAdminUser.objects.filter(email="test@exemple.fr").first()
hubspot_tracker = HubspotTracker.objects.filter(user=user).first()
assert hubspot_tracker is not None
assert hubspot_tracker.hubspot_cookie == hubspot_cookie
# Then
assert response.status_code == status.HTTP_201_CREATED
assert user is not None
mock_create_hubspot_contact_for_user.delay.assert_called_once_with(args=(user.id,))
def test_create_user_does_not_create_hubspot_tracking_if_no_cookie_is_provided(
mocker: MagicMock,
db: None,
settings: SettingsWrapper,
staff_client: APIClient,
) -> None:
# Given
settings.ENABLE_HUBSPOT_LEAD_TRACKING = True
data = {
"first_name": "new",
"last_name": "user",
"email": "test@exemple.fr",
"password": "password123456!=&",
"sign_up_type": "NO_INVITE",
}
mock_create_hubspot_contact_for_user = mocker.patch(
"integrations.lead_tracking.hubspot.services.create_hubspot_contact_for_user"
)
url = reverse("api-v1:custom_auth:ffadminuser-list")
# When
response = staff_client.post(url, data=data, format="json")
user = FFAdminUser.objects.filter(email="test@exemple.fr").first()
hubspot_tracker = HubspotTracker.objects.filter(user=user).first()
assert hubspot_tracker is None
# Then
assert response.status_code == status.HTTP_201_CREATED
assert user is not None
mock_create_hubspot_contact_for_user.delay.assert_called_once_with(args=(user.id,))