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 ApiTokensScopeTest < ActionDispatch::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 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)
38 test "token with multiple scopes can use them all" do
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")
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))
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)
68 get(v1_url('logs', logs(:noop).uuid), *req_args)
70 post(v1_url('logs'), *req_args)
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')
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")