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