From 5ab75738bb9a23567abf1285a6dadbcbfd2b2e15 Mon Sep 17 00:00:00 2001 From: zhavir Date: Fri, 26 Aug 2022 20:08:12 +0200 Subject: [PATCH] feat(router): define dummy password generator router(#6) * fix(workflows): fix pythonpath --- .github/workflows/auto-tag-and-release.yml | 3 +++ .github/workflows/run-tests.yml | 3 +++ requirements-dev.txt | 2 ++ src/app/main.py | 2 ++ src/app/routers/__init__.py | 5 +++++ src/app/routers/models/__init__.py | 0 src/app/routers/models/requests.py | 5 +++++ src/app/routers/models/responses.py | 5 +++++ src/app/routers/password_generation_router.py | 14 ++++++++++++++ src/tests/conftest.py | 11 +++++++++++ src/tests/dummy_test.py | 2 -- src/tests/unit/__init__.py | 0 src/tests/unit/routers/__init__.py | 0 .../routers/test_password_generate_router.py | 19 +++++++++++++++++++ 14 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/app/routers/__init__.py create mode 100644 src/app/routers/models/__init__.py create mode 100644 src/app/routers/models/requests.py create mode 100644 src/app/routers/models/responses.py create mode 100644 src/app/routers/password_generation_router.py create mode 100644 src/tests/conftest.py delete mode 100644 src/tests/dummy_test.py create mode 100644 src/tests/unit/__init__.py create mode 100644 src/tests/unit/routers/__init__.py create mode 100644 src/tests/unit/routers/test_password_generate_router.py diff --git a/.github/workflows/auto-tag-and-release.yml b/.github/workflows/auto-tag-and-release.yml index d1cbe11..f16bd58 100644 --- a/.github/workflows/auto-tag-and-release.yml +++ b/.github/workflows/auto-tag-and-release.yml @@ -24,6 +24,9 @@ jobs: uses: py-actions/py-dependency-install@v3 with: path: "requirements-dev.txt" + - name: set pythonpath + run: | + echo "PYTHONPATH=src/" >> $GITHUB_ENV - name: Test with pytest run: | pytest diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index b1c6a19..9c610d2 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -20,6 +20,9 @@ jobs: uses: py-actions/py-dependency-install@v3 with: path: "requirements-dev.txt" + - name: set pythonpath + run: | + echo "PYTHONPATH=src/" >> $GITHUB_ENV - name: Test with pytest run: | pytest --cov=src/app --cov=src/tests --cov-report xml:coverage.xml src diff --git a/requirements-dev.txt b/requirements-dev.txt index 2f47a37..bd59176 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,3 +1,5 @@ -r requirements.txt +httpx pytest +pytest-asyncio pytest-cov \ No newline at end of file diff --git a/src/app/main.py b/src/app/main.py index 3fdf7a0..5729a33 100644 --- a/src/app/main.py +++ b/src/app/main.py @@ -4,6 +4,7 @@ from fastapi import FastAPI, APIRouter from fastapi.openapi.utils import get_openapi +from app.routers import router APP_VERSION = 1.0 SERVICE_NAME = 'Password Generator' @@ -16,6 +17,7 @@ router_api_v1 = APIRouter() # Registration of routes +router_api_v1.include_router(router, prefix='/passwords') # Finalizing setup of router with FastAPI app router_api.include_router(router_api_v1, prefix="/v1") diff --git a/src/app/routers/__init__.py b/src/app/routers/__init__.py new file mode 100644 index 0000000..9cb5800 --- /dev/null +++ b/src/app/routers/__init__.py @@ -0,0 +1,5 @@ +from fastapi import APIRouter + +router: APIRouter = APIRouter() + +from .password_generation_router import generate_password diff --git a/src/app/routers/models/__init__.py b/src/app/routers/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/app/routers/models/requests.py b/src/app/routers/models/requests.py new file mode 100644 index 0000000..5f3289a --- /dev/null +++ b/src/app/routers/models/requests.py @@ -0,0 +1,5 @@ +from pydantic import BaseModel + + +class GeneratePasswordRequest(BaseModel): + pass diff --git a/src/app/routers/models/responses.py b/src/app/routers/models/responses.py new file mode 100644 index 0000000..bfc297e --- /dev/null +++ b/src/app/routers/models/responses.py @@ -0,0 +1,5 @@ +from pydantic import BaseModel + + +class GeneratedPasswordResponse(BaseModel): + password: str diff --git a/src/app/routers/password_generation_router.py b/src/app/routers/password_generation_router.py new file mode 100644 index 0000000..728d668 --- /dev/null +++ b/src/app/routers/password_generation_router.py @@ -0,0 +1,14 @@ +from starlette import status + +from app.routers import router +from app.routers.models.requests import GeneratePasswordRequest +from app.routers.models.responses import GeneratedPasswordResponse + + +@router.post( + path='/generate/', + status_code=status.HTTP_200_OK, + response_model=GeneratedPasswordResponse, +) +async def generate_password(request: GeneratePasswordRequest) -> GeneratedPasswordResponse: + return GeneratedPasswordResponse(password="something") diff --git a/src/tests/conftest.py b/src/tests/conftest.py new file mode 100644 index 0000000..1f93413 --- /dev/null +++ b/src/tests/conftest.py @@ -0,0 +1,11 @@ +from typing import Generator + +import pytest +from httpx import AsyncClient + +from app.main import app, SERVICE_NAME + + +@pytest.fixture(scope="function") +def mocked_client() -> Generator[AsyncClient, None, None]: + yield AsyncClient(app=app, base_url=f'http://{SERVICE_NAME}:9001') diff --git a/src/tests/dummy_test.py b/src/tests/dummy_test.py deleted file mode 100644 index f4f5361..0000000 --- a/src/tests/dummy_test.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_dummy(): - assert True diff --git a/src/tests/unit/__init__.py b/src/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/unit/routers/__init__.py b/src/tests/unit/routers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/unit/routers/test_password_generate_router.py b/src/tests/unit/routers/test_password_generate_router.py new file mode 100644 index 0000000..d27bb14 --- /dev/null +++ b/src/tests/unit/routers/test_password_generate_router.py @@ -0,0 +1,19 @@ +import pytest +from httpx import AsyncClient + + +@pytest.mark.asyncio +async def test_generate_password(mocked_client: AsyncClient): + async with mocked_client as client: + response = await client.post("/api/v1/passwords/generate/", json={}) + + assert response.status_code == 200 + assert response.json() == {"password": "something"} + + +@pytest.mark.asyncio +async def test_generate_password_but_input_is_not_valid(mocked_client: AsyncClient): + async with mocked_client as client: + response = await client.post("/api/v1/passwords/generate/", data={}) + + assert response.status_code == 422