~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/answers/model/tests/test_questionsubscription.py

[r=wallyworld][bug=816261] Move bugs subscriptions_list to apps and
        add IQuestion.canBeUnsubscribedBy

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
"""Tests for the QuestionSubscrption model object.."""
 
5
 
 
6
__metaclass__ = type
 
7
 
 
8
from zope.component import getUtility
 
9
 
 
10
from canonical.testing.layers import DatabaseFunctionalLayer
 
11
from lp.app.errors import UserCannotUnsubscribePerson
 
12
from lp.services.worlddata.interfaces.language import ILanguageSet
 
13
from lp.testing import (
 
14
    person_logged_in,
 
15
    TestCaseWithFactory,
 
16
    )
 
17
 
 
18
 
 
19
class TestQuestionSubscription(TestCaseWithFactory):
 
20
    """Tests relating to question subscriptions in general."""
 
21
 
 
22
    layer = DatabaseFunctionalLayer
 
23
 
 
24
    def test_owner_subscribed(self):
 
25
        # The owner of a question is subscribed to the question.
 
26
        question = self.factory.makeQuestion()
 
27
        [subscription] = list(question.subscriptions)
 
28
        self.assertEqual(question.owner, subscription.person)
 
29
 
 
30
    def test_subscribed_by_set(self):
 
31
        """The user subscribing is recorded along the subscriber."""
 
32
        subscriber = self.factory.makePerson()
 
33
        question = self.factory.makeQuestion()
 
34
        with person_logged_in(subscriber):
 
35
            subscription = question.subscribe(subscriber)
 
36
        self.assertEqual(subscriber, subscription.person)
 
37
 
 
38
    def test_unsubscribe(self):
 
39
        """Test unsubscribing by the subscriber."""
 
40
        subscription = self.factory.makeQuestionSubscription()
 
41
        subscriber = subscription.person
 
42
        question = subscription.question
 
43
        with person_logged_in(subscriber):
 
44
            question.unsubscribe(subscriber, subscriber)
 
45
        self.assertFalse(question.isSubscribed(subscriber))
 
46
 
 
47
    def test_unsubscribe_by_unauthorized(self):
 
48
        """Test unsubscribing someone you shouldn't be able to."""
 
49
        subscription = self.factory.makeQuestionSubscription()
 
50
        question = subscription.question
 
51
        unsubscriber = self.factory.makePerson()
 
52
        with person_logged_in(unsubscriber):
 
53
            self.assertRaises(
 
54
                UserCannotUnsubscribePerson,
 
55
                question.unsubscribe,
 
56
                subscription.person,
 
57
                unsubscriber)
 
58
 
 
59
 
 
60
class TestQuestionSubscriptionCanBeUnsubscribedbyUser(TestCaseWithFactory):
 
61
    """Tests for QuestionSubscription.canBeUnsubscribedByUser."""
 
62
 
 
63
    layer = DatabaseFunctionalLayer
 
64
 
 
65
    def test_none(self):
 
66
        """None for a user always returns False."""
 
67
        subscription = self.factory.makeQuestionSubscription()
 
68
        self.assertFalse(subscription.canBeUnsubscribedByUser(None))
 
69
 
 
70
    def test_self_subscriber(self):
 
71
        """The subscriber has permission to unsubscribe."""
 
72
        subscription = self.factory.makeQuestionSubscription()
 
73
        self.assertTrue(
 
74
            subscription.canBeUnsubscribedByUser(subscription.person))
 
75
 
 
76
    def test_non_subscriber_fails(self):
 
77
        """An unrelated person can't unsubscribe a user."""
 
78
        subscription = self.factory.makeQuestionSubscription()
 
79
        editor = self.factory.makePerson()
 
80
        self.assertFalse(subscription.canBeUnsubscribedByUser(editor))
 
81
 
 
82
    def test_team_member_can_unsubscribe(self):
 
83
        """Any team member can unsubscribe the team from a question."""
 
84
        team = self.factory.makeTeam()
 
85
        member = self.factory.makePerson()
 
86
        with person_logged_in(team.teamowner):
 
87
            team.addMember(member, team.teamowner)
 
88
        subscription = self.factory.makeQuestionSubscription(person=team)
 
89
        self.assertTrue(subscription.canBeUnsubscribedByUser(member))
 
90
 
 
91
    def test_question_person_owner_can_unsubscribe(self):
 
92
        """Question owner can unsubscribe someone from a question."""
 
93
        question_owner = self.factory.makePerson()
 
94
        question = self.factory.makeQuestion(owner=question_owner)
 
95
        subscriber = self.factory.makePerson()
 
96
        subscription = self.factory.makeQuestionSubscription(
 
97
            question=question, person=subscriber)
 
98
        self.assertTrue(subscription.canBeUnsubscribedByUser(question_owner))
 
99
 
 
100
    def test_question_team_owner_can_unsubscribe(self):
 
101
        """Question team owner can unsubscribe someone from a question.
 
102
 
 
103
        If the owner of a question is a team, then the team members can
 
104
        unsubscribe someone.
 
105
        """
 
106
        team_owner = self.factory.makePerson()
 
107
        team_member = self.factory.makePerson()
 
108
        question_owner = self.factory.makeTeam(
 
109
            owner=team_owner, members=[team_member])
 
110
        question = self.factory.makeQuestion(owner=question_owner)
 
111
        subscriber = self.factory.makePerson()
 
112
        subscription = self.factory.makeQuestionSubscription(
 
113
            question=question, person=subscriber)
 
114
        self.assertTrue(subscription.canBeUnsubscribedByUser(team_owner))
 
115
        self.assertTrue(subscription.canBeUnsubscribedByUser(team_member))
 
116
 
 
117
    def test_question_target_owner_can_unsubscribe(self):
 
118
        """Question target owner can unsubscribe someone from a question."""
 
119
        target_owner = self.factory.makePerson()
 
120
        product = self.factory.makeProduct(owner=target_owner)
 
121
        question = self.factory.makeQuestion(target=product)
 
122
        subscriber = self.factory.makePerson()
 
123
        subscription = self.factory.makeQuestionSubscription(
 
124
            question=question, person=subscriber)
 
125
        self.assertTrue(subscription.canBeUnsubscribedByUser(target_owner))
 
126
 
 
127
    def test_question_target_answer_contact_can_unsubscribe(self):
 
128
        """Question target answer contact can unsubscribe someone."""
 
129
        answer_contact = self.factory.makePerson()
 
130
        english = getUtility(ILanguageSet)['en']
 
131
        answer_contact.addLanguage(english)
 
132
        distro_owner = self.factory.makePerson()
 
133
        distro = self.factory.makeDistribution(owner=distro_owner)
 
134
        with person_logged_in(distro_owner):
 
135
            distro.addAnswerContact(answer_contact, answer_contact)
 
136
        question = self.factory.makeQuestion(target=distro)
 
137
        subscriber = self.factory.makePerson()
 
138
        subscription = self.factory.makeQuestionSubscription(
 
139
            question=question, person=subscriber)
 
140
        self.assertTrue(subscription.canBeUnsubscribedByUser(answer_contact))