def test_persistent_query_example_get_by_name_raises_does_not_exist_error_if_not_found( self, mock_get_by_name ): # Arrange mock_get_by_name.side_effect = exceptions.DoesNotExist(message="mock error") mock_user = create_mock_user("1") # Act # Assert with self.assertRaises(exceptions.DoesNotExist): persistent_query_example_api.get_by_name("mock_id", mock_user)
def test_get_by_id_as_anonymous_user(self): # Arrange persistent_query_example_name = self.fixture.persistent_query_example_1.name # Act # Assert if CAN_ANONYMOUS_ACCESS_PUBLIC_DOCUMENT: self.assertTrue( isinstance( persistent_query_example_api.get_by_name( persistent_query_example_name, AnonymousUser()), PersistentQueryExample, )) else: with self.assertRaises(AccessControlError): persistent_query_example_api.get_by_name( persistent_query_example_name, AnonymousUser())
def test_get_by_name_as_superuser_returns_persistent_query_example(self): # Arrange persistent_query_example_name = self.fixture.persistent_query_example_1.name mock_user = create_mock_user("0", is_staff=True, is_superuser=True) # Act persistent_query_example = persistent_query_example_api.get_by_name( persistent_query_example_name, mock_user) # Assert self.assertTrue( isinstance(persistent_query_example, PersistentQueryExample))
def test_get_by_id_as_user_not_owner_returns_persistent_query_example( self): # Arrange persistent_query_example_name = self.fixture.persistent_query_example_1.name mock_user = create_mock_user("0") # Act # Assert self.assertTrue( isinstance( persistent_query_example_api.get_by_name( persistent_query_example_name, mock_user), PersistentQueryExample, ))
def test_persistent_query_example_get_by_name_return_data_if_found( self, mock_get_by_name ): # Arrange expected_result = PersistentQueryExample(user_id="1") mock_get_by_name.return_value = expected_result mock_user = create_mock_user("1") # Act # Assert self.assertEqual( persistent_query_example_api.get_by_name("mock_name", mock_user), expected_result, )
def get(self, request, name): """Retrieve persistent query example from database Args: request: HTTP request name: name Returns: - code: 200 content: persistent query example - code: 403 content: Forbidden - code: 404 content: Object was not found - code: 500 content: Internal server error """ try: # Get object persistent_query_example = persistent_query_example_api.get_by_name( name, request.user) # Serialize object serializer = self.serializer(persistent_query_example) # Return response return Response(serializer.data) except exceptions.DoesNotExist: content = {"message": "Persistent query example not found."} return Response(content, status=status.HTTP_404_NOT_FOUND) except AccessControlError as e: content = {"message": str(e)} return Response(content, status=status.HTTP_403_FORBIDDEN) except Exception as api_exception: content = {"message": str(api_exception)} return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def _get_persistent_query_by_name(persistent_query_name, user): return persistent_query_example_api.get_by_name( persistent_query_name, user)