1
# Copyright 2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Tests for the QuestionSubscrption model object.."""
8
from zope.component import getUtility
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 (
19
class TestQuestionSubscription(TestCaseWithFactory):
20
"""Tests relating to question subscriptions in general."""
22
layer = DatabaseFunctionalLayer
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)
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)
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))
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):
54
UserCannotUnsubscribePerson,
60
class TestQuestionSubscriptionCanBeUnsubscribedbyUser(TestCaseWithFactory):
61
"""Tests for QuestionSubscription.canBeUnsubscribedByUser."""
63
layer = DatabaseFunctionalLayer
66
"""None for a user always returns False."""
67
subscription = self.factory.makeQuestionSubscription()
68
self.assertFalse(subscription.canBeUnsubscribedByUser(None))
70
def test_self_subscriber(self):
71
"""The subscriber has permission to unsubscribe."""
72
subscription = self.factory.makeQuestionSubscription()
74
subscription.canBeUnsubscribedByUser(subscription.person))
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))
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))
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))
100
def test_question_team_owner_can_unsubscribe(self):
101
"""Question team owner can unsubscribe someone from a question.
103
If the owner of a question is a team, then the team members can
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))
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))
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))