-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_query.py
341 lines (290 loc) · 14.3 KB
/
test_query.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
from portality import models
from doajtest.fixtures import AccountFixtureFactory, ArticleFixtureFactory
from doajtest.helpers import DoajTestCase, deep_sort
from portality.bll.services.query import QueryService, Query
from portality.bll import exceptions
QUERY_ROUTE = {
"query" : {
"article" : {
"auth" : False,
"role" : None,
"query_filters" : ["only_in_doaj"],
"result_filters" : ["public_result_filter"],
"dao" : "portality.models.Article"
}
},
"publisher_query" : {
"journal" : {
"auth" : True,
"role" : "publisher",
"query_filters" : ["owner", "only_in_doaj"],
"result_filters" : ["publisher_result_filter"],
"dao" : "portality.models.Journal"
}
},
"admin_query" : {
"journal" : {
"auth" : True,
"role" : "admin",
"dao" : "portality.models.Journal"
}
},
"api_query" : {
"article" : {
"auth" : False,
"role" : None,
"query_filters" : ["only_in_doaj", "public_source"],
"result_filters" : ["public_result_filter"],
"dao" : "portality.models.Article",
"page_size" : 1
},
"journal" : {
"auth" : False,
"role" : None,
"query_filters" : ["only_in_doaj", "public_source"],
"dao" : "portality.models.Journal"
},
"suggestion" : {
"auth" : True,
"role" : None,
"query_filters" : ["owner", "private_source"],
"dao" : "portality.models.Suggestion"
}
}
}
QUERY_FILTERS = {
# query filters
"only_in_doaj" : "portality.lib.query_filters.only_in_doaj",
"owner" : "portality.lib.query_filters.owner",
# result filters
"public_result_filter" : "portality.lib.query_filters.public_result_filter",
# source filter
"public_source": "portality.lib.query_filters.public_source"
}
def without_keys(d, keys):
return {x: d[x] for x in d if x not in keys}
class TestQuery(DoajTestCase):
def setUp(self):
super(TestQuery, self).setUp()
self.OLD_QUERY_ROUTE = self.app_test.config.get('QUERY_ROUTE')
self.app_test.config['QUERY_ROUTE'] = QUERY_ROUTE
self.OLD_QUERY_FILTERS = self.app_test.config.get('QUERY_FILTERS')
self.app_test.config['QUERY_FILTERS'] = QUERY_FILTERS
def tearDown(self):
super(TestQuery, self).tearDown()
self.app_test.config['QUERY_ROUTE'] = self.OLD_QUERY_ROUTE
self.app_test.config['QUERY_FILTERS'] = self.OLD_QUERY_FILTERS
def test_01_auth(self):
with self.app_test.test_client() as t_client:
response = t_client.get('/query/journal') # not in the settings above
assert response.status_code == 403, response.status_code
# theoretically should be a 404, but the code checks QUERY_ROUTE config first, so auth checks go first
response = t_client.get('/query/nonexistent')
assert response.status_code == 403, response.status_code
response = t_client.get('/query/article')
assert response.status_code == 200, response.status_code
response = t_client.get('/publisher_query/journal')
assert response.status_code == 403, response.status_code
# These don't work for some reason, the logging in bit with the context doesn't work so they're all 403 Forbidden.
# a publisher logs in
# pub = models.Account(**AccountFixtureFactory.make_publisher_source())
# ctx = self._make_and_push_test_context(acc=pub, path='/publisher_query/journal')
# response = t_client.get('/publisher_query/journal')
# assert response.status_code == 200, response.status_code
#
# response = t_client.get('/admin_query/journal')
# assert response.status_code == 403, response.status_code
# ctx.pop()
# a managing editor logs in
# maned = models.Account(**AccountFixtureFactory.make_managing_editor_source())
# with self._make_and_push_test_context(acc=maned):
# response = t_client.get('/admin_query/journal')
# assert response.status_code == 200, response.status_code
def test_02_query_gen(self):
q = Query()
q.add_must({"term": {"admin.in_doaj": True}})
assert q.as_dict() == {
'track_total_hits' : True,
'query': {
'bool': {
'must': [
{"match_all" : {}},
{'term': {'admin.in_doaj': True}}
]
}
}
},q.as_dict()
q = Query()
q.clear_match_all()
assert q.as_dict() == {'track_total_hits' : True, 'query': {}}, q.as_dict()
q = Query()
q.add_include("last_updated")
assert q.as_dict() == {'track_total_hits' : True, "query": {"match_all": {}},"_source": {"includes": ["last_updated"]}}, q.as_dict()
q = Query()
q.add_include(["last_updated", "id"])
assert sorted(q.as_dict()) == sorted({'track_total_hits' : True, "query": {"match_all": {}},"_source": {"includes": ["last_updated", "id"]}}) or sorted(q.as_dict()) == sorted({"query": {"match_all": {}},"_source": {"include": ["last_updated", "id"]}}), sorted(q.as_dict())
def test_03_query_svc_get_config(self):
qsvc = QueryService()
cfg = qsvc._get_config_for_search('query', 'article', account=None)
assert cfg == {
"auth" : False,
"role" : None,
"query_filters" : ["only_in_doaj"],
"result_filters" : ["public_result_filter"],
"dao" : "portality.models.Article"
}, cfg
with self.assertRaises(exceptions.AuthoriseException):
cfg = qsvc._get_config_for_search('query_nonexistent', 'article', account=None)
with self.assertRaises(exceptions.AuthoriseException):
cfg = qsvc._get_config_for_search('query', 'article_nonexistent', account=None)
with self.assertRaises(exceptions.AuthoriseException): # because account is None and auth is required
cfg = qsvc._get_config_for_search('publisher_query', 'journal', account=None)
pub = models.Account(**AccountFixtureFactory.make_publisher_source())
cfg = qsvc._get_config_for_search('publisher_query', 'journal', account=pub)
assert cfg == {
"auth" : True,
"role" : "publisher",
"query_filters" : ["owner", "only_in_doaj"],
"result_filters" : ["publisher_result_filter"],
"dao" : "portality.models.Journal"
}
with self.assertRaises(exceptions.AuthoriseException): # because account is a publisher and an 'admin' role is needed
cfg = qsvc._get_config_for_search('admin_query', 'journal', account=pub)
maned = models.Account(**AccountFixtureFactory.make_managing_editor_source())
cfg = qsvc._get_config_for_search('admin_query', 'journal', account=maned)
assert cfg == {
"auth" : True,
"role" : "admin",
"dao" : "portality.models.Journal"
}
def test_04_pre_filter_search_query(self):
q = Query()
qsvc = QueryService()
cfg = qsvc._get_config_for_search('query', 'article', account=None)
assert q.as_dict() == {"track_total_hits" : True, "query": {"match_all": {}}}, q.as_dict()
qsvc._pre_filter_search_query(cfg, q)
assert q.as_dict() == {
"track_total_hits": True,
'query': {
'bool': {
'filter': [
{'term': {'admin.in_doaj': True}}
]
}
}
}, q.as_dict()
def test_05_post_filter_search_results(self):
# The config above says that the public_result_filter should run on the results. That should delete admin.publisher_record_id.
# We take a fake result set with the secret info in, run the results filters and expect to have the data cleaned.
# We are testing that the mechanism that runs post filters works, the idea is not to test public_result_filter specifically.
qsvc = QueryService()
cfg = qsvc._get_config_for_search('query', 'article', account=None)
res = {
"hits": {
"hits": [
{ "_type": "article", "_source": { "admin": { "seal": False, "publisher_record_id" : "some_identifier"}, "bibjson": {}}},
{ "_type": "article", "_source": { "admin": { "seal": False, "publisher_record_id" : "some_identifier"}, "bibjson": {}}},
{ "_type": "article", "_source": { "admin": { "seal": False, "publisher_record_id" : "some_identifier"}, "bibjson": {}}}
],
"total": 3
}
}
res = qsvc._post_filter_search_results(cfg, res)
assert res == {
"hits": {
"hits": [
{ "_type": "article", "_source": { "admin": { "seal": False }, "bibjson": {}}},
{ "_type": "article", "_source": { "admin": { "seal": False }, "bibjson": {}}},
{ "_type": "article", "_source": { "admin": { "seal": False }, "bibjson": {}}}
],
"total": 3
}
}
def test_06_post_filter_search_results_unpacked(self):
# The config above says that the public_result_filter should run on the results. That should delete admin.publisher_record_id.
# We take a fake result set with the secret info in, run the results filters and expect to have the data cleaned.
# We are testing that the mechanism that runs post filters works, the idea is not to test public_result_filter specifically.
qsvc = QueryService()
cfg = qsvc._get_config_for_search('query', 'article', account=None)
res1 = { "admin": { "seal": False, "publisher_record_id" : "some_identifier"}, "bibjson": {}}
res2 = { "admin": { "seal": False, "publisher_record_id" : "some_identifier"}, "bibjson": {}}
res3 = { "admin": { "seal": False, "publisher_record_id" : "some_identifier"}, "bibjson": {}}
res1 = qsvc._post_filter_search_results(cfg, res1, unpacked=True)
assert res1 == { "admin": { "seal": False }, "bibjson": {}}
res2 = qsvc._post_filter_search_results(cfg, res2, unpacked=True)
assert res2 == { "admin": { "seal": False }, "bibjson": {}}
res3 = qsvc._post_filter_search_results(cfg, res3, unpacked=True)
assert res1 == { "admin": { "seal": False }, "bibjson": {}}
def test_07_get_query(self):
# q = Query()
raw_query = {
"query" : {
"query_string" : {
"query" : '*',
"default_operator": "AND"
}
},
"from" : 0,
"size" : 100
}
qsvc = QueryService()
cfg = qsvc._get_config_for_search('api_query', 'journal', account=None)
# assert q.as_dict() == {"query": {"match_all": {}}}, q.as_dict()
query = qsvc._get_query(cfg, raw_query)
expected_result = {
'query': {
'bool': {
'must': [
{'query_string': {'query': '*', 'default_operator': 'AND'}}
],
"filter" : [
{'term': {'admin.in_doaj': True}}
]
}
},
'_source': {'includes': ['last_updated', 'admin.ticked', 'created_date', 'admin.seal', 'id', 'bibjson']},
'from': 0, 'size': 100
}
q_but_source = without_keys(query.as_dict(), ['_source'])
r_but_source = without_keys(expected_result, ['_source'])
query_sorted = deep_sort(query.as_dict())
expected_result_sorted = deep_sort(expected_result)
assert query_sorted == expected_result_sorted, query_sorted
def test_08_get_dao_klass(self):
qsvc = QueryService()
cfg = qsvc._get_config_for_search('query', 'article', account=None)
dao_klass = qsvc._get_dao_klass(cfg)
self.assertIs(dao_klass, models.Article)
def test_09_search(self):
# Just bringing it all together. Make 4 articles: 3 in DOAJ, 1 not in DOAJ
# We then expect pre-filters to run on the query, ensuring we only get the 3 in DOAJ articles.
# We also expect the post-filters to run on the results, ensuring non-public data is deleted from the admin section.
qsvc = QueryService()
articles = []
for i in range(0, 3):
articles.append(models.Article(**ArticleFixtureFactory.make_article_source(with_id=False)))
assert articles[-1].publisher_record_id() == 'some_identifier'
articles[-1].save(blocking=True)
articles.append(models.Article(**ArticleFixtureFactory.make_article_source(with_id=False, in_doaj=False)))
articles[-1].save(blocking=True)
res = qsvc.search('query', 'article', {"query": {"match_all": {}}}, account=None, additional_parameters={})
assert res['hits']['total']["value"] == 3, res['hits']['total']["value"]
for hit in res['hits']['hits']:
am = models.Article(**hit)
assert am.publisher_record_id() is None, am.publisher_record_id()
def test_10_scroll(self):
# Just bringing it all together. Make 4 articles: 3 in DOAJ, 1 not in DOAJ
# We then expect pre-filters to run on the query, ensuring we only get the 3 in DOAJ articles.
# We also expect the post-filters to run on the results, ensuring non-public data is deleted from the admin section.
qsvc = QueryService()
articles = []
for i in range(0, 3):
articles.append(models.Article(**ArticleFixtureFactory.make_article_source(with_id=False)))
assert articles[-1].publisher_record_id() == 'some_identifier'
articles[-1].save(blocking=True)
articles.append(models.Article(**ArticleFixtureFactory.make_article_source(with_id=False, in_doaj=False)))
articles[-1].save(blocking=True)
q = {"query": {"match_all": {}}}
for res in qsvc.scroll('api_query', 'article', q, None, None):
am = models.Article(**res)
assert am.publisher_record_id() is None, am.publisher_record_id()