Merge branch 'master' into 4904-arv-web
[arvados.git] / apps / workbench / test / controllers / projects_controller_test.rb
1 require 'test_helper'
2 require 'helpers/share_object_helper'
3
4 class ProjectsControllerTest < ActionController::TestCase
5   include ShareObjectHelper
6
7   test "invited user is asked to sign user agreements on front page" do
8     get :index, {}, session_for(:inactive)
9     assert_response :redirect
10     assert_match(/^#{Regexp.escape(user_agreements_url)}\b/,
11                  @response.redirect_url,
12                  "Inactive user was not redirected to user_agreements page")
13   end
14
15   test "uninvited user is asked to wait for activation" do
16     get :index, {}, session_for(:inactive_uninvited)
17     assert_response :redirect
18     assert_match(/^#{Regexp.escape(inactive_users_url)}\b/,
19                  @response.redirect_url,
20                  "Uninvited user was not redirected to inactive user page")
21   end
22
23   [[:active, true],
24    [:project_viewer, false]].each do |which_user, should_show|
25     test "create subproject button #{'not ' unless should_show} shown to #{which_user}" do
26       readonly_project_uuid = api_fixture('groups')['aproject']['uuid']
27       get :show, {
28         id: readonly_project_uuid
29       }, session_for(which_user)
30       buttons = css_select('[data-method=post]').select do |el|
31         el.attributes['href'].match /project.*owner_uuid.*#{readonly_project_uuid}/
32       end
33       if should_show
34         assert_not_empty(buttons, "did not offer to create a subproject")
35       else
36         assert_empty(buttons.collect(&:to_s),
37                      "offered to create a subproject in a non-writable project")
38       end
39     end
40   end
41
42   test "sharing a project with a user and group" do
43     uuid_list = [api_fixture("groups")["future_project_viewing_group"]["uuid"],
44                  api_fixture("users")["future_project_user"]["uuid"]]
45     post(:share_with, {
46            id: api_fixture("groups")["asubproject"]["uuid"],
47            uuids: uuid_list,
48            format: "json"},
49          session_for(:active))
50     assert_response :success
51     assert_equal(uuid_list, json_response["success"])
52   end
53
54   test "user with project read permission can't add permissions" do
55     share_uuid = api_fixture("users")["spectator"]["uuid"]
56     post(:share_with, {
57            id: api_fixture("groups")["aproject"]["uuid"],
58            uuids: [share_uuid],
59            format: "json"},
60          session_for(:project_viewer))
61     assert_response 422
62     assert(json_response["errors"].andand.
63              any? { |msg| msg.start_with?("#{share_uuid}: ") },
64            "JSON response missing properly formatted sharing error")
65   end
66
67   test "admin can_manage aproject" do
68     assert user_can_manage(:admin, api_fixture("groups")["aproject"])
69   end
70
71   test "owner can_manage aproject" do
72     assert user_can_manage(:active, api_fixture("groups")["aproject"])
73   end
74
75   test "owner can_manage asubproject" do
76     assert user_can_manage(:active, api_fixture("groups")["asubproject"])
77   end
78
79   test "viewer can't manage aproject" do
80     refute user_can_manage(:project_viewer, api_fixture("groups")["aproject"])
81   end
82
83   test "viewer can't manage asubproject" do
84     refute user_can_manage(:project_viewer, api_fixture("groups")["asubproject"])
85   end
86
87   test "subproject_admin can_manage asubproject" do
88     assert user_can_manage(:subproject_admin, api_fixture("groups")["asubproject"])
89   end
90
91   test "detect ownership loop in project breadcrumbs" do
92     # This test has an arbitrary time limit -- otherwise we'd just sit
93     # here forever instead of reporting that the loop was not
94     # detected. The test passes quickly, but fails slowly.
95     Timeout::timeout 10 do
96       get(:show,
97           { id: api_fixture("groups")["project_owns_itself"]["uuid"] },
98           session_for(:admin))
99     end
100     assert_response :success
101   end
102
103   test "project admin can remove items from the project" do
104     coll_key = "collection_to_remove_from_subproject"
105     coll_uuid = api_fixture("collections")[coll_key]["uuid"]
106     delete(:remove_item,
107            { id: api_fixture("groups")["asubproject"]["uuid"],
108              item_uuid: coll_uuid,
109              format: "js" },
110            session_for(:subproject_admin))
111     assert_response :success
112     assert_match(/\b#{coll_uuid}\b/, @response.body,
113                  "removed object not named in response")
114   end
115
116   test 'projects#show tab infinite scroll partial obeys limit' do
117     get_contents_rows(limit: 1, filters: [['uuid','is_a',['arvados#job']]])
118     assert_response :success
119     assert_equal(1, json_response['content'].scan('<tr').count,
120                  "Did not get exactly one row")
121   end
122
123   ['', ' asc', ' desc'].each do |direction|
124     test "projects#show tab partial orders correctly by #{direction}" do
125       _test_tab_content_order direction
126     end
127   end
128
129   def _test_tab_content_order direction
130     get_contents_rows(limit: 100,
131                       order: "created_at#{direction}",
132                       filters: [['uuid','is_a',['arvados#job',
133                                                 'arvados#pipelineInstance']]])
134     assert_response :success
135     not_grouped_by_kind = nil
136     last_timestamp = nil
137     last_kind = nil
138     found_kind = {}
139     json_response['content'].scan /<tr[^>]+>/ do |tr_tag|
140       found_timestamps = 0
141       tr_tag.scan(/\ data-object-created-at=\"(.*?)\"/).each do |t,|
142         if last_timestamp
143           correct_operator = / desc$/ =~ direction ? :>= : :<=
144           assert_operator(last_timestamp, correct_operator, t,
145                           "Rows are not sorted by created_at#{direction}")
146         end
147         last_timestamp = t
148         found_timestamps += 1
149       end
150       assert_equal(1, found_timestamps,
151                    "Content row did not have exactly one timestamp")
152
153       # Confirm that the test for timestamp ordering couldn't have
154       # passed merely because the test fixtures have convenient
155       # timestamps (e.g., there is only one pipeline and one job in
156       # the project being tested, or there are no pipelines at all in
157       # the project being tested):
158       tr_tag.scan /\ data-kind=\"(.*?)\"/ do |kind|
159         if last_kind and last_kind != kind and found_kind[kind]
160           # We saw this kind before, then a different kind, then
161           # this kind again. That means objects are not grouped by
162           # kind.
163           not_grouped_by_kind = true
164         end
165         found_kind[kind] ||= 0
166         found_kind[kind] += 1
167         last_kind = kind
168       end
169     end
170     assert_equal(true, not_grouped_by_kind,
171                  "Could not confirm that results are not grouped by kind")
172   end
173
174   def get_contents_rows params
175     params = {
176       id: api_fixture('users')['active']['uuid'],
177       partial: :contents_rows,
178       format: :json,
179     }.merge(params)
180     encoded_params = Hash[params.map { |k,v|
181                             [k, (v.is_a?(Array) || v.is_a?(Hash)) ? v.to_json : v]
182                           }]
183     get :show, encoded_params, session_for(:active)
184   end
185 end