@@ -34,7 +34,6 @@ def __init__(self, hs):
34
34
35
35
36
36
class AuthTestCase (unittest .TestCase ):
37
-
38
37
@defer .inlineCallbacks
39
38
def setUp (self ):
40
39
self .state_handler = Mock ()
@@ -53,11 +52,7 @@ def setUp(self):
53
52
54
53
@defer .inlineCallbacks
55
54
def test_get_user_by_req_user_valid_token (self ):
56
- user_info = {
57
- "name" : self .test_user ,
58
- "token_id" : "ditto" ,
59
- "device_id" : "device" ,
60
- }
55
+ user_info = {"name" : self .test_user , "token_id" : "ditto" , "device_id" : "device" }
61
56
self .store .get_user_by_access_token = Mock (return_value = user_info )
62
57
63
58
request = Mock (args = {})
@@ -76,10 +71,7 @@ def test_get_user_by_req_user_bad_token(self):
76
71
self .failureResultOf (d , AuthError )
77
72
78
73
def test_get_user_by_req_user_missing_token (self ):
79
- user_info = {
80
- "name" : self .test_user ,
81
- "token_id" : "ditto" ,
82
- }
74
+ user_info = {"name" : self .test_user , "token_id" : "ditto" }
83
75
self .store .get_user_by_access_token = Mock (return_value = user_info )
84
76
85
77
request = Mock (args = {})
@@ -90,8 +82,7 @@ def test_get_user_by_req_user_missing_token(self):
90
82
@defer .inlineCallbacks
91
83
def test_get_user_by_req_appservice_valid_token (self ):
92
84
app_service = Mock (
93
- token = "foobar" , url = "a_url" , sender = self .test_user ,
94
- ip_range_whitelist = None ,
85
+ token = "foobar" , url = "a_url" , sender = self .test_user , ip_range_whitelist = None
95
86
)
96
87
self .store .get_app_service_by_token = Mock (return_value = app_service )
97
88
self .store .get_user_by_access_token = Mock (return_value = None )
@@ -106,8 +97,11 @@ def test_get_user_by_req_appservice_valid_token(self):
106
97
@defer .inlineCallbacks
107
98
def test_get_user_by_req_appservice_valid_token_good_ip (self ):
108
99
from netaddr import IPSet
100
+
109
101
app_service = Mock (
110
- token = "foobar" , url = "a_url" , sender = self .test_user ,
102
+ token = "foobar" ,
103
+ url = "a_url" ,
104
+ sender = self .test_user ,
111
105
ip_range_whitelist = IPSet (["192.168/16" ]),
112
106
)
113
107
self .store .get_app_service_by_token = Mock (return_value = app_service )
@@ -122,8 +116,11 @@ def test_get_user_by_req_appservice_valid_token_good_ip(self):
122
116
123
117
def test_get_user_by_req_appservice_valid_token_bad_ip (self ):
124
118
from netaddr import IPSet
119
+
125
120
app_service = Mock (
126
- token = "foobar" , url = "a_url" , sender = self .test_user ,
121
+ token = "foobar" ,
122
+ url = "a_url" ,
123
+ sender = self .test_user ,
127
124
ip_range_whitelist = IPSet (["192.168/16" ]),
128
125
)
129
126
self .store .get_app_service_by_token = Mock (return_value = app_service )
@@ -160,8 +157,7 @@ def test_get_user_by_req_appservice_missing_token(self):
160
157
def test_get_user_by_req_appservice_valid_token_valid_user_id (self ):
161
158
masquerading_user_id = b"@doppelganger:matrix.org"
162
159
app_service = Mock (
163
- token = "foobar" , url = "a_url" , sender = self .test_user ,
164
- ip_range_whitelist = None ,
160
+ token = "foobar" , url = "a_url" , sender = self .test_user , ip_range_whitelist = None
165
161
)
166
162
app_service .is_interested_in_user = Mock (return_value = True )
167
163
self .store .get_app_service_by_token = Mock (return_value = app_service )
@@ -174,15 +170,13 @@ def test_get_user_by_req_appservice_valid_token_valid_user_id(self):
174
170
request .requestHeaders .getRawHeaders = mock_getRawHeaders ()
175
171
requester = yield self .auth .get_user_by_req (request )
176
172
self .assertEquals (
177
- requester .user .to_string (),
178
- masquerading_user_id .decode ('utf8' )
173
+ requester .user .to_string (), masquerading_user_id .decode ('utf8' )
179
174
)
180
175
181
176
def test_get_user_by_req_appservice_valid_token_bad_user_id (self ):
182
177
masquerading_user_id = b"@doppelganger:matrix.org"
183
178
app_service = Mock (
184
- token = "foobar" , url = "a_url" , sender = self .test_user ,
185
- ip_range_whitelist = None ,
179
+ token = "foobar" , url = "a_url" , sender = self .test_user , ip_range_whitelist = None
186
180
)
187
181
app_service .is_interested_in_user = Mock (return_value = False )
188
182
self .store .get_app_service_by_token = Mock (return_value = app_service )
@@ -201,17 +195,15 @@ def test_get_user_from_macaroon(self):
201
195
# TODO(danielwh): Remove this mock when we remove the
202
196
# get_user_by_access_token fallback.
203
197
self .store .get_user_by_access_token = Mock (
204
- return_value = {
205
- "name" : "@baldrick:matrix.org" ,
206
- "device_id" : "device" ,
207
- }
198
+ return_value = {"name" : "@baldrick:matrix.org" , "device_id" : "device" }
208
199
)
209
200
210
201
user_id = "@baldrick:matrix.org"
211
202
macaroon = pymacaroons .Macaroon (
212
203
location = self .hs .config .server_name ,
213
204
identifier = "key" ,
214
- key = self .hs .config .macaroon_secret_key )
205
+ key = self .hs .config .macaroon_secret_key ,
206
+ )
215
207
macaroon .add_first_party_caveat ("gen = 1" )
216
208
macaroon .add_first_party_caveat ("type = access" )
217
209
macaroon .add_first_party_caveat ("user_id = %s" % (user_id ,))
@@ -225,15 +217,14 @@ def test_get_user_from_macaroon(self):
225
217
226
218
@defer .inlineCallbacks
227
219
def test_get_guest_user_from_macaroon (self ):
228
- self .store .get_user_by_id = Mock (return_value = {
229
- "is_guest" : True ,
230
- })
220
+ self .store .get_user_by_id = Mock (return_value = {"is_guest" : True })
231
221
232
222
user_id = "@baldrick:matrix.org"
233
223
macaroon = pymacaroons .Macaroon (
234
224
location = self .hs .config .server_name ,
235
225
identifier = "key" ,
236
- key = self .hs .config .macaroon_secret_key )
226
+ key = self .hs .config .macaroon_secret_key ,
227
+ )
237
228
macaroon .add_first_party_caveat ("gen = 1" )
238
229
macaroon .add_first_party_caveat ("type = access" )
239
230
macaroon .add_first_party_caveat ("user_id = %s" % (user_id ,))
@@ -257,7 +248,8 @@ def test_get_user_from_macaroon_user_db_mismatch(self):
257
248
macaroon = pymacaroons .Macaroon (
258
249
location = self .hs .config .server_name ,
259
250
identifier = "key" ,
260
- key = self .hs .config .macaroon_secret_key )
251
+ key = self .hs .config .macaroon_secret_key ,
252
+ )
261
253
macaroon .add_first_party_caveat ("gen = 1" )
262
254
macaroon .add_first_party_caveat ("type = access" )
263
255
macaroon .add_first_party_caveat ("user_id = %s" % (user ,))
@@ -277,7 +269,8 @@ def test_get_user_from_macaroon_missing_caveat(self):
277
269
macaroon = pymacaroons .Macaroon (
278
270
location = self .hs .config .server_name ,
279
271
identifier = "key" ,
280
- key = self .hs .config .macaroon_secret_key )
272
+ key = self .hs .config .macaroon_secret_key ,
273
+ )
281
274
macaroon .add_first_party_caveat ("gen = 1" )
282
275
macaroon .add_first_party_caveat ("type = access" )
283
276
@@ -298,7 +291,8 @@ def test_get_user_from_macaroon_wrong_key(self):
298
291
macaroon = pymacaroons .Macaroon (
299
292
location = self .hs .config .server_name ,
300
293
identifier = "key" ,
301
- key = self .hs .config .macaroon_secret_key + "wrong" )
294
+ key = self .hs .config .macaroon_secret_key + "wrong" ,
295
+ )
302
296
macaroon .add_first_party_caveat ("gen = 1" )
303
297
macaroon .add_first_party_caveat ("type = access" )
304
298
macaroon .add_first_party_caveat ("user_id = %s" % (user ,))
@@ -320,7 +314,8 @@ def test_get_user_from_macaroon_unknown_caveat(self):
320
314
macaroon = pymacaroons .Macaroon (
321
315
location = self .hs .config .server_name ,
322
316
identifier = "key" ,
323
- key = self .hs .config .macaroon_secret_key )
317
+ key = self .hs .config .macaroon_secret_key ,
318
+ )
324
319
macaroon .add_first_party_caveat ("gen = 1" )
325
320
macaroon .add_first_party_caveat ("type = access" )
326
321
macaroon .add_first_party_caveat ("user_id = %s" % (user ,))
@@ -347,7 +342,8 @@ def test_get_user_from_macaroon_expired(self):
347
342
macaroon = pymacaroons .Macaroon (
348
343
location = self .hs .config .server_name ,
349
344
identifier = "key" ,
350
- key = self .hs .config .macaroon_secret_key )
345
+ key = self .hs .config .macaroon_secret_key ,
346
+ )
351
347
macaroon .add_first_party_caveat ("gen = 1" )
352
348
macaroon .add_first_party_caveat ("type = access" )
353
349
macaroon .add_first_party_caveat ("user_id = %s" % (user ,))
@@ -380,7 +376,8 @@ def test_get_user_from_macaroon_with_valid_duration(self):
380
376
macaroon = pymacaroons .Macaroon (
381
377
location = self .hs .config .server_name ,
382
378
identifier = "key" ,
383
- key = self .hs .config .macaroon_secret_key )
379
+ key = self .hs .config .macaroon_secret_key ,
380
+ )
384
381
macaroon .add_first_party_caveat ("gen = 1" )
385
382
macaroon .add_first_party_caveat ("type = access" )
386
383
macaroon .add_first_party_caveat ("user_id = %s" % (user_id ,))
@@ -401,9 +398,7 @@ def test_cannot_use_regular_token_as_guest(self):
401
398
token = yield self .hs .handlers .auth_handler .issue_access_token (
402
399
USER_ID , "DEVICE"
403
400
)
404
- self .store .add_access_token_to_user .assert_called_with (
405
- USER_ID , token , "DEVICE"
406
- )
401
+ self .store .add_access_token_to_user .assert_called_with (USER_ID , token , "DEVICE" )
407
402
408
403
def get_user (tok ):
409
404
if token != tok :
@@ -414,10 +409,9 @@ def get_user(tok):
414
409
"token_id" : 1234 ,
415
410
"device_id" : "DEVICE" ,
416
411
}
412
+
417
413
self .store .get_user_by_access_token = get_user
418
- self .store .get_user_by_id = Mock (return_value = {
419
- "is_guest" : False ,
420
- })
414
+ self .store .get_user_by_id = Mock (return_value = {"is_guest" : False })
421
415
422
416
# check the token works
423
417
request = Mock (args = {})
0 commit comments