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.
7 class Arvados::V1::ApiTokensScopeTest < ActionController::IntegrationTest
11 (['arvados', 'v1'] + parts).join('/')
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)
22 get(v1_url('virtual_machines'), *get_args)
26 test "specimens token can see exactly owned specimens" do
27 get_args = [{}, auth(:active_specimens)]
28 get(v1_url('specimens'), *get_args)
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)
36 test "token with multiple scopes can use them all" do
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")
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))
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)
66 get(v1_url('logs', logs(:noop).uuid), *req_args)
68 post(v1_url('logs'), *req_args)
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')
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")