1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 require 'create_superuser_token'
9 class CreateSuperUserTokenTest < ActiveSupport::TestCase
10 include CreateSuperUserToken
12 test "create superuser token twice and expect same resutls" do
13 # Create a token with some string
14 token1 = create_superuser_token 'atesttoken'
16 assert_equal token1, 'atesttoken'
18 # Create token again; this time, we should get the one created earlier
19 token2 = create_superuser_token
21 assert_equal token1, token2
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'
28 assert_equal token1, 'atesttoken'
30 # Create token again with some other string and expect the existing superuser token back
31 token2 = create_superuser_token 'someothertokenstring'
33 assert_equal token1, token2
36 test "create superuser token twice and expect same results" do
37 # Create a token with some string
38 token1 = create_superuser_token 'atesttoken'
40 assert_equal token1, 'atesttoken'
42 # Create token again with that same superuser token and expect it back
43 token2 = create_superuser_token 'atesttoken'
45 assert_equal token1, token2
48 test "create superuser token and invoke again with some other valid token" do
49 # Create a token with some string
50 token1 = create_superuser_token 'atesttoken'
52 assert_equal token1, 'atesttoken'
54 su_token = api_client_authorizations("system_user").api_token
55 token2 = create_superuser_token su_token
56 assert_equal token2, su_token
59 test "create superuser token, expire it, and create again" do
60 # Create a token with some string
61 token1 = create_superuser_token 'atesttoken'
63 assert_equal token1, 'atesttoken'
65 # Expire this token and call create again; expect a new token created
66 apiClientAuth = ApiClientAuthorization.where(api_token: token1).first
67 Thread.current[:user] = users(:admin)
68 apiClientAuth.update_attributes expires_at: '2000-10-10'
70 token2 = create_superuser_token
72 assert_not_equal token1, token2
75 test "invoke create superuser token with an invalid non-superuser token and expect error" do
76 active_user_token = api_client_authorizations("active").api_token
77 e = assert_raises RuntimeError do
78 create_superuser_token active_user_token
81 assert_equal "Token exists but is not a superuser token.", e.message
84 test "specified token has limited scope" do
85 active_user_token = api_client_authorizations("data_manager").api_token
86 e = assert_raises RuntimeError do
87 create_superuser_token active_user_token
90 assert_match /^Token exists but has limited scope/, e.message
93 test "existing token has limited scope" do
94 active_user_token = api_client_authorizations("admin_vm").api_token
95 ApiClientAuthorization.
96 where(user_id: system_user.id).
97 update_all(scopes: ["GET /"])
98 fixture_tokens = ApiClientAuthorization.all.collect(&:api_token)
99 new_token = create_superuser_token
100 refute_includes(fixture_tokens, new_token)