Skip to content

Commit

Permalink
feat: pytest integration
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasvinclav committed Jan 1, 2025
1 parent d5a6871 commit 6f8928c
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 4 deletions.
Empty file added backend/api/tests/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions backend/api/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pytest_factoryboy import register

from api.tests.factories import UserFactory
from api.tests.fixtures import * # noqa: F403

register(UserFactory)
10 changes: 10 additions & 0 deletions backend/api/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.contrib.auth import get_user_model
from factory.django import DjangoModelFactory


class UserFactory(DjangoModelFactory):
username = "[email protected]"
email = "[email protected]"

class Meta:
model = get_user_model()
12 changes: 12 additions & 0 deletions backend/api/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest
from rest_framework.test import APIClient


@pytest.fixture
def api_client():
return APIClient()


@pytest.fixture
def regular_user(user_factory):
return user_factory.create(is_active=False)
16 changes: 16 additions & 0 deletions backend/api/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest
from django.urls import reverse
from rest_framework import status


@pytest.mark.django_db
def test_api_users_me_unauthorized(client):
response = client.get(reverse("api-users-me"))
assert response.status_code == status.HTTP_401_UNAUTHORIZED


@pytest.mark.django_db
def test_api_users_me_authorized(api_client, regular_user):
api_client.force_authenticate(user=regular_user)
response = api_client.get(reverse("api-users-me"))
assert response.status_code == status.HTTP_200_OK
5 changes: 1 addition & 4 deletions backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
from django.urls import include, path
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
from rest_framework import routers
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView

from .api import UserViewSet

Expand Down
11 changes: 11 additions & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ dependencies = [
"django-unfold>=0.43.0",
]

[dependency-groups]
dev = [
"pytest>=8.3.4",
"pytest-django>=4.9.0",
"pytest-factoryboy>=2.7.0",
]

[tool.ruff]
fix = true
line-length = 88
Expand All @@ -30,3 +37,7 @@ ignore = [
"B008", # do not perform function calls in argument defaults
"C901", # too complex
]

[tool.pytest.ini_options]
filterwarnings = "ignore"
addopts = "--strict-config --strict-markers --ds=api.settings"
148 changes: 148 additions & 0 deletions backend/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6f8928c

Please sign in to comment.