1+ import json
2+ from typing import Any
3+
4+ import pytest
15from django .urls import reverse
26from rest_framework import status
37from rest_framework .test import APIClient
@@ -20,3 +24,99 @@ def test_get_current_user(staff_user: FFAdminUser, staff_client: APIClient) -> N
2024 assert response_json ["first_name" ] == staff_user .first_name
2125 assert response_json ["last_name" ] == staff_user .last_name
2226 assert response_json ["uuid" ] == str (staff_user .uuid )
27+
28+
29+ def test_get_me_should_return_onboarding_object (db : None ) -> None :
30+ # Given
31+ onboarding = {
32+ "tasks" : [{"name" : "task-1" }],
33+ "tools" : {"completed" : True , "integrations" : ["integration-1" ]},
34+ }
35+ onboarding_serialized = json .dumps (onboarding )
36+ new_user = FFAdminUser .objects .create (
37+ email = "testuser@mail.com" ,
38+ onboarding_data = onboarding_serialized ,
39+ )
40+
41+ new_user .save ()
42+ client = APIClient ()
43+ client .force_authenticate (user = new_user )
44+ url = reverse ("api-v1:custom_auth:ffadminuser-me" )
45+
46+ # When
47+ response = client .get (url )
48+
49+ # Then
50+ assert response .status_code == status .HTTP_200_OK
51+ response_json = response .json ()
52+ assert response_json ["onboarding" ] is not None
53+ assert response_json ["onboarding" ].get ("tools" , {}).get ("completed" ) is True
54+ assert response_json ["onboarding" ].get ("tools" , {}).get ("integrations" ) == [
55+ "integration-1"
56+ ]
57+ assert response_json ["onboarding" ].get ("tasks" ) is not None
58+ assert response_json ["onboarding" ].get ("tasks" , [])[0 ].get ("name" ) == "task-1"
59+
60+
61+ @pytest .mark .parametrize (
62+ "data,expected_keys" ,
63+ [
64+ (
65+ {"tasks" : [{"name" : "task-1" , "completed_at" : "2024-01-01T12:00:00Z" }]},
66+ {"tasks" },
67+ ),
68+ ({"tools" : {"completed" : True , "integrations" : ["integration-1" ]}}, {"tools" }),
69+ (
70+ {
71+ "tasks" : [{"name" : "task-1" , "completed_at" : "2024-01-01T12:00:00Z" }],
72+ "tools" : {"completed" : True , "integrations" : ["integration-1" ]},
73+ },
74+ {"tasks" , "tools" },
75+ ),
76+ ],
77+ )
78+ def test_patch_user_onboarding_updates_only_nested_objects_if_provided (
79+ staff_user : FFAdminUser ,
80+ staff_client : APIClient ,
81+ data : dict [str , Any ],
82+ expected_keys : set [str ],
83+ ) -> None :
84+ # Given
85+ url = reverse ("api-v1:custom_auth:ffadminuser-patch-onboarding" )
86+
87+ # When
88+ response = staff_client .patch (url , data = data , format = "json" )
89+
90+ # Then
91+ staff_user .refresh_from_db ()
92+
93+ assert response .status_code == status .HTTP_204_NO_CONTENT
94+ onboarding_json = json .loads (staff_user .onboarding_data or "{}" )
95+ assert onboarding_json is not None
96+ if "tasks" in expected_keys :
97+ assert onboarding_json .get ("tasks" , [])[0 ]
98+ assert onboarding_json .get ("tasks" , [])[0 ].get ("name" ) == data .get ("tasks" , [])[
99+ 0
100+ ].get ("name" )
101+ if "tools" in expected_keys :
102+ assert onboarding_json .get ("tools" , {}).get ("completed" ) is True
103+ assert onboarding_json .get ("tools" , {}).get ("integrations" ) == data .get (
104+ "tools" , {}
105+ ).get ("integrations" )
106+
107+
108+ def test_patch_user_onboarding_returns_error_if_tasks_and_tools_are_missing (
109+ staff_user : FFAdminUser ,
110+ staff_client : APIClient ,
111+ ) -> None :
112+ # Given
113+ url = reverse ("api-v1:custom_auth:ffadminuser-patch-onboarding" )
114+
115+ # When
116+ response = staff_client .patch (url , data = {}, format = "json" )
117+
118+ # Then
119+ assert response .status_code == status .HTTP_400_BAD_REQUEST
120+ assert response .json () == {
121+ "non_field_errors" : ["At least one of 'tasks' or 'tools' must be provided." ]
122+ }
0 commit comments