summaryrefslogtreecommitdiff
path: root/patchwork/tests/api/test_relation.py
blob: d48c62bc5087708940c706b416c2b84048ffaab0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# Patchwork - automated patch tracking system
# Copyright (C) 2020, IBM Corporation
#
# SPDX-License-Identifier: GPL-2.0-or-later

import unittest

from django.conf import settings
from django.urls import reverse

from patchwork.models import Patch
from patchwork.models import PatchRelation
from patchwork.tests.api import utils
from patchwork.tests.utils import create_maintainer
from patchwork.tests.utils import create_patch
from patchwork.tests.utils import create_patches
from patchwork.tests.utils import create_project
from patchwork.tests.utils import create_relation
from patchwork.tests.utils import create_user

if settings.ENABLE_REST_API:
    from rest_framework import status


@unittest.skipUnless(settings.ENABLE_REST_API, 'requires ENABLE_REST_API')
class TestRelationSimpleAPI(utils.APITestCase):
    @staticmethod
    def api_url(item=None, version=None):
        kwargs = {}
        if version:
            kwargs['version'] = version

        if item is None:
            return reverse('api-patch-list', kwargs=kwargs)
        kwargs['pk'] = item
        return reverse('api-patch-detail', kwargs=kwargs)

    def setUp(self):
        self.project = create_project()
        self.normal_user = create_user()
        self.maintainer = create_maintainer(self.project)

    def test_no_relation(self):
        patch = create_patch(project=self.project)
        resp = self.client.get(self.api_url(item=patch.pk))
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertEqual(resp.data['related'], [])

    @utils.store_samples('relation-list')
    def test_list_two_patch_relation(self):
        relation = create_relation(2, project=self.project)
        patches = relation.patches.all()

        # nobody
        resp = self.client.get(self.api_url(item=patches[0].pk))
        self.assertEqual(resp.status_code, status.HTTP_200_OK)

        self.assertIn('related', resp.data)
        self.assertEqual(len(resp.data['related']), 1)
        self.assertEqual(resp.data['related'][0]['id'], patches[1].pk)

        resp = self.client.get(self.api_url(item=patches[1].pk))
        self.assertEqual(resp.status_code, status.HTTP_200_OK)

        self.assertIn('related', resp.data)
        self.assertEqual(len(resp.data['related']), 1)
        self.assertEqual(resp.data['related'][0]['id'], patches[0].pk)

    @utils.store_samples('relation-create-forbidden')
    def test_create_two_patch_relation_nobody(self):
        patches = create_patches(2, project=self.project)

        resp = self.client.patch(
            self.api_url(item=patches[0].pk), {'related': [patches[1].pk]}
        )
        self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)

    def test_create_two_patch_relation_user(self):
        patches = create_patches(2, project=self.project)

        self.client.force_authenticate(user=self.normal_user)
        resp = self.client.patch(
            self.api_url(item=patches[0].pk), {'related': [patches[1].pk]}
        )
        self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)

    @utils.store_samples('relation-create-maintainer')
    def test_create_two_patch_relation_maintainer(self):
        patches = create_patches(2, project=self.project)

        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(
            self.api_url(item=patches[0].pk), {'related': [patches[1].pk]}
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)

        # reload and verify
        patches = Patch.objects.all()
        self.assertIsNotNone(patches[0].related)
        self.assertIsNotNone(patches[1].related)
        self.assertEqual(patches[1].related, patches[0].related)

    def test_delete_two_patch_relation_nobody(self):
        relation = create_relation(project=self.project)
        patch = relation.patches.all()[0]

        self.assertEqual(PatchRelation.objects.count(), 1)

        resp = self.client.patch(self.api_url(item=patch.pk), {'related': []})
        self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(PatchRelation.objects.count(), 1)

    @utils.store_samples('relation-delete')
    def test_delete_two_patch_relation_maintainer(self):
        relation = create_relation(project=self.project)
        patch = relation.patches.all()[0]

        self.assertEqual(PatchRelation.objects.count(), 1)

        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(self.api_url(item=patch.pk), {'related': []})
        self.assertEqual(resp.status_code, status.HTTP_200_OK)

        self.assertEqual(PatchRelation.objects.count(), 0)
        self.assertEqual(
            Patch.objects.filter(related__isnull=False).exists(), False
        )

    def test_create_three_patch_relation(self):
        patches = create_patches(3, project=self.project)

        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(
            self.api_url(item=patches[0].pk),
            {'related': [patches[1].pk, patches[2].pk]},
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)

        # reload and verify
        patches = Patch.objects.all()
        self.assertIsNotNone(patches[0].related)
        self.assertIsNotNone(patches[1].related)
        self.assertIsNotNone(patches[2].related)
        self.assertEqual(patches[0].related, patches[1].related)
        self.assertEqual(patches[1].related, patches[2].related)

    def test_delete_from_three_patch_relation(self):
        relation = create_relation(3, project=self.project)
        patch = relation.patches.all()[0]

        self.assertEqual(PatchRelation.objects.count(), 1)

        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(self.api_url(item=patch.pk), {'related': []})
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertIsNone(Patch.objects.get(id=patch.pk).related)
        self.assertEqual(PatchRelation.objects.count(), 1)
        self.assertEqual(PatchRelation.objects.first().patches.count(), 2)

    @utils.store_samples('relation-extend-through-new')
    def test_extend_relation_through_new(self):
        relation = create_relation(project=self.project)
        existing_patch_a = relation.patches.first()

        new_patch = create_patch(project=self.project)

        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(
            self.api_url(item=new_patch.pk), {'related': [existing_patch_a.pk]}
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertEqual(relation, Patch.objects.get(pk=new_patch.pk).related)
        self.assertEqual(relation.patches.count(), 3)

    def test_extend_relation_through_old(self):
        relation = create_relation(project=self.project)
        existing_patch_a = relation.patches.first()

        new_patch = create_patch(project=self.project)

        # maintainer
        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(
            self.api_url(item=existing_patch_a.pk), {'related': [new_patch.pk]}
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertEqual(relation, Patch.objects.get(id=new_patch.id).related)
        self.assertEqual(relation.patches.count(), 3)

    def test_extend_relation_through_new_two(self):
        relation = create_relation(project=self.project)
        existing_patch_a = relation.patches.first()

        new_patch_a = create_patch(project=self.project)
        new_patch_b = create_patch(project=self.project)

        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(
            self.api_url(item=new_patch_a.pk),
            {'related': [existing_patch_a.pk, new_patch_b.pk]},
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertEqual(
            relation, Patch.objects.get(id=new_patch_a.id).related
        )
        self.assertEqual(
            relation, Patch.objects.get(id=new_patch_b.id).related
        )
        self.assertEqual(relation.patches.count(), 4)

    @utils.store_samples('relation-extend-through-old')
    def test_extend_relation_through_old_two(self):
        relation = create_relation(project=self.project)
        existing_patch_a = relation.patches.first()

        new_patch_a = create_patch(project=self.project)
        new_patch_b = create_patch(project=self.project)

        # maintainer
        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(
            self.api_url(item=existing_patch_a.pk),
            {'related': [new_patch_a.pk, new_patch_b.pk]},
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertEqual(
            relation, Patch.objects.get(id=new_patch_a.id).related
        )
        self.assertEqual(
            relation, Patch.objects.get(id=new_patch_b.id).related
        )
        self.assertEqual(relation.patches.count(), 4)

    def test_remove_one_patch_from_relation_bad(self):
        relation = create_relation(3, project=self.project)
        keep_patch_a = relation.patches.all()[1]
        keep_patch_b = relation.patches.all()[2]

        # this should do nothing - it is interpreted as
        # _adding_ keep_patch_b again which is a no-op.

        # maintainer
        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(
            self.api_url(item=keep_patch_a.pk), {'related': [keep_patch_b.pk]}
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertEqual(relation.patches.count(), 3)

    def test_remove_one_patch_from_relation_good(self):
        relation = create_relation(3, project=self.project)
        target_patch = relation.patches.all()[0]

        # maintainer
        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(
            self.api_url(item=target_patch.pk), {'related': []}
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertIsNone(Patch.objects.get(id=target_patch.id).related)
        self.assertEqual(relation.patches.count(), 2)

    @utils.store_samples('relation-forbid-moving-between-relations')
    def test_forbid_moving_patch_between_relations(self):
        """Test the break-before-make logic"""
        relation_a = create_relation(project=self.project)
        relation_b = create_relation(project=self.project)

        patch_a = relation_a.patches.first()
        patch_b = relation_b.patches.first()

        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(
            self.api_url(item=patch_a.pk), {'related': [patch_b.pk]}
        )
        self.assertEqual(resp.status_code, status.HTTP_409_CONFLICT)

        resp = self.client.patch(
            self.api_url(item=patch_b.pk), {'related': [patch_a.pk]}
        )
        self.assertEqual(resp.status_code, status.HTTP_409_CONFLICT)

    def test_cross_project_different_maintainers(self):
        patch_a = create_patch(project=self.project)

        project_b = create_project()
        patch_b = create_patch(project=project_b)

        # maintainer a, patch in own project
        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(
            self.api_url(item=patch_a.pk), {'related': [patch_b.pk]}
        )
        self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)

        # maintainer a, patch in project b
        resp = self.client.patch(
            self.api_url(item=patch_b.pk), {'related': [patch_a.pk]}
        )
        self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)

    def test_cross_project_relation_super_maintainer(self):
        patch_a = create_patch(project=self.project)

        project_b = create_project()
        patch_b = create_patch(project=project_b)

        project_b.maintainer_project.add(self.maintainer.profile)
        project_b.save()

        self.client.force_authenticate(user=self.maintainer)
        resp = self.client.patch(
            self.api_url(item=patch_a.pk), {'related': [patch_b.pk]}
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertEqual(
            Patch.objects.get(id=patch_a.id).related,
            Patch.objects.get(id=patch_b.id).related,
        )