async def accounts_from_user(user_id: str): user = await UsersService.get_user_by_id(int(user_id), UsersBackend()) status_code = HTTPStatus.OK if user else HTTPStatus.NOT_FOUND if user: accounts = await UsersService.get_accounts_from_user( user, UsersBackend()) return web.json_response( UserAccountsResource(accounts=accounts).dict(), status=status_code) return web.json_response(UserAccountsResource().dict(), status=status_code)
async def test_get_accounts_from_user_user_not_found(self): resp = await self.client.get( f"/users/99/accounts", headers={ "Authorization": f"Token {USER_WITH_MULTIPLE_ACCOUNTS_AUTH_KEY}" }, ) self.assertEqual(404, resp.status) accounts_data = await resp.json() self.assertEqual(UserAccountsResource().dict(), accounts_data)
async def test_get_accounts_from_user_user_with_accounts(self): resp = await self.client.get( f"/users/{USER_WITH_MULTIPLE_ACCOUNTS_ID}/accounts", headers={ "Authorization": f"Token {USER_WITH_MULTIPLE_ACCOUNTS_AUTH_KEY}" }, ) self.assertEqual(200, resp.status) accounts_data = await resp.json() self.assertEqual( UserAccountsResource(accounts=[ Account(**ACCOUNT_DEV_DICT), Account(**ACCOUNT_INFRA_DICT), ]).dict(), accounts_data, )