8784: Fix test for latest firefox.
[arvados.git] / services / api / test / integration / api_client_authorizations_scopes_test.rb
1 # The v1 API uses token scopes to control access to the REST API at the path
2 # level.  This is enforced in the base ApplicationController, making it a
3 # functional test that we can run against many different controllers.
4
5 require 'test_helper'
6
7 class ApiTokensScopeTest < ActionDispatch::IntegrationTest
8   fixtures :all
9
10   def v1_url(*parts)
11     (['', 'arvados', 'v1'] + parts).join('/')
12   end
13
14   test "user list token can only list users" do
15     get_args = [{}, auth(:active_userlist)]
16     get(v1_url('users'), *get_args)
17     assert_response :success
18     get(v1_url('users', ''), *get_args)  # Add trailing slash.
19     assert_response :success
20     get(v1_url('users', 'current'), *get_args)
21     assert_response 403
22     get(v1_url('virtual_machines'), *get_args)
23     assert_response 403
24   end
25
26   test "specimens token can see exactly owned specimens" do
27     get_args = [{}, auth(:active_specimens)]
28     get(v1_url('specimens'), *get_args)
29     assert_response 403
30     get(v1_url('specimens', specimens(:owned_by_active_user).uuid), *get_args)
31     assert_response :success
32     head(v1_url('specimens', specimens(:owned_by_active_user).uuid), *get_args)
33     assert_response :success
34     get(v1_url('specimens', specimens(:owned_by_spectator).uuid), *get_args)
35     assert_includes(403..404, @response.status)
36   end
37
38   test "token with multiple scopes can use them all" do
39     def get_token_count
40       get(v1_url('api_client_authorizations'), {}, auth(:active_apitokens))
41       assert_response :success
42       token_count = JSON.parse(@response.body)['items_available']
43       assert_not_nil(token_count, "could not find token count")
44       token_count
45     end
46     # Test the GET scope.
47     token_count = get_token_count
48     # Test the POST scope.
49     post(v1_url('api_client_authorizations'),
50          {api_client_authorization: {user_id: users(:active).id}},
51          auth(:active_apitokens))
52     assert_response :success
53     assert_equal(token_count + 1, get_token_count,
54                  "token count suggests POST was not accepted")
55     # Test other requests are denied.
56     get(v1_url('api_client_authorizations',
57                api_client_authorizations(:active_apitokens).uuid),
58         {}, auth(:active_apitokens))
59     assert_response 403
60   end
61
62   test "token without scope has no access" do
63     # Logs are good for this test, because logs have relatively
64     # few access controls enforced at the model level.
65     req_args = [{}, auth(:admin_noscope)]
66     get(v1_url('logs'), *req_args)
67     assert_response 403
68     get(v1_url('logs', logs(:noop).uuid), *req_args)
69     assert_response 403
70     post(v1_url('logs'), *req_args)
71     assert_response 403
72   end
73
74   test "VM login scopes work" do
75     # A system administration script makes an API token with limited scope
76     # for virtual machines to let it see logins.
77     def vm_logins_url(name)
78       v1_url('virtual_machines', virtual_machines(name).uuid, 'logins')
79     end
80     get_args = [{}, auth(:admin_vm)]
81     get(vm_logins_url(:testvm), *get_args)
82     assert_response :success
83     get(vm_logins_url(:testvm2), *get_args)
84     assert_includes(400..419, @response.status,
85                     "getting testvm2 logins should have failed")
86   end
87 end