mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-11-02 12:21:53 +08:00
Added codestyle check and lint to the build action (#798)
* Applied `make format` * Added format-check to makefile * Added a "Check codestyle" step to the build action. * Rerun `make format` * Added a "lint" target to makefile * Added lint step to the build action * Added W503 to the list of ignored flake8 errors. See https://www.flake8rules.com/rules/W503.html
This commit is contained in:
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@ -20,6 +20,10 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
pip install -r requirements.dev.txt
|
pip install -r requirements.dev.txt
|
||||||
|
- name: Check codestyle
|
||||||
|
run: make format-check
|
||||||
|
- name: Lint
|
||||||
|
run: make lint
|
||||||
- name: Test with pytest
|
- name: Test with pytest
|
||||||
env:
|
env:
|
||||||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
||||||
|
|||||||
12
Makefile
12
Makefile
@ -7,6 +7,18 @@ isort-docs:
|
|||||||
format: isort-src isort-docs
|
format: isort-src isort-docs
|
||||||
black .
|
black .
|
||||||
|
|
||||||
|
isort-src-check:
|
||||||
|
isort --check-only ./fastapi_users ./tests
|
||||||
|
|
||||||
|
isort-docs-check:
|
||||||
|
isort --check-only ./docs/src -o fastapi_users
|
||||||
|
|
||||||
|
format-check: isort-src-check isort-docs-check
|
||||||
|
black --check .
|
||||||
|
|
||||||
|
lint:
|
||||||
|
flake8 ./fastapi_users ./tests
|
||||||
|
|
||||||
test:
|
test:
|
||||||
pytest --cov=fastapi_users/ --cov-report=term-missing --cov-fail-under=100
|
pytest --cov=fastapi_users/ --cov-report=term-missing --cov-fail-under=100
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,9 @@
|
|||||||
import contextlib
|
import contextlib
|
||||||
|
|
||||||
from fastapi_users.manager import UserAlreadyExists
|
|
||||||
|
|
||||||
from app.db import get_user_db
|
from app.db import get_user_db
|
||||||
from app.models import UserCreate
|
from app.models import UserCreate
|
||||||
from app.users import get_user_manager
|
from app.users import get_user_manager
|
||||||
|
from fastapi_users.manager import UserAlreadyExists
|
||||||
|
|
||||||
get_user_db_context = contextlib.asynccontextmanager(get_user_db)
|
get_user_db_context = contextlib.asynccontextmanager(get_user_db)
|
||||||
get_user_manager_context = contextlib.asynccontextmanager(get_user_manager)
|
get_user_manager_context = contextlib.asynccontextmanager(get_user_manager)
|
||||||
|
|||||||
@ -21,7 +21,10 @@ def get_register_router(
|
|||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post(
|
@router.post(
|
||||||
"/register", response_model=user_model, status_code=status.HTTP_201_CREATED, name="register:register"
|
"/register",
|
||||||
|
response_model=user_model,
|
||||||
|
status_code=status.HTTP_201_CREATED,
|
||||||
|
name="register:register",
|
||||||
)
|
)
|
||||||
async def register(
|
async def register(
|
||||||
request: Request,
|
request: Request,
|
||||||
|
|||||||
@ -19,7 +19,11 @@ def get_reset_password_router(
|
|||||||
"""Generate a router with the reset password routes."""
|
"""Generate a router with the reset password routes."""
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/forgot-password", status_code=status.HTTP_202_ACCEPTED, name="reset:forgot_password")
|
@router.post(
|
||||||
|
"/forgot-password",
|
||||||
|
status_code=status.HTTP_202_ACCEPTED,
|
||||||
|
name="reset:forgot_password",
|
||||||
|
)
|
||||||
async def forgot_password(
|
async def forgot_password(
|
||||||
request: Request,
|
request: Request,
|
||||||
email: EmailStr = Body(..., embed=True),
|
email: EmailStr = Body(..., embed=True),
|
||||||
|
|||||||
@ -52,7 +52,7 @@ def get_users_router(
|
|||||||
"/me",
|
"/me",
|
||||||
response_model=user_model,
|
response_model=user_model,
|
||||||
dependencies=[Depends(get_current_active_user)],
|
dependencies=[Depends(get_current_active_user)],
|
||||||
name="users:current_user"
|
name="users:current_user",
|
||||||
)
|
)
|
||||||
async def update_me(
|
async def update_me(
|
||||||
request: Request,
|
request: Request,
|
||||||
@ -82,7 +82,7 @@ def get_users_router(
|
|||||||
"/{id:uuid}",
|
"/{id:uuid}",
|
||||||
response_model=user_model,
|
response_model=user_model,
|
||||||
dependencies=[Depends(get_current_superuser)],
|
dependencies=[Depends(get_current_superuser)],
|
||||||
name="users:user"
|
name="users:user",
|
||||||
)
|
)
|
||||||
async def get_user(user=Depends(get_user_or_404)):
|
async def get_user(user=Depends(get_user_or_404)):
|
||||||
return user
|
return user
|
||||||
@ -91,7 +91,7 @@ def get_users_router(
|
|||||||
"/{id:uuid}",
|
"/{id:uuid}",
|
||||||
response_model=user_model,
|
response_model=user_model,
|
||||||
dependencies=[Depends(get_current_superuser)],
|
dependencies=[Depends(get_current_superuser)],
|
||||||
name="users:user"
|
name="users:user",
|
||||||
)
|
)
|
||||||
async def update_user(
|
async def update_user(
|
||||||
user_update: user_update_model, # type: ignore
|
user_update: user_update_model, # type: ignore
|
||||||
@ -122,7 +122,7 @@ def get_users_router(
|
|||||||
status_code=status.HTTP_204_NO_CONTENT,
|
status_code=status.HTTP_204_NO_CONTENT,
|
||||||
response_class=Response,
|
response_class=Response,
|
||||||
dependencies=[Depends(get_current_superuser)],
|
dependencies=[Depends(get_current_superuser)],
|
||||||
name="users:user"
|
name="users:user",
|
||||||
)
|
)
|
||||||
async def delete_user(
|
async def delete_user(
|
||||||
user=Depends(get_user_or_404),
|
user=Depends(get_user_or_404),
|
||||||
|
|||||||
@ -21,7 +21,11 @@ def get_verify_router(
|
|||||||
):
|
):
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/request-verify-token", status_code=status.HTTP_202_ACCEPTED, name="verify:request-token")
|
@router.post(
|
||||||
|
"/request-verify-token",
|
||||||
|
status_code=status.HTTP_202_ACCEPTED,
|
||||||
|
name="verify:request-token",
|
||||||
|
)
|
||||||
async def request_verify_token(
|
async def request_verify_token(
|
||||||
request: Request,
|
request: Request,
|
||||||
email: EmailStr = Body(..., embed=True),
|
email: EmailStr = Body(..., embed=True),
|
||||||
|
|||||||
@ -11,7 +11,7 @@ replace = __version__ = "{new_version}"
|
|||||||
exclude = docs
|
exclude = docs
|
||||||
max-line-length = 88
|
max-line-length = 88
|
||||||
docstring-convention = numpy
|
docstring-convention = numpy
|
||||||
ignore = D1
|
ignore = D1, W503
|
||||||
|
|
||||||
[isort]
|
[isort]
|
||||||
profile = black
|
profile = black
|
||||||
@ -26,7 +26,7 @@ ignore_missing_imports = True
|
|||||||
|
|
||||||
[tool:pytest]
|
[tool:pytest]
|
||||||
addopts = --ignore=test_build.py
|
addopts = --ignore=test_build.py
|
||||||
markers =
|
markers =
|
||||||
authentication
|
authentication
|
||||||
db
|
db
|
||||||
fastapi_users
|
fastapi_users
|
||||||
|
|||||||
@ -157,17 +157,11 @@ class TestLogin:
|
|||||||
data = cast(Dict[str, Any], response.json())
|
data = cast(Dict[str, Any], response.json())
|
||||||
assert data["detail"] == ErrorCode.LOGIN_BAD_CREDENTIALS
|
assert data["detail"] == ErrorCode.LOGIN_BAD_CREDENTIALS
|
||||||
|
|
||||||
async def test_login_namespace(
|
async def test_login_namespace(self, path, app_factory):
|
||||||
self,
|
|
||||||
path,
|
|
||||||
app_factory
|
|
||||||
):
|
|
||||||
split_url = app_factory(True).url_path_for("auth:login").split("/")
|
split_url = app_factory(True).url_path_for("auth:login").split("/")
|
||||||
assert split_url[len(split_url) - 1] in path
|
assert split_url[len(split_url) - 1] in path
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.router
|
@pytest.mark.router
|
||||||
@pytest.mark.parametrize("path", ["/mock/logout", "/mock-bis/logout"])
|
@pytest.mark.parametrize("path", ["/mock/logout", "/mock-bis/logout"])
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
@ -210,10 +204,6 @@ class TestLogout:
|
|||||||
)
|
)
|
||||||
assert response.status_code == status.HTTP_200_OK
|
assert response.status_code == status.HTTP_200_OK
|
||||||
|
|
||||||
async def test_logout_namespace(
|
async def test_logout_namespace(self, path, app_factory):
|
||||||
self,
|
|
||||||
path,
|
|
||||||
app_factory
|
|
||||||
):
|
|
||||||
split_url = app_factory(True).url_path_for("auth:logout").split("/")
|
split_url = app_factory(True).url_path_for("auth:logout").split("/")
|
||||||
assert split_url[len(split_url) - 1] in path
|
assert split_url[len(split_url) - 1] in path
|
||||||
|
|||||||
@ -105,9 +105,7 @@ class TestRegister:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_register_namespace(
|
async def test_register_namespace(get_user_manager):
|
||||||
get_user_manager
|
|
||||||
):
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.include_router(
|
app.include_router(
|
||||||
get_register_router(
|
get_register_router(
|
||||||
|
|||||||
@ -151,22 +151,14 @@ class TestResetPassword:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_forgot_password_namespace(
|
async def test_forgot_password_namespace(get_user_manager):
|
||||||
get_user_manager
|
|
||||||
):
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.include_router(
|
app.include_router(get_reset_password_router(get_user_manager))
|
||||||
get_reset_password_router(get_user_manager)
|
|
||||||
)
|
|
||||||
assert app.url_path_for("reset:forgot_password") == "/forgot-password"
|
assert app.url_path_for("reset:forgot_password") == "/forgot-password"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_reset_password_namespace(
|
async def test_reset_password_namespace(get_user_manager):
|
||||||
get_user_manager
|
|
||||||
):
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.include_router(
|
app.include_router(get_reset_password_router(get_user_manager))
|
||||||
get_reset_password_router(get_user_manager)
|
|
||||||
)
|
|
||||||
assert app.url_path_for("reset:reset_password") == "/reset-password"
|
assert app.url_path_for("reset:reset_password") == "/reset-password"
|
||||||
|
|||||||
@ -98,10 +98,7 @@ class TestMe:
|
|||||||
assert data["id"] == str(verified_user.id)
|
assert data["id"] == str(verified_user.id)
|
||||||
assert data["email"] == verified_user.email
|
assert data["email"] == verified_user.email
|
||||||
|
|
||||||
async def test_current_user_namespace(
|
async def test_current_user_namespace(self, app_factory):
|
||||||
self,
|
|
||||||
app_factory
|
|
||||||
):
|
|
||||||
assert app_factory(True).url_path_for("users:current_user") == "/me"
|
assert app_factory(True).url_path_for("users:current_user") == "/me"
|
||||||
|
|
||||||
|
|
||||||
@ -486,11 +483,7 @@ class TestGetUser:
|
|||||||
assert data["id"] == str(user.id)
|
assert data["id"] == str(user.id)
|
||||||
assert "hashed_password" not in data
|
assert "hashed_password" not in data
|
||||||
|
|
||||||
async def test_get_user_namespace(
|
async def test_get_user_namespace(self, app_factory, user: UserDB):
|
||||||
self,
|
|
||||||
app_factory,
|
|
||||||
user: UserDB
|
|
||||||
):
|
|
||||||
assert app_factory(True).url_path_for("users:user", id=user.id) == f"/{user.id}"
|
assert app_factory(True).url_path_for("users:user", id=user.id) == f"/{user.id}"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user