From 666ab20d01f67b3e26b7d5890cdcbfd97d0d82bc Mon Sep 17 00:00:00 2001 From: SerRichard Date: Tue, 28 May 2024 16:27:51 +0200 Subject: [PATCH 1/2] update --- tests/api/test_api.py | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/api/test_api.py b/tests/api/test_api.py index 714f274..f825695 100644 --- a/tests/api/test_api.py +++ b/tests/api/test_api.py @@ -1,8 +1,12 @@ import uuid from typing import Optional +import pytest from fastapi import Depends, FastAPI, HTTPException, Response from fastapi.testclient import TestClient +from sqlalchemy import Column +from sqlalchemy.exc import IntegrityError +from sqlalchemy.types import BOOLEAN from openeo_fastapi.api.app import OpenEOApi from openeo_fastapi.api.models import FilesGetResponse @@ -18,6 +22,7 @@ from openeo_fastapi.client.auth import Authenticator, User from openeo_fastapi.client.core import OpenEOCore from openeo_fastapi.client.files import FILE_ENDPOINTS, FilesRegister +from openeo_fastapi.client.psql.models import UserORM def test_api_core(core_api): @@ -315,3 +320,50 @@ def my_new_cool_auth(): assert response.status_code == 200 assert "user_id" in response.json() assert response.json()["user_id"] == str(specific_uuid) + + +def test_extending_the_usermodel( + mocked_oidc_config, mocked_oidc_userinfo, core_api, app_settings +): + """Test the user model can be extended in the api available.""" + + # Extend the UserORM class + class ExtendedUserORM(UserORM): + """ORM for the UserORM table.""" + + new_value = Column(BOOLEAN, nullable=False) + + # Try to revise the database using the extended UserORM + import os + from pathlib import Path + + from alembic import command + from alembic.config import Config + + from tests.conftest import ALEMBIC_DIR + + os.chdir(Path(ALEMBIC_DIR)) + alembic_cfg = Config("alembic.ini") + + command.revision(alembic_cfg, f"openeo-fastapi-extended", autogenerate=True) + command.upgrade(alembic_cfg, "head") + + test_app = TestClient(core_api.app) + + with pytest.raises(IntegrityError): + test_app.get( + f"/{app_settings.OPENEO_VERSION}/me", + headers={"Authorization": "Bearer /oidc/egi/not-real"}, + ) + + def my_new_cool_auth(): + return User(user_id=uuid.uuid4(), oidc_sub="the-real-user", new_value=True) + + core_api.override_authentication(my_new_cool_auth) + + response = test_app.get( + f"/{app_settings.OPENEO_VERSION}/me", + headers={"Authorization": "Bearer /oidc/egi/not-real"}, + ) + + assert response.status_code == 200 From df48d33a8f5d3dbb104096c126d7fdaf1950f213 Mon Sep 17 00:00:00 2001 From: SerRichard Date: Wed, 29 May 2024 08:59:07 +0200 Subject: [PATCH 2/2] accept reformatting --- tests/api/test_api.py | 13 +++++++++++-- tests/conftest.py | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/api/test_api.py b/tests/api/test_api.py index f825695..7dc6d29 100644 --- a/tests/api/test_api.py +++ b/tests/api/test_api.py @@ -322,6 +322,9 @@ def my_new_cool_auth(): assert response.json()["user_id"] == str(specific_uuid) +@pytest.mark.skip( + reason="Can only be run independently, as it revisions the database and will break the tests which run after." +) def test_extending_the_usermodel( mocked_oidc_config, mocked_oidc_userinfo, core_api, app_settings ): @@ -345,8 +348,10 @@ class ExtendedUserORM(UserORM): os.chdir(Path(ALEMBIC_DIR)) alembic_cfg = Config("alembic.ini") - command.revision(alembic_cfg, f"openeo-fastapi-extended", autogenerate=True) - command.upgrade(alembic_cfg, "head") + command.revision( + alembic_cfg, f"openeo-fastapi-extended", rev_id="downgrademe", autogenerate=True + ) + command.upgrade(alembic_cfg, revision="downgrademe") test_app = TestClient(core_api.app) @@ -366,4 +371,8 @@ def my_new_cool_auth(): headers={"Authorization": "Bearer /oidc/egi/not-real"}, ) + # Downgrade to remove the use of the ExtendedUserORM + # Doesnt seem to work + command.downgrade(alembic_cfg, revision="downgrademe") + assert response.status_code == 200 diff --git a/tests/conftest.py b/tests/conftest.py index 996e6e5..8968238 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -53,7 +53,7 @@ def app_settings(): return settings.AppSettings(**SETTINGS_DICT) -@pytest.fixture() +@pytest.fixture(scope="function") def core_api(): formats = [ FileFormat( @@ -189,7 +189,7 @@ def mocked_issuer(): ) -@pytest.fixture(autouse=True) +@pytest.fixture(scope="function", autouse=True) def mock_engine(postgresql): """Postgresql engine for SQLAlchemy.""" import os