3036: Merge branch 'master' into 3036-mutable-collections
[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     click_link 'Other objects'
33     within(".selection-action-container") do
34       within (first('tr', text: 'Specimen')) do
35         find(".fa-pencil").click
36         find('.editable-input input').set('Now I have a name.')
37         find('.glyphicon-ok').click
38         find('.editable', text: 'Now I have a name.').click
39         find(".fa-pencil").click
40         find('.editable-input input').set('Now I have a new name.')
41         find('.glyphicon-ok').click
42         end
43       wait_for_ajax
44       find('.editable', text: 'Now I have a new name.')
45     end
46     visit current_path
47     click_link 'Other objects'
48     within '.selection-action-container' do
49       find '.editable', text: 'Now I have a new name.'
50       page.assert_no_selector '.editable', text: 'Now I have a name.'
51     end
52   end
53
54   test 'Create a project and move it into a different project' do
55     visit page_with_token 'active', '/projects'
56     find('.btn', text: "Add new project").click
57
58     # within('.editable', text: 'New project') do
59     within('h2') do
60       find('.fa-pencil').click
61       find('.editable-input input').set('Project 1234')
62       find('.glyphicon-ok').click
63     end
64     wait_for_ajax
65
66     visit '/projects'
67     find('.btn', text: "Add new project").click
68     within('h2') do
69       find('.fa-pencil').click
70       find('.editable-input input').set('Project 5678')
71       find('.glyphicon-ok').click
72     end
73     wait_for_ajax
74
75     click_link 'Move project...'
76     find('.selectable', text: 'Project 1234').click
77     find('.modal-footer a,button', text: 'Move').click
78     wait_for_ajax
79
80     # Wait for the page to refresh and show the new parent in Sharing panel
81     click_link 'Sharing'
82     assert(page.has_link?("Project 1234"),
83            "Project 5678 should now be inside project 1234")
84   end
85
86   def show_project_using(auth_key, proj_key='aproject')
87     project_uuid = api_fixture('groups')[proj_key]['uuid']
88     visit(page_with_token(auth_key, "/projects/#{project_uuid}"))
89     assert(page.has_text?("A Project"), "not on expected project page")
90   end
91
92   def share_rows
93     find('#project_sharing').all('tr')
94   end
95
96   def add_share_and_check(share_type, name)
97     assert(page.has_no_text?(name), "project is already shared with #{name}")
98     start_share_count = share_rows.size
99     click_on("Share with #{share_type}")
100     within(".modal-container") do
101       # Order is important here: we should find something that appears in the
102       # modal before we make any assertions about what's not in the modal.
103       # Otherwise, the not-included assertions might falsely pass because
104       # the modal hasn't loaded yet.
105       find(".selectable", text: name).click
106       assert(has_no_selector?(".modal-dialog-preview-pane"),
107              "preview pane available in sharing dialog")
108       assert_raises(Capybara::ElementNotFound,
109                     "Projects pulldown available from sharing dialog") do
110         click_on "All projects"
111       end
112       click_on "Add"
113     end
114     using_wait_time(Capybara.default_wait_time * 3) do
115       assert(page.has_link?(name),
116              "new share was not added to sharing table")
117       assert_equal(start_share_count + 1, share_rows.size,
118                    "new share did not add row to sharing table")
119     end
120   end
121
122   def modify_share_and_check(name)
123     start_rows = share_rows
124     link_row = start_rows.select { |row| row.has_text?(name) }
125     assert_equal(1, link_row.size, "row with new permission not found")
126     within(link_row.first) do
127       click_on("Read")
128       select("Write", from: "share_change_level")
129       click_on("editable-submit")
130       assert(has_link?("Write"),
131              "failed to change access level on new share")
132       click_on "Revoke"
133     end
134     using_wait_time(Capybara.default_wait_time * 3) do
135       assert(page.has_no_text?(name),
136              "new share row still exists after being revoked")
137       assert_equal(start_rows.size - 1, share_rows.size,
138                    "revoking share did not remove row from sharing table")
139     end
140   end
141
142   test "project viewer can't see project sharing tab" do
143     show_project_using("project_viewer")
144     assert(page.has_no_link?("Sharing"),
145            "read-only project user sees sharing tab")
146   end
147
148   test "project owner can manage sharing for another user" do
149     add_user = api_fixture('users')['future_project_user']
150     new_name = ["first_name", "last_name"].map { |k| add_user[k] }.join(" ")
151
152     show_project_using("active")
153     click_on "Sharing"
154     add_share_and_check("users", new_name)
155     modify_share_and_check(new_name)
156   end
157
158   test "project owner can manage sharing for another group" do
159     new_name = api_fixture('groups')['future_project_viewing_group']['name']
160
161     show_project_using("active")
162     click_on "Sharing"
163     add_share_and_check("groups", new_name)
164     modify_share_and_check(new_name)
165   end
166
167   test "'share with group' listing does not offer projects" do
168     show_project_using("active")
169     click_on "Sharing"
170     click_on "Share with groups"
171     good_uuid = api_fixture("groups")["private"]["uuid"]
172     assert(page.has_selector?(".selectable[data-object-uuid=\"#{good_uuid}\"]"),
173            "'share with groups' listing missing owned user group")
174     bad_uuid = api_fixture("groups")["asubproject"]["uuid"]
175     assert(page.has_no_selector?(".selectable[data-object-uuid=\"#{bad_uuid}\"]"),
176            "'share with groups' listing includes project")
177   end
178 end