Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:Funtech-3/backend into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis-Shtanskiy committed Apr 14, 2024
2 parents 6724ff0 + 09b30c5 commit d340570
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 1 deletion.
2 changes: 2 additions & 0 deletions backend/events/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ class Meta:
model = Event
fields = (
"event_id",
"slug",
"name",
"description",
"city",
"date_event",
"registration_status",
Expand Down
3 changes: 3 additions & 0 deletions backend/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
DJANGO_SETTINGS_MODULE = funtech.settings
testpaths = tests/
8 changes: 7 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ djangorestframework==3.15.1
djangorestframework-simplejwt==5.3.1
djoser==2.2.2
drf-spectacular==0.27.2
exceptiongroup==1.2.0
filelock==3.13.4
flake8==7.0.0
gunicorn==21.2.0
identify==2.5.35
idna==3.6
inflection==0.5.1
iniconfig==2.0.0
isort==5.13.2
jsonschema==4.21.1
jsonschema-specifications==2023.12.1
Expand All @@ -31,22 +33,26 @@ oauthlib==3.2.2
packaging==24.0
pillow==10.3.0
platformdirs==4.2.0
pluggy==1.4.0
pre-commit==3.7.0
pycodestyle==2.11.1
pycparser==2.22
pyflakes==3.2.0
PyJWT==2.8.0
pytest==8.1.1
pytest-django==4.8.0
python-dotenv==1.0.1
python3-openid==3.2.0
PyYAML==6.0.1
referencing==0.34.0
requests==2.31.0
requests-oauthlib==2.0.0
rpds-py==0.18.0
setuptools==69.2.0
social-auth-app-django==5.4.0
social-auth-core==4.5.3
sqlparse==0.4.4
tomli==2.0.1
typing_extensions==4.11.0
tzdata==2024.1
uritemplate==4.1.1
urllib3==2.2.1
Expand Down
Empty file added backend/tests/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pytest_plugins = [
'tests.fixtures.fixture_user',
'tests.fixtures.fixture_data',
'tests.fixtures.fixture_urls',
]
Empty file.
80 changes: 80 additions & 0 deletions backend/tests/fixtures/fixture_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from datetime import date, time, timedelta
from tempfile import NamedTemporaryFile

import pytest
from events.models import Event, EventStep, Speaker
from users.models import City, Tag

LEN_OBJ_IN_LIST = 5


@pytest.fixture
def cities_list(db):
return City.objects.bulk_create(
City(name=f'Город {index}')
for index in range(LEN_OBJ_IN_LIST)
)


@pytest.fixture
def city(db):
return City.objects.create(name='Тестовый город')


@pytest.fixture
def tags_list(db):
return Tag.objects.bulk_create(
Tag(title=f'Тег {index}')
for index in range(LEN_OBJ_IN_LIST)
)


@pytest.fixture
def tag(db):
return Tag.objects.create(title='Тестовый тег')


@pytest.fixture
def speaker(db):
return Speaker.objects.create(
first_name='Иван',
last_name='Иванов',
work_place='ACME',
position='Работник'
)


@pytest.fixture
def image():
return NamedTemporaryFile(suffix='.jpg').name


@pytest.fixture
def event(db, city, image, tag):
event = Event.objects.create(
title='Тестовое событие',
description='Тестовое',
slug='test-event',
city=city,
address='Зимний дворец',
date=date.today() + timedelta(days=7),
image=image,
)
event.tags.add(tag)
return event


@pytest.fixture
def event_steps(db, events, speaker):
steps = EventStep.objects.bulk_create(
EventStep(
title=f'Этап события {index}',
start_time=time(hour=index),
description=f'Событие {index}',
event=event
)
for index, event in enumerate(events)
)
for step in steps:
step.speakers.add(speaker)
return steps
12 changes: 12 additions & 0 deletions backend/tests/fixtures/fixture_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest
from django.urls import reverse


@pytest.fixture
def url_event_list():
return reverse('event-list')


@pytest.fixture
def url_event_detail(event):
return reverse('event-detail', args=(event.slug,))
36 changes: 36 additions & 0 deletions backend/tests/fixtures/fixture_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest


@pytest.fixture
def user(django_user_model):
return django_user_model.objects.create(
yandex_id=11111
)


@pytest.fixture
def another_user(django_user_model):
return django_user_model.objects.create(
yandex_id=22222
)


# @pytest.fixture
# def token(user):
# from rest_framework_simplejwt.tokens import RefreshToken

# refresh = RefreshToken.for_user(user)
# return {
# 'refresh': str(refresh),
# 'access': str(refresh.access_token)
# }


@pytest.fixture
def user_client(user):
from rest_framework.test import APIClient

client = APIClient()
# client.credentials(HTTP_AUTHORIZATION=f'Bearer {token["access"]}')
client.force_authenticate(user=user)
return client
36 changes: 36 additions & 0 deletions backend/tests/test_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from http import HTTPStatus

import pytest


@pytest.mark.django_db(transaction=True)
class TestEventAPI:
"""Тесты эндпоинтов для списка событий и конкретного события."""

def check_event_response_data(self,
response_data,
expected_fields):
for field in expected_fields:
assert field in response_data, (

)

def test_events_not_found(self, client, url_event_list):
response = client.get(url_event_list)
assert response.status_code != HTTPStatus.NOT_FOUND, (
f'Эндпоинт {url_event_list} не найден.'
)

def test_event_list_not_auth(self, client, url_event_list):
response = client.get(url_event_list)
assert response.status_code == HTTPStatus.OK, (
f'Эндпоинт {url_event_list} должен быть доступен'
f'неавторизованным пользователям.'
)

def test_event_detail_not_auth(self, client, url_event_detail):
response = client.get(url_event_detail)
assert response.status_code == HTTPStatus.OK, (
f'Эндпоинт {url_event_detail} должен быть доступен'
f'неавторизованным пользователям.'
)

0 comments on commit d340570

Please sign in to comment.