Merge branch 'master' into 9998-unsigned_manifest
[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 end