18948: Update test.
[arvados.git] / services / api / test / unit / api_client_authorization_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 ApiClientAuthorizationTest < ActiveSupport::TestCase
8   include CurrentApiClient
9
10   [:admin_trustedclient, :active_trustedclient].each do |token|
11     test "ApiClientAuthorization can be created then deleted by #{token}" do
12       set_user_from_auth token
13       x = ApiClientAuthorization.create!(user_id: current_user.id,
14                                          api_client_id: 0,
15                                          scopes: [])
16       newtoken = x.api_token
17       assert x.destroy, "Failed to destroy new ApiClientAuth"
18       assert_empty ApiClientAuthorization.where(api_token: newtoken), "Destroyed ApiClientAuth is still in database"
19     end
20   end
21
22   test "accepts SystemRootToken" do
23     assert_nil ApiClientAuthorization.validate(token: "xxxSystemRootTokenxxx")
24
25     # will create a new ApiClientAuthorization record
26     Rails.configuration.SystemRootToken = "xxxSystemRootTokenxxx"
27
28     auth = ApiClientAuthorization.validate(token: "xxxSystemRootTokenxxx")
29     assert_equal "xxxSystemRootTokenxxx", auth.api_token
30     assert_equal User.find_by_uuid(system_user_uuid).id, auth.user_id
31     assert auth.api_client.is_trusted
32
33     # now change the token and try to use the old one first
34     Rails.configuration.SystemRootToken = "newxxxSystemRootTokenxxx"
35
36     # old token will fail
37     assert_nil ApiClientAuthorization.validate(token: "xxxSystemRootTokenxxx")
38     # new token will work
39     auth = ApiClientAuthorization.validate(token: "newxxxSystemRootTokenxxx")
40     assert_equal "newxxxSystemRootTokenxxx", auth.api_token
41     assert_equal User.find_by_uuid(system_user_uuid).id, auth.user_id
42
43     # now change the token again and use the new one first
44     Rails.configuration.SystemRootToken = "new2xxxSystemRootTokenxxx"
45
46     # new token will work
47     auth = ApiClientAuthorization.validate(token: "new2xxxSystemRootTokenxxx")
48     assert_equal "new2xxxSystemRootTokenxxx", auth.api_token
49     assert_equal User.find_by_uuid(system_user_uuid).id, auth.user_id
50     # old token will fail
51     assert_nil ApiClientAuthorization.validate(token: "newxxxSystemRootTokenxxx")
52   end
53
54
55 end