Merge branch 'master' into 2333-crunch-dispatch-token-management
[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 "get index with limit=2 offset=99999" do
24     # Assume there are not that many test fixtures.
25     authorize_with :active
26     get :index, limit: 2, offset: 99999
27     assert_response :success
28     assert_equal 0, assigns(:objects).count
29     resp = JSON.parse(@response.body)
30     assert_equal 2, resp['limit']
31     assert_equal 99999, resp['offset']
32   end
33
34   test "should create" do
35     authorize_with :active
36     test_collection = {
37       manifest_text: <<-EOS
38 . d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt
39 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
40 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
41 ./baz acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
42 EOS
43     }
44     test_collection[:uuid] =
45       Digest::MD5.hexdigest(test_collection[:manifest_text]) +
46       '+' +
47       test_collection[:manifest_text].length.to_s
48     post :create, {
49       collection: test_collection
50     }
51     assert_response :success
52     assert_nil assigns(:objects)
53
54     get :show, {
55       id: test_collection[:uuid]
56     }
57     assert_response :success
58     assert_not_nil assigns(:object)
59     resp = JSON.parse(@response.body)
60     assert_equal test_collection[:uuid], resp['uuid']
61     assert_equal test_collection[:manifest_text], resp['manifest_text']
62     assert_equal 9, resp['data_size']
63     assert_equal [['.', 'foo.txt', 0],
64                   ['.', 'bar.txt', 6],
65                   ['./baz', 'bar.txt', 3]], resp['files']
66   end
67
68   test "list of files is correct for empty manifest" do
69     authorize_with :active
70     test_collection = {
71       manifest_text: "",
72       uuid: "d41d8cd98f00b204e9800998ecf8427e+0"
73     }
74     post :create, {
75       collection: test_collection
76     }
77     assert_response :success
78
79     get :show, {
80       id: "d41d8cd98f00b204e9800998ecf8427e+0"
81     }
82     assert_response :success
83     resp = JSON.parse(@response.body)
84     assert_equal [], resp['files']
85   end
86
87   test "create with owner_uuid set to owned group" do
88     authorize_with :active
89     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
90     post :create, {
91       collection: {
92         owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
93         manifest_text: manifest_text,
94         uuid: "d30fe8ae534397864cb96c544f4cf102"
95       }
96     }
97     assert_response :success
98     resp = JSON.parse(@response.body)
99     assert_equal 'zzzzz-tpzed-000000000000000', resp['owner_uuid']
100   end
101
102   test "create with owner_uuid set to group i can_manage" do
103     authorize_with :active
104     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
105     post :create, {
106       collection: {
107         owner_uuid: 'zzzzz-j7d0g-8ulrifv67tve5sx',
108         manifest_text: manifest_text,
109         uuid: "d30fe8ae534397864cb96c544f4cf102"
110       }
111     }
112     assert_response :success
113     resp = JSON.parse(@response.body)
114     assert_equal 'zzzzz-tpzed-000000000000000', resp['owner_uuid']
115   end
116
117   test "create with owner_uuid set to group with no can_manage permission" do
118     authorize_with :active
119     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
120     post :create, {
121       collection: {
122         owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
123         manifest_text: manifest_text,
124         uuid: "d30fe8ae534397864cb96c544f4cf102"
125       }
126     }
127     assert_response 403
128   end
129
130   test "admin create with owner_uuid set to group with no permission" do
131     authorize_with :admin
132     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
133     post :create, {
134       collection: {
135         owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
136         manifest_text: manifest_text,
137         uuid: "d30fe8ae534397864cb96c544f4cf102"
138       }
139     }
140     assert_response :success
141   end
142
143   test "should create with collection passed as json" do
144     authorize_with :active
145     post :create, {
146       collection: <<-EOS
147       {
148         "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
149         "uuid":"d30fe8ae534397864cb96c544f4cf102"\
150       }
151       EOS
152     }
153     assert_response :success
154   end
155
156   test "should fail to create with checksum mismatch" do
157     authorize_with :active
158     post :create, {
159       collection: <<-EOS
160       {
161         "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
162         "uuid":"d30fe8ae534397864cb96c544f4cf102"\
163       }
164       EOS
165     }
166     assert_response 422
167   end
168
169   test "get full provenance for baz file" do
170     authorize_with :active
171     get :provenance, uuid: 'ea10d51bcf88862dbcc36eb292017dfd+45'
172     assert_response :success
173     resp = JSON.parse(@response.body)
174     assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
175     assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
176     assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
177     assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
178     assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
179   end
180
181   test "get no provenance for foo file" do
182     # spectator user cannot even see baz collection
183     authorize_with :spectator
184     get :provenance, uuid: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
185     assert_response 404
186   end
187
188   test "get partial provenance for baz file" do
189     # spectator user can see bar->baz job, but not foo->bar job
190     authorize_with :spectator
191     get :provenance, uuid: 'ea10d51bcf88862dbcc36eb292017dfd+45'
192     assert_response :success
193     resp = JSON.parse(@response.body)
194     assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
195     assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
196     assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq']     # bar->baz
197     assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon']         # foo->bar
198     assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
199   end
200
201 end