|
| 1 | +# Patchwork - automated patch tracking system |
| 2 | +# Copyright (C) 2024 Collabora |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | + |
| 6 | +from django.test import override_settings |
| 7 | +from django.urls import reverse |
| 8 | +from rest_framework import status |
| 9 | + |
| 10 | +from patchwork.models import Note |
| 11 | +from patchwork.tests.api import utils |
| 12 | +from patchwork.tests.utils import create_patch |
| 13 | +from patchwork.tests.utils import create_maintainer |
| 14 | +from patchwork.tests.utils import create_person |
| 15 | +from patchwork.tests.utils import create_project |
| 16 | +from patchwork.tests.utils import create_note |
| 17 | +from patchwork.tests.utils import create_user |
| 18 | + |
| 19 | + |
| 20 | +@override_settings(ENABLE_REST_API=True) |
| 21 | +class TestPatchNotes(utils.APITestCase): |
| 22 | + def setUp(self): |
| 23 | + super().setUp() |
| 24 | + self.project = create_project() |
| 25 | + self.user = create_maintainer(self.project) |
| 26 | + self.patch = create_patch(project=self.project) |
| 27 | + |
| 28 | + def check_for_expected(self, instance, response_data): |
| 29 | + self.assertEqual(instance.id, response_data['id']) |
| 30 | + self.assertEqual(instance.patch.id, response_data['patch']['id']) |
| 31 | + self.assertEqual( |
| 32 | + instance.submitter.id, response_data['submitter']['id'] |
| 33 | + ) |
| 34 | + |
| 35 | + def test_create_note(self): |
| 36 | + start_num = Note.objects.count() |
| 37 | + url = reverse( |
| 38 | + 'api-patch-note-list', kwargs={'patch_id': self.patch.id} |
| 39 | + ) |
| 40 | + data = {'content': 'New note'} |
| 41 | + self.client.authenticate(user=self.user) |
| 42 | + resp = self.client.post(url, data=data) |
| 43 | + end_num = Note.objects.count() |
| 44 | + |
| 45 | + self.assertEqual(status.HTTP_201_CREATED, resp.status_code) |
| 46 | + self.assertEqual(start_num + 1, end_num) |
| 47 | + |
| 48 | + def test_get_note_as_maintainer(self): |
| 49 | + """Retrieve patch note with an user that is a maintainer.""" |
| 50 | + note = create_note(patch=self.patch, submitter=self.user) |
| 51 | + |
| 52 | + self.client.authenticate(user=self.user) |
| 53 | + url = reverse( |
| 54 | + 'api-patch-note-detail', |
| 55 | + kwargs={'patch_id': self.patch.id, 'note_id': note.id}, |
| 56 | + ) |
| 57 | + resp = self.client.get(url) |
| 58 | + |
| 59 | + self.assertEqual(status.HTTP_200_OK, resp.status_code) |
| 60 | + self.check_for_expected(note, resp.data) |
| 61 | + |
| 62 | + def test_get_note_as_non_maintainer(self): |
| 63 | + """Retrieve patch note with an user that is not a maintainer.""" |
| 64 | + note = create_note() |
| 65 | + |
| 66 | + self.client.authenticate(user=self.user) |
| 67 | + url = reverse( |
| 68 | + 'api-patch-note-detail', |
| 69 | + kwargs={'patch_id': self.patch.id, 'note_id': note.id}, |
| 70 | + ) |
| 71 | + resp = self.client.get(url) |
| 72 | + |
| 73 | + self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code) |
| 74 | + |
| 75 | + def test_get_note_public(self): |
| 76 | + """Retrieve public patch note with an user that is not a maintainer.""" |
| 77 | + person = create_person(user=self.user) |
| 78 | + note = create_note(patch=self.patch, maintainer_only=False) |
| 79 | + |
| 80 | + self.client.authenticate(user=person.user) |
| 81 | + url = reverse( |
| 82 | + 'api-patch-note-detail', |
| 83 | + kwargs={'patch_id': self.patch.id, 'note_id': note.id}, |
| 84 | + ) |
| 85 | + resp = self.client.get(url) |
| 86 | + |
| 87 | + self.assertEqual(status.HTTP_200_OK, resp.status_code) |
| 88 | + self.check_for_expected(note, resp.data) |
| 89 | + |
| 90 | + def test_get_note_list_as_maintainer(self): |
| 91 | + """Retrieve notes from a patch note with an user that is a maintainer.""" |
| 92 | + create_note(patch=self.patch, submitter=self.user) |
| 93 | + create_note( |
| 94 | + patch=self.patch, submitter=self.user, maintainer_only=False |
| 95 | + ) |
| 96 | + |
| 97 | + self.client.authenticate(user=self.user) |
| 98 | + url = reverse( |
| 99 | + 'api-patch-note-list', kwargs={'patch_id': self.patch.id} |
| 100 | + ) |
| 101 | + resp = self.client.get(url) |
| 102 | + |
| 103 | + self.assertEqual(status.HTTP_200_OK, resp.status_code) |
| 104 | + self.assertEqual(len(resp.data), 2) |
| 105 | + |
| 106 | + def test_get_note_list_as_non_maintainer(self): |
| 107 | + """Retrieve notes from a patch note with an user that is not a maintainer.""" |
| 108 | + create_note(patch=self.patch, submitter=self.user) |
| 109 | + public_note = create_note( |
| 110 | + patch=self.patch, submitter=self.user, maintainer_only=False |
| 111 | + ) |
| 112 | + not_maintainer = create_user() |
| 113 | + |
| 114 | + self.client.authenticate(user=not_maintainer) |
| 115 | + url = reverse( |
| 116 | + 'api-patch-note-list', kwargs={'patch_id': self.patch.id} |
| 117 | + ) |
| 118 | + resp = self.client.get(url) |
| 119 | + |
| 120 | + self.assertEqual(status.HTTP_200_OK, resp.status_code) |
| 121 | + self.assertEqual(len(resp.data), 1) |
| 122 | + self.assertEqual(resp.data[0]['id'], public_note.id) |
| 123 | + |
| 124 | + def test_edit_note_as_maintainer(self): |
| 125 | + """Edit patch note with an user that is a maintainer.""" |
| 126 | + note = create_note(patch=self.patch, submitter=self.user) |
| 127 | + |
| 128 | + url = reverse( |
| 129 | + 'api-patch-note-detail', |
| 130 | + kwargs={'patch_id': self.patch.id, 'note_id': note.id}, |
| 131 | + ) |
| 132 | + data = {'content': 'New content'} |
| 133 | + self.client.authenticate(user=self.user) |
| 134 | + resp = self.client.patch(url, data=data) |
| 135 | + |
| 136 | + self.assertEqual(status.HTTP_200_OK, resp.status_code) |
| 137 | + self.check_for_expected(note, resp.data) |
| 138 | + self.assertNotEqual(note.content, resp.data['content']) |
| 139 | + self.assertNotEqual(note.updated_at, resp.data['updated_at']) |
| 140 | + |
| 141 | + def test_edit_note_as_non_maintainer(self): |
| 142 | + """Edit patch note with an user that is not a maintainer.""" |
| 143 | + note = create_note() |
| 144 | + |
| 145 | + url = reverse( |
| 146 | + 'api-patch-note-detail', |
| 147 | + kwargs={'patch_id': self.patch.id, 'note_id': note.id}, |
| 148 | + ) |
| 149 | + data = {'content': 'New content'} |
| 150 | + self.client.authenticate(user=self.user) |
| 151 | + resp = self.client.patch(url, data=data) |
| 152 | + |
| 153 | + self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code) |
| 154 | + |
| 155 | + def test_edit_note_public(self): |
| 156 | + """ |
| 157 | + Edit public patch note with an user that is a maintainer with |
| 158 | + an user that is not a maintainer. |
| 159 | + """ |
| 160 | + note = create_note(maintainer_only=False) |
| 161 | + |
| 162 | + url = reverse( |
| 163 | + 'api-patch-note-detail', |
| 164 | + kwargs={'patch_id': self.patch.id, 'note_id': note.id}, |
| 165 | + ) |
| 166 | + data = {'content': 'New content'} |
| 167 | + self.client.authenticate(user=self.user) |
| 168 | + resp = self.client.patch(url, data=data) |
| 169 | + |
| 170 | + self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code) |
| 171 | + |
| 172 | + def test_delete_note_as_maintainer(self): |
| 173 | + """Delete patch note with an user that is a maintainer.""" |
| 174 | + note = create_note(patch=self.patch, submitter=self.user) |
| 175 | + start_num = Note.objects.count() |
| 176 | + |
| 177 | + url = reverse( |
| 178 | + 'api-patch-note-detail', |
| 179 | + kwargs={'patch_id': self.patch.id, 'note_id': note.id}, |
| 180 | + ) |
| 181 | + |
| 182 | + self.client.authenticate(user=self.user) |
| 183 | + resp = self.client.delete(url) |
| 184 | + end_num = Note.objects.count() |
| 185 | + |
| 186 | + self.assertEqual(status.HTTP_204_NO_CONTENT, resp.status_code) |
| 187 | + self.assertEqual(start_num - 1, end_num) |
| 188 | + |
| 189 | + def test_delete_note_as_non_maintainer(self): |
| 190 | + """Delete patch note with an user that is not a maintainer.""" |
| 191 | + note = create_note() |
| 192 | + |
| 193 | + url = reverse( |
| 194 | + 'api-patch-note-detail', |
| 195 | + kwargs={'patch_id': self.patch.id, 'note_id': note.id}, |
| 196 | + ) |
| 197 | + |
| 198 | + self.client.authenticate(user=self.user) |
| 199 | + resp = self.client.delete(url) |
| 200 | + |
| 201 | + self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code) |
| 202 | + |
| 203 | + def test_delete_note_public(self): |
| 204 | + """ |
| 205 | + Delete public patch note with an user that is a maintainer with |
| 206 | + an user that is not a maintainer. |
| 207 | + """ |
| 208 | + person = create_person() |
| 209 | + note = create_note(patch=self.patch, maintainer_only=False) |
| 210 | + |
| 211 | + url = reverse( |
| 212 | + 'api-patch-note-detail', |
| 213 | + kwargs={'patch_id': self.patch.id, 'note_id': note.id}, |
| 214 | + ) |
| 215 | + self.client.authenticate(user=person.user) |
| 216 | + resp = self.client.delete(url) |
| 217 | + |
| 218 | + self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code) |
| 219 | + |
| 220 | + def test_notes_in_patch(self): |
| 221 | + url = reverse('api-patch-detail', kwargs={'pk': self.patch.id}) |
| 222 | + self.client.authenticate(user=self.user) |
| 223 | + resp = self.client.get(url) |
| 224 | + |
| 225 | + correct_path = reverse( |
| 226 | + 'api-patch-note-list', kwargs={'patch_id': self.patch.id} |
| 227 | + ) |
| 228 | + self.assertEqual( |
| 229 | + resp.data.get('notes'), |
| 230 | + f'http://example.com{correct_path}', |
| 231 | + ) |
0 commit comments