Merge branch '8784-dir-listings'
[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 resutls" do
13     # Create a token with some string
14     token1 = create_superuser_token 'atesttoken'
15     assert_not_nil token1
16     assert_equal token1, 'atesttoken'
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_equal token1, 'atesttoken'
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 twice and expect same results" do
37     # Create a token with some string
38     token1 = create_superuser_token 'atesttoken'
39     assert_not_nil token1
40     assert_equal token1, 'atesttoken'
41
42     # Create token again with that same superuser token and expect it back
43     token2 = create_superuser_token 'atesttoken'
44     assert_not_nil token2
45     assert_equal token1, token2
46   end
47
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'
51     assert_not_nil token1
52     assert_equal token1, 'atesttoken'
53
54     su_token = api_client_authorizations("system_user").api_token
55     token2 = create_superuser_token su_token
56     assert_equal token2, su_token
57   end
58
59   test "create superuser token, expire it, and create again" do
60     # Create a token with some string
61     token1 = create_superuser_token 'atesttoken'
62     assert_not_nil token1
63     assert_equal token1, 'atesttoken'
64
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'
69
70     token2 = create_superuser_token
71     assert_not_nil token2
72     assert_not_equal token1, token2
73   end
74
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
79     end
80     assert_not_nil e
81     assert_equal "Token exists but is not a superuser token.", e.message
82   end
83
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
88     end
89     assert_not_nil e
90     assert_match /^Token exists but has limited scope/, e.message
91   end
92
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)
101   end
102 end