3 require 'create_superuser_token'
5 class CreateSuperUserTokenTest < ActiveSupport::TestCase
6 include CreateSuperUserToken
8 test "create superuser token twice and expect same resutls" do
9 # Create a token with some string
10 token1 = create_superuser_token 'atesttoken'
12 assert_equal token1, 'atesttoken'
14 # Create token again; this time, we should get the one created earlier
15 token2 = create_superuser_token
17 assert_equal token1, token2
20 test "create superuser token with two different inputs and expect the first both times" do
21 # Create a token with some string
22 token1 = create_superuser_token 'atesttoken'
24 assert_equal token1, 'atesttoken'
26 # Create token again with some other string and expect the existing superuser token back
27 token2 = create_superuser_token 'someothertokenstring'
29 assert_equal token1, token2
32 test "create superuser token twice and expect same results" do
33 # Create a token with some string
34 token1 = create_superuser_token 'atesttoken'
36 assert_equal token1, 'atesttoken'
38 # Create token again with that same superuser token and expect it back
39 token2 = create_superuser_token 'atesttoken'
41 assert_equal token1, token2
44 test "create superuser token and invoke again with some other valid token" do
45 # Create a token with some string
46 token1 = create_superuser_token 'atesttoken'
48 assert_equal token1, 'atesttoken'
50 su_token = api_client_authorizations("system_user").api_token
51 token2 = create_superuser_token su_token
52 assert_equal token2, su_token
55 test "create superuser token, expire it, and create again" do
56 # Create a token with some string
57 token1 = create_superuser_token 'atesttoken'
59 assert_equal token1, 'atesttoken'
61 # Expire this token and call create again; expect a new token created
62 apiClientAuth = ApiClientAuthorization.where(api_token: token1).first
63 Thread.current[:user] = users(:admin)
64 apiClientAuth.update_attributes expires_at: '2000-10-10'
66 token2 = create_superuser_token
68 assert_not_equal token1, token2
71 test "invoke create superuser token with an invalid non-superuser token and expect error" do
72 active_user_token = api_client_authorizations("active").api_token
73 e = assert_raises RuntimeError do
74 create_superuser_token active_user_token
77 assert_equal "Token exists but is not a superuser token.", e.message
80 test "specified token has limited scope" do
81 active_user_token = api_client_authorizations("data_manager").api_token
82 e = assert_raises RuntimeError do
83 create_superuser_token active_user_token
86 assert_match /^Token exists but has limited scope/, e.message
89 test "existing token has limited scope" do
90 active_user_token = api_client_authorizations("admin_vm").api_token
91 ApiClientAuthorization.
92 where(user_id: system_user.id).
93 update_all(scopes: SafeJSON.dump(["GET /"]))
94 fixture_tokens = ApiClientAuthorization.all.collect(&:api_token)
95 new_token = create_superuser_token
96 refute_includes(fixture_tokens, new_token)