77
88import pytest
99from django .apps import apps
10+ from django .test import override_settings
1011from django .urls import reverse
11-
12- from . import test_settings as settings
12+ from conftest import backends , sandbox_backends
13+ from .test_backends import _get_backend_cls
1314from . import factories as f
1415
1516pytestmark = pytest .mark .django_db
1920SESSION_TOKEN = "phone-auth-session-token"
2021
2122
22- def test_phone_registration_sends_message (client , mocker ):
23+ def test_phone_registration_sends_message (client , mocker , backend ):
2324 url = reverse ("phone-register" )
2425 phone_number = PHONE_NUMBER
2526 data = {"phone_number" : phone_number }
@@ -38,132 +39,164 @@ def test_phone_registration_sends_message(client, mocker):
3839 )
3940
4041
41- def test_security_code_session_token_verification_api (client ):
42- f .create_verification (
43- security_code = SECURITY_CODE ,
44- phone_number = PHONE_NUMBER ,
45- session_token = SESSION_TOKEN ,
46- )
47- url = reverse ("phone-verify" )
48- data = {
49- "phone_number" : PHONE_NUMBER ,
50- "security_code" : SECURITY_CODE ,
51- "session_token" : SESSION_TOKEN ,
52- }
53- response = client .json .post (url , data = data )
54- assert response .status_code == 200
55- assert response .data ["message" ] == "Security code is valid."
56-
57-
58- def test_phone_verification_with_incomplete_payload (client ):
59- f .create_verification (
60- security_code = SECURITY_CODE ,
61- phone_number = PHONE_NUMBER ,
62- session_token = SESSION_TOKEN ,
63- )
64- url = reverse ("phone-verify" )
65- data = {"phone_number" : PHONE_NUMBER }
66- response = client .json .post (url , data = data )
67- assert response .status_code == 400
68- response_data = json .loads (json .dumps (response .data ))
69- assert response_data ["session_token" ][0 ] == "This field is required."
70- assert response_data ["security_code" ][0 ] == "This field is required."
71-
72- data = {"security_code" : SECURITY_CODE }
73- response = client .json .post (url , data = data )
74- assert response .status_code == 400
75- response_data = json .loads (json .dumps (response .data ))
76- assert response_data ["phone_number" ][0 ] == "This field is required."
77-
78-
79- def test_phone_verification_with_incorrect_payload (client ):
80- f .create_verification (
81- security_code = SECURITY_CODE ,
82- phone_number = PHONE_NUMBER ,
83- session_token = SESSION_TOKEN ,
84- )
85- url = reverse ("phone-verify" )
86- # Payload with wrong session token
87- data = {
88- "phone_number" : PHONE_NUMBER ,
89- "security_code" : SECURITY_CODE ,
90- "session_token" : "wrong-session-token" ,
91- }
92- response = client .json .post (url , data = data )
93- response_data = json .loads (json .dumps (response .data ))
94- assert response .status_code == 400
95- assert response_data ["non_field_errors" ][0 ] == "Session Token mis-match"
96-
97- # Payload with wrong security code
98- data = {
99- "phone_number" : PHONE_NUMBER ,
100- "security_code" : "999999" ,
101- "session_token" : SESSION_TOKEN ,
102- }
103- response = client .json .post (url , data = data )
104- assert response .status_code == 400
105- response_data = json .loads (json .dumps (response .data ))
106- assert response_data ["non_field_errors" ][0 ] == "Security code is not valid"
107-
108- # Payload with incorrect phone_number
109- data = {
110- "phone_number" : "+13478379632" ,
111- "security_code" : SECURITY_CODE ,
112- "session_token" : SESSION_TOKEN ,
113- }
114- response = client .json .post (url , data = data )
115- assert response .status_code == 400
116- response_data = json .loads (json .dumps (response .data ))
117- assert response_data ["non_field_errors" ][0 ] == "Security code is not valid"
118-
119-
120- def test_check_security_code_expiry (client ):
121- f .create_verification (
122- security_code = SECURITY_CODE ,
123- phone_number = PHONE_NUMBER ,
124- session_token = SESSION_TOKEN ,
125- )
126- time .sleep (2 )
127- url = reverse ("phone-verify" )
128- data = {
129- "phone_number" : PHONE_NUMBER ,
130- "security_code" : SECURITY_CODE ,
131- "session_token" : SESSION_TOKEN ,
132- }
133- response = client .json .post (url , data = data )
134- assert response .status_code == 400
135- response_data = json .loads (json .dumps (response .data ))
136- assert response_data ["non_field_errors" ][0 ] == "Security code has expired"
137-
138-
139- def test_verified_security_code (client ):
140- f .create_verification (
141- security_code = SECURITY_CODE ,
142- phone_number = PHONE_NUMBER ,
143- session_token = SESSION_TOKEN ,
144- is_verified = True ,
145- )
146- url = reverse ("phone-verify" )
147- data = {
148- "phone_number" : PHONE_NUMBER ,
149- "security_code" : SECURITY_CODE ,
150- "session_token" : SESSION_TOKEN ,
151- }
152-
153- # Security code verification is restricted to one time
154- settings .DJANGO_SETTINGS ["PHONE_VERIFICATION" ][
155- "VERIFY_SECURITY_CODE_ONLY_ONCE"
156- ] = True
157- response = client .json .post (url , data = data )
158- response_data = json .loads (json .dumps (response .data ))
159- assert response .status_code == 400
160- assert response_data ["non_field_errors" ][0 ] == "Security code is already verified"
161-
162- # Security code verification is not restricted to one time
163- settings .DJANGO_SETTINGS ["PHONE_VERIFICATION" ][
164- "VERIFY_SECURITY_CODE_ONLY_ONCE"
165- ] = False
166- response = client .json .post (url , data = data )
167- response_data = json .loads (json .dumps (response .data ))
168- assert response .status_code == 200
169- assert response .data ["message" ] == "Security code is valid."
42+ def test_security_code_session_token_verification_api (client , backend ):
43+ with override_settings (PHONE_VERIFICATION = backend ):
44+ f .create_verification (
45+ security_code = SECURITY_CODE ,
46+ phone_number = PHONE_NUMBER ,
47+ session_token = SESSION_TOKEN ,
48+ )
49+ url = reverse ("phone-verify" )
50+ data = {
51+ "phone_number" : PHONE_NUMBER ,
52+ "security_code" : SECURITY_CODE ,
53+ "session_token" : SESSION_TOKEN ,
54+ }
55+ response = client .json .post (url , data = data )
56+ assert response .status_code == 200
57+ assert response .data ["message" ] == "Security code is valid."
58+
59+
60+ def test_phone_verification_with_incomplete_payload (client , backend ):
61+ with override_settings (PHONE_VERIFICATION = backend ):
62+ f .create_verification (
63+ security_code = SECURITY_CODE ,
64+ phone_number = PHONE_NUMBER ,
65+ session_token = SESSION_TOKEN ,
66+ )
67+ url = reverse ("phone-verify" )
68+ data = {"phone_number" : PHONE_NUMBER }
69+ response = client .json .post (url , data = data )
70+ assert response .status_code == 400
71+ response_data = json .loads (json .dumps (response .data ))
72+ assert response_data ["session_token" ][0 ] == "This field is required."
73+ assert response_data ["security_code" ][0 ] == "This field is required."
74+
75+ data = {"security_code" : SECURITY_CODE }
76+ response = client .json .post (url , data = data )
77+ assert response .status_code == 400
78+ response_data = json .loads (json .dumps (response .data ))
79+ assert response_data ["phone_number" ][0 ] == "This field is required."
80+
81+
82+ def test_phone_verification_with_incorrect_payload (client , backend ):
83+ with override_settings (PHONE_VERIFICATION = backend ):
84+ f .create_verification (
85+ security_code = SECURITY_CODE ,
86+ phone_number = PHONE_NUMBER ,
87+ session_token = SESSION_TOKEN ,
88+ )
89+ url = reverse ("phone-verify" )
90+ # Payload with wrong session token
91+ data = {
92+ "phone_number" : PHONE_NUMBER ,
93+ "security_code" : SECURITY_CODE ,
94+ "session_token" : "wrong-session-token" ,
95+ }
96+ response = client .json .post (url , data = data )
97+ response_data = json .loads (json .dumps (response .data ))
98+
99+ backend_cls = _get_backend_cls (backend )
100+
101+ if backend_cls in backends :
102+ assert response .status_code == 400
103+ assert response_data ["non_field_errors" ][0 ] == "Session Token mis-match"
104+ elif backend_cls in sandbox_backends :
105+ # Sandbox Backend returns a 200 status code as it does not check the payload information
106+ assert response .status_code == 200
107+
108+ # Payload with wrong security code
109+ data = {
110+ "phone_number" : PHONE_NUMBER ,
111+ "security_code" : "999999" ,
112+ "session_token" : SESSION_TOKEN ,
113+ }
114+ response = client .json .post (url , data = data )
115+ response_data = json .loads (json .dumps (response .data ))
116+ if backend_cls in backends :
117+ assert response .status_code == 400
118+ assert response_data ["non_field_errors" ][0 ] == "Security code is not valid"
119+ elif backend_cls in sandbox_backends :
120+ # Sandbox Backend returns a 200 status code as it does not check the payload information
121+ assert response .status_code == 200
122+
123+ # Payload with incorrect phone_number
124+ data = {
125+ "phone_number" : "+13478379632" ,
126+ "security_code" : SECURITY_CODE ,
127+ "session_token" : SESSION_TOKEN ,
128+ }
129+ response = client .json .post (url , data = data )
130+ response_data = json .loads (json .dumps (response .data ))
131+ if backend_cls in backends :
132+ assert response .status_code == 400
133+ assert response_data ["non_field_errors" ][0 ] == "Security code is not valid"
134+ elif backend_cls in sandbox_backends :
135+ # Sandbox Backend returns a 200 status code as it does not check the payload information
136+ assert response .status_code == 200
137+
138+
139+ def test_check_security_code_expiry (client , backend ):
140+ with override_settings (PHONE_VERIFICATION = backend ):
141+ f .create_verification (
142+ security_code = SECURITY_CODE ,
143+ phone_number = PHONE_NUMBER ,
144+ session_token = SESSION_TOKEN ,
145+ )
146+ time .sleep (2 )
147+ url = reverse ("phone-verify" )
148+ data = {
149+ "phone_number" : PHONE_NUMBER ,
150+ "security_code" : SECURITY_CODE ,
151+ "session_token" : SESSION_TOKEN ,
152+ }
153+ response = client .json .post (url , data = data )
154+ response_data = json .loads (json .dumps (response .data ))
155+
156+ backend_cls = _get_backend_cls (backend )
157+
158+ if backend_cls in backends :
159+ assert response .status_code == 400
160+ assert response_data ["non_field_errors" ][0 ] == "Security code has expired"
161+ elif backend_cls in sandbox_backends :
162+ # Sandbox Backend returns a 200 status code when verifying security code expiry
163+ assert response .status_code == 200
164+
165+
166+ def test_verified_security_code (client , backend ):
167+ with override_settings (PHONE_VERIFICATION = backend ):
168+ f .create_verification (
169+ security_code = SECURITY_CODE ,
170+ phone_number = PHONE_NUMBER ,
171+ session_token = SESSION_TOKEN ,
172+ is_verified = True ,
173+ )
174+ url = reverse ("phone-verify" )
175+ data = {
176+ "phone_number" : PHONE_NUMBER ,
177+ "security_code" : SECURITY_CODE ,
178+ "session_token" : SESSION_TOKEN ,
179+ }
180+
181+ backend_cls = _get_backend_cls (backend )
182+
183+ # Security code verification is restricted to one time
184+ backend ["VERIFY_SECURITY_CODE_ONLY_ONCE" ] = True
185+ response = client .json .post (url , data = data )
186+ response_data = json .loads (json .dumps (response .data ))
187+ if backend_cls in backends :
188+ assert response .status_code == 400
189+ assert (
190+ response_data ["non_field_errors" ][0 ]
191+ == "Security code is already verified"
192+ )
193+ elif backend_cls in sandbox_backends :
194+ # Sandbox Backend returns a 200 status code when verifying security code
195+ assert response .status_code == 200
196+
197+ # Security code verification is not restricted to one time
198+ backend ["VERIFY_SECURITY_CODE_ONLY_ONCE" ] = False
199+ response = client .json .post (url , data = data )
200+ response_data = json .loads (json .dumps (response .data ))
201+ assert response .status_code == 200
202+ assert response .data ["message" ] == "Security code is valid."
0 commit comments