async def _get_extended_request(self, request_id, log_data): dynamo = UserDynamoHandler(self.user) requests = await dynamo.get_policy_requests(request_id=request_id) if len(requests) == 0: log_data["message"] = "Request with that ID not found" log.warn(log_data) stats.count(f"{log_data['function']}.not_found", tags={"user": self.user}) raise NoMatchingRequest(log_data["message"]) if len(requests) > 1: log_data["message"] = "Multiple requests with that ID found" log.error(log_data) stats.count( f"{log_data['function']}.multiple_requests_found", tags={"user": self.user}, ) raise InvalidRequestParameter(log_data["message"]) request = requests[0] if request.get("version") != "2": # Request format is not compatible with this endpoint version raise InvalidRequestParameter( "Request with that ID is not a v2 request") extended_request = ExtendedRequestModel.parse_obj( request.get("extended_request")) return extended_request, request.get("last_updated")
def resolve_request_ids( self, request_ids: List[str]) -> List[Dict[str, Union[int, str]]]: requests = [] for request_id in request_ids: request = self.requests_table.query( KeyConditionExpression="request_id = :ri", ExpressionAttributeValues={":ri": request_id}, ) if request["Items"]: items = self._data_from_dynamo_replace(request["Items"]) requests.append(items[0]) else: raise NoMatchingRequest( f"No matching request for request_id: {request_id}") return requests
def test_get_request_by_id_failure(self, mock_auth, mock_user_dynamo_handler): """Chuck Norris has a request and is an secondary approver for group1""" from consoleme.lib.requests import get_request_by_id mock_user = "******" mock_requests = NoMatchingRequest("foo") mock_secondary_approver = ["group1"] mock_user_dynamo_handler.return_value.resolve_request_ids.side_effect = ( mock_requests) mock_sa = Future() mock_sa.set_result(mock_secondary_approver) mock_auth.get_secondary_approvers.return_value = mock_sa requests = asyncio.get_event_loop().run_until_complete( get_request_by_id(mock_user, "123456")) self.assertEqual(requests, None, "None should be returned when there is no match")