Commit b2a5344a2d68922d5c6cb8de228fb9b41ce3efc4
1 parent
fba174e9
Exists in
master
and in
4 other branches
Add a simple `api` method to ApiHelpers, replacing api_prefix
See docs for usage
Showing
5 changed files
with
33 additions
and
3 deletions
Show diff stats
spec/requests/api/issues_spec.rb
| 1 | require 'spec_helper' | 1 | require 'spec_helper' |
| 2 | 2 | ||
| 3 | describe Gitlab::API do | 3 | describe Gitlab::API do |
| 4 | + include ApiHelpers | ||
| 5 | + | ||
| 4 | let(:user) { Factory :user } | 6 | let(:user) { Factory :user } |
| 5 | let!(:project) { Factory :project, owner: user } | 7 | let!(:project) { Factory :project, owner: user } |
| 6 | let!(:issue) { Factory :issue, author: user, assignee: user, project: project } | 8 | let!(:issue) { Factory :issue, author: user, assignee: user, project: project } |
spec/requests/api/projects_spec.rb
| 1 | require 'spec_helper' | 1 | require 'spec_helper' |
| 2 | 2 | ||
| 3 | describe Gitlab::API do | 3 | describe Gitlab::API do |
| 4 | + include ApiHelpers | ||
| 5 | + | ||
| 4 | let(:user) { Factory :user } | 6 | let(:user) { Factory :user } |
| 5 | let!(:project) { Factory :project, owner: user } | 7 | let!(:project) { Factory :project, owner: user } |
| 6 | let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' } | 8 | let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' } |
spec/requests/api/users_spec.rb
spec/spec_helper.rb
| @@ -27,7 +27,6 @@ RSpec.configure do |config| | @@ -27,7 +27,6 @@ RSpec.configure do |config| | ||
| 27 | config.mock_with :rspec | 27 | config.mock_with :rspec |
| 28 | 28 | ||
| 29 | config.include LoginHelpers, type: :request | 29 | config.include LoginHelpers, type: :request |
| 30 | - config.include ApiHelpers, type: :request | ||
| 31 | 30 | ||
| 32 | # If you're not using ActiveRecord, or you'd prefer not to run each of your | 31 | # If you're not using ActiveRecord, or you'd prefer not to run each of your |
| 33 | # examples within a transaction, remove the following line or assign false | 32 | # examples within a transaction, remove the following line or assign false |
spec/support/api_helpers.rb
| 1 | module ApiHelpers | 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 | end | 29 | end |
| 5 | 30 | ||
| 6 | def json_response | 31 | def json_response |