Beispiel #1
0
 def http_get(url, *args, **kwargs):
     params = kwargs.pop('params')
     assert params
     assert params.get('type[]') == [
         'stop_area', 'address', 'poi', 'city'
     ]
     return MockResponse({}, 200, '')
Beispiel #2
0
    def test_places_for_user_with_shape(self):
        """
        Test that with a shape on user, it is correctly posted
        """
        with user_set(app, FakeUserBragi, "test_user_with_shape"):

            mock_post = mock.MagicMock(return_value=MockResponse({}, 200, '{}'))

            def http_get(url, *args, **kwargs):
                assert False

            with mock.patch('requests.get', http_get):
                with mock.patch('requests.post', mock_post):

                    self.query('v1/coverage/main_routing_test/places?q=toto&_autocomplete=bragi')
                    assert mock_post.called

                    mock_post.reset_mock()
                    self.query('v1/places?q=toto')
                    assert mock_post.called

            # test that the shape is posted
            def http_post(url, *args, **kwargs):
                json = kwargs.pop('json')
                assert json['shape']['type'] == 'Feature'
                assert json.get('shape').get('geometry')
                return MockResponse({}, 200, '{}')

            with mock.patch('requests.get', http_get):
                with mock.patch('requests.post', http_post):
                    self.query('v1/coverage/main_routing_test/places?q=toto&_autocomplete=bragi')
                    self.query('v1/places?q=toto')
Beispiel #3
0
 def __call__(self, *args, **kwargs):
     d = kwargs.get('params', {}).get('pt_dataset')
     if d is not None:
         assert set(d) == self.datasets
     else:
         assert d == self.datasets
     return MockResponse({"features": []}, 200, url='')
Beispiel #4
0
 def http_get(url, *args, **kwargs):
     params = kwargs.pop('params')
     assert params
     assert params.get('type[]') == [
         'public_transport:stop_area', 'street', 'house', 'poi', 'city'
     ]
     return MockResponse({}, 200, '')
Beispiel #5
0
def mock_here(_, url, params):
    s = (8.98312e-05, 8.98312e-05)
    r = (0.00071865, 0.00188646)

    a = (0.000718649585564, 0.00107797437835)
    b = (0.000269493594586, 8.98311981955e-05)
    c = (0.00107797437835, 0.000628818387368)
    useless_a = a

    if url == 'https://matrix.route.bob.here.com/routing/7.2/calculatematrix.json':
        if check_here_point(params['start0'], s):
            # we check that the destinations are correct since the order is important
            assert check_here_point(params.get('destination0'), b)
            assert check_here_point(params.get('destination1'), c)
            assert check_here_point(params.get('destination2'), a)
            assert check_here_point(params.get('destination3'), useless_a)

            # we also check that we gave HERE the departure datetime (to get realtime info)
            # NOTE: since the instance has no timezone we consider the request as UTC
            assert params.get('departure') == '2012-06-14T07:00:00Z'

            return MockResponse(HERE_BEGGINING_MATRIX_RESPONSE, 200)
        if check_here_point(params['destination0'], r):
            assert check_here_point(params.get('start0'), a)
            assert check_here_point(params.get('start1'), useless_a)
            assert check_here_point(params.get('start2'), c)
            assert params.get('departure') == '2012-06-14T07:00:00Z'
            return MockResponse(HERE_END_MATRIX_RESPONSE, 200)
    elif url == 'https://route.bob.here.com/routing/7.2/calculateroute.json':
        if check_here_point(params['waypoint0'], s) and check_here_point(
                params['waypoint1'], r):
            assert params.get('departure') == '2012-06-14T07:00:00Z'
            return MockResponse(HERE_ROUTING_RESPONSE_DIRECT_PATH, 200)
        if check_here_point(params['waypoint0'], s) and check_here_point(
                params['waypoint1'], b):
            assert params.get('departure') == '2012-06-14T08:01:00Z'
            return MockResponse(HERE_ROUTING_RESPONSE_BEGINNING_FALLBACK_PATH,
                                200)
        if check_here_point(params['waypoint0'], a) and check_here_point(
                params['waypoint1'], r):
            assert params.get('departure') == '2012-06-14T08:01:02Z'
            return MockResponse(HERE_ROUTING_RESPONSE_END_FALLBACK_PATH, 200)
    assert False, 'invalid url'
 def test_global_places(self):
     """
     test the v1/places authentication
     """
     bragi_response_get = lambda *args, **kwargs: MockResponse(
         {"features": []}, 200, url='')
     with mock.patch('requests.get', bragi_response_get):
         # bob is a normal user, it can access the open_data, he thus can access the global places
         with user_set(app, FakeUserAuth, 'bob'):
             r, status = self.query_no_assert('/v1/places?q=bob')
             assert status == 200
         # tgv has not access to the open_data, he cannot access the global places
         with user_set(app, FakeUserAuth, 'tgv'):
             _, status = self.query_no_assert('/v1/places?q=bob')
             assert status == 403
         # super_user_not_open cannot access the open data, but since he is a super user, he can access /places
         with user_set(app, FakeUserAuth, 'super_user_not_open'):
             _, status = self.query_no_assert('/v1/places?q=bob')
             assert status == 200
Beispiel #7
0
    def test_places_for_user_without_shape(self):
        """
        Test that without shape for user, we use the get method
        """
        with user_set(app, FakeUserBragi, "test_user_no_shape"):

            mock_get = mock.MagicMock(return_value=MockResponse({}, 200, '{}'))

            def http_post(self, url, *args, **kwargs):
                assert False

            with mock.patch('requests.get', mock_get):
                with mock.patch('requests.post', http_post):

                    self.query('v1/coverage/main_routing_test/places?q=toto&_autocomplete=bragi')
                    assert mock_get.called

                    mock_get.reset_mock()
                    self.query('v1/places?q=toto')
                    assert mock_get.called
Beispiel #8
0
    def test_places_authentication(self):
        """
        test the v1/places authentication

        Only users with open data access can use this API and the user's authentication drives the underlying
        query made to bragi (the external autocomplete service).

        navitia gives bragi all the instances the user can use
        """
        no_check = lambda *args, **kwargs: MockResponse(
            {"features": []}, 200, url='')
        # bob is a normal user, it can access the open_data, and it can access main_routing_test
        # he thus can use main_routing_test and empty_routing_test (because it's opendata)
        with user_set(app, FakeUserAuth, 'bob'):
            with mock.patch(
                    'requests.get',
                    DatasetChecker({'main_routing_test',
                                    'empty_routing_test'})):
                r, status = self.query_no_assert('/v1/places?q=bob')
                assert status == 200

        # user_without_any_coverage cannot access anything, so no pt_dataset is given
        with user_set(app, FakeUserAuth, 'user_without_any_coverage'):
            with mock.patch('requests.get', no_check):
                r, status = self.query_no_assert('/v1/places?q=bob')
                assert status == 403

        # tgv has not access to the open_data but can use main_routing_test, it cannot use the global place
        with user_set(app, FakeUserAuth, 'tgv'):
            with mock.patch('requests.get', no_check):
                _, status = self.query_no_assert('/v1/places?q=bob')
                assert status == 403
            # but it can use the main_routing_test places
            with mock.patch('requests.get',
                            DatasetChecker({'main_routing_test'})):
                _, status = self.query_no_assert(
                    '/v1/coverage/main_routing_test/places?q=bob')
                assert status == 200

        # super_user can use all the instances
        with user_set(app, FakeUserAuth, 'super_user'):
            with mock.patch(
                    'requests.get',
                    DatasetChecker({
                        'main_routing_test', 'empty_routing_test',
                        'departure_board_test'
                    })):
                _, status = self.query_no_assert('/v1/places?q=bob')
                assert status == 200

        # but when querying /v1/coverage/<something>/places only one pt_dataset is given to bragi
        with user_set(app, FakeUserAuth, 'super_user'):
            with mock.patch('requests.get',
                            DatasetChecker({'main_routing_test'})):
                _, status = self.query_no_assert(
                    '/v1/coverage/main_routing_test/places?q=bob')
                assert status == 200
            with mock.patch('requests.get',
                            DatasetChecker({'departure_board_test'})):
                _, status = self.query_no_assert(
                    '/v1/coverage/departure_board_test/places?q=bob')
                assert status == 200
Beispiel #9
0
 def http_post(url, *args, **kwargs):
     json = kwargs.pop('json')
     assert json.get('geometry')
     return MockResponse({}, 200, '{}')
def mock_karos(_, params, headers):
    return MockResponse(KAROS_RESPONSE, 200)
Beispiel #11
0
def mock_instance_system(_, params):
    return MockResponse(INSTANCE_SYSTEM_RESPONSE, 200)
Beispiel #12
0
def mock_klaxit(_, params):
    return MockResponse(KLAXIT_RESPONSE, 200)
Beispiel #13
0
 def http_post(url, *args, **kwargs):
     json = kwargs.pop('json')
     assert json['shape']['type'] == 'Feature'
     assert json.get('shape').get('geometry')
     return MockResponse({}, 200, '{}')
Beispiel #14
0
        def http_get(url, *args, **kwargs):
            params = kwargs.pop('params')
            assert params
            assert params.get('type[]') == ['street', 'house']

            return MockResponse({}, 200, '')
Beispiel #15
0
 def http_get(url, *args, **kwargs):
     return MockResponse({}, 422, '')
Beispiel #16
0
def mock_free_floating(_, params):
    return MockResponse(FREE_FLOATINGS_RESPONSE, 200)
Beispiel #17
0
 def http_get(url, *args, **kwargs):
     params = kwargs.pop('params')
     assert params
     assert params.get('lon') == '3.25'
     assert params.get('lat') == '49.84'
     return MockResponse({}, 200, '')
def mock_instant_system(_, params, headers):
    return MockResponse(INSTANT_SYSTEM_RESPONSE, 200)
def mock_free_floating(_, params):
    return MockResponse(VEHICLE_OCCUPANCIES_RESPONSE, 200)
def mock_blablalines(_, params):
    return MockResponse(BLABLALINES_RESPONSE, 200)