Merge branch 'master' into 1971-show-image-thumbnails
[arvados.git] / services / api / test / functional / arvados / v1 / collections_controller_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
4
5   test "should get index" do
6     authorize_with :active
7     get :index
8     assert_response :success
9     assert_not_nil assigns(:objects)
10   end
11
12   [0,1,2].each do |limit|
13     test "get index with limit=#{limit}" do
14       authorize_with :active
15       get :index, limit: limit
16       assert_response :success
17       assert_equal limit, assigns(:objects).count
18       resp = JSON.parse(@response.body)
19       assert_equal limit, resp['limit']
20     end
21   end
22
23   test "items.count == items_available" do
24     authorize_with :active
25     get :index, limit: 100000
26     assert_response :success
27     resp = JSON.parse(@response.body)
28     assert_equal resp['items_available'], assigns(:objects).length
29     assert_equal resp['items_available'], resp['items'].count
30     unique_uuids = resp['items'].collect { |i| i['uuid'] }.compact.uniq
31     assert_equal unique_uuids.count, resp['items'].count
32   end
33
34   test "get index with limit=2 offset=99999" do
35     # Assume there are not that many test fixtures.
36     authorize_with :active
37     get :index, limit: 2, offset: 99999
38     assert_response :success
39     assert_equal 0, assigns(:objects).count
40     resp = JSON.parse(@response.body)
41     assert_equal 2, resp['limit']
42     assert_equal 99999, resp['offset']
43   end
44
45   test "should create" do
46     authorize_with :active
47     test_collection = {
48       manifest_text: <<-EOS
49 . d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt
50 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
51 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
52 ./baz acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
53 EOS
54     }
55     test_collection[:uuid] =
56       Digest::MD5.hexdigest(test_collection[:manifest_text]) +
57       '+' +
58       test_collection[:manifest_text].length.to_s
59     post :create, {
60       collection: test_collection
61     }
62     assert_response :success
63     assert_nil assigns(:objects)
64
65     get :show, {
66       id: test_collection[:uuid]
67     }
68     assert_response :success
69     assert_not_nil assigns(:object)
70     resp = JSON.parse(@response.body)
71     assert_equal test_collection[:uuid], resp['uuid']
72     assert_equal test_collection[:manifest_text], resp['manifest_text']
73     assert_equal 9, resp['data_size']
74     assert_equal [['.', 'foo.txt', 0],
75                   ['.', 'bar.txt', 6],
76                   ['./baz', 'bar.txt', 3]], resp['files']
77   end
78
79   test "list of files is correct for empty manifest" do
80     authorize_with :active
81     test_collection = {
82       manifest_text: "",
83       uuid: "d41d8cd98f00b204e9800998ecf8427e+0"
84     }
85     post :create, {
86       collection: test_collection
87     }
88     assert_response :success
89
90     get :show, {
91       id: "d41d8cd98f00b204e9800998ecf8427e+0"
92     }
93     assert_response :success
94     resp = JSON.parse(@response.body)
95     assert_equal [], resp['files']
96   end
97
98   test "create with owner_uuid set to owned group" do
99     authorize_with :active
100     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
101     post :create, {
102       collection: {
103         owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
104         manifest_text: manifest_text,
105         uuid: "d30fe8ae534397864cb96c544f4cf102"
106       }
107     }
108     assert_response :success
109     resp = JSON.parse(@response.body)
110     assert_equal 'zzzzz-tpzed-000000000000000', resp['owner_uuid']
111   end
112
113   test "create with owner_uuid set to group i can_manage" do
114     authorize_with :active
115     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
116     post :create, {
117       collection: {
118         owner_uuid: 'zzzzz-j7d0g-8ulrifv67tve5sx',
119         manifest_text: manifest_text,
120         uuid: "d30fe8ae534397864cb96c544f4cf102"
121       }
122     }
123     assert_response :success
124     resp = JSON.parse(@response.body)
125     assert_equal 'zzzzz-tpzed-000000000000000', resp['owner_uuid']
126   end
127
128   test "create with owner_uuid set to group with no can_manage permission" do
129     authorize_with :active
130     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
131     post :create, {
132       collection: {
133         owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
134         manifest_text: manifest_text,
135         uuid: "d30fe8ae534397864cb96c544f4cf102"
136       }
137     }
138     assert_response 403
139   end
140
141   test "admin create with owner_uuid set to group with no permission" do
142     authorize_with :admin
143     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
144     post :create, {
145       collection: {
146         owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
147         manifest_text: manifest_text,
148         uuid: "d30fe8ae534397864cb96c544f4cf102"
149       }
150     }
151     assert_response :success
152   end
153
154   test "should create with collection passed as json" do
155     authorize_with :active
156     post :create, {
157       collection: <<-EOS
158       {
159         "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
160         "uuid":"d30fe8ae534397864cb96c544f4cf102"\
161       }
162       EOS
163     }
164     assert_response :success
165   end
166
167   test "should fail to create with checksum mismatch" do
168     authorize_with :active
169     post :create, {
170       collection: <<-EOS
171       {
172         "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
173         "uuid":"d30fe8ae534397864cb96c544f4cf102"\
174       }
175       EOS
176     }
177     assert_response 422
178   end
179
180   test "get full provenance for baz file" do
181     authorize_with :active
182     get :provenance, uuid: 'ea10d51bcf88862dbcc36eb292017dfd+45'
183     assert_response :success
184     resp = JSON.parse(@response.body)
185     assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
186     assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
187     assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
188     assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
189     assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
190   end
191
192   test "get no provenance for foo file" do
193     # spectator user cannot even see baz collection
194     authorize_with :spectator
195     get :provenance, uuid: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
196     assert_response 404
197   end
198
199   test "get partial provenance for baz file" do
200     # spectator user can see bar->baz job, but not foo->bar job
201     authorize_with :spectator
202     get :provenance, uuid: 'ea10d51bcf88862dbcc36eb292017dfd+45'
203     assert_response :success
204     resp = JSON.parse(@response.body)
205     assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
206     assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
207     assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq']     # bar->baz
208     assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon']         # foo->bar
209     assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
210   end
211
212   test "search collections with 'any' operator" do
213     authorize_with :active
214     get :index, {
215       where: { any: ['contains', '7f9102c395f4ffc5e3'] }
216     }
217     assert_response :success
218     found = assigns(:objects).collect(&:uuid)
219     assert_equal 1, found.count
220     assert_equal true, !!found.index('1f4b0bc7583c2a7f9102c395f4ffc5e3+45')
221   end
222
223 end