Merge branch 'master' into 3618-column-ordering
[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, obj=nil)
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       if share_type == 'users' and obj and obj['email']
198         assert(page.has_text?(obj['email']), "Did not find user's email")
199       end
200       assert_raises(Capybara::ElementNotFound,
201                     "Projects pulldown available from sharing dialog") do
202         click_on "All projects"
203       end
204       click_on "Add"
205     end
206     using_wait_time(Capybara.default_wait_time * 3) do
207       assert(page.has_link?(name),
208              "new share was not added to sharing table")
209       assert_equal(start_share_count + 1, share_rows.size,
210                    "new share did not add row to sharing table")
211     end
212   end
213
214   def modify_share_and_check(name)
215     start_rows = share_rows
216     link_row = start_rows.select { |row| row.has_text?(name) }
217     assert_equal(1, link_row.size, "row with new permission not found")
218     within(link_row.first) do
219       click_on("Read")
220       select("Write", from: "share_change_level")
221       click_on("editable-submit")
222       assert(has_link?("Write"),
223              "failed to change access level on new share")
224       click_on "Revoke"
225     end
226     using_wait_time(Capybara.default_wait_time * 3) do
227       assert(page.has_no_text?(name),
228              "new share row still exists after being revoked")
229       assert_equal(start_rows.size - 1, share_rows.size,
230                    "revoking share did not remove row from sharing table")
231     end
232   end
233
234   test "project viewer can't see project sharing tab" do
235     show_project_using("project_viewer")
236     assert(page.has_no_link?("Sharing"),
237            "read-only project user sees sharing tab")
238   end
239
240   test "project owner can manage sharing for another user" do
241     add_user = api_fixture('users')['future_project_user']
242     new_name = ["first_name", "last_name"].map { |k| add_user[k] }.join(" ")
243
244     show_project_using("active")
245     click_on "Sharing"
246     add_share_and_check("users", new_name, add_user)
247     modify_share_and_check(new_name)
248   end
249
250   test "project owner can manage sharing for another group" do
251     new_name = api_fixture('groups')['future_project_viewing_group']['name']
252
253     show_project_using("active")
254     click_on "Sharing"
255     add_share_and_check("groups", new_name)
256     modify_share_and_check(new_name)
257   end
258
259   test "'share with group' listing does not offer projects" do
260     show_project_using("active")
261     click_on "Sharing"
262     click_on "Share with groups"
263     good_uuid = api_fixture("groups")["private"]["uuid"]
264     assert(page.has_selector?(".selectable[data-object-uuid=\"#{good_uuid}\"]"),
265            "'share with groups' listing missing owned user group")
266     bad_uuid = api_fixture("groups")["asubproject"]["uuid"]
267     assert(page.has_no_selector?(".selectable[data-object-uuid=\"#{bad_uuid}\"]"),
268            "'share with groups' listing includes project")
269   end
270
271   [
272     ['Move',api_fixture('collections')['collection_to_move_around_in_aproject'],
273       api_fixture('groups')['aproject'],api_fixture('groups')['asubproject']],
274     ['Remove',api_fixture('collections')['collection_to_move_around_in_aproject'],
275       api_fixture('groups')['aproject']],
276     ['Copy',api_fixture('collections')['collection_to_move_around_in_aproject'],
277       api_fixture('groups')['aproject'],api_fixture('groups')['asubproject']],
278     ['Remove',api_fixture('collections')['collection_in_aproject_with_same_name_as_in_home_project'],
279       api_fixture('groups')['aproject'],nil,true],
280   ].each do |action, my_collection, src, dest=nil, expect_name_change=nil|
281     test "selection #{action} #{expect_name_change} for project" do
282       perform_selection_action src, dest, my_collection, action
283
284       case action
285       when 'Copy'
286         assert page.has_text?(my_collection['name']), 'Collection not found in src project after copy'
287         visit page_with_token 'active', '/'
288         find("#projects-menu").click
289         find(".dropdown-menu a", text: dest['name']).click
290         assert page.has_text?(my_collection['name']), 'Collection not found in dest project after copy'
291
292         # now remove it from destination project to restore to original state
293         perform_selection_action dest, nil, my_collection, 'Remove'
294       when 'Move'
295         assert page.has_no_text?(my_collection['name']), 'Collection still found in src project after move'
296         visit page_with_token 'active', '/'
297         find("#projects-menu").click
298         find(".dropdown-menu a", text: dest['name']).click
299         assert page.has_text?(my_collection['name']), 'Collection not found in dest project after move'
300
301         # move it back to src project to restore to original state
302         perform_selection_action dest, src, my_collection, action
303       when 'Remove'
304         assert page.has_no_text?(my_collection['name']), 'Collection still found in src project after remove'
305         visit page_with_token 'active', '/'
306         find("#projects-menu").click
307         find(".dropdown-menu a", text: "Home").click
308         assert page.has_text?(my_collection['name']), 'Collection not found in home project after remove'
309         if expect_name_change
310           assert page.has_text?(my_collection['name']+' removed from ' + src['name']),
311             'Collection with update name is not found in home project after remove'
312         end
313       end
314     end
315   end
316
317   def perform_selection_action src, dest, item, action
318     visit page_with_token 'active', '/'
319     find("#projects-menu").click
320     find(".dropdown-menu a", text: src['name']).click
321     assert page.has_text?(item['name']), 'Collection not found in src project'
322
323     within('tr', text: item['name']) do
324       find('input[type=checkbox]').click
325     end
326
327     click_button 'Selection...'
328
329     within('.selection-action-container') do
330       assert page.has_text?("Compare selected"), "Compare selected link text not found"
331       assert page.has_link?("Copy selected"), "Copy selected link not found"
332       assert page.has_link?("Move selected"), "Move selected link not found"
333       assert page.has_link?("Remove selected"), "Remove selected link not found"
334
335       click_link "#{action} selected"
336     end
337
338     # select the destination project if a Copy or Move action is being performed
339     if action == 'Copy' || action == 'Move'
340       within(".modal-container") do
341         find('.selectable', text: dest['name']).click
342         find('.modal-footer a,button', text: action).click
343         wait_for_ajax
344       end
345     end
346   end
347
348   # Test copy action state. It should not be available when a subproject is selected.
349   test "copy action is disabled when a subproject is selected" do
350     my_project = api_fixture('groups')['aproject']
351     my_collection = api_fixture('collections')['collection_to_move_around_in_aproject']
352     my_subproject = api_fixture('groups')['asubproject']
353
354     # verify that selection options are disabled on the project until an item is selected
355     visit page_with_token 'active', '/'
356     find("#projects-menu").click
357     find(".dropdown-menu a", text: my_project['name']).click
358
359     click_button 'Selection...'
360     within('.selection-action-container') do
361       page.assert_selector 'li.disabled', text: 'Create new collection with selected collections'
362       page.assert_selector 'li.disabled', text: 'Compare selected'
363       page.assert_selector 'li.disabled', text: 'Copy selected'
364       page.assert_selector 'li.disabled', text: 'Move selected'
365       page.assert_selector 'li.disabled', text: 'Remove selected'
366     end
367
368     # select collection and verify links are enabled
369     visit page_with_token 'active', '/'
370     find("#projects-menu").click
371     find(".dropdown-menu a", text: my_project['name']).click
372     assert page.has_text?(my_collection['name']), 'Collection not found in project'
373
374     within('tr', text: my_collection['name']) do
375       find('input[type=checkbox]').click
376     end
377
378     click_button 'Selection...'
379     within('.selection-action-container') do
380       page.assert_no_selector 'li.disabled', text: 'Create new collection with selected collections'
381       page.assert_selector 'li', text: 'Create new collection with selected collections'
382       page.assert_selector 'li.disabled', text: 'Compare selected'
383       page.assert_no_selector 'li.disabled', text: 'Copy selected'
384       page.assert_selector 'li', text: 'Copy selected'
385       page.assert_no_selector 'li.disabled', text: 'Move selected'
386       page.assert_selector 'li', text: 'Move selected'
387       page.assert_no_selector 'li.disabled', text: 'Remove selected'
388       page.assert_selector 'li', text: 'Remove selected'
389     end
390
391     # select subproject and verify that copy action is disabled
392     visit page_with_token 'active', '/'
393     find("#projects-menu").click
394     find(".dropdown-menu a", text: my_project['name']).click
395
396     click_link 'Subprojects'
397     assert page.has_text?(my_subproject['name']), 'Subproject not found in project'
398
399     within('tr', text: my_subproject['name']) do
400       find('input[type=checkbox]').click
401     end
402
403     click_button 'Selection...'
404     within('.selection-action-container') do
405       page.assert_selector 'li.disabled', text: 'Create new collection with selected collections'
406       page.assert_selector 'li.disabled', text: 'Compare selected'
407       page.assert_selector 'li.disabled', text: 'Copy selected'
408       page.assert_no_selector 'li.disabled', text: 'Move selected'
409       page.assert_selector 'li', text: 'Move selected'
410       page.assert_no_selector 'li.disabled', text: 'Remove selected'
411       page.assert_selector 'li', text: 'Remove selected'
412     end
413
414     # select subproject and a collection and verify that copy action is still disabled
415     visit page_with_token 'active', '/'
416     find("#projects-menu").click
417     find(".dropdown-menu a", text: my_project['name']).click
418
419     click_link 'Subprojects'
420     assert page.has_text?(my_subproject['name']), 'Subproject not found in project'
421
422     within('tr', text: my_subproject['name']) do
423       find('input[type=checkbox]').click
424     end
425
426     click_link 'Data collections'
427     assert page.has_text?(my_collection['name']), 'Collection not found in project'
428
429     within('tr', text: my_collection['name']) do
430       find('input[type=checkbox]').click
431     end
432
433     click_button 'Selection...'
434     within('.selection-action-container') do
435       page.assert_selector 'li.disabled', text: 'Create new collection with selected collections'
436       page.assert_selector 'li.disabled', text: 'Compare selected'
437       page.assert_selector 'li.disabled', text: 'Copy selected'
438       page.assert_no_selector 'li.disabled', text: 'Move selected'
439       page.assert_selector 'li', text: 'Move selected'
440       page.assert_no_selector 'li.disabled', text: 'Remove selected'
441       page.assert_selector 'li', text: 'Remove selected'
442     end
443   end
444
445   [
446     ['active', true],
447     ['project_viewer', false],
448   ].each do |user, expect_collection_in_aproject|
449     test "combine selected collections into new collection #{user} #{expect_collection_in_aproject}" do
450       my_project = api_fixture('groups')['aproject']
451       my_collection = api_fixture('collections')['collection_to_move_around_in_aproject']
452
453       visit page_with_token user, '/'
454       find("#projects-menu").click
455       find(".dropdown-menu a", text: my_project['name']).click
456       assert page.has_text?(my_collection['name']), 'Collection not found in project'
457
458       within('tr', text: my_collection['name']) do
459         find('input[type=checkbox]').click
460       end
461
462       click_button 'Selection...'
463       within('.selection-action-container') do
464         click_link 'Create new collection with selected collections'
465       end
466
467       # now in the new collection page
468       if expect_collection_in_aproject
469         assert page.has_text?("Created new collection in the project #{my_project['name']}"),
470                               'Not found flash message that new collection is created in aproject'
471       else
472         assert page.has_text?("Created new collection in your Home project"),
473                               'Not found flash message that new collection is created in Home project'
474       end
475       assert page.has_text?('Content hash'), 'Not found content hash in collection page'
476     end
477   end
478
479   [
480     ["jobs", "/jobs"],
481     ["pipelines", "/pipeline_instances"],
482     ["collections", "/collections"]
483   ].each do |target,path|
484     test "Test dashboard button all #{target}" do
485       visit page_with_token 'active', '/'
486       click_link "All #{target}"
487       assert_equal path, current_path
488     end
489   end
490
491   def scroll_setup(project_name,
492                    total_nbr_items,
493                    item_list_parameter,
494                    sorted = false,
495                    sort_parameters = nil)
496     headless = Headless.new
497     headless.start
498     Capybara.current_driver = :selenium
499
500     project_uuid = api_fixture('groups')[project_name]['uuid']
501     visit page_with_token 'user1_with_load', '/projects/' + project_uuid
502
503     assert(page.has_text?("#{item_list_parameter.humanize} (#{total_nbr_items})"), "Number of #{item_list_parameter.humanize} did not match the input amount")
504
505     click_link item_list_parameter.humanize
506     wait_for_ajax
507
508     if sorted
509       find("th[data-sort-order='#{sort_parameters.gsub(/\s/,'')}']").click
510       wait_for_ajax
511     end
512   end
513
514   def scroll_items_check(nbr_items,
515                          fixture_prefix,
516                          item_list_parameter,
517                          item_selector,
518                          sorted = false)
519     items = []
520     for i in 1..nbr_items
521       items << "#{fixture_prefix}#{i}"
522     end
523
524     verify_items = items.dup
525     unexpected_items = []
526     item_count = 0
527     within(".arv-project-#{item_list_parameter}") do
528       page.execute_script "window.scrollBy(0,999000)"
529       begin
530         wait_for_ajax
531       rescue
532       end
533
534       # Visit all rows. If not all expected items are found, retry
535       found_items = page.all(item_selector)
536       item_count = found_items.count
537
538       previous = nil
539       (0..item_count-1).each do |i|
540         # Found row text using the fixture string e.g. "Show Collection_#{n} "
541         item_name = found_items[i].text.split[1]
542         if !items.include? item_name
543           unexpected_items << item_name
544         else
545           verify_items.delete item_name
546         end
547         if sorted
548           # check sort order
549           assert_operator( previous.downcase, :<=, item_name.downcase) if previous
550           previous = item_name
551         end
552       end
553
554       assert_equal true, unexpected_items.empty?, "Found unexpected #{item_list_parameter.humanize} #{unexpected_items.inspect}"
555       assert_equal nbr_items, item_count, "Found different number of #{item_list_parameter.humanize}"
556       assert_equal true, verify_items.empty?, "Did not find all the #{item_list_parameter.humanize}"
557     end
558   end
559
560   [
561     ['project_with_10_collections', 10],
562     ['project_with_201_collections', 201], # two pages of data
563   ].each do |project_name, nbr_items|
564     test "scroll collections tab for #{project_name} with #{nbr_items} objects" do
565       item_list_parameter = "Data_collections"
566       scroll_setup project_name,
567                    nbr_items,
568                    item_list_parameter
569       scroll_items_check nbr_items,
570                          "Collection_",
571                          item_list_parameter,
572                          'tr[data-kind="arvados#collection"]'
573     end
574   end
575
576   [
577     ['project_with_10_collections', 10],
578     ['project_with_201_collections', 201], # two pages of data
579   ].each do |project_name, nbr_items|
580     test "scroll collections tab for #{project_name} with #{nbr_items} objects with ascending sort (case insensitive)" do
581       item_list_parameter = "Data_collections"
582       scroll_setup project_name,
583                    nbr_items,
584                    item_list_parameter,
585                    true,
586                    "collections.name"
587       scroll_items_check nbr_items,
588                          "Collection_",
589                          item_list_parameter,
590                          'tr[data-kind="arvados#collection"]',
591                          true
592     end
593   end
594
595   [
596     ['project_with_10_pipelines', 10, 0],
597     ['project_with_2_pipelines_and_60_jobs', 2, 60],
598     ['project_with_25_pipelines', 25, 0],
599   ].each do |project_name, num_pipelines, num_jobs|
600     test "scroll pipeline instances tab for #{project_name} with #{num_pipelines} pipelines and #{num_jobs} jobs" do
601       item_list_parameter = "Jobs_and_pipelines"
602       scroll_setup project_name,
603                    num_pipelines + num_jobs,
604                    item_list_parameter
605       # check the general scrolling and the pipelines
606       scroll_items_check num_pipelines,
607                          "pipeline_",
608                          item_list_parameter,
609                          'tr[data-kind="arvados#pipelineInstance"]'
610       # Check job count separately
611       jobs_found = page.all('tr[data-kind="arvados#job"]')
612       found_job_count = jobs_found.count
613       assert_equal num_jobs, found_job_count, 'Did not find expected number of jobs'
614     end
615   end
616
617   # Move button accessibility
618   [
619     ['admin', true],
620     ['active', true],  # project owner
621     ['project_viewer', false],
622     ].each do |user, can_move|
623     test "#{user} can move subproject under another user's Home #{can_move}" do
624       project = api_fixture('groups')['aproject']
625       collection = api_fixture('collections')['collection_to_move_around_in_aproject']
626
627       # verify the project move button
628       visit page_with_token user, "/projects/#{project['uuid']}"
629       if can_move
630         assert page.has_link? 'Move project...'
631       else
632         assert page.has_no_link? 'Move project...'
633       end
634     end
635   end
636
637 end