21700: Install Bundler system-wide in Rails postinst
[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' => "OAuth2 #{api_client_authorizations(:admin_trustedclient).api_token}"}
16     assert_response :success
17   end
18
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
24       else
25         token = api_client_authorizations(tk).api_token
26       end
27
28       post "/arvados/v1/api_client_authorizations",
29            params: {
30              :format => :json,
31              :api_client_authorization => {
32                :owner_uuid => users(:spectator).uuid
33              }
34            },
35            headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{token}"}
36       assert_response :success
37
38       get "/arvados/v1/users/current",
39           params: {:format => :json},
40           headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{json_response['api_token']}"}
41       @json_response = nil
42       assert_equal json_response['uuid'], users(:spectator).uuid
43     end
44   end
45
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
53   end
54
55   test "refuse to create token for different user if not trusted client" do
56     post "/arvados/v1/api_client_authorizations",
57       params: {
58         :format => :json,
59         :api_client_authorization => {
60           :owner_uuid => users(:spectator).uuid
61         }
62       },
63       headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:admin).api_token}"}
64     assert_response 403
65   end
66
67   test "refuse to create token for different user if not admin" do
68     post "/arvados/v1/api_client_authorizations",
69       params: {
70         :format => :json,
71         :api_client_authorization => {
72           :owner_uuid => users(:spectator).uuid
73         }
74       },
75       headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:active_trustedclient).api_token}"}
76     assert_response 403
77   end
78
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
84
85         # Test token creation
86         start_t = db_current_time
87         post "/arvados/v1/api_client_authorizations",
88              params: {
89                :format => :json,
90                :api_client_authorization => {
91                  :owner_uuid => users(admin ? :admin : :active).uuid,
92                  :expires_at => desired_expiration,
93                }
94              },
95              headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{token}"}
96         assert_response 200
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
100         else
101           assert_in_delta (start_t + Rails.configuration.API.MaxTokenLifetime).to_f, expiration_t.to_f, 2
102         end
103
104         # Test token update
105         previous_expiration = expiration_t
106         token_uuid = json_response["uuid"]
107
108         start_t = db_current_time
109         patch "/arvados/v1/api_client_authorizations/#{token_uuid}",
110             params: {
111               :api_client_authorization => {
112                 :expires_at => desired_expiration
113               }
114             },
115             headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{token}"}
116         assert_response 200
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
120         else
121           assert_in_delta (start_t + Rails.configuration.API.MaxTokenLifetime).to_f, expiration_t.to_f, 2
122         end
123       end
124     end
125   end
126
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'])
137   end
138 end