/
test_api.py
252 lines (208 loc) · 9.07 KB
/
test_api.py
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
245
246
247
248
249
250
251
252
import json
from nose.tools import eq_
from rest_framework.test import APIClient
from django.conf import settings
from kitsune.search.tests.test_es import ElasticTestCase
from kitsune.sumo.urlresolvers import reverse
from kitsune.questions.tests import question, answer
from kitsune.products.tests import product
from kitsune.wiki.tests import document, revision
class SuggestViewTests(ElasticTestCase):
client_class = APIClient
def _make_question(self, solved=True, **kwargs):
defaults = {
'title': 'Login to website comments disabled',
'content': """
readersupportednews.org, sends me emails with a list of
articles to read.
The links to the articles work as normal, except that I
cannot login from the linked article - as required - to
send my comments.
I see a javascript activity statement at the bottom left
corner of my screen while the left button is depressed
on the Login button. it is gone when I release the left
button, but no results.
I have the latest (7) version of java enabled, on an XP
box.
Why this inability to login to this website commentary?
""",
'save': True,
}
defaults.update(kwargs)
q = question(**defaults)
if solved:
a = answer(question=q, save=True)
q.solution = a
# Trigger a reindex for the question.
q.save()
return q
def _make_document(self, **kwargs):
defaults = {
'title': 'How to make a pie from scratch with email',
'category': 10,
'save': True
}
defaults.update(kwargs)
d = document(**defaults)
revision(document=d, is_approved=True, save=True)
d.save()
return d
def test_invalid_product(self):
res = self.client.get(reverse('search.suggest'), {
'product': 'nonexistant',
'q': 'search',
})
eq_(res.status_code, 400)
eq_(res.data['detail'], {'product': [u'Could not find product with slug "nonexistant".']})
def test_invalid_locale(self):
res = self.client.get(reverse('search.suggest'), {
'locale': 'bad-medicine',
'q': 'search',
})
eq_(res.status_code, 400)
eq_(res.data['detail'], {'locale': [u'Could not find locale "bad-medicine".']})
def test_invalid_fallback_locale_none_case(self):
# Test the locale -> locale case.
non_none_locale_fallback_pairs = [
(key, val) for key, val in sorted(settings.NON_SUPPORTED_LOCALES.items())
if val is not None
]
locale, fallback = non_none_locale_fallback_pairs[0]
res = self.client.get(reverse('search.suggest'), {
'locale': locale,
'q': 'search',
})
eq_(res.status_code, 400)
eq_(
res.data['detail'],
{'locale': [u'"{0}" is not supported, but has fallback locale "{1}".'.format(
locale, fallback)]}
)
def test_invalid_fallback_locale_non_none_case(self):
# Test the locale -> None case which falls back to WIKI_DEFAULT_LANGUAGE.
has_none_locale_fallback_pairs = [
(key, val) for key, val in sorted(settings.NON_SUPPORTED_LOCALES.items())
if val is None
]
locale, fallback = has_none_locale_fallback_pairs[0]
res = self.client.get(reverse('search.suggest'), {
'locale': locale,
'q': 'search',
})
eq_(res.status_code, 400)
eq_(
res.data['detail'],
{'locale': [u'"{0}" is not supported, but has fallback locale "{1}".'.format(
locale, settings.WIKI_DEFAULT_LANGUAGE)]}
)
def test_invalid_numbers(self):
res = self.client.get(reverse('search.suggest'), {
'max_questions': 'a',
'max_documents': 'b',
'q': 'search',
})
eq_(res.status_code, 400)
eq_(res.data['detail'], {
'max_questions': [u'Enter a whole number.'],
'max_documents': [u'Enter a whole number.'],
})
def test_q_required(self):
res = self.client.get(reverse('search.suggest'))
eq_(res.status_code, 400)
eq_(res.data['detail'], {'q': [u'This field is required.']})
def test_it_works(self):
q1 = self._make_question()
d1 = self._make_document()
self.refresh()
req = self.client.get(reverse('search.suggest'), {'q': 'emails'})
eq_([q['id'] for q in req.data['questions']], [q1.id])
eq_([d['title'] for d in req.data['documents']], [d1.title])
def test_filters_in_postdata(self):
q1 = self._make_question()
d1 = self._make_document()
self.refresh()
data = json.dumps({'q': 'emails'})
# Note: Have to use .generic() because .get() will convert the
# data into querystring params and then it's clownshoes all
# the way down.
req = self.client.generic(
'GET', reverse('search.suggest'), data=data,
content_type='application/json')
eq_(req.status_code, 200)
eq_([q['id'] for q in req.data['questions']], [q1.id])
eq_([d['title'] for d in req.data['documents']], [d1.title])
def test_both_querystring_and_body_raises_error(self):
self._make_question()
self._make_document()
self.refresh()
data = json.dumps({'q': 'emails'})
# Note: Have to use .generic() because .get() will convert the
# data into querystring params and then it's clownshoes all
# the way down.
req = self.client.generic(
'GET', reverse('search.suggest') + '?max_documents=3', data=data,
content_type='application/json')
eq_(req.status_code, 400)
eq_(req.data,
{u'detail': 'Put all parameters either in the querystring or the HTTP request body.'})
def test_questions_max_results_0(self):
self._make_question()
self.refresh()
# Make sure something matches the query first.
req = self.client.get(reverse('search.suggest'), {'q': 'emails'})
eq_(len(req.data['questions']), 1)
# If we specify "don't give me any" make sure we don't get any.
req = self.client.get(reverse('search.suggest'), {'q': 'emails', 'max_questions': '0'})
eq_(len(req.data['questions']), 0)
def test_documents_max_results_0(self):
self._make_document()
self.refresh()
# Make sure something matches the query first.
req = self.client.get(reverse('search.suggest'), {'q': 'emails'})
eq_(len(req.data['documents']), 1)
# If we specify "don't give me any" make sure we don't get any.
req = self.client.get(reverse('search.suggest'), {'q': 'emails', 'max_documents': '0'})
eq_(len(req.data['documents']), 0)
def test_product_filter_works(self):
p1 = product(save=True)
p2 = product(save=True)
q1 = self._make_question(product=p1)
self._make_question(product=p2)
self.refresh()
req = self.client.get(reverse('search.suggest'), {'q': 'emails', 'product': p1.slug})
eq_([q['id'] for q in req.data['questions']], [q1.id])
def test_locale_filter_works_for_questions(self):
q1 = self._make_question(locale='fr')
self._make_question(locale='en-US')
self.refresh()
req = self.client.get(reverse('search.suggest'), {'q': 'emails', 'locale': 'fr'})
eq_([q['id'] for q in req.data['questions']], [q1.id])
def test_locale_filter_works_for_documents(self):
d1 = self._make_document(slug='right-doc', locale='fr')
self._make_document(slug='wrong-doc', locale='en-US')
self.refresh()
req = self.client.get(reverse('search.suggest'), {'q': 'emails', 'locale': 'fr'})
eq_([d['slug'] for d in req.data['documents']], [d1.slug])
def test_document_fields(self):
self._make_document()
self.refresh()
req = self.client.get(reverse('search.suggest'), {'q': 'emails'})
eq_(len(req.data['documents']), 1)
eq_(sorted(req.data['documents'][0].keys()), ['slug', 'summary', 'title', 'url'])
def test_serializer_fields(self):
"""Test that fields from the serializer are included."""
self._make_question()
self.refresh()
req = self.client.get(reverse('search.suggest'), {'q': 'emails'})
# Check that a field that is only available from the DB is in the response.
assert 'metadata' in req.data['questions'][0]
def test_only_solved(self):
"""Test that only solved questions are suggested."""
q1 = self._make_question(solved=True)
q2 = self._make_question(solved=False)
self.refresh()
req = self.client.get(reverse('search.suggest'), {'q': 'emails'})
ids = [q['id'] for q in req.data['questions']]
assert q1.id in ids
assert q2.id not in ids
eq_(len(ids), 1)