Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use API v2 urls in unit tests languages & audit_log apps #4863

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from rest_framework import status
from rest_framework.reverse import reverse

from kobo.apps.audit_log.models import AuditAction, AuditLog
from kpi.tests.base_test_case import BaseTestCase
from kpi.urls.router_api_v2 import URL_NAMESPACE as ROUTER_URL_NAMESPACE
from ..models import AuditAction, AuditLog


class ApiAuditLogTestCase(BaseTestCase):
Expand All @@ -15,7 +15,7 @@ class ApiAuditLogTestCase(BaseTestCase):

def setUp(self):
super().setUp()
self.audit_log_list_url = reverse('audit-log-list')
self.audit_log_list_url = reverse(self._get_endpoint('audit-log-list'))

def test_list_as_anonymous(self):
self.client.logout()
Expand Down Expand Up @@ -53,7 +53,7 @@ def test_list_as_superuser(self):
'app_label': 'foo',
'model_name': 'bar',
'object_id': 1,
'user': 'http://testserver/users/someuser/',
'user': 'http://testserver/api/v2/users/someuser/',
'user_uid': someuser.extra_details.uid,
'action': 'DELETE',
'metadata': {},
Expand Down Expand Up @@ -90,7 +90,7 @@ def test_filter_list(self):
'app_label': 'foo',
'model_name': 'bar',
'object_id': 1,
'user': 'http://testserver/users/anotheruser/',
'user': 'http://testserver/api/v2/users/anotheruser/',
'user_uid': anotheruser.extra_details.uid,
'action': 'DELETE',
'metadata': {},
Expand Down
4 changes: 1 addition & 3 deletions kobo/apps/audit_log/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@
router = DefaultRouter()
router.register(r'audit-logs', AuditLogViewSet, basename='audit-log')

urlpatterns = [
path('', include(router.urls)),
]
urlpatterns = []
7 changes: 5 additions & 2 deletions kobo/apps/languages/tests/api/v2/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# coding: utf-8
from rest_framework.test import APITestCase
from kpi.tests.base_test_case import BaseTestCase
from kpi.urls.router_api_v2 import URL_NAMESPACE as ROUTER_URL_NAMESPACE


class BaseApiTestCase(APITestCase):
class BaseApiTestCase(BaseTestCase):

fixtures = ['test_data']
URL_NAMESPACE = ROUTER_URL_NAMESPACE

def setUp(self):
super().setUp()
self.client.login(username='someuser', password='someuser')
16 changes: 9 additions & 7 deletions kobo/apps/languages/tests/api/v2/test_api_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
class LanguageListApiTestCase(BaseApiTestCase):

def test_can_list_as_authenticated_user(self):
response = self.client.get(reverse('language-list'))
response = self.client.get(reverse(self._get_endpoint('language-list')))
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_cannot_list_as_anonymous_user(self):
self.client.logout()
response = self.client.get(reverse('language-list'))
response = self.client.get(reverse(self._get_endpoint('language-list')))
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_list_with_search(self):
"""
Search within language names and codes.
"""
url = f"{reverse('language-list')}?q=fr"
url = f"{reverse(self._get_endpoint('language-list'))}?q=fr"
response = self.client.get(url)
expected_codes = ['fr', 'af']
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand All @@ -33,7 +33,7 @@ def test_list_with_utf8_search(self):
"""
Search within language names and codes with non-ascii characters
"""
url = f"{reverse('language-list')}?q=hé"
url = f"{reverse(self._get_endpoint('language-list'))}?q=hé"
response = self.client.get(url)
expected_codes = ['guq']
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand All @@ -51,7 +51,7 @@ def test_list_with_advanced_search(self):
'translation_services__code:msft '
'AND transcription_services__code:goog'
)
url = f"{reverse('language-list')}?q={search_query}"
url = f"{reverse(self._get_endpoint('language-list'))}?q={search_query}"
response = self.client.get(url)
expected_names = ['French']
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand All @@ -64,7 +64,7 @@ def test_sort(self):
"""
Should sort by featured DESC, name ASC
"""
response = self.client.get(reverse('language-list'))
response = self.client.get(reverse(self._get_endpoint('language-list')))
expected_names = [
'English',
'French',
Expand All @@ -84,7 +84,9 @@ class LanguageApiTestCase(BaseApiTestCase):

def setUp(self):
super().setUp()
self.detail_url = reverse('language-detail', kwargs={'code': 'fr'})
self.detail_url = reverse(
self._get_endpoint('language-detail'), kwargs={'code': 'fr'}
)

def test_can_read_as_authenticated_user(self):
expected = {
Expand Down
32 changes: 22 additions & 10 deletions kobo/apps/languages/tests/api/v2/test_api_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
class TranscriptionServiceListApiTestCase(BaseApiTestCase):

def test_can_list_as_authenticated_user(self):
response = self.client.get(reverse('transcription_service-list'))
response = self.client.get(
reverse(self._get_endpoint('transcription_service-list'))
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_cannot_list_as_anonymous_user(self):
self.client.logout()
response = self.client.get(reverse('transcription_service-list'))
response = self.client.get(
reverse(self._get_endpoint('transcription_service-list'))
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_list_with_search(self):
"""
Search within transcription service names and codes.
"""
url = f"{reverse('transcription_service-list')}?q=goo"
url = f"{reverse(self._get_endpoint('transcription_service-list'))}?q=goo"
response = self.client.get(url)
expected_codes = ['goog']
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand All @@ -33,7 +37,7 @@ def test_list_with_2_characters_search(self):
"""
Try to search with only 2 characters
"""
url = f"{reverse('transcription_service-list')}?q=ms"
url = f"{reverse(self._get_endpoint('transcription_service-list'))}?q=ms"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertTrue('detail' in response.data)
Expand All @@ -43,7 +47,9 @@ def test_sort(self):
"""
Should sort by name ASC
"""
response = self.client.get(reverse('transcription_service-list'))
response = self.client.get(
reverse(self._get_endpoint('transcription_service-list'))
)
expected_names = ['Google', 'Microsoft']
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
Expand All @@ -55,19 +61,23 @@ def test_sort(self):
class TranslationServiceListApiTestCase(BaseApiTestCase):

def test_can_list_as_authenticated_user(self):
response = self.client.get(reverse('translation_service-list'))
response = self.client.get(
reverse(self._get_endpoint('translation_service-list'))
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_cannot_list_as_anonymous_user(self):
self.client.logout()
response = self.client.get(reverse('translation_service-list'))
response = self.client.get(
reverse(self._get_endpoint('translation_service-list'))
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_list_with_search(self):
"""
Search within translation service names and codes.
"""
url = f"{reverse('translation_service-list')}?q=goo"
url = f"{reverse(self._get_endpoint('translation_service-list'))}?q=goo"
response = self.client.get(url)
expected_codes = ['goog']
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand All @@ -80,7 +90,7 @@ def test_list_with_2_characters_search(self):
"""
Try to search with only 2 characters
"""
url = f"{reverse('translation_service-list')}?q=ms"
url = f"{reverse(self._get_endpoint('translation_service-list'))}?q=ms"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertTrue('detail' in response.data)
Expand All @@ -90,7 +100,9 @@ def test_sort(self):
"""
Should sort by name ASC
"""
response = self.client.get(reverse('translation_service-list'))
response = self.client.get(
reverse(self._get_endpoint('translation_service-list'))
)
expected_names = ['Google', 'Microsoft']
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
Expand Down
4 changes: 1 addition & 3 deletions kobo/apps/languages/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@
basename='translation_service',
)

urlpatterns = [
path('', include(router.urls)),
]
urlpatterns = []
2 changes: 0 additions & 2 deletions kpi/urls/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
}), name='currentuser-detail'),
re_path(r'^', include(router_api_v1.urls)),
re_path(r'^api/v2/', include((router_api_v2.urls, URL_NAMESPACE))),
re_path(r'^api/v2/', include('kobo.apps.languages.urls')),
re_path(r'^api/v2/', include('kobo.apps.audit_log.urls')),
path('', include('kobo.apps.accounts.urls')),
path('', include('kobo.apps.service_health.urls')),
re_path(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
Expand Down
9 changes: 9 additions & 0 deletions kpi/urls/router_api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from django.urls import path
from rest_framework_extensions.routers import ExtendedDefaultRouter

from kobo.apps.audit_log.urls import router as audit_log_router
from kobo.apps.hook.views.v2.hook import HookViewSet
from kobo.apps.hook.views.v2.hook_log import HookLogViewSet
from kobo.apps.hook.views.v2.hook_signal import HookSignalViewSet
from kobo.apps.languages.urls import router as language_router
from kobo.apps.organizations.views import OrganizationViewSet
from kobo.apps.project_views.views import ProjectViewViewSet
from kpi.views.v2.asset import AssetViewSet
Expand Down Expand Up @@ -150,6 +152,13 @@ def get_urls(self, *args, **kwargs):
ServiceUsageViewSet, basename='service-usage')
router_api_v2.register(r'users', UserViewSet)


# Merge django apps routers with API v2 router
# All routes are under `/api/v2/` within the same namespace.
router_api_v2.registry.extend(language_router.registry)
noliveleger marked this conversation as resolved.
Show resolved Hide resolved
router_api_v2.registry.extend(audit_log_router.registry)


# TODO migrate ViewSet below
# router_api_v2.register(r'sitewide_messages', SitewideMessageViewSet)
#
Expand Down
Loading