19146: Add can_write/can_manage to users#list, fix select=can_*.
[arvados.git] / services / api / test / functional / arvados / v1 / jobs_controller_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6 require 'helpers/git_test_helper'
7
8 class Arvados::V1::JobsControllerTest < ActionController::TestCase
9
10   test "search jobs by uuid with >= query" do
11     authorize_with :active
12     get :index, params: {
13       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
14     }
15     assert_response :success
16     found = assigns(:objects).collect(&:uuid)
17     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
18     assert_equal false, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
19   end
20
21   test "search jobs by uuid with <= query" do
22     authorize_with :active
23     get :index, params: {
24       filters: [['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
25     }
26     assert_response :success
27     found = assigns(:objects).collect(&:uuid)
28     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
29     assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
30   end
31
32   test "search jobs by uuid with >= and <= query" do
33     authorize_with :active
34     get :index, params: {
35       filters: [['uuid', '>=', 'zzzzz-8i9sb-pshmckwoma9plh7'],
36               ['uuid', '<=', 'zzzzz-8i9sb-pshmckwoma9plh7']]
37     }
38     assert_response :success
39     found = assigns(:objects).collect(&:uuid)
40     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
41   end
42
43   test "search jobs by uuid with < query" do
44     authorize_with :active
45     get :index, params: {
46       filters: [['uuid', '<', 'zzzzz-8i9sb-pshmckwoma9plh7']]
47     }
48     assert_response :success
49     found = assigns(:objects).collect(&:uuid)
50     assert_equal false, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
51     assert_equal true, !!found.index('zzzzz-8i9sb-4cf0nhn6xte809j')
52   end
53
54   test "search jobs by uuid with like query" do
55     authorize_with :active
56     get :index, params: {
57       filters: [['uuid', 'like', '%hmckwoma9pl%']]
58     }
59     assert_response :success
60     found = assigns(:objects).collect(&:uuid)
61     assert_equal found, ['zzzzz-8i9sb-pshmckwoma9plh7']
62   end
63
64   test "search jobs by uuid with 'in' query" do
65     authorize_with :active
66     get :index, params: {
67       filters: [['uuid', 'in', ['zzzzz-8i9sb-4cf0nhn6xte809j',
68                                 'zzzzz-8i9sb-pshmckwoma9plh7']]]
69     }
70     assert_response :success
71     found = assigns(:objects).collect(&:uuid)
72     assert_equal found.sort, ['zzzzz-8i9sb-4cf0nhn6xte809j',
73                               'zzzzz-8i9sb-pshmckwoma9plh7']
74   end
75
76   test "search jobs by uuid with 'not in' query" do
77     exclude_uuids = [jobs(:running).uuid,
78                      jobs(:running_cancelled).uuid]
79     authorize_with :active
80     get :index, params: {
81       filters: [['uuid', 'not in', exclude_uuids]]
82     }
83     assert_response :success
84     found = assigns(:objects).collect(&:uuid)
85     assert_not_empty found, "'not in' query returned nothing"
86     assert_empty(found & exclude_uuids,
87                  "'not in' query returned uuids I asked not to get")
88   end
89
90   ['=', '!='].each do |operator|
91     [['uuid', 'zzzzz-8i9sb-pshmckwoma9plh7'],
92      ['output', nil]].each do |attr, operand|
93       test "search jobs with #{attr} #{operator} #{operand.inspect} query" do
94         authorize_with :active
95         get :index, params: {
96           filters: [[attr, operator, operand]]
97         }
98         assert_response :success
99         values = assigns(:objects).collect { |x| x.send(attr) }
100         assert_not_empty values, "query should return non-empty result"
101         if operator == '='
102           assert_empty values - [operand], "query results do not satisfy query"
103         else
104           assert_empty values & [operand], "query results do not satisfy query"
105         end
106       end
107     end
108   end
109
110   test "search jobs by started_at with < query" do
111     authorize_with :active
112     get :index, params: {
113       filters: [['started_at', '<', Time.now.to_s]]
114     }
115     assert_response :success
116     found = assigns(:objects).collect(&:uuid)
117     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
118   end
119
120   test "search jobs by started_at with > query" do
121     authorize_with :active
122     get :index, params: {
123       filters: [['started_at', '>', Time.now.to_s]]
124     }
125     assert_response :success
126     assert_equal 0, assigns(:objects).count
127   end
128
129   test "search jobs by started_at with >= query on metric date" do
130     authorize_with :active
131     get :index, params: {
132       filters: [['started_at', '>=', '2014-01-01']]
133     }
134     assert_response :success
135     found = assigns(:objects).collect(&:uuid)
136     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
137   end
138
139   test "search jobs by started_at with >= query on metric date and time" do
140     authorize_with :active
141     get :index, params: {
142       filters: [['started_at', '>=', '2014-01-01 01:23:45']]
143     }
144     assert_response :success
145     found = assigns(:objects).collect(&:uuid)
146     assert_equal true, !!found.index('zzzzz-8i9sb-pshmckwoma9plh7')
147   end
148
149   test "search jobs with 'any' operator" do
150     authorize_with :active
151     get :index, params: {
152       where: { any: ['contains', 'pshmckw'] }
153     }
154     assert_response :success
155     found = assigns(:objects).collect(&:uuid)
156     assert_equal 0, found.index('zzzzz-8i9sb-pshmckwoma9plh7')
157     assert_equal 1, found.count
158   end
159
160   test "search jobs by nonexistent column with < query" do
161     authorize_with :active
162     get :index, params: {
163       filters: [['is_borked', '<', 'fizzbuzz']]
164     }
165     assert_response 422
166   end
167
168   [:spectator, :admin].each_with_index do |which_token, i|
169     test "get job queue as #{which_token} user" do
170       authorize_with which_token
171       get :queue
172       assert_response :success
173       assert_equal 0, assigns(:objects).count
174     end
175   end
176
177   test "job includes assigned nodes" do
178     authorize_with :active
179     get :show, params: {id: jobs(:nearly_finished_job).uuid}
180     assert_response :success
181     assert_equal([nodes(:busy).uuid], json_response["node_uuids"])
182   end
183
184   test 'get job with components' do
185     authorize_with :active
186     get :show, params: {id: jobs(:running_job_with_components).uuid}
187     assert_response :success
188     assert_not_nil json_response["components"]
189     assert_equal ["component1", "component2"], json_response["components"].keys
190   end
191 end