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