Merge branch '8784-dir-listings'
[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", {:format => :json, :scopes => ['test'].to_json}, {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:admin_trustedclient).api_token}"}
12     assert_response :success
13   end
14
15   test "create token for different user" do
16     post "/arvados/v1/api_client_authorizations", {
17       :format => :json,
18       :api_client_authorization => {
19         :owner_uuid => users(:spectator).uuid
20       }
21     }, {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:admin_trustedclient).api_token}"}
22     assert_response :success
23
24     get "/arvados/v1/users/current", {
25       :format => :json
26     }, {'HTTP_AUTHORIZATION' => "OAuth2 #{json_response['api_token']}"}
27     @json_response = nil
28     assert_equal users(:spectator).uuid, json_response['uuid']
29   end
30
31   test "refuse to create token for different user if not trusted client" do
32     post "/arvados/v1/api_client_authorizations", {
33       :format => :json,
34       :api_client_authorization => {
35         :owner_uuid => users(:spectator).uuid
36       }
37     }, {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:admin).api_token}"}
38     assert_response 403
39   end
40
41   test "refuse to create token for different user if not admin" do
42     post "/arvados/v1/api_client_authorizations", {
43       :format => :json,
44       :api_client_authorization => {
45         :owner_uuid => users(:spectator).uuid
46       }
47     }, {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:active_trustedclient).api_token}"}
48     assert_response 403
49   end
50
51 end