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' => "OAuth2 #{api_client_authorizations(:admin_trustedclient).api_token}"}
16 assert_response :success
19 [:admin_trustedclient, :SystemRootToken].each do |tk|
20 test "create token for different user using #{tk}" do
21 if tk == :SystemRootToken
22 token = "xyzzy-SystemRootToken"
23 Rails.configuration.SystemRootToken = token
25 token = api_client_authorizations(tk).api_token
28 post "/arvados/v1/api_client_authorizations",
31 :api_client_authorization => {
32 :owner_uuid => users(:spectator).uuid
35 headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{token}"}
36 assert_response :success
38 get "/arvados/v1/users/current",
39 params: {:format => :json},
40 headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{json_response['api_token']}"}
42 assert_equal json_response['uuid'], users(:spectator).uuid
46 test "System root token is system user" do
47 token = "xyzzy-SystemRootToken"
48 Rails.configuration.SystemRootToken = token
49 get "/arvados/v1/users/current",
50 params: {:format => :json},
51 headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{token}"}
52 assert_equal json_response['uuid'], system_user_uuid
55 test "refuse to create token for different user if not trusted client" do
56 post "/arvados/v1/api_client_authorizations",
59 :api_client_authorization => {
60 :owner_uuid => users(:spectator).uuid
63 headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:admin).api_token}"}
67 test "refuse to create token for different user if not admin" do
68 post "/arvados/v1/api_client_authorizations",
71 :api_client_authorization => {
72 :owner_uuid => users(:spectator).uuid
75 headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:active_trustedclient).api_token}"}
79 [nil, db_current_time + 2.hours].each do |desired_expiration|
80 [false, true].each do |admin|
81 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
82 Rails.configuration.API.MaxTokenLifetime = 1.hour
83 token = api_client_authorizations(admin ? :admin_trustedclient : :active_trustedclient).api_token
86 start_t = db_current_time
87 post "/arvados/v1/api_client_authorizations",
90 :api_client_authorization => {
91 :owner_uuid => users(admin ? :admin : :active).uuid,
92 :expires_at => desired_expiration,
95 headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{token}"}
97 expiration_t = json_response['expires_at'].to_time
98 if admin && desired_expiration
99 assert_in_delta desired_expiration.to_f, expiration_t.to_f, 1
101 assert_in_delta (start_t + Rails.configuration.API.MaxTokenLifetime).to_f, expiration_t.to_f, 2
105 previous_expiration = expiration_t
106 token_uuid = json_response["uuid"]
108 start_t = db_current_time
109 patch "/arvados/v1/api_client_authorizations/#{token_uuid}",
111 :api_client_authorization => {
112 :expires_at => desired_expiration
115 headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{token}"}
117 expiration_t = json_response['expires_at'].to_time
118 if admin && desired_expiration
119 assert_in_delta desired_expiration.to_f, expiration_t.to_f, 1
121 assert_in_delta (start_t + Rails.configuration.API.MaxTokenLifetime).to_f, expiration_t.to_f, 2
127 test "get current token using salted token" do
128 salted = salt_token(fixture: :active, remote: 'abcde')
129 get('/arvados/v1/api_client_authorizations/current',
130 params: {remote: 'abcde'},
131 headers: {'HTTP_AUTHORIZATION' => "Bearer #{salted}"})
132 assert_response :success
133 assert_equal(json_response['uuid'], api_client_authorizations(:active).uuid)
134 assert_equal(json_response['scopes'], ['all'])
135 assert_not_nil(json_response['expires_at'])
136 assert_nil(json_response['api_token'])