/
test_search.py
234 lines (180 loc) · 7.64 KB
/
test_search.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
import itertools
import pytest
from isic.core.models.image import RESTRICTED_SEARCH_FIELDS
from isic.core.search import add_to_search_index, get_elasticsearch_client
@pytest.fixture
def private_searchable_image(image_factory, search_index):
image = image_factory(public=False)
add_to_search_index(image)
# Ensure that the images are available in the index for search
get_elasticsearch_client().indices.refresh(index='_all')
return image
@pytest.fixture
def searchable_images(image_factory, search_index):
images = [
image_factory(public=True, accession__metadata={'diagnosis': 'melanoma'}),
image_factory(public=False, accession__metadata={'diagnosis': 'nevus'}),
]
for image in images:
add_to_search_index(image)
# Ensure that the images are available in the index for search
get_elasticsearch_client().indices.refresh(index='_all')
return images
@pytest.fixture
def searchable_images_with_private_fields(image_factory, search_index):
images = [
image_factory(public=True, accession__metadata={'age': 50}),
image_factory(public=True, accession__metadata={'patient_id': 'IP_1234567'}),
image_factory(public=True, accession__metadata={'lesion_id': 'IL_1234567'}),
]
for image in images:
add_to_search_index(image)
# Ensure that the images are available in the index for search
get_elasticsearch_client().indices.refresh(index='_all')
return images
@pytest.fixture
def private_and_public_images_collections(search_index, image_factory, collection_factory):
public_coll, private_coll = collection_factory(public=True), collection_factory(public=False)
public_image, private_image = image_factory(public=True), image_factory(public=False)
public_coll.images.add(public_image)
private_coll.images.add(private_image)
for image in [public_image, private_image]:
add_to_search_index(image)
get_elasticsearch_client().indices.refresh(index='_all')
yield public_coll, private_coll
@pytest.fixture
def collection_with_image(search_index, image_factory, collection_factory):
public_coll = collection_factory(public=True)
public_image = image_factory(public=True, accession__metadata={'age': 52})
public_coll.images.add(public_image)
add_to_search_index(public_image)
get_elasticsearch_client().indices.refresh(index='_all')
yield public_coll
@pytest.mark.django_db
def test_core_api_image_search(searchable_images, staff_api_client):
r = staff_api_client.get('/api/v2/images/search/')
assert r.status_code == 200, r.data
assert r.data['count'] == 2
r = staff_api_client.get('/api/v2/images/search/', {'query': 'diagnosis:nevus'})
assert r.status_code == 200, r.data
assert r.data['count'] == 1
@pytest.mark.django_db
def test_core_api_image_search_private_image(private_searchable_image, authenticated_api_client):
r = authenticated_api_client.get('/api/v2/images/search/')
assert r.status_code == 200, r.data
assert r.data['count'] == 0
@pytest.mark.django_db
def test_core_api_image_search_private_image_as_guest(private_searchable_image, api_client):
r = api_client.get('/api/v2/images/search/')
assert r.status_code == 200, r.data
assert r.data['count'] == 0
@pytest.mark.django_db
def test_core_api_image_search_images_as_guest(searchable_images, api_client):
r = api_client.get('/api/v2/images/search/')
assert r.status_code == 200, r.data
assert r.data['count'] == 1
@pytest.mark.django_db
def test_core_api_image_search_contributed(
private_searchable_image, authenticated_api_client, user
):
private_searchable_image.accession.upload.cohort.contributor.owners.add(user)
add_to_search_index(private_searchable_image)
get_elasticsearch_client().indices.refresh(index='_all')
r = authenticated_api_client.get('/api/v2/images/search/')
assert r.status_code == 200, r.data
assert r.data['count'] == 1
@pytest.mark.django_db
def test_core_api_image_search_shares(private_searchable_image, authenticated_api_client, user):
private_searchable_image.shares.add(user, through_defaults={'creator': user})
private_searchable_image.save()
add_to_search_index(private_searchable_image)
get_elasticsearch_client().indices.refresh(index='_all')
r = authenticated_api_client.get('/api/v2/images/search/')
assert r.status_code == 200, r.data
assert r.data['count'] == 1
@pytest.mark.django_db
@pytest.mark.parametrize(
'restricted_field,route',
itertools.product(RESTRICTED_SEARCH_FIELDS, ['/api/v2/images/', '/api/v2/images/search/']),
)
def test_core_api_image_hides_fields(
authenticated_api_client, searchable_images_with_private_fields, restricted_field, route
):
r = authenticated_api_client.get('/api/v2/images/search/')
assert r.status_code == 200, r.data
assert r.data['count'] == 3
for image in r.data['results']:
assert restricted_field not in image['metadata']
@pytest.mark.django_db
def test_core_api_image_search_collection_and_query(
collection_with_image, authenticated_api_client
):
r = authenticated_api_client.get(
'/api/v2/images/search/',
{'collections': f'{collection_with_image.pk}', 'query': 'age_approx:50'},
)
assert r.status_code == 200, r.data
assert r.data['count'] == 1
@pytest.mark.django_db
@pytest.mark.parametrize(
'collection_is_public,image_is_public,can_see',
[
(True, True, True),
# Don't leak which images are in a private collection
(False, True, False),
(False, False, False),
],
ids=['all-public', 'private-coll-public-image', 'all-private'],
)
def test_core_api_image_search_collection(
authenticated_api_client,
image_factory,
collection_factory,
search_index,
collection_is_public,
image_is_public,
can_see,
):
collection = collection_factory(public=collection_is_public)
image = image_factory(public=image_is_public)
collection.images.add(image)
add_to_search_index(image)
get_elasticsearch_client().indices.refresh(index='_all')
r = authenticated_api_client.get('/api/v2/images/search/', {'collections': str(collection.pk)})
assert r.status_code == 200, r.data
if can_see:
assert r.data['count'] == 1
else:
assert r.data['count'] == 0
@pytest.mark.django_db
def test_core_api_image_search_collection_parsing(
private_and_public_images_collections, authenticated_api_client
):
public_coll, private_coll = private_and_public_images_collections
r = authenticated_api_client.get(
'/api/v2/images/search/', {'collections': f'{public_coll.pk},{private_coll.pk}'}
)
assert r.status_code == 200, r.data
assert r.data['count'] == 1
@pytest.mark.django_db
def test_core_api_image_faceting_collections(
private_and_public_images_collections, authenticated_api_client
):
public_coll, private_coll = private_and_public_images_collections
r = authenticated_api_client.get(
'/api/v2/images/facets/', {'collections': f'{public_coll.pk},{private_coll.pk}'}
)
assert r.status_code == 200, r.data
buckets = r.data['collections']['buckets']
assert len(buckets) == 1
assert buckets[0] == {'key': public_coll.pk, 'doc_count': 1}
@pytest.mark.django_db
def test_core_api_image_faceting(private_and_public_images_collections, authenticated_api_client):
public_coll, private_coll = private_and_public_images_collections
r = authenticated_api_client.get(
'/api/v2/images/facets/',
)
assert r.status_code == 200, r.data
buckets = r.data['collections']['buckets']
assert len(buckets) == 1, buckets
assert buckets[0] == {'key': public_coll.pk, 'doc_count': 1}, buckets