Merge branch '8784-dir-listings'
[arvados.git] / services / api / test / functional / arvados / v1 / api_client_authorizations_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
7 class Arvados::V1::ApiClientAuthorizationsControllerTest < ActionController::TestCase
8   test "should get index" do
9     authorize_with :active_trustedclient
10     get :index
11     assert_response :success
12   end
13
14   test "should not get index with expired auth" do
15     authorize_with :expired
16     get :index, format: :json
17     assert_response 401
18   end
19
20   test "should not get index from untrusted client" do
21     authorize_with :active
22     get :index
23     assert_response 403
24   end
25
26   test "create system auth" do
27     authorize_with :admin_trustedclient
28     post :create_system_auth, scopes: '["test"]'
29     assert_response :success
30     assert_not_nil JSON.parse(@response.body)['uuid']
31   end
32
33   test "prohibit create system auth with token from non-trusted client" do
34     authorize_with :admin
35     post :create_system_auth, scopes: '["test"]'
36     assert_response 403
37   end
38
39   test "prohibit create system auth by non-admin" do
40     authorize_with :active
41     post :create_system_auth, scopes: '["test"]'
42     assert_response 403
43   end
44
45   def assert_found_tokens(auth, search_params, expected)
46     authorize_with auth
47     expected_tokens = expected.map do |name|
48       api_client_authorizations(name).api_token
49     end
50     get :index, search_params
51     assert_response :success
52     got_tokens = JSON.parse(@response.body)['items']
53       .map { |a| a['api_token'] }
54     assert_equal(expected_tokens.sort, got_tokens.sort,
55                  "wrong results for #{search_params.inspect}")
56   end
57
58   # Three-tuples with auth to use, scopes to find, and expected tokens.
59   # Make two tests for each tuple, one searching with where and the other
60   # with filter.
61   [[:admin_trustedclient, [], [:admin_noscope]],
62    [:active_trustedclient, ["GET /arvados/v1/users"], [:active_userlist]],
63    [:active_trustedclient,
64     ["POST /arvados/v1/api_client_authorizations",
65      "GET /arvados/v1/api_client_authorizations"],
66     [:active_apitokens]],
67   ].each do |auth, scopes, expected|
68     test "#{auth.to_s} can find auths where scopes=#{scopes.inspect}" do
69       assert_found_tokens(auth, {where: {scopes: scopes}}, expected)
70     end
71
72     test "#{auth.to_s} can find auths filtered with scopes=#{scopes.inspect}" do
73       assert_found_tokens(auth, {filters: [['scopes', '=', scopes]]}, expected)
74     end
75
76     test "#{auth.to_s} offset works with filter scopes=#{scopes.inspect}" do
77       assert_found_tokens(auth, {
78                             offset: expected.length,
79                             filters: [['scopes', '=', scopes]]
80                           }, [])
81     end
82   end
83
84   [# anyone can look up the token they're currently using
85    [:admin, :admin, 200, 200, 1],
86    [:active, :active, 200, 200, 1],
87    # cannot look up other tokens (even for same user) if not trustedclient
88    [:admin, :active, 403, 403],
89    [:admin, :admin_vm, 403, 403],
90    [:active, :admin, 403, 403],
91    # cannot look up other tokens for other users, regardless of trustedclient
92    [:admin_trustedclient, :active, 404, 200, 0],
93    [:active_trustedclient, :admin, 404, 200, 0],
94   ].each do |user, token, expect_get_response, expect_list_response, expect_list_items|
95     test "using '#{user}', get '#{token}' by uuid" do
96       authorize_with user
97       get :show, {
98         id: api_client_authorizations(token).uuid,
99       }
100       assert_response expect_get_response
101     end
102
103     test "using '#{user}', update '#{token}' by uuid" do
104       authorize_with user
105       put :update, {
106         id: api_client_authorizations(token).uuid,
107         api_client_authorization: {},
108       }
109       assert_response expect_get_response
110     end
111
112     test "using '#{user}', delete '#{token}' by uuid" do
113       authorize_with user
114       post :destroy, {
115         id: api_client_authorizations(token).uuid,
116       }
117       assert_response expect_get_response
118     end
119
120     test "using '#{user}', list '#{token}' by uuid" do
121       authorize_with user
122       get :index, {
123         filters: [['uuid','=',api_client_authorizations(token).uuid]],
124       }
125       assert_response expect_list_response
126       if expect_list_items
127         assert_equal assigns(:objects).length, expect_list_items
128         assert_equal json_response['items_available'], expect_list_items
129       end
130     end
131
132     if expect_list_items
133       test "using '#{user}', list '#{token}' by uuid with offset" do
134         authorize_with user
135         get :index, {
136           filters: [['uuid','=',api_client_authorizations(token).uuid]],
137           offset: expect_list_items,
138         }
139         assert_response expect_list_response
140         assert_equal json_response['items_available'], expect_list_items
141         assert_equal json_response['items'].length, 0
142       end
143     end
144
145     test "using '#{user}', list '#{token}' by token" do
146       authorize_with user
147       get :index, {
148         filters: [['api_token','=',api_client_authorizations(token).api_token]],
149       }
150       assert_response expect_list_response
151       if expect_list_items
152         assert_equal assigns(:objects).length, expect_list_items
153         assert_equal json_response['items_available'], expect_list_items
154       end
155     end
156   end
157
158   test "scoped token cannot change its own scopes" do
159     authorize_with :admin_vm
160     put :update, {
161       id: api_client_authorizations(:admin_vm).uuid,
162       api_client_authorization: {scopes: ['all']},
163     }
164     assert_response 403
165   end
166
167   test "token cannot change its own uuid" do
168     authorize_with :admin
169     put :update, {
170       id: api_client_authorizations(:admin).uuid,
171       api_client_authorization: {uuid: 'zzzzz-gj3su-zzzzzzzzzzzzzzz'},
172     }
173     assert_response 403
174   end
175
176   test "get current token" do
177     authorize_with :active
178     get :current
179     assert_response :success
180     assert_equal(json_response['api_token'],
181                  api_client_authorizations(:active).api_token)
182   end
183
184   test "get current token, no auth" do
185     get :current
186     assert_response 401
187   end
188 end