Ejemplo n.º 1
0
    def get_all_route_configs(self):
        data = self._file_helper.read_yaml(self.CONFIG_FILE_PATH)

        route_configs = []
        for route in self._data_retriever_helper.get_value(data, '.routes'):
            query = route.get('query')
            if query:
                for key, values in query.items():
                    if isinstance(values, str):
                        query[key] = [values]

            listen_path = self._data_retriever_helper.get_value(
                route, '.listen_path')
            if listen_path is not None and listen_path.startswith('/'):
                listen_path = listen_path[1:]

            route_configs.append(
                entities.RouteConfig(
                    listen_path=listen_path,
                    method=self._data_retriever_helper.get_value(
                        route, '.method', 'GET'),
                    status_code=self._data_retriever_helper.get_value(
                        route, '.status_code', 200),
                    query=query,
                    response=entities.RouteResponseConfig(
                        replace_funcs=self._data_retriever_helper.get_value(
                            route, '.response.replace_funcs'),
                        data=self._data_retriever_helper.get_value(
                            route, '.response.data'),
                        file_path=self._data_retriever_helper.get_value(
                            route, '.response.file_path'),
                    )))

        return route_configs
Ejemplo n.º 2
0
def test_check_mock_response_exists():
    mat_config_repository = mock.MagicMock(
        spec=repositories.MatConfigRepositoryBase)
    mat_config_repository.query_route_config.return_value = entities.RouteConfig(
        listen_path='listen_path',
        method='GET',
        status_code=200,
        query=None,
        response=entities.RouteResponseConfig(),
    )

    uc = CheckIfMockResponseExistsUseCase(
        mat_config_repository=mat_config_repository, )

    assert uc.execute(
        entities.ClientRequest(method='GET',
                               path='path',
                               query_string='query_string',
                               headers={},
                               raw_body=b'')) is True

    mat_config_repository.query_route_config.assert_called_with(
        path='path',
        method='GET',
        query_string='query_string',
    )
Ejemplo n.º 3
0
def test_get_mock_response_using_response_file_path():
    data = b'bytes'

    client_request = entities.ClientRequest(
        method='GET',
        path='path',
        query_string='name=name',
        headers={},
        raw_body=b'',
    )

    route_config = entities.RouteConfig(
        listen_path=client_request.path,
        method=client_request.method,
        status_code=200,
        query=urllib.parse.parse_qs(client_request.query_string),
        response=entities.RouteResponseConfig(file_path='file_path'),
    )

    template_service = mock.MagicMock(spec=services.TemplateService)

    mat_config_repository = mock.MagicMock(
        spec=repositories.MatConfigRepositoryBase)
    mat_config_repository.query_route_config.return_value = route_config

    file_helper = mock.MagicMock(spec=helpers.FileHelperBase)
    file_helper.join_file_paths.return_value = 'joined_file_path'
    file_helper.read_bytes.return_value = data
    file_helper.guess_file_type.return_value = 'application/json'

    json_helper = mock.MagicMock(spec=helpers.JSONHelperBase)

    uc = GetMockResponseUseCase(
        template_service=template_service,
        mat_config_repository=mat_config_repository,
        file_helper=file_helper,
        json_helper=json_helper,
    )
    assert uc.execute(client_request) == entities.ServerResponse(
        raw_body=data,
        status_code=route_config.status_code,
        headers={
            'Content-Type': 'application/json',
        },
    )

    mat_config_repository.query_route_config.assert_called_with(
        path=client_request.path,
        method=client_request.method,
        query_string=client_request.query_string,
    )

    file_helper.join_file_paths.assert_called_with('mat-data', 'file_path')
    file_helper.read_bytes.assert_called_with('joined_file_path')
    file_helper.guess_file_type.assert_called_with('joined_file_path')
Ejemplo n.º 4
0
def test_get_mock_response_using_response_data_with_json_type():
    client_request = entities.ClientRequest(
        method='GET',
        path='path',
        query_string='name=name',
        headers={},
        raw_body=b'',
    )

    route_config = entities.RouteConfig(
        listen_path=client_request.path,
        method=client_request.method,
        status_code=200,
        query=urllib.parse.parse_qs(client_request.query_string),
        response=entities.RouteResponseConfig(data={
            'msg': 'data',
        }),
    )

    template_service = mock.MagicMock(spec=services.TemplateService)

    mat_config_repository = mock.MagicMock(
        spec=repositories.MatConfigRepositoryBase)
    mat_config_repository.query_route_config.return_value = route_config

    file_helper = mock.MagicMock(spec=helpers.FileHelperBase)
    json_helper = mock.MagicMock(spec=helpers.JSONHelperBase)
    json_helper.serialize.return_value = json.dumps(route_config.response.data)

    uc = GetMockResponseUseCase(
        template_service=template_service,
        mat_config_repository=mat_config_repository,
        file_helper=file_helper,
        json_helper=json_helper,
    )
    assert uc.execute(client_request) == entities.ServerResponse(
        raw_body=json_helper.serialize.return_value.encode(),
        status_code=route_config.status_code,
        headers={
            'Content-Type': 'application/json',
        },
    )

    mat_config_repository.query_route_config.assert_called_with(
        path=client_request.path,
        method=client_request.method,
        query_string=client_request.query_string,
    )
Ejemplo n.º 5
0
def test_get_mock_response_with_conflict_response_config():
    client_request = entities.ClientRequest(
        method='GET',
        path='path',
        query_string='name=name',
        headers={},
        raw_body=b'',
    )

    route_config = entities.RouteConfig(
        listen_path=client_request.path,
        method=client_request.method,
        status_code=200,
        query=urllib.parse.parse_qs(client_request.query_string),
        response=entities.RouteResponseConfig(
            file_path='file_path',
            data='raw_data',
        ),
    )

    template_service = mock.MagicMock(spec=services.TemplateService)
    mat_config_repository = mock.MagicMock(
        spec=repositories.MatConfigRepositoryBase)
    mat_config_repository.query_route_config.return_value = route_config
    file_helper = mock.MagicMock(spec=helpers.FileHelperBase)
    json_helper = mock.MagicMock(spec=helpers.JSONHelperBase)

    uc = GetMockResponseUseCase(
        template_service=template_service,
        mat_config_repository=mat_config_repository,
        file_helper=file_helper,
        json_helper=json_helper,
    )

    with pytest.raises(exceptions.ValidationError, match='回傳資源衝突'):
        assert uc.execute(client_request)

    mat_config_repository.query_route_config.assert_called_with(
        path=client_request.path,
        method=client_request.method,
        query_string=client_request.query_string,
    )
Ejemplo n.º 6
0
def test_query_route_config():
    file_helper = mock.MagicMock(spec=helpers.FileHelperBase)
    file_helper.read_yaml.return_value = {
        'routes': [
            {
                'listen_path': 'path',
                'method': 'GET',
                'status_code': 200,
                'query': {
                    'name': '大類',
                },
                'response': {
                    'data': '哈囉 廢物',
                    'file_path': 'file_path',
                }
            }
        ]
    }

    data_retriever_helper = DataRetrieverHelper()

    mat_config_repository = MatConfigRepository(
        file_helper=file_helper,
        data_retriever_helper=data_retriever_helper,
    )

    assert mat_config_repository.query_route_config(
        path='path',
        method='GET',
        query_string='name=大類',
    ) == entities.RouteConfig(
        listen_path='path',
        method='GET',
        status_code=200,
        query={
            'name': ['大類']
        },
        response=entities.RouteResponseConfig(
            data='哈囉 廢物',
            file_path='file_path'
        )
    )
Ejemplo n.º 7
0
def test_get_mat_config():
    mat_config = entities.MatConfig(
        server=entities.ServerConfig(proxy_url='proxy_url', ),
        routes=[
            entities.RouteConfig(listen_path='listen_path',
                                 method='GET',
                                 status_code=200,
                                 query={'key': ['value']},
                                 response=entities.RouteResponseConfig(
                                     file_path='file_path',
                                     data='data',
                                 ))
        ])

    mat_config_repository = mock.MagicMock(
        spec=repositories.MatConfigRepositoryBase)
    mat_config_repository.get_config.return_value = mat_config

    uc = GetConfigUseCase(mat_config_repository=mat_config_repository, )

    assert uc.execute() == mat_config