def test_supports_legacy_is_admin_field(alice): # Manually craft a local user in legacy format raw_legacy_local_user = { "organization_addr": alice.organization_addr.to_url(), "device_id": str(alice.device_id), "signing_key": alice.signing_key.encode(), "private_key": alice.private_key.encode(), "is_admin": True, "user_manifest_id": UUID(alice.user_manifest_id.hex), "user_manifest_key": bytes(alice.user_manifest_key), "local_symkey": bytes(alice.local_symkey), } dumped_legacy_local_user = packb(raw_legacy_local_user) # Make sure the legacy format can be loaded legacy_local_user = LocalDevice.load(dumped_legacy_local_user) assert legacy_local_user == alice # Manually decode new format to check it is compatible with legacy dumped_local_user = alice.dump() raw_local_user = unpackb(dumped_local_user) assert raw_local_user == { **raw_legacy_local_user, "profile": alice.profile.value, "human_handle": None, "device_label": None, }
def _on_notification(self, connection, pid, channel, payload): data = unpackb(b64decode(payload.encode("ascii"))) data.pop("__id__") # Simply discard the notification id signal = data.pop("__signal__") logger.debug("notif received", pid=pid, channel=channel, payload=payload) # Convert strings to enums signal = STR_TO_BACKEND_EVENTS[signal] # Kind of a hack, but fine enough for the moment if signal == BackendEvent.REALM_ROLES_UPDATED: data["role"] = STR_TO_REALM_ROLE.get(data.pop("role_str")) elif signal == BackendEvent.INVITE_STATUS_CHANGED: data["status"] = STR_TO_INVITATION_STATUS.get( data.pop("status_str")) self.event_bus.send(signal, **data)
def test_user_certificate_supports_legacy_is_admin_field(alice, bob): now = pendulum_now() certif = UserCertificateContent( author=bob.device_id, timestamp=now, user_id=alice.user_id, human_handle=None, public_key=alice.public_key, profile=alice.profile, ) # Manually craft a certificate in legacy format raw_legacy_certif = { "type": "user_certificate", "author": bob.device_id, "timestamp": now, "user_id": alice.user_id, "public_key": alice.public_key.encode(), "is_admin": True, } dumped_legacy_certif = bob.signing_key.sign(zlib.compress(packb(raw_legacy_certif))) # Make sure the legacy format can be loaded legacy_certif = UserCertificateContent.verify_and_load( dumped_legacy_certif, author_verify_key=bob.verify_key, expected_author=bob.device_id, expected_user=alice.user_id, expected_human_handle=None, ) assert legacy_certif == certif # Manually decode new format to check it is compatible with legacy dumped_certif = certif.dump_and_sign(bob.signing_key) raw_certif = unpackb(zlib.decompress(bob.verify_key.verify(dumped_certif))) assert raw_certif == {**raw_legacy_certif, "profile": alice.profile.value, "human_handle": None}
def test_pack_uuid(): data = {"uuid": uuid.uuid4()} packed = packb(data) unpacked = unpackb(packed) assert unpacked == data assert isinstance(unpacked["uuid"], uuid.UUID)
def test_pack_datetime(): data = {"date": pendulum.now()} packed = packb(data) unpacked = unpackb(packed) assert unpacked == data assert isinstance(unpacked["date"], pendulum.DateTime)