8784: Fix test for latest firefox.
[arvados.git] / services / api / test / unit / create_superuser_token_test.rb
1 require 'safe_json'
2 require 'test_helper'
3 require 'create_superuser_token'
4
5 class CreateSuperUserTokenTest < ActiveSupport::TestCase
6   include CreateSuperUserToken
7
8   test "create superuser token twice and expect same resutls" do
9     # Create a token with some string
10     token1 = create_superuser_token 'atesttoken'
11     assert_not_nil token1
12     assert_equal token1, 'atesttoken'
13
14     # Create token again; this time, we should get the one created earlier
15     token2 = create_superuser_token
16     assert_not_nil token2
17     assert_equal token1, token2
18   end
19
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'
23     assert_not_nil token1
24     assert_equal token1, 'atesttoken'
25
26     # Create token again with some other string and expect the existing superuser token back
27     token2 = create_superuser_token 'someothertokenstring'
28     assert_not_nil token2
29     assert_equal token1, token2
30   end
31
32   test "create superuser token twice and expect same results" do
33     # Create a token with some string
34     token1 = create_superuser_token 'atesttoken'
35     assert_not_nil token1
36     assert_equal token1, 'atesttoken'
37
38     # Create token again with that same superuser token and expect it back
39     token2 = create_superuser_token 'atesttoken'
40     assert_not_nil token2
41     assert_equal token1, token2
42   end
43
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'
47     assert_not_nil token1
48     assert_equal token1, 'atesttoken'
49
50     su_token = api_client_authorizations("system_user").api_token
51     token2 = create_superuser_token su_token
52     assert_equal token2, su_token
53   end
54
55   test "create superuser token, expire it, and create again" do
56     # Create a token with some string
57     token1 = create_superuser_token 'atesttoken'
58     assert_not_nil token1
59     assert_equal token1, 'atesttoken'
60
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'
65
66     token2 = create_superuser_token
67     assert_not_nil token2
68     assert_not_equal token1, token2
69   end
70
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
75     end
76     assert_not_nil e
77     assert_equal "Token exists but is not a superuser token.", e.message
78   end
79
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
84     end
85     assert_not_nil e
86     assert_match /^Token exists but has limited scope/, e.message
87   end
88
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: ["GET /"])
94     fixture_tokens = ApiClientAuthorization.all.collect(&:api_token)
95     new_token = create_superuser_token
96     refute_includes(fixture_tokens, new_token)
97   end
98 end