Commit b2a5344a2d68922d5c6cb8de228fb9b41ce3efc4

Authored by Robert Speicher
1 parent fba174e9

Add a simple `api` method to ApiHelpers, replacing api_prefix

See docs for usage
spec/requests/api/issues_spec.rb
1 1 require 'spec_helper'
2 2  
3 3 describe Gitlab::API do
  4 + include ApiHelpers
  5 +
4 6 let(:user) { Factory :user }
5 7 let!(:project) { Factory :project, owner: user }
6 8 let!(:issue) { Factory :issue, author: user, assignee: user, project: project }
... ...
spec/requests/api/projects_spec.rb
1 1 require 'spec_helper'
2 2  
3 3 describe Gitlab::API do
  4 + include ApiHelpers
  5 +
4 6 let(:user) { Factory :user }
5 7 let!(:project) { Factory :project, owner: user }
6 8 let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' }
... ...
spec/requests/api/users_spec.rb
1 1 require 'spec_helper'
2 2  
3 3 describe Gitlab::API do
  4 + include ApiHelpers
  5 +
4 6 let(:user) { Factory :user }
5 7  
6 8 describe "GET /users" do
... ...
spec/spec_helper.rb
... ... @@ -27,7 +27,6 @@ RSpec.configure do |config|
27 27 config.mock_with :rspec
28 28  
29 29 config.include LoginHelpers, type: :request
30   - config.include ApiHelpers, type: :request
31 30  
32 31 # If you're not using ActiveRecord, or you'd prefer not to run each of your
33 32 # examples within a transaction, remove the following line or assign false
... ...
spec/support/api_helpers.rb
1 1 module ApiHelpers
2   - def api_prefix
3   - "/api/#{Gitlab::API::VERSION}"
  2 + # Public: Prepend a request path with the path to the API
  3 + #
  4 + # path - Path to append
  5 + # user - User object - If provided, automatically appends private_token query
  6 + # string for authenticated requests
  7 + #
  8 + # Examples
  9 + #
  10 + # >> api('/issues')
  11 + # => "/api/v2/issues"
  12 + #
  13 + # >> api('/issues', User.last)
  14 + # => "/api/v2/issues?private_token=..."
  15 + #
  16 + # >> api('/issues?foo=bar', User.last)
  17 + # => "/api/v2/issues?foo=bar&private_token=..."
  18 + #
  19 + # Returns the relative path to the requested API resource
  20 + def api(path, user = nil)
  21 + "/api/#{Gitlab::API::VERSION}#{path}" +
  22 +
  23 + # Normalize query string
  24 + (path.index('?') ? '' : '?') +
  25 +
  26 + # Append private_token if given a User object
  27 + (user.respond_to?(:private_token) ?
  28 + "&private_token=#{user.private_token}" : "")
4 29 end
5 30  
6 31 def json_response
... ...