1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
# Copyright 2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Webservice unit tests related to Launchpad Questions."""
__metaclass__ = type
from BeautifulSoup import BeautifulSoup
from lazr.restfulclient.errors import HTTPError
from simplejson import dumps
import transaction
from zope.component import getUtility
from canonical.testing.layers import (
AppServerLayer,
DatabaseFunctionalLayer,
FunctionalLayer,
)
from lp.answers.errors import (
AddAnswerContactError,
FAQTargetError,
InvalidQuestionStateError,
NotAnswerContactError,
NotMessageOwnerError,
NotQuestionOwnerError,
QuestionTargetError,
)
from lp.registry.interfaces.person import IPersonSet
from lp.testing import (
celebrity_logged_in,
launchpadlib_for,
logout,
person_logged_in,
TestCase,
TestCaseWithFactory,
ws_object,
)
from lp.testing.pages import LaunchpadWebServiceCaller
from lp.testing.views import create_webservice_error_view
class ErrorsTestCase(TestCase):
"""Test answers errors are exported as HTTPErrors."""
layer = FunctionalLayer
def test_AddAnswerContactError(self):
error_view = create_webservice_error_view(AddAnswerContactError())
self.assertEqual(400, error_view.status)
def test_FAQTargetError(self):
error_view = create_webservice_error_view(FAQTargetError())
self.assertEqual(400, error_view.status)
def test_InvalidQuestionStateError(self):
error_view = create_webservice_error_view(InvalidQuestionStateError())
self.assertEqual(400, error_view.status)
def test_NotAnswerContactError(self):
error_view = create_webservice_error_view(NotAnswerContactError())
self.assertEqual(400, error_view.status)
def test_NotMessageOwnerError(self):
error_view = create_webservice_error_view(NotMessageOwnerError())
self.assertEqual(400, error_view.status)
def test_NotQuestionOwnerError(self):
error_view = create_webservice_error_view(NotQuestionOwnerError())
self.assertEqual(400, error_view.status)
def test_QuestionTargetError(self):
error_view = create_webservice_error_view(QuestionTargetError())
self.assertEqual(400, error_view.status)
class TestQuestionRepresentation(TestCaseWithFactory):
"""Test ways of interacting with Question webservice representations."""
layer = DatabaseFunctionalLayer
def setUp(self):
super(TestQuestionRepresentation, self).setUp()
with celebrity_logged_in('admin'):
self.question = self.factory.makeQuestion(
title="This is a question")
self.webservice = LaunchpadWebServiceCaller(
'launchpad-library', 'salgado-change-anything')
self.webservice.default_api_version = 'devel'
def findQuestionTitle(self, response):
"""Find the question title field in an XHTML document fragment."""
soup = BeautifulSoup(response.body)
dt = soup.find('dt', text="title").parent
dd = dt.findNextSibling('dd')
return str(dd.contents.pop())
def test_top_level_question_get(self):
# The top level question set can be used via the api to get
# a question by id via redirect without url hacking.
response = self.webservice.get(
'/questions/%s' % self.question.id, 'application/xhtml+xml')
self.assertEqual(response.status, 200)
def test_GET_xhtml_representation(self):
# A question's xhtml representation is available on the api.
response = self.webservice.get(
'/%s/+question/%d' % (self.question.target.name,
self.question.id),
'application/xhtml+xml')
self.assertEqual(response.status, 200)
self.assertEqual(
self.findQuestionTitle(response),
"<p>This is a question</p>")
def test_PATCH_xhtml_representation(self):
# You can update the question through the api with PATCH.
new_title = "No, this is a question"
question_json = self.webservice.get(
'/%s/+question/%d' % (self.question.target.name,
self.question.id)).jsonBody()
response = self.webservice.patch(
question_json['self_link'],
'application/json',
dumps(dict(title=new_title)),
headers=dict(accept='application/xhtml+xml'))
self.assertEqual(response.status, 209)
self.assertEqual(
self.findQuestionTitle(response),
"<p>No, this is a question</p>")
class TestSetCommentVisibility(TestCaseWithFactory):
"""Tests who can successfully set comment visibility."""
layer = DatabaseFunctionalLayer
def setUp(self):
super(TestSetCommentVisibility, self).setUp()
self.person_set = getUtility(IPersonSet)
admins = self.person_set.getByName('admins')
self.admin = admins.teamowner
with person_logged_in(self.admin):
self.question = self.factory.makeQuestion()
self.message = self.question.addComment(
self.admin, 'Some comment')
transaction.commit()
def _get_question_for_user(self, user=None):
"""Convenience function to get the api question reference."""
# End any open lplib instance.
logout()
lp = launchpadlib_for("test", user)
return ws_object(lp, self.question)
def _set_visibility(self, question):
"""Method to set visibility; needed for assertRaises."""
question.setCommentVisibility(
comment_number=0,
visible=False)
def test_random_user_cannot_set_visible(self):
# Logged in users without privs can't set question comment
# visibility.
nopriv = self.person_set.getByName('no-priv')
question = self._get_question_for_user(nopriv)
self.assertRaises(
HTTPError,
self._set_visibility,
question)
def test_anon_cannot_set_visible(self):
# Anonymous users can't set question comment
# visibility.
question = self._get_question_for_user()
self.assertRaises(
HTTPError,
self._set_visibility,
question)
def test_registry_admin_can_set_visible(self):
# Members of registry experts can set question comment
# visibility.
registry = self.person_set.getByName('registry')
person = self.factory.makePerson()
with person_logged_in(registry.teamowner):
registry.addMember(person, registry.teamowner)
question = self._get_question_for_user(person)
self._set_visibility(question)
self.assertFalse(self.message.visible)
def test_admin_can_set_visible(self):
# Admins can set question comment
# visibility.
admins = self.person_set.getByName('admins')
person = self.factory.makePerson()
with person_logged_in(admins.teamowner):
admins.addMember(person, admins.teamowner)
question = self._get_question_for_user(person)
self._set_visibility(question)
self.assertFalse(self.message.visible)
class TestQuestionWebServiceSubscription(TestCaseWithFactory):
layer = AppServerLayer
def test_subscribe(self):
# Test subscribe() API.
person = self.factory.makePerson()
with person_logged_in(person):
db_question = self.factory.makeQuestion()
db_person = self.factory.makePerson()
launchpad = self.factory.makeLaunchpadService()
question = ws_object(launchpad, db_question)
person = ws_object(launchpad, db_person)
question.subscribe(person=person)
transaction.commit()
# Check the results.
self.assertTrue(db_question.isSubscribed(db_person))
def test_unsubscribe(self):
# Test unsubscribe() API.
person = self.factory.makePerson()
with person_logged_in(person):
db_question = self.factory.makeQuestion()
db_person = self.factory.makePerson()
db_question.subscribe(person=db_person)
launchpad = self.factory.makeLaunchpadService(person=db_person)
question = ws_object(launchpad, db_question)
person = ws_object(launchpad, db_person)
question.unsubscribe(person=person)
transaction.commit()
# Check the results.
self.assertFalse(db_question.isSubscribed(db_person))
|