diff --git a/gateway/api/views.py b/gateway/api/views.py index 5ac530c56..374acbc53 100644 --- a/gateway/api/views.py +++ b/gateway/api/views.py @@ -204,7 +204,9 @@ def run_existing(self, request): carrier = {} TraceContextTextMapPropagator().inject(carrier) arguments = serializer.data.get("arguments") - token = request.auth.token.decode() + token = "" + if request.auth: + token = request.auth.token.decode() try: job = self.get_service_job_class().save( program=program, diff --git a/gateway/main/settings.py b/gateway/main/settings.py index 03432fe59..2589da95b 100644 --- a/gateway/main/settings.py +++ b/gateway/main/settings.py @@ -47,7 +47,6 @@ # Application definition INSTALLED_APPS = [ - "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", @@ -59,10 +58,6 @@ "rest_framework.authtoken", "rest_framework_simplejwt", "allauth", - "allauth.account", - "allauth.socialaccount", - "dj_rest_auth", - "dj_rest_auth.registration", "api", "psycopg2", "drf_yasg", @@ -76,11 +71,9 @@ "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", - "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", "django_prometheus.middleware.PrometheusAfterMiddleware", - "allauth.account.middleware.AccountMiddleware", ] ROOT_URLCONF = "main.urls" @@ -180,8 +173,6 @@ ] AUTHENTICATION_BACKENDS = [ - # Needed to login by username in Django admin, regardless of `allauth` - "django.contrib.auth.backends.ModelBackend", # `allauth` specific authentication methods, such as login by e-mail "allauth.account.auth_backends.AuthenticationBackend", ] diff --git a/gateway/main/urls.py b/gateway/main/urls.py index 296ed5c96..3180e81b4 100644 --- a/gateway/main/urls.py +++ b/gateway/main/urls.py @@ -16,7 +16,6 @@ from django.conf import settings from django.conf.urls.static import static -from django.contrib import admin from django.urls import path, include, re_path from django.views.generic import TemplateView from rest_framework import routers, permissions @@ -43,10 +42,7 @@ urlpatterns = [ - path("dj-rest-auth/", include("dj_rest_auth.urls")), path("accounts/", include("allauth.urls")), - path("api-auth/", include("rest_framework.urls")), - path("admin/", admin.site.urls), path("readiness/", probes.views.readiness, name="readiness"), path("liveness/", probes.views.liveness, name="liveness"), path("", include("django_prometheus.urls")), diff --git a/gateway/tests/api/test_files.py b/gateway/tests/api/test_files.py index 28d2d3abc..bdb75d165 100644 --- a/gateway/tests/api/test_files.py +++ b/gateway/tests/api/test_files.py @@ -5,6 +5,7 @@ from django.urls import reverse from rest_framework import status from rest_framework.test import APITestCase +from django.contrib.auth import models class TestFilesApi(APITestCase): @@ -30,12 +31,8 @@ def test_files_list(self): media_root = os.path.normpath(os.path.join(os.getcwd(), media_root)) with self.settings(MEDIA_ROOT=media_root): - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) url = reverse("v1:files-list") response = self.client.get(url, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) @@ -43,12 +40,8 @@ def test_files_list(self): def test_non_existing_file_download(self): """Tests downloading non-existing file.""" - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) url = reverse("v1:files-download") response = self.client.get( url, data={"file": "non_existing.tar"}, format="json" @@ -68,12 +61,8 @@ def test_file_download(self): media_root = os.path.normpath(os.path.join(os.getcwd(), media_root)) with self.settings(MEDIA_ROOT=media_root): - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) url = reverse("v1:files-download") response = self.client.get( url, data={"file": "artifact.tar"}, format="json" @@ -99,12 +88,8 @@ def test_file_delete(self): fp.close() with self.settings(MEDIA_ROOT=media_root): - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) url = reverse("v1:files-delete") response = self.client.delete( url, data={"file": "artifact_delete.tar"}, format="json" @@ -122,12 +107,8 @@ def test_non_existing_file_delete(self): media_root = os.path.normpath(os.path.join(os.getcwd(), media_root)) with self.settings(MEDIA_ROOT=media_root): - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) url = reverse("v1:files-delete") response = self.client.delete( url, data={"file": "artifact_delete.tar"}, format="json" @@ -145,12 +126,8 @@ def test_file_upload(self): media_root = os.path.normpath(os.path.join(os.getcwd(), media_root)) with self.settings(MEDIA_ROOT=media_root): - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) url = reverse("v1:files-upload") with open("README.md") as f: response = self.client.post( @@ -172,12 +149,8 @@ def test_escape_directory(self): "fake_media", ) ): - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) url = reverse("v1:files-download") response = self.client.get( url, data={"file": "../test_user_2/artifact_2.tar"}, format="json" diff --git a/gateway/tests/api/test_job.py b/gateway/tests/api/test_job.py index 39b02b987..8020eb14a 100644 --- a/gateway/tests/api/test_job.py +++ b/gateway/tests/api/test_job.py @@ -5,6 +5,7 @@ from rest_framework.test import APITestCase from api.models import Job +from django.contrib.auth import models class TestJobApi(APITestCase): @@ -14,12 +15,8 @@ class TestJobApi(APITestCase): def _authorize(self): """Authorize client.""" - auth = reverse("rest_login") - resp = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = resp.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) def test_job_non_auth_user(self): """Tests job list non-authorized.""" @@ -68,12 +65,7 @@ def test_job_save_result(self): def test_stop_job(self): """Tests job stop.""" - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + self._authorize() job_stop_response = self.client.post( reverse( diff --git a/gateway/tests/api/test_v1_program.py b/gateway/tests/api/test_v1_program.py index 46d346783..34711ff57 100644 --- a/gateway/tests/api/test_v1_program.py +++ b/gateway/tests/api/test_v1_program.py @@ -4,6 +4,7 @@ from rest_framework.test import APITestCase from api.models import Job, JobConfig import json +from django.contrib.auth import models class TestProgramApi(APITestCase): @@ -20,12 +21,8 @@ def test_programs_non_auth_user(self): def test_programs_list(self): """Tests programs list authorized.""" - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) programs_response = self.client.get(reverse("v1:programs-list"), format="json") @@ -38,13 +35,9 @@ def test_programs_list(self): def test_program_detail(self): """Tests program detail authorized.""" - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) programs_response = self.client.get( reverse( "v1:programs-detail", @@ -58,13 +51,9 @@ def test_program_detail(self): def test_run_existing(self): """Tests run existing authorized.""" - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) programs_response = self.client.post( "/api/v1/programs/run_existing/", data={ @@ -85,13 +74,9 @@ def test_run_existing(self): def test_public(self): """Tests public flag.""" - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) programs_response = self.client.get( "/api/v1/programs/1a7947f9-6ae8-4e3d-ac1e-e7d608deec82/", format="json", @@ -117,13 +102,9 @@ def test_public(self): def test_runtime_job(self): """Tests run existing authorized.""" - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) programs_response = self.client.get( "/api/v1/runtime_jobs/", format="json", @@ -144,13 +125,8 @@ def test_runtime_job(self): self.assertEqual(programs_response.status_code, status.HTTP_200_OK) self.assertEqual(programs_response.json().get("count"), 2) - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user_2", "password": "123"}, format="json" - ) - token_2 = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token_2) - + user = models.User.objects.get(username="test_user_2") + self.client.force_authenticate(user=user) programs_response = self.client.get( "/api/v1/runtime_jobs/", format="json", @@ -160,13 +136,9 @@ def test_runtime_job(self): def test_add_runtimejob(self): """Tests run existing authorized.""" - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) programs_response = self.client.post( "/api/v1/jobs/1a7947f9-6ae8-4e3d-ac1e-e7d608deec83/add_runtimejob/", data={ @@ -187,13 +159,9 @@ def test_add_runtimejob(self): ) def test_list_runtimejob(self): - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) programs_response = self.client.get( "/api/v1/jobs/1a7947f9-6ae8-4e3d-ac1e-e7d608deec83/list_runtimejob/", format="json", @@ -209,14 +177,8 @@ def test_list_runtimejob(self): def test_catalog_entry(self): """Tests catalog entry.""" - # Non-owner - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user_2", "password": "123"}, format="json" - ) - token_2 = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token_2) - + user = models.User.objects.get(username="test_user_2") + self.client.force_authenticate(user=user) # list catalog programs_response = self.client.get( "/api/v1/catalog_entries/", @@ -241,13 +203,8 @@ def test_catalog_entry(self): self.assertEqual(programs_response.status_code, status.HTTP_403_FORBIDDEN) # Program owner - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) - + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) # list catalog programs_response = self.client.get( "/api/v1/catalog_entries/", @@ -296,13 +253,8 @@ def test_catalog_entry(self): def test_to_catalog(self): """Tests add catalog entry.""" - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) - + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) # to catalog entry programs_response = self.client.post( "/api/v1/programs/1a7947f9-6ae8-4e3d-ac1e-e7d608deec82/to_catalog/", @@ -349,13 +301,8 @@ def test_to_catalog(self): def test_list_catalog_entry(self): """Tests list catalog entry.""" - auth = reverse("rest_login") - response = self.client.post( - auth, {"username": "test_user", "password": "123"}, format="json" - ) - token = response.data.get("access") - self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token) - + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) programs_response = self.client.get( "/api/v1/catalog_entries/?tags=tag3", format="json",