Skip to content

Commit

Permalink
Защитил ручки (#16)
Browse files Browse the repository at this point in the history
## Изменения
Методы входа
  • Loading branch information
dyakovri authored Aug 26, 2023
1 parent a11fee3 commit f514a2a
Show file tree
Hide file tree
Showing 12 changed files with 348 additions and 247 deletions.
23 changes: 18 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: Python package
on:
pull_request:


jobs:
test:
name: Unit tests
Expand All @@ -19,7 +18,7 @@ jobs:
docker run -d -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust --name db-test postgres:15-alpine
- uses: actions/setup-python@v4
with:
python-version: '3.11'
python-version: "3.11"
- name: Install dependencies
run: |
python -m ensurepip
Expand All @@ -29,8 +28,15 @@ jobs:
run: |
DB_DSN=postgresql://postgres@localhost:5432/postgres alembic upgrade head
- name: Build coverage file
id: pytest
run: |
DB_DSN=postgresql://postgres@localhost:5432/postgres pytest --junitxml=pytest.xml --cov-report=term-missing:skip-covered tests/ | tee pytest-coverage.txt
DB_DSN=postgresql://postgres@localhost:5432/postgres \
pytest \
--junitxml=pytest.xml \
--cov-report=term-missing:skip-covered \
tests/ \
| tee pytest-coverage.txt
exit ${PIPESTATUS[0]}
- name: Print report
if: always()
run: |
Expand All @@ -49,6 +55,9 @@ jobs:
remove-link-from-badge: false
junitxml-path: ./pytest.xml
junitxml-title: Summary
- name: Fail
if: always() && steps.pytest.outcome == 'failure'
run: exit 1
linting:
runs-on: ubuntu-latest
steps:
Expand All @@ -61,8 +70,12 @@ jobs:
requirementsFiles: "requirements.txt requirements.dev.txt"
- uses: psf/black@stable
- name: Comment if linting failed
if: ${{ failure() }}
if: failure()
id: comment
uses: thollander/actions-comment-pull-request@v2
with:
message: |
:poop: Code linting failed, use `black` and `isort` to fix it.
:poop: Code linting failed, use `black` and `isort` to fix it.
- name: Fail
if: steps.comment.conclusion != 'failure' && steps.comment.conclusion != 'skipped'
run: exit 1
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ format:
make atomic-format module=aciniformes_backend
make atomic-format module=settings.py

format-dev:
make atomic-format module=tests

db:
docker run -d -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust --name db-pinger_backend postgres:15

Expand Down
22 changes: 15 additions & 7 deletions aciniformes_backend/routes/alert/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging

from auth_lib.fastapi import UnionAuth
from fastapi import APIRouter, Depends
from fastapi.exceptions import HTTPException
from pydantic import BaseModel
Expand Down Expand Up @@ -35,26 +36,31 @@ class GetSchema(BaseModel):
router = APIRouter()


@router.post(
"",
response_model=PostResponseSchema,
)
@router.post("")
async def create(
create_schema: CreateSchema,
alert: AlertServiceInterface = Depends(alert_service),
):
_: dict[str] = Depends(UnionAuth(['pinger.alert.create'])),
) -> PostResponseSchema:
id_ = await alert.create(create_schema.model_dump(exclude_unset=True))
return PostResponseSchema(**create_schema.model_dump(), id=id_)


@router.get("")
async def get_all(alert: AlertServiceInterface = Depends(alert_service)):
async def get_all(
alert: AlertServiceInterface = Depends(alert_service),
_: dict[str] = Depends(UnionAuth(['pinger.alert.read'])),
):
res = await alert.get_all()
return res


@router.get("/{id}")
async def get(id: int, alert: AlertServiceInterface = Depends(alert_service)):
async def get(
id: int,
alert: AlertServiceInterface = Depends(alert_service),
_: dict[str] = Depends(UnionAuth(['pinger.alert.read'])),
):
try:
res = await alert.get_by_id(id)
except exc.ObjectNotFound:
Expand All @@ -67,6 +73,7 @@ async def update(
id: int,
update_schema: UpdateSchema,
alert: AlertServiceInterface = Depends(alert_service),
_: dict[str] = Depends(UnionAuth(['pinger.alert.update'])),
):
try:
res = await alert.update(id, update_schema.model_dump(exclude_unset=True))
Expand All @@ -79,5 +86,6 @@ async def update(
async def delete(
id: int,
alert: AlertServiceInterface = Depends(alert_service),
_: dict[str] = Depends(UnionAuth(['pinger.alert.delete'])),
):
await alert.delete(id)
16 changes: 14 additions & 2 deletions aciniformes_backend/routes/alert/reciever.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from enum import Enum

from auth_lib.fastapi import UnionAuth
from fastapi import APIRouter, Depends
from fastapi.exceptions import HTTPException
from pydantic import BaseModel
Expand Down Expand Up @@ -47,21 +48,30 @@ class GetSchema(BaseModel):


@router.post("", response_model=PostResponseSchema)
async def create(create_schema: CreateSchema, receiver: ReceiverServiceInterface = Depends(receiver_service)):
async def create(
create_schema: CreateSchema,
receiver: ReceiverServiceInterface = Depends(receiver_service),
_: dict[str] = Depends(UnionAuth(['pinger.reciever.create'])),
):
id_ = await receiver.create(create_schema.model_dump())
return PostResponseSchema(**create_schema.model_dump(), id=id_)


@router.get("")
async def get_all(
receiver: ReceiverServiceInterface = Depends(receiver_service),
_: dict[str] = Depends(UnionAuth(['pinger.reciever.read'])),
):
res = await receiver.get_all()
return res


@router.get("/{id}")
async def get(id: int, receiver: ReceiverServiceInterface = Depends(receiver_service)):
async def get(
id: int,
receiver: ReceiverServiceInterface = Depends(receiver_service),
_: dict[str] = Depends(UnionAuth(['pinger.reciever.read'])),
):
try:
res = await receiver.get_by_id(id)
return res
Expand All @@ -74,6 +84,7 @@ async def update(
id: int,
update_schema: UpdateSchema,
receiver: ReceiverServiceInterface = Depends(receiver_service),
_: dict[str] = Depends(UnionAuth(['pinger.reciever.update'])),
):
try:
res = await receiver.update(id, update_schema.model_dump(exclude_unset=True))
Expand All @@ -86,5 +97,6 @@ async def update(
async def delete(
id: int,
receiver: ReceiverServiceInterface = Depends(receiver_service),
_: dict[str] = Depends(UnionAuth(['pinger.reciever.delete'])),
):
await receiver.delete(id)
6 changes: 6 additions & 0 deletions aciniformes_backend/routes/fetcher.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from auth_lib.fastapi import UnionAuth
from fastapi import APIRouter, Depends
from fastapi.exceptions import HTTPException
from pydantic import BaseModel, HttpUrl
Expand Down Expand Up @@ -45,6 +46,7 @@ class GetSchema(BaseModel):
async def create(
create_schema: CreateSchema,
fetcher: FetcherServiceInterface = Depends(fetcher_service),
_: dict[str] = Depends(UnionAuth(['pinger.fetcher.create'])),
):
id_ = await fetcher.create(create_schema.model_dump())
return ResponsePostSchema(**create_schema.model_dump(), id=id_)
Expand All @@ -53,6 +55,7 @@ async def create(
@router.get("")
async def get_all(
fetcher: FetcherServiceInterface = Depends(fetcher_service),
_: dict[str] = Depends(UnionAuth(['pinger.fetcher.read'])),
):
res = await fetcher.get_all()
return res
Expand All @@ -62,6 +65,7 @@ async def get_all(
async def get(
id: int,
fetcher: FetcherServiceInterface = Depends(fetcher_service),
_: dict[str] = Depends(UnionAuth(['pinger.fetcher.read'])),
):
try:
res = await fetcher.get_by_id(id)
Expand All @@ -75,6 +79,7 @@ async def update(
id: int,
update_schema: UpdateSchema,
fetcher: FetcherServiceInterface = Depends(fetcher_service),
_: dict[str] = Depends(UnionAuth(['pinger.fetcher.update'])),
):
try:
res = await fetcher.update(id, update_schema.model_dump(exclude_unset=True))
Expand All @@ -87,5 +92,6 @@ async def update(
async def delete(
id: int,
fetcher: FetcherServiceInterface = Depends(fetcher_service),
_: dict[str] = Depends(UnionAuth(['pinger.fetcher.delete'])),
):
await fetcher.delete(id)
13 changes: 11 additions & 2 deletions aciniformes_backend/routes/mectric.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from auth_lib.fastapi import UnionAuth
from fastapi import APIRouter, Depends
from fastapi.exceptions import HTTPException
from pydantic import BaseModel
Expand Down Expand Up @@ -32,19 +33,27 @@ class GetSchema(BaseModel):
async def create(
metric_schema: CreateSchema,
metric: MetricServiceInterface = Depends(metric_service),
_: dict[str] = Depends(UnionAuth(['pinger.metric.create'])),
):
id_ = await metric.create(metric_schema.model_dump())
return ResponsePostSchema(**metric_schema.model_dump(), id=id_)


@router.get("")
async def get_all(metric: MetricServiceInterface = Depends(metric_service)):
async def get_all(
metric: MetricServiceInterface = Depends(metric_service),
_: dict[str] = Depends(UnionAuth(['pinger.metric.read'])),
):
res = await metric.get_all()
return res


@router.get("/{id}")
async def get(id: int, metric: MetricServiceInterface = Depends(metric_service)):
async def get(
id: int,
metric: MetricServiceInterface = Depends(metric_service),
_: dict[str] = Depends(UnionAuth(['pinger.metric.read'])),
):
try:
res = await metric.get_by_id(id)
except exc.ObjectNotFound:
Expand Down
11 changes: 8 additions & 3 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
autoflake
black
flake8
httpx
isort
pytest
pytest-cov
pytest-asyncio
pytest_mock
httpx
pytest-asyncio
pytest-cov
requests
2 changes: 1 addition & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class Settings(BaseSettings):
DB_DSN: PostgresDsn
DB_DSN: PostgresDsn = 'postgresql://postgres@localhost:5432/postgres'
BACKEND_URL: HttpUrl = "http://127.0.0.1:8000"
FETCHERS_UPDATE_DELAY_IN_SECONDS: int = 10
model_config = ConfigDict(case_sensitive=True, env_file=".env", extra="ignore")
Expand Down
Loading

0 comments on commit f514a2a

Please sign in to comment.