8784: Fix test for latest firefox.
[arvados.git] / services / api / test / functional / arvados / v1 / api_client_authorizations_controller_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::ApiClientAuthorizationsControllerTest < ActionController::TestCase
4   test "should get index" do
5     authorize_with :active_trustedclient
6     get :index
7     assert_response :success
8   end
9
10   test "should not get index with expired auth" do
11     authorize_with :expired
12     get :index, format: :json
13     assert_response 401
14   end
15
16   test "should not get index from untrusted client" do
17     authorize_with :active
18     get :index
19     assert_response 403
20   end
21
22   test "create system auth" do
23     authorize_with :admin_trustedclient
24     post :create_system_auth, scopes: '["test"]'
25     assert_response :success
26     assert_not_nil JSON.parse(@response.body)['uuid']
27   end
28
29   test "prohibit create system auth with token from non-trusted client" do
30     authorize_with :admin
31     post :create_system_auth, scopes: '["test"]'
32     assert_response 403
33   end
34
35   test "prohibit create system auth by non-admin" do
36     authorize_with :active
37     post :create_system_auth, scopes: '["test"]'
38     assert_response 403
39   end
40
41   def assert_found_tokens(auth, search_params, expected)
42     authorize_with auth
43     expected_tokens = expected.map do |name|
44       api_client_authorizations(name).api_token
45     end
46     get :index, search_params
47     assert_response :success
48     got_tokens = JSON.parse(@response.body)['items']
49       .map { |a| a['api_token'] }
50     assert_equal(expected_tokens.sort, got_tokens.sort,
51                  "wrong results for #{search_params.inspect}")
52   end
53
54   # Three-tuples with auth to use, scopes to find, and expected tokens.
55   # Make two tests for each tuple, one searching with where and the other
56   # with filter.
57   [[:admin_trustedclient, [], [:admin_noscope]],
58    [:active_trustedclient, ["GET /arvados/v1/users"], [:active_userlist]],
59    [:active_trustedclient,
60     ["POST /arvados/v1/api_client_authorizations",
61      "GET /arvados/v1/api_client_authorizations"],
62     [:active_apitokens]],
63   ].each do |auth, scopes, expected|
64     test "#{auth.to_s} can find auths where scopes=#{scopes.inspect}" do
65       assert_found_tokens(auth, {where: {scopes: scopes}}, expected)
66     end
67
68     test "#{auth.to_s} can find auths filtered with scopes=#{scopes.inspect}" do
69       assert_found_tokens(auth, {filters: [['scopes', '=', scopes]]}, expected)
70     end
71
72     test "#{auth.to_s} offset works with filter scopes=#{scopes.inspect}" do
73       assert_found_tokens(auth, {
74                             offset: expected.length,
75                             filters: [['scopes', '=', scopes]]
76                           }, [])
77     end
78   end
79
80   [# anyone can look up the token they're currently using
81    [:admin, :admin, 200, 200, 1],
82    [:active, :active, 200, 200, 1],
83    # cannot look up other tokens (even for same user) if not trustedclient
84    [:admin, :active, 403, 403],
85    [:admin, :admin_vm, 403, 403],
86    [:active, :admin, 403, 403],
87    # cannot look up other tokens for other users, regardless of trustedclient
88    [:admin_trustedclient, :active, 404, 200, 0],
89    [:active_trustedclient, :admin, 404, 200, 0],
90   ].each do |user, token, expect_get_response, expect_list_response, expect_list_items|
91     test "using '#{user}', get '#{token}' by uuid" do
92       authorize_with user
93       get :show, {
94         id: api_client_authorizations(token).uuid,
95       }
96       assert_response expect_get_response
97     end
98
99     test "using '#{user}', update '#{token}' by uuid" do
100       authorize_with user
101       put :update, {
102         id: api_client_authorizations(token).uuid,
103         api_client_authorization: {},
104       }
105       assert_response expect_get_response
106     end
107
108     test "using '#{user}', delete '#{token}' by uuid" do
109       authorize_with user
110       post :destroy, {
111         id: api_client_authorizations(token).uuid,
112       }
113       assert_response expect_get_response
114     end
115
116     test "using '#{user}', list '#{token}' by uuid" do
117       authorize_with user
118       get :index, {
119         filters: [['uuid','=',api_client_authorizations(token).uuid]],
120       }
121       assert_response expect_list_response
122       if expect_list_items
123         assert_equal assigns(:objects).length, expect_list_items
124         assert_equal json_response['items_available'], expect_list_items
125       end
126     end
127
128     if expect_list_items
129       test "using '#{user}', list '#{token}' by uuid with offset" do
130         authorize_with user
131         get :index, {
132           filters: [['uuid','=',api_client_authorizations(token).uuid]],
133           offset: expect_list_items,
134         }
135         assert_response expect_list_response
136         assert_equal json_response['items_available'], expect_list_items
137         assert_equal json_response['items'].length, 0
138       end
139     end
140
141     test "using '#{user}', list '#{token}' by token" do
142       authorize_with user
143       get :index, {
144         filters: [['api_token','=',api_client_authorizations(token).api_token]],
145       }
146       assert_response expect_list_response
147       if expect_list_items
148         assert_equal assigns(:objects).length, expect_list_items
149         assert_equal json_response['items_available'], expect_list_items
150       end
151     end
152   end
153
154   test "scoped token cannot change its own scopes" do
155     authorize_with :admin_vm
156     put :update, {
157       id: api_client_authorizations(:admin_vm).uuid,
158       api_client_authorization: {scopes: ['all']},
159     }
160     assert_response 403
161   end
162
163   test "token cannot change its own uuid" do
164     authorize_with :admin
165     put :update, {
166       id: api_client_authorizations(:admin).uuid,
167       api_client_authorization: {uuid: 'zzzzz-gj3su-zzzzzzzzzzzzzzz'},
168     }
169     assert_response 403
170   end
171
172   test "get current token" do
173     authorize_with :active
174     get :current
175     assert_response :success
176     assert_equal(json_response['api_token'],
177                  api_client_authorizations(:active).api_token)
178   end
179
180   test "get current token, no auth" do
181     get :current
182     assert_response 401
183   end
184 end