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, 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, 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, scopes: '["test"]'
39 test "prohibit create system auth by non-admin" do
40 authorize_with :active
41 post :create_system_auth, 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, 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 [# 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
98 id: api_client_authorizations(token).uuid,
100 assert_response expect_get_response
103 test "using '#{user}', update '#{token}' by uuid" do
106 id: api_client_authorizations(token).uuid,
107 api_client_authorization: {},
109 assert_response expect_get_response
112 test "using '#{user}', delete '#{token}' by uuid" do
115 id: api_client_authorizations(token).uuid,
117 assert_response expect_get_response
120 test "using '#{user}', list '#{token}' by uuid" do
123 filters: [['uuid','=',api_client_authorizations(token).uuid]],
125 assert_response expect_list_response
127 assert_equal assigns(:objects).length, expect_list_items
128 assert_equal json_response['items_available'], expect_list_items
133 test "using '#{user}', list '#{token}' by uuid with offset" do
136 filters: [['uuid','=',api_client_authorizations(token).uuid]],
137 offset: expect_list_items,
139 assert_response expect_list_response
140 assert_equal json_response['items_available'], expect_list_items
141 assert_equal json_response['items'].length, 0
145 test "using '#{user}', list '#{token}' by token" do
148 filters: [['api_token','=',api_client_authorizations(token).api_token]],
150 assert_response expect_list_response
152 assert_equal assigns(:objects).length, expect_list_items
153 assert_equal json_response['items_available'], expect_list_items
158 test "scoped token cannot change its own scopes" do
159 authorize_with :admin_vm
161 id: api_client_authorizations(:admin_vm).uuid,
162 api_client_authorization: {scopes: ['all']},
167 test "token cannot change its own uuid" do
168 authorize_with :admin
170 id: api_client_authorizations(:admin).uuid,
171 api_client_authorization: {uuid: 'zzzzz-gj3su-zzzzzzzzzzzzzzz'},
176 test "get current token" do
177 authorize_with :active
179 assert_response :success
180 assert_equal(json_response['api_token'],
181 api_client_authorizations(:active).api_token)
184 test "get current token, no auth" do