11497.1.1
by Curtis Hovey
Restrict the +addquestion summary (field.title) to 250 max characters. |
1 |
# Copyright 2010 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 question module."""
|
|
5 |
||
6 |
__metaclass__ = type |
|
7 |
||
8 |
__all__ = [] |
|
9 |
||
14022.1.3
by Curtis Hovey
created QuestionTargetWidget to show distros that use answers. |
10 |
from canonical.launchpad.webapp.servers import LaunchpadTestRequest |
11666.3.5
by Curtis Hovey
Import layers from canonical.testing.layers. |
11 |
from canonical.testing.layers import DatabaseFunctionalLayer |
14022.1.3
by Curtis Hovey
created QuestionTargetWidget to show distros that use answers. |
12 |
from lp.answers.browser.question import QuestionTargetWidget |
13 |
from lp.answers.interfaces.question import IQuestion |
|
11497.1.1
by Curtis Hovey
Restrict the +addquestion summary (field.title) to 250 max characters. |
14 |
from lp.answers.publisher import AnswersLayer |
15 |
from lp.testing import ( |
|
16 |
login_person, |
|
17 |
TestCaseWithFactory, |
|
18 |
)
|
|
19 |
from lp.testing.views import create_initialized_view |
|
20 |
||
21 |
||
22 |
class TestQuestionAddView(TestCaseWithFactory): |
|
23 |
"""Verify the behavior of the QuestionAddView."""
|
|
24 |
||
25 |
layer = DatabaseFunctionalLayer |
|
26 |
||
27 |
def setUp(self): |
|
28 |
super(TestQuestionAddView, self).setUp() |
|
29 |
self.question_target = self.factory.makeProduct() |
|
30 |
self.user = self.factory.makePerson() |
|
31 |
login_person(self.user) |
|
32 |
||
33 |
def getSearchForm(self, title, language='en'): |
|
34 |
return { |
|
35 |
'field.title': title, |
|
36 |
'field.language': language, |
|
37 |
'field.actions.continue': 'Continue', |
|
38 |
}
|
|
39 |
||
40 |
def test_question_title_within_max_display_width(self): |
|
41 |
# Titles (summary in the view) less than 250 characters are accepted.
|
|
42 |
form = self.getSearchForm('123456789 ' * 10) |
|
43 |
view = create_initialized_view( |
|
44 |
self.question_target, name='+addquestion', layer=AnswersLayer, |
|
45 |
form=form, principal=self.user) |
|
46 |
self.assertEqual([], view.errors) |
|
47 |
||
48 |
def test_question_title_exceeds_max_display_width(self): |
|
49 |
# Titles (summary in the view) cannot exceed 250 characters.
|
|
50 |
form = self.getSearchForm('123456789 ' * 26) |
|
51 |
view = create_initialized_view( |
|
52 |
self.question_target, name='+addquestion', layer=AnswersLayer, |
|
53 |
form=form, principal=self.user) |
|
54 |
self.assertEqual(1, len(view.errors)) |
|
55 |
self.assertEqual( |
|
56 |
'The summary cannot exceed 250 characters.', view.errors[0]) |
|
14022.1.3
by Curtis Hovey
created QuestionTargetWidget to show distros that use answers. |
57 |
|
58 |
||
14022.1.6
by Curtis Hovey
Retargeting must be the last change made to the question. |
59 |
class QuestionEditViewTestCase(TestCaseWithFactory): |
60 |
"""Verify the behavior of the QuestionEditView."""
|
|
61 |
||
62 |
layer = DatabaseFunctionalLayer |
|
63 |
||
64 |
def getForm(self, question): |
|
65 |
if question.assignee is None: |
|
66 |
assignee = '' |
|
67 |
else: |
|
68 |
assignee = question.assignee.name |
|
69 |
return { |
|
70 |
'field.title': question.title, |
|
71 |
'field.description': question.description, |
|
72 |
'field.language': question.language.code, |
|
73 |
'field.assignee': assignee, |
|
74 |
'field.target': 'product', |
|
75 |
'field.target.distribution': '', |
|
76 |
'field.target.package': '', |
|
77 |
'field.target.product': question.target.name, |
|
78 |
'field.whiteboard': question.whiteboard, |
|
79 |
'field.actions.change': 'Change', |
|
80 |
}
|
|
81 |
||
82 |
def test_retarget_with_other_changed(self): |
|
83 |
# Retargeting must be the last change made to the question
|
|
84 |
# to ensure that user permission do not change while there
|
|
85 |
# are more changes to make.
|
|
86 |
target = self.factory.makeProduct() |
|
87 |
question = self.factory.makeQuestion(target=target) |
|
88 |
other_target = self.factory.makeProduct() |
|
89 |
login_person(target.owner) |
|
90 |
form = self.getForm(question) |
|
91 |
form['field.whiteboard'] = 'comment' |
|
92 |
form['field.target.product'] = other_target.name |
|
93 |
view = create_initialized_view( |
|
94 |
question, name='+edit', layer=AnswersLayer, form=form) |
|
95 |
self.assertEqual([], view.errors) |
|
96 |
self.assertEqual(other_target, question.target) |
|
97 |
self.assertEqual('comment', question.whiteboard) |
|
98 |
||
99 |
||
14022.1.3
by Curtis Hovey
created QuestionTargetWidget to show distros that use answers. |
100 |
class QuestionTargetWidgetTestCase(TestCaseWithFactory): |
101 |
"""Test that QuestionTargetWidgetTestCase behaves as expected."""
|
|
102 |
layer = DatabaseFunctionalLayer |
|
103 |
||
104 |
def getWidget(self, question): |
|
105 |
field = IQuestion['target'] |
|
106 |
bound_field = field.bind(question) |
|
107 |
request = LaunchpadTestRequest() |
|
108 |
return QuestionTargetWidget(bound_field, request) |
|
109 |
||
110 |
def test_getDistributionVocabulary_with_product_question(self): |
|
111 |
# The vocabulary does not contain distros that do not use
|
|
112 |
# launchpad to track answers.
|
|
113 |
distribution = self.factory.makeDistribution() |
|
114 |
product = self.factory.makeProduct() |
|
115 |
question = self.factory.makeQuestion(target=product) |
|
116 |
target_widget = self.getWidget(question) |
|
117 |
vocabulary = target_widget.getDistributionVocabulary() |
|
118 |
self.assertEqual(None, vocabulary.distribution) |
|
119 |
self.assertFalse( |
|
120 |
distribution in vocabulary, |
|
121 |
"Vocabulary contains distros that do not use Launchpad Answers.") |
|
122 |
||
123 |
def test_getDistributionVocabulary_with_distribution_question(self): |
|
124 |
# The vocabulary does not contain distros that do not use
|
|
125 |
# launchpad to track answers.
|
|
126 |
distribution = self.factory.makeDistribution() |
|
127 |
other_distribution = self.factory.makeDistribution() |
|
128 |
question = self.factory.makeQuestion(target=distribution) |
|
129 |
target_widget = self.getWidget(question) |
|
130 |
vocabulary = target_widget.getDistributionVocabulary() |
|
131 |
self.assertEqual(distribution, vocabulary.distribution) |
|
132 |
self.assertTrue( |
|
133 |
distribution in vocabulary, |
|
134 |
"Vocabulary missing context distribution.") |
|
135 |
self.assertFalse( |
|
136 |
other_distribution in vocabulary, |
|
137 |
"Vocabulary contains distros that do not use Launchpad Answers.") |