|
| 1 | +import pytest |
| 2 | +from django.contrib.auth.models import Permission |
| 3 | +from django.test import Client |
| 4 | +from django.urls import reverse |
| 5 | +from model_bakery import baker |
| 6 | +from pytest_django.asserts import assertNumQueries, assertRedirects |
| 7 | + |
| 8 | +from club.models import Club |
| 9 | +from core.models import User |
| 10 | +from reservation.forms import RoomUpdateForm |
| 11 | +from reservation.models import Room |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.django_db |
| 15 | +class TestFetchRoom: |
| 16 | + @pytest.fixture |
| 17 | + def user(self): |
| 18 | + return baker.make( |
| 19 | + User, |
| 20 | + user_permissions=[Permission.objects.get(codename="view_room")], |
| 21 | + ) |
| 22 | + |
| 23 | + def test_fetch_simple(self, client: Client, user: User): |
| 24 | + rooms = baker.make(Room, _quantity=3, _bulk_create=True) |
| 25 | + client.force_login(user) |
| 26 | + response = client.get(reverse("api:fetch_reservable_rooms")) |
| 27 | + assert response.status_code == 200 |
| 28 | + assert response.json() == [ |
| 29 | + { |
| 30 | + "id": room.id, |
| 31 | + "name": room.name, |
| 32 | + "description": room.description, |
| 33 | + "location": room.location, |
| 34 | + "club": {"id": room.club.id, "name": room.club.name}, |
| 35 | + } |
| 36 | + for room in rooms |
| 37 | + ] |
| 38 | + |
| 39 | + def test_nb_queries(self, client: Client, user: User): |
| 40 | + client.force_login(user) |
| 41 | + with assertNumQueries(5): |
| 42 | + # 4 for authentication |
| 43 | + # 1 to fetch the actual data |
| 44 | + client.get(reverse("api:fetch_reservable_rooms")) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.django_db |
| 48 | +class TestCreateRoom: |
| 49 | + def test_ok(self, client: Client): |
| 50 | + perm = Permission.objects.get(codename="add_room") |
| 51 | + club = baker.make(Club) |
| 52 | + client.force_login( |
| 53 | + baker.make(User, user_permissions=[perm], groups=[club.board_group]) |
| 54 | + ) |
| 55 | + response = client.post( |
| 56 | + reverse("reservation:room_create"), |
| 57 | + data={"club": club.id, "name": "test", "location": "BELFORT"}, |
| 58 | + ) |
| 59 | + assertRedirects(response, reverse("club:tools", kwargs={"club_id": club.id})) |
| 60 | + room = Room.objects.last() |
| 61 | + assert room is not None |
| 62 | + assert room.club == club |
| 63 | + assert room.name == "test" |
| 64 | + assert room.location == "BELFORT" |
| 65 | + |
| 66 | + def test_permission_denied(self, client: Client): |
| 67 | + club = baker.make(Club) |
| 68 | + client.force_login(baker.make(User)) |
| 69 | + response = client.get(reverse("reservation:room_create")) |
| 70 | + assert response.status_code == 403 |
| 71 | + response = client.post( |
| 72 | + reverse("reservation:room_create"), |
| 73 | + data={"club": club.id, "name": "test", "location": "BELFORT"}, |
| 74 | + ) |
| 75 | + assert response.status_code == 403 |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.django_db |
| 79 | +class TestUpdateRoom: |
| 80 | + def test_ok(self, client: Client): |
| 81 | + club = baker.make(Club) |
| 82 | + room = baker.make(Room, club=club) |
| 83 | + client.force_login(baker.make(User, groups=[club.board_group])) |
| 84 | + url = reverse("reservation:room_edit", kwargs={"room_id": room.id}) |
| 85 | + response = client.post(url, data={"name": "test", "location": "BELFORT"}) |
| 86 | + assertRedirects(response, url) |
| 87 | + room.refresh_from_db() |
| 88 | + assert room.club == club |
| 89 | + assert room.name == "test" |
| 90 | + assert room.location == "BELFORT" |
| 91 | + |
| 92 | + def test_permission_denied(self, client: Client): |
| 93 | + club = baker.make(Club) |
| 94 | + room = baker.make(Room, club=club) |
| 95 | + client.force_login(baker.make(User)) |
| 96 | + url = reverse("reservation:room_edit", kwargs={"room_id": room.id}) |
| 97 | + response = client.get(url) |
| 98 | + assert response.status_code == 403 |
| 99 | + response = client.post(url, data={"name": "test", "location": "BELFORT"}) |
| 100 | + assert response.status_code == 403 |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.django_db |
| 104 | +class TestUpdateRoomForm: |
| 105 | + def test_form_club_edition_rights(self): |
| 106 | + """The club field should appear only if the request user can edit it.""" |
| 107 | + room = baker.make(Room) |
| 108 | + perm = Permission.objects.get(codename="change_room") |
| 109 | + user_authorized = baker.make(User, user_permissions=[perm]) |
| 110 | + assert "club" in RoomUpdateForm(request_user=user_authorized).fields |
| 111 | + |
| 112 | + user_forbidden = baker.make(User, groups=[room.club.board_group]) |
| 113 | + assert "club" not in RoomUpdateForm(request_user=user_forbidden).fields |
0 commit comments