1
+ import json
2
+ from unittest .mock import patch
3
+
4
+ from django .conf import settings
5
+ from django .contrib import auth
1
6
from django .test import Client , RequestFactory , TestCase
7
+ from django .urls import reverse
2
8
9
+ from djangoblog .utils import get_sha256
3
10
from oauth .models import OAuthConfig
11
+ from oauth .oauthmanager import BaseOauthManager
4
12
5
13
6
14
# Create your tests here.
@@ -23,3 +31,219 @@ def test_oauth_login_test(self):
23
31
response = self .client .get ('/oauth/authorize?type=weibo&code=code' )
24
32
self .assertEqual (response .status_code , 302 )
25
33
self .assertEqual (response .url , '/' )
34
+
35
+
36
+ class OauthLoginTest (TestCase ):
37
+ def setUp (self ) -> None :
38
+ self .client = Client ()
39
+ self .factory = RequestFactory ()
40
+ self .apps = self .init_apps ()
41
+
42
+ def init_apps (self ):
43
+ applications = [p () for p in BaseOauthManager .__subclasses__ ()]
44
+ for application in applications :
45
+ c = OAuthConfig ()
46
+ c .type = application .ICON_NAME .lower ()
47
+ c .appkey = 'appkey'
48
+ c .appsecret = 'appsecret'
49
+ c .save ()
50
+ return applications
51
+
52
+ def get_app_by_type (self , type ):
53
+ for app in self .apps :
54
+ if app .ICON_NAME .lower () == type :
55
+ return app
56
+
57
+ @patch ("oauth.oauthmanager.WBOauthManager.do_post" )
58
+ @patch ("oauth.oauthmanager.WBOauthManager.do_get" )
59
+ def test_weibo_login (self , mock_do_get , mock_do_post ):
60
+ weibo_app = self .get_app_by_type ('weibo' )
61
+ assert weibo_app
62
+ url = weibo_app .get_authorization_url ()
63
+ mock_do_post .return_value = json .dumps ({"access_token" : "access_token" ,
64
+ "uid" : "uid"
65
+ })
66
+ mock_do_get .return_value = json .dumps ({
67
+ "avatar_large" : "avatar_large" ,
68
+ "screen_name" : "screen_name" ,
69
+ "id" : "id" ,
70
+ "email" : "email" ,
71
+ })
72
+ userinfo = weibo_app .get_access_token_by_code ('code' )
73
+ self .assertEqual (userinfo .token , 'access_token' )
74
+ self .assertEqual (userinfo .openid , 'id' )
75
+
76
+ @patch ("oauth.oauthmanager.GoogleOauthManager.do_post" )
77
+ @patch ("oauth.oauthmanager.GoogleOauthManager.do_get" )
78
+ def test_google_login (self , mock_do_get , mock_do_post ):
79
+ google_app = self .get_app_by_type ('google' )
80
+ assert google_app
81
+ url = google_app .get_authorization_url ()
82
+ mock_do_post .return_value = json .dumps ({
83
+ "access_token" : "access_token" ,
84
+ "id_token" : "id_token" ,
85
+ })
86
+ mock_do_get .return_value = json .dumps ({
87
+ "picture" : "picture" ,
88
+ "name" : "name" ,
89
+ "sub" : "sub" ,
90
+ "email" : "email" ,
91
+ })
92
+ token = google_app .get_access_token_by_code ('code' )
93
+ userinfo = google_app .get_oauth_userinfo ()
94
+ self .assertEqual (userinfo .token , 'access_token' )
95
+ self .assertEqual (userinfo .openid , 'sub' )
96
+
97
+ @patch ("oauth.oauthmanager.GitHubOauthManager.do_post" )
98
+ @patch ("oauth.oauthmanager.GitHubOauthManager.do_get" )
99
+ def test_github_login (self , mock_do_get , mock_do_post ):
100
+ github_app = self .get_app_by_type ('github' )
101
+ assert github_app
102
+ url = github_app .get_authorization_url ()
103
+ self .assertTrue ("github.com" in url )
104
+ self .assertTrue ("client_id" in url )
105
+ mock_do_post .return_value = "access_token=gho_16C7e42F292c6912E7710c838347Ae178B4a&scope=repo%2Cgist&token_type=bearer"
106
+ mock_do_get .return_value = json .dumps ({
107
+ "avatar_url" : "avatar_url" ,
108
+ "name" : "name" ,
109
+ "id" : "id" ,
110
+ "email" : "email" ,
111
+ })
112
+ token = github_app .get_access_token_by_code ('code' )
113
+ userinfo = github_app .get_oauth_userinfo ()
114
+ self .assertEqual (userinfo .token , 'gho_16C7e42F292c6912E7710c838347Ae178B4a' )
115
+ self .assertEqual (userinfo .openid , 'id' )
116
+
117
+ @patch ("oauth.oauthmanager.FaceBookOauthManager.do_post" )
118
+ @patch ("oauth.oauthmanager.FaceBookOauthManager.do_get" )
119
+ def test_facebook_login (self , mock_do_get , mock_do_post ):
120
+ facebook_app = self .get_app_by_type ('facebook' )
121
+ assert facebook_app
122
+ url = facebook_app .get_authorization_url ()
123
+ self .assertTrue ("facebook.com" in url )
124
+ mock_do_post .return_value = json .dumps ({
125
+ "access_token" : "access_token" ,
126
+ })
127
+ mock_do_get .return_value = json .dumps ({
128
+ "name" : "name" ,
129
+ "id" : "id" ,
130
+ "email" : "email" ,
131
+ "picture" : {
132
+ "data" : {
133
+ "url" : "url"
134
+ }
135
+ }
136
+ })
137
+ token = facebook_app .get_access_token_by_code ('code' )
138
+ userinfo = facebook_app .get_oauth_userinfo ()
139
+ self .assertEqual (userinfo .token , 'access_token' )
140
+
141
+ @patch ("oauth.oauthmanager.QQOauthManager.do_get" , side_effect = [
142
+ 'access_token=access_token&expires_in=3600' ,
143
+ 'callback({"client_id":"appid","openid":"openid"} );' ,
144
+ json .dumps ({
145
+ "nickname" : "nickname" ,
146
+ "email" : "email" ,
147
+ "figureurl" : "figureurl" ,
148
+ "openid" : "openid" ,
149
+ })
150
+ ])
151
+ def test_qq_login (self , mock_do_get ):
152
+ qq_app = self .get_app_by_type ('qq' )
153
+ assert qq_app
154
+ url = qq_app .get_authorization_url ()
155
+ self .assertTrue ("qq.com" in url )
156
+ token = qq_app .get_access_token_by_code ('code' )
157
+ userinfo = qq_app .get_oauth_userinfo ()
158
+ self .assertEqual (userinfo .token , 'access_token' )
159
+
160
+ @patch ("oauth.oauthmanager.WBOauthManager.do_post" )
161
+ @patch ("oauth.oauthmanager.WBOauthManager.do_get" )
162
+ def test_weibo_authoriz_login_with_email (self , mock_do_get , mock_do_post ):
163
+
164
+ mock_do_post .return_value = json .dumps ({"access_token" : "access_token" ,
165
+ "uid" : "uid"
166
+ })
167
+ mock_user_info = {
168
+ "avatar_large" : "avatar_large" ,
169
+ "screen_name" : "screen_name1" ,
170
+ "id" : "id" ,
171
+ "email" : "email" ,
172
+ }
173
+ mock_do_get .return_value = json .dumps (mock_user_info )
174
+
175
+ response = self .client .get ('/oauth/oauthlogin?type=weibo' )
176
+ self .assertEqual (response .status_code , 302 )
177
+ self .assertTrue ("api.weibo.com" in response .url )
178
+
179
+ response = self .client .get ('/oauth/authorize?type=weibo&code=code' )
180
+ self .assertEqual (response .status_code , 302 )
181
+ self .assertEqual (response .url , '/' )
182
+
183
+ user = auth .get_user (self .client )
184
+ assert user .is_authenticated
185
+ self .assertTrue (user .is_authenticated )
186
+ self .assertEqual (user .username , mock_user_info ['screen_name' ])
187
+ self .assertEqual (user .email , mock_user_info ['email' ])
188
+ self .client .logout ()
189
+
190
+ response = self .client .get ('/oauth/authorize?type=weibo&code=code' )
191
+ self .assertEqual (response .status_code , 302 )
192
+ self .assertEqual (response .url , '/' )
193
+
194
+ user = auth .get_user (self .client )
195
+ assert user .is_authenticated
196
+ self .assertTrue (user .is_authenticated )
197
+ self .assertEqual (user .username , mock_user_info ['screen_name' ])
198
+ self .assertEqual (user .email , mock_user_info ['email' ])
199
+
200
+ @patch ("oauth.oauthmanager.WBOauthManager.do_post" )
201
+ @patch ("oauth.oauthmanager.WBOauthManager.do_get" )
202
+ def test_weibo_authoriz_login_without_email (self , mock_do_get , mock_do_post ):
203
+
204
+ mock_do_post .return_value = json .dumps ({"access_token" : "access_token" ,
205
+ "uid" : "uid"
206
+ })
207
+ mock_user_info = {
208
+ "avatar_large" : "avatar_large" ,
209
+ "screen_name" : "screen_name1" ,
210
+ "id" : "id" ,
211
+ }
212
+ mock_do_get .return_value = json .dumps (mock_user_info )
213
+
214
+ response = self .client .get ('/oauth/oauthlogin?type=weibo' )
215
+ self .assertEqual (response .status_code , 302 )
216
+ self .assertTrue ("api.weibo.com" in response .url )
217
+
218
+ response = self .client .get ('/oauth/authorize?type=weibo&code=code' )
219
+
220
+ self .assertEqual (response .status_code , 302 )
221
+
222
+ oauth_user_id = int (response .url .split ('/' )[- 1 ].split ('.' )[0 ])
223
+ self .assertEqual (response .url , f'/oauth/requireemail/{ oauth_user_id } .html' )
224
+
225
+ response = self .
client .
post (
response .
url , {
'email' :
'[email protected] ' ,
'oauthid' :
oauth_user_id })
226
+
227
+ self .assertEqual (response .status_code , 302 )
228
+ sign = get_sha256 (settings .SECRET_KEY +
229
+ str (1 ) + settings .SECRET_KEY )
230
+
231
+ url = reverse ('oauth:bindsuccess' , kwargs = {
232
+ 'oauthid' : oauth_user_id ,
233
+ })
234
+ self .assertEqual (response .url , f'{ url } ?type=email' )
235
+
236
+ path = reverse ('oauth:email_confirm' , kwargs = {
237
+ 'id' : oauth_user_id ,
238
+ 'sign' : sign
239
+ })
240
+ response = self .client .get (path )
241
+ self .assertEqual (response .status_code , 302 )
242
+ self .assertEqual (response .url , f'/oauth/bindsuccess/{ oauth_user_id } .html?type=success' )
243
+ user = auth .get_user (self .client )
244
+ from oauth .models import OAuthUser
245
+ oauth_user = OAuthUser .objects .get (author = user )
246
+ self .assertTrue (user .is_authenticated )
247
+ self .assertEqual (user .username , mock_user_info ['screen_name' ])
248
+ self .
assertEqual (
user .
email ,
'[email protected] ' )
249
+ self .assertEqual (oauth_user .pk , oauth_user_id )
0 commit comments