diff --git a/spec/requests/api/issues_spec.rb b/spec/requests/api/issues_spec.rb index f6d8e3792c2bf7fd24ce2309af80670c5caf47c8..4701ca2fd1a3b268a344732828d305ed85cf48f1 100644 --- a/spec/requests/api/issues_spec.rb +++ b/spec/requests/api/issues_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe Gitlab::API do + include ApiHelpers + let(:user) { Factory :user } let!(:project) { Factory :project, owner: user } let!(:issue) { Factory :issue, author: user, assignee: user, project: project } diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index ff45619e58af31069ca412e32638ea62e94619ea..a721ab3a763cd4a44c4e505ec5f4eeaed79be846 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe Gitlab::API do + include ApiHelpers + let(:user) { Factory :user } let!(:project) { Factory :project, owner: user } let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' } diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index 32b9379d212969d789b529bff5cca9380351eca4..fcfea13104651a59e9b011d23307d61ab56b12d7 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe Gitlab::API do + include ApiHelpers + let(:user) { Factory :user } describe "GET /users" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d3635ed1979fd9942026d7b4c0a8863aaa233632..9fb0ad7e24979f31382792ddc12491eb682098a1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,7 +27,6 @@ RSpec.configure do |config| config.mock_with :rspec config.include LoginHelpers, type: :request - config.include ApiHelpers, type: :request # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false diff --git a/spec/support/api_helpers.rb b/spec/support/api_helpers.rb index e35d5735ec3025302965404766113e87ba10f1a7..7d9011971dd1a91482ff11c2d63ac156993fbdbc 100644 --- a/spec/support/api_helpers.rb +++ b/spec/support/api_helpers.rb @@ -1,6 +1,31 @@ module ApiHelpers - def api_prefix - "/api/#{Gitlab::API::VERSION}" + # Public: Prepend a request path with the path to the API + # + # path - Path to append + # user - User object - If provided, automatically appends private_token query + # string for authenticated requests + # + # Examples + # + # >> api('/issues') + # => "/api/v2/issues" + # + # >> api('/issues', User.last) + # => "/api/v2/issues?private_token=..." + # + # >> api('/issues?foo=bar', User.last) + # => "/api/v2/issues?foo=bar&private_token=..." + # + # Returns the relative path to the requested API resource + def api(path, user = nil) + "/api/#{Gitlab::API::VERSION}#{path}" + + + # Normalize query string + (path.index('?') ? '' : '?') + + + # Append private_token if given a User object + (user.respond_to?(:private_token) ? + "&private_token=#{user.private_token}" : "") end def json_response