def deserialize(cls, serial: str, token: str = None) -> N: """ Deserializes and returns a new Notification instance. """ try: assert serial except AssertionError: raise InvalidNotificationError( "Can't deserialize an input of 'None'") try: struct = json.loads(serial) except json.JSONDecodeError: raise InvalidNotificationError( "Can only deserialize a JSON string") required_keys = set(['a', 'v', 'o', 's', 'l', 't', 'c', 'i', 'e']) missing_keys = required_keys.difference(struct.keys()) if missing_keys: raise InvalidNotificationError( 'Missing keys: {}'.format(missing_keys)) users = [Entity.from_str(u, token=token) for u in struct.get('u', [])] target = [Entity.from_str(t, token=token) for t in struct.get('t', [])] deserial = cls(Entity.from_str(struct['a'], token=token), str(struct['v']), Entity.from_str(struct['o'], token=token), struct['s'], level=str(struct['l']), target=target, context=struct.get('n'), external_key=struct.get('x'), users=users) deserial.created = struct['c'] deserial.id = struct['i'] deserial.expires = struct['e'] return deserial
def test_entity_from_str(): eid = "foo" etype = "user" s = etype + STR_SEPARATOR + eid e = Entity.from_str(s) assert e.id == eid assert e.type == etype
def test_entity_from_str_fail(s, err, msg): with pytest.raises(err) as e: Entity.from_str(s) assert msg in str(e)