def test_coverage(self): with user_set(app, FakeUserAuth, 'bobitto'): response = self.query('v1/coverage') r = get_not_null(response, 'regions') region_ids = {region['id']: region for region in r} assert len(region_ids) == 3 assert 'main_routing_test' in region_ids assert 'departure_board_test' in region_ids # bob have not the access to this region, but the region is free so it is there anyway assert 'empty_routing_test' in region_ids with user_set(app, FakeUserAuth, 'bobette'): response = self.query('v1/coverage') r = get_not_null(response, 'regions') region_ids = {region['id']: region for region in r} assert len(region_ids) == 1 # bobette does not have access to anything, so we only have the free region here assert 'empty_routing_test' in region_ids with user_set(app, FakeUserAuth, 'tgv'): response = self.query('v1/coverage') r = get_not_null(response, 'regions') region_ids = {region['id']: region for region in r} assert len(region_ids) == 1 # tgv must not see free regions assert 'empty_routing_test' not in region_ids
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')
def test_places_for_tgv(self): with user_set(app, FakeUserAuth, "tgv"): response = self.query('v1/coverage/main_routing_test/places?q=toto') assert 'error' not in response _, status = self.query_no_assert('v1/coverage/departure_board_test/places?q=toto') assert status == 403 _, status = self.query_no_assert('v1/coverage/empty_routing_test/places?q=toto') assert status == 403
def test_status_code_coords(self): """ Test of status response for coverage containing coordinates """ with user_set(app, FakeUserAuth, 'bob'): # Coverage by coords response = self.app.get('/v1/coverage/{coords}'.format(coords=s_coord)) assert response.status_code == 200
def test_journeys_for_bobitto(self): with user_set(app, FakeUserAuth, 'bobitto'): response = self.query( '/v1/journeys?from=stopA&to=stopB&datetime=20120614T080000') assert 'error' not in response response = self.query( '/v1/journeys?from=stop1&to=stop2&datetime=20120614T080000') assert 'error' not in response
def test_pt_ref_for_bobette(self): with user_set(app, FakeUserAuth, 'bobette'): _, status = self.query_no_assert('v1/coverage/main_routing_test/stop_points') assert status == 403 _, status = self.query_no_assert('v1/coverage/departure_board_test/stop_points') assert status == 403 _, status = self.query_no_assert('v1/coverage/empty_routing_test/stop_points') assert status == 404 # same than for bobitto, we have access to the region, but no stops
def test_wrong_journeys_for_bobitto(self): """ we query with one stop for main_routing and one from departure_board, we have access to both, but we get an error """ with user_set(app, FakeUserAuth, 'bobitto'): response, status = self.query_no_assert('/v1/journeys?from=stopA&to=stop2&datetime=20120614T080000') assert status == 404 assert 'error' in response and response['error']['id'] == "unknown_object"
def test_sort_coverage(self): with user_set(app, FakeUserAuth, 'bobitto'): response = self.query('v1/coverage') regions = get_not_null(response, 'regions') assert len(regions) == 3 assert regions[0]["name"] == 'departure board' assert regions[1]["name"] == 'empty routing' assert regions[2]["name"] == 'routing api data'
def test_status_code(self): """ We query the api with user 6 who must not be blocked """ requests_status_codes = [('/v1/coverage/main_routing_test', 200)] with user_set(app, FakeUserAuth, 'test_user_not_blocked'): for request, status_code in requests_status_codes: assert (self.app.get(request).status_code == status_code)
def test_coverage(self): """ User only has access to the first region """ with user_set(app, FakeUserAuth, 'bob'): response_obj = self.app.get('/v1/coverage') response = json.loads(response_obj.data) assert ('regions' in response) assert (len(response['regions']) == 1) assert (response['regions'][0]['id'] == "main_routing_test")
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
def test_status_code(self): """ We query the api with user 5 who must be blocked """ requests_status_codes = [('/v1/coverage/main_routing_test', 429), ('/v1/coverage/departure_board_test', 429)] with user_set(app, FakeUserAuth, 'test_user_blocked'): for request, status_code in requests_status_codes: assert (self.app.get(request).status_code == status_code)
def test_unkown_region_coords(self): """ Test coverage containing coordinates, coordinate outside region """ with user_set(app, FakeUserAuth, 'bob'): # coords outside regions r, status = self.query_no_assert('/v1/coverage/{lon};{lat}/stop_areas'.format(lon=20, lat=15)) assert status == 404 assert 'error' in r assert get_not_null(r, 'error')['message'] == "No region available for the coordinates:20.0, 15.0"
def test_coverage_by_coords(self): """ Test coverage containing coordinates, bobitto user authorized """ with user_set(app, FakeUserAuth, 'bobitto'): response = self.query('v1/coverage/{coords}'.format(coords=s_coord)) r = get_not_null(response, 'regions') assert {region['id'] for region in r} == {'main_routing_test'}
def test_speficic_places_authentication(self): """ for a specific coverage's places, there is still only one coverage """ with user_set(app, FakeUserAuth, 'bob'): 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
def test_journeys_for_tgv(self): with user_set(app, FakeUserAuth, 'tgv'): response = self.query('/v1/journeys?from=stopA&to=stopB&datetime=20120614T080000') assert 'error' not in response _, status = self.query_no_assert('/v1/journeys?from=stop1&to=stop2&datetime=20120614T080000') assert status == 403 _, status = self.query_no_assert( '/v1/coverage/empty_routing_test/journeys?from=stop1&to=stop2&datetime=20120614T080000' ) assert status == 403
def test_global_places_authentication(self): """ On a public navitia, a user can use all the instances """ with user_set(app, FakeUserAuth, 'bob'): with requests_mock.Mocker() as m: m.get(requests_mock.ANY, json=BRAGI_RESPONSE) r, status = self.query_no_assert('/v1/places?q=bob') assert status == 200 assert m.called check_dataset(m, {'main_routing_test', 'empty_routing_test', 'departure_board_test'})
def test_stop_schedules_for_bobette(self): with user_set(app, FakeUserAuth, 'bobette'): _, status = self.query_no_assert('v1/coverage/main_routing_test/stop_areas/stopA/stop_schedules') assert status == 403 _, status = self.query_no_assert('v1/coverage/departure_board_test/stop_areas/stop1/stop_schedules') assert status == 403 # we get a 404 (because 'stopbidon' cannot be found) and not a 403 _, status = self.query_no_assert( 'v1/coverage/empty_routing_test/stop_areas/' 'stopbidon/stop_schedules' ) assert status == 404
def test_places_for_bobette(self): with user_set(app, FakeUserAuth, "bobette"): _, status = self.query_no_assert('v1/coverage/main_routing_test/places?q=toto') assert status == 403 _, status = self.query_no_assert('v1/coverage/departure_board_test/places?q=toto') assert status == 403 response = self.query('v1/coverage/empty_routing_test/places?q=toto') assert 'error' not in response # this test suppose no elasticsearch is lanched at localhost _, status = self.query_no_assert('v1/places?q=toto') assert status == 500
def test_stop_schedules_for_tgv(self): with user_set(app, FakeUserAuth, 'tgv'): response = self.query( 'v1/coverage/main_routing_test/stop_areas/stopA/stop_schedules?from_datetime=20120614T080000' ) assert 'error' not in response _, status = self.query_no_assert('v1/coverage/departure_board_test/stop_areas/stop1/stop_schedules') assert status == 403 _, status = self.query_no_assert( 'v1/coverage/empty_routing_test/stop_areas/' 'stopbidon/stop_schedules' ) assert status == 403
def test_users_default_values(self): """ User With default values """ with user_set(app, FakeUserAuth, 'bob'): response_obj = self.app.get('/v1/users') response = json.loads(response_obj.data) assert response["type"] == "with_free_instances" assert "shape_scope" in response assert len(DEFAULT_SHAPE_SCOPE) == len(response['shape_scope']) for ss in response['shape_scope']: assert ss in DEFAULT_SHAPE_SCOPE
def test_pt_ref_for_bobitto(self): with user_set(app, FakeUserAuth, 'bobitto'): response = self.query('v1/coverage/main_routing_test/stop_points') assert 'error' not in response response = self.query('v1/coverage/departure_board_test/stop_points') assert 'error' not in response # the empty region is empty, so no stop points. but we check that we have no authentication errors response, status = self.query_no_assert('v1/coverage/empty_routing_test/stop_points') assert status == 404 assert 'error' in response assert 'unknown_object' in response['error']['id']
def test_unkown_region(self): """ the authentication process must not mess if the region is not found """ with user_set(app, FakeUserAuth, 'bob'): r, status = self.query_no_assert( '/v1/coverage/the_marvelous_unknown_region/stop_areas') assert status == 404 assert 'error' in r assert get_not_null(r, 'error')['message'] \ == "The region the_marvelous_unknown_region doesn't exists"
def test_global_places_authentication(self): """ On a public navitia, a user can use all the instances """ with user_set(app, FakeUserAuth, 'bob'): with mock.patch( 'requests.get', DatasetChecker({ 'main_routing_test', 'empty_routing_test', 'departure_board_test' })): r, status = self.query_no_assert('/v1/places?q=bob') assert status == 200
def test_speficic_places_authentication(self): """ for a specific coverage's places, there is still only one coverage """ with user_set(app, FakeUserAuth, 'bob'): with requests_mock.Mocker() as m: m.post(requests_mock.ANY, json=BRAGI_RESPONSE) _, status = self.query_no_assert( '/v1/coverage/main_routing_test/places?q=bob&_autocomplete=bragi' ) assert status == 200 assert m.called check_dataset(m, {'main_routing_test'})
def test_places_for_user_with_coord(self): """ Test that with a default_coord on user, it is correctly posted """ with user_set(app, FakeUserBragi, "test_user_with_coord"): def http_get(url, *args, **kwargs): params = kwargs.pop('params') assert params assert params.get('lon') == '12' assert params.get('lat') == '42' return MockResponse({}, 200, '') with mock.patch('requests.get', http_get): self.query('v1/coverage/main_routing_test/places?q=toto&_autocomplete=bragi')
def test_places_for_user_with_coord_and_coord_overriden_to_null(self): """ Test that with a default_coord on user, if the user gives an empty coord we do not pass a coord """ with user_set(app, FakeUserBragi, "test_user_with_coord"): def http_get(url, *args, **kwargs): params = kwargs.pop('params') assert params assert not params.get('lon') assert not params.get('lat') return MockResponse({}, 200, '') with mock.patch('requests.get', http_get): self.query('v1/coverage/main_routing_test/places?q=toto&_autocomplete=bragi&from=')
def test_pt_ref_for_bobitto_coords(self): """ Test ptref for coverage containing coordinates """ with user_set(app, FakeUserAuth, 'bobitto'): # By coords: All stops of main_routing_test coverage response = self.query('v1/coverage/{coords}/stop_points'.format(coords=s_coord)) stop_points = get_not_null(response, 'stop_points') assert len(stop_points) == 4 response = self.query( 'v1/coverage/{coords}/stop_points/{id}'.format(coords=s_coord, id='stop_point:stopB') ) stop_points = get_not_null(response, 'stop_points') assert len(stop_points) == 1 assert stop_points[0]['id'] == 'stop_point:stopB'
def test_users_with_shape(self): """ User with shape and shape_scope """ with user_set(app, FakeUserAuth, 'api_users'): response_obj = self.app.get('/v1/users') response = json.loads(response_obj.data) assert response["type"] == "with_free_instances" assert len(response['shape_scope']) == 2 for ss in response['shape_scope']: assert ss in ["poi", "admin"] assert "shape" in response assert "coord" in response assert response["coord"]["lon"] == "2.4" assert response["coord"]["lat"] == "48.6" assert response["block_until"] == "20191005T235830"
def test_status_code(self): """ We query the api with user 1 who have access to the main routintg test and not to the departure board """ requests_status_codes = [ ('/v1/coverage/main_routing_test', 200), ('/v1/coverage/departure_board_test', 403), # stopA and stopB and in main routing test, all is ok ('/v1/journeys?from=stopA&to=stopB&datetime=20120614T080000', 200), # stop1 is in departure board -> KO ('/v1/journeys?from=stopA&to=stop2&datetime=20120614T080000', 403), # stop1 and stop2 are in departure board -> KO ('/v1/journeys?from=stop1&to=stop2&datetime=20120614T080000', 403) ] with user_set(app, FakeUserAuth, 'bob'): for request, status_code in requests_status_codes: assert (self.app.get(request).status_code == status_code)