Skip to content

Commit

Permalink
Merge pull request #93 from ssciwr/add_researcher_flag_to_user
Browse files Browse the repository at this point in the history
Add is_researcher boolean flag to User
  • Loading branch information
MaHaWo authored Sep 27, 2024
2 parents 3e90c1f + 995437c commit 4a7dae5
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 13 deletions.
12 changes: 12 additions & 0 deletions mondey_backend/src/mondey_backend/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Annotated

from fastapi import Depends
from fastapi import HTTPException
from sqlmodel import Session

from .databases.milestones import engine as milestones_engine
Expand All @@ -21,6 +22,17 @@ def get_session():

CurrentActiveUserDep = Annotated[User, Depends(current_active_user)]


def current_active_researcher(
user: Annotated[User, Depends(current_active_user)],
) -> User:
if user and user.is_researcher:
return user
raise HTTPException(401)


ResearchDep = Depends(current_active_researcher)

current_active_superuser = fastapi_users.current_user(active=True, superuser=True)

AdminDep = Depends(current_active_superuser)
2 changes: 2 additions & 0 deletions mondey_backend/src/mondey_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .routers import admin
from .routers import auth
from .routers import milestones
from .routers import research
from .routers import users
from .settings import app_settings

Expand All @@ -33,6 +34,7 @@ def create_app() -> FastAPI:
app.include_router(admin.create_router())
app.include_router(users.create_router())
app.include_router(auth.create_router())
app.include_router(research.create_router())
app.mount(
"/static", StaticFiles(directory=app_settings.STATIC_FILES_PATH), name="static"
)
Expand Down
8 changes: 5 additions & 3 deletions mondey_backend/src/mondey_backend/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi_users import schemas
from fastapi_users.db import SQLAlchemyBaseUserTable
from fastapi_users_db_sqlalchemy.access_token import SQLAlchemyBaseAccessTokenTable
from sqlalchemy import Boolean
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy.orm import DeclarativeBase
Expand All @@ -17,18 +18,19 @@ class Base(DeclarativeBase):

class User(SQLAlchemyBaseUserTable[int], Base):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
is_researcher: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)


class UserRead(schemas.BaseUser[int]):
pass
is_researcher: bool


class UserCreate(schemas.BaseUserCreate):
pass
is_researcher: bool | None = False


class UserUpdate(schemas.BaseUserUpdate):
pass
is_researcher: bool | None = None


class AccessToken(SQLAlchemyBaseAccessTokenTable[int], Base):
Expand Down
17 changes: 17 additions & 0 deletions mondey_backend/src/mondey_backend/routers/research.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __future__ import annotations

from fastapi import APIRouter

from ..dependencies import ResearchDep


def create_router() -> APIRouter:
router = APIRouter(
prefix="/research", tags=["research"], dependencies=[ResearchDep]
)

@router.get("/auth/")
def auth():
return {"ok": True}

return router
46 changes: 39 additions & 7 deletions mondey_backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from fastapi import FastAPI
from fastapi.testclient import TestClient
from mondey_backend import settings
from mondey_backend.dependencies import current_active_researcher
from mondey_backend.dependencies import current_active_superuser
from mondey_backend.dependencies import current_active_user
from mondey_backend.dependencies import get_session
Expand Down Expand Up @@ -117,23 +118,37 @@ def session(static_dir: pathlib.Path):


@pytest.fixture
def admin():
def active_admin_user():
UserRead(
id=1,
id=3,
email="[email protected]",
is_active=True,
is_superuser=True,
is_researcher=False,
is_verified=True,
)


@pytest.fixture
def user():
def active_research_user():
UserRead(
id=2,
email="[email protected]",
is_active=True,
is_superuser=False,
is_researcher=True,
is_verified=True,
)


@pytest.fixture
def active_user():
UserRead(
id=1,
email="[email protected]",
is_active=True,
is_superuser=False,
is_researcher=False,
is_verified=True,
)

Expand All @@ -147,21 +162,38 @@ def app(static_dir: pathlib.Path):

@pytest.fixture
def user_client(
app: FastAPI, static_dir: pathlib.Path, session: Session, user: UserRead
app: FastAPI, static_dir: pathlib.Path, session: Session, active_user: UserRead
):
app.dependency_overrides[get_session] = lambda: session
app.dependency_overrides[current_active_user] = lambda: active_user
client = TestClient(app)
yield client
app.dependency_overrides.clear()


@pytest.fixture
def research_client(
app: FastAPI,
static_dir: pathlib.Path,
session: Session,
active_research_user: UserRead,
):
app.dependency_overrides[get_session] = lambda: session
app.dependency_overrides[current_active_user] = lambda: user
app.dependency_overrides[current_active_researcher] = lambda: active_research_user
client = TestClient(app)
yield client
app.dependency_overrides.clear()


@pytest.fixture
def admin_client(
app: FastAPI, static_dir: pathlib.Path, session: Session, admin: UserRead
app: FastAPI,
static_dir: pathlib.Path,
session: Session,
active_admin_user: UserRead,
):
app.dependency_overrides[get_session] = lambda: session
app.dependency_overrides[current_active_superuser] = lambda: admin
app.dependency_overrides[current_active_superuser] = lambda: active_admin_user
client = TestClient(app)
yield client
app.dependency_overrides.clear()
Expand Down
13 changes: 11 additions & 2 deletions mondey_backend/tests/routers/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pathlib

import pytest
from fastapi.testclient import TestClient


Expand Down Expand Up @@ -80,8 +81,16 @@ def test_delete_milestone_group_invalid_group_id(admin_client: TestClient):
assert response.status_code == 404


def test_delete_milestone_group_invalid_admin_user(user_client: TestClient):
response = user_client.delete("/admin/milestone-groups/2")
@pytest.mark.parametrize(
"endpoint",
["/admin/languages/1", "/admin/milestone-groups/1", "/admin/milestones/1"],
)
@pytest.mark.parametrize("client_type", ["user_client", "research_client"])
def test_delete_endpoints_invalid_admin_user(
endpoint: str, client_type: str, request: pytest.FixtureRequest
):
client = request.getfixturevalue(client_type)
response = client.delete(endpoint)
assert response.status_code == 401


Expand Down
4 changes: 3 additions & 1 deletion mondey_backend/tests/routers/test_milestones.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest


@pytest.mark.parametrize("client_type", ["user_client", "admin_client"])
@pytest.mark.parametrize(
"client_type", ["user_client", "research_client", "admin_client"]
)
class TestMilestones:
def test_get_languages(self, client_type: str, request: pytest.FixtureRequest):
client = request.getfixturevalue(client_type)
Expand Down
11 changes: 11 additions & 0 deletions mondey_backend/tests/routers/test_research.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from fastapi.testclient import TestClient


def test_auth_invalid_user(user_client: TestClient):
response = user_client.get("/research/auth/")
assert response.status_code == 401


def test_auth(research_client: TestClient):
response = research_client.get("/research/auth/")
assert response.status_code == 200

0 comments on commit 4a7dae5

Please sign in to comment.