1
# Copyright 2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
6
from canonical.testing.layers import DatabaseFunctionalLayer
7
from lp.app.errors import UserCannotUnsubscribePerson
8
from lp.testing import (
14
class TestSpecificationSubscription(TestCaseWithFactory):
15
""" Test whether a user can unsubscribe someone
17
As user can't unsubscribe just anyone from a spec. To check whether
18
someone can be unusubscribed, the canBeUnsubscribedByUser() method on
19
the SpecificationSubscription object is used.
22
layer = DatabaseFunctionalLayer
24
def _make_subscription(self):
25
spec = self.factory.makeSpecification()
26
subscriber = self.factory.makePerson()
27
subscribed_by = self.factory.makePerson()
28
subscription = spec.subscribe(subscriber, subscribed_by)
29
return spec, subscriber, subscribed_by, subscription
31
def test_can_unsubscribe_self(self):
32
# The user can of course unsubscribe himself, even if someone else
35
subscribed_by, subscription) = self._make_subscription()
36
self.assertTrue(subscription.canBeUnsubscribedByUser(subscriber))
38
def test_subscriber_cannot_unsubscribe_user(self):
39
# The one who subscribed the subscriber doesn't have permission to
42
subscribed_by, subscription) = self._make_subscription()
43
self.assertFalse(subscription.canBeUnsubscribedByUser(subscribed_by))
45
def test_anonymous_cannot_unsubscribe(self):
46
# The anonymous user (represented by None) can't unsubscribe anyone.
48
subscribed_by, subscription) = self._make_subscription()
49
self.assertFalse(subscription.canBeUnsubscribedByUser(None))
51
def test_can_unsubscribe_team(self):
52
# A user can unsubscribe a team he's a member of.
54
subscribed_by, subscription) = self._make_subscription()
55
team = self.factory.makeTeam()
56
member = self.factory.makePerson()
57
with person_logged_in(member):
59
subscription = spec.subscribe(team, subscribed_by)
60
self.assertTrue(subscription.canBeUnsubscribedByUser(member))
62
non_member = self.factory.makePerson()
63
self.assertFalse(subscription.canBeUnsubscribedByUser(non_member))
65
def test_cannot_unsubscribe_team(self):
66
# A user cannot unsubscribe a team he's a not member of.
68
subscribed_by, subscription) = self._make_subscription()
69
team = self.factory.makeTeam()
70
member = self.factory.makePerson()
71
with person_logged_in(member):
73
subscription = spec.subscribe(team, subscribed_by)
74
non_member = self.factory.makePerson()
75
self.assertFalse(subscription.canBeUnsubscribedByUser(non_member))
77
def test_unallowed_unsubscribe_raises(self):
78
# A spec's unsubscribe method uses canBeUnsubscribedByUser to check
79
# that the unsubscribing user has the appropriate permissions.
80
# unsubscribe will raise an exception if the user does not have
83
subscribed_by, subscription) = self._make_subscription()
84
person = self.factory.makePerson()
86
UserCannotUnsubscribePerson, spec.unsubscribe, subscriber, person)