From 136483b335bc652bfd29882af70466694cfd6ddd Mon Sep 17 00:00:00 2001 From: Ernesto Rico Schmidt <ernesto@helsinki.at> Date: Thu, 28 Mar 2024 16:01:02 -0400 Subject: [PATCH] test: update tests for notes to use pytest & permissions --- program/tests/test_notes.py | 170 +++++++++++++++++++++--------------- 1 file changed, 98 insertions(+), 72 deletions(-) diff --git a/program/tests/test_notes.py b/program/tests/test_notes.py index c16f048d..ee7a587d 100644 --- a/program/tests/test_notes.py +++ b/program/tests/test_notes.py @@ -1,72 +1,98 @@ -from rest_framework.test import APITransactionTestCase - -from program import tests -from program.models import Schedule, Show - - -class NoteViewTestCase(tests.BaseMixin, APITransactionTestCase): - reset_sequences = True - - show_beatbetrieb: Show - schedule_beatbetrieb: Schedule - show_musikrotation: Show - schedule_musikrotation: Schedule - - def setUp(self) -> None: - super().setUp() - self.show_beatbetrieb = self._create_show("Beatbetrieb") - self.schedule_beatbetrieb = self._create_schedule(self.show_beatbetrieb) - self.show_musikrotation = self._create_show("Musikrotation", owners=[self.user_common]) - self.schedule_musikrotation = self._create_schedule( - self.show_musikrotation, start_time="10:00", end_time="12:00" - ) - - def test_everyone_can_read_notes(self): - self._create_note(self._create_timeslot(schedule=self.schedule_beatbetrieb)) - self._create_note(self._create_timeslot(schedule=self.schedule_musikrotation)) - res = self._get_client().get(self._url("notes")) - self.assertEqual(len(res.data), 2) - - def test_common_users_can_create_notes_for_owned_shows(self): - ts = self._create_timeslot(schedule=self.schedule_musikrotation) - client = self._get_client(self.user_common) - endpoint = self._url("notes") - res = client.post( - endpoint, self._create_random_note_content(timeslot_id=ts.id), format="json" - ) - self.assertEqual(res.status_code, 201) - - def test_common_users_cannot_create_notes_for_foreign_shows(self): - ts = self._create_timeslot(schedule=self.schedule_beatbetrieb) - client = self._get_client(self.user_common) - endpoint = self._url("notes") - res = client.post( - endpoint, self._create_random_note_content(timeslot_id=ts.id), format="json" - ) - self.assertEqual(res.status_code, 404) - - def test_common_user_can_update_owned_shows(self): - ts = self._create_timeslot(schedule=self.schedule_musikrotation) - note = self._create_note(ts) - client = self._get_client(self.user_common) - new_note_content = self._create_random_note_content(title="meh") - res = client.put(self._url("notes", note.id), new_note_content, format="json") - self.assertEqual(res.status_code, 200) - - def test_common_user_cannot_update_notes_of_foreign_shows(self): - ts = self._create_timeslot(schedule=self.schedule_beatbetrieb) - note = self._create_note(ts) - client = self._get_client(self.user_common) - new_note_content = self._create_random_note_content(title="meh") - res = client.put(self._url("notes", note.id), new_note_content, format="json") - self.assertEqual(res.status_code, 404) - - def test_admin_can_create_notes_for_all_timeslots(self): - timeslot = self._create_timeslot(schedule=self.schedule_musikrotation) - client = self._get_client(self.user_admin) - res = client.post( - self._url("notes"), - self._create_random_note_content(timeslot_id=timeslot.id), - format="json", - ) - self.assertEqual(res.status_code, 201) +import pytest + +from program.models import Note, TimeSlot +from program.tests.factories import NoteFactory + +pytestmark = pytest.mark.django_db + + +def url(note=None): + if note: + return f"/api/v1/notes/{note.id}/" + else: + return "/api/v1/notes/" + + +def note_data(timeslot: TimeSlot) -> dict[str, str | int]: + return {"content": "CONTENT", "title": "TITLE", "timeslot_id": timeslot.id} + + +def test_read_notes_as_unauthenticated_user(api_client): + NOTES = 3 + NoteFactory.create_batch(size=NOTES) + + response = api_client.get(url()) + + assert response.status_code == 200 + assert len(response.data) == NOTES + + +@pytest.mark.django_db(transaction=True) +def test_create_note_for_owned_show_fails_if_it_exists( + user_with_note_perms, api_client_note_perms, owned_show_once_timeslot_perms +): + data = note_data(timeslot=owned_show_once_timeslot_perms) + + response = api_client_note_perms.post(url(), data=data) + + assert response.status_code == 400 + + +def test_create_note_for_owned_show( + user_with_note_perms, api_client_note_perms, owned_show_once_timeslot_perms +): + Note.objects.get(pk=owned_show_once_timeslot_perms.note.id).delete() + + data = note_data(timeslot=owned_show_once_timeslot_perms) + + response = api_client_note_perms.post(url(), data=data) + + assert response.status_code == 201 + + +def test_create_note_not_found_for_not_owned_show( + user_with_note_perms, api_client_note_perms, show_once_timeslot +): + data = note_data(timeslot=show_once_timeslot) + + response = api_client_note_perms.patch(url(note=show_once_timeslot.note), data=data) + + assert response.status_code == 404 + + +def test_update_note_for_owned_show( + user_with_note_perms, api_client_note_perms, owned_show_once_timeslot_perms +): + update = note_data(timeslot=owned_show_once_timeslot_perms) + + response = api_client_note_perms.patch( + url(note=owned_show_once_timeslot_perms.note), data=update + ) + + assert response.status_code == 200 + + +def test_update_note_not_found_for_not_owned_show( + user_with_note_perms, api_client_note_perms, once_timeslot +): + update = note_data(timeslot=once_timeslot) + + response = api_client_note_perms.patch(url(note=once_timeslot.note), data=update) + + assert response.status_code == 404 + + +def test_delete_note_for_owned_show( + user_with_note_perms, api_client_note_perms, owned_show_once_timeslot_perms +): + response = api_client_note_perms.delete(url(note=owned_show_once_timeslot_perms.note)) + + assert response.status_code == 204 + + +def test_delete_note_not_found_for_not_owned_show( + user_with_note_perms, api_client_note_perms, once_timeslot +): + response = api_client_note_perms.delete(url(note=once_timeslot.note)) + + assert response.status_code == 404 -- GitLab