1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class ApiClientAuthorizationsApiTest < ActionDispatch::IntegrationTest
12 test "create system auth" do
13 post "/arvados/v1/api_client_authorizations/create_system_auth",
14 params: {:format => :json, :scopes => ['test'].to_json},
15 headers: {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(:admin_trustedclient).api_token}"}
16 assert_response :success
22 [true, :system_user, 200],
23 [false, :active, 403],
25 [false, :system_user, 200],
26 ].each do |issue_trusted_tokens, tk, expect_response|
27 test "create token for different user using #{tk} with IssueTrustedTokens=#{issue_trusted_tokens}" do
28 Rails.configuration.Login.IssueTrustedTokens = issue_trusted_tokens
29 post "/arvados/v1/api_client_authorizations",
32 :api_client_authorization => {
33 :owner_uuid => users(:spectator).uuid
36 headers: {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(tk).api_token}"}
38 assert_response expect_response
39 return if expect_response >= 300
41 get "/arvados/v1/users/current",
42 params: {:format => :json},
43 headers: {'HTTP_AUTHORIZATION' => "Bearer #{json_response['api_token']}"}
45 assert_equal json_response['uuid'], users(:spectator).uuid
49 test "System root token is system user" do
50 token = "xyzzy-SystemRootToken"
51 Rails.configuration.SystemRootToken = token
52 get "/arvados/v1/users/current",
53 params: {:format => :json},
54 headers: {'HTTP_AUTHORIZATION' => "Bearer #{token}"}
55 assert_equal json_response['uuid'], system_user_uuid
58 test "refuse to create token for different user if not admin" do
59 post "/arvados/v1/api_client_authorizations",
62 :api_client_authorization => {
63 :owner_uuid => users(:spectator).uuid
66 headers: {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(:active_trustedclient).api_token}"}
70 [nil, db_current_time + 2.hours].each do |desired_expiration|
71 [false, true].each do |admin|
72 test "expires_at gets clamped on #{admin ? 'admins' : 'non-admins'} when API.MaxTokenLifetime is set and desired expires_at #{desired_expiration.nil? ? 'is not set' : 'exceeds the limit'}" do
73 Rails.configuration.API.MaxTokenLifetime = 1.hour
74 token = api_client_authorizations(admin ? :admin_trustedclient : :active_trustedclient).api_token
77 start_t = db_current_time
78 post "/arvados/v1/api_client_authorizations",
81 :api_client_authorization => {
82 :owner_uuid => users(admin ? :admin : :active).uuid,
83 :expires_at => desired_expiration,
86 headers: {'HTTP_AUTHORIZATION' => "Bearer #{token}"}
88 expiration_t = json_response['expires_at'].to_time
89 if admin && desired_expiration
90 assert_in_delta desired_expiration.to_f, expiration_t.to_f, 1
92 assert_in_delta (start_t + Rails.configuration.API.MaxTokenLifetime).to_f, expiration_t.to_f, 2
96 previous_expiration = expiration_t
97 token_uuid = json_response["uuid"]
99 start_t = db_current_time
100 patch "/arvados/v1/api_client_authorizations/#{token_uuid}",
102 :api_client_authorization => {
103 :expires_at => desired_expiration
106 headers: {'HTTP_AUTHORIZATION' => "Bearer #{token}"}
108 expiration_t = json_response['expires_at'].to_time
109 if admin && desired_expiration
110 assert_in_delta desired_expiration.to_f, expiration_t.to_f, 1
112 assert_in_delta (start_t + Rails.configuration.API.MaxTokenLifetime).to_f, expiration_t.to_f, 2
118 test "get current token using salted token" do
119 salted = salt_token(fixture: :active, remote: 'abcde')
120 get('/arvados/v1/api_client_authorizations/current',
121 params: {remote: 'abcde'},
122 headers: {'HTTP_AUTHORIZATION' => "Bearer #{salted}"})
123 assert_response :success
124 assert_equal(json_response['uuid'], api_client_authorizations(:active).uuid)
125 assert_equal(json_response['scopes'], ['all'])
126 assert_not_nil(json_response['expires_at'])
127 assert_nil(json_response['api_token'])