def test_serialization(): expected = { "authority": "http://localhost", "clientId": "client-id", "homeAccountId": "object-id.tenant-id", "tenantId": "tenant-id", "username": "******", "version": "1.0", } record = AuthenticationRecord( expected["tenantId"], expected["clientId"], expected["authority"], expected["homeAccountId"], expected["username"], ) serialized = record.serialize() assert json.loads(serialized) == expected deserialized = AuthenticationRecord.deserialize(serialized) assert sorted(vars(deserialized)) == sorted(vars(record)) assert record.authority == deserialized.authority == expected["authority"] assert record.client_id == deserialized.client_id == expected["clientId"] assert record.home_account_id == deserialized.home_account_id == expected[ "homeAccountId"] assert record.tenant_id == deserialized.tenant_id == expected["tenantId"] assert record.username == deserialized.username == expected["username"]
def _save_auth_record(self, auth_record: AuthenticationRecord): record = auth_record.serialize() try: with open(AUTH_RECORD_LOCATION, 'w') as file: file.write(record) except IOError as ex: raise CLIException( 'Authentication session not saved, you\'ll be prompted \ to login when running a command') from ex
def test_serialization(): """serialize should accept arbitrary additional key/value pairs, which deserialize should ignore""" attrs = ("authority", "client_id", "home_account_id", "tenant_id", "username") nums = (n for n in range(len(attrs))) record_values = {attr: next(nums) for attr in attrs} record = AuthenticationRecord(**record_values) serialized = record.serialize() # AuthenticationRecord's fields should have been serialized assert json.loads(serialized) == record_values deserialized = AuthenticationRecord.deserialize(serialized) # the deserialized record and the constructed record should have the same fields assert sorted(vars(deserialized)) == sorted(vars(record)) # the constructed and deserialized records should have the same values assert all( getattr(deserialized, attr) == record_values[attr] for attr in attrs)
def _cache_auth_record(record: AuthenticationRecord, token_path: Path): token = record.serialize() with token_path.open('w') as token_file: token_file.write(token)