8784: Fix test for latest firefox.
[arvados.git] / services / api / test / functional / arvados / v1 / filters_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::FiltersTest < ActionController::TestCase
4   test '"not in" filter passes null values' do
5     @controller = Arvados::V1::GroupsController.new
6     authorize_with :admin
7     get :index, {
8       filters: [ ['group_class', 'not in', ['project']] ],
9       controller: 'groups',
10     }
11     assert_response :success
12     found = assigns(:objects)
13     assert_includes(found.collect(&:group_class), nil,
14                     "'group_class not in ['project']' filter should pass null")
15   end
16
17   test 'error message for non-array element in filters array' do
18     @controller = Arvados::V1::CollectionsController.new
19     authorize_with :active
20     get :index, {
21       filters: [{bogus: 'filter'}],
22     }
23     assert_response 422
24     assert_match(/Invalid element in filters array/,
25                  json_response['errors'].join(' '))
26   end
27
28   test 'error message for full text search on a specific column' do
29     @controller = Arvados::V1::CollectionsController.new
30     authorize_with :active
31     get :index, {
32       filters: [['uuid', '@@', 'abcdef']],
33     }
34     assert_response 422
35     assert_match(/not supported/, json_response['errors'].join(' '))
36   end
37
38   test 'difficult characters in full text search' do
39     @controller = Arvados::V1::CollectionsController.new
40     authorize_with :active
41     get :index, {
42       filters: [['any', '@@', 'a|b"c']],
43     }
44     assert_response :success
45     # (Doesn't matter so much which results are returned.)
46   end
47
48   test 'array operand in full text search' do
49     @controller = Arvados::V1::CollectionsController.new
50     authorize_with :active
51     get :index, {
52       filters: [['any', '@@', ['abc', 'def']]],
53     }
54     assert_response 422
55     assert_match(/not supported/, json_response['errors'].join(' '))
56   end
57
58   test 'api responses provide timestamps with nanoseconds' do
59     @controller = Arvados::V1::CollectionsController.new
60     authorize_with :active
61     get :index
62     assert_response :success
63     assert_not_empty json_response['items']
64     json_response['items'].each do |item|
65       %w(created_at modified_at).each do |attr|
66         # Pass fixtures with null timestamps.
67         next if item[attr].nil?
68         assert_match(/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d.\d{9}Z$/, item[attr])
69       end
70     end
71   end
72
73   %w(< > <= >= =).each do |operator|
74     test "timestamp #{operator} filters work with nanosecond precision" do
75       # Python clients like Node Manager rely on this exact format.
76       # If you must change this format for some reason, make sure you
77       # coordinate the change with them.
78       expect_match = !!operator.index('=')
79       mine = act_as_user users(:active) do
80         Collection.create!(manifest_text: '')
81       end
82       timestamp = mine.modified_at.strftime('%Y-%m-%dT%H:%M:%S.%NZ')
83       @controller = Arvados::V1::CollectionsController.new
84       authorize_with :active
85       get :index, {
86         filters: [['modified_at', operator, timestamp],
87                   ['uuid', '=', mine.uuid]],
88       }
89       assert_response :success
90       uuids = json_response['items'].map { |item| item['uuid'] }
91       if expect_match
92         assert_includes uuids, mine.uuid
93       else
94         assert_not_includes uuids, mine.uuid
95       end
96     end
97   end
98
99   test "full text search with count='none'" do
100     @controller = Arvados::V1::GroupsController.new
101     authorize_with :admin
102
103     get :contents, {
104       format: :json,
105       count: 'none',
106       limit: 1000,
107       filters: [['any', '@@', Rails.configuration.uuid_prefix]],
108     }
109
110     assert_response :success
111
112     all_objects = Hash.new(0)
113     json_response['items'].map{|o| o['kind']}.each{|t| all_objects[t] += 1}
114
115     assert_equal true, all_objects['arvados#group']>0
116     assert_equal true, all_objects['arvados#job']>0
117     assert_equal true, all_objects['arvados#pipelineInstance']>0
118     assert_equal true, all_objects['arvados#pipelineTemplate']>0
119
120     # Perform test again mimicking a second page request with:
121     # last_object_class = PipelineInstance
122     #   and hence groups and jobs should not be included in the response
123     # offset = 5, which means first 5 pipeline instances were already received in page 1
124     #   and hence the remaining pipeline instances and all other object types should be included in the response
125
126     @test_counter = 0  # Reset executed action counter
127
128     @controller = Arvados::V1::GroupsController.new
129
130     get :contents, {
131       format: :json,
132       count: 'none',
133       limit: 1000,
134       offset: '5',
135       last_object_class: 'PipelineInstance',
136       filters: [['any', '@@', Rails.configuration.uuid_prefix]],
137     }
138
139     assert_response :success
140
141     second_page = Hash.new(0)
142     json_response['items'].map{|o| o['kind']}.each{|t| second_page[t] += 1}
143
144     assert_equal false, second_page.include?('arvados#group')
145     assert_equal false, second_page.include?('arvados#job')
146     assert_equal true, second_page['arvados#pipelineInstance']>0
147     assert_equal all_objects['arvados#pipelineInstance'], second_page['arvados#pipelineInstance']+5
148     assert_equal true, second_page['arvados#pipelineTemplate']>0
149   end
150 end