1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class Arvados::V1::ApiClientAuthorizationsControllerTest < ActionController::TestCase
8 test "should get index" do
9 authorize_with :active_trustedclient
11 assert_response :success
14 test "should not get index with expired auth" do
15 authorize_with :expired
16 get :index, params: {format: :json}
20 test "should not get index from untrusted client" do
21 authorize_with :active
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']
33 test "prohibit create system auth with token from non-trusted client" do
35 post :create_system_auth, params: {scopes: '["test"]'}
39 test "prohibit create system auth by non-admin" do
40 authorize_with :active
41 post :create_system_auth, params: {scopes: '["test"]'}
45 def assert_found_tokens(auth, search_params, expected)
47 expected_tokens = expected.map do |name|
48 api_client_authorizations(name).api_token
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}")
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
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"],
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)
72 test "#{auth.to_s} can find auths filtered with scopes=#{scopes.inspect}" do
73 assert_found_tokens(auth, {filters: [['scopes', '=', scopes]]}, expected)
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]]
84 [:admin, :active].each do |token|
85 test "using '#{token}', get token details via 'current'" do
89 assert_equal json_response['scopes'], ['all']
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
107 id: api_client_authorizations(token).uuid,
109 assert_response expect_get_response
112 test "using '#{user}', update '#{token}' by uuid" do
114 put :update, params: {
115 id: api_client_authorizations(token).uuid,
116 api_client_authorization: {},
118 assert_response expect_get_response
121 test "using '#{user}', delete '#{token}' by uuid" do
123 post :destroy, params: {
124 id: api_client_authorizations(token).uuid,
126 assert_response expect_get_response
129 test "using '#{user}', list '#{token}' by uuid" do
131 get :index, params: {
132 filters: [['uuid','=',api_client_authorizations(token).uuid]],
134 assert_response expect_list_response
136 assert_equal assigns(:objects).length, expect_list_items
137 assert_equal json_response['items_available'], expect_list_items
142 test "using '#{user}', list '#{token}' by uuid with offset" do
144 get :index, params: {
145 filters: [['uuid','=',api_client_authorizations(token).uuid]],
146 offset: expect_list_items,
148 assert_response expect_list_response
149 assert_equal json_response['items_available'], expect_list_items
150 assert_equal json_response['items'].length, 0
154 test "using '#{user}', list '#{token}' by token" do
156 get :index, params: {
157 filters: [['api_token','=',api_client_authorizations(token).api_token]],
159 assert_response expect_list_response
161 assert_equal assigns(:objects).length, expect_list_items
162 assert_equal json_response['items_available'], expect_list_items
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']},
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'},
185 test "get current token" do
186 authorize_with :active
188 assert_response :success
189 assert_equal(json_response['api_token'],
190 api_client_authorizations(:active).api_token)
193 test "get current token using SystemRootToken" do
194 Rails.configuration.SystemRootToken = "xyzzy-systemroottoken"
195 authorize_with_token Rails.configuration.SystemRootToken
197 assert_response :success
198 assert_equal(Rails.configuration.SystemRootToken, json_response['api_token'])
199 assert_not_empty(json_response['uuid'])
202 test "get current token, no auth" do
207 # Tests regression #18801
208 test "select param is respected in 'show' response" do
209 authorize_with :active
211 id: api_client_authorizations(:active).uuid,
214 assert_response :success
215 assert_raises ActiveModel::MissingAttributeError do
216 assigns(:object).api_token
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"]