Commit fbd559d44f1fc7e37f88844a9260bf7a5e1ba822
1 parent
378dc55d
Exists in
master
and in
4 other branches
add API version
Showing
5 changed files
with
37 additions
and
34 deletions
Show diff stats
lib/api.rb
| @@ -2,6 +2,9 @@ Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file} | @@ -2,6 +2,9 @@ Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file} | ||
| 2 | 2 | ||
| 3 | module Gitlab | 3 | module Gitlab |
| 4 | class API < Grape::API | 4 | class API < Grape::API |
| 5 | + VERSION = 'v2' | ||
| 6 | + version VERSION, :using => :path | ||
| 7 | + | ||
| 5 | format :json | 8 | format :json |
| 6 | error_format :json | 9 | error_format :json |
| 7 | helpers APIHelpers | 10 | helpers APIHelpers |
spec/api/projects_spec.rb
| @@ -8,75 +8,69 @@ describe Gitlab::API do | @@ -8,75 +8,69 @@ describe Gitlab::API do | ||
| 8 | 8 | ||
| 9 | describe "GET /projects" do | 9 | describe "GET /projects" do |
| 10 | it "should return authentication error" do | 10 | it "should return authentication error" do |
| 11 | - get "/api/projects" | 11 | + get "#{api_prefix}/projects" |
| 12 | response.status.should == 401 | 12 | response.status.should == 401 |
| 13 | end | 13 | end |
| 14 | 14 | ||
| 15 | describe "authenticated GET /projects" do | 15 | describe "authenticated GET /projects" do |
| 16 | it "should return an array of projects" do | 16 | it "should return an array of projects" do |
| 17 | - get "/api/projects?private_token=#{user.private_token}" | 17 | + get "#{api_prefix}/projects?private_token=#{user.private_token}" |
| 18 | response.status.should == 200 | 18 | response.status.should == 200 |
| 19 | - json = JSON.parse(response.body) | ||
| 20 | - json.should be_an Array | ||
| 21 | - json.first['name'].should == project.name | ||
| 22 | - json.first['owner']['email'].should == user.email | 19 | + json_response.should be_an Array |
| 20 | + json_response.first['name'].should == project.name | ||
| 21 | + json_response.first['owner']['email'].should == user.email | ||
| 23 | end | 22 | end |
| 24 | end | 23 | end |
| 25 | end | 24 | end |
| 26 | 25 | ||
| 27 | describe "GET /projects/:id" do | 26 | describe "GET /projects/:id" do |
| 28 | it "should return a project by id" do | 27 | it "should return a project by id" do |
| 29 | - get "/api/projects/#{project.code}?private_token=#{user.private_token}" | 28 | + get "#{api_prefix}/projects/#{project.code}?private_token=#{user.private_token}" |
| 30 | response.status.should == 200 | 29 | response.status.should == 200 |
| 31 | - json = JSON.parse(response.body) | ||
| 32 | - json['name'].should == project.name | ||
| 33 | - json['owner']['email'].should == user.email | 30 | + json_response['name'].should == project.name |
| 31 | + json_response['owner']['email'].should == user.email | ||
| 34 | end | 32 | end |
| 35 | end | 33 | end |
| 36 | 34 | ||
| 37 | describe "GET /projects/:id/repository/branches" do | 35 | describe "GET /projects/:id/repository/branches" do |
| 38 | it "should return an array of project branches" do | 36 | it "should return an array of project branches" do |
| 39 | - get "/api/projects/#{project.code}/repository/branches?private_token=#{user.private_token}" | 37 | + get "#{api_prefix}/projects/#{project.code}/repository/branches?private_token=#{user.private_token}" |
| 40 | response.status.should == 200 | 38 | response.status.should == 200 |
| 41 | - json = JSON.parse(response.body) | ||
| 42 | - json.should be_an Array | ||
| 43 | - json.first['name'].should == project.repo.heads.sort_by(&:name).first.name | 39 | + json_response.should be_an Array |
| 40 | + json_response.first['name'].should == project.repo.heads.sort_by(&:name).first.name | ||
| 44 | end | 41 | end |
| 45 | end | 42 | end |
| 46 | 43 | ||
| 47 | describe "GET /projects/:id/repository/tags" do | 44 | describe "GET /projects/:id/repository/tags" do |
| 48 | it "should return an array of project tags" do | 45 | it "should return an array of project tags" do |
| 49 | - get "/api/projects/#{project.code}/repository/tags?private_token=#{user.private_token}" | 46 | + get "#{api_prefix}/projects/#{project.code}/repository/tags?private_token=#{user.private_token}" |
| 50 | response.status.should == 200 | 47 | response.status.should == 200 |
| 51 | - json = JSON.parse(response.body) | ||
| 52 | - json.should be_an Array | ||
| 53 | - json.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name | 48 | + json_response.should be_an Array |
| 49 | + json_response.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name | ||
| 54 | end | 50 | end |
| 55 | end | 51 | end |
| 56 | 52 | ||
| 57 | describe "GET /projects/:id/snippets/:snippet_id" do | 53 | describe "GET /projects/:id/snippets/:snippet_id" do |
| 58 | it "should return a project snippet" do | 54 | it "should return a project snippet" do |
| 59 | - get "/api/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}" | 55 | + get "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}" |
| 60 | response.status.should == 200 | 56 | response.status.should == 200 |
| 61 | - json = JSON.parse(response.body) | ||
| 62 | - json['title'].should == snippet.title | 57 | + json_response['title'].should == snippet.title |
| 63 | end | 58 | end |
| 64 | end | 59 | end |
| 65 | 60 | ||
| 66 | describe "POST /projects/:id/snippets" do | 61 | describe "POST /projects/:id/snippets" do |
| 67 | it "should create a new project snippet" do | 62 | it "should create a new project snippet" do |
| 68 | - post "/api/projects/#{project.code}/snippets?private_token=#{user.private_token}", | 63 | + post "#{api_prefix}/projects/#{project.code}/snippets?private_token=#{user.private_token}", |
| 69 | :title => 'api test', :file_name => 'sample.rb', :code => 'test' | 64 | :title => 'api test', :file_name => 'sample.rb', :code => 'test' |
| 70 | response.status.should == 201 | 65 | response.status.should == 201 |
| 71 | - json = JSON.parse(response.body) | ||
| 72 | - json['title'].should == 'api test' | 66 | + json_response['title'].should == 'api test' |
| 73 | end | 67 | end |
| 74 | end | 68 | end |
| 75 | 69 | ||
| 76 | describe "DELETE /projects/:id/snippets/:snippet_id" do | 70 | describe "DELETE /projects/:id/snippets/:snippet_id" do |
| 77 | it "should create a new project snippet" do | 71 | it "should create a new project snippet" do |
| 78 | expect { | 72 | expect { |
| 79 | - delete "/api/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}" | 73 | + delete "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}" |
| 80 | }.should change { Snippet.count }.by(-1) | 74 | }.should change { Snippet.count }.by(-1) |
| 81 | end | 75 | end |
| 82 | end | 76 | end |
spec/api/users_spec.rb
| @@ -5,34 +5,33 @@ describe Gitlab::API do | @@ -5,34 +5,33 @@ describe Gitlab::API do | ||
| 5 | 5 | ||
| 6 | describe "GET /users" do | 6 | describe "GET /users" do |
| 7 | it "should return authentication error" do | 7 | it "should return authentication error" do |
| 8 | - get "/api/users" | 8 | + get "#{api_prefix}/users" |
| 9 | response.status.should == 401 | 9 | response.status.should == 401 |
| 10 | end | 10 | end |
| 11 | 11 | ||
| 12 | describe "authenticated GET /users" do | 12 | describe "authenticated GET /users" do |
| 13 | it "should return an array of users" do | 13 | it "should return an array of users" do |
| 14 | - get "/api/users?private_token=#{user.private_token}" | 14 | + get "#{api_prefix}/users?private_token=#{user.private_token}" |
| 15 | response.status.should == 200 | 15 | response.status.should == 200 |
| 16 | - json = JSON.parse(response.body) | ||
| 17 | - json.should be_an Array | ||
| 18 | - json.first['email'].should == user.email | 16 | + json_response.should be_an Array |
| 17 | + json_response.first['email'].should == user.email | ||
| 19 | end | 18 | end |
| 20 | end | 19 | end |
| 21 | end | 20 | end |
| 22 | 21 | ||
| 23 | describe "GET /users/:id" do | 22 | describe "GET /users/:id" do |
| 24 | it "should return a user by id" do | 23 | it "should return a user by id" do |
| 25 | - get "/api/users/#{user.id}?private_token=#{user.private_token}" | 24 | + get "#{api_prefix}/users/#{user.id}?private_token=#{user.private_token}" |
| 26 | response.status.should == 200 | 25 | response.status.should == 200 |
| 27 | - JSON.parse(response.body)['email'].should == user.email | 26 | + json_response['email'].should == user.email |
| 28 | end | 27 | end |
| 29 | end | 28 | end |
| 30 | 29 | ||
| 31 | describe "GET /user" do | 30 | describe "GET /user" do |
| 32 | it "should return current user" do | 31 | it "should return current user" do |
| 33 | - get "/api/user?private_token=#{user.private_token}" | 32 | + get "#{api_prefix}/user?private_token=#{user.private_token}" |
| 34 | response.status.should == 200 | 33 | response.status.should == 200 |
| 35 | - JSON.parse(response.body)['email'].should == user.email | 34 | + json_response['email'].should == user.email |
| 36 | end | 35 | end |
| 37 | end | 36 | end |
| 38 | end | 37 | end |
spec/support/security.rb