def _convert_json_to_user(self, user_data: dict) -> User: args = [ user_data[UserAttribute.USER_ID], user_data[UserAttribute.USER_NAME], User.Privilege(user_data[UserAttribute.USER_PRIVILEGE]) ] return User(*args)
def test_gunserialize_user(self): raw_json = { UserAttribute.USER_ID: 123, UserAttribute.USER_NAME: "username", UserAttribute.USER_PRIVILEGE: 1 # = User.Privilege.ADMINISTRATOR } conv = AuthenticatedUserSerializer() user = conv.unserialize(json.dumps(raw_json)) self.assertEqual(raw_json[UserAttribute.USER_ID], user.id) self.assertEqual(raw_json[UserAttribute.USER_NAME], user.name) self.assertEqual( User.Privilege(raw_json[UserAttribute.USER_PRIVILEGE]), user.privilege)
def _unserialize_user(self, json_formatted_user) -> User: return User( json_formatted_user[JsonAttribute.id], json_formatted_user[JsonAttribute.name], User.Privilege(json_formatted_user[JsonAttribute.privilege]), )
def add_user(self, user: ApplicationUserData, identity: UserIdentity = None) -> User: """ Return a user based on the data provided """ return User("123-456-789", user["name"], User.Privilege(user["privilege"]))
def _privilege_from_str(self, privilege_name: str) -> User.Privilege: privilege_numeric = next(privilege.value for privilege in User.Privilege if privilege.name == privilege_name) return User.Privilege(privilege_numeric)