Commit 0ff1d161 authored by Timothy Andrew's avatar Timothy Andrew

Test OAuth token scope verification in the `API::Users` endpoint

parent d774825f
...@@ -14,7 +14,9 @@ describe API::Helpers do ...@@ -14,7 +14,9 @@ describe API::Helpers do
let(:request) { Rack::Request.new(env) } let(:request) { Rack::Request.new(env) }
let(:header) { } let(:header) { }
before { allow_any_instance_of(self.class).to receive(:options).and_return({}) } before do
allow_any_instance_of(self.class).to receive(:options).and_return({})
end
def set_env(user_or_token, identifier) def set_env(user_or_token, identifier)
clear_env clear_env
......
shared_examples_for 'allows the "read_user" scope' do shared_examples_for 'allows the "read_user" scope' do
describe 'when the requesting token has the "read_user" scope' do context 'for personal access tokens' do
let(:token) { create(:personal_access_token, scopes: ['read_user'], user: user) } context 'when the requesting token has the "api" scope' do
let(:token) { create(:personal_access_token, scopes: ['api'], user: user) }
it 'returns a "200" response' do
get api_call.call(path, user, personal_access_token: token)
expect(response).to have_http_status(200)
end
end
context 'when the requesting token has the "read_user" scope' do
let(:token) { create(:personal_access_token, scopes: ['read_user'], user: user) }
it 'returns a "200" response' do it 'returns a "200" response' do
get api_call.call(path, user, personal_access_token: token) get api_call.call(path, user, personal_access_token: token)
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end
end
context 'when the requesting token does not have any required scope' do
let(:token) { create(:personal_access_token, scopes: ['read_registry'], user: user) }
it 'returns a "401" response' do
get api_call.call(path, user, personal_access_token: token)
expect(response).to have_http_status(401)
end
end end
end end
describe 'when the requesting token does not have any required scope' do context 'for doorkeeper (OAuth) tokens' do
let(:token) { create(:personal_access_token, scopes: ['read_registry'], user: user) } let!(:user) {create(:user)}
let!(:application) { Doorkeeper::Application.create!(name: "MyApp", redirect_uri: "https://app.com", owner: user) }
it 'returns a "401" response' do context 'when the requesting token has the "api" scope' do
get api_call.call(path, user, personal_access_token: token) let!(:token) { Doorkeeper::AccessToken.create! application_id: application.id, resource_owner_id: user.id, scopes: "api" }
expect(response).to have_http_status(401) it 'returns a "200" response' do
get api_call.call(path, user, oauth_access_token: token)
expect(response).to have_http_status(200)
end
end
context 'when the requesting token has the "read_user" scope' do
let!(:token) { Doorkeeper::AccessToken.create! application_id: application.id, resource_owner_id: user.id, scopes: "read_user" }
it 'returns a "200" response' do
get api_call.call(path, user, oauth_access_token: token)
expect(response).to have_http_status(200)
end
end
context 'when the requesting token does not have any required scope' do
let!(:token) { Doorkeeper::AccessToken.create! application_id: application.id, resource_owner_id: user.id, scopes: "invalid" }
it 'returns a "403" response' do
get api_call.call(path, user, oauth_access_token: token)
expect(response).to have_http_status(403)
end
end end
end end
end end
......
...@@ -17,7 +17,7 @@ module ApiHelpers ...@@ -17,7 +17,7 @@ module ApiHelpers
# => "/api/v2/issues?foo=bar&private_token=..." # => "/api/v2/issues?foo=bar&private_token=..."
# #
# Returns the relative path to the requested API resource # Returns the relative path to the requested API resource
def api(path, user = nil, version: API::API.version, personal_access_token: nil) def api(path, user = nil, version: API::API.version, personal_access_token: nil, oauth_access_token: nil)
"/api/#{version}#{path}" + "/api/#{version}#{path}" +
# Normalize query string # Normalize query string
...@@ -25,6 +25,8 @@ module ApiHelpers ...@@ -25,6 +25,8 @@ module ApiHelpers
if personal_access_token.present? if personal_access_token.present?
"&private_token=#{personal_access_token.token}" "&private_token=#{personal_access_token.token}"
elsif oauth_access_token.present?
"&access_token=#{oauth_access_token.token}"
# Append private_token if given a User object # Append private_token if given a User object
elsif user.respond_to?(:private_token) elsif user.respond_to?(:private_token)
"&private_token=#{user.private_token}" "&private_token=#{user.private_token}"
...@@ -34,8 +36,14 @@ module ApiHelpers ...@@ -34,8 +36,14 @@ module ApiHelpers
end end
# Temporary helper method for simplifying V3 exclusive API specs # Temporary helper method for simplifying V3 exclusive API specs
def v3_api(path, user = nil, personal_access_token: nil) def v3_api(path, user = nil, personal_access_token: nil, oauth_access_token: nil)
api(path, user, version: 'v3', personal_access_token: personal_access_token) api(
path,
user,
version: 'v3',
personal_access_token: personal_access_token,
oauth_access_token: oauth_access_token
)
end end
def ci_api(path, user = nil) def ci_api(path, user = nil)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment