Fix 2.4.2 upgrade notes formatting refs #19330
[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, params: {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, params: {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, params: {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, params: {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, params: 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   [:admin, :active].each do |token|
85     test "using '#{token}', get token details via 'current'" do
86       authorize_with token
87       get :current
88       assert_response 200
89       assert_equal json_response['scopes'], ['all']
90     end
91   end
92
93   [# anyone can look up the token they're currently using
94    [:admin, :admin, 200, 200, 1],
95    [:active, :active, 200, 200, 1],
96    # cannot look up other tokens (even for same user) if not trustedclient
97    [:admin, :active, 403, 403],
98    [:admin, :admin_vm, 403, 403],
99    [:active, :admin, 403, 403],
100    # cannot look up other tokens for other users, regardless of trustedclient
101    [:admin_trustedclient, :active, 404, 200, 0],
102    [:active_trustedclient, :admin, 404, 200, 0],
103   ].each do |user, token, expect_get_response, expect_list_response, expect_list_items|
104     test "using '#{user}', get '#{token}' by uuid" do
105       authorize_with user
106       get :show, params: {
107         id: api_client_authorizations(token).uuid,
108       }
109       assert_response expect_get_response
110     end
111
112     test "using '#{user}', update '#{token}' by uuid" do
113       authorize_with user
114       put :update, params: {
115         id: api_client_authorizations(token).uuid,
116         api_client_authorization: {},
117       }
118       assert_response expect_get_response
119     end
120
121     test "using '#{user}', delete '#{token}' by uuid" do
122       authorize_with user
123       post :destroy, params: {
124         id: api_client_authorizations(token).uuid,
125       }
126       assert_response expect_get_response
127     end
128
129     test "using '#{user}', list '#{token}' by uuid" do
130       authorize_with user
131       get :index, params: {
132         filters: [['uuid','=',api_client_authorizations(token).uuid]],
133       }
134       assert_response expect_list_response
135       if expect_list_items
136         assert_equal assigns(:objects).length, expect_list_items
137         assert_equal json_response['items_available'], expect_list_items
138       end
139     end
140
141     if expect_list_items
142       test "using '#{user}', list '#{token}' by uuid with offset" do
143         authorize_with user
144         get :index, params: {
145           filters: [['uuid','=',api_client_authorizations(token).uuid]],
146           offset: expect_list_items,
147         }
148         assert_response expect_list_response
149         assert_equal json_response['items_available'], expect_list_items
150         assert_equal json_response['items'].length, 0
151       end
152     end
153
154     test "using '#{user}', list '#{token}' by token" do
155       authorize_with user
156       get :index, params: {
157         filters: [['api_token','=',api_client_authorizations(token).api_token]],
158       }
159       assert_response expect_list_response
160       if expect_list_items
161         assert_equal assigns(:objects).length, expect_list_items
162         assert_equal json_response['items_available'], expect_list_items
163       end
164     end
165   end
166
167   test "scoped token cannot change its own scopes" do
168     authorize_with :admin_vm
169     put :update, params: {
170       id: api_client_authorizations(:admin_vm).uuid,
171       api_client_authorization: {scopes: ['all']},
172     }
173     assert_response 403
174   end
175
176   test "token cannot change its own uuid" do
177     authorize_with :admin
178     put :update, params: {
179       id: api_client_authorizations(:admin).uuid,
180       api_client_authorization: {uuid: 'zzzzz-gj3su-zzzzzzzzzzzzzzz'},
181     }
182     assert_response 403
183   end
184
185   test "get current token" do
186     authorize_with :active
187     get :current
188     assert_response :success
189     assert_equal(json_response['api_token'],
190                  api_client_authorizations(:active).api_token)
191   end
192
193   test "get current token using SystemRootToken" do
194     Rails.configuration.SystemRootToken = "xyzzy-systemroottoken"
195     authorize_with_token Rails.configuration.SystemRootToken
196     get :current
197     assert_response :success
198     assert_equal(Rails.configuration.SystemRootToken, json_response['api_token'])
199     assert_not_empty(json_response['uuid'])
200   end
201
202   test "get current token, no auth" do
203     get :current
204     assert_response 401
205   end
206
207   # Tests regression #18801
208   test "select param is respected in 'show' response" do
209     authorize_with :active
210     get :show, params: {
211           id: api_client_authorizations(:active).uuid,
212           select: ["uuid"],
213         }
214     assert_response :success
215     assert_raises ActiveModel::MissingAttributeError do
216       assigns(:object).api_token
217     end
218     assert_nil json_response["expires_at"]
219     assert_nil json_response["api_token"]
220     assert_equal api_client_authorizations(:active).uuid, json_response["uuid"]
221   end
222 end