Merge branch '3787-docker-docs' closes #3787
[arvados.git] / apps / workbench / test / integration / projects_test.rb
1 require 'integration_helper'
2 require 'selenium-webdriver'
3 require 'headless'
4
5 class ProjectsTest < ActionDispatch::IntegrationTest
6   setup do
7     Capybara.current_driver = Capybara.javascript_driver
8   end
9
10   test 'Check collection count for A Project in the tab pane titles' do
11     project_uuid = api_fixture('groups')['aproject']['uuid']
12     visit page_with_token 'active', '/projects/' + project_uuid
13     collection_count = page.all("[data-pk*='collection']").count
14     assert_selector '#Data_collections-tab span', text: "(#{collection_count})"
15   end
16
17   test 'Find a project and edit its description' do
18     visit page_with_token 'active', '/'
19     find("#projects-menu").click
20     find(".dropdown-menu a", text: "A Project").click
21     within('.container-fluid', text: api_fixture('groups')['aproject']['name']) do
22       find('span', text: api_fixture('groups')['aproject']['name']).click
23       within('.arv-description-as-subtitle') do
24         find('.fa-pencil').click
25         find('.editable-input textarea').set('I just edited this.')
26         find('.editable-submit').click
27       end
28       wait_for_ajax
29     end
30     visit current_path
31     assert(find?('.container-fluid', text: 'I just edited this.'),
32            "Description update did not survive page refresh")
33   end
34
35   test 'Find a project and edit description to textile description' do
36     visit page_with_token 'active', '/'
37     find("#projects-menu").click
38     find(".dropdown-menu a", text: "A Project").click
39     within('.container-fluid', text: api_fixture('groups')['aproject']['name']) do
40       find('span', text: api_fixture('groups')['aproject']['name']).click
41       within('.arv-description-as-subtitle') do
42         find('.fa-pencil').click
43         find('.editable-input textarea').set('<p>*Textile description for A project* - "take me home":/ </p><p>And a new paragraph in description.</p>')
44         find('.editable-submit').click
45       end
46       wait_for_ajax
47     end
48
49     # visit project page
50     visit current_path
51     assert(has_no_text?('.container-fluid', text: '*Textile description for A project*'),
52            "Description is not rendered properly")
53     assert(find?('.container-fluid', text: 'Textile description for A project'),
54            "Description update did not survive page refresh")
55     assert(find?('.container-fluid', text: 'And a new paragraph in description'),
56            "Description did not contain the expected new paragraph")
57     assert(page.has_link?("take me home"), "link not found in description")
58
59     click_link 'take me home'
60
61     # now in dashboard
62     assert(page.has_text?('Active pipelines'), 'Active pipelines - not found on dashboard')
63   end
64
65   test 'Find a project and edit description to html description' do
66     visit page_with_token 'active', '/'
67     find("#projects-menu").click
68     find(".dropdown-menu a", text: "A Project").click
69     within('.container-fluid', text: api_fixture('groups')['aproject']['name']) do
70       find('span', text: api_fixture('groups')['aproject']['name']).click
71       within('.arv-description-as-subtitle') do
72         find('.fa-pencil').click
73         find('.editable-input textarea').set('<br>Textile description for A project</br> - <a href="/">take me home</a>')
74         find('.editable-submit').click
75       end
76       wait_for_ajax
77     end
78     visit current_path
79     assert(find?('.container-fluid', text: 'Textile description for A project'),
80            "Description update did not survive page refresh")
81     assert(!find?('.container-fluid', text: '<br>Textile description for A project</br>'),
82            "Textile description is displayed with uninterpreted formatting characters")
83     assert(page.has_link?("take me home"),"link not found in description")
84     click_link 'take me home'
85     assert page.has_text?('Active pipelines')
86   end
87
88   test 'Find a project and edit description to textile description with link to object' do
89     visit page_with_token 'active', '/'
90     find("#projects-menu").click
91     find(".dropdown-menu a", text: "A Project").click
92     within('.container-fluid', text: api_fixture('groups')['aproject']['name']) do
93       find('span', text: api_fixture('groups')['aproject']['name']).click
94       within('.arv-description-as-subtitle') do
95         find('.fa-pencil').click
96         find('.editable-input textarea').set('*Textile description for A project* - "go to sub-project":' + api_fixture('groups')['asubproject']['uuid'] + "'")
97         find('.editable-submit').click
98       end
99       wait_for_ajax
100     end
101     visit current_path
102     assert(find?('.container-fluid', text: 'Textile description for A project'),
103            "Description update did not survive page refresh")
104     assert(!find?('.container-fluid', text: '*Textile description for A project*'),
105            "Textile description is displayed with uninterpreted formatting characters")
106     assert(page.has_link?("go to sub-project"), "link not found in description")
107     click_link 'go to sub-project'
108     assert(page.has_text?(api_fixture('groups')['asubproject']['name']), 'sub-project name not found after clicking link')
109   end
110
111   test 'Add a new name, then edit it, without creating a duplicate' do
112     project_uuid = api_fixture('groups')['aproject']['uuid']
113     specimen_uuid = api_fixture('traits')['owned_by_aproject_with_no_name']['uuid']
114     visit page_with_token 'active', '/projects/' + project_uuid
115     click_link 'Other objects'
116     within '.selection-action-container' do
117       # Wait for the tab to load:
118       assert_selector 'tr[data-kind="arvados#trait"]'
119       within first('tr', text: 'Trait') do
120         find(".fa-pencil").click
121         find('.editable-input input').set('Now I have a name.')
122         find('.glyphicon-ok').click
123         assert_selector '.editable', text: 'Now I have a name.'
124         find(".fa-pencil").click
125         find('.editable-input input').set('Now I have a new name.')
126         find('.glyphicon-ok').click
127       end
128       wait_for_ajax
129       assert_selector '.editable', text: 'Now I have a new name.'
130     end
131     visit current_path
132     click_link 'Other objects'
133     within '.selection-action-container' do
134       find '.editable', text: 'Now I have a new name.'
135       page.assert_no_selector '.editable', text: 'Now I have a name.'
136     end
137   end
138
139   test 'Create a project and move it into a different project' do
140     visit page_with_token 'active', '/projects'
141     find("#projects-menu").click
142     find(".dropdown-menu a", text: "Home").click
143     find('.btn', text: "Add a subproject").click
144
145     # within('.editable', text: 'New project') do
146     within('h2') do
147       find('.fa-pencil').click
148       find('.editable-input input').set('Project 1234')
149       find('.glyphicon-ok').click
150     end
151     wait_for_ajax
152
153     visit '/projects'
154     find("#projects-menu").click
155     find(".dropdown-menu a", text: "Home").click
156     find('.btn', text: "Add a subproject").click
157     within('h2') do
158       find('.fa-pencil').click
159       find('.editable-input input').set('Project 5678')
160       find('.glyphicon-ok').click
161     end
162     wait_for_ajax
163
164     click_link 'Move project...'
165     find('.selectable', text: 'Project 1234').click
166     find('.modal-footer a,button', text: 'Move').click
167     wait_for_ajax
168
169     # Wait for the page to refresh and show the new parent in Sharing panel
170     click_link 'Sharing'
171     assert(page.has_link?("Project 1234"),
172            "Project 5678 should now be inside project 1234")
173   end
174
175   def show_project_using(auth_key, proj_key='aproject')
176     project_uuid = api_fixture('groups')[proj_key]['uuid']
177     visit(page_with_token(auth_key, "/projects/#{project_uuid}"))
178     assert(page.has_text?("A Project"), "not on expected project page")
179   end
180
181   def share_rows
182     find('#project_sharing').all('tr')
183   end
184
185   def add_share_and_check(share_type, name)
186     assert(page.has_no_text?(name), "project is already shared with #{name}")
187     start_share_count = share_rows.size
188     click_on("Share with #{share_type}")
189     within(".modal-container") do
190       # Order is important here: we should find something that appears in the
191       # modal before we make any assertions about what's not in the modal.
192       # Otherwise, the not-included assertions might falsely pass because
193       # the modal hasn't loaded yet.
194       find(".selectable", text: name).click
195       assert(has_no_selector?(".modal-dialog-preview-pane"),
196              "preview pane available in sharing dialog")
197       assert_raises(Capybara::ElementNotFound,
198                     "Projects pulldown available from sharing dialog") do
199         click_on "All projects"
200       end
201       click_on "Add"
202     end
203     using_wait_time(Capybara.default_wait_time * 3) do
204       assert(page.has_link?(name),
205              "new share was not added to sharing table")
206       assert_equal(start_share_count + 1, share_rows.size,
207                    "new share did not add row to sharing table")
208     end
209   end
210
211   def modify_share_and_check(name)
212     start_rows = share_rows
213     link_row = start_rows.select { |row| row.has_text?(name) }
214     assert_equal(1, link_row.size, "row with new permission not found")
215     within(link_row.first) do
216       click_on("Read")
217       select("Write", from: "share_change_level")
218       click_on("editable-submit")
219       assert(has_link?("Write"),
220              "failed to change access level on new share")
221       click_on "Revoke"
222     end
223     using_wait_time(Capybara.default_wait_time * 3) do
224       assert(page.has_no_text?(name),
225              "new share row still exists after being revoked")
226       assert_equal(start_rows.size - 1, share_rows.size,
227                    "revoking share did not remove row from sharing table")
228     end
229   end
230
231   test "project viewer can't see project sharing tab" do
232     show_project_using("project_viewer")
233     assert(page.has_no_link?("Sharing"),
234            "read-only project user sees sharing tab")
235   end
236
237   test "project owner can manage sharing for another user" do
238     add_user = api_fixture('users')['future_project_user']
239     new_name = ["first_name", "last_name"].map { |k| add_user[k] }.join(" ")
240
241     show_project_using("active")
242     click_on "Sharing"
243     add_share_and_check("users", new_name)
244     modify_share_and_check(new_name)
245   end
246
247   test "project owner can manage sharing for another group" do
248     new_name = api_fixture('groups')['future_project_viewing_group']['name']
249
250     show_project_using("active")
251     click_on "Sharing"
252     add_share_and_check("groups", new_name)
253     modify_share_and_check(new_name)
254   end
255
256   test "'share with group' listing does not offer projects" do
257     show_project_using("active")
258     click_on "Sharing"
259     click_on "Share with groups"
260     good_uuid = api_fixture("groups")["private"]["uuid"]
261     assert(page.has_selector?(".selectable[data-object-uuid=\"#{good_uuid}\"]"),
262            "'share with groups' listing missing owned user group")
263     bad_uuid = api_fixture("groups")["asubproject"]["uuid"]
264     assert(page.has_no_selector?(".selectable[data-object-uuid=\"#{bad_uuid}\"]"),
265            "'share with groups' listing includes project")
266   end
267
268   [
269     ['Move',api_fixture('collections')['collection_to_move_around_in_aproject'],
270       api_fixture('groups')['aproject'],api_fixture('groups')['asubproject']],
271     ['Remove',api_fixture('collections')['collection_to_move_around_in_aproject'],
272       api_fixture('groups')['aproject']],
273     ['Copy',api_fixture('collections')['collection_to_move_around_in_aproject'],
274       api_fixture('groups')['aproject'],api_fixture('groups')['asubproject']],
275     ['Remove',api_fixture('collections')['collection_in_aproject_with_same_name_as_in_home_project'],
276       api_fixture('groups')['aproject'],nil,true],
277   ].each do |action, my_collection, src, dest=nil, expect_name_change=nil|
278     test "selection #{action} #{expect_name_change} for project" do
279       perform_selection_action src, dest, my_collection, action
280
281       case action
282       when 'Copy'
283         assert page.has_text?(my_collection['name']), 'Collection not found in src project after copy'
284         visit page_with_token 'active', '/'
285         find("#projects-menu").click
286         find(".dropdown-menu a", text: dest['name']).click
287         assert page.has_text?(my_collection['name']), 'Collection not found in dest project after copy'
288
289         # now remove it from destination project to restore to original state
290         perform_selection_action dest, nil, my_collection, 'Remove'
291       when 'Move'
292         assert page.has_no_text?(my_collection['name']), 'Collection still found in src project after move'
293         visit page_with_token 'active', '/'
294         find("#projects-menu").click
295         find(".dropdown-menu a", text: dest['name']).click
296         assert page.has_text?(my_collection['name']), 'Collection not found in dest project after move'
297
298         # move it back to src project to restore to original state
299         perform_selection_action dest, src, my_collection, action
300       when 'Remove'
301         assert page.has_no_text?(my_collection['name']), 'Collection still found in src project after remove'
302         visit page_with_token 'active', '/'
303         find("#projects-menu").click
304         find(".dropdown-menu a", text: "Home").click
305         assert page.has_text?(my_collection['name']), 'Collection not found in home project after remove'
306         if expect_name_change
307           assert page.has_text?(my_collection['name']+' removed from ' + src['name']),
308             'Collection with update name is not found in home project after remove'
309         end
310       end
311     end
312   end
313
314   def perform_selection_action src, dest, item, action
315     visit page_with_token 'active', '/'
316     find("#projects-menu").click
317     find(".dropdown-menu a", text: src['name']).click
318     assert page.has_text?(item['name']), 'Collection not found in src project'
319
320     within('tr', text: item['name']) do
321       find('input[type=checkbox]').click
322     end
323
324     click_button 'Selection...'
325
326     within('.selection-action-container') do
327       assert page.has_text?("Compare selected"), "Compare selected link text not found"
328       assert page.has_link?("Copy selected"), "Copy selected link not found"
329       assert page.has_link?("Move selected"), "Move selected link not found"
330       assert page.has_link?("Remove selected"), "Remove selected link not found"
331
332       click_link "#{action} selected"
333     end
334
335     # select the destination project if a Copy or Move action is being performed
336     if action == 'Copy' || action == 'Move'
337       within(".modal-container") do
338         find('.selectable', text: dest['name']).click
339         find('.modal-footer a,button', text: action).click
340         wait_for_ajax
341       end
342     end
343   end
344
345   # Test copy action state. It should not be available when a subproject is selected.
346   test "copy action is disabled when a subproject is selected" do
347     my_project = api_fixture('groups')['aproject']
348     my_collection = api_fixture('collections')['collection_to_move_around_in_aproject']
349     my_subproject = api_fixture('groups')['asubproject']
350
351     # verify that selection options are disabled on the project until an item is selected
352     visit page_with_token 'active', '/'
353     find("#projects-menu").click
354     find(".dropdown-menu a", text: my_project['name']).click
355
356     click_button 'Selection...'
357     within('.selection-action-container') do
358       page.assert_selector 'li.disabled', text: 'Create new collection with selected collections'
359       page.assert_selector 'li.disabled', text: 'Compare selected'
360       page.assert_selector 'li.disabled', text: 'Copy selected'
361       page.assert_selector 'li.disabled', text: 'Move selected'
362       page.assert_selector 'li.disabled', text: 'Remove selected'
363     end
364
365     # select collection and verify links are enabled
366     visit page_with_token 'active', '/'
367     find("#projects-menu").click
368     find(".dropdown-menu a", text: my_project['name']).click
369     assert page.has_text?(my_collection['name']), 'Collection not found in project'
370
371     within('tr', text: my_collection['name']) do
372       find('input[type=checkbox]').click
373     end
374
375     click_button 'Selection...'
376     within('.selection-action-container') do
377       page.assert_no_selector 'li.disabled', text: 'Create new collection with selected collections'
378       page.assert_selector 'li', text: 'Create new collection with selected collections'
379       page.assert_selector 'li.disabled', text: 'Compare selected'
380       page.assert_no_selector 'li.disabled', text: 'Copy selected'
381       page.assert_selector 'li', text: 'Copy selected'
382       page.assert_no_selector 'li.disabled', text: 'Move selected'
383       page.assert_selector 'li', text: 'Move selected'
384       page.assert_no_selector 'li.disabled', text: 'Remove selected'
385       page.assert_selector 'li', text: 'Remove selected'
386     end
387
388     # select subproject and verify that copy action is disabled
389     visit page_with_token 'active', '/'
390     find("#projects-menu").click
391     find(".dropdown-menu a", text: my_project['name']).click
392
393     click_link 'Subprojects'
394     assert page.has_text?(my_subproject['name']), 'Subproject not found in project'
395
396     within('tr', text: my_subproject['name']) do
397       find('input[type=checkbox]').click
398     end
399
400     click_button 'Selection...'
401     within('.selection-action-container') do
402       page.assert_selector 'li.disabled', text: 'Create new collection with selected collections'
403       page.assert_selector 'li.disabled', text: 'Compare selected'
404       page.assert_selector 'li.disabled', text: 'Copy selected'
405       page.assert_no_selector 'li.disabled', text: 'Move selected'
406       page.assert_selector 'li', text: 'Move selected'
407       page.assert_no_selector 'li.disabled', text: 'Remove selected'
408       page.assert_selector 'li', text: 'Remove selected'
409     end
410
411     # select subproject and a collection and verify that copy action is still disabled
412     visit page_with_token 'active', '/'
413     find("#projects-menu").click
414     find(".dropdown-menu a", text: my_project['name']).click
415
416     click_link 'Subprojects'
417     assert page.has_text?(my_subproject['name']), 'Subproject not found in project'
418
419     within('tr', text: my_subproject['name']) do
420       find('input[type=checkbox]').click
421     end
422
423     click_link 'Data collections'
424     assert page.has_text?(my_collection['name']), 'Collection not found in project'
425
426     within('tr', text: my_collection['name']) do
427       find('input[type=checkbox]').click
428     end
429
430     click_button 'Selection...'
431     within('.selection-action-container') do
432       page.assert_selector 'li.disabled', text: 'Create new collection with selected collections'
433       page.assert_selector 'li.disabled', text: 'Compare selected'
434       page.assert_selector 'li.disabled', text: 'Copy selected'
435       page.assert_no_selector 'li.disabled', text: 'Move selected'
436       page.assert_selector 'li', text: 'Move selected'
437       page.assert_no_selector 'li.disabled', text: 'Remove selected'
438       page.assert_selector 'li', text: 'Remove selected'
439     end
440   end
441
442   [
443     ['active', true],
444     ['project_viewer', false],
445   ].each do |user, expect_collection_in_aproject|
446     test "combine selected collections into new collection #{user} #{expect_collection_in_aproject}" do
447       my_project = api_fixture('groups')['aproject']
448       my_collection = api_fixture('collections')['collection_to_move_around_in_aproject']
449
450       visit page_with_token user, '/'
451       find("#projects-menu").click
452       find(".dropdown-menu a", text: my_project['name']).click
453       assert page.has_text?(my_collection['name']), 'Collection not found in project'
454
455       within('tr', text: my_collection['name']) do
456         find('input[type=checkbox]').click
457       end
458
459       click_button 'Selection...'
460       within('.selection-action-container') do
461         click_link 'Create new collection with selected collections'
462       end
463
464       # now in the new collection page
465       if expect_collection_in_aproject
466         assert page.has_text?("Created new collection in the project #{my_project['name']}"),
467                               'Not found flash message that new collection is created in aproject'
468       else
469         assert page.has_text?("Created new collection in your Home project"),
470                               'Not found flash message that new collection is created in Home project'
471       end
472       assert page.has_text?('Content hash'), 'Not found content hash in collection page'
473     end
474   end
475
476   [
477     ["jobs", "/jobs"],
478     ["pipelines", "/pipeline_instances"],
479     ["collections", "/collections"]
480   ].each do |target,path|
481     test "Test dashboard button all #{target}" do
482       visit page_with_token 'active', '/'
483       click_link "All #{target}"
484       assert_equal path, current_path
485     end
486   end
487
488   [
489     ['project with 10 collections', 10],
490     ['project with 201 collections', 201], # two pages of data
491   ].each do |project_name, amount|
492     test "scroll collections tab for #{project_name} with #{amount} objects" do
493       headless = Headless.new
494       headless.start
495       Capybara.current_driver = :selenium
496
497       visit page_with_token 'user1_with_load'
498
499       find("#projects-menu").click
500       find(".dropdown-menu a", text: project_name).click
501
502       my_collections = []
503       for i in 1..amount
504         my_collections << "Collection_#{i}"
505       end
506
507       # verify Data collections scroll
508       assert(page.has_text?("Data collections (#{amount})"), "Number of collections did not match the input amount")
509
510       click_link 'Data collections'
511       begin
512         wait_for_ajax
513       rescue
514       end
515
516       verify_collections = my_collections.dup
517       unexpected_items = []
518       collections_count = 0
519       within('.arv-project-Data_collections') do
520         page.execute_script "window.scrollBy(0,999000)"
521         begin
522           wait_for_ajax
523         rescue
524         end
525
526         # Visit all rows. If not all expected collections are found, retry
527         found_collections = page.all('tr[data-kind="arvados#collection"]')
528         collections_count = found_collections.count
529
530         (0..collections_count-1).each do |i|
531           # Found row text would be of the format "Show Collection_#{n} "
532           collection_name = found_collections[i].text.split[1]
533           if !my_collections.include? collection_name
534             unexpected_items << collection_name
535           else
536             verify_collections.delete collection_name
537           end
538         end
539
540         assert_equal true, unexpected_items.empty?, "Found unexpected items #{unexpected_items.inspect}"
541         assert_equal amount, collections_count, "Found different number of collections"
542         assert_equal true, verify_collections.empty?, "Did not find all the collections"
543       end
544     end
545   end
546
547   [
548     ['project with 10 pipelines', 10, 0],
549     ['project with 200 jobs and 10 pipelines', 10, 200],
550     ['project with 25 pipelines', 25, 0],
551   ].each do |project_name, num_pipelines, num_jobs|
552     test "scroll pipeline instances tab for #{project_name} with #{num_pipelines} pipelines and #{num_jobs} jobs" do
553       headless = Headless.new
554       headless.start
555       Capybara.current_driver = :selenium
556
557       visit page_with_token 'user1_with_load'
558
559       find("#projects-menu").click
560       find(".dropdown-menu a", text: project_name).click
561
562       my_pipelines = []
563       (1..num_pipelines).each do |i|
564         name = "pipeline_#{i}"
565         my_pipelines << name
566       end
567
568       # verify Jobs and pipelines tab scroll
569       assert(page.has_text?("Jobs and pipelines (#{num_pipelines+num_jobs})"), "Number of objects did not match the input counts")
570       click_link 'Jobs and pipelines'
571       begin
572         wait_for_ajax
573       rescue
574       end
575
576       verify_pipelines = my_pipelines.dup
577       unexpected_items = []
578       object_count = 0
579       within('.arv-project-Jobs_and_pipelines') do
580         page.execute_script "window.scrollBy(0,999000)"
581         begin
582           wait_for_ajax
583         rescue
584         end
585
586         # Visit all rows. Repeat if not all expected my_pipelines are found (inifinite scrolling should kick in)
587         pipelines_found = page.all('tr[data-kind="arvados#pipelineInstance"]')
588         found_pipeline_count = pipelines_found.count
589         (0..found_pipeline_count-1).each do |i|
590           name = pipelines_found[i].text.split[1]
591           if !my_pipelines.include? name
592             unexpected_items << name
593           else
594             verify_pipelines.delete name
595           end
596
597           assert_equal true, unexpected_items.empty?, "Found unexpected items #{unexpected_items.inspect}"
598         end
599
600         jobs_found = page.all('tr[data-kind="arvados#job"]')
601         found_job_count = jobs_found.count
602
603         assert_equal num_pipelines, found_pipeline_count, "Found different number of pipelines and jobs"
604         assert_equal num_jobs, found_job_count, 'Did not find expected number of jobs'
605         assert_equal true, verify_pipelines.empty?, "Did not find all the pipelines and jobs"
606       end
607     end
608   end
609
610 end