From 204553532973718f300b0e08aeaa79e31bb2c6c0 Mon Sep 17 00:00:00 2001 From: Ernesto Rico Schmidt <ernesto@helsinki.at> Date: Mon, 8 May 2023 16:09:07 -0400 Subject: [PATCH] Add conftest --- conftest.py | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 conftest.py diff --git a/conftest.py b/conftest.py new file mode 100644 index 00000000..67683060 --- /dev/null +++ b/conftest.py @@ -0,0 +1,155 @@ +from datetime import datetime, timedelta + +import pytest +from rest_framework.test import APIClient + +from django.contrib.auth.models import User +from django.core.files.uploadedfile import SimpleUploadedFile +from program.models import FundingCategory, Host, RRule, Schedule, Show, TimeSlot, Type, Image +from program.tests.factories import ( + CommonUserFactory, + FundingCategoryFactory, + HostFactory, + RRuleFactory, + ScheduleFactory, + ShowFactory, + TimeslotFactory, + TypeFactory, ImageFactory, +) + + +def assert_data(response, data) -> None: + if "schedule" in data: + for key, value in data["schedule"].items(): + if key == "rrule": + assert response.data[key] == value + else: + assert str(response.data[key]) == value + else: + for key, value in data.items(): + if key == "password": + continue + assert response.data[key] == value + + +@pytest.fixture +def host() -> Host: + return HostFactory() + + +@pytest.fixture +def image_file() -> SimpleUploadedFile: + return SimpleUploadedFile("image.png", open("program/tests/data/image.png", "rb").read()) + + +@pytest.fixture +def image(image_file) -> Image: + return ImageFactory(image=image_file) + + +@pytest.fixture +def owned_image(image_file, common_user1) -> Image: + return ImageFactory(image=image_file, owner=common_user1) + + +@pytest.fixture +def api_client() -> APIClient: + """Unauthenticated API client""" + + return APIClient() + + +@pytest.fixture +def admin_api_client(api_client, admin_user) -> APIClient: + """Authenticated admin user (superuser) API client""" + + api_client.force_authenticate(admin_user) + yield api_client + api_client.force_authenticate() + + +@pytest.fixture +def common_user1() -> User: + return CommonUserFactory() + + +@pytest.fixture +def common_user2() -> User: + return CommonUserFactory() + + +@pytest.fixture +def common_api_client1(api_client, common_user1) -> APIClient: + """Authenticated common user 1 API client""" + + api_client.force_authenticate(common_user1) + yield api_client + api_client.force_authenticate() + + +@pytest.fixture +def common_api_client2(api_client, common_user2) -> APIClient: + """Authenticated common user 2 API client""" + + api_client.force_authenticate(common_user2) + yield api_client + api_client.force_authenticate() + + +@pytest.fixture +def once_rrule() -> RRule: + return RRuleFactory(freq=0) + + +@pytest.fixture +def show() -> Show: + return ShowFactory() + + +@pytest.fixture +def owned_show(common_user1): + show = ShowFactory() + + show.owners.set([common_user1]) + show.save() + + return show + + +@pytest.fixture +def owned_show_once_timeslot(common_user1, show, once_schedule) -> TimeSlot: + show.owners.set([common_user1]) + show.save() + + timeslot = TimeslotFactory(show=show, schedule=once_schedule) + + return timeslot + + +@pytest.fixture +def once_timeslot(show, once_schedule) -> TimeSlot: + return TimeslotFactory(show=show, schedule=once_schedule) + + +@pytest.fixture +def once_schedule(once_rrule, show) -> Schedule: + start = datetime.now() + end = start + timedelta(hours=1) + + return ScheduleFactory( + end_time=end.strftime("%H:%M:%S"), + first_date=start.strftime("%Y-%m-%d"), + rrule=once_rrule, + show=show, + start_time=start.strftime("%H:%M:%S"), + ) + + +@pytest.fixture +def funding_category() -> FundingCategory: + return FundingCategoryFactory() + + +@pytest.fixture +def type_() -> Type: + return TypeFactory() -- GitLab