~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/blueprints/model/tests/test_subscription.py

[r=benji][bug=50875] Allow teams to be unsubscribed from blueprints
        and fix (modernise) form infrastructure.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
__metaclass__ = type
 
5
 
 
6
from canonical.testing.layers import DatabaseFunctionalLayer
 
7
from lp.app.errors import UserCannotUnsubscribePerson
 
8
from lp.testing import (
 
9
    person_logged_in,
 
10
    TestCaseWithFactory,
 
11
    )
 
12
 
 
13
 
 
14
class TestSpecificationSubscription(TestCaseWithFactory):
 
15
    """ Test whether a user can unsubscribe someone
 
16
 
 
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.
 
20
    """
 
21
 
 
22
    layer = DatabaseFunctionalLayer
 
23
 
 
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
 
30
 
 
31
    def test_can_unsubscribe_self(self):
 
32
        # The user can of course unsubscribe himself, even if someone else
 
33
        # subscribed him.
 
34
        (spec, subscriber,
 
35
            subscribed_by, subscription) = self._make_subscription()
 
36
        self.assertTrue(subscription.canBeUnsubscribedByUser(subscriber))
 
37
 
 
38
    def test_subscriber_cannot_unsubscribe_user(self):
 
39
        # The one who subscribed the subscriber doesn't have permission to
 
40
        # unsubscribe him.
 
41
        (spec, subscriber,
 
42
            subscribed_by, subscription) = self._make_subscription()
 
43
        self.assertFalse(subscription.canBeUnsubscribedByUser(subscribed_by))
 
44
 
 
45
    def test_anonymous_cannot_unsubscribe(self):
 
46
        # The anonymous user (represented by None) can't unsubscribe anyone.
 
47
        (spec, subscriber,
 
48
            subscribed_by, subscription) = self._make_subscription()
 
49
        self.assertFalse(subscription.canBeUnsubscribedByUser(None))
 
50
 
 
51
    def test_can_unsubscribe_team(self):
 
52
        # A user can unsubscribe a team he's a member of.
 
53
        (spec, subscriber,
 
54
            subscribed_by, subscription) = self._make_subscription()
 
55
        team = self.factory.makeTeam()
 
56
        member = self.factory.makePerson()
 
57
        with person_logged_in(member):
 
58
            member.join(team)
 
59
            subscription = spec.subscribe(team, subscribed_by)
 
60
        self.assertTrue(subscription.canBeUnsubscribedByUser(member))
 
61
 
 
62
        non_member = self.factory.makePerson()
 
63
        self.assertFalse(subscription.canBeUnsubscribedByUser(non_member))
 
64
 
 
65
    def test_cannot_unsubscribe_team(self):
 
66
        # A user cannot unsubscribe a team he's a not member of.
 
67
        (spec, subscriber,
 
68
            subscribed_by, subscription) = self._make_subscription()
 
69
        team = self.factory.makeTeam()
 
70
        member = self.factory.makePerson()
 
71
        with person_logged_in(member):
 
72
            member.join(team)
 
73
            subscription = spec.subscribe(team, subscribed_by)
 
74
        non_member = self.factory.makePerson()
 
75
        self.assertFalse(subscription.canBeUnsubscribedByUser(non_member))
 
76
 
 
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
 
81
        # permission.
 
82
        (spec, subscriber,
 
83
            subscribed_by, subscription) = self._make_subscription()
 
84
        person = self.factory.makePerson()
 
85
        self.assertRaises(
 
86
            UserCannotUnsubscribePerson, spec.unsubscribe, subscriber, person)