Exemplo n.º 1
0
    def test_post_with_bad_request_returns_BAD_REQUEST(self):
        # Setup
        mock_data_client = MagicMock()
        data_handler = DataHandler(mock_data_client)

        # Execute
        result = data_handler.handle_post_request(None)

        # Assert
        self.assertEqual(result[0], Constants.BAD_REQUEST_CODE)
        self.assertEqual(result[1], Constants.BAD_REQUEST_MESSAGE)
Exemplo n.º 2
0
    def test_delete_returns_SUCCESS_RESPONSE(self):
        # Setup
        mock_data_client = MagicMock()
        mock_data_client.delete.return_value = True
        data_handler = DataHandler(mock_data_client)

        # Execute
        result = data_handler.handle_delete_request(mock_data_client)

        # Assert
        self.assertEqual(result[0], Constants.SUCCESS_RESPONSE_CODE)
        self.assertEqual(result[1], Constants.DELETE_REQUEST_MESSAGE)
Exemplo n.º 3
0
    def test_delete_returns_RESOURCE_NOT_FOUND_CODE(self):
        # Setup
        mock_data_client = MagicMock()
        mock_data_client.delete.return_value = False
        data_handler = DataHandler(mock_data_client)

        # Execute
        result = data_handler.handle_delete_request(mock_data_client)

        # Assert
        self.assertEqual(result[0], Constants.RESOURCE_NOT_FOUND_CODE)
        self.assertEqual(result[1], Constants.RESOURCE_NOT_FOUND_MESSAGE)
Exemplo n.º 4
0
    def test_count_returns_SUCCESS_RESPONSE(self):
        # Setup
        mock_data_client = MagicMock()
        mock_data_client.count.return_value = 10
        data_handler = DataHandler(mock_data_client)

        # Execute
        result = data_handler.handle_count_request()

        # Assert
        self.assertEqual(result[0], Constants.SUCCESS_RESPONSE_CODE)
        self.assertEqual(result[1], json.dumps(10))
Exemplo n.º 5
0
    def test_post_with_failure_returns_error(self):
        # Setup
        mock_data_client = MagicMock()
        mock_data_client.create.return_value = False
        payload = {'name': 'item'}
        data_handler = DataHandler(mock_data_client)

        # Execute
        result = data_handler.handle_post_request(payload)

        # Assert
        self.assertEqual(result, "Failed to create new item: {'name': 'item'}")
Exemplo n.º 6
0
    def test_update_returns_SUCCESS_RESPONSE(self):
        # Setup
        mock_data_client = MagicMock()
        mock_data_client.delete.return_value = True
        mock_data_client.create.return_value = True
        data_handler = DataHandler(mock_data_client)

        # Execute
        result = data_handler.handle_update_request(
            mock_data_client, mock_data_client)

        # Assert
        self.assertEqual(result[0], Constants.CREATED_SUCCESS_CODE)
Exemplo n.º 7
0
    def test_purge_returns_SUCCESS_RESPONSE(self):
        # Setup
        mock_data_client = MagicMock()
        purge_message = {'message': 'Purge Successful!'}
        mock_data_client.purge.return_value = purge_message
        data_handler = DataHandler(mock_data_client)

        # Execute
        result = data_handler.handle_purge_request()

        # Assert
        self.assertEqual(result[0], Constants.SUCCESS_RESPONSE_CODE)
        self.assertEqual(result[1], json.dumps(purge_message))
Exemplo n.º 8
0
    def test_get_single_with_no_item_returns_RESOURCE_NOT_FOUND(self):
        # Setup
        mock_client = MagicMock()
        mock_id = MagicMock()
        mock_client.get.return_value = None
        data_handler = DataHandler(mock_client)

        # Execute
        result = data_handler.handle_get_request_single(mock_id)

        # Assert
        self.assertEqual(result[0], Constants.RESOURCE_NOT_FOUND_CODE)
        self.assertEqual(result[1], Constants.RESOURCE_NOT_FOUND_MESSAGE)
Exemplo n.º 9
0
    def test_get_with_negative_limit_returns_BAD_REQUEST_CODE(self):
        # Setup
        mock_request = MagicMock()
        mock_data_client = MagicMock()
        mock_data_client.filter.return_value = ("mock_items", "mock_offset",
                                                -1, "mock_count")
        data_handler = DataHandler(mock_data_client)

        # Execute
        result = data_handler.handle_get_request(mock_request)

        # Assert
        self.assertEqual(result[0], Constants.BAD_REQUEST_CODE)
        self.assertEqual(result[1], Constants.BAD_REQUEST_MESSAGE)
Exemplo n.º 10
0
    def test_get_with_item_returns_BAD_REQUEST_CODE(self):
        # Setup
        mock_request = MagicMock()
        mock_data_client = MagicMock()
        mock_data_client.read.return_value = ValueError
        data_handler = DataHandler(mock_data_client)

        # Execute
        mock_request.args = dict()
        result = data_handler.handle_get_request(mock_request)

        # Assert
        self.assertEqual(result[0], Constants.BAD_REQUEST_CODE)
        self.assertEqual(result[1], Constants.BAD_REQUEST_MESSAGE)
Exemplo n.º 11
0
    def test_get_single_with_item_returns_SUCCESS_RESPONSE_CODE(self):
        # Setup
        mock_id = MagicMock()
        mock_data_client = MagicMock()
        item = [{'name': 'item'}]
        mock_data_client.get.return_value = item
        data_handler = DataHandler(mock_data_client)

        # Execute
        result = data_handler.handle_get_request_single(mock_id)

        # Assert
        self.assertEqual(result[0], Constants.SUCCESS_RESPONSE_CODE)
        self.assertEqual(result[1], json.dumps(item))
Exemplo n.º 12
0
    def test_post_with_request_returns_CREATED_SUCCESS(self):
        # Setup
        mock_model = MagicMock()
        mock_data_client = MagicMock()
        payload = {'name': 'item'}
        mock_model.add.return_value = payload
        data_handler = DataHandler(mock_data_client)

        # Execute
        result = data_handler.handle_post_request(payload)

        # Assert
        self.assertEqual(result[0], Constants.CREATED_SUCCESS_CODE)
        self.assertEqual(result[1], json.dumps(payload))
Exemplo n.º 13
0
    def test_get_with_item_returns_SUCCESS_RESPONSE_CODE(self):
        # Setup
        mock_request = MagicMock()
        mock_data_client = MagicMock()
        items = [{'name1': 'item1'}, {'name2': 'item2'}]
        mock_data_client.count.return_value = 2
        mock_data_client.filter.return_value = [items, 0, 25, 2]
        data_handler = DataHandler(mock_data_client)

        # Execute
        mock_request.args = dict()
        result = data_handler.handle_get_request(mock_request)
        # Assert
        self.assertEqual(result[0], Constants.SUCCESS_RESPONSE_CODE)
        result_data = json.loads(result[1])
        self.assertEqual(result_data[list(result_data)[0]], items)
Exemplo n.º 14
0
 def __init__(self, **kwargs):
     self.client = kwargs.get(Constants.CLIENT,
                              MongoDataClient(table=self.name))
     self.model = kwargs.get(Constants.MODEL, BaseModel())
     self.data_handler = DataHandler(self.client)
     self.healthcheck = HealthCheckProvider(mongo=self.client)
Exemplo n.º 15
0
class DataController(metaclass=Meta):
    name = Settings.SERVICE_NAME
    model = BaseModel()
    monitor = MonitoringProvider()
    logger = LoggingProvider(name)

    def __init__(self, **kwargs):
        self.client = kwargs.get(Constants.CLIENT,
                                 MongoDataClient(table=self.name))
        self.model = kwargs.get(Constants.MODEL, BaseModel())
        self.data_handler = DataHandler(self.client)
        self.healthcheck = HealthCheckProvider(mongo=self.client)

    # pylint: disable=no-self-use
    @http(Constants.OPTIONS_REQUEST, get_route())
    def options(self, _):
        return Constants.SUCCESS_RESPONSE_CODE, version()

    # pylint: disable=no-self-use
    @http(Constants.OPTIONS_REQUEST, get_item_route())
    def options_item(self, _, item_id):
        return Constants.SUCCESS_RESPONSE_CODE, item_id

    @http(Constants.GET_REQUEST, get_route())
    def get(self, request):
        self.logger.info(
            get_log_string(request, Constants.GET_REQUEST, self.name))
        result = self.data_handler.handle_get_request(request)
        return result

    @http(Constants.GET_REQUEST, get_item_route())
    def get_one(self, request, item_id):
        self.logger.info(
            get_log_string(request, Constants.GET_REQUEST, self.name))
        return self.data_handler.handle_get_request_single(item_id)

    # pylint: disable=no-self-use
    @http(Constants.GET_REQUEST, get_route(Constants.PING_ROUTE))
    def ping(self, _):
        return Constants.SUCCESS_RESPONSE_CODE, Constants.PING_RESPONSE

    @http(Constants.GET_REQUEST, get_route(Constants.COUNT_ROUTE))
    def count(self, request):
        self.logger.info(
            get_log_string(request, Constants.GET_REQUEST, self.name))
        return self.data_handler.handle_count_request()

    @http(Constants.GET_REQUEST, get_route(Constants.HEALTH_ROUTE))
    def health(self, request):
        self.logger.info(
            get_log_string(request, Constants.GET_REQUEST, self.name,
                           Constants.HEALTH))
        return Constants.SUCCESS_RESPONSE_CODE, self.healthcheck.check_health()

    @http(Constants.GET_REQUEST, get_route(Constants.VERSION_ROUTE))
    def version(self, request):
        self.logger.info(
            get_log_string(request, Constants.GET_REQUEST, self.name))
        return Constants.SUCCESS_RESPONSE_CODE, version()

    @http(Constants.POST_REQUEST, get_route())
    def post(self, request):
        self.logger.info(
            get_log_string(request, Constants.POST_REQUEST, self.name))
        payload = json.loads(request.get_data(as_text=True))
        if self.model.validate(payload):
            return self.data_handler.handle_post_request(payload)
        return Constants.UNPROCESSABLE_ENTITY_CODE, Constants.UNPROCESSABLE_ENTITY_MESSAGE

    @http(Constants.PUT_REQUEST, get_item_route())
    def put(self, request, item_id):
        self.logger.info(
            get_log_string(request, Constants.PUT_REQUEST, self.name))
        payload = json.loads(request.get_data(as_text=True))
        return self.data_handler.handle_update_request(item_id, payload)

    @http(Constants.POST_REQUEST, get_route(Constants.RESTORE_ROUTE))
    def restore(self, request):
        self.logger.info(
            get_log_string(request, Constants.POST_REQUEST, self.name))
        payload = json.loads(request.get_data(as_text=True))
        return self.data_handler.handle_restore_request(payload)

    @http(Constants.PUT_REQUEST, get_route())
    @http(Constants.DELETE_REQUEST, get_route())
    def bad_verbs(self, request):
        self.logger.info(
            get_log_string(request, Constants.PUT_REQUEST, self.name))
        return Constants.BAD_REQUEST_CODE, Constants.BAD_REQUEST_MESSAGE

    @http(Constants.DELETE_REQUEST, get_item_route())
    @http(Constants.DELETE_REQUEST, get_route())
    def delete(self, request, item_id):
        self.logger.info(
            get_log_string(request, Constants.DELETE_REQUEST, self.name,
                           item_id))
        self.logger.info(f'{self.name} DELETE {item_id} called...')
        return self.data_handler.handle_delete_request(item_id)

    @http(Constants.POST_REQUEST, get_route(Constants.PURGE_ROUTE))
    def purge(self, request):
        self.logger.info(
            get_log_string(request, Constants.POST_REQUEST, self.name))
        return self.data_handler.handle_purge_request()