Merge branch '8784-dir-listings'
[arvados.git] / services / api / test / integration / reader_tokens_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 ReaderTokensTest < ActionDispatch::IntegrationTest
8   fixtures :all
9
10   def spectator_specimen
11     specimens(:owned_by_spectator).uuid
12   end
13
14   def get_specimens(main_auth, read_auth, formatter=:to_a)
15     params = {}
16     params[:reader_tokens] = [api_token(read_auth)].send(formatter) if read_auth
17     headers = {}
18     headers.merge!(auth(main_auth)) if main_auth
19     get('/arvados/v1/specimens', params, headers)
20   end
21
22   def get_specimen_uuids(main_auth, read_auth, formatter=:to_a)
23     get_specimens(main_auth, read_auth, formatter)
24     assert_response :success
25     json_response['items'].map { |spec| spec['uuid'] }
26   end
27
28   def assert_post_denied(main_auth, read_auth, formatter=:to_a)
29     if main_auth
30       headers = auth(main_auth)
31       expected = 403
32     else
33       headers = {}
34       expected = 401
35     end
36     post('/arvados/v1/specimens.json',
37          {specimen: {}, reader_tokens: [api_token(read_auth)].send(formatter)},
38          headers)
39     assert_response expected
40   end
41
42   test "active user can't see spectator specimen" do
43     # Other tests in this suite assume that the active user doesn't
44     # have read permission to the owned_by_spectator specimen.
45     # This test checks that this assumption still holds.
46     refute_includes(get_specimen_uuids(:active, nil), spectator_specimen,
47                     ["active user can read the owned_by_spectator specimen",
48                      "other tests will return false positives"].join(" - "))
49   end
50
51   [nil, :active_noscope].each do |main_auth|
52     [:spectator, :spectator_specimens].each do |read_auth|
53       test "#{main_auth} auth with reader token #{read_auth} can read" do
54         assert_includes(get_specimen_uuids(main_auth, read_auth),
55                         spectator_specimen, "did not find spectator specimen")
56       end
57
58       test "#{main_auth} auth with JSON read token #{read_auth} can read" do
59         assert_includes(get_specimen_uuids(main_auth, read_auth, :to_json),
60                         spectator_specimen, "did not find spectator specimen")
61       end
62
63       test "#{main_auth} auth with reader token #{read_auth} can't write" do
64         assert_post_denied(main_auth, read_auth)
65       end
66
67       test "#{main_auth} auth with JSON read token #{read_auth} can't write" do
68         assert_post_denied(main_auth, read_auth, :to_json)
69       end
70     end
71   end
72
73   test "scopes are still limited with reader tokens" do
74     get('/arvados/v1/collections',
75         {reader_tokens: [api_token(:spectator_specimens)]},
76         auth(:active_noscope))
77     assert_response 403
78   end
79
80   test "reader tokens grant no permissions when expired" do
81     get_specimens(:active_noscope, :expired)
82     assert_response 403
83   end
84
85   test "reader tokens grant no permissions outside their scope" do
86     refute_includes(get_specimen_uuids(:active, :admin_vm), spectator_specimen,
87                     "scoped reader token granted permissions out of scope")
88   end
89 end