Merge branch '2044-workbench-project-sharing'
[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 'Find a project and edit its description' do
11     visit page_with_token 'active', '/'
12     find('.arv-project-list a,button', text: 'A Project').
13       click
14     within('.container-fluid', text: api_fixture('groups')['aproject']['name']) do
15       find('span', text: api_fixture('groups')['aproject']['name']).click
16       within('.arv-description-as-subtitle') do
17         find('.fa-pencil').click
18         find('.editable-input textarea').set('I just edited this.')
19         find('.editable-submit').click
20       end
21       wait_for_ajax
22     end
23     visit current_path
24     assert(find?('.container-fluid', text: 'I just edited this.'),
25            "Description update did not survive page refresh")
26   end
27
28   test 'Add a new name, then edit it, without creating a duplicate' do
29     project_uuid = api_fixture('groups')['aproject']['uuid']
30     specimen_uuid = api_fixture('specimens')['owned_by_aproject_with_no_name_link']['uuid']
31     visit page_with_token 'active', '/projects/' + project_uuid
32     within(".selection-action-container") do
33       within (first('tr', text: 'Specimen')) do
34         find(".fa-pencil").click
35         find('.editable-input input').set('Now I have a name.')
36         find('.glyphicon-ok').click
37         find('.editable', text: 'Now I have a name.').click
38         find(".fa-pencil").click
39         find('.editable-input input').set('Now I have a new name.')
40         find('.glyphicon-ok').click
41         end
42       wait_for_ajax
43       find('.editable', text: 'Now I have a new name.')
44     end
45     visit current_path
46     within '.selection-action-container' do
47       find '.editable', text: 'Now I have a new name.'
48       page.assert_no_selector '.editable', text: 'Now I have a name.'
49     end
50   end
51
52   test 'Create a project and move it into a different project' do
53     visit page_with_token 'active', '/projects'
54     find('.btn', text: "Add new project").click
55
56     # within('.editable', text: 'New project') do
57     within('h2') do
58       find('.fa-pencil').click
59       find('.editable-input input').set('Project 1234')
60       find('.glyphicon-ok').click
61     end
62     wait_for_ajax
63
64     visit '/projects'
65     find('.btn', text: "Add new project").click
66     within('h2') do
67       find('.fa-pencil').click
68       find('.editable-input input').set('Project 5678')
69       find('.glyphicon-ok').click
70     end
71     wait_for_ajax
72
73     click_link 'Move project...'
74     find('.selectable', text: 'Project 1234').click
75     find('.modal-footer a,button', text: 'Move').click
76     wait_for_ajax
77
78     # Wait for the page to refresh and show the new parent in Sharing panel
79     click_link 'Sharing'
80     assert(page.has_link?("Project 1234"),
81            "Project 5678 should now be inside project 1234")
82   end
83
84   def show_project_using(auth_key, proj_key='aproject')
85     project_uuid = api_fixture('groups')[proj_key]['uuid']
86     visit(page_with_token(auth_key, "/projects/#{project_uuid}"))
87     assert(page.has_text?("A Project"), "not on expected project page")
88   end
89
90   def count_shares
91     page.all("#project_sharing tr").size
92   end
93
94   def add_share_and_check(share_type, name)
95     assert(page.has_no_text?(name), "project is already shared with #{name}")
96     start_share_count = count_shares
97     click_on("Share with #{share_type}")
98     find(".selectable", text: name).click
99     find(".modal-footer a,button", text: "Add").click
100     assert(page.has_link?(name),
101            "new share was not added to sharing table")
102     assert_equal(start_share_count + 1, count_shares,
103                  "new share did not add row to sharing table")
104   end
105
106   def modify_share_and_check(name)
107     start_share_count = count_shares
108     link_row = all("#project_sharing tr").select { |row| row.has_text?(name) }
109     assert_equal(1, link_row.size, "row with new permission not found")
110     within(link_row.first) do
111       click_on("Read")
112       select("Write", from: "share_change_level")
113       click_on("editable-submit")
114       assert(has_link?("Write"),
115              "failed to change access level on new share")
116       click_on "Revoke"
117     end
118     assert(page.has_no_text?(name),
119            "new share row still exists after being revoked")
120     assert_equal(start_share_count - 1, count_shares,
121                  "revoking share did not remove row from sharing table")
122   end
123
124   test "project viewer can't see project sharing tab" do
125     show_project_using("project_viewer")
126     assert(page.has_no_link?("Sharing"),
127            "read-only project user sees sharing tab")
128   end
129
130   test "project owner can manage sharing for another user" do
131     add_user = api_fixture('users')['future_project_user']
132     new_name = ["first_name", "last_name"].map { |k| add_user[k] }.join(" ")
133
134     show_project_using("active")
135     click_on "Sharing"
136     add_share_and_check("users", new_name)
137     modify_share_and_check(new_name)
138   end
139
140   test "project owner can manage sharing for another group" do
141     new_name = api_fixture('groups')['future_project_viewing_group']['name']
142
143     show_project_using("active")
144     click_on "Sharing"
145     add_share_and_check("groups", new_name)
146     modify_share_and_check(new_name)
147   end
148 end