1
- from rest_framework .test import APISimpleTestCase , APIClient , APIRequestFactory
1
+ from rest_framework .test import APITestCase , APIClient , APIRequestFactory
2
2
from rest_framework .reverse import reverse
3
3
from rest_framework import status
4
4
from unittest .mock import patch , Mock , PropertyMock
14
14
from todo .constants .messages import AppMessages , AuthErrorMessages
15
15
16
16
17
- class GoogleLoginViewTests (APISimpleTestCase ):
17
+ class GoogleLoginViewTests (APITestCase ):
18
18
def setUp (self ):
19
19
super ().setUp ()
20
20
self .client = APIClient ()
@@ -59,46 +59,54 @@ def test_get_with_redirect_url(self, mock_get_auth_url):
59
59
mock_get_auth_url .assert_called_once_with (redirect_url )
60
60
61
61
62
- class GoogleCallbackViewTests (APISimpleTestCase ):
62
+ class GoogleCallbackViewTests (APITestCase ):
63
63
def setUp (self ):
64
64
super ().setUp ()
65
65
self .client = APIClient ()
66
66
self .url = reverse ("google_callback" )
67
67
self .factory = APIRequestFactory ()
68
68
self .view = GoogleCallbackView .as_view ()
69
69
70
- def test_get_returns_error_for_oauth_error (self ):
70
+ def test_get_redirects_for_oauth_error (self ):
71
71
error = "access_denied"
72
- request = self .factory .get (f"{ self .url } ?error={ error } " )
72
+ response = self .client .get (f"{ self .url } ?error={ error } " )
73
73
74
- response = self .view (request )
74
+ self .assertEqual (response .status_code , status .HTTP_302_FOUND )
75
+ self .assertIn ("error=access_denied" , response .url )
75
76
76
- self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
77
- self .assertEqual (response .data ["message" ], error )
78
- self .assertEqual (response .data ["errors" ][0 ]["detail" ], error )
77
+ def test_get_redirects_for_missing_code (self ):
78
+ response = self .client .get (self .url )
79
+
80
+ self .assertEqual (response .status_code , status .HTTP_302_FOUND )
81
+ self .assertIn ("error=missing_parameters" , response .url )
82
+
83
+ def test_get_redirects_for_valid_code_and_state (self ):
84
+ response = self .client .get (f"{ self .url } ?code=test_code&state=test_state" )
79
85
80
- def test_get_returns_error_for_missing_code (self ):
81
- request = self .factory .get (self .url )
86
+ self .assertEqual (response .status_code , status .HTTP_302_FOUND )
87
+ self .assertIn ("code=test_code" , response .url )
88
+ self .assertIn ("state=test_state" , response .url )
82
89
83
- response = self .view (request )
90
+ def test_post_returns_error_for_missing_code (self ):
91
+ response = self .client .post (self .url , {})
84
92
85
93
self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
86
94
self .assertEqual (response .data ["message" ], "No authorization code received from Google" )
87
- self .assertEqual (response .data ["errors" ][0 ]["detail" ], "No authorization code received from Google" )
88
95
89
- def test_get_returns_error_for_invalid_state (self ):
90
- request = self .factory .get (f"{ self .url } ?code=test_code&state=invalid_state" )
91
- request .session = {"oauth_state" : "different_state" }
96
+ def test_post_returns_error_for_invalid_state (self ):
97
+
98
+ session = self .client .session
99
+ session ["oauth_state" ] = "different_state"
100
+ session .save ()
92
101
93
- response = self .view ( request )
102
+ response = self .client . post ( self . url , { "code" : "test_code" , "state" : "invalid_state" } )
94
103
95
104
self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
96
105
self .assertEqual (response .data ["message" ], "Invalid state parameter" )
97
- self .assertEqual (response .data ["errors" ][0 ]["detail" ], "Invalid state parameter" )
98
106
99
107
@patch ("todo.services.google_oauth_service.GoogleOAuthService.handle_callback" )
100
108
@patch ("todo.services.user_service.UserService.create_or_update_user" )
101
- def test_get_handles_callback_successfully (self , mock_create_user , mock_handle_callback ):
109
+ def test_post_handles_callback_successfully (self , mock_create_user , mock_handle_callback ):
102
110
mock_google_data = {
103
111
"id" : "test_google_id" ,
104
112
@@ -115,70 +123,26 @@ def test_get_handles_callback_successfully(self, mock_create_user, mock_handle_c
115
123
mock_handle_callback .return_value = mock_google_data
116
124
mock_create_user .return_value = mock_user
117
125
118
- request = self .factory .get (f"{ self .url } ?code=test_code&state=test_state" )
119
- request .session = {"oauth_state" : "test_state" }
126
+ session = self .client .session
127
+ session ["oauth_state" ] = "test_state"
128
+ session .save ()
120
129
121
- response = self .view ( request )
130
+ response = self .client . post ( self . url , { "code" : "test_code" , "state" : "test_state" } )
122
131
123
132
self .assertEqual (response .status_code , status .HTTP_200_OK )
124
- self .assertIn ("✅ Google OAuth Login Successful!" , response .content .decode ())
125
- self .assertIn (str (mock_user .id ), response .content .decode ())
126
- self .assertIn (mock_user .name , response .content .decode ())
127
- self .assertIn (mock_user .email_id , response .content .decode ())
128
- self .assertIn (mock_user .google_id , response .content .decode ())
133
+ self .assertEqual (response .data ["data" ]["user" ]["id" ], user_id )
134
+ self .assertEqual (response .data ["data" ]["user" ]["name" ], mock_user .name )
135
+ self .assertEqual (response .data ["data" ]["user" ]["email" ], mock_user .email_id )
136
+ self .assertEqual (response .data ["data" ]["user" ]["google_id" ], mock_user .google_id )
129
137
self .assertIn ("ext-access" , response .cookies )
130
138
self .assertIn ("ext-refresh" , response .cookies )
131
- self .assertNotIn ("oauth_state" , request .session )
139
+ self .assertNotIn ("oauth_state" , self . client .session )
132
140
133
141
134
- class GoogleAuthStatusViewTests (APISimpleTestCase ):
135
- def setUp (self ):
136
- super ().setUp ()
137
- self .client = APIClient ()
138
- self .url = reverse ("google_status" )
139
142
140
- def test_get_returns_401_when_no_access_token (self ):
141
- response = self .client .get (self .url )
142
143
143
- self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
144
- self .assertEqual (response .data ["message" ], AuthErrorMessages .NO_ACCESS_TOKEN )
145
- self .assertEqual (response .data ["authenticated" ], False )
146
- self .assertEqual (response .data ["statusCode" ], status .HTTP_401_UNAUTHORIZED )
147
144
148
- @patch ("todo.utils.google_jwt_utils.validate_google_access_token" )
149
- @patch ("todo.services.user_service.UserService.get_user_by_id" )
150
- def test_get_returns_user_info_when_authenticated (self , mock_get_user , mock_validate_token ):
151
- user_id = str (ObjectId ())
152
- user_data = {
153
- "user_id" : user_id ,
154
- "google_id" : "test_google_id" ,
155
-
156
- "name" : "Test User" ,
157
- }
158
- mock_validate_token .return_value = user_data
159
-
160
- mock_user = Mock ()
161
- mock_user .id = ObjectId (user_id )
162
- mock_user .google_id = "test_google_id"
163
- mock_user .
email_id = "[email protected] "
164
- mock_user .name = "Test User"
165
- type(mock_user ).id = PropertyMock (return_value = ObjectId (user_id ))
166
-
167
- mock_get_user .return_value = mock_user
168
-
169
- tokens = generate_google_token_pair (user_data )
170
- self .client .cookies ["ext-access" ] = tokens ["access_token" ]
171
-
172
- response = self .client .get (self .url , HTTP_ACCEPT = "application/json" )
173
-
174
- self .assertEqual (response .status_code , status .HTTP_200_OK )
175
- self .assertEqual (response .data ["data" ]["user" ]["id" ], user_id )
176
- self .assertEqual (response .data ["data" ]["user" ]["email" ], mock_user .email_id )
177
- self .assertEqual (response .data ["data" ]["user" ]["name" ], mock_user .name )
178
- self .assertEqual (response .data ["data" ]["user" ]["google_id" ], mock_user .google_id )
179
-
180
-
181
- class GoogleRefreshViewTests (APISimpleTestCase ):
145
+ class GoogleRefreshViewTests (APITestCase ):
182
146
def setUp (self ):
183
147
super ().setUp ()
184
148
self .client = APIClient ()
@@ -213,7 +177,7 @@ def test_get_refreshes_token_successfully(self, mock_validate_token):
213
177
self .assertIn ("ext-access" , response .cookies )
214
178
215
179
216
- class GoogleLogoutViewTests (APISimpleTestCase ):
180
+ class GoogleLogoutViewTests (APITestCase ):
217
181
def setUp (self ):
218
182
super ().setUp ()
219
183
self .client = APIClient ()
@@ -231,7 +195,7 @@ def test_get_returns_success_and_clears_cookies(self):
231
195
self .client .cookies ["ext-refresh" ] = tokens ["refresh_token" ]
232
196
233
197
response = self .client .get (self .url , HTTP_ACCEPT = "application/json" )
234
-
198
+
235
199
self .assertEqual (response .status_code , status .HTTP_200_OK )
236
200
self .assertEqual (response .data ["data" ]["success" ], True )
237
201
self .assertEqual (response .data ["message" ], AppMessages .GOOGLE_LOGOUT_SUCCESS )
0 commit comments