Skip to content

Commit

Permalink
feat(router): define dummy password generator router(#6)
Browse files Browse the repository at this point in the history
* fix(workflows): fix pythonpath
  • Loading branch information
zhavir authored Aug 26, 2022
1 parent 4a033bc commit 5ab7573
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/auto-tag-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-r requirements.txt
httpx
pytest
pytest-asyncio
pytest-cov
2 changes: 2 additions & 0 deletions src/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions src/app/routers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from fastapi import APIRouter

router: APIRouter = APIRouter()

from .password_generation_router import generate_password
Empty file.
5 changes: 5 additions & 0 deletions src/app/routers/models/requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pydantic import BaseModel


class GeneratePasswordRequest(BaseModel):
pass
5 changes: 5 additions & 0 deletions src/app/routers/models/responses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pydantic import BaseModel


class GeneratedPasswordResponse(BaseModel):
password: str
14 changes: 14 additions & 0 deletions src/app/routers/password_generation_router.py
Original file line number Diff line number Diff line change
@@ -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")
11 changes: 11 additions & 0 deletions src/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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')
2 changes: 0 additions & 2 deletions src/tests/dummy_test.py

This file was deleted.

Empty file added src/tests/unit/__init__.py
Empty file.
Empty file.
19 changes: 19 additions & 0 deletions src/tests/unit/routers/test_password_generate_router.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5ab7573

Please sign in to comment.