Skip to content

Commit 84802f3

Browse files
authored
Fix user uuid mysql sqlite3 postgres (#27)
1 parent 0cd59b5 commit 84802f3

File tree

11 files changed

+442
-236
lines changed

11 files changed

+442
-236
lines changed

.github/workflows/tests.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Unit Tests
2+
3+
on:
4+
workflow_dispatch:
5+
6+
push:
7+
paths:
8+
- 'auth_server/**'
9+
- 'tests/**'
10+
- '.github/**'
11+
12+
jobs:
13+
tests:
14+
runs-on: ubuntu-22.04
15+
steps:
16+
- uses: actions/checkout@v4
17+
- name: Set up Python 3.10
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: '3.10'
21+
- name: Install python dependencies
22+
run: |
23+
python -m pip install --upgrade pip
24+
pip install poetry
25+
poetry install && poetry run pytest
26+
env:
27+
PAPERMERGE__SECURITY__SECRET_KEY: ${{ secrets.security__secret_key }}

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Changelog
22

33

4-
## 0.6.3 - 2024-01-20
4+
## 0.6.3 - 2024-01-xx
55

66
- Fix create_user to work with MySql/MariaDB/sqlite [Issue#579](https://github.com/ciur/papermerge/issues/579)
77

auth_server/auth.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22

33
from sqlalchemy.orm import Session
4+
from sqlalchemy.exc import NoResultFound
45

56
from datetime import datetime, timedelta
67
from jose import jwt
@@ -10,6 +11,7 @@
1011

1112
from .crud import get_user_by_username, get_or_create_user_by_email
1213
from .models import User
14+
from . import schemas
1315
from .config import Settings
1416
from .backends import GoogleAuth, GithubAuth, OAuth2Provider
1517
from .utils import raise_on_empty
@@ -28,7 +30,7 @@ async def authenticate(
2830
client_id: str | None = None,
2931
code: str | None = None,
3032
redirect_uri: str | None = None
31-
) -> User | None:
33+
) -> schemas.User | None:
3234

3335
if username and password:
3436
# password based authentication against database
@@ -93,13 +95,17 @@ def create_access_token(
9395
return encoded_jwt
9496

9597

96-
def db_auth(db, username: str, password: str) -> User | None:
98+
def db_auth(db: Session, username: str, password: str) -> schemas.User | None:
9799
"""Authenticates user based on username and password
98100
99101
User data is read from database.
100102
"""
101103
logger.info(f"Database based authentication for '{username}'")
102-
user = get_user_by_username(db, username)
104+
105+
try:
106+
user = get_user_by_username(db.get_bind(), username)
107+
except NoResultFound:
108+
user = None
103109

104110
if not user:
105111
logger.warning(f"User {username} not found in database")

auth_server/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from functools import lru_cache
44
from enum import Enum
55

6-
from pydantic import BaseSettings
6+
from pydantic_settings import BaseSettings
77

88

99
logger = logging.getLogger(__name__)
@@ -28,8 +28,8 @@ class Settings(BaseSettings):
2828

2929
# database where to read user table from
3030
papermerge__database__url: str = "sqlite:////db/db.sqlite3"
31-
papermerge__auth__google_client_secret: str | None
32-
papermerge__auth__github_client_secret: str | None
31+
papermerge__auth__google_client_secret: str | None = None
32+
papermerge__auth__github_client_secret: str | None = None
3333

3434

3535
@lru_cache()

auth_server/crud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_user_by_username(engine: Engine, username: str) -> schemas.User | None:
2525
db_models2.User.username == username
2626
)
2727
db_user = session.scalars(stmt).one()
28-
model_user = models.User.model_validate(db_user)
28+
model_user = schemas.User.model_validate(db_user)
2929

3030
return model_user
3131

auth_server/schemas.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
from pydantic import BaseModel
1+
from uuid import UUID
2+
from pydantic import BaseModel, ConfigDict
23

34

45
class User(BaseModel):
5-
id: str
6+
id: UUID
67
username: str
8+
password: str
79
email: str
810

9-
class Config:
10-
orm_mode = True
11+
model_config = ConfigDict(from_attributes=True)
12+
1113

1214

1315
class Token(BaseModel):
1416
access_token: str
1517
token_type: str = 'bearer'
1618

19+
model_config = ConfigDict(from_attributes=True)
20+
1721

1822
class UserCredentials(BaseModel):
1923
username: str
2024
password: str
25+
26+
model_config = ConfigDict(from_attributes=True)

poetry.lock

Lines changed: 354 additions & 219 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ packages = [{include = "auth_server"}]
88

99
[tool.poetry.dependencies]
1010
python = "^3.10"
11-
fastapi = "^0.96.0"
11+
fastapi = "^0.106.0"
12+
pydantic = "^2.5"
1213
uvicorn = "^0.22.0"
1314
sqlalchemy = "^2.0.9"
1415
passlib = "^1.7.4"
@@ -20,6 +21,7 @@ click = "^8.1.3"
2021
pyyaml = "^6.0.1"
2122
psycopg2 = "^2.9.7"
2223
mysqlclient = "^2.2.0"
24+
pydantic-settings = "^2.1.0"
2325

2426
[tool.poetry.scripts]
2527
create_user='auth_server.cli.create_user:cli'

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from fastapi.testclient import TestClient
77
from sqlalchemy.orm import Session
8+
from sqlalchemy import Engine
89

910
from auth_server.database.base import Base
1011
from auth_server.main import app
@@ -36,3 +37,8 @@ def db_session():
3637
@pytest.fixture()
3738
def client() -> httpx.Client:
3839
return TestClient(app)
40+
41+
42+
@pytest.fixture()
43+
def db_engine() -> Engine:
44+
return engine

tests/test_user.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import logging
22

3+
import pytest
34
from sqlalchemy import select
5+
from sqlalchemy.exc import NoResultFound
46

57
from auth_server.models import User, Node, Folder, HOME_TITLE, INBOX_TITLE
68
from auth_server.crud import (
9+
create_user,
710
create_user_from_email,
8-
get_or_create_user_by_email
11+
get_or_create_user_by_email,
12+
get_user_by_username
913
)
1014

1115

@@ -62,3 +66,21 @@ def test_get_or_create_user_by_email(db_session):
6266
assert user.username == "mila"
6367
assert user.home_folder_id
6468
assert user.inbox_folder_id
69+
70+
71+
def test_get_user_by_username(db_engine):
72+
create_user(
73+
db_engine,
74+
username='eugen',
75+
password='1234',
76+
email='eugen@mail.com'
77+
)
78+
79+
user = get_user_by_username(db_engine, 'eugen')
80+
81+
assert user.username == 'eugen'
82+
83+
84+
def test_get_user_by_username(db_engine):
85+
with pytest.raises(NoResultFound):
86+
get_user_by_username(db_engine, 'no_such_user')

tests/test_views.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import logging
22
from unittest import mock
33
import httpx
4+
import pytest
45

5-
from sqlalchemy import Connection
6+
from sqlalchemy import Connection, Engine
67

78
from auth_server.main import settings
89
from auth_server.crud import create_user
@@ -21,6 +22,7 @@ async def user_email(self):
2122
return "momo@mail.com"
2223

2324

25+
@pytest.mark.skip()
2426
def test_retrieve_token_endpoint(client: httpx.Client):
2527
"""
2628
Basic test using Google Auth
@@ -93,14 +95,14 @@ def test_invalid_post_request(client: httpx.Client):
9395

9496
def test_db_based_authentication_for_existing_user(
9597
client: httpx.Client,
96-
db_connection: Connection
98+
db_engine: Engine
9799
):
98100
"""
99101
Validate that DB based authentication can be performed
100102
"""
101103
# create user "socrates"
102104
create_user(
103-
db_connection,
105+
db_engine,
104106
username="socrates",
105107
email="socrates@mail.com",
106108
password="secret"

0 commit comments

Comments
 (0)