async def update_user_partial(request: web.Request): user_id: str = request.match_info["user_id"] try: body_data = await request.json() except JSONDecodeError as e: return web.json_response( ErrorResource(errors=[ErrorDetail(msg=str(e))]).dict(), status=HTTPStatus.UNPROCESSABLE_ENTITY, ) user = await UsersService.get_user_by_id(int(user_id), UsersBackend()) status_code = HTTPStatus.ACCEPTED if user else HTTPStatus.NOT_FOUND try: if user: body_user = User(**body_data) user.name = body_user.name if body_user.name else user.name user.email = body_user.email if body_user.email else user.email updated_user = await UsersService.update_user(user, UsersBackend()) except DuplicateEntity as de: return web.json_response( ErrorResource(errors=[ErrorDetail(msg=str(de))]).dict(), status=status_code, ) return web.json_response(UserResource(user=updated_user).dict(), status=status_code)
async def test_create_job_validation_error(self, infra_job_fixture): """ Validamos que retornamos HTTPStatus.UNPROCESSABLE_ENTITY caso a entrada esteja incompleta """ account = Account(**ACCOUNT_DEV_DICT) asgard_job_no_namespace = ChronosScheduledJobConverter.to_asgard_model( ChronosJob(**infra_job_fixture)).remove_namespace(account) incomplete_asgard_job = asgard_job_no_namespace.dict() del incomplete_asgard_job["container"] resp = await self.client.post( "/jobs", headers={ "Authorization": f"Token {USER_WITH_MULTIPLE_ACCOUNTS_AUTH_KEY}" }, json=incomplete_asgard_job, ) self.assertEqual(HTTPStatus.UNPROCESSABLE_ENTITY, resp.status) resp_data = await resp.json() expected_error_msg = """1 validation error for ScheduledJob\ncontainer\n field required (type=value_error.missing)""" self.assertEqual( ErrorResource(errors=[ErrorDetail(msg=expected_error_msg)]).dict(), resp_data, )
async def _wrapper(request: web.Request): try: req_body = await request.json() except JSONDecodeError as e: return web.json_response( ErrorResource(errors=[ErrorDetail(msg=str(e))]).dict(), status=HTTPStatus.BAD_REQUEST, ) try: job = ScheduledJob(**req_body) except ValidationError as e: return web.json_response( ErrorResource(errors=[ErrorDetail(msg=str(e))]).dict(), status=HTTPStatus.UNPROCESSABLE_ENTITY, ) request["types_registry"].set(job) return await call_http_handler(request, handler)
async def create_job(job: ScheduledJob, user: User, account: Account): try: created_job = await ScheduledJobsService.create_job( job, user, account, ChronosScheduledJobsBackend()) except DuplicateEntity as e: return json_response( ErrorResource(errors=[ErrorDetail(msg=str(e))]).dict(), status=HTTPStatus.UNPROCESSABLE_ENTITY, ) return json_response( CreateScheduledJobResource(job=created_job).dict(), status=HTTPStatus.CREATED, )
async def _update_job(job: ScheduledJob, user: User, account: Account) -> Response: try: updated_job = await ScheduledJobsService.update_job( job, user, account, ChronosScheduledJobsBackend()) except NotFoundEntity as e: return json_response( ErrorResource(errors=[ErrorDetail(msg=str(e))]).dict(), status=HTTPStatus.NOT_FOUND, ) return json_response( CreateScheduledJobResource(job=updated_job).dict(), status=HTTPStatus.ACCEPTED, )
async def test_update_user_invalid_json(self): resp = await self.client.patch( f"/users/{USER_WITH_MULTIPLE_ACCOUNTS_ID}", headers={ "Authorization": f"Token {USER_WITH_MULTIPLE_ACCOUNTS_AUTH_KEY}" }, data="{data", ) self.assertEqual(HTTPStatus.UNPROCESSABLE_ENTITY, resp.status) resp_data = await resp.json() expected_error_msg = "Expecting property name enclosed in double quotes: line 1 column 2 (char 1)" self.assertEqual( ErrorResource(errors=[ErrorDetail(msg=expected_error_msg)]).dict(), resp_data, )
async def test_create_user_duplicate_email(self): user = User(name="New User", email=USER_WITH_MULTIPLE_ACCOUNTS_EMAIL) resp = await self.client.post( f"/users", headers={ "Authorization": f"Token {USER_WITH_MULTIPLE_ACCOUNTS_AUTH_KEY}" }, json=user.dict(), ) self.assertEqual(422, resp.status) resp_data = await resp.json() expected_error_message = """ERROR: duplicate key value violates unique constraint "user_tx_email_key"\nDETAIL: Key (tx_email)=([email protected]) already exists.\n""" self.assertEqual( ErrorResource(errors=[ErrorDetail( msg=expected_error_message)]).dict(), resp_data, )
async def create_user(request: web.Request): status_code = HTTPStatus.CREATED try: user = User(**await request.json()) except ValueError: return web.json_response(UserResource().dict(), status=HTTPStatus.BAD_REQUEST) try: created_user = await UsersService.create_user(user, UsersBackend()) except DuplicateEntity as de: return web.json_response( ErrorResource(errors=[ErrorDetail(msg=str(de))]).dict(), status=HTTPStatus.UNPROCESSABLE_ENTITY, ) return web.json_response(UserResource(user=created_user).dict(), status=status_code)
async def test_update_job_validation_error(self, dev_job_fixture): asgard_job_no_namespace = ChronosScheduledJobConverter.to_asgard_model( ChronosJob(**dev_job_fixture)).remove_namespace(self.account) incomplete_asgard_job = asgard_job_no_namespace.dict() del incomplete_asgard_job["container"] resp = await self.client.put( f"/jobs/{asgard_job_no_namespace.id}", headers={ "Authorization": f"Token {USER_WITH_MULTIPLE_ACCOUNTS_AUTH_KEY}" }, json=incomplete_asgard_job, ) self.assertEqual(HTTPStatus.UNPROCESSABLE_ENTITY, resp.status) resp_data = await resp.json() expected_error_msg = """1 validation error for ScheduledJob\ncontainer\n field required (type=value_error.missing)""" self.assertEqual( ErrorResource(errors=[ErrorDetail(msg=expected_error_msg)]).dict(), resp_data, )
async def test_update_job_job_does_not_exist(self, dev_job_fixture): await _load_jobs_into_chronos(dev_job_fixture) asgard_job = ChronosScheduledJobConverter.to_asgard_model( ChronosJob(**dev_job_fixture)) asgard_job.id = "job-does-not-exist" asgard_job.remove_namespace(self.account) resp = await self.client.put( f"/jobs/{asgard_job.id}", headers={ "Authorization": f"Token {USER_WITH_MULTIPLE_ACCOUNTS_AUTH_KEY}" }, json=asgard_job.dict(), ) self.assertEqual(HTTPStatus.NOT_FOUND, resp.status) self.assertEqual( ErrorResource( errors=[ErrorDetail( msg=f"Entity not found: {asgard_job.id}")]).dict(), await resp.json(), )
async def test_create_job_duplicate_entity(self, dev_job_fixture): account = Account(**ACCOUNT_DEV_DICT) await _load_jobs_into_chronos(dev_job_fixture) asgard_job_sem_namespace = ChronosScheduledJobConverter.to_asgard_model( ChronosJob(**dev_job_fixture)).remove_namespace(account) resp = await self.client.post( "/jobs", headers={ "Authorization": f"Token {USER_WITH_MULTIPLE_ACCOUNTS_AUTH_KEY}" }, json=asgard_job_sem_namespace.dict(), ) self.assertEqual(HTTPStatus.UNPROCESSABLE_ENTITY, resp.status) resp_data = await resp.json() expected_error_msg = ( f"Scheduled job already exists: {asgard_job_sem_namespace.id}") self.assertEqual( ErrorResource(errors=[ErrorDetail(msg=expected_error_msg)]), resp_data, )
async def test_update_user_duplicate_email(self): expected_new_email = USER_WITH_NO_ACCOUNTS_EMAIL new_user_data = {"email": expected_new_email} resp = await self.client.patch( f"/users/{USER_WITH_MULTIPLE_ACCOUNTS_ID}", headers={ "Authorization": f"Token {USER_WITH_MULTIPLE_ACCOUNTS_AUTH_KEY}" }, json=new_user_data, ) self.assertEqual(HTTPStatus.ACCEPTED, resp.status) user_data = await resp.json() new_user = User(**USER_WITH_MULTIPLE_ACCOUNTS_DICT) new_user.name = expected_new_email expected_error_message = """ERROR: duplicate key value violates unique constraint "user_tx_email_key"\nDETAIL: Key (tx_email)=([email protected]) already exists.\n""" self.assertEqual( ErrorResource(errors=[ErrorDetail( msg=expected_error_message)]).dict(), user_data, ) resp = await self.client.get( f"/users/{USER_WITH_MULTIPLE_ACCOUNTS_ID}", headers={ "Authorization": f"Token {USER_WITH_MULTIPLE_ACCOUNTS_AUTH_KEY}" }, ) updated_user_data = await resp.json() self.assertEqual( UserResource(user=User(**USER_WITH_MULTIPLE_ACCOUNTS_DICT)).dict(), updated_user_data, )