summaryrefslogtreecommitdiff
path: root/patchwork/tests/test_bundles.py
blob: 6a744093df8e96f800d78d0925361508fe90a149 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# Patchwork - automated patch tracking system
# Copyright (C) 2009 Jeremy Kerr <jk@ozlabs.org>
#
# SPDX-License-Identifier: GPL-2.0-or-later

import base64
import datetime
import unittest

from django.conf import settings
from django.test import TestCase
from django.urls import reverse
from django.utils.html import escape
from django.utils.http import urlencode

from patchwork.models import Bundle
from patchwork.models import BundlePatch
from patchwork.tests.utils import create_bundle
from patchwork.tests.utils import create_patches
from patchwork.tests.utils import create_project
from patchwork.tests.utils import create_user
from patchwork.views import utils as view_utils


def bundle_url(bundle):
    return reverse('bundle-detail', kwargs={
        'username': bundle.owner.username, 'bundlename': bundle.name})


def bundle_mbox_url(bundle):
    return reverse('bundle-mbox', kwargs={
        'username': bundle.owner.username, 'bundlename': bundle.name})


class BundleListTest(TestCase):

    def setUp(self):
        self.user = create_user()
        self.client.login(username=self.user.username,
                          password=self.user.username)

    def test_no_bundles(self):
        response = self.client.get(reverse('user-bundles'))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context['bundles']), 0)

    def test_single_bundle(self):
        bundle = create_bundle(owner=self.user)
        bundle.save()
        response = self.client.get(reverse('user-bundles'))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context['bundles']), 1)


class BundleTestBase(TestCase):

    def setUp(self, count=3):
        self.user = create_user()
        self.client.login(username=self.user.username,
                          password=self.user.username)
        self.bundle = create_bundle(owner=self.user)
        self.project = create_project()
        self.patches = create_patches(count, project=self.project)


class BundleViewTest(BundleTestBase):

    def test_empty_bundle(self):
        response = self.client.get(bundle_url(self.bundle))
        self.assertEqual(response.status_code, 200)
        page = response.context['page']
        self.assertEqual(len(page.object_list), 0)

    def test_non_empty_bundle(self):
        self.bundle.append_patch(self.patches[0])

        response = self.client.get(bundle_url(self.bundle))
        self.assertEqual(response.status_code, 200)
        page = response.context['page']
        self.assertEqual(len(page.object_list), 1)

    def test_bundle_order(self):
        for patch in self.patches:
            self.bundle.append_patch(patch)

        response = self.client.get(bundle_url(self.bundle))

        pos = 0
        for patch in self.patches:
            next_pos = response.content.decode().find(patch.name)
            # ensure that this patch is after the previous
            self.assertTrue(next_pos > pos)
            pos = next_pos

        # reorder and recheck
        i = 0
        for patch in self.patches.__reversed__():
            bundlepatch = BundlePatch.objects.get(bundle=self.bundle,
                                                  patch=patch)
            bundlepatch.order = i
            bundlepatch.save()
            i += 1

        response = self.client.get(bundle_url(self.bundle))
        pos = len(response.content)
        for patch in self.patches:
            next_pos = response.content.decode().find(patch.name)
            # ensure that this patch is now *before* the previous
            self.assertTrue(next_pos < pos)
            pos = next_pos


class BundleMboxTest(BundleTestBase):

    def test_empty_bundle(self):
        response = self.client.get(bundle_mbox_url(self.bundle))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'')

    def test_non_empty_bundle(self):
        self.bundle.append_patch(self.patches[0])

        response = self.client.get(bundle_mbox_url(self.bundle))
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.content, b'')


class BundleUpdateTest(BundleTestBase):

    def test_no_action(self):
        newname = 'newbundlename'
        data = {
            'form': 'bundle',
            'name': newname,
            'public': 'on',
        }
        response = self.client.post(bundle_url(self.bundle), data)
        self.assertEqual(response.status_code, 200)

        bundle = Bundle.objects.get(pk=self.bundle.pk)
        self.assertEqual(bundle.name, self.bundle.name)
        self.assertEqual(bundle.public, self.bundle.public)

    def test_update_name(self):
        newname = 'newbundlename'
        data = {
            'form': 'bundle',
            'action': 'update',
            'name': newname,
            'public': '',
        }
        response = self.client.post(bundle_url(self.bundle), data)
        bundle = Bundle.objects.get(pk=self.bundle.pk)
        self.assertRedirects(response, bundle_url(bundle))
        self.assertEqual(bundle.name, newname)
        self.assertEqual(bundle.public, self.bundle.public)

    def test_update_public(self):
        data = {
            'form': 'bundle',
            'action': 'update',
            'name': self.bundle.name,
            'public': 'on',
        }
        response = self.client.post(bundle_url(self.bundle), data)
        self.assertEqual(response.status_code, 200)
        bundle = Bundle.objects.get(pk=self.bundle.pk)
        self.assertEqual(bundle.name, self.bundle.name)
        self.assertEqual(bundle.public, not self.bundle.public)

        # check other forms for errors
        formname = 'patchform'
        if formname not in response.context:
            return
        form = response.context[formname]
        if not form:
            return
        self.assertEqual(form.errors, {})


class BundleMaintainerUpdateTest(BundleUpdateTest):

    def setUp(self):
        super(BundleMaintainerUpdateTest, self).setUp()

        profile = self.user.profile
        profile.maintainer_projects.add(self.project)
        profile.save()


class BundlePublicViewTest(BundleTestBase):

    def setUp(self):
        super(BundlePublicViewTest, self).setUp()
        self.client.logout()
        self.bundle.append_patch(self.patches[0])
        self.url = bundle_url(self.bundle)

    def test_public_bundle(self):
        self.bundle.public = True
        self.bundle.save()
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, self.patches[0].name)

    def test_private_bundle(self):
        self.bundle.public = False
        self.bundle.save()
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 404)


class BundlePublicViewMboxTest(BundlePublicViewTest):

    def setUp(self):
        super(BundlePublicViewMboxTest, self).setUp()
        self.url = reverse('bundle-mbox', kwargs={
            'username': self.bundle.owner.username,
            'bundlename': self.bundle.name})


class BundlePublicModifyTest(BundleTestBase):

    """Ensure that non-owners can't modify bundles"""

    def setUp(self):
        super(BundlePublicModifyTest, self).setUp()
        self.bundle.public = True
        self.bundle.save()
        self.other_user = create_user()

    def test_bundle_form_presence(self):
        """Check for presence of the modify form on the bundle"""
        self.client.login(username=self.other_user.username,
                          password=self.other_user.username)
        response = self.client.get(bundle_url(self.bundle))
        self.assertNotContains(response, 'name="form" value="bundle"')
        self.assertNotContains(response, 'Change order')

    def test_bundle_form_submission(self):
        oldname = 'oldbundlename'
        newname = 'newbundlename'
        data = {
            'form': 'bundle',
            'action': 'update',
            'name': newname,
        }
        self.bundle.name = oldname
        self.bundle.save()

        # first, check that we can modify with the owner
        self.client.login(username=self.user.username,
                          password=self.user.username)
        self.client.post(bundle_url(self.bundle), data)
        self.bundle = Bundle.objects.get(pk=self.bundle.pk)
        self.assertEqual(self.bundle.name, newname)

        # reset bundle name
        self.bundle.name = oldname
        self.bundle.save()

        # log in with a different user, and check that we can no longer modify
        self.client.login(username=self.other_user.username,
                          password=self.other_user.username)
        self.client.post(bundle_url(self.bundle), data)
        self.bundle = Bundle.objects.get(pk=self.bundle.pk)
        self.assertNotEqual(self.bundle.name, newname)


class BundlePrivateViewTest(BundleTestBase):

    """Ensure that non-owners can't view private bundles"""

    def setUp(self):
        super(BundlePrivateViewTest, self).setUp()
        self.bundle.public = False
        self.bundle.save()
        self.bundle.append_patch(self.patches[0])
        self.url = bundle_url(self.bundle)
        self.other_user = create_user()

    def test_private_bundle(self):
        # Check we can view as owner
        self.client.login(username=self.user.username,
                          password=self.user.username)
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, self.patches[0].name)

        # Check we can't view as another user
        self.client.login(username=self.other_user.username,
                          password=self.other_user.username)
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 404)


@unittest.skipUnless(settings.ENABLE_REST_API, 'requires ENABLE_REST_API')
class BundlePrivateViewMboxTest(BundlePrivateViewTest):

    """Ensure that non-owners can't view private bundle mboxes"""

    def setUp(self):
        super(BundlePrivateViewMboxTest, self).setUp()
        self.url = reverse('bundle-mbox', kwargs={
            'username': self.bundle.owner.username,
            'bundlename': self.bundle.name})

    def test_private_bundle_mbox_basic_auth(self):
        self.client.logout()

        def _get_auth_string(user):
            return 'Basic ' + base64.b64encode(b':'.join((
                user.username.encode(),
                user.username.encode()))
            ).strip().decode()

        # Check we can view as owner
        auth_string = _get_auth_string(self.user)
        response = self.client.get(self.url, HTTP_AUTHORIZATION=auth_string)

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, self.patches[0].name)

        # Check we can't view as another user
        auth_string = _get_auth_string(self.other_user)
        response = self.client.get(self.url, HTTP_AUTHORIZATION=auth_string)
        self.assertEqual(response.status_code, 404)

    def test_private_bundle_mbox_token_auth(self):
        self.client.logout()

        # create tokens for both users
        for user in [self.user, self.other_user]:
            view_utils.regenerate_token(user)

        def _get_auth_string(user):
            return 'Token {}'.format(str(user.profile.token))

        # Check we can view as owner
        auth_string = _get_auth_string(self.user)
        response = self.client.get(self.url, HTTP_AUTHORIZATION=auth_string)

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, self.patches[0].name)

        # Check we can't view as another user
        auth_string = _get_auth_string(self.other_user)
        response = self.client.get(self.url, HTTP_AUTHORIZATION=auth_string)
        self.assertEqual(response.status_code, 404)


class BundleCreateFromListTest(BundleTestBase):

    def test_create_empty_bundle(self):
        newbundlename = 'testbundle-new'
        params = {'form': 'patchlistform',
                  'bundle_name': newbundlename,
                  'action': 'Create',
                  'project': self.project.id}

        response = self.client.post(
            reverse('patch-list', kwargs={
                'project_id': self.project.linkname}),
            params)

        self.assertContains(response, 'Bundle %s created' % newbundlename)

    def test_create_non_empty_bundle(self):
        newbundlename = 'testbundle-new'
        patch = self.patches[0]

        params = {'form': 'patchlistform',
                  'bundle_name': newbundlename,
                  'action': 'Create',
                  'project': self.project.id,
                  'patch_id:%d' % patch.id: 'checked'}

        response = self.client.post(
            reverse('patch-list', kwargs={
                'project_id': self.project.linkname}),
            params)

        self.assertContains(response, 'Bundle %s created' % newbundlename)
        self.assertContains(response, 'added to bundle %s' % newbundlename,
                            count=1)

        bundle = Bundle.objects.get(name=newbundlename)
        self.assertEqual(bundle.patches.count(), 1)
        self.assertEqual(bundle.patches.all()[0], patch)

    def test_create_non_empty_bundle_empty_name(self):
        patch = self.patches[0]

        n_bundles = Bundle.objects.count()

        params = {'form': 'patchlistform',
                  'bundle_name': '',
                  'action': 'Create',
                  'project': self.project.id,
                  'patch_id:%d' % patch.id: 'checked'}

        response = self.client.post(
            reverse('patch-list', kwargs={
                'project_id': self.project.linkname}),
            params)

        self.assertContains(response, 'No bundle name was specified',
                            status_code=200)

        # test that no new bundles are present
        self.assertEqual(n_bundles, Bundle.objects.count())

    def test_create_duplicate_name(self):
        newbundlename = 'testbundle-dup'
        patch = self.patches[0]

        params = {'form': 'patchlistform',
                  'bundle_name': newbundlename,
                  'action': 'Create',
                  'project': self.project.id,
                  'patch_id:%d' % patch.id: 'checked'}

        response = self.client.post(
            reverse('patch-list', kwargs={
                'project_id': self.project.linkname}),
            params)

        n_bundles = Bundle.objects.count()
        self.assertContains(response, 'Bundle %s created' % newbundlename)
        self.assertContains(response, 'added to bundle %s' % newbundlename,
                            count=1)

        bundle = Bundle.objects.get(name=newbundlename)
        self.assertEqual(bundle.patches.count(), 1)
        self.assertEqual(bundle.patches.all()[0], patch)

        response = self.client.post(
            reverse('patch-list', kwargs={
                'project_id': self.project.linkname}),
            params)

        self.assertNotContains(response, 'Bundle %s created' % newbundlename)
        self.assertContains(response, 'You already have a bundle called')
        self.assertEqual(Bundle.objects.count(), n_bundles)
        self.assertEqual(bundle.patches.count(), 1)


class BundleCreateFromPatchTest(BundleTestBase):

    def test_create_non_empty_bundle(self):
        newbundlename = 'testbundle-new'
        patch = self.patches[0]

        params = {'name': newbundlename,
                  'action': 'createbundle'}

        response = self.client.post(
            reverse('patch-detail',
                    kwargs={'project_id': patch.project.linkname,
                            'msgid': patch.url_msgid}), params)

        self.assertContains(response,
                            'Bundle %s created' % newbundlename)

        bundle = Bundle.objects.get(name=newbundlename)
        self.assertEqual(bundle.patches.count(), 1)
        self.assertEqual(bundle.patches.all()[0], patch)

    def test_create_with_existing_name(self):
        newbundlename = self.bundle.name
        patch = self.patches[0]

        params = {'name': newbundlename,
                  'action': 'createbundle'}

        response = self.client.post(
            reverse('patch-detail',
                    kwargs={'project_id': patch.project.linkname,
                            'msgid': patch.url_msgid}), params)

        self.assertContains(
            response,
            'A bundle called %s already exists' % newbundlename)

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


class BundleAddFromListTest(BundleTestBase):

    def test_add_to_empty_bundle(self):
        patch = self.patches[0]
        params = {'form': 'patchlistform',
                  'action': 'Add',
                  'project': self.project.id,
                  'bundle_id': self.bundle.id,
                  'patch_id:%d' % patch.id: 'checked'}

        response = self.client.post(
            reverse('patch-list', kwargs={
                'project_id': self.project.linkname}),
            params)

        self.assertContains(response, 'added to bundle %s' % self.bundle.name,
                            count=1)

        self.assertEqual(self.bundle.patches.count(), 1)
        self.assertEqual(self.bundle.patches.all()[0], patch)

    def test_add_to_non_empty_bundle(self):
        self.bundle.append_patch(self.patches[0])
        patch = self.patches[1]
        params = {'form': 'patchlistform',
                  'action': 'Add',
                  'project': self.project.id,
                  'bundle_id': self.bundle.id,
                  'patch_id:%d' % patch.id: 'checked'}

        response = self.client.post(
            reverse('patch-list', kwargs={
                'project_id': self.project.linkname}),
            params)

        self.assertContains(response, 'added to bundle %s' % self.bundle.name,
                            count=1)

        self.assertEqual(self.bundle.patches.count(), 2)
        self.assertIn(self.patches[0], self.bundle.patches.all())
        self.assertIn(self.patches[1], self.bundle.patches.all())

        # check order
        bps = [BundlePatch.objects.get(bundle=self.bundle,
                                       patch=self.patches[i])
               for i in [0, 1]]
        self.assertTrue(bps[0].order < bps[1].order)

    def test_add_duplicate(self):
        self.bundle.append_patch(self.patches[0])
        count = self.bundle.patches.count()
        patch = self.patches[0]

        params = {'form': 'patchlistform',
                  'action': 'Add',
                  'project': self.project.id,
                  'bundle_id': self.bundle.id,
                  'patch_id:%d' % patch.id: 'checked'}

        response = self.client.post(
            reverse('patch-list', kwargs={
                'project_id': self.project.linkname}),
            params)

        expected = escape(f"Patch '{patch.name}' already in bundle")
        self.assertContains(response, expected, count=1, status_code=200)

        self.assertEqual(count, self.bundle.patches.count())

    def test_add_new_and_duplicate(self):
        self.bundle.append_patch(self.patches[0])
        count = self.bundle.patches.count()
        patch = self.patches[0]

        params = {'form': 'patchlistform',
                  'action': 'Add',
                  'project': self.project.id,
                  'bundle_id': self.bundle.id,
                  'patch_id:%d' % patch.id: 'checked',
                  'patch_id:%d' % self.patches[1].id: 'checked'}

        response = self.client.post(
            reverse('patch-list', kwargs={
                'project_id': self.project.linkname}),
            params)

        for expected in (
            escape(f"Patch '{patch.name}' already in bundle"),
            escape(f"Patch '{self.patches[1].name}' added to bundle"),
        ):
            self.assertContains(response, expected, count=1, status_code=200)

        self.assertEqual(count + 1, self.bundle.patches.count())


class BundleAddFromPatchTest(BundleTestBase):

    def test_add_to_empty_bundle(self):
        patch = self.patches[0]
        params = {'action': 'addtobundle',
                  'bundle_id': self.bundle.id}

        response = self.client.post(
            reverse('patch-detail',
                    kwargs={'project_id': patch.project.linkname,
                            'msgid': patch.url_msgid}), params)

        self.assertContains(
            response,
            'added to bundle &quot;%s&quot;' % self.bundle.name,
            count=1)

        self.assertEqual(self.bundle.patches.count(), 1)
        self.assertEqual(self.bundle.patches.all()[0], patch)

    def test_add_to_non_empty_bundle(self):
        self.bundle.append_patch(self.patches[0])
        patch = self.patches[1]
        params = {'action': 'addtobundle',
                  'bundle_id': self.bundle.id}

        response = self.client.post(
            reverse('patch-detail',
                    kwargs={'project_id': patch.project.linkname,
                            'msgid': patch.url_msgid}), params)

        self.assertContains(
            response,
            'added to bundle &quot;%s&quot;' % self.bundle.name,
            count=1)

        self.assertEqual(self.bundle.patches.count(), 2)
        self.assertIn(self.patches[0], self.bundle.patches.all())
        self.assertIn(self.patches[1], self.bundle.patches.all())

        # check order
        bps = [BundlePatch.objects.get(bundle=self.bundle,
                                       patch=self.patches[i])
               for i in [0, 1]]
        self.assertTrue(bps[0].order < bps[1].order)


class BundleInitialOrderTest(BundleTestBase):

    """When creating bundles from a patch list, ensure that the patches in the
       bundle are ordered by date"""

    def setUp(self):
        super(BundleInitialOrderTest, self).setUp(5)

        # put patches in an arbitrary order
        idxs = [2, 4, 3, 1, 0]
        self.patches = [self.patches[i] for i in idxs]

        # set dates to be sequential
        last_patch = self.patches[0]
        for patch in self.patches[1:]:
            patch.date = last_patch.date + datetime.timedelta(0, 1)
            patch.save()
            last_patch = patch

    def _test_order(self, ids, expected_order):
        newbundlename = 'testbundle-new'

        # need to define our querystring explicity to enforce ordering
        params = {'form': 'patchlistform',
                  'bundle_name': newbundlename,
                  'action': 'Create',
                  'project': self.project.id,
                  }

        data = urlencode(params) + \
            ''.join(['&patch_id:%d=checked' % i for i in ids])

        response = self.client.post(
            reverse('patch-list', kwargs={
                'project_id': self.project.linkname}),
            data=data,
            content_type='application/x-www-form-urlencoded',
        )

        self.assertContains(response, 'Bundle %s created' % newbundlename)
        self.assertContains(response, 'added to bundle %s' % newbundlename,
                            count=5)

        bundle = Bundle.objects.get(name=newbundlename)

        # BundlePatches should be sorted by .order by default
        bps = BundlePatch.objects.filter(bundle=bundle)

        for (bp, p) in zip(bps, expected_order):
            self.assertEqual(bp.patch.pk, p.pk)

        bundle.delete()

    def test_bundle_forward_order(self):
        ids = [p.id for p in self.patches]
        self._test_order(ids, self.patches)

    def test_bundle_reverse_order(self):
        ids = [p.id for p in self.patches]
        ids.reverse()
        self._test_order(ids, self.patches)


class BundleReorderTest(BundleTestBase):

    def setUp(self):
        super(BundleReorderTest, self).setUp(5)
        for i in range(5):
            self.bundle.append_patch(self.patches[i])

    def check_reordering(self, neworder, start, end):
        neworder_ids = [self.patches[i].id for i in neworder]

        firstpatch = BundlePatch.objects.get(bundle=self.bundle,
                                             patch=self.patches[start]).patch

        slice_ids = neworder_ids[start:end]
        params = {'form': 'reorderform',
                  'order_start': firstpatch.id,
                  'neworder': slice_ids}

        response = self.client.post(bundle_url(self.bundle), params)

        self.assertEqual(response.status_code, 200)

        bps = BundlePatch.objects.filter(bundle=self.bundle).order_by('order')

        # check if patch IDs are in the expected order:
        bundle_ids = [bp.patch.id for bp in bps]
        self.assertEqual(neworder_ids, bundle_ids)

        # check if order field is still sequential:
        order_numbers = [bp.order for bp in bps]
        # [1 ... len(neworder)]
        expected_order = list(range(1, len(neworder) + 1))
        self.assertEqual(order_numbers, expected_order)

    def test_bundle_reorder_all(self):
        """Reorder all patches."""
        self.check_reordering([2, 1, 4, 0, 3], 0, 5)

    def test_bundle_reorder_end(self):
        """Reorder only the last three patches."""
        self.check_reordering([0, 1, 3, 2, 4], 2, 5)

    def test_bundle_reorder_begin(self):
        """Reorder only the first three patches."""
        self.check_reordering([2, 0, 1, 3, 4], 0, 3)

    def test_bundle_reorder_middle(self):
        """Reorder only 2nd, 3rd, and 4th patches."""
        self.check_reordering([0, 2, 3, 1, 4], 1, 4)


@unittest.skipUnless(settings.COMPAT_REDIR,
                     'requires compat redirection (use the COMPAT_REDIR '
                     'setting)')
class BundleRedirTest(BundleTestBase):
    """Validate redirection of legacy URLs."""

    def setUp(self):
        super(BundleRedirTest, self).setUp()

    def test_bundle_redir(self):
        response = self.client.get(
            reverse('bundle-redir', kwargs={'bundle_id': self.bundle.id}))
        self.assertRedirects(response, bundle_url(self.bundle))

    def test_mbox_redir(self):
        response = self.client.get(reverse(
            'bundle-mbox-redir', kwargs={'bundle_id': self.bundle.id}))
        self.assertRedirects(response, reverse('bundle-mbox', kwargs={
            'username': self.bundle.owner.username,
            'bundlename': self.bundle.name}))