bec8b5784a09ae77d450f62b87bc1ebdc40d24e2
[arvados.git] / services / api / test / integration / container_auth_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 ContainerAuthTest < ActionDispatch::IntegrationTest
8   fixtures :all
9
10   test "container token validate, Queued" do
11     get "/arvados/v1/containers/current", {
12       :format => :json
13         }, {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(:container_runtime_token).token}/#{containers(:runtime_token).uuid}"}
14     # Container is Queued, token cannot be used
15     assert_response 401
16   end
17
18   test "container token validate, Running, regular auth" do
19     get "/arvados/v1/containers/current", {
20       :format => :json
21         }, {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(:running_container_auth).token}/#{containers(:running).uuid}"}
22     # Container is Running, token can be used
23     assert_response :success
24     assert_equal containers(:running).uuid, json_response['uuid']
25   end
26
27   test "container token validate, Locked, runtime_token" do
28     post "/arvados/v1/containers/#{containers(:runtime_token).uuid}/lock", {
29       :format => :json
30     }, {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(:dispatch1).token}"}
31     get "/arvados/v1/containers/current", {
32       :format => :json
33         }, {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(:container_runtime_token).token}/#{containers(:runtime_token).uuid}"}
34     # Container is Running, token can be used
35     assert_response :success
36     assert_equal containers(:runtime_token).uuid, json_response['uuid']
37   end
38
39   test "container token validate, Running, without optional portion" do
40     get "/arvados/v1/containers/current", {
41       :format => :json
42         }, {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(:running_container_auth).token}"}
43     # Container is Running, token can be used
44     assert_response :success
45     assert_equal containers(:running).uuid, json_response['uuid']
46   end
47
48   test "container token validate, Locked, runtime_token, without optional portion" do
49     post "/arvados/v1/containers/#{containers(:runtime_token).uuid}/lock", {
50       :format => :json
51     }, {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(:dispatch1).token}"}
52     get "/arvados/v1/containers/current", {
53       :format => :json
54         }, {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(:container_runtime_token).token}"}
55     # runtime_token without container uuid won't return 'current'
56     assert_response 404
57   end
58
59   test "container token validate, wrong container uuid" do
60     post "/arvados/v1/containers/#{containers(:runtime_token).uuid}/lock", {
61       :format => :json
62     }, {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(:dispatch1).token}"}
63     get "/arvados/v1/containers/current", {
64       :format => :json
65         }, {'HTTP_AUTHORIZATION' => "Bearer #{api_client_authorizations(:container_runtime_token).token}/#{containers(:running).uuid}"}
66     # Container uuid mismatch, token can't be used
67     assert_response 401
68   end
69 end