async def test_serialize_all_fields(self): """ Confere que é possível serializar um UsersMeResources a apartir dos outros modelos necessários. data = UsersMeResources(user=..., current_account=..., accounts=...) """ async with AsgardDBSession() as s: account = (await s.query(AccountDB).filter(AccountDB.id == ACCOUNT_DEV_ID ).one()) other_accounts = (await s.query(AccountDB).filter( AccountDB.id == ACCOUNT_INFRA_ID).one()) user = (await s.query(UserDB).filter( UserDB.id == USER_WITH_MULTIPLE_ACCOUNTS_ID).one()) users_me_resource = UserResource( user=await User.from_alchemy_obj(user), current_account=await Account.from_alchemy_obj(account), accounts=[await Account.from_alchemy_obj(other_accounts)], ) self.assertDictEqual( { "accounts": [{ "errors": {}, "id": ACCOUNT_INFRA_ID, "name": ACCOUNT_INFRA_NAME, "namespace": ACCOUNT_INFRA_NAMESPACE, "owner": ACCOUNT_INFRA_OWNER, "type": "ASGARD", }], "current_account": { "errors": {}, "id": ACCOUNT_DEV_ID, "name": ACCOUNT_DEV_NAME, "namespace": ACCOUNT_DEV_NAMESPACE, "owner": ACCOUNT_DEV_OWNER, "type": "ASGARD", }, "user": { "email": USER_WITH_MULTIPLE_ACCOUNTS_EMAIL, "errors": {}, "id": USER_WITH_MULTIPLE_ACCOUNTS_ID, "name": USER_WITH_MULTIPLE_ACCOUNTS_NAME, "type": "ASGARD", }, }, users_me_resource.dict(), )
async def whoami(request): user = await User.from_alchemy_obj(request.user) current_account = await Account.from_alchemy_obj( request.user.current_account ) alternate_accounts = await UsersService.get_alternate_accounts( user, current_account, UsersBackend() ) resource_response = UserResource( user=user, current_account=current_account, accounts=alternate_accounts ) return json_response(resource_response.dict())