Update tests to match controller updates towards admin_required
[arvados.git] / services / api / test / functional / arvados / v1 / jobs_controller_test.rb
1 require 'test_helper'
2 load 'test/functional/arvados/v1/git_setup.rb'
3
4 class Arvados::V1::JobsControllerTest < ActionController::TestCase
5
6   include GitSetup
7
8   test "submit a job" do
9     authorize_with :active
10     post :create, job: {
11       script: "hash",
12       script_version: "master",
13       repository: "foo",
14       script_parameters: {}
15     }
16     assert_response :success
17     assert_not_nil assigns(:object)
18     new_job = JSON.parse(@response.body)
19     assert_not_nil new_job['uuid']
20     assert_not_nil new_job['script_version'].match(/^[0-9a-f]{40}$/)
21   end
22
23   test "normalize output and log uuids when creating job" do
24     authorize_with :active
25     post :create, job: {
26       script: "hash",
27       script_version: "master",
28       script_parameters: {},
29       repository: "foo",
30       started_at: Time.now,
31       finished_at: Time.now,
32       running: false,
33       success: true,
34       output: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy',
35       log: 'd41d8cd98f00b204e9800998ecf8427e+0+K@xyzzy'
36     }
37     assert_response :success
38     assert_not_nil assigns(:object)
39     new_job = JSON.parse(@response.body)
40     assert_equal 'd41d8cd98f00b204e9800998ecf8427e+0', new_job['log']
41     assert_equal 'd41d8cd98f00b204e9800998ecf8427e+0', new_job['output']
42     version = new_job['script_version']
43
44     # Make sure version doesn't get mangled by normalize
45     assert_not_nil version.match(/^[0-9a-f]{40}$/)
46     put :update, {
47       id: new_job['uuid'],
48       job: {
49         log: new_job['log']
50       }
51     }
52     assert_equal version, JSON.parse(@response.body)['script_version']
53   end
54
55   test "cancel a running job" do
56     # We need to verify that "cancel" creates a trigger file, so first
57     # let's make sure there is no stale trigger file.
58     begin
59       File.unlink(Rails.configuration.crunch_refresh_trigger)
60     rescue Errno::ENOENT
61     end
62
63     authorize_with :active
64     put :update, {
65       id: jobs(:running).uuid,
66       job: {
67         cancelled_at: 4.day.ago
68       }
69     }
70     assert_response :success
71     assert_not_nil assigns(:object)
72     job = JSON.parse(@response.body)
73     assert_not_nil job['uuid']
74     assert_not_nil job['cancelled_at']
75     assert_not_nil job['cancelled_by_user_uuid']
76     assert_not_nil job['cancelled_by_client_uuid']
77     assert_equal(true, Time.parse(job['cancelled_at']) > 1.minute.ago,
78                  'server should correct bogus cancelled_at ' +
79                  job['cancelled_at'])
80     assert_equal(true,
81                  File.exists?(Rails.configuration.crunch_refresh_trigger),
82                  'trigger file should be created when job is cancelled')
83
84     put :update, {
85       id: jobs(:running).uuid,
86       job: {
87         cancelled_at: nil
88       }
89     }
90     job = JSON.parse(@response.body)
91     assert_not_nil job['cancelled_at'], 'un-cancelled job stays cancelled'
92   end
93
94   test "update a job without failing script_version check" do
95     authorize_with :admin
96     put :update, {
97       id: jobs(:uses_nonexistent_script_version).uuid,
98       job: {
99         owner_uuid: users(:admin).uuid
100       }
101     }
102     assert_response :success
103     put :update, {
104       id: jobs(:uses_nonexistent_script_version).uuid,
105       job: {
106         owner_uuid: users(:active).uuid
107       }
108     }
109     assert_response :success
110   end
111
112   test "search jobs by uuid with >= query" do
113     authorize_with :active
114     get :index, {
115       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
116     }
117     assert_response :success
118     found = assigns(:objects).collect(&:uuid)
119     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
120     assert_equal false, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
121   end
122
123   test "search jobs by uuid with <= query" do
124     authorize_with :active
125     get :index, {
126       filters: [['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
127     }
128     assert_response :success
129     found = assigns(:objects).collect(&:uuid)
130     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
131     assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
132   end
133
134   test "search jobs by uuid with >= and <= query" do
135     authorize_with :active
136     get :index, {
137       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
138               ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
139     }
140     assert_response :success
141     found = assigns(:objects).collect(&:uuid)
142     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
143   end
144
145   test "search jobs by uuid with < query" do
146     authorize_with :active
147     get :index, {
148       filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
149     }
150     assert_response :success
151     found = assigns(:objects).collect(&:uuid)
152     assert_equal false, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
153     assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
154   end
155
156   test "search jobs by uuid with like query" do
157     authorize_with :active
158     get :index, {
159       filters: [['uuid', 'like', '%hmckwoma9pl%']]
160     }
161     assert_response :success
162     found = assigns(:objects).collect(&:uuid)
163     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
164   end
165
166   test "search jobs by uuid with 'in' query" do
167     authorize_with :active
168     get :index, {
169       filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
170                                 'zzzzz-8i9sb-pshmckwoma9plh7']]]
171     }
172     assert_response :success
173     found = assigns(:objects).collect(&:uuid)
174     assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
175                               'zzzzz-8i9sb-pshmckwoma9plh7']
176   end
177
178   test "search jobs by started_at with < query" do
179     authorize_with :active
180     get :index, {
181       filters: [['started_at', '<', Time.now.to_s]]
182     }
183     assert_response :success
184     found = assigns(:objects).collect(&:uuid)
185     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
186   end
187
188   test "search jobs by started_at with > query" do
189     authorize_with :active
190     get :index, {
191       filters: [['started_at', '>', Time.now.to_s]]
192     }
193     assert_response :success
194     assert_equal 0, assigns(:objects).count
195   end
196
197   test "search jobs by started_at with >= query on metric date" do
198     authorize_with :active
199     get :index, {
200       filters: [['started_at', '>=', '2014-01-01']]
201     }
202     assert_response :success
203     found = assigns(:objects).collect(&:uuid)
204     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
205   end
206
207   test "search jobs by started_at with >= query on metric date and time" do
208     authorize_with :active
209     get :index, {
210       filters: [['started_at', '>=', '2014-01-01 01:23:45']]
211     }
212     assert_response :success
213     found = assigns(:objects).collect(&:uuid)
214     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
215   end
216
217   test "search jobs with 'any' operator" do
218     authorize_with :active
219     get :index, {
220       where: { any: ['contains', 'pshmckw'] }
221     }
222     assert_response :success
223     found = assigns(:objects).collect(&:uuid)
224     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
225   end
226
227   test "search jobs by nonexistent column with < query" do
228     authorize_with :active
229     get :index, {
230       filters: [['is_borked', '<', 'fizzbuzz']]
231     }
232     assert_response 422
233   end
234
235
236 end