def test_cache(client, user, django_assert_num_queries, django_assert_no_duplicate_queries, settings): settings.CACHES = { 'api': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache' } } service = PartnerViewSet.get_service() url = f"{service.endpoint}?country_name=bolivia" client.force_authenticate(user) with user_allow_country(user, 'bolivia'): with user_allow_service(user, PartnerViewSet): res = client.get(url) assert res.status_code == 200, res.content assert res['cache-version'] == str(service.cache_version) assert res['cache-ttl'] == '1y' key = res['cache-key'] etag = res['etag'] with django_assert_no_duplicate_queries( ignored=['.*FROM "constance_config"']): res = client.get(url) assert res.status_code == 200, res.content assert res['cache-version'] == str(service.cache_version) assert res['cache-key'] == key assert res['cache-ttl'] == '1y' assert res['etag'] == etag assert res['cache-hit'] == str(True)
def test_permission_check_serializer_allow(client, allow_many_serializer): url = allow_many_serializer.service.endpoint view = allow_many_serializer.service.viewset client.force_authenticate(allow_many_serializer.user) with user_allow_country(allow_many_serializer.user, 'bolivia'): res = client.get("%s?%s=short" % (url, quote(view.serializer_field_param))) assert res.status_code == 200
def test_access(db, user_type, op, query, code, allowed): # etools user has access same countries as in eTools app user = user_type() client = APIClient() client.force_authenticate(user) url = HACTAggreagateViewSet.get_service().endpoint with user_allow_service(user, HACTAggreagateViewSet): with user_allow_country(user, allowed): res = client.get(f"{url}?country_name{op}{query}") assert res.status_code == code, res.content
def test_list_web(username, viewset): user = User.objects.get(username=username) client = APIClient() client.force_authenticate(user) with user_allow_country(user, "bolivia"): with user_allow_service(user, viewset): res = client.get(viewset.get_service().endpoint, HTTP_ACCEPT="text/html") assert res.status_code == 200, res.content assert res["Content-Type"] == "text/html; charset=utf-8"
def test_filter_cache_country_arg(db, client, flt, monkeypatch): fake = MockCache() monkeypatch.setattr('etools_datamart.api.filtering.cache', fake) url = f"/api/latest/sources/etools/partners/assessment/?%s" % flt with user_allow_service(client.handler._force_user, EtoolsAssessmentViewSet): with user_allow_country(client.handler._force_user, ["bolivia", "chad"]): client.get(url) res = client.get(url) assert fake.data assert res.status_code == 200, res.content assert res.json()
def test_etools_user_access_allowed_countries(params): # etools user has access same countries as in eTools app user_type, viewset, code = params user = user_type() url = viewset.get_service().endpoint client = APIClient() client.force_authenticate(user) # url = PartnerViewSet.get_service().endpoint # with user_allow_service(user, viewset): with user_allow_country(user, "bolivia"): res = client.get(f"{url}") assert res.status_code == code, res.content
def test_user_system_filter(client: APIClient, data_service: Service, user1: User, django_assert_no_duplicate_queries, settings, django_assert_num_queries): settings.CACHES = { 'api': { 'BACKEND': 'django.core.cache.backends.dummy.DummyCache' } } client.force_authenticate(user1) data_service.invalidate_cache() SystemFilterFactory(service=data_service, user=user1, rules={'country_name': 'Bolivia'}) UserAccessControlFactory(service=data_service, user=user1) with user_allow_country(user1, "bolivia"): res = client.get(data_service.endpoint) assert res.status_code == 200 results = res.json()['results'] assert res['system-filters'] == "country_name=Bolivia" assert res['cache-hit'] == "False" assert len(results) == 1 assert [r['country_name'] for r in results] == ['Bolivia']
def test_permission_allow(client, allow): url = allow.service.endpoint client.force_authenticate(allow.user) with user_allow_country(allow.user, 'bolivia'): res = client.get(url) assert res.status_code == 200