-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmaterials_controller_test.rb
More file actions
1701 lines (1453 loc) · 62.2 KB
/
Copy pathmaterials_controller_test.rb
File metadata and controls
1701 lines (1453 loc) · 62.2 KB
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
require 'test_helper'
class MaterialsControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers
include ActiveJob::TestHelper
setup do
mock_images
mock_biotools
@material = materials(:good_material)
@event = events(:one)
@collection = collections(:two)
@user = users(:regular_user)
@material.user_id = @user.id
@material.save!
@updated_material = {
title: 'New title',
description: 'New description',
url: 'http://new.url.com',
content_provider_id: ContentProvider.first.id
}
@failing_material = materials(:failing_material)
@failing_material.title = 'Fail!'
@monitor = @failing_material.create_link_monitor(url: @failing_material.url, code: 404, fail_count: 5)
end
# Tests
# INDEX, NEW, EDIT, CREATE, SHOW, BREADCRUMBS, TABS, API CHECKS
# INDEX TESTS
test 'should get index' do
get :index
assert_response :success
assert_select '#content h2', text: 'Training materials'
assert_not_nil assigns(:materials)
end
test 'should get index with solr enabled' do
with_settings(solr_enabled: true) do
Material.stub(:search_and_filter, MockSearch.new(Material.all)) do
get :index, params: { q: 'breakdance for beginners', keywords: 'dancing' }
assert_response :success
assert_not_empty assigns(:materials)
assert_select '.searchbox-sm form[action=?]', materials_path
end
end
end
test 'should get index as json' do
@material.scientific_topic_uris = ['http://edamontology.org/topic_0654']
@material.save!
get :index, params: { format: :json }
assert_response :success
assert_not_nil assigns(:materials)
assert_valid_legacy_json_response
assert_nothing_raised do
JSON.parse(response.body)
end
end
test 'should get index as json-api' do
@material.scientific_topic_uris = ['http://edamontology.org/topic_0654']
@material.save!
get :index, params: { format: :json_api }
assert_response :success
assert_not_nil assigns(:materials)
assert_valid_json_api_response
body = nil
assert_nothing_raised do
body = JSON.parse(response.body)
end
assert body['data'].any?
assert body['meta']['results-count'] > 0
assert body['meta'].key?('query')
assert body['meta'].key?('facets')
assert body['meta'].key?('available-facets')
assert_equal materials_path, body['links']['self']
end
test 'should get faceted index as json-api with search enabled' do
@material.scientific_topic_uris = ['http://edamontology.org/topic_0654']
@material.save!
with_settings(solr_enabled: true) do
Material.stub(:search_and_filter, MockSearch.new(Material.all)) do
get :index, params: { q: 'breakdance for beginners', keywords: 'dancing', format: :json_api }
assert_response :success
assert_not_nil assigns(:materials)
assert_valid_json_api_response
body = nil
assert_nothing_raised do
body = JSON.parse(response.body)
end
assert body['data'].any?
assert body['meta']['results-count'] > 0
assert_equal 'breakdance for beginners', body['meta']['query']
assert_includes body['meta']['facets']['keywords'], 'dancing'
assert body['meta']['available-facets'].keys.any?
assert body['meta']['available-facets'].values.any?
assert body['links']['self'].include?('dancing')
assert body['links']['self'].include?('breakdance+for+beginners')
end
end
end
test 'admins should be able to directly load failing records' do
sign_in users(:admin)
get :show, params: { id: @failing_material }
assert_response :success
end
test 'regular users should be able to directly load failing record and see warning' do
sign_in users(:regular_user)
@monitor.update(failed_at: DateTime.new(2023, 4, 2))
get :show, params: { id: @failing_material }
assert_response :success
assert_select '.broken-link-notice', text: /this material's URL.+since 2 April 2023/
end
# NEW TESTS
test 'should get new' do
sign_in users(:regular_user)
get :new
assert_response :success
end
test 'should get new page for logged in users only' do
# Redirect to login if not logged in
get :new
assert_response :redirect
sign_in users(:regular_user)
# Success for everyone else
get :new
assert_response :success
sign_in users(:admin)
get :new
assert_response :success
end
test 'should not get new page for basic users' do
sign_in users(:basic_user)
get :new
assert_response :forbidden
end
# EDIT TESTS
test 'should not get edit page for not logged in users' do
# Not logged in = Redirect to login
get :edit, params: { id: @material }
assert_redirected_to new_user_session_path
end
# logged in but insufficient permissions = ERROR
test 'should get edit for material owner' do
sign_in users(:regular_user)
get :edit, params: { id: @material }
assert_response :success
end
test 'should get edit for admin' do
# Owner of material logged in = SUCCESS
sign_in users(:admin)
get :edit, params: { id: @material }
assert_response :success
end
test 'should get edit for curator' do
sign_in users(:curator)
get :edit, params: { id: @material }
assert_response :success
end
test 'should get edit for content provider owner' do
sign_in users(:curator)
get :edit, params: { id: materials(:scraper_user_material) }
assert_response :success
end
test 'should not get edit page for non-owner user' do
# Administrator = SUCCESS
sign_in users(:another_regular_user)
get :edit, params: { id: @material }
assert :forbidden
end
test 'should get edit page for approved editor' do
@material.content_provider.add_editor users(:another_regular_user)
sign_in users(:another_regular_user)
get :edit, params: { id: @material }
assert_response :success
end
# CREATE TEST
test 'should create material for user' do
sign_in users(:regular_user)
assert_difference('Material.count') do
post :create, params: {
material: {
doi: @material.doi,
remote_created_date: @material.remote_created_date,
remote_updated_date: @material.remote_updated_date,
description: @material.description,
title: @material.title,
url: @material.url,
licence: @material.licence,
keywords: @material.keywords,
contact: @material.contact,
status: @material.status
}
}
end
assert_redirected_to material_path(assigns(:material))
@material.reload
assert_nil assigns(:material).space
end
test 'should create material with all optional attributes' do
# reference data
test_title = 'Test of create with optionals via post'
test_url = 'https://test.of.create/with/optionals_via_post'
test_material = materials(:material_with_optionals)
test_provider = content_providers(:portal_provider)
assert_not_nil test_material, 'missing reference material'
assert_not_nil test_provider, 'missing reference provider'
sign_in users(:regular_user)
assert_difference('Material.count') do
post :create, params: {
material: {
# required attributes
title: test_title,
url: test_url,
description: test_material.description,
licence: test_material.licence,
keywords: test_material.keywords,
contact: test_material.contact,
status: test_material.status,
# optional attributes
content_provider_id: test_provider.id,
events: test_material.events,
target_audience: test_material.target_audience,
resource_type: test_material.resource_type,
other_types: test_material.other_types,
version: test_material.version,
date_created: test_material.date_created,
date_modified: test_material.date_modified,
date_published: test_material.date_published,
doi: test_material.doi,
subsets: test_material.subsets,
authors: test_material.authors.map { |person| { role: person.role, name: person.name, orcid: person.orcid } },
contributors: test_material.contributors.map { |person| { role: person.role, name: person.name, orcid: person.orcid } },
prerequisites: test_material.prerequisites,
syllabus: test_material.syllabus,
learning_objectives: test_material.learning_objectives
}
}
end
assert_redirected_to material_path(assigns(:material))
# check response
post :check_exists, params: {
format: :json,
material: { title: test_title,
url: test_url,
content_provider_id: test_provider.id }
}
assert_response :success
assert_equal 'application/json; charset=utf-8', response.content_type, 'response content type not matched'
# required attributes
assert_equal test_title, JSON.parse(response.body)['title'], 'title not matched.'
assert_equal test_url, JSON.parse(response.body)['url'], 'description not matched.'
assert_equal test_material.description, JSON.parse(response.body)['description'], 'description not matched.'
assert_equal test_material.licence, JSON.parse(response.body)['licence'], 'licence not matched.'
assert_equal test_material.contact, JSON.parse(response.body)['contact'], 'contact not matched.'
assert_equal test_material.keywords, JSON.parse(response.body)['keywords'], 'keywords not matched'
assert_equal test_material.status, JSON.parse(response.body)['status'], 'status not matched'
# optional attributes
assert_equal test_material.doi, JSON.parse(response.body)['doi'], 'doi not matched.'
assert_equal test_provider.id, JSON.parse(response.body)['content_provider_id'], 'provider not matched'
assert_equal test_material.resource_type, JSON.parse(response.body)['resource_type'], 'resource_type not matched'
assert_equal test_material.version, JSON.parse(response.body)['version'], 'version not matched'
assert_equal test_material.other_types, JSON.parse(response.body)['other_types'], 'other_types not matched'
# assert_equal test_material.events, JSON.parse(response.body)['events'], 'events not matched'
assert_equal test_material.target_audience, JSON.parse(response.body)['target_audience'], 'target audience not matched'
response_authors = JSON.parse(response.body)['authors']
assert_equal test_material.authors.size, response_authors.size, 'authors count not matched'
response_authors.each_with_index do |author_json, i|
expected_author = test_material.authors[i]
assert_equal expected_author.display_name, author_json, "author #{i} name not matched"
end
# Contributors is now an array of objects with id, name, orcid
response_contributors = JSON.parse(response.body)['contributors']
assert_equal test_material.contributors.size, response_contributors.size, 'contributors count not matched'
response_contributors.each_with_index do |contributor_json, i|
expected_contributor = test_material.contributors[i]
assert_equal expected_contributor.display_name, contributor_json, "contributor #{i} name not matched"
end
assert_equal test_material.subsets, JSON.parse(response.body)['subsets'], 'subsets not matched'
assert_equal test_material.prerequisites, JSON.parse(response.body)['prerequisites'], 'prerequisites not matched'
assert_equal test_material.syllabus, JSON.parse(response.body)['syllabus'], 'syllabus not matched'
assert_equal test_material.learning_objectives, JSON.parse(response.body)['learning_objectives'],
'learning objectives not matched'
assert_equal test_material.date_created.to_fs('%Y-%m-%d'), JSON.parse(response.body)['date_created'],
'date created not matched'
assert_equal test_material.date_modified.to_fs('%Y-%m-%d'), JSON.parse(response.body)['date_modified'],
'date modified not matched'
assert_equal test_material.date_published.to_fs('%Y-%m-%d'), JSON.parse(response.body)['date_published'],
'date published not matched'
# reload
@material.reload
end
test 'should create material for admin' do
sign_in users(:admin)
assert_difference('Material.count') do
post :create, params: {
material: {
doi: @material.doi,
remote_created_date: @material.remote_created_date,
remote_updated_date: @material.remote_updated_date,
description: @material.description,
title: @material.title,
url: @material.url,
licence: @material.licence,
keywords: @material.keywords,
contact: @material.contact,
status: @material.status
}
}
end
assert_redirected_to material_path(assigns(:material))
end
test 'should not create material for non-logged in user' do
assert_no_difference('Material.count') do
post :create, params: {
material: {
doi: @material.doi,
remote_created_date: @material.remote_created_date,
remote_updated_date: @material.remote_updated_date,
description: @material.description,
title: @material.title,
url: @material.url,
licence: @material.licence,
keywords: @material.keywords,
contact: @material.contact,
status: @material.status
}
}
end
assert_redirected_to new_user_session_path
end
# SHOW TEST
test 'should show material' do
get :show, params: { id: @material }
assert_response :success
assert assigns(:material)
assert_select 'fa-commenting-o', count: 0
assert_select '.broken-link-notice', count: 0
assert_select '.archived-notice', count: 0
assert_select '.learning-path-navigation', count: 0
end
test 'should show material as json' do
@material.scientific_topic_uris = ['http://edamontology.org/topic_0654']
@material.authors = [{ name: 'Josiah Carberry', orcid: 'https://orcid.org/0000-0002-1825-0097' }, { name: 'Lara Croft' }]
@material.contributors = [{ name: 'Contri Butor', orcid: '0000-0002-1694-233X' }]
@material.events << events(:one)
@material.collections << collections(:one)
@material.save!
get :show, params: { id: @material, format: :json }
assert_response :success
assert assigns(:material)
assert_valid_legacy_json_response
assert_nothing_raised do
JSON.parse(response.body)
end
end
test 'should show material as json-api' do
@material.scientific_topic_uris = ['http://edamontology.org/topic_0654']
@material.events << @event
@material.collections << @collection
@material.save!
get :show, params: { id: @material, format: :json_api }
assert_response :success
assert assigns(:material)
assert_valid_json_api_response
body = nil
assert_nothing_raised do
body = JSON.parse(response.body)
end
assert_equal @material.title, body['data']['attributes']['title']
assert_equal @material.scientific_topic_uris.first, body['data']['attributes']['scientific-topics'].first['uri']
assert_equal material_path(assigns(:material)), body['data']['links']['self']
assert_equal @event.id.to_s, body['data']['relationships']['events']['data'][0]['id']
assert_equal @collection.id.to_s, body['data']['relationships']['collections']['data'][0]['id']
end
test 'should show material as bioschemas JSON-LD' do
@material.scientific_topic_uris = ['http://edamontology.org/topic_0654']
@material.events << @event
@material.collections << @collection
@material.save!
get :show, params: { id: @material, format: :jsonld }
assert_response :success
assert assigns(:material)
body = nil
assert_nothing_raised do
body = JSON.parse(response.body)
end
assert_equal 'http://schema.org', body['@context']
assert_equal 'LearningResource', body['@type']
assert_equal 'https://bioschemas.org/profiles/TrainingMaterial/1.0-RELEASE', body['dct:conformsTo']['@id']
assert_equal @material.title, body['name']
assert_equal @material.url, body['url']
assert_equal @material.scientific_topic_uris.first, body['about'].first['@id']
assert_equal material_url(assigns(:material), host: TeSS::Config.base_url), body['@id']
end
# UPDATE TEST
test 'should update material' do
sign_in @material.user
patch :update, params: { id: @material, material: @updated_material }
assert_redirected_to material_path(assigns(:material))
end
test 'should update material if curator' do
sign_in users(:curator)
assert_not_equal @material.user, users(:curator)
patch :update, params: { id: @material, material: @updated_material }
assert_redirected_to material_path(assigns(:material))
end
test 'should update material if content provider owner' do
material = materials(:scraper_user_material)
user = material.content_provider.user
assert_not_equal material.user, user
assert_equal material.content_provider.user, user
sign_in user
patch :update, params: { id: material, material: @updated_material }
assert_redirected_to material_path(assigns(:material))
end
test 'should not update material if not owner or curator etc.' do
sign_in users(:collaborative_user)
assert_not_equal @material.user, users(:collaborative_user)
patch :update, params: { id: @material, material: @updated_material }
assert_response :forbidden
end
test 'should update material if approved editor' do
@material.content_provider.add_editor users(:collaborative_user)
sign_in users(:collaborative_user)
assert_not_equal @material.user, users(:curator)
patch :update, params: { id: @material, material: @updated_material }
assert_redirected_to material_path(assigns(:material))
end
test 'should apply edit suggestions to material' do
material = materials(:material_with_suggestions)
sign_in users(:admin)
assert_empty material.scientific_topics
refute_nil material.edit_suggestion
get :show, params: { id: material }
assert_response :success
assert_select 'h2', text: 'Material with suggestions', count: 1
assert_select 'i.fa-commenting-o', count: 1
assert_select 'ul[data-uri=?]', 'http://edamontology.org/topic_3168', count: 1
assert_select 'ul[data-uri=?]', 'http://edamontology.org/topic_0199', count: 1
assert_no_difference('EditSuggestion.count') do
post :add_term, params: { id: material.id, field: 'scientific_topics', uri: 'http://edamontology.org/topic_3168' }
end
assert_response :success
assert_difference('EditSuggestion.count', -1) do
post :add_term, params: { id: material.id, field: 'scientific_topics', uri: 'http://edamontology.org/topic_0199' }
end
assert_response :success
assert_nil material.reload.edit_suggestion
end
# DESTROY TEST
test 'should destroy material owned by user' do
sign_in users(:regular_user)
assert_difference('Material.count', -1) do
delete :destroy, params: { id: @material }
end
assert_redirected_to materials_path
end
test 'should destroy material when administrator' do
sign_in users(:admin)
assert_difference('Material.count', -1) do
delete :destroy, params: { id: @material }
end
assert_redirected_to materials_path
end
test 'should destroy material if approved editor' do
@material.content_provider.add_editor users(:another_regular_user)
sign_in users(:another_regular_user)
assert_difference('Material.count', -1) do
delete :destroy, params: { id: @material }
end
assert_redirected_to materials_path
end
test 'should not destroy material not owned by user' do
sign_in users(:another_regular_user)
assert_no_difference('Material.count') do
delete :destroy, params: { id: @material }
end
assert_response :forbidden
end
test 'should destroy material when curator' do
sign_in users(:curator)
assert_difference('Material.count', -1) do
delete :destroy, params: { id: @material }
end
assert_redirected_to materials_path
end
test 'should destroy material when content provider owner' do
material = materials(:scraper_user_material)
user = material.content_provider.user
sign_in user
assert_difference('Material.count', -1) do
delete :destroy, params: { id: material }
end
assert_redirected_to materials_path
end
# CONTENT TESTS
# BREADCRUMBS
test 'breadcrumbs for materials index' do
get :index
assert_response :success
assert_select 'div.breadcrumbs', text: /Home/, count: 1 do
assert_select 'a[href=?]', root_path, count: 1
assert_select 'li[class=active]', text: /Materials/, count: 1
end
end
test 'breadcrumbs for showing material' do
get :show, params: { id: @material }
assert_response :success
assert_select 'div.breadcrumbs', text: /Home/, count: 1 do
assert_select 'a[href=?]', root_path, count: 1
assert_select 'li', text: /Materials/, count: 1 do
assert_select 'a[href=?]', materials_url, count: 1
end
assert_select 'li[class=active]', text: /#{@material.title}/, count: 1
end
end
test 'breadcrumbs for editing material' do
sign_in users(:regular_user)
get :edit, params: { id: @material }
assert_response :success
assert_select 'div.breadcrumbs', text: /Home/, count: 1 do
assert_select 'a[href=?]', root_path, count: 1
assert_select 'li', text: /Materials/, count: 1 do
assert_select 'a[href=?]', materials_url, count: 1
end
assert_select 'li', text: /#{@material.title}/, count: 1 do
assert_select 'a[href=?]', material_url(@material), count: 1
end
assert_select 'li[class=active]', text: /Edit/, count: 1
end
end
test 'breadcrumbs for creating new material' do
sign_in users(:regular_user)
get :new
assert_response :success
assert_select 'div.breadcrumbs', text: /Home/, count: 1 do
assert_select 'a[href=?]', root_path, count: 1
assert_select 'li', text: /Materials/, count: 1 do
assert_select 'a[href=?]', materials_url, count: 1
end
assert_select 'li[class=active]', text: /New/, count: 1
end
end
# OTHER CONTENT
test 'material has correct layout' do
get :show, params: { id: @material }
assert_response :success
assert_select 'h2', text: @material.title # Has Title
assert_select 'a.btn', text: 'View material', count: 1 do
assert_select 'a[href=?]', @material.url, count: 1
end
# Should not show when not logged in
assert_select 'a.btn[href=?]', edit_material_path(@material), count: 0 # No Edit
assert_select 'a.btn[href=?]', material_path(@material), count: 0 # No Edit
end
test 'do not show action buttons when not owner or admin' do
sign_in users(:another_regular_user)
get :show, params: { id: @material }
assert_select 'a.btn[href=?]', edit_material_path(@material), count: 0 # No Edit
assert_select 'a.btn[href=?]', material_path(@material), count: 0 # No Edit
end
test 'show action buttons when owner' do
sign_in users(:regular_user)
get :show, params: { id: @material }
assert_select 'a.btn[href=?]', edit_material_path(@material), count: 1
assert_select 'a.btn[href=?]', material_path(@material), text: 'Delete', count: 1
end
test 'show action buttons when admin' do
sign_in users(:admin)
get :show, params: { id: @material }
assert_select 'a.btn[href=?]', edit_material_path(@material), count: 1
assert_select 'a.btn[href=?]', material_path(@material), text: 'Delete', count: 1
end
test 'show action buttons when approved editor' do
@material.content_provider.add_editor users(:another_regular_user)
sign_in users(:another_regular_user)
get :show, params: { id: @material }
assert_select 'a.btn[href=?]', edit_material_path(@material), count: 1
assert_select 'a.btn[href=?]', material_path(@material), text: 'Delete', count: 1
end
# API Actions
test 'should find existing material by title and content provider' do
post :check_exists, params: {
format: :json,
material: { title: @material.title,
url: 'whatever.com',
content_provider_id: @material.content_provider_id }
}
assert_response :success
assert_equal(JSON.parse(response.body)['id'], @material.id)
end
test 'should find existing material by url' do
post :check_exists, params: {
format: :json,
material: { title: 'whatever',
url: @material.url,
content_provider_id: @material.content_provider_id }
}
assert_response :success
assert_equal(JSON.parse(response.body)['id'], @material.id)
end
test 'should find existing material by url without provider' do
post :check_exists, params: { format: :json, material: { title: 'whatever', url: @material.url } }
assert_response :success
assert_equal(JSON.parse(response.body)['url'], @material.url)
assert_equal(JSON.parse(response.body)['id'], @material.id)
end
test 'should find existing material by url and given content provider' do
provider1 = content_providers(:iann)
provider2 = content_providers(:two)
e1 = provider1.materials.create!(title: 'another material', url: @material.url, description: '123', user: users(:regular_user))
e2 = provider2.materials.create!(title: 'a third material', url: @material.url, description: '123', user: users(:regular_user))
post :check_exists, params: { format: :json, material: { url: @material.url, content_provider_id: provider1.id } }
assert_response :success
assert_equal(JSON.parse(response.body)['id'], e1.id)
post :check_exists, params: { format: :json, material: { url: @material.url, content_provider_id: provider2.id } }
assert_response :success
assert_equal(JSON.parse(response.body)['id'], e2.id)
end
test 'should return nothing when material does not exist' do
post :check_exists, params: {
format: :json,
material: { url: 'http://no-such-url.com' }
}
assert_response :success
assert_equal '{}', response.body
end
test 'should render properly when no url supplied' do
post :check_exists, params: {
format: :json,
material: { url: nil }
}
assert_response :success
assert_equal '{}', response.body
end
test 'should display filters on index' do
get :index
assert_select 'h4.nav-heading', text: /Content provider/, count: 0
assert_select 'li.masonry-brick', count: Material.count
end
test 'should create new material through API' do
scraper_role = Role.fetch('scraper_user')
scraper_user = User.where(role_id: scraper_role.id).first
material_title = 'horse'
assert scraper_user
assert_difference('Material.count') do
post :create, params: {
user_token: scraper_user.authentication_token,
user_email: scraper_user.email,
material: {
title: material_title,
url: 'http://horse.com',
description: 'I love horses',
contact: 'default contact',
doi: 'https://doi.org/10.1001/RSE.2.190',
licence: 'CC-BY-4.0',
keywords: %w[scraped through api],
status: 'active'
},
format: 'json'
}
end
assert_equal material_title, JSON.parse(response.body)['title']
end
test 'should not create new material without valid authentication token' do
scraper_role = Role.fetch('scraper_user')
scraper_user = User.where(role_id: scraper_role.id).first
assert scraper_user
assert_no_difference('Material.count') do
post :create, params: {
user_token: 'made up authentication token',
user_email: scraper_user.email,
material: {
title: 'material_title',
url: 'http://horse.com',
description: 'All about horses',
contact: 'default contact',
doi: 'https://doi.org/10.1001/RSE.2.190',
licence: 'CC-BY-4.0',
keywords: %( invalid authtoken ),
status: 'active'
},
format: 'json'
}
end
assert_response 401
end
test 'should update existing material through API' do
user = users(:scraper_user)
material = materials(:scraper_user_material)
new_title = 'totally new title'
assert_no_difference('Material.count') do
patch :update, params: {
user_token: user.authentication_token,
user_email: user.email,
material: {
title: new_title,
url: material.url,
description: material.description
},
id: material.id,
format: 'json'
}
end
assert_not_equal material.title, JSON.parse(response.body)['title']
assert_equal new_title, JSON.parse(response.body)['title']
end
test 'should not update non scraper owner through API' do
user = users(:regular_user)
other_user = users(:another_regular_user)
material = user.materials.first
new_title = 'totally new title'
assert_no_difference('Material.count') do
patch :update, params: {
user_token: other_user.authentication_token,
user_email: other_user.email,
material: {
title: new_title,
url: material.url,
description: material.description
},
id: material.id,
format: 'json'
}
end
assert_response 401
end
test 'should add material to multiple collections' do
sign_in @material.user
collection1 = collections(:one)
collection1_material_count = collection1.materials.count
collection2 = collections(:two)
@material.collections = []
@material.save!
assert_difference('@material.collections.count', 2) do
post :update_collections, params: {
id: @material.id,
material: {
collection_ids: [collection1.id, collection2.id]
}
}
end
assert_in_delta(collection1.materials.count, collection1_material_count, 1)
end
test 'should remove material from collections' do
sign_in @material.user
collection1 = collections(:one)
collection1_material_count = collection1.materials.count
collection2 = collections(:two)
@material.collections << [collection1, collection2]
@material.save
assert_difference('@material.collections.count', -2) do
post :update_collections, params: {
id: @material.id,
material: {
collection_ids: ['']
}
}
end
assert_in_delta(collection1.materials.count, collection1_material_count, 1)
end
test 'should add external resource to material' do
sign_in @material.user
assert_difference('ExternalResource.count', 1) do
patch :update, params: {
id: @material,
material: {
title: 'New title',
description: 'New description',
url: 'http://new.url.com',
content_provider_id: ContentProvider.first.id,
external_resources_attributes: { '1' => { title: 'Cool link', url: 'https://tess.elixir-uk.org/', _destroy: '0' } }
}
}
end
assert_redirected_to material_path(assigns(:material))
resource = assigns(:material).external_resources.first
assert_equal 'Cool link', resource.title
assert_equal 'https://tess.elixir-uk.org/', resource.url
end
test 'should remove external resource from material' do
material = materials(:material_with_external_resource)
resource = material.external_resources.first
count = material.external_resources.count
sign_in material.user
assert_difference('ExternalResource.count', -1) do
patch :update, params: {
id: material,
material: {
title: 'New title',
description: 'New description',
url: 'http://new.url.com',
content_provider_id: ContentProvider.first.id,
external_resources_attributes: { '0' => { id: resource.id, _destroy: '1' } }
}
}
end
assert_redirected_to material_path(assigns(:material))
assert_equal count - 1, assigns(:material).external_resources.count
end
test 'should modify external resource from material' do
material = materials(:material_with_external_resource)
resource = material.external_resources.first
sign_in material.user
assert_no_difference('ExternalResource.count') do
patch :update, params: {
id: material,
material: {
title: 'New title',
description: 'New description',
url: 'http://new.url.com',
content_provider_id: ContentProvider.first.id,
external_resources_attributes: { '1' => { id: resource.id, title: 'Cool link',
url: 'http://www.reddit.com', _destroy: '0' } }
}
}
end
assert_redirected_to material_path(assigns(:material))
assert_equal 'Cool link', resource.reload.title
assert_equal 'http://www.reddit.com', resource.url
end
# TODO: SOLR tests will not run on TRAVIS. Explore stratergy for testing solr
# test 'should return matching materials' do
# get 'index', :format => :json, :q => 'training'
# assert_response :success
# assert response.body.size > 0
# end
#
# test 'should return no matching materials' do
# get 'index', :format => :json, :q => 'kdfsajfklasdjfljsdfljdsfjncvmn'
# assert_response :success
# assert_equal(response.body,'[]')
# end
test 'finds multiple preferred labels' do
topic_one = Edam::Ontology.instance.lookup('http://edamontology.org/topic_0154')
topic_two = Edam::Ontology.instance.lookup('http://edamontology.org/topic_0078')
topics = [topic_one.preferred_label, topic_two.preferred_label]
@material.scientific_topic_names = topics
assert_not_empty @material.scientific_topics
assert_equal [topic_one, topic_two], @material.scientific_topics
end
test 'finds single preferred label' do
topic_one = Edam::Ontology.instance.lookup('http://edamontology.org/topic_0154')
topics = topic_one.preferred_label
@material.scientific_topic_names = topics
assert_not_empty @material.scientific_topics
assert_equal [topic_one], @material.scientific_topics
end
test 'find scientific topic that has an exact synonym of parameter' do
synonym_topic = Edam::Ontology.instance.lookup('http://edamontology.org/topic_0092')
topics = synonym_topic.has_exact_synonym
@material.scientific_topic_names = topics
assert_equal [synonym_topic], @material.scientific_topics
end
test 'find scientific topic that is a narrow synonym of parameter' do
narrow_topic = Edam::Ontology.instance.lookup('http://edamontology.org/topic_3957')
topics = narrow_topic.has_narrow_synonym
@material.scientific_topic_names = topics
assert_equal [narrow_topic], @material.scientific_topics
end
test 'set topics to nil if empty array passed' do
topics = []
@material.scientific_topic_names = topics
assert_empty @material.scientific_topics
end
test 'should sanitize descriptions when creating material' do
sign_in @user
assert_difference('Material.count', 1) do
post :create, params: {
material: { description: '<b>hi</b><script>alert("hi!");</script>',
title: 'Insanity',
url: 'http://www.example.com/sanity/0',
doi: 'https://doi.org/10.1100/RSE.2019.23',
licence: 'CC-BY-4.0',
keywords: %w[insanity sanitized sanitary],
contact: 'default contact',
status: 'development' }
}
end
assert_redirected_to material_path(assigns(:material))
assert_equal 'hi', assigns(:material).description
end
test 'should log parameter changes when updating a material' do
sign_in @material.user
@material.activities.destroy_all
# 5 = 4 for parameters + 1 for update
assert_difference('PublicActivity::Activity.count', 5) do
patch :update, params: { id: @material, material: @updated_material }
end
assert_equal 1, @material.activities.where(key: 'material.update').count
assert_equal 4, @material.activities.where(key: 'material.update_parameter').count
parameters = @material.activities.where(key: 'material.update_parameter').map(&:parameters)