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 results" do
13 # Create a token with some string
14 token1 = create_superuser_token 'atesttoken'
16 assert_match(/atesttoken$/, token1)
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_match(/\/atesttoken$/, token1)
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 and invoke again with some other valid token" do
37 # Create a token with some string
38 token1 = create_superuser_token 'atesttoken'
40 assert_match(/\/atesttoken$/, token1)
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
47 test "create superuser token, expire it, and create again" do
48 # Create a token with some string
49 token1 = create_superuser_token 'atesttoken'
51 assert_match(/\/atesttoken$/, token1)
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'
59 token2 = create_superuser_token
61 assert_not_equal token1, token2
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
70 assert_equal "Token exists but is not a superuser token.", e.message
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
79 assert_match /^Token exists but has limited scope/, e.message
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)