Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[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 Arvados::V1::ApiTokensScopeTest < ActionController::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     get(v1_url('specimens', specimens(:owned_by_spectator).uuid), *get_args)
33     assert_includes(403..404, @response.status)
34   end
35
36   test "token with multiple scopes can use them all" do
37     def get_token_count
38       get(v1_url('api_client_authorizations'), {}, auth(:active_apitokens))
39       assert_response :success
40       token_count = JSON.parse(@response.body)['items_available']
41       assert_not_nil(token_count, "could not find token count")
42       token_count
43     end
44     # Test the GET scope.
45     token_count = get_token_count
46     # Test the POST scope.
47     post(v1_url('api_client_authorizations'),
48          {api_client_authorization: {user_id: users(:active).id}},
49          auth(:active_apitokens))
50     assert_response :success
51     assert_equal(token_count + 1, get_token_count,
52                  "token count suggests POST was not accepted")
53     # Test other requests are denied.
54     get(v1_url('api_client_authorizations',
55                api_client_authorizations(:active_apitokens).uuid),
56         {}, auth(:active_apitokens))
57     assert_response 403
58   end
59
60   test "token without scope has no access" do
61     # Logs are good for this test, because logs have relatively
62     # few access controls enforced at the model level.
63     req_args = [{}, auth(:admin_noscope)]
64     get(v1_url('logs'), *req_args)
65     assert_response 403
66     get(v1_url('logs', logs(:log1).uuid), *req_args)
67     assert_response 403
68     post(v1_url('logs'), *req_args)
69     assert_response 403
70   end
71
72   test "VM login scopes work" do
73     # A system administration script makes an API token with limited scope
74     # for virtual machines to let it see logins.
75     def vm_logins_url(name)
76       v1_url('virtual_machines', virtual_machines(name).uuid, 'logins')
77     end
78     get_args = [{}, auth(:admin_vm)]
79     get(vm_logins_url(:testvm), *get_args)
80     assert_response :success
81     get(vm_logins_url(:testvm2), *get_args)
82     assert_includes(400..419, @response.status,
83                     "getting testvm2 logins should have failed")
84   end
85 end