Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / services / api / test / unit / create_superuser_token_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'safe_json'
6 require 'test_helper'
7 require 'create_superuser_token'
8
9 class CreateSuperUserTokenTest < ActiveSupport::TestCase
10   include CreateSuperUserToken
11
12   test "create superuser token twice and expect same results" do
13     # Create a token with some string
14     token1 = create_superuser_token 'atesttoken'
15     assert_not_nil token1
16     assert_match(/atesttoken$/, token1)
17
18     # Create token again; this time, we should get the one created earlier
19     token2 = create_superuser_token
20     assert_not_nil token2
21     assert_equal token1, token2
22   end
23
24   test "create superuser token with two different inputs and expect the first both times" do
25     # Create a token with some string
26     token1 = create_superuser_token 'atesttoken'
27     assert_not_nil token1
28     assert_match(/\/atesttoken$/, token1)
29
30     # Create token again with some other string and expect the existing superuser token back
31     token2 = create_superuser_token 'someothertokenstring'
32     assert_not_nil token2
33     assert_equal token1, token2
34   end
35
36   test "create superuser token and invoke again with some other valid token" do
37     # Create a token with some string
38     token1 = create_superuser_token 'atesttoken'
39     assert_not_nil token1
40     assert_match(/\/atesttoken$/, token1)
41
42     su_token = api_client_authorizations("system_user").api_token
43     token2 = create_superuser_token su_token
44     assert_equal token2.split('/')[2], su_token
45   end
46
47   test "create superuser token, expire it, and create again" do
48     # Create a token with some string
49     token1 = create_superuser_token 'atesttoken'
50     assert_not_nil token1
51     assert_match(/\/atesttoken$/, token1)
52
53     # Expire this token and call create again; expect a new token created
54     apiClientAuth = ApiClientAuthorization.where(api_token: 'atesttoken').first
55     refute_nil apiClientAuth
56     Thread.current[:user] = users(:admin)
57     apiClientAuth.update_attributes expires_at: '2000-10-10'
58
59     token2 = create_superuser_token
60     assert_not_nil token2
61     assert_not_equal token1, token2
62   end
63
64   test "invoke create superuser token with an invalid non-superuser token and expect error" do
65     active_user_token = api_client_authorizations("active").api_token
66     e = assert_raises RuntimeError do
67       create_superuser_token active_user_token
68     end
69     assert_not_nil e
70     assert_equal "Token exists but is not a superuser token.", e.message
71   end
72
73   test "specified token has limited scope" do
74     active_user_token = api_client_authorizations("data_manager").api_token
75     e = assert_raises RuntimeError do
76       create_superuser_token active_user_token
77     end
78     assert_not_nil e
79     assert_match /^Token exists but has limited scope/, e.message
80   end
81
82   test "existing token has limited scope" do
83     active_user_token = api_client_authorizations("admin_vm").api_token
84     ApiClientAuthorization.
85       where(user_id: system_user.id).
86       update_all(scopes: ["GET /"])
87     fixture_tokens = ApiClientAuthorization.all.collect(&:api_token)
88     new_token = create_superuser_token
89     refute_includes(fixture_tokens, new_token)
90   end
91 end