diff --git a/requirements.txt b/requirements.txt --- a/requirements.txt +++ b/requirements.txt @@ -22,4 +22,4 @@ requests sentry-sdk typing-extensions -psycopg2 +psycopg2 < 2.8.6 diff --git a/swh/web/config.py b/swh/web/config.py --- a/swh/web/config.py +++ b/swh/web/config.py @@ -90,7 +90,7 @@ "vault": ("dict", {"cls": "remote", "args": {"url": "http://127.0.0.1:5005/",}}), "scheduler": ("dict", {"cls": "remote", "url": "http://127.0.0.1:5008/"}), "development_db": ("string", os.path.join(SETTINGS_DIR, "db.sqlite3")), - "test_db": ("string", os.path.join(SETTINGS_DIR, "testdb.sqlite3")), + "test_db": ("dict", {"name": "swh-web-test"}), "production_db": ("dict", {"name": "swh-web"}), "deposit": ( "dict", diff --git a/swh/web/settings/tests.py b/swh/web/settings/tests.py --- a/swh/web/settings/tests.py +++ b/swh/web/settings/tests.py @@ -92,17 +92,18 @@ ALLOWED_HOSTS = ["*"] - DATABASES = { "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": swh_web_config["test_db"], + "ENGINE": "django.db.backends.postgresql", + "NAME": swh_web_config["test_db"]["name"], } } # when not running unit tests, make the webapp fetch data from memory storages if "pytest" not in sys.argv[0] and "PYTEST_XDIST_WORKER" not in os.environ: swh_web_config.update({"debug": True, "e2e_tests_mode": True}) + from django.conf import settings + from swh.web.tests.data import get_tests_data, override_storages test_data = get_tests_data() @@ -112,6 +113,9 @@ test_data["search"], test_data["counters"], ) + + # using sqlite3 for frontend tests + settings.DATABASES["default"].update({("ENGINE", "django.db.backends.sqlite3")}) else: # Silent DEBUG output when running unit tests LOGGING["handlers"]["console"]["level"] = "INFO" # type: ignore diff --git a/swh/web/tests/auth/test_views.py b/swh/web/tests/auth/test_views.py --- a/swh/web/tests/auth/test_views.py +++ b/swh/web/tests/auth/test_views.py @@ -118,7 +118,7 @@ # check token has been generated and saved encrypted to database assert len(OIDCUserOfflineTokens.objects.all()) == nb_tokens + 1 - encrypted_token = OIDCUserOfflineTokens.objects.last().offline_token + encrypted_token = OIDCUserOfflineTokens.objects.last().offline_token.tobytes() secret = get_config()["secret_key"].encode() salt = request.user.sub.encode() decrypted_token = decrypt_data(encrypted_token, secret, salt) @@ -131,7 +131,7 @@ return decrypted_token -@pytest.mark.django_db +@pytest.mark.django_db(reset_sequences=True) def test_oidc_generate_bearer_token_authenticated_user_success(client, keycloak_oidc): """ Authenticated user should be able to generate a bearer token using OIDC @@ -150,7 +150,7 @@ check_http_get_response(client, url, status_code=403) -@pytest.mark.django_db +@pytest.mark.django_db(reset_sequences=True) def test_oidc_list_bearer_tokens(client, keycloak_oidc): """ User with correct credentials should be allowed to list his tokens. @@ -182,7 +182,7 @@ check_http_post_response(client, url, status_code=403) -@pytest.mark.django_db +@pytest.mark.django_db(reset_sequences=True) def test_oidc_get_bearer_token(client, keycloak_oidc): """ User with correct credentials should be allowed to display a token. @@ -204,7 +204,7 @@ assert response.content == token -@pytest.mark.django_db +@pytest.mark.django_db(reset_sequences=True) def test_oidc_get_bearer_token_expired_token(client, keycloak_oidc): """ User with correct credentials should be allowed to display a token. @@ -245,7 +245,7 @@ check_http_post_response(client, url, status_code=403) -@pytest.mark.django_db +@pytest.mark.django_db(reset_sequences=True) def test_oidc_revoke_bearer_tokens(client, keycloak_oidc): """ User with correct credentials should be allowed to revoke tokens. @@ -279,7 +279,7 @@ assert resp["location"] == login_url -@pytest.mark.django_db +@pytest.mark.django_db(reset_sequences=True) def test_oidc_profile_view(client, keycloak_oidc): """ Authenticated users should be able to request the profile page diff --git a/swh/web/tests/conftest.py b/swh/web/tests/conftest.py --- a/swh/web/tests/conftest.py +++ b/swh/web/tests/conftest.py @@ -16,6 +16,7 @@ import pytest from django.core.cache import cache +from django.test.utils import setup_databases # type: ignore from rest_framework.test import APIClient, APIRequestFactory from swh.model.hashutil import ALGORITHMS, hash_to_bytes @@ -495,3 +496,22 @@ yield swh_scheduler config["scheduler"] = scheduler get_scheduler_load_task_types.cache_clear() + + +@pytest.fixture(scope="session") +def django_db_setup(request, django_db_blocker, postgresql_proc): + from django.conf import settings + + settings.DATABASES["default"].update( + { + ("ENGINE", "django.db.backends.postgresql"), + ("NAME", get_config()["test_db"]["name"]), + ("USER", postgresql_proc.user), + ("HOST", postgresql_proc.host), + ("PORT", postgresql_proc.port), + } + ) + with django_db_blocker.unblock(): + setup_databases( + verbosity=request.config.option.verbose, interactive=False, keepdb=False + )