Merge branch '21898-info-button-behavior'
[arvados.git] / services / api / test / integration / api_client_authorizations_api_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 ApiClientAuthorizationsApiTest < ActionDispatch::IntegrationTest
8   include DbCurrentTime
9   extend DbCurrentTime
10   fixtures :all
11
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
17   end
18
19   [
20     [true, :active, 403],
21     [true, :admin, 200],
22     [true, :system_user, 200],
23     [false, :active, 403],
24     [false, :admin, 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",
30            params: {
31              :format => :json,
32              :api_client_authorization => {
33                :owner_uuid => users(:spectator).uuid
34              }
35            },
36            headers: {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(tk).api_token}"}
37
38       assert_response expect_response
39       return if expect_response >= 300
40
41       get "/arvados/v1/users/current",
42           params: {:format => :json},
43           headers: {'HTTP_AUTHORIZATION' => "Bearer #{json_response['api_token']}"}
44       @json_response = nil
45       assert_equal json_response['uuid'], users(:spectator).uuid
46     end
47   end
48
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
56   end
57
58   test "refuse to create token for different user if not admin" do
59     post "/arvados/v1/api_client_authorizations",
60       params: {
61         :format => :json,
62         :api_client_authorization => {
63           :owner_uuid => users(:spectator).uuid
64         }
65       },
66       headers: {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(:active_trustedclient).api_token}"}
67     assert_response 403
68   end
69
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
75
76         # Test token creation
77         start_t = db_current_time
78         post "/arvados/v1/api_client_authorizations",
79              params: {
80                :format => :json,
81                :api_client_authorization => {
82                  :owner_uuid => users(admin ? :admin : :active).uuid,
83                  :expires_at => desired_expiration,
84                }
85              },
86              headers: {'HTTP_AUTHORIZATION' => "Bearer #{token}"}
87         assert_response 200
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
91         else
92           assert_in_delta (start_t + Rails.configuration.API.MaxTokenLifetime).to_f, expiration_t.to_f, 2
93         end
94
95         # Test token update
96         previous_expiration = expiration_t
97         token_uuid = json_response["uuid"]
98
99         start_t = db_current_time
100         patch "/arvados/v1/api_client_authorizations/#{token_uuid}",
101             params: {
102               :api_client_authorization => {
103                 :expires_at => desired_expiration
104               }
105             },
106             headers: {'HTTP_AUTHORIZATION' => "Bearer #{token}"}
107         assert_response 200
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
111         else
112           assert_in_delta (start_t + Rails.configuration.API.MaxTokenLifetime).to_f, expiration_t.to_f, 2
113         end
114       end
115     end
116   end
117
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'])
128   end
129 end