@@ -34,7 +34,6 @@ def __init__(self, hs):
3434
3535
3636class AuthTestCase (unittest .TestCase ):
37-
3837 @defer .inlineCallbacks
3938 def setUp (self ):
4039 self .state_handler = Mock ()
@@ -53,11 +52,7 @@ def setUp(self):
5352
5453 @defer .inlineCallbacks
5554 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" }
6156 self .store .get_user_by_access_token = Mock (return_value = user_info )
6257
6358 request = Mock (args = {})
@@ -76,10 +71,7 @@ def test_get_user_by_req_user_bad_token(self):
7671 self .failureResultOf (d , AuthError )
7772
7873 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" }
8375 self .store .get_user_by_access_token = Mock (return_value = user_info )
8476
8577 request = Mock (args = {})
@@ -90,8 +82,7 @@ def test_get_user_by_req_user_missing_token(self):
9082 @defer .inlineCallbacks
9183 def test_get_user_by_req_appservice_valid_token (self ):
9284 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
9586 )
9687 self .store .get_app_service_by_token = Mock (return_value = app_service )
9788 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):
10697 @defer .inlineCallbacks
10798 def test_get_user_by_req_appservice_valid_token_good_ip (self ):
10899 from netaddr import IPSet
100+
109101 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 ,
111105 ip_range_whitelist = IPSet (["192.168/16" ]),
112106 )
113107 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):
122116
123117 def test_get_user_by_req_appservice_valid_token_bad_ip (self ):
124118 from netaddr import IPSet
119+
125120 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 ,
127124 ip_range_whitelist = IPSet (["192.168/16" ]),
128125 )
129126 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):
160157 def test_get_user_by_req_appservice_valid_token_valid_user_id (self ):
161158 masquerading_user_id = b"@doppelganger:matrix.org"
162159 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
165161 )
166162 app_service .is_interested_in_user = Mock (return_value = True )
167163 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):
174170 request .requestHeaders .getRawHeaders = mock_getRawHeaders ()
175171 requester = yield self .auth .get_user_by_req (request )
176172 self .assertEquals (
177- requester .user .to_string (),
178- masquerading_user_id .decode ('utf8' )
173+ requester .user .to_string (), masquerading_user_id .decode ('utf8' )
179174 )
180175
181176 def test_get_user_by_req_appservice_valid_token_bad_user_id (self ):
182177 masquerading_user_id = b"@doppelganger:matrix.org"
183178 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
186180 )
187181 app_service .is_interested_in_user = Mock (return_value = False )
188182 self .store .get_app_service_by_token = Mock (return_value = app_service )
@@ -201,17 +195,15 @@ def test_get_user_from_macaroon(self):
201195 # TODO(danielwh): Remove this mock when we remove the
202196 # get_user_by_access_token fallback.
203197 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" }
208199 )
209200
210201 user_id = "@baldrick:matrix.org"
211202 macaroon = pymacaroons .Macaroon (
212203 location = self .hs .config .server_name ,
213204 identifier = "key" ,
214- key = self .hs .config .macaroon_secret_key )
205+ key = self .hs .config .macaroon_secret_key ,
206+ )
215207 macaroon .add_first_party_caveat ("gen = 1" )
216208 macaroon .add_first_party_caveat ("type = access" )
217209 macaroon .add_first_party_caveat ("user_id = %s" % (user_id ,))
@@ -225,15 +217,14 @@ def test_get_user_from_macaroon(self):
225217
226218 @defer .inlineCallbacks
227219 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 })
231221
232222 user_id = "@baldrick:matrix.org"
233223 macaroon = pymacaroons .Macaroon (
234224 location = self .hs .config .server_name ,
235225 identifier = "key" ,
236- key = self .hs .config .macaroon_secret_key )
226+ key = self .hs .config .macaroon_secret_key ,
227+ )
237228 macaroon .add_first_party_caveat ("gen = 1" )
238229 macaroon .add_first_party_caveat ("type = access" )
239230 macaroon .add_first_party_caveat ("user_id = %s" % (user_id ,))
@@ -257,7 +248,8 @@ def test_get_user_from_macaroon_user_db_mismatch(self):
257248 macaroon = pymacaroons .Macaroon (
258249 location = self .hs .config .server_name ,
259250 identifier = "key" ,
260- key = self .hs .config .macaroon_secret_key )
251+ key = self .hs .config .macaroon_secret_key ,
252+ )
261253 macaroon .add_first_party_caveat ("gen = 1" )
262254 macaroon .add_first_party_caveat ("type = access" )
263255 macaroon .add_first_party_caveat ("user_id = %s" % (user ,))
@@ -277,7 +269,8 @@ def test_get_user_from_macaroon_missing_caveat(self):
277269 macaroon = pymacaroons .Macaroon (
278270 location = self .hs .config .server_name ,
279271 identifier = "key" ,
280- key = self .hs .config .macaroon_secret_key )
272+ key = self .hs .config .macaroon_secret_key ,
273+ )
281274 macaroon .add_first_party_caveat ("gen = 1" )
282275 macaroon .add_first_party_caveat ("type = access" )
283276
@@ -298,7 +291,8 @@ def test_get_user_from_macaroon_wrong_key(self):
298291 macaroon = pymacaroons .Macaroon (
299292 location = self .hs .config .server_name ,
300293 identifier = "key" ,
301- key = self .hs .config .macaroon_secret_key + "wrong" )
294+ key = self .hs .config .macaroon_secret_key + "wrong" ,
295+ )
302296 macaroon .add_first_party_caveat ("gen = 1" )
303297 macaroon .add_first_party_caveat ("type = access" )
304298 macaroon .add_first_party_caveat ("user_id = %s" % (user ,))
@@ -320,7 +314,8 @@ def test_get_user_from_macaroon_unknown_caveat(self):
320314 macaroon = pymacaroons .Macaroon (
321315 location = self .hs .config .server_name ,
322316 identifier = "key" ,
323- key = self .hs .config .macaroon_secret_key )
317+ key = self .hs .config .macaroon_secret_key ,
318+ )
324319 macaroon .add_first_party_caveat ("gen = 1" )
325320 macaroon .add_first_party_caveat ("type = access" )
326321 macaroon .add_first_party_caveat ("user_id = %s" % (user ,))
@@ -347,7 +342,8 @@ def test_get_user_from_macaroon_expired(self):
347342 macaroon = pymacaroons .Macaroon (
348343 location = self .hs .config .server_name ,
349344 identifier = "key" ,
350- key = self .hs .config .macaroon_secret_key )
345+ key = self .hs .config .macaroon_secret_key ,
346+ )
351347 macaroon .add_first_party_caveat ("gen = 1" )
352348 macaroon .add_first_party_caveat ("type = access" )
353349 macaroon .add_first_party_caveat ("user_id = %s" % (user ,))
@@ -380,7 +376,8 @@ def test_get_user_from_macaroon_with_valid_duration(self):
380376 macaroon = pymacaroons .Macaroon (
381377 location = self .hs .config .server_name ,
382378 identifier = "key" ,
383- key = self .hs .config .macaroon_secret_key )
379+ key = self .hs .config .macaroon_secret_key ,
380+ )
384381 macaroon .add_first_party_caveat ("gen = 1" )
385382 macaroon .add_first_party_caveat ("type = access" )
386383 macaroon .add_first_party_caveat ("user_id = %s" % (user_id ,))
@@ -401,9 +398,7 @@ def test_cannot_use_regular_token_as_guest(self):
401398 token = yield self .hs .handlers .auth_handler .issue_access_token (
402399 USER_ID , "DEVICE"
403400 )
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" )
407402
408403 def get_user (tok ):
409404 if token != tok :
@@ -414,10 +409,9 @@ def get_user(tok):
414409 "token_id" : 1234 ,
415410 "device_id" : "DEVICE" ,
416411 }
412+
417413 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 })
421415
422416 # check the token works
423417 request = Mock (args = {})
0 commit comments