Refactor AdminNotifier.
[arvados.git] / services / api / test / functional / arvados / v1 / groups_controller_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::GroupsControllerTest < ActionController::TestCase
4
5   test "attempt to delete group without read or write access" do
6     authorize_with :active
7     post :destroy, id: groups(:empty_lonely_group).uuid
8     assert_response 404
9   end
10
11   test "attempt to delete group without write access" do
12     authorize_with :active
13     post :destroy, id: groups(:all_users).uuid
14     assert_response 403
15   end
16
17   test "get list of folders" do
18     authorize_with :active
19     get :index, filters: [['group_class', '=', 'folder']], format: :json
20     assert_response :success
21     group_uuids = []
22     json_response['items'].each do |group|
23       assert_equal 'folder', group['group_class']
24       group_uuids << group['uuid']
25     end
26     assert_includes group_uuids, groups(:afolder).uuid
27     assert_includes group_uuids, groups(:asubfolder).uuid
28     assert_not_includes group_uuids, groups(:system_group).uuid
29     assert_not_includes group_uuids, groups(:private).uuid
30   end
31
32   test "get list of groups that are not folders" do
33     authorize_with :active
34     get :index, filters: [['group_class', '=', nil]], format: :json
35     assert_response :success
36     group_uuids = []
37     json_response['items'].each do |group|
38       assert_equal nil, group['group_class']
39       group_uuids << group['uuid']
40     end
41     assert_not_includes group_uuids, groups(:afolder).uuid
42     assert_not_includes group_uuids, groups(:asubfolder).uuid
43     assert_includes group_uuids, groups(:private).uuid
44   end
45
46   test "get list of groups with bogus group_class" do
47     authorize_with :active
48     get :index, {
49       filters: [['group_class', '=', 'nogrouphasthislittleclass']],
50       format: :json,
51     }
52     assert_response :success
53     assert_equal [], json_response['items']
54     assert_equal 0, json_response['items_available']
55   end
56
57   test 'get group-owned objects' do
58     authorize_with :active
59     get :contents, {
60       id: groups(:afolder).uuid,
61       format: :json,
62       include_linked: true,
63     }
64     assert_response :success
65     assert_operator 2, :<=, json_response['items_available']
66     assert_operator 2, :<=, json_response['items'].count
67     kinds = json_response['items'].collect { |i| i['kind'] }.uniq
68     expect_kinds = %w'arvados#group arvados#specimen arvados#pipelineTemplate arvados#job'
69     assert_equal expect_kinds, (expect_kinds & kinds)
70   end
71
72   test 'get group-owned objects with limit' do
73     authorize_with :active
74     get :contents, {
75       id: groups(:afolder).uuid,
76       limit: 1,
77       format: :json,
78     }
79     assert_response :success
80     assert_operator 1, :<, json_response['items_available']
81     assert_equal 1, json_response['items'].count
82   end
83
84   test 'get group-owned objects with limit and offset' do
85     authorize_with :active
86     get :contents, {
87       id: groups(:afolder).uuid,
88       limit: 1,
89       offset: 12345,
90       format: :json,
91     }
92     assert_response :success
93     assert_operator 1, :<, json_response['items_available']
94     assert_equal 0, json_response['items'].count
95   end
96
97   test 'get group-owned objects with additional filter matching nothing' do
98     authorize_with :active
99     get :contents, {
100       id: groups(:afolder).uuid,
101       filters: [['uuid', 'in', ['foo_not_a_uuid','bar_not_a_uuid']]],
102       format: :json,
103     }
104     assert_response :success
105     assert_equal [], json_response['items']
106     assert_equal 0, json_response['items_available']
107   end
108
109   test 'get group-owned objects without include_linked' do
110     unexpected_uuid = specimens(:in_afolder_linked_from_asubfolder).uuid
111     authorize_with :active
112     get :contents, {
113       id: groups(:asubfolder).uuid,
114       format: :json,
115     }
116     assert_response :success
117     uuids = json_response['items'].collect { |i| i['uuid'] }
118     assert_equal nil, uuids.index(unexpected_uuid)
119   end
120
121   test 'get group-owned objects with include_linked' do
122     expected_uuid = specimens(:in_afolder_linked_from_asubfolder).uuid
123     authorize_with :active
124     get :contents, {
125       id: groups(:asubfolder).uuid,
126       include_linked: true,
127       format: :json,
128     }
129     assert_response :success
130     uuids = json_response['items'].collect { |i| i['uuid'] }
131     assert_includes uuids, expected_uuid, "Did not get #{expected_uuid}"
132
133     expected_name = links(:specimen_is_in_two_folders).name
134     found_specimen_name = false
135     assert(json_response['links'].any?,
136            "Expected a non-empty array of links in response")
137     json_response['links'].each do |link|
138       if link['head_uuid'] == expected_uuid
139         if link['name'] == expected_name
140           found_specimen_name = true
141         end
142       end
143     end
144     assert(found_specimen_name,
145            "Expected to find name '#{expected_name}' in response")
146   end
147
148   [false, true].each do |inc_ind|
149     test "get all pages of group-owned #{'and -linked ' if inc_ind}objects" do
150       authorize_with :active
151       limit = 5
152       offset = 0
153       items_available = nil
154       uuid_received = {}
155       owner_received = {}
156       while true
157         # Behaving badly here, using the same controller multiple
158         # times within a test.
159         @json_response = nil
160         get :contents, {
161           id: groups(:afolder).uuid,
162           include_linked: inc_ind,
163           limit: limit,
164           offset: offset,
165           format: :json,
166         }
167         assert_response :success
168         assert_operator(0, :<, json_response['items'].count,
169                         "items_available=#{items_available} but received 0 "\
170                         "items with offset=#{offset}")
171         items_available ||= json_response['items_available']
172         assert_equal(items_available, json_response['items_available'],
173                      "items_available changed between page #{offset/limit} "\
174                      "and page #{1+offset/limit}")
175         json_response['items'].each do |item|
176           uuid = item['uuid']
177           assert_equal(nil, uuid_received[uuid],
178                        "Received '#{uuid}' again on page #{1+offset/limit}")
179           uuid_received[uuid] = true
180           owner_received[item['owner_uuid']] = true
181           offset += 1
182           if not inc_ind
183             assert_equal groups(:afolder).uuid, item['owner_uuid']
184           end
185         end
186         break if offset >= items_available
187       end
188       if inc_ind
189         assert_operator 0, :<, (json_response.keys - [users(:active).uuid]).count,
190         "Set include_linked=true but did not receive any non-owned items"
191       end
192     end
193   end
194
195   %w(offset limit).each do |arg|
196     ['foo', '', '1234five', '0x10', '-8'].each do |val|
197       test "Raise error on bogus #{arg} parameter #{val.inspect}" do
198         authorize_with :active
199         get :contents, {
200           :id => groups(:afolder).uuid,
201           :format => :json,
202           arg => val,
203         }
204         assert_response 422
205       end
206     end
207   end
208
209 end