Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Blackify the tests #3679

Merged
merged 3 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3679.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Synapse's tests are now formatted with the black autoformatter.
76 changes: 35 additions & 41 deletions tests/api/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def __init__(self, hs):


class AuthTestCase(unittest.TestCase):

@defer.inlineCallbacks
def setUp(self):
self.state_handler = Mock()
Expand All @@ -53,11 +52,7 @@ def setUp(self):

@defer.inlineCallbacks
def test_get_user_by_req_user_valid_token(self):
user_info = {
"name": self.test_user,
"token_id": "ditto",
"device_id": "device",
}
user_info = {"name": self.test_user, "token_id": "ditto", "device_id": "device"}
self.store.get_user_by_access_token = Mock(return_value=user_info)

request = Mock(args={})
Expand All @@ -76,10 +71,7 @@ def test_get_user_by_req_user_bad_token(self):
self.failureResultOf(d, AuthError)

def test_get_user_by_req_user_missing_token(self):
user_info = {
"name": self.test_user,
"token_id": "ditto",
}
user_info = {"name": self.test_user, "token_id": "ditto"}
self.store.get_user_by_access_token = Mock(return_value=user_info)

request = Mock(args={})
Expand All @@ -90,8 +82,7 @@ def test_get_user_by_req_user_missing_token(self):
@defer.inlineCallbacks
def test_get_user_by_req_appservice_valid_token(self):
app_service = Mock(
token="foobar", url="a_url", sender=self.test_user,
ip_range_whitelist=None,
token="foobar", url="a_url", sender=self.test_user, ip_range_whitelist=None
)
self.store.get_app_service_by_token = Mock(return_value=app_service)
self.store.get_user_by_access_token = Mock(return_value=None)
Expand All @@ -106,8 +97,11 @@ def test_get_user_by_req_appservice_valid_token(self):
@defer.inlineCallbacks
def test_get_user_by_req_appservice_valid_token_good_ip(self):
from netaddr import IPSet

app_service = Mock(
token="foobar", url="a_url", sender=self.test_user,
token="foobar",
url="a_url",
sender=self.test_user,
ip_range_whitelist=IPSet(["192.168/16"]),
)
self.store.get_app_service_by_token = Mock(return_value=app_service)
Expand All @@ -122,8 +116,11 @@ def test_get_user_by_req_appservice_valid_token_good_ip(self):

def test_get_user_by_req_appservice_valid_token_bad_ip(self):
from netaddr import IPSet

app_service = Mock(
token="foobar", url="a_url", sender=self.test_user,
token="foobar",
url="a_url",
sender=self.test_user,
ip_range_whitelist=IPSet(["192.168/16"]),
)
self.store.get_app_service_by_token = Mock(return_value=app_service)
Expand Down Expand Up @@ -160,8 +157,7 @@ def test_get_user_by_req_appservice_missing_token(self):
def test_get_user_by_req_appservice_valid_token_valid_user_id(self):
masquerading_user_id = b"@doppelganger:matrix.org"
app_service = Mock(
token="foobar", url="a_url", sender=self.test_user,
ip_range_whitelist=None,
token="foobar", url="a_url", sender=self.test_user, ip_range_whitelist=None
)
app_service.is_interested_in_user = Mock(return_value=True)
self.store.get_app_service_by_token = Mock(return_value=app_service)
Expand All @@ -174,15 +170,13 @@ def test_get_user_by_req_appservice_valid_token_valid_user_id(self):
request.requestHeaders.getRawHeaders = mock_getRawHeaders()
requester = yield self.auth.get_user_by_req(request)
self.assertEquals(
requester.user.to_string(),
masquerading_user_id.decode('utf8')
requester.user.to_string(), masquerading_user_id.decode('utf8')
)

def test_get_user_by_req_appservice_valid_token_bad_user_id(self):
masquerading_user_id = b"@doppelganger:matrix.org"
app_service = Mock(
token="foobar", url="a_url", sender=self.test_user,
ip_range_whitelist=None,
token="foobar", url="a_url", sender=self.test_user, ip_range_whitelist=None
)
app_service.is_interested_in_user = Mock(return_value=False)
self.store.get_app_service_by_token = Mock(return_value=app_service)
Expand All @@ -201,17 +195,15 @@ def test_get_user_from_macaroon(self):
# TODO(danielwh): Remove this mock when we remove the
# get_user_by_access_token fallback.
self.store.get_user_by_access_token = Mock(
return_value={
"name": "@baldrick:matrix.org",
"device_id": "device",
}
return_value={"name": "@baldrick:matrix.org", "device_id": "device"}
)

user_id = "@baldrick:matrix.org"
macaroon = pymacaroons.Macaroon(
location=self.hs.config.server_name,
identifier="key",
key=self.hs.config.macaroon_secret_key)
key=self.hs.config.macaroon_secret_key,
)
macaroon.add_first_party_caveat("gen = 1")
macaroon.add_first_party_caveat("type = access")
macaroon.add_first_party_caveat("user_id = %s" % (user_id,))
Expand All @@ -225,15 +217,14 @@ def test_get_user_from_macaroon(self):

@defer.inlineCallbacks
def test_get_guest_user_from_macaroon(self):
self.store.get_user_by_id = Mock(return_value={
"is_guest": True,
})
self.store.get_user_by_id = Mock(return_value={"is_guest": True})

user_id = "@baldrick:matrix.org"
macaroon = pymacaroons.Macaroon(
location=self.hs.config.server_name,
identifier="key",
key=self.hs.config.macaroon_secret_key)
key=self.hs.config.macaroon_secret_key,
)
macaroon.add_first_party_caveat("gen = 1")
macaroon.add_first_party_caveat("type = access")
macaroon.add_first_party_caveat("user_id = %s" % (user_id,))
Expand All @@ -257,7 +248,8 @@ def test_get_user_from_macaroon_user_db_mismatch(self):
macaroon = pymacaroons.Macaroon(
location=self.hs.config.server_name,
identifier="key",
key=self.hs.config.macaroon_secret_key)
key=self.hs.config.macaroon_secret_key,
)
macaroon.add_first_party_caveat("gen = 1")
macaroon.add_first_party_caveat("type = access")
macaroon.add_first_party_caveat("user_id = %s" % (user,))
Expand All @@ -277,7 +269,8 @@ def test_get_user_from_macaroon_missing_caveat(self):
macaroon = pymacaroons.Macaroon(
location=self.hs.config.server_name,
identifier="key",
key=self.hs.config.macaroon_secret_key)
key=self.hs.config.macaroon_secret_key,
)
macaroon.add_first_party_caveat("gen = 1")
macaroon.add_first_party_caveat("type = access")

Expand All @@ -298,7 +291,8 @@ def test_get_user_from_macaroon_wrong_key(self):
macaroon = pymacaroons.Macaroon(
location=self.hs.config.server_name,
identifier="key",
key=self.hs.config.macaroon_secret_key + "wrong")
key=self.hs.config.macaroon_secret_key + "wrong",
)
macaroon.add_first_party_caveat("gen = 1")
macaroon.add_first_party_caveat("type = access")
macaroon.add_first_party_caveat("user_id = %s" % (user,))
Expand All @@ -320,7 +314,8 @@ def test_get_user_from_macaroon_unknown_caveat(self):
macaroon = pymacaroons.Macaroon(
location=self.hs.config.server_name,
identifier="key",
key=self.hs.config.macaroon_secret_key)
key=self.hs.config.macaroon_secret_key,
)
macaroon.add_first_party_caveat("gen = 1")
macaroon.add_first_party_caveat("type = access")
macaroon.add_first_party_caveat("user_id = %s" % (user,))
Expand All @@ -347,7 +342,8 @@ def test_get_user_from_macaroon_expired(self):
macaroon = pymacaroons.Macaroon(
location=self.hs.config.server_name,
identifier="key",
key=self.hs.config.macaroon_secret_key)
key=self.hs.config.macaroon_secret_key,
)
macaroon.add_first_party_caveat("gen = 1")
macaroon.add_first_party_caveat("type = access")
macaroon.add_first_party_caveat("user_id = %s" % (user,))
Expand Down Expand Up @@ -380,7 +376,8 @@ def test_get_user_from_macaroon_with_valid_duration(self):
macaroon = pymacaroons.Macaroon(
location=self.hs.config.server_name,
identifier="key",
key=self.hs.config.macaroon_secret_key)
key=self.hs.config.macaroon_secret_key,
)
macaroon.add_first_party_caveat("gen = 1")
macaroon.add_first_party_caveat("type = access")
macaroon.add_first_party_caveat("user_id = %s" % (user_id,))
Expand All @@ -401,9 +398,7 @@ def test_cannot_use_regular_token_as_guest(self):
token = yield self.hs.handlers.auth_handler.issue_access_token(
USER_ID, "DEVICE"
)
self.store.add_access_token_to_user.assert_called_with(
USER_ID, token, "DEVICE"
)
self.store.add_access_token_to_user.assert_called_with(USER_ID, token, "DEVICE")

def get_user(tok):
if token != tok:
Expand All @@ -414,10 +409,9 @@ def get_user(tok):
"token_id": 1234,
"device_id": "DEVICE",
}

self.store.get_user_by_access_token = get_user
self.store.get_user_by_id = Mock(return_value={
"is_guest": False,
})
self.store.get_user_by_id = Mock(return_value={"is_guest": False})

# check the token works
request = Mock(args={})
Expand Down
Loading