-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from Tinkoff/overhave-api
Overhave api
- Loading branch information
Showing
24 changed files
with
201 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import fastapi as fastapi | ||
|
||
from overhave.api.views import receive_test_user | ||
from overhave.api.views import test_user_handler | ||
|
||
|
||
def create_overhave_api() -> fastapi.FastAPI: | ||
router = fastapi.APIRouter(prefix="/api", tags=["test_users"]) | ||
router.add_api_route("/test_users", receive_test_user, methods=["GET", "POST", "UPDATE", "DELETE"]) | ||
test_user_router = fastapi.APIRouter() | ||
test_user_router.add_api_route("/", test_user_handler, methods=["GET"]) | ||
|
||
app = fastapi.FastAPI() | ||
app.include_router(router) | ||
app.include_router(test_user_router, prefix="/test_user", tags=["test_users"]) | ||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from overhave.storage import ITestUserStorage, TestUserStorage | ||
|
||
|
||
def get_test_user_storage() -> ITestUserStorage: | ||
return TestUserStorage() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,43 @@ | ||
def receive_test_user() -> None: | ||
pass | ||
import logging | ||
from http import HTTPStatus | ||
from typing import Optional | ||
|
||
import fastapi | ||
|
||
from overhave.api.deps import get_test_user_storage | ||
from overhave.entities.converters import TestUserModel | ||
from overhave.storage import ITestUserStorage | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _test_user_id_handler(user_id: int, test_user_storage: ITestUserStorage) -> TestUserModel: | ||
logger.info("Getting %s with user_id=%s...", TestUserModel.__name__, user_id) | ||
test_user = test_user_storage.get_test_user_by_id(user_id) | ||
if test_user is None: | ||
raise fastapi.HTTPException(status_code=HTTPStatus.NOT_FOUND, detail=f"User with id={user_id} does not exist") | ||
return test_user | ||
|
||
|
||
def _test_user_name_handler(user_name: str, test_user_storage: ITestUserStorage) -> TestUserModel: | ||
logger.info("Getting %s with user_name='%s'...", TestUserModel.__name__, user_name) | ||
test_user = test_user_storage.get_test_user_by_name(user_name) | ||
if test_user is None: | ||
raise fastapi.HTTPException( | ||
status_code=HTTPStatus.NOT_FOUND, detail=f"User with name='{user_name}' does not exist" | ||
) | ||
return test_user | ||
|
||
|
||
def test_user_handler( | ||
user_id: Optional[int] = None, | ||
user_name: Optional[str] = None, | ||
test_user_storage: ITestUserStorage = fastapi.Depends(get_test_user_storage), | ||
) -> TestUserModel: | ||
if user_id is not None: | ||
return _test_user_id_handler(user_id=user_id, test_user_storage=test_user_storage) | ||
if user_name is not None: | ||
return _test_user_name_handler(user_name=user_name, test_user_storage=test_user_storage) | ||
raise fastapi.HTTPException( | ||
status_code=HTTPStatus.BAD_REQUEST, detail="'user_id' or 'user_name' query parameter should be set" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# flake8: noqa | ||
from .admin import admin | ||
from .consumers import consumer | ||
from .db import set_config_to_context | ||
from .db_cmds import set_config_to_context | ||
from .group import overhave | ||
from .synchronization import synchronize |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# flake8: noqa | ||
from .emulator import Emulator |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
|
||
from overhave import overhave_api | ||
|
||
|
||
@pytest.fixture() | ||
def test_api_client(database: None) -> TestClient: | ||
return TestClient(overhave_api()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import pytest as pytest | ||
from faker import Faker | ||
from fastapi.testclient import TestClient | ||
|
||
from overhave import db | ||
from overhave.entities.converters import TestUserModel | ||
|
||
|
||
@pytest.mark.parametrize("test_user_role", [db.Role.user], indirect=True) | ||
class TestTestUserAPi: | ||
"""Integration tests for Overhave TestUser API.""" | ||
|
||
def test_get_user_no_query(self, test_api_client: TestClient, test_user_role: db.Role) -> None: | ||
response = test_api_client.get("/test_user/") | ||
assert response.status_code == 400 | ||
assert response.content is not None | ||
|
||
def test_get_user_by_id_empty(self, test_api_client: TestClient, faker: Faker, test_user_role: db.Role) -> None: | ||
response = test_api_client.get(f"/test_user/?user_id={faker.random_int()}") | ||
assert response.status_code == 404 | ||
assert response.content is not None | ||
|
||
def test_get_user_by_id(self, test_api_client: TestClient, test_testuser: TestUserModel) -> None: | ||
response = test_api_client.get(f"/test_user/?user_id={test_testuser.id}") | ||
assert response.status_code == 200 | ||
assert response.json() | ||
obj = TestUserModel.parse_obj(response.json()) | ||
assert obj == test_testuser | ||
|
||
def test_get_user_by_name_empty(self, test_api_client: TestClient, faker: Faker, test_user_role: db.Role) -> None: | ||
response = test_api_client.get(f"/test_user/?user_name={faker.random_int()}") | ||
assert response.status_code == 404 | ||
assert response.content is not None | ||
|
||
def test_get_user_by_name(self, test_api_client: TestClient, test_testuser: TestUserModel) -> None: | ||
response = test_api_client.get(f"/test_user/?user_name={test_testuser.name}") | ||
assert response.status_code == 200 | ||
assert response.json() | ||
obj = TestUserModel.parse_obj(response.json()) | ||
assert obj == test_testuser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.