Skip to content

Commit

Permalink
Mock (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoriev-semyon authored Apr 19, 2023
1 parent 433fb0a commit 9826f42
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_and_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ jobs:
name: Build and publish Python 🐍 distributions 📦 to PyPI and TestPyPI
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
linting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-python@v3
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.11
- uses: isort/isort-action@master
Expand Down
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
## Примеры использования
```python
from auth_lib.fastapi import UnionAuth
from fastapi import APIRouter, Depends
router = APIRouter(prefix="/...")

## Чтобы дернуть ручку нужен один скоуп, авторизация обязательна
## Юзкейс https://github.com/profcomff/timetable-api/blob/a374c74cd960941100f6c923ff9c3ff706a1ed09/calendar_backend/routes/room/room.py#L45
Expand Down Expand Up @@ -48,3 +50,47 @@ AUTH_AUTO_ERROR: bool = True
AUTH_ALLOW_NONE: bool = False

```

Пример мока библиотеки:

Установите нужные завивсимости
```shell
pip install 'auth-lib-profcomff[testing]'
```

```python
import pytest
from fastapi.testclient import TestClient
from fastapi import FastAPI

@pytest.fixture
def client(auth_mock):
yield TestClient(FastAPI())

@pytest.mark.authenticated("scope1", "scope2", ...)
def test1(client):
"""
В этом тесте будут выданы скоупы scope1, scope2,
библиотека не будет проверять токен через АПИ, будет просто возвращать
нужный словарь, как будто пользователь авторизован с нужными скоупами
"""
assert 2*2 == 4


@pytest.mark.authenticated()
def test2(client):
"""
В этом тесте скоупов выдано не будет,
но библиотека не будет проверять токен через АПИ, будет просто возвращать
нужный словарь, как будто пользователь авторизован с нужными скоупами
"""
assert 2*2 == 4


def test3(client):
"""
В этом тесте скоупов выдано не будет, библиотека будет проверять
токен через АПИ
"""
assert 2*2 == 4
```
5 changes: 5 additions & 0 deletions auth_lib/testing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
try:
from auth_lib.testing.testutils import auth_mock, pytest_configure
except ImportError:
print("You have to install testing requirements")
print("pip install 'auth-lib-profcomff[testing]'")
33 changes: 33 additions & 0 deletions auth_lib/testing/testutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from unittest.mock import MagicMock, patch

import pytest


def pytest_configure(config):
config.addinivalue_line(
"markers", "authenticated(*scopes): mark test to mock auth_lib"
)


@pytest.fixture(autouse=True)
def auth_mock(request):
marker: pytest.mark = request.node.get_closest_marker("authenticated")
if not marker:
return (yield)
scopes: tuple[str] = marker.args
session_scopes = []
for cnt, scope in enumerate(scopes):
session_scopes.append({"id": cnt, "name": scope, "comment": ""})
_return_val: dict[str, int | list[dict[str, str | int]]] = {
"user_id": 0,
"id": 0,
"session_scopes": session_scopes,
"user_scopes": session_scopes,
}
patcher = patch(
"auth_lib.fastapi.UnionAuth.__call__",
new=MagicMock(return_value=_return_val),
)
patcher.start()
yield
patcher.stop()
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="auth_lib_profcomff",
version="2023.03.16.2",
version="2023.04.19",
author="Semyon Grigoriev",
long_description=readme,
long_description_content_type="text/markdown",
Expand All @@ -14,7 +14,9 @@
install_requires=["requests", "aiohttp", "setuptools"],
extras_require={
"fastapi": ["fastapi", "starlette", "pydantic"],
"testing": ["pytest"],
},
entry_points={"pytest11": ["pytest_auth_lib = auth_lib.testing"]},
classifiers=[
"Programming Language :: Python :: 3.11",
],
Expand Down

0 comments on commit 9826f42

Please sign in to comment.