Merge branch '5160-share-with-anonymous-group-wip'
[arvados.git] / apps / workbench / test / integration / projects_test.rb
index 5da61536f010cdc9cfc1aee35f2d91e93cd7a3fa..73a23d6d35c9eae078be49028ed6f93cc89bb3b2 100644 (file)
@@ -1,15 +1,17 @@
 require 'integration_helper'
-require 'selenium-webdriver'
-require 'headless'
+require 'helpers/share_object_helper'
 
 class ProjectsTest < ActionDispatch::IntegrationTest
+  include ShareObjectHelper
+
   setup do
-    Capybara.current_driver = Capybara.javascript_driver
+    need_javascript
   end
 
   test 'Check collection count for A Project in the tab pane titles' do
     project_uuid = api_fixture('groups')['aproject']['uuid']
     visit page_with_token 'active', '/projects/' + project_uuid
+    wait_for_ajax
     collection_count = page.all("[data-pk*='collection']").count
     assert_selector '#Data_collections-tab span', text: "(#{collection_count})"
   end
@@ -48,8 +50,7 @@ class ProjectsTest < ActionDispatch::IntegrationTest
 
     # visit project page
     visit current_path
-    assert(has_no_text?('.container-fluid', text: '*Textile description for A project*'),
-           "Description is not rendered properly")
+    assert_no_text '*Textile description for A project*'
     assert(find?('.container-fluid', text: 'Textile description for A project'),
            "Description update did not survive page refresh")
     assert(find?('.container-fluid', text: 'And a new paragraph in description'),
@@ -132,7 +133,7 @@ class ProjectsTest < ActionDispatch::IntegrationTest
     click_link 'Other objects'
     within '.selection-action-container' do
       find '.editable', text: 'Now I have a new name.'
-      page.assert_no_selector '.editable', text: 'Now I have a name.'
+      assert_no_selector '.editable', text: 'Now I have a name.'
     end
   end
 
@@ -142,7 +143,6 @@ class ProjectsTest < ActionDispatch::IntegrationTest
     find(".dropdown-menu a", text: "Home").click
     find('.btn', text: "Add a subproject").click
 
-    # within('.editable', text: 'New project') do
     within('h2') do
       find('.fa-pencil').click
       find('.editable-input input').set('Project 1234')
@@ -172,67 +172,36 @@ class ProjectsTest < ActionDispatch::IntegrationTest
            "Project 5678 should now be inside project 1234")
   end
 
-  def show_project_using(auth_key, proj_key='aproject')
-    project_uuid = api_fixture('groups')[proj_key]['uuid']
-    visit(page_with_token(auth_key, "/projects/#{project_uuid}"))
-    assert(page.has_text?("A Project"), "not on expected project page")
+  def open_groups_sharing(project_name="aproject", token_name="active")
+    project = api_fixture("groups", project_name)
+    visit(page_with_token(token_name, "/projects/#{project['uuid']}"))
+    click_on "Sharing"
+    click_on "Share with groups"
   end
 
-  def share_rows
-    find('#project_sharing').all('tr')
+  def group_name(group_key)
+    api_fixture("groups", group_key, "name")
   end
 
-  def add_share_and_check(share_type, name, obj=nil)
-    assert(page.has_no_text?(name), "project is already shared with #{name}")
-    start_share_count = share_rows.size
-    click_on("Share with #{share_type}")
-    within(".modal-container") do
-      # Order is important here: we should find something that appears in the
-      # modal before we make any assertions about what's not in the modal.
-      # Otherwise, the not-included assertions might falsely pass because
-      # the modal hasn't loaded yet.
-      find(".selectable", text: name).click
-      assert(has_no_selector?(".modal-dialog-preview-pane"),
-             "preview pane available in sharing dialog")
-      if share_type == 'users' and obj and obj['email']
-        assert(page.has_text?(obj['email']), "Did not find user's email")
-      end
-      assert_raises(Capybara::ElementNotFound,
-                    "Projects pulldown available from sharing dialog") do
-        click_on "All projects"
-      end
-      click_on "Add"
-    end
-    using_wait_time(Capybara.default_wait_time * 3) do
-      assert(page.has_link?(name),
-             "new share was not added to sharing table")
-      assert_equal(start_share_count + 1, share_rows.size,
-                   "new share did not add row to sharing table")
-    end
+  test "projects not publicly sharable when anonymous browsing disabled" do
+    Rails.configuration.anonymous_user_token = false
+    open_groups_sharing
+    # Check for a group we do expect first, to make sure the modal's loaded.
+    assert_selector(".modal-container .selectable",
+                    text: group_name("all_users"))
+    assert_no_selector(".modal-container .selectable",
+                       text: group_name("anonymous_group"))
   end
 
-  def modify_share_and_check(name)
-    start_rows = share_rows
-    link_row = start_rows.select { |row| row.has_text?(name) }
-    assert_equal(1, link_row.size, "row with new permission not found")
-    within(link_row.first) do
-      click_on("Read")
-      select("Write", from: "share_change_level")
-      click_on("editable-submit")
-      assert(has_link?("Write"),
-             "failed to change access level on new share")
-      click_on "Revoke"
-    end
-    using_wait_time(Capybara.default_wait_time * 3) do
-      assert(page.has_no_text?(name),
-             "new share row still exists after being revoked")
-      assert_equal(start_rows.size - 1, share_rows.size,
-                   "revoking share did not remove row from sharing table")
-    end
+  test "projects publicly sharable when anonymous browsing enabled" do
+    Rails.configuration.anonymous_user_token = "testonlytoken"
+    open_groups_sharing
+    assert_selector(".modal-container .selectable",
+                    text: group_name("anonymous_group"))
   end
 
   test "project viewer can't see project sharing tab" do
-    show_project_using("project_viewer")
+    show_object_using('project_viewer', 'groups', 'aproject', 'A Project')
     assert(page.has_no_link?("Sharing"),
            "read-only project user sees sharing tab")
   end
@@ -241,7 +210,7 @@ class ProjectsTest < ActionDispatch::IntegrationTest
     add_user = api_fixture('users')['future_project_user']
     new_name = ["first_name", "last_name"].map { |k| add_user[k] }.join(" ")
 
-    show_project_using("active")
+    show_object_using('active', 'groups', 'aproject', 'A Project')
     click_on "Sharing"
     add_share_and_check("users", new_name, add_user)
     modify_share_and_check(new_name)
@@ -250,14 +219,14 @@ class ProjectsTest < ActionDispatch::IntegrationTest
   test "project owner can manage sharing for another group" do
     new_name = api_fixture('groups')['future_project_viewing_group']['name']
 
-    show_project_using("active")
+    show_object_using('active', 'groups', 'aproject', 'A Project')
     click_on "Sharing"
     add_share_and_check("groups", new_name)
     modify_share_and_check(new_name)
   end
 
   test "'share with group' listing does not offer projects" do
-    show_project_using("active")
+    show_object_using('active', 'groups', 'aproject', 'A Project')
     click_on "Sharing"
     click_on "Share with groups"
     good_uuid = api_fixture("groups")["private"]["uuid"]
@@ -278,7 +247,7 @@ class ProjectsTest < ActionDispatch::IntegrationTest
     ['Remove',api_fixture('collections')['collection_in_aproject_with_same_name_as_in_home_project'],
       api_fixture('groups')['aproject'],nil,true],
   ].each do |action, my_collection, src, dest=nil, expect_name_change=nil|
-    test "selection #{action} #{expect_name_change} for project" do
+    test "selection #{action} -> #{expect_name_change.inspect} for project" do
       perform_selection_action src, dest, my_collection, action
 
       case action
@@ -289,8 +258,6 @@ class ProjectsTest < ActionDispatch::IntegrationTest
         find(".dropdown-menu a", text: dest['name']).click
         assert page.has_text?(my_collection['name']), 'Collection not found in dest project after copy'
 
-        # now remove it from destination project to restore to original state
-        perform_selection_action dest, nil, my_collection, 'Remove'
       when 'Move'
         assert page.has_no_text?(my_collection['name']), 'Collection still found in src project after move'
         visit page_with_token 'active', '/'
@@ -298,18 +265,8 @@ class ProjectsTest < ActionDispatch::IntegrationTest
         find(".dropdown-menu a", text: dest['name']).click
         assert page.has_text?(my_collection['name']), 'Collection not found in dest project after move'
 
-        # move it back to src project to restore to original state
-        perform_selection_action dest, src, my_collection, action
       when 'Remove'
         assert page.has_no_text?(my_collection['name']), 'Collection still found in src project after remove'
-        visit page_with_token 'active', '/'
-        find("#projects-menu").click
-        find(".dropdown-menu a", text: "Home").click
-        assert page.has_text?(my_collection['name']), 'Collection not found in home project after remove'
-        if expect_name_change
-          assert page.has_text?(my_collection['name']+' removed from ' + src['name']),
-            'Collection with update name is not found in home project after remove'
-        end
       end
     end
   end
@@ -324,7 +281,7 @@ class ProjectsTest < ActionDispatch::IntegrationTest
       find('input[type=checkbox]').click
     end
 
-    click_button 'Selection...'
+    click_button 'Selection'
 
     within('.selection-action-container') do
       assert page.has_text?("Compare selected"), "Compare selected link text not found"
@@ -356,13 +313,13 @@ class ProjectsTest < ActionDispatch::IntegrationTest
     find("#projects-menu").click
     find(".dropdown-menu a", text: my_project['name']).click
 
-    click_button 'Selection...'
+    click_button 'Selection'
     within('.selection-action-container') do
-      page.assert_selector 'li.disabled', text: 'Create new collection with selected collections'
-      page.assert_selector 'li.disabled', text: 'Compare selected'
-      page.assert_selector 'li.disabled', text: 'Copy selected'
-      page.assert_selector 'li.disabled', text: 'Move selected'
-      page.assert_selector 'li.disabled', text: 'Remove selected'
+      assert_selector 'li.disabled', text: 'Create new collection with selected collections'
+      assert_selector 'li.disabled', text: 'Compare selected'
+      assert_selector 'li.disabled', text: 'Copy selected'
+      assert_selector 'li.disabled', text: 'Move selected'
+      assert_selector 'li.disabled', text: 'Remove selected'
     end
 
     # select collection and verify links are enabled
@@ -375,17 +332,17 @@ class ProjectsTest < ActionDispatch::IntegrationTest
       find('input[type=checkbox]').click
     end
 
-    click_button 'Selection...'
+    click_button 'Selection'
     within('.selection-action-container') do
-      page.assert_no_selector 'li.disabled', text: 'Create new collection with selected collections'
-      page.assert_selector 'li', text: 'Create new collection with selected collections'
-      page.assert_selector 'li.disabled', text: 'Compare selected'
-      page.assert_no_selector 'li.disabled', text: 'Copy selected'
-      page.assert_selector 'li', text: 'Copy selected'
-      page.assert_no_selector 'li.disabled', text: 'Move selected'
-      page.assert_selector 'li', text: 'Move selected'
-      page.assert_no_selector 'li.disabled', text: 'Remove selected'
-      page.assert_selector 'li', text: 'Remove selected'
+      assert_no_selector 'li.disabled', text: 'Create new collection with selected collections'
+      assert_selector 'li', text: 'Create new collection with selected collections'
+      assert_selector 'li.disabled', text: 'Compare selected'
+      assert_no_selector 'li.disabled', text: 'Copy selected'
+      assert_selector 'li', text: 'Copy selected'
+      assert_no_selector 'li.disabled', text: 'Move selected'
+      assert_selector 'li', text: 'Move selected'
+      assert_no_selector 'li.disabled', text: 'Remove selected'
+      assert_selector 'li', text: 'Remove selected'
     end
 
     # select subproject and verify that copy action is disabled
@@ -400,22 +357,61 @@ class ProjectsTest < ActionDispatch::IntegrationTest
       find('input[type=checkbox]').click
     end
 
-    click_button 'Selection...'
+    click_button 'Selection'
+    within('.selection-action-container') do
+      assert_selector 'li.disabled', text: 'Create new collection with selected collections'
+      assert_selector 'li.disabled', text: 'Compare selected'
+      assert_selector 'li.disabled', text: 'Copy selected'
+      assert_no_selector 'li.disabled', text: 'Move selected'
+      assert_selector 'li', text: 'Move selected'
+      assert_no_selector 'li.disabled', text: 'Remove selected'
+      assert_selector 'li', text: 'Remove selected'
+    end
+
+    # select subproject and a collection and verify that copy action is still disabled
+    visit page_with_token 'active', '/'
+    find("#projects-menu").click
+    find(".dropdown-menu a", text: my_project['name']).click
+
+    click_link 'Subprojects'
+    assert page.has_text?(my_subproject['name']), 'Subproject not found in project'
+
+    within('tr', text: my_subproject['name']) do
+      find('input[type=checkbox]').click
+    end
+
+    click_link 'Data collections'
+    assert page.has_text?(my_collection['name']), 'Collection not found in project'
+
+    within('tr', text: my_collection['name']) do
+      find('input[type=checkbox]').click
+    end
+
+    click_link 'Subprojects'
+    click_button 'Selection'
     within('.selection-action-container') do
-      page.assert_selector 'li.disabled', text: 'Create new collection with selected collections'
-      page.assert_selector 'li.disabled', text: 'Compare selected'
-      page.assert_selector 'li.disabled', text: 'Copy selected'
-      page.assert_no_selector 'li.disabled', text: 'Move selected'
-      page.assert_selector 'li', text: 'Move selected'
-      page.assert_no_selector 'li.disabled', text: 'Remove selected'
-      page.assert_selector 'li', text: 'Remove selected'
+      assert_selector 'li.disabled', text: 'Create new collection with selected collections'
+      assert_selector 'li.disabled', text: 'Compare selected'
+      assert_selector 'li.disabled', text: 'Copy selected'
+      assert_no_selector 'li.disabled', text: 'Move selected'
+      assert_selector 'li', text: 'Move selected'
+      assert_no_selector 'li.disabled', text: 'Remove selected'
+      assert_selector 'li', text: 'Remove selected'
     end
+  end
+
+  # When project tabs are switched, only options applicable to the current tab's selections are enabled.
+  test "verify selection options when tabs are switched" do
+    my_project = api_fixture('groups')['aproject']
+    my_collection = api_fixture('collections')['collection_to_move_around_in_aproject']
+    my_subproject = api_fixture('groups')['asubproject']
 
     # select subproject and a collection and verify that copy action is still disabled
     visit page_with_token 'active', '/'
     find("#projects-menu").click
     find(".dropdown-menu a", text: my_project['name']).click
 
+    # Select a sub-project
     click_link 'Subprojects'
     assert page.has_text?(my_subproject['name']), 'Subproject not found in project'
 
@@ -423,6 +419,7 @@ class ProjectsTest < ActionDispatch::IntegrationTest
       find('input[type=checkbox]').click
     end
 
+    # Select a collection
     click_link 'Data collections'
     assert page.has_text?(my_collection['name']), 'Collection not found in project'
 
@@ -430,15 +427,50 @@ class ProjectsTest < ActionDispatch::IntegrationTest
       find('input[type=checkbox]').click
     end
 
-    click_button 'Selection...'
+    # Go back to Subprojects tab
+    click_link 'Subprojects'
+    click_button 'Selection'
+    within('.selection-action-container') do
+      assert_selector 'li.disabled', text: 'Create new collection with selected collections'
+      assert_selector 'li.disabled', text: 'Compare selected'
+      assert_selector 'li.disabled', text: 'Copy selected'
+      assert_no_selector 'li.disabled', text: 'Move selected'
+      assert_selector 'li', text: 'Move selected'
+      assert_no_selector 'li.disabled', text: 'Remove selected'
+      assert_selector 'li', text: 'Remove selected'
+    end
+
+    # Close the dropdown by clicking outside it.
+    find('.dropdown-toggle', text: 'Selection').find(:xpath, '..').click
+
+    # Go back to Data collections tab
+    find('.nav-tabs a', text: 'Data collections').click
+    click_button 'Selection'
+    within('.selection-action-container') do
+      assert_no_selector 'li.disabled', text: 'Create new collection with selected collections'
+      assert_selector 'li', text: 'Create new collection with selected collections'
+      assert_selector 'li.disabled', text: 'Compare selected'
+      assert_no_selector 'li.disabled', text: 'Copy selected'
+      assert_selector 'li', text: 'Copy selected'
+      assert_no_selector 'li.disabled', text: 'Move selected'
+      assert_selector 'li', text: 'Move selected'
+      assert_no_selector 'li.disabled', text: 'Remove selected'
+      assert_selector 'li', text: 'Remove selected'
+    end
+  end
+
+  # "Move selected" and "Remove selected" options should not be available when current user cannot write to the project
+  test "move selected and remove selected actions not available when current user cannot write to project" do
+    my_project = api_fixture('groups')['anonymously_accessible_project']
+    visit page_with_token 'active', "/projects/#{my_project['uuid']}"
+
+    click_button 'Selection'
     within('.selection-action-container') do
-      page.assert_selector 'li.disabled', text: 'Create new collection with selected collections'
-      page.assert_selector 'li.disabled', text: 'Compare selected'
-      page.assert_selector 'li.disabled', text: 'Copy selected'
-      page.assert_no_selector 'li.disabled', text: 'Move selected'
-      page.assert_selector 'li', text: 'Move selected'
-      page.assert_no_selector 'li.disabled', text: 'Remove selected'
-      page.assert_selector 'li', text: 'Remove selected'
+      assert_selector 'li', text: 'Create new collection with selected collections'
+      assert_selector 'li', text: 'Compare selected'
+      assert_selector 'li', text: 'Copy selected'
+      assert_no_selector 'li', text: 'Move selected'
+      assert_no_selector 'li', text: 'Remove selected'
     end
   end
 
@@ -459,7 +491,7 @@ class ProjectsTest < ActionDispatch::IntegrationTest
         find('input[type=checkbox]').click
       end
 
-      click_button 'Selection...'
+      click_button 'Selection'
       within('.selection-action-container') do
         click_link 'Create new collection with selected collections'
       end
@@ -493,10 +525,6 @@ class ProjectsTest < ActionDispatch::IntegrationTest
                    item_list_parameter,
                    sorted = false,
                    sort_parameters = nil)
-    headless = Headless.new
-    headless.start
-    Capybara.current_driver = :selenium
-
     project_uuid = api_fixture('groups')[project_name]['uuid']
     visit page_with_token 'user1_with_load', '/projects/' + project_uuid
 
@@ -634,4 +662,56 @@ class ProjectsTest < ActionDispatch::IntegrationTest
     end
   end
 
+  test "error while loading tab" do
+    original_arvados_v1_base = Rails.configuration.arvados_v1_base
+
+    visit page_with_token 'active', '/projects/' + api_fixture('groups')['aproject']['uuid']
+
+    # Point to a bad api server url to generate error
+    Rails.configuration.arvados_v1_base = "https://[100::f]:1/"
+    click_link 'Other objects'
+    within '#Other_objects' do
+      # Error
+      assert_selector('a', text: 'Reload tab')
+
+      # Now point back to the orig api server and reload tab
+      Rails.configuration.arvados_v1_base = original_arvados_v1_base
+      click_link 'Reload tab'
+      assert_no_selector('a', text: 'Reload tab')
+      assert_selector('button', text: 'Selection')
+      within '.selection-action-container' do
+        assert_selector 'tr[data-kind="arvados#trait"]'
+      end
+    end
+  end
+
+  test "add new project using projects dropdown" do
+    # verify that selection options are disabled on the project until an item is selected
+    visit page_with_token 'active', '/'
+
+    # Add a new project
+    find("#projects-menu").click
+    click_link 'Add a new project'
+    assert_text 'New project'
+    assert_text 'No description provided'
+
+    # Add one more new project
+    find("#projects-menu").click
+    click_link 'Add a new project'
+    match = /New project \(\d\)/.match page.text
+    assert match, 'Expected project name not found'
+    assert_text 'No description provided'
+  end
+
+  test "first tab loads data when visiting other tab directly" do
+    # As of 2014-12-19, the first tab of project#show uses infinite scrolling.
+    # Make sure that it loads data even if we visit another tab directly.
+    need_selenium 'to land on specified tab using {url}#Advanced'
+    project = api_fixture("groups", "aproject")
+    visit(page_with_token("active_trustedclient",
+                          "/projects/#{project['uuid']}#Advanced"))
+    assert_text("API response")
+    find("#page-wrapper .nav-tabs :first-child a").click
+    assert_text("bytes Collection")
+  end
 end