1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class ApiClientAuthorizationTest < ActiveSupport::TestCase
8 include CurrentApiClient
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,
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"
22 test "accepts SystemRootToken" do
23 assert_nil ApiClientAuthorization.validate(token: "xxxSystemRootTokenxxx")
25 # will create a new ApiClientAuthorization record
26 Rails.configuration.SystemRootToken = "xxxSystemRootTokenxxx"
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
33 # now change the token and try to use the old one first
34 Rails.configuration.SystemRootToken = "newxxxSystemRootTokenxxx"
37 assert_nil ApiClientAuthorization.validate(token: "xxxSystemRootTokenxxx")
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
43 # now change the token again and use the new one first
44 Rails.configuration.SystemRootToken = "new2xxxSystemRootTokenxxx"
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
51 assert_nil ApiClientAuthorization.validate(token: "newxxxSystemRootTokenxxx")