Skip to content

Commit a4603b4

Browse files
tests: validate patch attention_set api behavior
Signed-off-by: Victor Accarini <[email protected]>
1 parent 789f447 commit a4603b4

File tree

2 files changed

+245
-3
lines changed

2 files changed

+245
-3
lines changed

patchwork/tests/api/test_patch.py

+234-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
from django.urls import reverse
1212
from rest_framework import status
1313

14-
from patchwork.models import Patch
14+
from patchwork.models import Patch, PatchAttentionSet
1515
from patchwork.tests.api import utils
16-
from patchwork.tests.utils import create_maintainer
16+
from patchwork.tests.utils import create_attention_set, create_maintainer
1717
from patchwork.tests.utils import create_patch
1818
from patchwork.tests.utils import create_patches
1919
from patchwork.tests.utils import create_person
@@ -456,3 +456,235 @@ def test_delete(self):
456456
self.client.authenticate(user=user)
457457
resp = self.client.delete(self.api_url(patch.id))
458458
self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code)
459+
460+
def test_declare_review_intention(self):
461+
project = create_project()
462+
state = create_state()
463+
patch = create_patch(project=project, state=state)
464+
user = create_user()
465+
self.client.authenticate(user=user)
466+
467+
# No intention of reviewing
468+
self.assertEqual(
469+
len(
470+
PatchAttentionSet.objects.filter(patch=patch, user=user).all()
471+
),
472+
0,
473+
)
474+
475+
# declare intention
476+
resp = self.client.patch(
477+
self.api_url(patch.id),
478+
{'attention_set': [user.id]},
479+
)
480+
481+
self.assertEqual(resp.status_code, status.HTTP_200_OK)
482+
self.assertEqual(
483+
len(
484+
PatchAttentionSet.objects.filter(patch=patch, user=user).all()
485+
),
486+
1,
487+
)
488+
489+
# redeclare intention should have no effect
490+
resp = self.client.patch(
491+
self.api_url(patch.id),
492+
{'attention_set': [user.id]},
493+
)
494+
495+
self.assertEqual(resp.status_code, status.HTTP_200_OK)
496+
self.assertEqual(
497+
len(
498+
PatchAttentionSet.objects.filter(patch=patch, user=user).all()
499+
),
500+
1,
501+
)
502+
503+
def test_remove_review_intention(self):
504+
project = create_project()
505+
state = create_state()
506+
patch = create_patch(project=project, state=state)
507+
user = create_user()
508+
create_attention_set(patch=patch, user=user)
509+
self.client.authenticate(user=user)
510+
511+
# Existing intention of reviewing
512+
self.assertEqual(
513+
len(
514+
PatchAttentionSet.objects.filter(patch=patch, user=user).all()
515+
),
516+
1,
517+
)
518+
519+
# remove intention
520+
resp = self.client.patch(
521+
self.api_url(patch.id),
522+
{'attention_set': [-user.id]},
523+
)
524+
525+
self.assertEqual(resp.status_code, status.HTTP_200_OK)
526+
self.assertEqual(
527+
len(
528+
PatchAttentionSet.objects.filter(patch=patch, user=user).all()
529+
),
530+
0,
531+
)
532+
# uses soft delete
533+
self.assertEqual(
534+
len(
535+
PatchAttentionSet.raw_objects.filter(
536+
patch=patch, user=user
537+
).all()
538+
),
539+
1,
540+
)
541+
542+
def test_add_review_intention_updates_old_entry(self):
543+
project = create_project()
544+
state = create_state()
545+
patch = create_patch(project=project, state=state)
546+
user = create_user()
547+
interest = create_attention_set(patch=patch, user=user, removed=True)
548+
self.client.authenticate(user=user)
549+
550+
# Existing deleted intention of reviewing
551+
self.assertTrue(interest.removed)
552+
553+
# updates intention
554+
resp = self.client.patch(
555+
self.api_url(patch.id),
556+
{'attention_set': [user.id]},
557+
)
558+
559+
self.assertEqual(resp.status_code, status.HTTP_200_OK)
560+
self.assertEqual(
561+
len(
562+
PatchAttentionSet.objects.filter(patch=patch, user=user).all()
563+
),
564+
1,
565+
)
566+
# uses upsert
567+
self.assertEqual(
568+
len(
569+
PatchAttentionSet.raw_objects.filter(
570+
patch=patch, user=user
571+
).all()
572+
),
573+
1,
574+
)
575+
576+
def test_remove_review_intention_with_empty_array(self):
577+
project = create_project()
578+
state = create_state()
579+
patch = create_patch(project=project, state=state)
580+
user = create_user()
581+
create_attention_set(patch=patch, user=user)
582+
self.client.authenticate(user=user)
583+
584+
# Existing intention of reviewing
585+
self.assertEqual(
586+
len(
587+
PatchAttentionSet.objects.filter(patch=patch, user=user).all()
588+
),
589+
1,
590+
)
591+
592+
# remove intention
593+
resp = self.client.patch(
594+
self.api_url(patch.id),
595+
{'attention_set': []},
596+
)
597+
598+
self.assertEqual(resp.status_code, status.HTTP_200_OK)
599+
self.assertEqual(
600+
len(
601+
PatchAttentionSet.objects.filter(patch=patch, user=user).all()
602+
),
603+
0,
604+
)
605+
606+
def test_remove_review_intention_of_others(self):
607+
project = create_project()
608+
state = create_state()
609+
patch = create_patch(project=project, state=state)
610+
user = create_user()
611+
user2 = create_user()
612+
create_attention_set(patch=patch, user=user2)
613+
614+
self.client.authenticate(user=user)
615+
616+
# remove intention
617+
resp = self.client.patch(
618+
self.api_url(patch.id),
619+
{'attention_set': [-user2.id]},
620+
)
621+
622+
self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
623+
self.assertEqual(
624+
len(
625+
PatchAttentionSet.objects.filter(patch=patch, user=user2).all()
626+
),
627+
1,
628+
)
629+
630+
def test_remove_review_intention_of_others_as_maintainer(self):
631+
project = create_project()
632+
state = create_state()
633+
patch = create_patch(project=project, state=state)
634+
maintainer = create_maintainer(project)
635+
user2 = create_user()
636+
create_attention_set(patch=patch, user=user2)
637+
638+
self.client.authenticate(user=maintainer)
639+
640+
# remove intention
641+
resp = self.client.patch(
642+
self.api_url(patch.id),
643+
{'attention_set': [-user2.id]},
644+
)
645+
646+
self.assertEqual(resp.status_code, status.HTTP_200_OK)
647+
self.assertEqual(
648+
len(
649+
PatchAttentionSet.objects.filter(patch=patch, user=user2).all()
650+
),
651+
0,
652+
)
653+
654+
def test_declare_review_intention_of_others(self):
655+
project = create_project()
656+
state = create_state()
657+
patch = create_patch(project=project, state=state)
658+
user = create_user()
659+
maintainer = create_maintainer(project)
660+
user2 = create_user()
661+
self.client.authenticate(user=user)
662+
663+
# declare intention
664+
resp = self.client.patch(
665+
self.api_url(patch.id),
666+
{'attention_set': [user2.id]},
667+
)
668+
669+
self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
670+
self.assertEqual(
671+
len(
672+
PatchAttentionSet.objects.filter(patch=patch, user=user).all()
673+
),
674+
0,
675+
)
676+
677+
# maintaners also can't assign someone
678+
self.client.authenticate(user=maintainer)
679+
resp = self.client.patch(
680+
self.api_url(patch.id),
681+
{'attention_set': [user2.id]},
682+
)
683+
684+
self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
685+
self.assertEqual(
686+
len(
687+
PatchAttentionSet.objects.filter(patch=patch, user=user).all()
688+
),
689+
0,
690+
)

patchwork/tests/utils.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from django.contrib.auth.models import User
1212
from django.utils import timezone as tz_utils
1313

14-
from patchwork.models import Bundle
14+
from patchwork.models import Bundle, PatchAttentionSet
1515
from patchwork.models import Check
1616
from patchwork.models import Cover
1717
from patchwork.models import CoverComment
@@ -206,6 +206,16 @@ def create_patch(**kwargs):
206206
return patch
207207

208208

209+
def create_attention_set(**kwargs):
210+
values = {
211+
'patch': create_patch() if 'patch' not in kwargs else None,
212+
'user': create_person() if 'user' not in kwargs else None,
213+
}
214+
values.update(kwargs)
215+
216+
return PatchAttentionSet.objects.create(**values)
217+
218+
209219
def create_cover(**kwargs):
210220
"""Create 'Cover' object."""
211221
num = Cover.objects.count()

0 commit comments

Comments
 (0)