From e078a173d6fad0ec79ed252f5e268f060c37508d Mon Sep 17 00:00:00 2001 From: Alex Denisov <1101.debian@gmail.com> Date: Mon, 3 Sep 2012 18:00:24 +0300 Subject: Create project via API: fixes added --- doc/api/projects.md | 6 ++++++ lib/api/projects.rb | 22 +++++++++++++++----- spec/requests/api/projects_spec.rb | 41 +++++++++++++++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/doc/api/projects.md b/doc/api/projects.md index b0c1e65709c..32775c3216a 100644 --- a/doc/api/projects.md +++ b/doc/api/projects.md @@ -102,6 +102,12 @@ Parameters: + `name` (required) - new project name + `code` (optional) - new project code, uses project name if not set + `path` (optional) - new project path, uses project name if not set ++ `description (optional) - short project description ++ `default_branch` (optional) - 'master' by default ++ `issues_enabled` (optional) - enabled by default ++ `wall_enabled` (optional) - enabled by default ++ `merge_requests_enabled` (optional) - enabled by default ++ `wiki_enabled` (optional) - enabled by default Will return created project with status `201 Created` on success, or `404 Not found` on fail. diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 3d4fde9270f..45c25127813 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -29,14 +29,26 @@ module Gitlab # name (required) - name for new project # code (optional) - code for new project, uses project name if not set # path (optional) - path for new project, uses project name if not set + # description (optional) - short project description + # default_branch (optional) - 'master' by default + # issues_enabled (optional) - enabled by default + # wall_enabled (optional) - enabled by default + # merge_requests_enabled (optional) - enabled by default + # wiki_enabled (optional) - enabled by default # Example Request # POST /projects post do - project = {} - project[:name] = params[:name] - project[:code] = params[:code] || project[:name] - project[:path] = params[:path] || project[:name] - @project = Project.create_by_user(project, current_user) + @project = Project.create_by_user({ + name: params[:name], + code: (params[:code] || params[:name]), + path: (params[:path] || params[:name]), + description: (params[:description] || Project.columns_hash["description"].default), + default_branch: (params[:default_branch] || Project.columns_hash["default_branch"].default), + issues_enabled: (params[:issues_enabled] || Project.columns_hash["issues_enabled"].default), + wall_enabled: (params[:wall_enabled] || Project.columns_hash["wall_enabled"].default), + merge_requests_enabled: (params[:merge_requests_enabled] || Project.columns_hash["merge_requests_enabled"].default), + wiki_enabled: (params[:wiki_enabled] || Project.columns_hash["wiki_enabled"].default) + }, current_user) if @project.saved? present @project, with: Entities::Project else diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index cc6843ccb66..cdab2657012 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -27,7 +27,7 @@ describe Gitlab::API do describe "POST /projects" do it "should create new project without code and path" do - lambda { + expect { name = "foo" post api("/projects", user), { name: name @@ -39,7 +39,41 @@ describe Gitlab::API do }.should change{Project.count}.by(1) end it "should create new project" do - lambda { + expect { + name = "foo" + path = "bar" + code = "bazz" + description = "fuu project" + default_branch = "default_branch" + issues_enabled = false + wall_enabled = false + merge_requests_enabled = false + wiki_enabled = false + post api("/projects", user), { + code: code, + path: path, + name: name, + description: description, + default_branch: default_branch, + issues_enabled: issues_enabled, + wall_enabled: wall_enabled, + merge_requests_enabled: merge_requests_enabled, + wiki_enabled: wiki_enabled + } + response.status.should == 201 + json_response["name"].should == name + json_response["path"].should == path + json_response["code"].should == code + json_response["description"].should == description + json_response["default_branch"].should == default_branch + json_response["issues_enabled"].should == issues_enabled + json_response["wall_enabled"].should == wall_enabled + json_response["merge_requests_enabled"].should == merge_requests_enabled + json_response["wiki_enabled"].should == wiki_enabled + }.should change{Project.count}.by(1) + end + it "should create new projects within all parameters" do + expect { name = "foo" path = "bar" code = "bazz" @@ -53,9 +87,10 @@ describe Gitlab::API do json_response["path"].should == path json_response["code"].should == code }.should change{Project.count}.by(1) + end it "should not create project without name" do - lambda { + expect { post api("/projects", user) response.status.should == 404 }.should_not change{Project.count} -- cgit v1.2.1 From b73d4419ea458a2824a35563bcd84d519b2a5516 Mon Sep 17 00:00:00 2001 From: Alex Denisov <1101.debian@gmail.com> Date: Tue, 4 Sep 2012 09:38:48 +0300 Subject: json_spec added. Create project via REST API fixed --- Gemfile | 1 + Gemfile.lock | 4 +++ spec/factories.rb | 3 ++ spec/requests/api/projects_spec.rb | 62 ++++++++------------------------------ spec/spec_helper.rb | 1 + 5 files changed, 22 insertions(+), 49 deletions(-) diff --git a/Gemfile b/Gemfile index b0724fadf5b..33f9cf4c1ea 100644 --- a/Gemfile +++ b/Gemfile @@ -117,6 +117,7 @@ group :test do gem 'email_spec' gem 'resque_spec' gem "webmock" + gem 'json_spec' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index 7ec37f59dfc..ae85a43a7bf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -207,6 +207,9 @@ GEM jquery-rails railties (>= 3.1.0) json (1.7.5) + json_spec (1.0.3) + multi_json (~> 1.0) + rspec (~> 2.0) kaminari (0.14.0) actionpack (>= 3.0.0) activesupport (>= 3.0.0) @@ -406,6 +409,7 @@ DEPENDENCIES httparty jquery-rails (= 2.0.2) jquery-ui-rails (= 0.5.0) + json_spec kaminari launchy letter_opener diff --git a/spec/factories.rb b/spec/factories.rb index 2e4acf39461..95c919fd915 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -11,6 +11,9 @@ module Factory def self.new(type, *args) FactoryGirl.build(type, *args) end + def self.attributes(type, *args) + FactoryGirl.attributes_for(type, *args) + end end FactoryGirl.define do diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index cdab2657012..43c44974bb0 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -39,55 +39,19 @@ describe Gitlab::API do }.should change{Project.count}.by(1) end it "should create new project" do - expect { - name = "foo" - path = "bar" - code = "bazz" - description = "fuu project" - default_branch = "default_branch" - issues_enabled = false - wall_enabled = false - merge_requests_enabled = false - wiki_enabled = false - post api("/projects", user), { - code: code, - path: path, - name: name, - description: description, - default_branch: default_branch, - issues_enabled: issues_enabled, - wall_enabled: wall_enabled, - merge_requests_enabled: merge_requests_enabled, - wiki_enabled: wiki_enabled - } - response.status.should == 201 - json_response["name"].should == name - json_response["path"].should == path - json_response["code"].should == code - json_response["description"].should == description - json_response["default_branch"].should == default_branch - json_response["issues_enabled"].should == issues_enabled - json_response["wall_enabled"].should == wall_enabled - json_response["merge_requests_enabled"].should == merge_requests_enabled - json_response["wiki_enabled"].should == wiki_enabled - }.should change{Project.count}.by(1) - end - it "should create new projects within all parameters" do - expect { - name = "foo" - path = "bar" - code = "bazz" - post api("/projects", user), { - code: code, - path: path, - name: name - } - response.status.should == 201 - json_response["name"].should == name - json_response["path"].should == path - json_response["code"].should == code - }.should change{Project.count}.by(1) - + attributes = Factory.attributes(:project, + name: "foo", + path: "bar", + code: "bazz", + description: "foo project", + default_branch: "default_branch", + issues_enabled: false, + wall_enabled: false, + merge_requests_enabled: false, + wiki_enabled: false) + post api("/projects", user), attributes + response.status.should == 201 + response.body.should be_json_eql(attributes.to_json).excluding("owner", "private") end it "should not create project without name" do expect { diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d381b3f1e2e..30a213bac18 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -28,6 +28,7 @@ RSpec.configure do |config| config.include LoginHelpers, type: :request config.include GitoliteStub config.include FactoryGirl::Syntax::Methods + config.include JsonSpec::Helpers # 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 -- cgit v1.2.1 From 4bd30245805bc7814fc24686a418a9c883259800 Mon Sep 17 00:00:00 2001 From: Alex Denisov <1101.debian@gmail.com> Date: Tue, 4 Sep 2012 10:29:26 +0300 Subject: json_spec removed --- Gemfile | 1 - Gemfile.lock | 4 --- spec/requests/api/projects_spec.rb | 61 ++++++++++++++++++++------------------ spec/spec_helper.rb | 1 - 4 files changed, 32 insertions(+), 35 deletions(-) diff --git a/Gemfile b/Gemfile index 33f9cf4c1ea..b0724fadf5b 100644 --- a/Gemfile +++ b/Gemfile @@ -117,7 +117,6 @@ group :test do gem 'email_spec' gem 'resque_spec' gem "webmock" - gem 'json_spec' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index ae85a43a7bf..7ec37f59dfc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -207,9 +207,6 @@ GEM jquery-rails railties (>= 3.1.0) json (1.7.5) - json_spec (1.0.3) - multi_json (~> 1.0) - rspec (~> 2.0) kaminari (0.14.0) actionpack (>= 3.0.0) activesupport (>= 3.0.0) @@ -409,7 +406,6 @@ DEPENDENCIES httparty jquery-rails (= 2.0.2) jquery-ui-rails (= 0.5.0) - json_spec kaminari launchy letter_opener diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index 43c44974bb0..1373748f50d 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -27,37 +27,40 @@ describe Gitlab::API do describe "POST /projects" do it "should create new project without code and path" do - expect { - name = "foo" - post api("/projects", user), { - name: name - } - response.status.should == 201 - json_response["name"].should == name - json_response["code"].should == name - json_response["path"].should == name - }.should change{Project.count}.by(1) - end - it "should create new project" do - attributes = Factory.attributes(:project, - name: "foo", - path: "bar", - code: "bazz", - description: "foo project", - default_branch: "default_branch", - issues_enabled: false, - wall_enabled: false, - merge_requests_enabled: false, - wiki_enabled: false) - post api("/projects", user), attributes + expect { post api("/projects", user), name: 'foo' }.to change {Project.count}.by(1) + end + + it "should not create new project without name" do + expect { post api("/projects", user) }.to_not change {Project.count} + end + + it "should respond with 201 on success" do + post api("/projects", user), name: 'foo' response.status.should == 201 - response.body.should be_json_eql(attributes.to_json).excluding("owner", "private") end - it "should not create project without name" do - expect { - post api("/projects", user) - response.status.should == 404 - }.should_not change{Project.count} + + it "should repsond with 404 on failure" do + post api("/projects", user) + response.status.should == 404 + end + + it "should assign attributes to project" do + project = Factory.attributes(:project, { + path: 'path', + code: 'code', + description: Faker::Lorem.sentence, + default_branch: 'stable', + issues_enabled: false, + wall_enabled: false, + merge_requests_enabled: false, + wiki_enabled: false + }) + + post api("/projects", user), project + + project.each_pair do |k,v| + json_response[k.to_s].should == v + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 30a213bac18..d381b3f1e2e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -28,7 +28,6 @@ RSpec.configure do |config| config.include LoginHelpers, type: :request config.include GitoliteStub config.include FactoryGirl::Syntax::Methods - config.include JsonSpec::Helpers # 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 -- cgit v1.2.1 From 9b9dd3f9bf62e8b116213012b766abcaef3e641e Mon Sep 17 00:00:00 2001 From: Alex Denisov <1101.debian@gmail.com> Date: Wed, 5 Sep 2012 11:44:47 +0300 Subject: Unnecessary code removed --- lib/api/projects.rb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 45c25127813..d45d1d82d40 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -38,17 +38,15 @@ module Gitlab # Example Request # POST /projects post do - @project = Project.create_by_user({ - name: params[:name], - code: (params[:code] || params[:name]), - path: (params[:path] || params[:name]), - description: (params[:description] || Project.columns_hash["description"].default), - default_branch: (params[:default_branch] || Project.columns_hash["default_branch"].default), - issues_enabled: (params[:issues_enabled] || Project.columns_hash["issues_enabled"].default), - wall_enabled: (params[:wall_enabled] || Project.columns_hash["wall_enabled"].default), - merge_requests_enabled: (params[:merge_requests_enabled] || Project.columns_hash["merge_requests_enabled"].default), - wiki_enabled: (params[:wiki_enabled] || Project.columns_hash["wiki_enabled"].default) - }, current_user) + params[:code] ||= params[:name] + params[:path] ||= params[:name] + project_attrs = {} + params.each_pair do |k ,v| + if Project.attribute_names.include? k + project_attrs[k] = v + end + end + @project = Project.create_by_user(project_attrs, current_user) if @project.saved? present @project, with: Entities::Project else -- cgit v1.2.1