Merge branch '8079-lookup-token-uuid' closes #8079
[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_tokens)
42     authorize_with auth
43     expected_tokens.map! { |name| api_client_authorizations(name).api_token }
44     get :index, search_params
45     assert_response :success
46     got_tokens = JSON.parse(@response.body)['items']
47       .map { |auth| auth['api_token'] }
48     assert_equal(expected_tokens.sort, got_tokens.sort,
49                  "wrong results for #{search_params.inspect}")
50   end
51
52   # Three-tuples with auth to use, scopes to find, and expected tokens.
53   # Make two tests for each tuple, one searching with where and the other
54   # with filter.
55   [[:admin_trustedclient, [], :admin_noscope],
56    [:active_trustedclient, ["GET /arvados/v1/users"], :active_userlist],
57    [:active_trustedclient,
58     ["POST /arvados/v1/api_client_authorizations",
59      "GET /arvados/v1/api_client_authorizations"],
60     :active_apitokens],
61   ].each do |auth, scopes, *expected|
62     test "#{auth.to_s} can find auths where scopes=#{scopes.inspect}" do
63       assert_found_tokens(auth, {where: {scopes: scopes}}, *expected)
64     end
65
66     test "#{auth.to_s} can find auths filtered with scopes=#{scopes.inspect}" do
67       assert_found_tokens(auth, {filters: [['scopes', '=', scopes]]}, *expected)
68     end
69   end
70
71   [# anyone can look up the token they're currently using
72    [:admin, :admin, 200, 200, 1],
73    [:active, :active, 200, 200, 1],
74    # cannot look up other tokens (even for same user) if not trustedclient
75    [:admin, :active, 403, 403],
76    [:admin, :admin_vm, 403, 403],
77    [:active, :admin, 403, 403],
78    # cannot look up other tokens for other users, regardless of trustedclient
79    [:admin_trustedclient, :active, 404, 200, 0],
80    [:active_trustedclient, :admin, 404, 200, 0],
81   ].each do |user, token, expect_get_response, expect_list_response, expect_list_items|
82     test "using '#{user}', get '#{token}' by uuid" do
83       authorize_with user
84       get :show, {
85         id: api_client_authorizations(token).uuid,
86       }
87       assert_response expect_get_response
88     end
89
90     test "using '#{user}', update '#{token}' by uuid" do
91       authorize_with user
92       put :update, {
93         id: api_client_authorizations(token).uuid,
94         api_client_authorization: {},
95       }
96       assert_response expect_get_response
97     end
98
99     test "using '#{user}', delete '#{token}' by uuid" do
100       authorize_with user
101       post :destroy, {
102         id: api_client_authorizations(token).uuid,
103       }
104       assert_response expect_get_response
105     end
106
107     test "using '#{user}', list '#{token}' by uuid" do
108       authorize_with user
109       get :index, {
110         filters: [['uuid','=',api_client_authorizations(token).uuid]],
111       }
112       assert_response expect_list_response
113       if expect_list_items
114         assert_equal assigns(:objects).length, expect_list_items
115       end
116     end
117
118     test "using '#{user}', list '#{token}' by token" do
119       authorize_with user
120       get :index, {
121         filters: [['api_token','=',api_client_authorizations(token).api_token]],
122       }
123       assert_response expect_list_response
124       if expect_list_items
125         assert_equal assigns(:objects).length, expect_list_items
126       end
127     end
128   end
129
130   test "scoped token cannot change its own scopes" do
131     authorize_with :admin_vm
132     put :update, {
133       id: api_client_authorizations(:admin_vm).uuid,
134       api_client_authorization: {scopes: ['all']},
135     }
136     assert_response 403
137   end
138
139   test "token cannot change its own uuid" do
140     authorize_with :admin
141     put :update, {
142       id: api_client_authorizations(:admin).uuid,
143       api_client_authorization: {uuid: 'zzzzz-gj3su-zzzzzzzzzzzzzzz'},
144     }
145     assert_response 403
146   end
147 end