Merge branch 'master' into 9998-unsigned_manifest
[arvados.git] / services / api / test / unit / create_superuser_token_test.rb
1 require 'test_helper'
2 require 'create_superuser_token'
3
4 class CreateSuperUserTokenTest < ActiveSupport::TestCase
5   include CreateSuperUserToken
6
7   test "create superuser token twice and expect same resutls" do
8     # Create a token with some string
9     token1 = create_superuser_token 'atesttoken'
10     assert_not_nil token1
11     assert_equal token1, 'atesttoken'
12
13     # Create token again; this time, we should get the one created earlier
14     token2 = create_superuser_token
15     assert_not_nil token2
16     assert_equal token1, token2
17   end
18
19   test "create superuser token with two different inputs and expect the first both times" do
20     # Create a token with some string
21     token1 = create_superuser_token 'atesttoken'
22     assert_not_nil token1
23     assert_equal token1, 'atesttoken'
24
25     # Create token again with some other string and expect the existing superuser token back
26     token2 = create_superuser_token 'someothertokenstring'
27     assert_not_nil token2
28     assert_equal token1, token2
29   end
30
31   test "create superuser token twice and expect same results" do
32     # Create a token with some string
33     token1 = create_superuser_token 'atesttoken'
34     assert_not_nil token1
35     assert_equal token1, 'atesttoken'
36
37     # Create token again with that same superuser token and expect it back
38     token2 = create_superuser_token 'atesttoken'
39     assert_not_nil token2
40     assert_equal token1, token2
41   end
42
43   test "create superuser token and invoke again with some other valid token" do
44     # Create a token with some string
45     token1 = create_superuser_token 'atesttoken'
46     assert_not_nil token1
47     assert_equal token1, 'atesttoken'
48
49     su_token = api_client_authorizations("system_user").api_token
50     token2 = create_superuser_token su_token
51     assert_equal token2, su_token
52   end
53
54   test "create superuser token, expire it, and create again" do
55     # Create a token with some string
56     token1 = create_superuser_token 'atesttoken'
57     assert_not_nil token1
58     assert_equal token1, 'atesttoken'
59
60     # Expire this token and call create again; expect a new token created
61     apiClientAuth = ApiClientAuthorization.where(api_token: token1).first
62     Thread.current[:user] = users(:admin)
63     apiClientAuth.update_attributes expires_at: '2000-10-10'
64
65     token2 = create_superuser_token
66     assert_not_nil token2
67     assert_not_equal token1, token2
68   end
69
70   test "invoke create superuser token with an invalid non-superuser token and expect error" do
71     active_user_token = api_client_authorizations("active").api_token
72     e = assert_raises RuntimeError do
73       create_superuser_token active_user_token
74     end
75     assert_not_nil e
76     assert_equal "Token already exists but is not a superuser token.", e.message
77   end
78 end