1
+ import json
2
+
1
3
from django .test import TestCase
2
4
from django .contrib .auth import get_user_model
3
5
from django .test .utils import override_settings
@@ -34,9 +36,19 @@ def setUp(self):
34
36
client_id = '123123123' ,
35
37
secret = '321321321' ,
36
38
)
39
+
40
+ twitter_social_app = SocialApp .objects .create (
41
+ provider = 'twitter' ,
42
+ name = 'Twitter' ,
43
+ client_id = '11223344' ,
44
+ secret = '55667788' ,
45
+ )
46
+
37
47
site = Site .objects .get_current ()
38
48
social_app .sites .add (site )
49
+ twitter_social_app .sites .add (site )
39
50
self .graph_api_url = GRAPH_API_URL + '/me'
51
+ self .twitter_url = 'http://twitter.com/foobarme'
40
52
41
53
@responses .activate
42
54
def test_failed_social_auth (self ):
@@ -57,11 +69,24 @@ def test_failed_social_auth(self):
57
69
@responses .activate
58
70
def test_social_auth (self ):
59
71
# fake response for facebook call
60
- resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\ /\\ /www.facebook.com\\ /john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true}' # noqa
72
+ resp_body = {
73
+ "id" : "123123123123" ,
74
+ "first_name" : "John" ,
75
+ "gender" : "male" ,
76
+ "last_name" : "Smith" ,
77
+ "link" : "https://www.facebook.com/john.smith" ,
78
+ "locale" : "en_US" ,
79
+ "name" : "John Smith" ,
80
+ "timezone" : 2 ,
81
+ "updated_time" : "2014-08-13T10:14:38+0000" ,
82
+ "username" : "john.smith" ,
83
+ "verified" : True
84
+ }
85
+
61
86
responses .add (
62
87
responses .GET ,
63
88
self .graph_api_url ,
64
- body = resp_body ,
89
+ body = json . dumps ( resp_body ) ,
65
90
status = 200 ,
66
91
content_type = 'application/json'
67
92
)
@@ -80,18 +105,145 @@ def test_social_auth(self):
80
105
self .assertIn ('key' , self .response .json .keys ())
81
106
self .assertEqual (get_user_model ().objects .all ().count (), users_count + 1 )
82
107
108
+ def _twitter_social_auth (self ):
109
+ # fake response for twitter call
110
+ resp_body = {
111
+ "id" : "123123123123" ,
112
+ }
113
+
114
+ responses .add (
115
+ responses .GET ,
116
+ 'https://api.twitter.com/1.1/account/verify_credentials.json' ,
117
+ body = json .dumps (resp_body ),
118
+ status = 200 ,
119
+ content_type = 'application/json'
120
+ )
121
+
122
+ users_count = get_user_model ().objects .all ().count ()
123
+ payload = {
124
+ 'access_token' : 'abc123' ,
125
+ 'token_secret' : '1111222233334444'
126
+ }
127
+
128
+ self .post (self .tw_login_url , data = payload )
129
+
130
+ self .assertIn ('key' , self .response .json .keys ())
131
+ self .assertEqual (get_user_model ().objects .all ().count (), users_count + 1 )
132
+
133
+ # make sure that second request will not create a new user
134
+ self .post (self .tw_login_url , data = payload , status_code = 200 )
135
+ self .assertIn ('key' , self .response .json .keys ())
136
+ self .assertEqual (get_user_model ().objects .all ().count (), users_count + 1 )
137
+
138
+ @responses .activate
139
+ @override_settings (SOCIALACCOUNT_AUTO_SIGNUP = True )
140
+ def test_twitter_social_auth (self ):
141
+ self ._twitter_social_auth ()
142
+
143
+ @responses .activate
144
+ @override_settings (SOCIALACCOUNT_AUTO_SIGNUP = False )
145
+ def test_twitter_social_auth_without_auto_singup (self ):
146
+ self ._twitter_social_auth ()
147
+
148
+ @responses .activate
149
+ def test_twitter_social_auth_request_error (self ):
150
+ # fake response for twitter call
151
+ resp_body = {
152
+ "id" : "123123123123" ,
153
+ }
154
+
155
+ responses .add (
156
+ responses .GET ,
157
+ 'https://api.twitter.com/1.1/account/verify_credentials.json' ,
158
+ body = json .dumps (resp_body ),
159
+ status = 400 ,
160
+ content_type = 'application/json'
161
+ )
162
+
163
+ users_count = get_user_model ().objects .all ().count ()
164
+ payload = {
165
+ 'access_token' : 'abc123' ,
166
+ 'token_secret' : '1111222233334444'
167
+ }
168
+
169
+ self .post (self .tw_login_url , data = payload , status_code = 400 )
170
+ self .assertNotIn ('key' , self .response .json .keys ())
171
+ self .assertEqual (get_user_model ().objects .all ().count (), users_count )
172
+
173
+ @responses .activate
174
+ def test_twitter_social_auth_no_view_in_context (self ):
175
+ # fake response for twitter call
176
+ resp_body = {
177
+ "id" : "123123123123" ,
178
+ }
179
+
180
+ responses .add (
181
+ responses .GET ,
182
+ 'https://api.twitter.com/1.1/account/verify_credentials.json' ,
183
+ body = json .dumps (resp_body ),
184
+ status = 400 ,
185
+ content_type = 'application/json'
186
+ )
187
+
188
+ users_count = get_user_model ().objects .all ().count ()
189
+ payload = {
190
+ 'access_token' : 'abc123' ,
191
+ 'token_secret' : '1111222233334444'
192
+ }
193
+
194
+ self .post (self .tw_login_no_view_url , data = payload , status_code = 400 )
195
+ self .assertEqual (get_user_model ().objects .all ().count (), users_count )
196
+
197
+ @responses .activate
198
+ def test_twitter_social_auth_no_adapter (self ):
199
+ # fake response for twitter call
200
+ resp_body = {
201
+ "id" : "123123123123" ,
202
+ }
203
+
204
+ responses .add (
205
+ responses .GET ,
206
+ 'https://api.twitter.com/1.1/account/verify_credentials.json' ,
207
+ body = json .dumps (resp_body ),
208
+ status = 400 ,
209
+ content_type = 'application/json'
210
+ )
211
+
212
+ users_count = get_user_model ().objects .all ().count ()
213
+ payload = {
214
+ 'access_token' : 'abc123' ,
215
+ 'token_secret' : '1111222233334444'
216
+ }
217
+
218
+ self .post (self .tw_login_no_adapter_url , data = payload , status_code = 400 )
219
+ self .assertEqual (get_user_model ().objects .all ().count (), users_count )
220
+
83
221
@responses .activate
84
222
@override_settings (
85
223
ACCOUNT_EMAIL_VERIFICATION = 'mandatory' ,
86
224
ACCOUNT_EMAIL_REQUIRED = True ,
87
225
REST_SESSION_LOGIN = False
88
226
)
89
227
def test_edge_case (self ):
90
- resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\ /\\ /www.facebook.com\\ /john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true,"email":"%s"}' # noqa
228
+ resp_body = {
229
+ "id" : "123123123123" ,
230
+ "first_name" : "John" ,
231
+ "gender" : "male" ,
232
+ "last_name" : "Smith" ,
233
+ "link" : "https://www.facebook.com/john.smith" ,
234
+ "locale" : "en_US" ,
235
+ "name" : "John Smith" ,
236
+ "timezone" : 2 ,
237
+ "updated_time" : "2014-08-13T10:14:38+0000" ,
238
+ "username" : "john.smith" ,
239
+ "verified" : True ,
240
+ "email" : self .EMAIL
241
+ }
242
+
91
243
responses .add (
92
244
responses .GET ,
93
245
self .graph_api_url ,
94
- body = resp_body % self . EMAIL ,
246
+ body = json . dumps ( resp_body ) ,
95
247
status = 200 ,
96
248
content_type = 'application/json'
97
249
)
0 commit comments