16306: Merge branch 'master'
[arvados.git] / services / api / test / integration / api_client_authorizations_api_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class ApiClientAuthorizationsApiTest < ActionDispatch::IntegrationTest
8   fixtures :all
9
10   test "create system auth" do
11     post "/arvados/v1/api_client_authorizations/create_system_auth",
12       params: {:format => :json, :scopes => ['test'].to_json},
13       headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:admin_trustedclient).api_token}"}
14     assert_response :success
15   end
16
17   [:admin_trustedclient, :SystemRootToken].each do |tk|
18     test "create token for different user using #{tk}" do
19       if tk == :SystemRootToken
20         token = "xyzzy-SystemRootToken"
21         Rails.configuration.SystemRootToken = token
22       else
23         token = api_client_authorizations(tk).api_token
24       end
25
26       post "/arvados/v1/api_client_authorizations",
27            params: {
28              :format => :json,
29              :api_client_authorization => {
30                :owner_uuid => users(:spectator).uuid
31              }
32            },
33            headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{token}"}
34       assert_response :success
35
36       get "/arvados/v1/users/current",
37           params: {:format => :json},
38           headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{json_response['api_token']}"}
39       @json_response = nil
40       assert_equal json_response['uuid'], users(:spectator).uuid
41     end
42   end
43
44   test "System root token is system user" do
45     token = "xyzzy-SystemRootToken"
46     Rails.configuration.SystemRootToken = token
47     get "/arvados/v1/users/current",
48         params: {:format => :json},
49         headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{token}"}
50     assert_equal json_response['uuid'], system_user_uuid
51   end
52
53   test "refuse to create token for different user if not trusted client" do
54     post "/arvados/v1/api_client_authorizations",
55       params: {
56         :format => :json,
57         :api_client_authorization => {
58           :owner_uuid => users(:spectator).uuid
59         }
60       },
61       headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:admin).api_token}"}
62     assert_response 403
63   end
64
65   test "refuse to create token for different user if not admin" do
66     post "/arvados/v1/api_client_authorizations",
67       params: {
68         :format => :json,
69         :api_client_authorization => {
70           :owner_uuid => users(:spectator).uuid
71         }
72       },
73       headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:active_trustedclient).api_token}"}
74     assert_response 403
75   end
76
77 end