forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_elastic_search.py
1561 lines (1296 loc) · 59.8 KB
/
test_elastic_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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import time
import logging
import functools
import unittest
import pytest
from unittest import mock
from framework.auth.core import Auth
from website import settings
import website.search.search as search
from website.search import elastic_search
from website.search.util import build_query
from website.search_migration.migrate import migrate
from osf.models import (
Retraction,
NodeLicense,
OSFGroup,
Tag,
Preprint,
)
from addons.wiki.models import WikiPage
from addons.osfstorage.models import OsfStorageFile
from addons.osfstorage import settings as osfstorage_settings
from scripts.populate_institutions import main as populate_institutions
from osf_tests import factories
from tests.base import OsfTestCase
from tests.test_features import requires_search
from tests.utils import run_celery_tasks
from osf.utils.workflows import CollectionSubmissionStates
TEST_INDEX = 'test'
def query(term, raw=False):
results = search.search(build_query(term), index=elastic_search.INDEX, raw=raw)
return results
def query_collections(name):
term = f'category:collectionSubmission AND "{name}"'
return query(term, raw=True)
def query_user(name):
term = f'category:user AND "{name}"'
return query(term)
def query_file(name):
term = f'category:file AND "{name}"'
return query(term)
def query_tag_file(name):
term = f'category:file AND (tags:u"{name}")'
return query(term)
def retry_assertion(interval=0.3, retries=3):
def test_wrapper(func):
t_interval = interval
t_retries = retries
@functools.wraps(func)
def wrapped(*args, **kwargs):
try:
func(*args, **kwargs)
except AssertionError as e:
if retries:
time.sleep(t_interval)
retry_assertion(interval=t_interval, retries=t_retries - 1)(func)(*args, **kwargs)
else:
raise e
return wrapped
return test_wrapper
def create_file_version(file, user):
file.create_version(user, {
'object': '06d80e',
'service': 'cloud',
osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
}, {
'size': 7,
'contentType': 'img/png'
}).save()
@pytest.mark.enable_search
@pytest.mark.enable_enqueue_task
class TestCollectionsSearch(OsfTestCase):
def setUp(self):
super().setUp()
search.delete_index(elastic_search.INDEX)
search.create_index(elastic_search.INDEX)
self.user = factories.UserFactory(fullname='Salif Keita')
self.node_private = factories.NodeFactory(creator=self.user, title='Salif Keita: Madan', is_public=False)
self.node_public = factories.NodeFactory(creator=self.user, title='Salif Keita: Yamore', is_public=True)
self.node_one = factories.NodeFactory(creator=self.user, title='Salif Keita: Mandjou', is_public=True)
self.node_two = factories.NodeFactory(creator=self.user, title='Salif Keita: Tekere', is_public=True)
self.reg_private = factories.RegistrationFactory(title='Salif Keita: Madan', creator=self.user, is_public=False, archive=True)
self.reg_public = factories.RegistrationFactory(title='Salif Keita: Madan', creator=self.user, is_public=True, archive=True)
self.reg_one = factories.RegistrationFactory(title='Salif Keita: Madan', creator=self.user, is_public=True, archive=True)
self.provider = factories.CollectionProviderFactory()
self.reg_provider = factories.RegistrationProviderFactory()
self.collection_one = factories.CollectionFactory(creator=self.user, is_public=True, provider=self.provider)
self.collection_public = factories.CollectionFactory(creator=self.user, is_public=True, provider=self.provider)
self.collection_private = factories.CollectionFactory(creator=self.user, is_public=False, provider=self.provider)
self.reg_collection = factories.CollectionFactory(creator=self.user, provider=self.reg_provider, is_public=True)
self.reg_collection_private = factories.CollectionFactory(creator=self.user, provider=self.reg_provider, is_public=False)
def test_only_public_collections_submissions_are_searchable(self):
docs = query_collections('Salif Keita')['results']
assert len(docs) == 0
self.collection_public.collect_object(self.node_private, self.user)
self.reg_collection.collect_object(self.reg_private, self.user)
docs = query_collections('Salif Keita')['results']
assert len(docs) == 0
assert not self.node_one.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
assert not self.node_public.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
self.collection_one.collect_object(self.node_one, self.user)
self.collection_public.collect_object(self.node_public, self.user)
self.reg_collection.collect_object(self.reg_public, self.user)
assert self.node_one.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
assert self.node_public.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
assert self.reg_public.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
docs = query_collections('Salif Keita')['results']
assert len(docs) == 3
self.collection_private.collect_object(self.node_two, self.user)
self.reg_collection_private.collect_object(self.reg_one, self.user)
docs = query_collections('Salif Keita')['results']
assert len(docs) == 3
def test_index_on_submission_privacy_changes(self):
# test_submissions_turned_private_are_deleted_from_index
docs = query_collections('Salif Keita')['results']
assert len(docs) == 0
self.collection_public.collect_object(self.node_one, self.user)
self.collection_one.collect_object(self.node_one, self.user)
docs = query_collections('Salif Keita')['results']
assert len(docs) == 2
with run_celery_tasks():
self.node_one.is_public = False
self.node_one.save()
docs = query_collections('Salif Keita')['results']
assert len(docs) == 0
# test_submissions_turned_public_are_added_to_index
self.collection_public.collect_object(self.node_private, self.user)
docs = query_collections('Salif Keita')['results']
assert len(docs) == 0
with run_celery_tasks():
self.node_private.is_public = True
self.node_private.save()
docs = query_collections('Salif Keita')['results']
assert len(docs) == 1
def test_index_on_collection_privacy_changes(self):
# test_submissions_of_collection_turned_private_are_removed_from_index
docs = query_collections('Salif Keita')['results']
assert len(docs) == 0
self.collection_public.collect_object(self.node_one, self.user)
self.collection_public.collect_object(self.node_two, self.user)
self.collection_public.collect_object(self.node_public, self.user)
self.reg_collection.collect_object(self.reg_public, self.user)
docs = query_collections('Salif Keita')['results']
assert len(docs) == 4
with run_celery_tasks():
self.collection_public.is_public = False
self.collection_public.save()
self.reg_collection.is_public = False
self.reg_collection.save()
docs = query_collections('Salif Keita')['results']
assert len(docs) == 0
# test_submissions_of_collection_turned_public_are_added_to_index
self.collection_private.collect_object(self.node_one, self.user)
self.collection_private.collect_object(self.node_two, self.user)
self.collection_private.collect_object(self.node_public, self.user)
self.reg_collection_private.collect_object(self.reg_public, self.user)
assert self.node_one.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
assert self.node_two.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
assert self.node_public.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
assert self.reg_public.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
docs = query_collections('Salif Keita')['results']
assert len(docs) == 0
with run_celery_tasks():
self.collection_private.is_public = True
self.collection_private.save()
self.reg_collection.is_public = True
self.reg_collection.save()
docs = query_collections('Salif Keita')['results']
assert len(docs) == 4
def test_collection_submissions_are_removed_from_index_on_delete(self):
docs = query_collections('Salif Keita')['results']
assert len(docs) == 0
self.collection_public.collect_object(self.node_one, self.user)
self.collection_public.collect_object(self.node_two, self.user)
self.collection_public.collect_object(self.node_public, self.user)
self.reg_collection.collect_object(self.reg_public, self.user)
docs = query_collections('Salif Keita')['results']
assert len(docs) == 4
self.collection_public.delete()
self.reg_collection.delete()
assert self.collection_public.deleted
assert self.reg_collection.deleted
docs = query_collections('Salif Keita')['results']
assert len(docs) == 0
def test_removed_submission_are_removed_from_index(self):
self.collection_public.collect_object(self.node_one, self.user)
self.reg_collection.collect_object(self.reg_public, self.user)
assert self.node_one.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
assert self.reg_public.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
docs = query_collections('Salif Keita')['results']
assert len(docs) == 2
self.collection_public.remove_object(self.node_one, Auth(self.user))
self.reg_collection.remove_object(self.reg_public, Auth(self.user))
assert not self.node_one.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
assert not self.reg_public.collection_submissions.filter(
machine_state=CollectionSubmissionStates.ACCEPTED
).exists()
docs = query_collections('Salif Keita')['results']
assert len(docs) == 0
def test_collection_submission_doc_structure(self):
self.collection_public.collect_object(self.node_one, self.user)
docs = query_collections('Keita')['results']
assert docs[0]['_source']['title'] == self.node_one.title
with run_celery_tasks():
self.node_one.title = 'Keita Royal Family of Mali'
self.node_one.save()
docs = query_collections('Keita')['results']
assert docs[0]['_source']['title'] == self.node_one.title
assert docs[0]['_source']['abstract'] == self.node_one.description
assert docs[0]['_source']['contributors'][0]['url'] == self.user.url
assert docs[0]['_source']['contributors'][0]['fullname'] == self.user.fullname
assert docs[0]['_source']['url'] == self.node_one.url
assert docs[0]['_source']['id'] == '{}-{}'.format(self.node_one._id,
self.node_one.collection_submissions[0].collection._id)
assert docs[0]['_source']['category'] == 'collectionSubmission'
def test_search_updated_after_id_change(self):
self.provider.primary_collection.collect_object(self.node_one, self.node_one.creator)
with run_celery_tasks():
self.node_one.save()
term = f'provider:{self.provider._id}'
docs = search.search(build_query(term), index=elastic_search.INDEX, raw=True)
assert len(docs['results']) == 1
self.provider._id = 'new_id'
self.provider.save()
docs = query('provider:new_id', raw=True)['results']
assert len(docs) == 1
@pytest.mark.enable_search
@pytest.mark.enable_enqueue_task
class TestUserUpdate(OsfTestCase):
def setUp(self):
super().setUp()
search.delete_index(elastic_search.INDEX)
search.create_index(elastic_search.INDEX)
self.user = factories.UserFactory(fullname='David Bowie')
def test_new_user(self):
# Verify that user has been added to Elastic Search
docs = query_user(self.user.fullname)['results']
assert len(docs) == 1
def test_new_user_unconfirmed(self):
user = factories.UnconfirmedUserFactory()
docs = query_user(user.fullname)['results']
assert len(docs) == 0
token = user.get_confirmation_token(user.username)
user.confirm_email(token)
user.save()
docs = query_user(user.fullname)['results']
assert len(docs) == 1
@retry_assertion
def test_change_name(self):
# Add a user, change her name, and verify that only the new name is
# found in search.
user = factories.UserFactory(fullname='Barry Mitchell')
fullname_original = user.fullname
user.fullname = user.fullname[::-1]
user.save()
docs_original = query_user(fullname_original)['results']
assert len(docs_original) == 0
docs_current = query_user(user.fullname)['results']
assert len(docs_current) == 1
def test_disabled_user(self):
# Test that disabled users are not in search index
user = factories.UserFactory(fullname='Bettie Page')
user.save()
# Ensure user is in search index
assert len(query_user(user.fullname)['results']) == 1
# Disable the user
user.is_disabled = True
user.save()
# Ensure user is not in search index
assert len(query_user(user.fullname)['results']) == 0
def test_merged_user(self):
user = factories.UserFactory(fullname='Annie Lennox')
merged_user = factories.UserFactory(fullname='Lisa Stansfield')
user.save()
merged_user.save()
assert len(query_user(user.fullname)['results']) == 1
assert len(query_user(merged_user.fullname)['results']) == 1
user.merge_user(merged_user)
assert len(query_user(user.fullname)['results']) == 1
assert len(query_user(merged_user.fullname)['results']) == 0
def test_employment(self):
user = factories.UserFactory(fullname='Helga Finn')
user.save()
institution = 'Finn\'s Fine Filers'
docs = query_user(institution)['results']
assert len(docs) == 0
user.jobs.append({
'institution': institution,
'title': 'The Big Finn',
})
user.save()
docs = query_user(institution)['results']
assert len(docs) == 1
def test_education(self):
user = factories.UserFactory(fullname='Henry Johnson')
user.save()
institution = 'Henry\'s Amazing School!!!'
docs = query_user(institution)['results']
assert len(docs) == 0
user.schools.append({
'institution': institution,
'degree': 'failed all classes',
})
user.save()
docs = query_user(institution)['results']
assert len(docs) == 1
def test_name_fields(self):
names = ['Bill Nye', 'William', 'the science guy', 'Sanford', 'the Great']
user = factories.UserFactory(fullname=names[0])
user.given_name = names[1]
user.middle_names = names[2]
user.family_name = names[3]
user.suffix = names[4]
user.save()
docs = [query_user(name)['results'] for name in names]
assert sum(map(len, docs)) == len(docs) # 1 result each
assert all([user._id == doc[0]['id'] for doc in docs])
@pytest.mark.enable_search
@pytest.mark.enable_enqueue_task
class TestProject(OsfTestCase):
def setUp(self):
super().setUp()
search.delete_index(elastic_search.INDEX)
search.create_index(elastic_search.INDEX)
self.user = factories.UserFactory(fullname='John Deacon')
self.project = factories.ProjectFactory(title='Red Special', creator=self.user)
def test_new_project_private(self):
# Verify that a private project is not present in Elastic Search.
docs = query(self.project.title)['results']
assert len(docs) == 0
def test_make_public(self):
# Make project public, and verify that it is present in Elastic
# Search.
with run_celery_tasks():
self.project.set_privacy('public')
docs = query(self.project.title)['results']
assert len(docs) == 1
@pytest.mark.enable_search
@pytest.mark.enable_enqueue_task
class TestOSFGroup(OsfTestCase):
def setUp(self):
with run_celery_tasks():
super().setUp()
search.delete_index(elastic_search.INDEX)
search.create_index(elastic_search.INDEX)
self.user = factories.UserFactory(fullname='John Deacon')
self.user_two = factories.UserFactory(fullname='Grapes McGee')
self.group = OSFGroup(
name='Cornbread',
creator=self.user,
)
self.group.save()
self.project = factories.ProjectFactory(is_public=True, creator=self.user, title='Biscuits')
self.project.save()
def test_create_osf_group(self):
title = 'Butter'
group = OSFGroup(name=title, creator=self.user)
group.save()
docs = query(title)['results']
assert len(docs) == 1
def test_set_group_name(self):
title = 'Eggs'
self.group.set_group_name(title)
self.group.save()
docs = query(title)['results']
assert len(docs) == 1
docs = query('Cornbread')['results']
assert len(docs) == 0
def test_add_member(self):
self.group.make_member(self.user_two)
docs = query(f'category:group AND "{self.user_two.fullname}"')['results']
assert len(docs) == 1
self.group.make_manager(self.user_two)
docs = query(f'category:group AND "{self.user_two.fullname}"')['results']
assert len(docs) == 1
self.group.remove_member(self.user_two)
docs = query(f'category:group AND "{self.user_two.fullname}"')['results']
assert len(docs) == 0
def test_connect_to_node(self):
self.project.add_osf_group(self.group)
docs = query(f'category:project AND "{self.group.name}"')['results']
assert len(docs) == 1
self.project.remove_osf_group(self.group)
docs = query(f'category:project AND "{self.group.name}"')['results']
assert len(docs) == 0
def test_remove_group(self):
group_name = self.group.name
self.project.add_osf_group(self.group)
docs = query(f'category:project AND "{group_name}"')['results']
assert len(docs) == 1
self.group.remove_group()
docs = query(f'category:project AND "{group_name}"')['results']
assert len(docs) == 0
docs = query(group_name)['results']
assert len(docs) == 0
@pytest.mark.enable_search
@pytest.mark.enable_enqueue_task
class TestPreprint(OsfTestCase):
def setUp(self):
with run_celery_tasks():
super().setUp()
search.delete_index(elastic_search.INDEX)
search.create_index(elastic_search.INDEX)
self.user = factories.UserFactory(fullname='John Deacon')
self.preprint = Preprint(
title='Red Special',
description='We are the champions',
creator=self.user,
provider=factories.PreprintProviderFactory()
)
self.preprint.save()
self.file = OsfStorageFile.create(
target=self.preprint,
path='/panda.txt',
name='panda.txt',
materialized_path='/panda.txt')
self.file.save()
create_file_version(self.file, self.user)
self.published_preprint = factories.PreprintFactory(
creator=self.user,
title='My Fairy King',
description='Under pressure',
)
def test_new_preprint_unsubmitted(self):
# Verify that an unsubmitted preprint is not present in Elastic Search.
title = 'Apple'
self.preprint.title = title
self.preprint.save()
docs = query(title)['results']
assert len(docs) == 0
def test_new_preprint_unpublished(self):
# Verify that an unpublished preprint is not present in Elastic Search.
title = 'Banana'
self.preprint = factories.PreprintFactory(creator=self.user, is_published=False, title=title)
assert self.preprint.title == title
docs = query(title)['results']
assert len(docs) == 0
def test_unsubmitted_preprint_primary_file(self):
# Unpublished preprint's primary_file not showing up in Elastic Search
title = 'Cantaloupe'
self.preprint.title = title
self.preprint.set_primary_file(self.file, auth=Auth(self.user), save=True)
assert self.preprint.title == title
docs = query(title)['results']
assert len(docs) == 0
def test_publish_preprint(self):
title = 'Date'
self.preprint = factories.PreprintFactory(creator=self.user, is_published=False, title=title)
self.preprint.set_published(True, auth=Auth(self.preprint.creator), save=True)
assert self.preprint.title == title
docs = query(title)['results']
# Both preprint and primary_file showing up in Elastic
assert len(docs) == 2
def test_preprint_title_change(self):
title_original = self.published_preprint.title
new_title = 'New preprint title'
self.published_preprint.set_title(new_title, auth=Auth(self.user), save=True)
docs = query('category:preprint AND ' + title_original)['results']
assert len(docs) == 0
docs = query('category:preprint AND ' + new_title)['results']
assert len(docs) == 1
def test_preprint_description_change(self):
description_original = self.published_preprint.description
new_abstract = 'My preprint abstract'
self.published_preprint.set_description(new_abstract, auth=Auth(self.user), save=True)
docs = query(self.published_preprint.title)['results']
docs = query('category:preprint AND ' + description_original)['results']
assert len(docs) == 0
docs = query('category:preprint AND ' + new_abstract)['results']
assert len(docs) == 1
def test_set_preprint_private(self):
# Not currently an option for users, but can be used for spam
self.published_preprint.set_privacy('private', auth=Auth(self.user), save=True)
docs = query(self.published_preprint.title)['results']
# Both preprint and primary_file showing up in Elastic
assert len(docs) == 0
def test_set_primary_file(self):
# Only primary_file should be in index, if primary_file is changed, other files are removed from index.
self.file = OsfStorageFile.create(
target=self.published_preprint,
path='/panda.txt',
name='panda.txt',
materialized_path='/panda.txt')
self.file.save()
create_file_version(self.file, self.user)
self.published_preprint.set_primary_file(self.file, auth=Auth(self.user), save=True)
docs = query(self.published_preprint.title)['results']
assert len(docs) == 2
assert docs[1]['name'] == self.file.name
def test_set_license(self):
license_details = {
'id': 'NONE',
'year': '2015',
'copyrightHolders': ['Iron Man']
}
title = 'Elderberry'
self.published_preprint.title = title
self.published_preprint.set_preprint_license(license_details, Auth(self.user), save=True)
assert self.published_preprint.title == title
docs = query(title)['results']
assert len(docs) == 2
assert docs[0]['license']['copyright_holders'][0] == 'Iron Man'
assert docs[0]['license']['name'] == 'No license'
def test_add_tags(self):
tags = ['stonecoldcrazy', 'just a poor boy', 'from-a-poor-family']
for tag in tags:
docs = query(f'tags:"{tag}"')['results']
assert len(docs) == 0
self.published_preprint.add_tag(tag, Auth(self.user), save=True)
for tag in tags:
docs = query(f'tags:"{tag}"')['results']
assert len(docs) == 1
def test_remove_tag(self):
tags = ['stonecoldcrazy', 'just a poor boy', 'from-a-poor-family']
for tag in tags:
self.published_preprint.add_tag(tag, Auth(self.user), save=True)
self.published_preprint.remove_tag(tag, Auth(self.user), save=True)
docs = query(f'tags:"{tag}"')['results']
assert len(docs) == 0
def test_add_contributor(self):
# Add a contributor, then verify that project is found when searching
# for contributor.
user2 = factories.UserFactory(fullname='Adam Lambert')
docs = query(f'category:preprint AND "{user2.fullname}"')['results']
assert len(docs) == 0
# with run_celery_tasks():
self.published_preprint.add_contributor(user2, save=True)
docs = query(f'category:preprint AND "{user2.fullname}"')['results']
assert len(docs) == 1
def test_remove_contributor(self):
# Add and remove a contributor, then verify that project is not found
# when searching for contributor.
user2 = factories.UserFactory(fullname='Brian May')
self.published_preprint.add_contributor(user2, save=True)
self.published_preprint.remove_contributor(user2, Auth(self.user))
docs = query(f'category:preprint AND "{user2.fullname}"')['results']
assert len(docs) == 0
def test_hide_contributor(self):
user2 = factories.UserFactory(fullname='Brian May')
self.published_preprint.add_contributor(user2)
self.published_preprint.set_visible(user2, False, save=True)
docs = query(f'category:preprint AND "{user2.fullname}"')['results']
assert len(docs) == 0
self.published_preprint.set_visible(user2, True, save=True)
docs = query(f'category:preprint AND "{user2.fullname}"')['results']
assert len(docs) == 1
def test_move_contributor(self):
user2 = factories.UserFactory(fullname='Brian May')
self.published_preprint.add_contributor(user2, save=True)
docs = query(f'category:preprint AND "{user2.fullname}"')['results']
assert len(docs) == 1
docs[0]['contributors'][0]['fullname'] == self.user.fullname
docs[0]['contributors'][1]['fullname'] == user2.fullname
self.published_preprint.move_contributor(user2, Auth(self.user), 0)
docs = query(f'category:preprint AND "{user2.fullname}"')['results']
assert len(docs) == 1
docs[0]['contributors'][0]['fullname'] == user2.fullname
docs[0]['contributors'][1]['fullname'] == self.user.fullname
def test_tag_aggregation(self):
tags = ['stonecoldcrazy', 'just a poor boy', 'from-a-poor-family']
for tag in tags:
self.published_preprint.add_tag(tag, Auth(self.user), save=True)
docs = query(self.published_preprint.title)['tags']
assert len(docs) == 3
for doc in docs:
assert doc['key'] in tags
@pytest.mark.enable_search
@pytest.mark.enable_enqueue_task
class TestNodeSearch(OsfTestCase):
def setUp(self):
super().setUp()
with run_celery_tasks():
self.node = factories.ProjectFactory(is_public=True, title='node')
self.public_child = factories.ProjectFactory(parent=self.node, is_public=True, title='public_child')
self.private_child = factories.ProjectFactory(parent=self.node, title='private_child')
self.public_subchild = factories.ProjectFactory(parent=self.private_child, is_public=True)
self.node.node_license = factories.NodeLicenseRecordFactory()
self.node.save()
self.query = 'category:project & category:component'
@retry_assertion()
def test_node_license_added_to_search(self):
docs = query(self.query)['results']
node = [d for d in docs if d['title'] == self.node.title][0]
assert 'license' in node
assert node['license']['id'] == self.node.node_license.license_id
@unittest.skip('Elasticsearch latency seems to be causing theses tests to fail randomly.')
@retry_assertion(retries=10)
def test_node_license_propogates_to_children(self):
docs = query(self.query)['results']
child = [d for d in docs if d['title'] == self.public_child.title][0]
assert 'license' in child
assert child['license'].get('id') == self.node.node_license.license_id
child = [d for d in docs if d['title'] == self.public_subchild.title][0]
assert 'license' in child
assert child['license'].get('id') == self.node.node_license.license_id
@unittest.skip('Elasticsearch latency seems to be causing theses tests to fail randomly.')
@retry_assertion(retries=10)
def test_node_license_updates_correctly(self):
other_license = NodeLicense.objects.get(name='MIT License')
new_license = factories.NodeLicenseRecordFactory(node_license=other_license)
self.node.node_license = new_license
self.node.save()
docs = query(self.query)['results']
for doc in docs:
assert doc['license'].get('id') == new_license.license_id
@pytest.mark.enable_search
@pytest.mark.enable_enqueue_task
class TestRegistrationRetractions(OsfTestCase):
def setUp(self):
super().setUp()
self.user = factories.UserFactory(fullname='Doug Bogie')
self.title = 'Red Special'
self.consolidate_auth = Auth(user=self.user)
self.project = factories.ProjectFactory(
title=self.title,
description='',
creator=self.user,
is_public=True,
)
self.registration = factories.RegistrationFactory(project=self.project, is_public=True)
@mock.patch('osf.models.registrations.Registration.archiving', mock.PropertyMock(return_value=False))
def test_retraction_is_searchable(self):
self.registration.retract_registration(self.user)
self.registration.retraction.state = Retraction.APPROVED
self.registration.retraction.save()
self.registration.save()
self.registration.retraction._on_complete(self.user)
docs = query('category:registration AND ' + self.title)['results']
assert len(docs) == 1
@mock.patch('osf.models.registrations.Registration.archiving', mock.PropertyMock(return_value=False))
def test_pending_retraction_wiki_content_is_searchable(self):
# Add unique string to wiki
wiki_content = {'home': 'public retraction test'}
for key, value in wiki_content.items():
docs = query(value)['results']
assert len(docs) == 0
with run_celery_tasks():
WikiPage.objects.create_for_node(self.registration, key, value, self.consolidate_auth)
# Query and ensure unique string shows up
docs = query(value)['results']
assert len(docs) == 1
# Query and ensure registration does show up
docs = query('category:registration AND ' + self.title)['results']
assert len(docs) == 1
# Retract registration
self.registration.retract_registration(self.user, '')
with run_celery_tasks():
self.registration.save()
self.registration.reload()
# Query and ensure unique string in wiki doesn't show up
docs = query('category:registration AND "{}"'.format(wiki_content['home']))['results']
assert len(docs) == 1
# Query and ensure registration does show up
docs = query('category:registration AND ' + self.title)['results']
assert len(docs) == 1
@mock.patch('osf.models.registrations.Registration.archiving', mock.PropertyMock(return_value=False))
def test_retraction_wiki_content_is_not_searchable(self):
# Add unique string to wiki
wiki_content = {'home': 'public retraction test'}
for key, value in wiki_content.items():
docs = query(value)['results']
assert len(docs) == 0
with run_celery_tasks():
WikiPage.objects.create_for_node(self.registration, key, value, self.consolidate_auth)
# Query and ensure unique string shows up
docs = query(value)['results']
assert len(docs) == 1
# Query and ensure registration does show up
docs = query('category:registration AND ' + self.title)['results']
assert len(docs) == 1
# Retract registration
self.registration.retract_registration(self.user, '')
self.registration.retraction.state = Retraction.APPROVED
with run_celery_tasks():
self.registration.retraction.save()
self.registration.save()
self.registration.update_search()
# Query and ensure unique string in wiki doesn't show up
docs = query('category:registration AND "{}"'.format(wiki_content['home']))['results']
assert len(docs) == 0
# Query and ensure registration does show up
docs = query('category:registration AND ' + self.title)['results']
assert len(docs) == 1
@pytest.mark.enable_search
@pytest.mark.enable_enqueue_task
class TestPublicNodes(OsfTestCase):
def setUp(self):
with run_celery_tasks():
super().setUp()
self.user = factories.UserFactory(fullname='Doug Bogie')
self.title = 'Red Special'
self.consolidate_auth = Auth(user=self.user)
self.project = factories.ProjectFactory(
title=self.title,
description='',
creator=self.user,
is_public=True,
)
self.component = factories.NodeFactory(
parent=self.project,
description='',
title=self.title,
creator=self.user,
is_public=True
)
self.registration = factories.RegistrationFactory(
title=self.title,
description='',
creator=self.user,
is_public=True,
)
self.registration.archive_job.target_addons.clear()
self.registration.archive_job.status = 'SUCCESS'
self.registration.archive_job.save()
def test_make_private(self):
# Make project public, then private, and verify that it is not present
# in search.
with run_celery_tasks():
self.project.set_privacy('private')
docs = query('category:project AND ' + self.title)['results']
assert len(docs) == 0
with run_celery_tasks():
self.component.set_privacy('private')
docs = query('category:component AND ' + self.title)['results']
assert len(docs) == 0
def test_search_node_partial(self):
self.project.set_title('Blue Rider-Express', self.consolidate_auth)
with run_celery_tasks():
self.project.save()
find = query('Blue')['results']
assert len(find) == 1
def test_search_node_partial_with_sep(self):
self.project.set_title('Blue Rider-Express', self.consolidate_auth)
with run_celery_tasks():
self.project.save()
find = query('Express')['results']
assert len(find) == 1
def test_search_node_not_name(self):
self.project.set_title('Blue Rider-Express', self.consolidate_auth)
with run_celery_tasks():
self.project.save()
find = query('Green Flyer-Slow')['results']
assert len(find) == 0
def test_public_parent_title(self):
self.project.set_title('hello & world', self.consolidate_auth)
with run_celery_tasks():
self.project.save()
docs = query('category:component AND ' + self.title)['results']
assert len(docs) == 1
assert docs[0]['parent_title'] == 'hello & world'
assert docs[0]['parent_url']
def test_make_parent_private(self):
# Make parent of component, public, then private, and verify that the
# component still appears but doesn't link to the parent in search.
with run_celery_tasks():
self.project.set_privacy('private')
docs = query('category:component AND ' + self.title)['results']
assert len(docs) == 1
assert not docs[0]['parent_title']
assert not docs[0]['parent_url']
def test_delete_project(self):
with run_celery_tasks():
self.component.remove_node(self.consolidate_auth)
docs = query('category:component AND ' + self.title)['results']
assert len(docs) == 0
with run_celery_tasks():
self.project.remove_node(self.consolidate_auth)
docs = query('category:project AND ' + self.title)['results']
assert len(docs) == 0
def test_change_title(self):
title_original = self.project.title
with run_celery_tasks():
self.project.set_title(
'Blue Ordinary', self.consolidate_auth, save=True
)
docs = query('category:project AND ' + title_original)['results']
assert len(docs) == 0
docs = query('category:project AND ' + self.project.title)['results']
assert len(docs) == 1
def test_add_tags(self):
tags = ['stonecoldcrazy', 'just a poor boy', 'from-a-poor-family']
with run_celery_tasks():
for tag in tags:
docs = query(f'tags:"{tag}"')['results']
assert len(docs) == 0
self.project.add_tag(tag, self.consolidate_auth, save=True)
for tag in tags:
docs = query(f'tags:"{tag}"')['results']
assert len(docs) == 1
def test_remove_tag(self):
tags = ['stonecoldcrazy', 'just a poor boy', 'from-a-poor-family']
for tag in tags:
self.project.add_tag(tag, self.consolidate_auth, save=True)
self.project.remove_tag(tag, self.consolidate_auth, save=True)
docs = query(f'tags:"{tag}"')['results']
assert len(docs) == 0
def test_update_wiki(self):
"""Add text to a wiki page, then verify that project is found when
searching for wiki text.
"""
wiki_content = {
'home': 'Hammer to fall',
'swag': '#YOLO'
}