From b1a9e2aa21642c1577510cd439abfe8fb174ff25 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 8 Oct 2012 16:47:02 +0400 Subject: [PATCH 01/39] #374: updated api for projects --- app/views/api/v1/projects/get_id.json.jbuilder | 8 ++++++++ app/views/api/v1/projects/show.json.jbuilder | 11 +++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/views/api/v1/projects/get_id.json.jbuilder b/app/views/api/v1/projects/get_id.json.jbuilder index db584a3c8..4f26ee8d8 100644 --- a/app/views/api/v1/projects/get_id.json.jbuilder +++ b/app/views/api/v1/projects/get_id.json.jbuilder @@ -5,5 +5,13 @@ json.project do |json| json_owner.type @project.owner_type json_owner.url url_for(@project.owner) end + json.repositories @project.repositories do |json_repos, repo| + json_repos.(repo, :id, :name) + json_repos.url api_v1_repository_path(repo.name, :format => :json) + json_repos.platform do |json_platform| + json_platform.(repo.platform, :id, :name) + json_platform.url api_v1_platform_path(repo.platform, :format => :json) + end + end json.url api_v1_project_path(@project, :format => :json) end diff --git a/app/views/api/v1/projects/show.json.jbuilder b/app/views/api/v1/projects/show.json.jbuilder index ed8b5f1ce..553c586c5 100644 --- a/app/views/api/v1/projects/show.json.jbuilder +++ b/app/views/api/v1/projects/show.json.jbuilder @@ -1,7 +1,6 @@ json.project do |json| json.(@project, :id, :name, :visibility, :description, :ancestry, :has_issues, :has_wiki, - :srpm_file_name, :srpm_content_type, :srpm_file_size, :srpm_updated_at, :default_branch, :is_package, - :average_build_time, :build_count) + :default_branch, :is_package, :average_build_time) json.created_at @project.created_at.to_i json.updated_at @project.updated_at.to_i json.owner do |json_owner| @@ -9,6 +8,14 @@ json.project do |json| json_owner.type @project.owner_type json_owner.url url_for(@project.owner) end + json.repositories @project.repositories do |json_repos, repo| + json_repos.(repo, :id, :name) + json_repos.url api_v1_repository_path(repo.name, :format => :json) + json_repos.platform do |json_platform| + json_platform.(repo.platform, :id, :name) + json_platform.url api_v1_platform_path(repo.platform, :format => :json) + end + end end json.url api_v1_project_path(@project, :format => :json) From ec0ec5fc32bf8459cec23068007f4ca3d8b7bf67 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 8 Oct 2012 17:37:09 +0400 Subject: [PATCH 02/39] #374: revert some changes --- app/views/api/v1/projects/get_id.json.jbuilder | 8 -------- 1 file changed, 8 deletions(-) diff --git a/app/views/api/v1/projects/get_id.json.jbuilder b/app/views/api/v1/projects/get_id.json.jbuilder index 4f26ee8d8..db584a3c8 100644 --- a/app/views/api/v1/projects/get_id.json.jbuilder +++ b/app/views/api/v1/projects/get_id.json.jbuilder @@ -5,13 +5,5 @@ json.project do |json| json_owner.type @project.owner_type json_owner.url url_for(@project.owner) end - json.repositories @project.repositories do |json_repos, repo| - json_repos.(repo, :id, :name) - json_repos.url api_v1_repository_path(repo.name, :format => :json) - json_repos.platform do |json_platform| - json_platform.(repo.platform, :id, :name) - json_platform.url api_v1_platform_path(repo.platform, :format => :json) - end - end json.url api_v1_project_path(@project, :format => :json) end From feefe8ed89ddc2bc53a7fab4e9e730f580101e1d Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 8 Oct 2012 22:02:44 +0400 Subject: [PATCH 03/39] #374: added #platforms_for_build action for Platforms API --- app/controllers/api/v1/platforms_controller.rb | 6 +++++- config/routes.rb | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index 25e4b63b1..361b9f9bd 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -2,7 +2,7 @@ class Api::V1::PlatformsController < Api::V1::BaseController before_filter :authenticate_user! - skip_before_filter :authenticate_user!, :only => [:show] if APP_CONFIG['anonymous_access'] + skip_before_filter :authenticate_user!, :only => [:show, :platforms_for_build] if APP_CONFIG['anonymous_access'] load_and_authorize_resource @@ -12,6 +12,10 @@ class Api::V1::PlatformsController < Api::V1::BaseController end def show + end + def platforms_for_build + @platforms = Platform.main.opened.paginate(:page => params[:page], :per_page => 20) + render :index end end diff --git a/config/routes.rb b/config/routes.rb index 319bc2f1b..098e7d9f5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,7 +20,9 @@ Rosa::Application.routes.draw do } end resources :arches, :only => [:index] - resources :platforms, :only => [:index, :show] + resources :platforms, :only => [:index, :show] do + collection { get :platforms_for_build } + end resources :repositories, :only => [:show] resources :projects, :only => [:show] do collection { get :get_id } From d83243a3a30497043d1b7b50a5893ba94cd17d69 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 9 Oct 2012 17:47:05 +0400 Subject: [PATCH 04/39] hot fix for API --- app/models/ability.rb | 2 +- app/views/api/v1/platforms/index.json.jbuilder | 8 +++----- app/views/api/v1/platforms/show.json.jbuilder | 8 +++----- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index 1ffd45c91..fbd7b5171 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -27,7 +27,7 @@ class Ability can [:publish_build, :status_build, :pre_build, :post_build, :circle_build, :new_bbdt], BuildList # Platforms block - can [:show, :members, :advisories], Platform, :visibility => 'open' + can [:show, :members, :advisories, :platforms_for_build], Platform, :visibility => 'open' can [:read, :projects_list], Repository, :platform => {:visibility => 'open'} can :read, Product, :platform => {:visibility => 'open'} diff --git a/app/views/api/v1/platforms/index.json.jbuilder b/app/views/api/v1/platforms/index.json.jbuilder index 6faab63d8..ac3d0dc3a 100644 --- a/app/views/api/v1/platforms/index.json.jbuilder +++ b/app/views/api/v1/platforms/index.json.jbuilder @@ -5,11 +5,9 @@ json.platforms @platforms do |json, platform| json_owner.type platform.owner_type json_owner.url url_for(platform.owner) end - json.repositories do |json_rep| - platform.repositories.each do |repo| - json_rep.(repo, :id, :name) - json_rep.url api_v1_repository_path(repo.name, :format => :json) - end + json.repositories platform.repositories do |json_repos, repo| + json_repos.(repo, :id, :name) + json_repos.url api_v1_repository_path(repo.name, :format => :json) end json.url api_v1_platform_path(platform.name, :format => :json) end diff --git a/app/views/api/v1/platforms/show.json.jbuilder b/app/views/api/v1/platforms/show.json.jbuilder index b707681bc..21a1b07f0 100644 --- a/app/views/api/v1/platforms/show.json.jbuilder +++ b/app/views/api/v1/platforms/show.json.jbuilder @@ -7,11 +7,9 @@ json.platform do |json| json_owner.type @platform.owner_type json_owner.url url_for(@platform.owner) end - json.repositories do |json_rep| - @platform.repositories.each do |repo| - json_rep.(repo, :id, :name) - json_rep.url api_v1_repository_path(repo.name, :format => :json) - end + json.repositories @platform.repositories do |json_repos, repo| + json_repos.(repo, :id, :name) + json_repos.url api_v1_repository_path(repo.name, :format => :json) end end json.url api_v1_platform_path(@platform, :format => :json) From 1e2d41b183477d832369b0e51881e7e7f9a59af9 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 9 Oct 2012 17:50:09 +0400 Subject: [PATCH 05/39] updated Ability model --- app/models/ability.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index fbd7b5171..8324712fe 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -27,7 +27,8 @@ class Ability can [:publish_build, :status_build, :pre_build, :post_build, :circle_build, :new_bbdt], BuildList # Platforms block - can [:show, :members, :advisories, :platforms_for_build], Platform, :visibility => 'open' + can [:show, :members, :advisories], Platform, :visibility => 'open' + can :platforms_for_build, Platform, :visibility => 'open', :name => 'main' can [:read, :projects_list], Repository, :platform => {:visibility => 'open'} can :read, Product, :platform => {:visibility => 'open'} From 07bf8f1d0f5d7be2efdcc56cb40486b9d33b7971 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 9 Oct 2012 17:54:17 +0400 Subject: [PATCH 06/39] fixed missprint --- app/models/ability.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index 8324712fe..24c4a7a6a 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -28,7 +28,7 @@ class Ability # Platforms block can [:show, :members, :advisories], Platform, :visibility => 'open' - can :platforms_for_build, Platform, :visibility => 'open', :name => 'main' + can :platforms_for_build, Platform, :visibility => 'open', :platform_type => 'main' can [:read, :projects_list], Repository, :platform => {:visibility => 'open'} can :read, Product, :platform => {:visibility => 'open'} From abe31b3c2eca5e2c96cec5e2dc18ded268d1bbf8 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 9 Oct 2012 18:46:20 +0400 Subject: [PATCH 07/39] #671: added API #update action for platform --- app/controllers/api/v1/platforms_controller.rb | 18 ++++++++++++++++++ app/models/platform.rb | 3 ++- app/views/api/v1/platforms/index.json.jbuilder | 2 +- app/views/api/v1/platforms/show.json.jbuilder | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index 361b9f9bd..b27a98513 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -18,4 +18,22 @@ class Api::V1::PlatformsController < Api::V1::BaseController @platforms = Platform.main.opened.paginate(:page => params[:page], :per_page => 20) render :index end + + def update + p = params[:platform] || {} + owner = User.where(:id => p[:owner_id]).first + platform_params = {} + platform_params[:owner] = owner if owner + platform_params[:released] = p[:released] if p[:released] + platform_params[:description] = p[:description] if p[:description] + if @platform.update_attributes(p) + render :json => { + :platform => {:id => @platform.id, + :message => 'Platform has been updated successfully'} + }.to_json + else + render :json => {:message => "Validation Failed", :errors => @platform.errors.messages}.to_json, :status => 422 + end + end + end diff --git a/app/models/platform.rb b/app/models/platform.rb index 2dedbbde4..e43db852b 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -20,6 +20,7 @@ class Platform < ActiveRecord::Base has_many :mass_builds validates :description, :presence => true + validates :owner, :presence => true validates :visibility, :presence => true, :inclusion => {:in => VISIBILITIES} validates :name, :uniqueness => {:case_sensitive => false}, :presence => true, :format => { :with => /^[a-zA-Z0-9_\-\.]+$/ } validates :distrib_type, :presence => true, :inclusion => {:in => APP_CONFIG['distr_types']} @@ -167,7 +168,7 @@ class Platform < ActiveRecord::Base def update_owner_relation if owner_id_was != owner_id - r = relations.where(:actor_id => owner_id_was, :actor_type => owner_type_was)[0] + r = relations.where(:actor_id => owner_id_was, :actor_type => owner_type_was).first r.update_attributes(:actor_id => owner_id, :actor_type => owner_type) end end diff --git a/app/views/api/v1/platforms/index.json.jbuilder b/app/views/api/v1/platforms/index.json.jbuilder index ac3d0dc3a..1a7298f12 100644 --- a/app/views/api/v1/platforms/index.json.jbuilder +++ b/app/views/api/v1/platforms/index.json.jbuilder @@ -7,7 +7,7 @@ json.platforms @platforms do |json, platform| end json.repositories platform.repositories do |json_repos, repo| json_repos.(repo, :id, :name) - json_repos.url api_v1_repository_path(repo.name, :format => :json) + json_repos.url api_v1_repository_path(repo.id, :format => :json) end json.url api_v1_platform_path(platform.name, :format => :json) end diff --git a/app/views/api/v1/platforms/show.json.jbuilder b/app/views/api/v1/platforms/show.json.jbuilder index 21a1b07f0..05be542b8 100644 --- a/app/views/api/v1/platforms/show.json.jbuilder +++ b/app/views/api/v1/platforms/show.json.jbuilder @@ -9,7 +9,7 @@ json.platform do |json| end json.repositories @platform.repositories do |json_repos, repo| json_repos.(repo, :id, :name) - json_repos.url api_v1_repository_path(repo.name, :format => :json) + json_repos.url api_v1_repository_path(repo.id, :format => :json) end end json.url api_v1_platform_path(@platform, :format => :json) From 9e6e2de9555e80e12da63157aeb40395be43c219 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 9 Oct 2012 19:06:27 +0400 Subject: [PATCH 08/39] #671: added #members action for platforms API --- app/controllers/api/v1/base_controller.rb | 4 ++++ app/controllers/api/v1/platforms_controller.rb | 8 ++++++-- app/views/api/v1/platforms/members.json.jbuilder | 8 ++++++++ config/routes.rb | 9 +++++++-- 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 app/views/api/v1/platforms/members.json.jbuilder diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 7d8ef241d..97034e12a 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -17,4 +17,8 @@ class Api::V1::BaseController < ApplicationController params[:per_page] = 100 if params[:per_page].to_i >100 end + def paginate_params + {:page => params[:page], :per_page => 20} + end + end diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index b27a98513..ae5c4b84f 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -8,14 +8,14 @@ class Api::V1::PlatformsController < Api::V1::BaseController def index @platforms = @platforms.accessible_by(current_ability, :related). - by_type(params[:type]).paginate(:page => params[:page], :per_page => 20) + by_type(params[:type]).paginate(paginate_params) end def show end def platforms_for_build - @platforms = Platform.main.opened.paginate(:page => params[:page], :per_page => 20) + @platforms = Platform.main.opened.paginate(paginate_params) render :index end @@ -36,4 +36,8 @@ class Api::V1::PlatformsController < Api::V1::BaseController end end + def members + @members = @platform.members.order('name').paginate(paginate_params) + end + end diff --git a/app/views/api/v1/platforms/members.json.jbuilder b/app/views/api/v1/platforms/members.json.jbuilder new file mode 100644 index 000000000..d491ca5ff --- /dev/null +++ b/app/views/api/v1/platforms/members.json.jbuilder @@ -0,0 +1,8 @@ +json.platform do |json| + json.(@platform, :id) + json.members @members do |json_members, member| + json_members.(member, :id) + json_members.type member.class.name + end +end +json.url members_api_v1_platform_path(@platform, :format => :json) \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 098e7d9f5..3c1ea38fe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,8 +20,13 @@ Rosa::Application.routes.draw do } end resources :arches, :only => [:index] - resources :platforms, :only => [:index, :show] do - collection { get :platforms_for_build } + resources :platforms, :only => [:index, :show, :update] do + collection { + get :platforms_for_build + } + member { + get :members + } end resources :repositories, :only => [:show] resources :projects, :only => [:show] do From 30909b281dae8b637c8bba5ee981986c3f875e1a Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 9 Oct 2012 20:23:48 +0400 Subject: [PATCH 09/39] #671: added #add_member action for platforms API, added new specs --- app/controllers/api/v1/base_controller.rb | 4 ++ .../api/v1/platforms_controller.rb | 29 +++++++-- config/routes.rb | 1 + .../api/v1/platforms_controller_spec.rb | 61 ++++++++++++++++--- 4 files changed, 83 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 97034e12a..bfee062a8 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -21,4 +21,8 @@ class Api::V1::BaseController < ApplicationController {:page => params[:page], :per_page => 20} end + def validation_failed(subject) + {:message => "Validation Failed", :errors => subject.errors.messages}.to_json + end + end diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index ae5c4b84f..630395a07 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -2,7 +2,7 @@ class Api::V1::PlatformsController < Api::V1::BaseController before_filter :authenticate_user! - skip_before_filter :authenticate_user!, :only => [:show, :platforms_for_build] if APP_CONFIG['anonymous_access'] + skip_before_filter :authenticate_user!, :only => [:show, :platforms_for_build, :members] if APP_CONFIG['anonymous_access'] load_and_authorize_resource @@ -28,11 +28,13 @@ class Api::V1::PlatformsController < Api::V1::BaseController platform_params[:description] = p[:description] if p[:description] if @platform.update_attributes(p) render :json => { - :platform => {:id => @platform.id, - :message => 'Platform has been updated successfully'} + :platform => { + :id => @platform.id, + :message => 'Platform has been updated successfully' + } }.to_json else - render :json => {:message => "Validation Failed", :errors => @platform.errors.messages}.to_json, :status => 422 + render :json => validation_failed(@platform), :status => 422 end end @@ -40,4 +42,23 @@ class Api::V1::PlatformsController < Api::V1::BaseController @members = @platform.members.order('name').paginate(paginate_params) end + def add_member + if params[:type] == 'User' + member = User + elsif params[:type] == 'Group' + member = Group + end + member = member.where(:id => params[:member_id]).first if member + if member && @platform.add_member(member) + render :json => { + :platform => { + :id => @platform.id, + :message => "#{member.class.to_s} '#{member.id}' has been added to platform successfully" + } + }.to_json + else + render :json => validation_failed(@platform), :status => 422 + end + end + end diff --git a/config/routes.rb b/config/routes.rb index 3c1ea38fe..e9fe3826b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,6 +26,7 @@ Rosa::Application.routes.draw do } member { get :members + put :add_member } end resources :repositories, :only => [:show] diff --git a/spec/controllers/api/v1/platforms_controller_spec.rb b/spec/controllers/api/v1/platforms_controller_spec.rb index ce565c7eb..8e9651f6c 100644 --- a/spec/controllers/api/v1/platforms_controller_spec.rb +++ b/spec/controllers/api/v1/platforms_controller_spec.rb @@ -8,7 +8,34 @@ shared_examples_for 'api platform user with reader rights' do get :index, :format => :json response.should render_template(:index) end +end +shared_examples_for 'api platform user with update rights' do + before do + put :update, {:platform => {:description => 'new description'}, :id => @platform.id}, :format => :json + end + + it 'should be able to perform update action' do + response.should be_success + end + it 'ensures that platform has been updated' do + @platform.reload + @platform.description.should == 'new description' + end +end + +shared_examples_for 'api platform user without update rights' do + before do + put :update, {:platform => {:description => 'new description'}, :id => @platform.id}, :format => :json + end + + it 'should not be able to perform update action' do + response.should_not be_success + end + it 'ensures that platform has not been updated' do + @platform.reload + @platform.description.should_not == 'new description' + end end shared_examples_for 'api platform user with reader rights for hidden platform' do @@ -32,12 +59,19 @@ shared_examples_for "api platform user with show rights" do get :show, :id => @platform.id, :format => :json response.should render_template(:show) end + + it 'should be able to perform platforms_for_build action' do + get :platforms_for_build, :format => :json + response.should render_template(:index) + end end shared_examples_for "api platform user without show rights" do - it 'should not be able to perform show action' do - get :show, :id => @platform.id, :format => :json - response.body.should == {"message" => "Access violation to this page!"}.to_json + [:show, :members].each do |action| + it "should not be able to perform #{ action } action" do + get action, :id => @platform.id, :format => :json + response.body.should == {"message" => "Access violation to this page!"}.to_json + end end end @@ -45,7 +79,7 @@ describe Api::V1::PlatformsController do before do stub_symlink_methods - @platform = FactoryGirl.create(:platform) + @platform = FactoryGirl.create(:platform, :visibility => 'open') @personal_platform = FactoryGirl.create(:platform, :platform_type => 'personal') @user = FactoryGirl.create(:user) end @@ -57,14 +91,21 @@ describe Api::V1::PlatformsController do response.status.should == 401 end - it "should not be able to perform show action", :anonymous_access => false do - get :show, :id => @platform.id, :format => :json - response.status.should == 401 + [:show, :platforms_for_build].each do |action| + it "should not be able to perform #{ action } action", :anonymous_access => false do + get action, :format => :json + response.status.should == 401 + end + end + + it 'should be able to perform members action', :anonymous_access => true do + get :members, :id => @platform.id, :format => :json + response.should render_template(:members) end it_should_behave_like 'api platform user with show rights' if APP_CONFIG['anonymous_access'] it_should_behave_like 'api platform user without reader rights for hidden platform' if APP_CONFIG['anonymous_access'] - + it_should_behave_like 'api platform user without update rights' end context 'for global admin' do @@ -76,6 +117,7 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights' it_should_behave_like 'api platform user with reader rights for hidden platform' + it_should_behave_like 'api platform user with update rights' end context 'for owner user' do @@ -88,6 +130,7 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights' it_should_behave_like 'api platform user with reader rights for hidden platform' + it_should_behave_like 'api platform user with update rights' end context 'for reader user' do @@ -111,6 +154,7 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights' it_should_behave_like 'api platform user with reader rights for hidden platform' + it_should_behave_like 'api platform user without update rights' end context 'for simple user' do @@ -121,5 +165,6 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights' it_should_behave_like 'api platform user without reader rights for hidden platform' + it_should_behave_like 'api platform user without update rights' end end From 109afad259ab775cfbc2dddd38f67619aeb067dd Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 9 Oct 2012 20:51:41 +0400 Subject: [PATCH 10/39] #671: added specs for API --- .../api/v1/platforms_controller_spec.rb | 92 +++++++++++++------ 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/spec/controllers/api/v1/platforms_controller_spec.rb b/spec/controllers/api/v1/platforms_controller_spec.rb index 8e9651f6c..1af389736 100644 --- a/spec/controllers/api/v1/platforms_controller_spec.rb +++ b/spec/controllers/api/v1/platforms_controller_spec.rb @@ -8,33 +8,73 @@ shared_examples_for 'api platform user with reader rights' do get :index, :format => :json response.should render_template(:index) end -end -shared_examples_for 'api platform user with update rights' do - before do - put :update, {:platform => {:description => 'new description'}, :id => @platform.id}, :format => :json - end - - it 'should be able to perform update action' do - response.should be_success - end - it 'ensures that platform has been updated' do - @platform.reload - @platform.description.should == 'new description' + it 'should be able to perform members action' do + get :members, :id => @platform.id, :format => :json + response.should render_template(:members) end end -shared_examples_for 'api platform user without update rights' do - before do - put :update, {:platform => {:description => 'new description'}, :id => @platform.id}, :format => :json +shared_examples_for 'api platform user with writer rights' do + + context 'api platform user with update rights' do + before do + put :update, {:platform => {:description => 'new description'}, :id => @platform.id}, :format => :json + end + + it 'should be able to perform update action' do + response.should be_success + end + it 'ensures that platform has been updated' do + @platform.reload + @platform.description.should == 'new description' + end end - it 'should not be able to perform update action' do - response.should_not be_success + context 'api platform user with add_member rights' do + let(:member) { FactoryGirl.create(:user) } + before do + put :add_member, {:member_id => member.id, :type => 'User', :id => @platform.id}, :format => :json + end + + it 'should be able to perform update action' do + response.should be_success + end + it 'ensures that new member has been added to platform' do + @platform.members.should include(member) + end end - it 'ensures that platform has not been updated' do - @platform.reload - @platform.description.should_not == 'new description' + +end + +shared_examples_for 'api platform user without writer rights' do + + context 'api platform user without update rights' do + before do + put :update, {:platform => {:description => 'new description'}, :id => @platform.id}, :format => :json + end + + it 'should not be able to perform update action' do + response.should_not be_success + end + it 'ensures that platform has not been updated' do + @platform.reload + @platform.description.should_not == 'new description' + end + end + + context 'api platform user without add_member rights' do + let(:member) { FactoryGirl.create(:user) } + before do + put :add_member, {:member_id => member.id, :type => 'User', :id => @platform.id}, :format => :json + end + + it 'should not be able to perform update action' do + response.should_not be_success + end + it 'ensures that new member has not been added to platform' do + @platform.members.should_not include(member) + end end end @@ -98,14 +138,14 @@ describe Api::V1::PlatformsController do end end - it 'should be able to perform members action', :anonymous_access => true do + it 'should be able to perform members action' do get :members, :id => @platform.id, :format => :json response.should render_template(:members) end it_should_behave_like 'api platform user with show rights' if APP_CONFIG['anonymous_access'] it_should_behave_like 'api platform user without reader rights for hidden platform' if APP_CONFIG['anonymous_access'] - it_should_behave_like 'api platform user without update rights' + it_should_behave_like 'api platform user without writer rights' end context 'for global admin' do @@ -117,7 +157,7 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights' it_should_behave_like 'api platform user with reader rights for hidden platform' - it_should_behave_like 'api platform user with update rights' + it_should_behave_like 'api platform user with writer rights' end context 'for owner user' do @@ -130,7 +170,7 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights' it_should_behave_like 'api platform user with reader rights for hidden platform' - it_should_behave_like 'api platform user with update rights' + it_should_behave_like 'api platform user with writer rights' end context 'for reader user' do @@ -154,7 +194,7 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights' it_should_behave_like 'api platform user with reader rights for hidden platform' - it_should_behave_like 'api platform user without update rights' + it_should_behave_like 'api platform user without writer rights' end context 'for simple user' do @@ -165,6 +205,6 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights' it_should_behave_like 'api platform user without reader rights for hidden platform' - it_should_behave_like 'api platform user without update rights' + it_should_behave_like 'api platform user without writer rights' end end From e5c6707594f5e9ab7bec1c7d8f1435b6af1ce744 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 9 Oct 2012 21:07:10 +0400 Subject: [PATCH 11/39] #671: added #remove_member action for platforms API, updated specs --- .../api/v1/platforms_controller.rb | 34 ++++++++++++++---- config/routes.rb | 1 + .../api/v1/platforms_controller_spec.rb | 35 +++++++++++++++++-- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index 630395a07..a676ff89e 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -43,13 +43,7 @@ class Api::V1::PlatformsController < Api::V1::BaseController end def add_member - if params[:type] == 'User' - member = User - elsif params[:type] == 'Group' - member = Group - end - member = member.where(:id => params[:member_id]).first if member - if member && @platform.add_member(member) + if member.present? && @platform.add_member(member) render :json => { :platform => { :id => @platform.id, @@ -61,4 +55,30 @@ class Api::V1::PlatformsController < Api::V1::BaseController end end + def remove_member + if member.present? && @platform.remove_member(member) + render :json => { + :platform => { + :id => @platform.id, + :message => "#{member.class.to_s} '#{member.id}' has been removed from platform successfully" + } + }.to_json + else + render :json => validation_failed(@platform), :status => 422 + end + end + + private + + def member + return @member if @member + if params[:type] == 'User' + member = User + elsif params[:type] == 'Group' + member = Group + end + @member = member.where(:id => params[:member_id]).first if member + @member ||= '' + end + end diff --git a/config/routes.rb b/config/routes.rb index e9fe3826b..c87672d5e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -27,6 +27,7 @@ Rosa::Application.routes.draw do member { get :members put :add_member + delete :remove_member } end resources :repositories, :only => [:show] diff --git a/spec/controllers/api/v1/platforms_controller_spec.rb b/spec/controllers/api/v1/platforms_controller_spec.rb index 1af389736..24d6d358d 100644 --- a/spec/controllers/api/v1/platforms_controller_spec.rb +++ b/spec/controllers/api/v1/platforms_controller_spec.rb @@ -37,7 +37,7 @@ shared_examples_for 'api platform user with writer rights' do put :add_member, {:member_id => member.id, :type => 'User', :id => @platform.id}, :format => :json end - it 'should be able to perform update action' do + it 'should be able to perform add_member action' do response.should be_success end it 'ensures that new member has been added to platform' do @@ -45,6 +45,21 @@ shared_examples_for 'api platform user with writer rights' do end end + context 'api platform user with remove_member rights' do + let(:member) { FactoryGirl.create(:user) } + before do + @platform.add_member(member) + delete :remove_member, {:member_id => member.id, :type => 'User', :id => @platform.id}, :format => :json + end + + it 'should be able to perform update action' do + response.should be_success + end + it 'ensures that member has been removed from platform' do + @platform.members.should_not include(member) + end + end + end shared_examples_for 'api platform user without writer rights' do @@ -69,13 +84,29 @@ shared_examples_for 'api platform user without writer rights' do put :add_member, {:member_id => member.id, :type => 'User', :id => @platform.id}, :format => :json end - it 'should not be able to perform update action' do + it 'should not be able to perform add_member action' do response.should_not be_success end it 'ensures that new member has not been added to platform' do @platform.members.should_not include(member) end end + + context 'api platform user without remove_member rights' do + let(:member) { FactoryGirl.create(:user) } + before do + @platform.add_member(member) + delete :remove_member, {:member_id => member.id, :type => 'User', :id => @platform.id}, :format => :json + end + + it 'should be able to perform update action' do + response.should_not be_success + end + it 'ensures that member has not been removed from platform' do + @platform.members.should include(member) + end + end + end shared_examples_for 'api platform user with reader rights for hidden platform' do From e6bb63db0825a72a8054fdccce51bfe7c9e7bc61 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 10 Oct 2012 16:02:59 +0400 Subject: [PATCH 12/39] #671: removed restrict_paginate method --- app/controllers/api/v1/arches_controller.rb | 2 +- app/controllers/api/v1/base_controller.rb | 11 ++++------- app/controllers/api/v1/build_lists_controller.rb | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/v1/arches_controller.rb b/app/controllers/api/v1/arches_controller.rb index 2b2e164bd..641869742 100644 --- a/app/controllers/api/v1/arches_controller.rb +++ b/app/controllers/api/v1/arches_controller.rb @@ -3,7 +3,7 @@ class Api::V1::ArchesController < Api::V1::BaseController before_filter :authenticate_user! unless APP_CONFIG['anonymous_access'] def index - @arches = Arch.order(:id).all + @arches = Arch.order(:id).paginate(paginate_params) end end \ No newline at end of file diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index bfee062a8..f27fc4dfe 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -1,7 +1,6 @@ # -*- encoding : utf-8 -*- class Api::V1::BaseController < ApplicationController - before_filter :restrict_paginate, :only => :index #respond_to :json rescue_from CanCan::AccessDenied do |exception| @@ -12,13 +11,11 @@ class Api::V1::BaseController < ApplicationController protected - def restrict_paginate - params[:per_page] = 30 if params[:per_page].to_i < 1 - params[:per_page] = 100 if params[:per_page].to_i >100 - end - def paginate_params - {:page => params[:page], :per_page => 20} + per_page = params[:per_page].to_i + per_page = 20 if per_page < 1 + per_page = 100 if per_page >100 + {:page => params[:page], :per_page => per_page} end def validation_failed(subject) diff --git a/app/controllers/api/v1/build_lists_controller.rb b/app/controllers/api/v1/build_lists_controller.rb index 04e1e4bdd..59558f3e0 100644 --- a/app/controllers/api/v1/build_lists_controller.rb +++ b/app/controllers/api/v1/build_lists_controller.rb @@ -10,7 +10,7 @@ class Api::V1::BuildListsController < Api::V1::BaseController def index filter = BuildList::Filter.new(nil, current_user, params[:filter] || {}) @build_lists = filter.find.scoped(:include => [:save_to_platform, :project, :user, :arch]) - @build_lists = @build_lists.recent.paginate :page => params[:page], :per_page => params[:per_page] + @build_lists = @build_lists.recent.paginate(paginate_params) end def create From d9c583e22659b29a5e11b564b16eb78a877c88c7 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 10 Oct 2012 19:13:14 +0400 Subject: [PATCH 13/39] #671: added specs for API Platforms#clone action --- app/controllers/api/v1/base_controller.rb | 4 -- .../api/v1/platforms_controller.rb | 51 +++++++++++-------- config/routes.rb | 2 + .../api/v1/platforms_controller_spec.rb | 28 +++++++++- 4 files changed, 59 insertions(+), 26 deletions(-) diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index f27fc4dfe..5e0a6c780 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -18,8 +18,4 @@ class Api::V1::BaseController < ApplicationController {:page => params[:page], :per_page => per_page} end - def validation_failed(subject) - {:message => "Validation Failed", :errors => subject.errors.messages}.to_json - end - end diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index a676ff89e..e416ad783 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -27,14 +27,9 @@ class Api::V1::PlatformsController < Api::V1::BaseController platform_params[:released] = p[:released] if p[:released] platform_params[:description] = p[:description] if p[:description] if @platform.update_attributes(p) - render :json => { - :platform => { - :id => @platform.id, - :message => 'Platform has been updated successfully' - } - }.to_json + render :json => json_response('Platform has been updated successfully') else - render :json => validation_failed(@platform), :status => 422 + render :json => json_response('Platform has not been updated', true), :status => 422 end end @@ -44,32 +39,46 @@ class Api::V1::PlatformsController < Api::V1::BaseController def add_member if member.present? && @platform.add_member(member) - render :json => { - :platform => { - :id => @platform.id, - :message => "#{member.class.to_s} '#{member.id}' has been added to platform successfully" - } - }.to_json + render :json => json_response("#{member.class.to_s} '#{member.id}' has been added to platform successfully") else - render :json => validation_failed(@platform), :status => 422 + render :json => json_response('Member has not been added to platform', true), :status => 422 end end def remove_member if member.present? && @platform.remove_member(member) - render :json => { - :platform => { - :id => @platform.id, - :message => "#{member.class.to_s} '#{member.id}' has been removed from platform successfully" - } - }.to_json + render :json => json_response("#{member.class.to_s} '#{member.id}' has been removed from platform successfully") else - render :json => validation_failed(@platform), :status => 422 + render :json => json_response('Member has not been removed from platform'), :status => 422 end end + def clone + p = params[:platform] || {} + platform_params = {} + platform_params[:description] = p[:description] if p[:description] + platform_params[:name] = p[:name] if p[:name] + platform_params[:owner] = current_user + @cloned = @platform.full_clone(platform_params) + if @cloned.persisted? + render :json => json_response('Platform has been cloned successfully') + else + render :json => json_response('Platform has not been cloned', true), :status => 422 + end + end + + def clear + @platform.clear + render :json => json_response('Platform has been cleared successfully') + end + private + def json_response(message, nullify_id = false) + id = nullify_id ? nil : @platform.id + { :platform => {:id => id, :message => message} }.to_json + end + def member return @member if @member if params[:type] == 'User' diff --git a/config/routes.rb b/config/routes.rb index c87672d5e..2a9b5b461 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -28,6 +28,8 @@ Rosa::Application.routes.draw do get :members put :add_member delete :remove_member + post :clone + put :clear } end resources :repositories, :only => [:show] diff --git a/spec/controllers/api/v1/platforms_controller_spec.rb b/spec/controllers/api/v1/platforms_controller_spec.rb index 24d6d358d..571d72c8c 100644 --- a/spec/controllers/api/v1/platforms_controller_spec.rb +++ b/spec/controllers/api/v1/platforms_controller_spec.rb @@ -59,7 +59,6 @@ shared_examples_for 'api platform user with writer rights' do @platform.members.should_not include(member) end end - end shared_examples_for 'api platform user without writer rights' do @@ -107,6 +106,31 @@ shared_examples_for 'api platform user without writer rights' do end end + it_should_behave_like 'api platform user without clone rights' +end + +shared_examples_for 'api platform user with clone rights' do + before { any_instance_of(Platform, :create_directory => true) } + let(:params) { {:id => @platform.id, :platform => {:description => 'new description', :name => 'new_name'}} } + it 'should be able to perform clone action' do + post :clone, params, :format => :json + response.should be_success + end + it 'ensures that platform has been cloned' do + lambda { post :clone, params, :format => :json }.should change{ Platform.count }.by(1) + end +end + +shared_examples_for 'api platform user without clone rights' do + before { any_instance_of(Platform, :create_directory => true) } + let(:params) { {:id => @platform.id, :platform => {:description => 'new description', :name => 'new_name'}} } + it 'should not be able to perform clone action' do + post :clone, params, :format => :json + response.should_not be_success + end + it 'ensures that platform has not been cloned' do + lambda { post :clone, params, :format => :json }.should_not change{ Platform.count } + end end shared_examples_for 'api platform user with reader rights for hidden platform' do @@ -189,6 +213,7 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights' it_should_behave_like 'api platform user with reader rights for hidden platform' it_should_behave_like 'api platform user with writer rights' + it_should_behave_like 'api platform user with clone rights' end context 'for owner user' do @@ -202,6 +227,7 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights' it_should_behave_like 'api platform user with reader rights for hidden platform' it_should_behave_like 'api platform user with writer rights' + it_should_behave_like 'api platform user without clone rights' end context 'for reader user' do From d0bb3eebe6601ab73e31834531791d2388afd9ad Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 10 Oct 2012 19:27:53 +0400 Subject: [PATCH 14/39] #671: added specs for API Platforms#clear action --- .../api/v1/platforms_controller_spec.rb | 52 +++++++++++++++---- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/spec/controllers/api/v1/platforms_controller_spec.rb b/spec/controllers/api/v1/platforms_controller_spec.rb index 571d72c8c..0e8a9e717 100644 --- a/spec/controllers/api/v1/platforms_controller_spec.rb +++ b/spec/controllers/api/v1/platforms_controller_spec.rb @@ -106,18 +106,29 @@ shared_examples_for 'api platform user without writer rights' do end end + context 'should not be able to perform clear action' do + it 'for personal platform' do + put :clear, :id => @personal_platform.id, :format => :json + response.should_not be_success + end + it 'for main platform' do + put :clear, :id => @platform.id, :format => :json + response.should_not be_success + end + end + it_should_behave_like 'api platform user without clone rights' + it_should_behave_like 'api platform user without clear rights' end -shared_examples_for 'api platform user with clone rights' do - before { any_instance_of(Platform, :create_directory => true) } - let(:params) { {:id => @platform.id, :platform => {:description => 'new description', :name => 'new_name'}} } - it 'should be able to perform clone action' do - post :clone, params, :format => :json - response.should be_success +shared_examples_for 'api platform user without clear rights' do + it 'for personal platform' do + put :clear, :id => @personal_platform.id, :format => :json + response.should_not be_success end - it 'ensures that platform has been cloned' do - lambda { post :clone, params, :format => :json }.should change{ Platform.count }.by(1) + it 'for main platform' do + put :clear, :id => @platform.id, :format => :json + response.should_not be_success end end @@ -213,7 +224,29 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights' it_should_behave_like 'api platform user with reader rights for hidden platform' it_should_behave_like 'api platform user with writer rights' - it_should_behave_like 'api platform user with clone rights' + + context 'with clone rights' do + before { any_instance_of(Platform, :create_directory => true) } + let(:params) { {:id => @platform.id, :platform => {:description => 'new description', :name => 'new_name'}} } + it 'should be able to perform clone action' do + post :clone, params, :format => :json + response.should be_success + end + it 'ensures that platform has been cloned' do + lambda { post :clone, params, :format => :json }.should change{ Platform.count }.by(1) + end + end + + context 'with clear rights for personal platform' do + it 'personal platform should be cleared successfully' do + put :clear, :id => @personal_platform.id, :format => :json + response.should be_success + end + it 'main platform should not be cleared' do + put :clear, :id => @platform.id, :format => :json + response.should_not be_success + end + end end context 'for owner user' do @@ -228,6 +261,7 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights for hidden platform' it_should_behave_like 'api platform user with writer rights' it_should_behave_like 'api platform user without clone rights' + it_should_behave_like 'api platform user without clear rights' end context 'for reader user' do From 972877bc76e2801f4754bf7e69974e09fe17a7f8 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 10 Oct 2012 19:43:14 +0400 Subject: [PATCH 15/39] #671: added API Platforms#destroy action and specs --- .../api/v1/platforms_controller.rb | 5 +++ config/routes.rb | 2 +- .../api/v1/platforms_controller_spec.rb | 34 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index e416ad783..bb25df24f 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -72,6 +72,11 @@ class Api::V1::PlatformsController < Api::V1::BaseController render :json => json_response('Platform has been cleared successfully') end + def destroy + @platform.destroy # later with resque + render :json => json_response('Platform has been destroyed successfully') + end + private def json_response(message, nullify_id = false) diff --git a/config/routes.rb b/config/routes.rb index 2a9b5b461..d6169fc78 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,7 +20,7 @@ Rosa::Application.routes.draw do } end resources :arches, :only => [:index] - resources :platforms, :only => [:index, :show, :update] do + resources :platforms, :only => [:index, :show, :update, :destroy] do collection { get :platforms_for_build } diff --git a/spec/controllers/api/v1/platforms_controller_spec.rb b/spec/controllers/api/v1/platforms_controller_spec.rb index 0e8a9e717..90a69727d 100644 --- a/spec/controllers/api/v1/platforms_controller_spec.rb +++ b/spec/controllers/api/v1/platforms_controller_spec.rb @@ -59,6 +59,23 @@ shared_examples_for 'api platform user with writer rights' do @platform.members.should_not include(member) end end + + context 'api platform user with destroy rights for main platforms only' do + it 'should be able to perform destroy action for main platform' do + delete :destroy, :id => @platform.id, :format => :json + response.should be_success + end + it 'ensures that main platform has been destroyed' do + lambda { delete :destroy, :id => @platform.id, :format => :json }.should change{ Platform.count }.by(-1) + end + it 'should not be able to perform destroy action for personal platform' do + delete :destroy, :id => @personal_platform.id, :format => :json + response.should_not be_success + end + it 'ensures that personal platform has not been destroyed' do + lambda { delete :destroy, :id => @personal_platform.id, :format => :json }.should_not change{ Platform.count } + end + end end shared_examples_for 'api platform user without writer rights' do @@ -117,6 +134,23 @@ shared_examples_for 'api platform user without writer rights' do end end + context 'api platform user without destroy rights' do + it 'should not be able to perform destroy action for main platform' do + delete :destroy, :id => @platform.id, :format => :json + response.should_not be_success + end + it 'ensures that main platform has not been destroyed' do + lambda { delete :destroy, :id => @platform.id, :format => :json }.should_not change{ Platform.count } + end + it 'should not be able to perform destroy action for personal platform' do + delete :destroy, :id => @personal_platform.id, :format => :json + response.should_not be_success + end + it 'ensures that personal platform has not been destroyed' do + lambda { delete :destroy, :id => @personal_platform.id, :format => :json }.should_not change{ Platform.count } + end + end + it_should_behave_like 'api platform user without clone rights' it_should_behave_like 'api platform user without clear rights' end From 0841c4d705e77d118a3e93b7054418f570eb9d3b Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 10 Oct 2012 20:19:00 +0400 Subject: [PATCH 16/39] added 400 error for API BuildLists#create action --- .../api/v1/build_lists_controller.rb | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/app/controllers/api/v1/build_lists_controller.rb b/app/controllers/api/v1/build_lists_controller.rb index 04e1e4bdd..4a783de8b 100644 --- a/app/controllers/api/v1/build_lists_controller.rb +++ b/app/controllers/api/v1/build_lists_controller.rb @@ -14,21 +14,27 @@ class Api::V1::BuildListsController < Api::V1::BaseController end def create - project = Project.find(params[:build_list][:project_id]) - save_to_repository = Repository.find params[:build_list][:save_to_repository_id] #FIXME - params[:build_list][:save_to_platform_id] = save_to_repository.platform_id - params[:build_list][:auto_publish] = false unless save_to_repository.publish_without_qa? + bl_params = params[:build_list] || {} + project = Project.where(:id => bl_params[:project_id]).first + save_to_repository = Repository.where(:id => bl_params[:save_to_repository_id]).first - @build_list = project.build_lists.build(params[:build_list]) - @build_list.project_version = @build_list.commit_hash + if project && save_to_repository + bl_params[:save_to_platform_id] = save_to_repository.platform_id + bl_params[:auto_publish] = false unless save_to_repository.publish_without_qa? - @build_list.user = current_user - @build_list.priority = current_user.build_priority # User builds more priority than mass rebuild with zero priority + @build_list = project.build_lists.build(bl_params) + @build_list.project_version = @build_list.commit_hash - if @build_list.save - render :action => 'show' + @build_list.user = current_user + @build_list.priority = current_user.build_priority # User builds more priority than mass rebuild with zero priority + + if @build_list.save + render :action => 'show' + else + render :json => {:message => "Validation Failed", :errors => @build_list.errors.messages}.to_json, :status => 422 + end else - render :json => {:message => "Validation Failed", :errors => @build_list.errors.messages}.to_json, :status => 422 + render :json => {:message => "Bad Request"}.to_json, :status => 400 end end From 919459617a93319236296a109d853579c5c8703b Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 10 Oct 2012 20:31:22 +0400 Subject: [PATCH 17/39] #671: small refactoring --- app/controllers/api/v1/platforms_controller.rb | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index bb25df24f..74a49fea0 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -20,13 +20,10 @@ class Api::V1::PlatformsController < Api::V1::BaseController end def update - p = params[:platform] || {} - owner = User.where(:id => p[:owner_id]).first - platform_params = {} - platform_params[:owner] = owner if owner - platform_params[:released] = p[:released] if p[:released] - platform_params[:description] = p[:description] if p[:description] - if @platform.update_attributes(p) + platform_params = params[:platform] || {} + owner = User.where(:id => platform_params[:owner_id]).first + platform_params[:owner] = owner if owner + if @platform.update_attributes(platform_params) render :json => json_response('Platform has been updated successfully') else render :json => json_response('Platform has not been updated', true), :status => 422 @@ -54,11 +51,8 @@ class Api::V1::PlatformsController < Api::V1::BaseController end def clone - p = params[:platform] || {} - platform_params = {} - platform_params[:description] = p[:description] if p[:description] - platform_params[:name] = p[:name] if p[:name] - platform_params[:owner] = current_user + platform_params = params[:platform] || {} + platform_params[:owner] = current_user @cloned = @platform.full_clone(platform_params) if @cloned.persisted? render :json => json_response('Platform has been cloned successfully') From eef640e82c52207b21bdd9895daed883e252393f Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 10 Oct 2012 21:45:56 +0400 Subject: [PATCH 18/39] #671: added API Platforms#create action, updated specs, added validation to platform model --- .../api/v1/platforms_controller.rb | 11 +++ app/models/platform.rb | 5 ++ config/locales/models/platform.en.yml | 1 + config/locales/models/platform.ru.yml | 1 + config/routes.rb | 2 +- .../api/v1/platforms_controller_spec.rb | 82 +++++++++---------- 6 files changed, 56 insertions(+), 46 deletions(-) diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index 74a49fea0..b68c0755b 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -19,6 +19,17 @@ class Api::V1::PlatformsController < Api::V1::BaseController render :index end + def create + platform_params = params[:platform] || {} + owner = User.where(:id => platform_params[:owner_id]).first + @platform.owner = owner || get_owner + if @platform.save + render :json => json_response('Platform has been created successfully') + else + render :json => json_response('Platform has not been created', true), :status => 422 + end + end + def update platform_params = params[:platform] || {} owner = User.where(:id => platform_params[:owner_id]).first diff --git a/app/models/platform.rb b/app/models/platform.rb index e43db852b..802a2461a 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -24,6 +24,11 @@ class Platform < ActiveRecord::Base validates :visibility, :presence => true, :inclusion => {:in => VISIBILITIES} validates :name, :uniqueness => {:case_sensitive => false}, :presence => true, :format => { :with => /^[a-zA-Z0-9_\-\.]+$/ } validates :distrib_type, :presence => true, :inclusion => {:in => APP_CONFIG['distr_types']} + validate lambda { + if released_was && !released + errors.add(:released, I18n.t('flash.platform.released_status_can_not_be_changed')) + end + } before_create :create_directory, :if => lambda {Thread.current[:skip]} # TODO remove this when core will be ready before_create :xml_rpc_create, :unless => lambda {Thread.current[:skip]} diff --git a/config/locales/models/platform.en.yml b/config/locales/models/platform.en.yml index 9a828c31c..afb83a510 100644 --- a/config/locales/models/platform.en.yml +++ b/config/locales/models/platform.en.yml @@ -49,6 +49,7 @@ en: flash: platform: + released_status_can_not_be_changed: Released status can't be changed if platform has been released saved: Platform saved created: Platform created save_error: Platform save error diff --git a/config/locales/models/platform.ru.yml b/config/locales/models/platform.ru.yml index a71e49bdc..a379f293b 100644 --- a/config/locales/models/platform.ru.yml +++ b/config/locales/models/platform.ru.yml @@ -49,6 +49,7 @@ ru: flash: platform: + released_status_can_not_be_changed: Released статус платформы не может быть изменен, если платформа уже выпущена saved: Платформа успешно сохранена created: Платформа успешно добавлена save_error: Не удалось сохранить платформу diff --git a/config/routes.rb b/config/routes.rb index d6169fc78..03034a36e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,7 +20,7 @@ Rosa::Application.routes.draw do } end resources :arches, :only => [:index] - resources :platforms, :only => [:index, :show, :update, :destroy] do + resources :platforms, :only => [:index, :show, :update, :destroy, :create] do collection { get :platforms_for_build } diff --git a/spec/controllers/api/v1/platforms_controller_spec.rb b/spec/controllers/api/v1/platforms_controller_spec.rb index 90a69727d..4638a32af 100644 --- a/spec/controllers/api/v1/platforms_controller_spec.rb +++ b/spec/controllers/api/v1/platforms_controller_spec.rb @@ -151,30 +151,32 @@ shared_examples_for 'api platform user without writer rights' do end end - it_should_behave_like 'api platform user without clone rights' - it_should_behave_like 'api platform user without clear rights' + it_should_behave_like 'api platform user without global admin rights' end -shared_examples_for 'api platform user without clear rights' do - it 'for personal platform' do - put :clear, :id => @personal_platform.id, :format => :json - response.should_not be_success +shared_examples_for 'api platform user without global admin rights' do + context 'should not be able to perform clear action' do + it 'for personal platform' do + put :clear, :id => @personal_platform.id, :format => :json + response.should_not be_success + end + it 'for main platform' do + put :clear, :id => @platform.id, :format => :json + response.should_not be_success + end end - it 'for main platform' do - put :clear, :id => @platform.id, :format => :json - response.should_not be_success - end -end -shared_examples_for 'api platform user without clone rights' do - before { any_instance_of(Platform, :create_directory => true) } - let(:params) { {:id => @platform.id, :platform => {:description => 'new description', :name => 'new_name'}} } - it 'should not be able to perform clone action' do - post :clone, params, :format => :json - response.should_not be_success - end - it 'ensures that platform has not been cloned' do - lambda { post :clone, params, :format => :json }.should_not change{ Platform.count } + [:create, :clone].each do |action| + context "api platform user without #{action} rights" do + before { any_instance_of(Platform, :create_directory => true) } + it "should not be able to perform #{action} action" do + post action, clone_or_create_params, :format => :json + response.should_not be_success + end + it "ensures that platform has not been #{action}d" do + lambda { post action, clone_or_create_params, :format => :json }.should_not change{ Platform.count } + end + end end end @@ -216,6 +218,7 @@ shared_examples_for "api platform user without show rights" do end describe Api::V1::PlatformsController do + let(:clone_or_create_params) { {:id => @platform.id, :platform => {:description => 'new description', :name => 'new_name', :owner_id => @user.id, :distrib_type => APP_CONFIG['distr_types'].first}} } before do stub_symlink_methods @@ -251,7 +254,6 @@ describe Api::V1::PlatformsController do context 'for global admin' do before do @admin = FactoryGirl.create(:admin) - @user = FactoryGirl.create(:user) http_login(@admin) end @@ -259,33 +261,26 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights for hidden platform' it_should_behave_like 'api platform user with writer rights' - context 'with clone rights' do - before { any_instance_of(Platform, :create_directory => true) } - let(:params) { {:id => @platform.id, :platform => {:description => 'new description', :name => 'new_name'}} } - it 'should be able to perform clone action' do - post :clone, params, :format => :json - response.should be_success - end - it 'ensures that platform has been cloned' do - lambda { post :clone, params, :format => :json }.should change{ Platform.count }.by(1) + [:clone, :create].each do |action| + context "with #{action} rights" do + before do + any_instance_of(Platform, :create_directory => true) + clone_or_create_params[:platform][:owner_id] = @admin.id + end + it "should be able to perform #{action} action" do + post action, clone_or_create_params, :format => :json + response.should be_success + end + it "ensures that platform has been #{action}d" do + lambda { post action, clone_or_create_params, :format => :json }.should change{ Platform.count }.by(1) + end end end - context 'with clear rights for personal platform' do - it 'personal platform should be cleared successfully' do - put :clear, :id => @personal_platform.id, :format => :json - response.should be_success - end - it 'main platform should not be cleared' do - put :clear, :id => @platform.id, :format => :json - response.should_not be_success - end - end end context 'for owner user' do before do - @user = FactoryGirl.create(:user) http_login(@user) @platform.owner = @user; @platform.save @platform.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'admin') @@ -294,13 +289,11 @@ describe Api::V1::PlatformsController do it_should_behave_like 'api platform user with reader rights' it_should_behave_like 'api platform user with reader rights for hidden platform' it_should_behave_like 'api platform user with writer rights' - it_should_behave_like 'api platform user without clone rights' - it_should_behave_like 'api platform user without clear rights' + it_should_behave_like 'api platform user without global admin rights' end context 'for reader user' do before do - @user = FactoryGirl.create(:user) http_login(@user) @platform.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'reader') @personal_platform.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'reader') @@ -324,7 +317,6 @@ describe Api::V1::PlatformsController do context 'for simple user' do before do - @user = FactoryGirl.create(:user) http_login(@user) end From 05d222e9def5342fd846d8d782771e5e6210c131 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 10 Oct 2012 21:51:00 +0400 Subject: [PATCH 19/39] #671: updated output errors --- app/controllers/api/v1/platforms_controller.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index b68c0755b..2a2ecec78 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -85,7 +85,15 @@ class Api::V1::PlatformsController < Api::V1::BaseController private def json_response(message, nullify_id = false) - id = nullify_id ? nil : @platform.id + if nullify_id + errors = @platform.errors.full_messages.join('. ') + if errors.present? + message << '. ' << errors + end + id = nil + else + id = @platform.id + end { :platform => {:id => id, :message => message} }.to_json end From e379a603b480a9dd56e2b5d26b656ff6d3a9d2da Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Thu, 11 Oct 2012 00:59:25 +0600 Subject: [PATCH 20/39] [refs #90] get new pull to this project --- app/controllers/projects/pull_requests_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/projects/pull_requests_controller.rb b/app/controllers/projects/pull_requests_controller.rb index 478e230f5..6a8cc030d 100644 --- a/app/controllers/projects/pull_requests_controller.rb +++ b/app/controllers/projects/pull_requests_controller.rb @@ -144,7 +144,7 @@ class Projects::PullRequestsController < Projects::BaseController args = params[:to_project].try(:split, '/') || [] project = (args.length == 2) ? Project.find_by_owner_and_name(*args) : nil raise ActiveRecord::RecordNotFound if bang && !project - project.try(:parent) || @project + project || @project end def set_attrs From 01b60fc4d72674529391623c5a49410c6e9cfdf7 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Thu, 11 Oct 2012 01:00:21 +0600 Subject: [PATCH 21/39] [refs #90] test to creation pull on the forked project --- .../projects/pull_requests_controller_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/controllers/projects/pull_requests_controller_spec.rb b/spec/controllers/projects/pull_requests_controller_spec.rb index 8f5ce3ed8..f18061209 100644 --- a/spec/controllers/projects/pull_requests_controller_spec.rb +++ b/spec/controllers/projects/pull_requests_controller_spec.rb @@ -74,6 +74,23 @@ shared_examples_for 'pull request user with project reader rights' do post :create, @create_params.merge({:pull_request => {:issue_attributes => {:title => 'already', :body => 'creating'}, :to_ref => 'master', :from_ref => 'master'}, :to_project_id => @project.id}) PullRequest.joins(:issue).where(:issues => {:title => 'already', :body => 'creating'}).count.should == 0 end + + it "should create pull request to the same project" do + @parent = FactoryGirl.create(:project) + @project.update_attributes({:parent_id => @parent}, :without_protection => true) + + lambda{ post :create, @create_params }.should change{ PullRequest.joins(:issue). + where(:issues => {:user_id => @user}, :to_project_id => @project, :from_project_id => @project).count }.by(1) + end + + it "should create pull request to the parent project" do + @parent = FactoryGirl.create(:project) + %x(cp -Rf #{Rails.root}/spec/tests.git/* #{@parent.path}) + @project.update_attributes({:parent_id => @parent}, :without_protection => true) + + lambda{ post :create, @create_params.merge({:to_project => @parent.name_with_owner}) }.should change{ PullRequest.joins(:issue). + where(:issues => {:user_id => @user}, :to_project_id => @parent, :from_project_id => @project).count }.by(1) + end end shared_examples_for 'user with pull request update rights' do From a7bb5f48ba5c9a9774c77c04d897ccb2f2ea0e89 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Thu, 11 Oct 2012 01:16:47 +0600 Subject: [PATCH 22/39] [refs #90] fixed wiki diff --- app/views/projects/wiki/_diff_data.html.haml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/views/projects/wiki/_diff_data.html.haml b/app/views/projects/wiki/_diff_data.html.haml index 1c00ff781..286aed576 100644 --- a/app/views/projects/wiki/_diff_data.html.haml +++ b/app/views/projects/wiki/_diff_data.html.haml @@ -1,4 +1,6 @@ -.blob_header - .size= h(diff.deleted_file ? diff.a_path : diff.b_path) - .clear -.diff_data.highlight= render_diff(diff) \ No newline at end of file +.file + %a{:name => "diff-#{diff_counter}"} + .top + .l= h((diff.deleted_file ? diff.a_path : diff.b_path).rtruncate 100) + .clear + .diff_data.highlight= render_diff(diff, diff_counter) From 992892f2c8896a9d186ea09a18396cd292017f3c Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Thu, 11 Oct 2012 19:18:54 +0400 Subject: [PATCH 23/39] #671: small refactoring --- app/controllers/api/v1/base_controller.rb | 20 ++++++++- .../api/v1/platforms_controller.rb | 43 ++++++++----------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 5e0a6c780..c99ce0692 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -1,6 +1,5 @@ # -*- encoding : utf-8 -*- class Api::V1::BaseController < ApplicationController - #respond_to :json rescue_from CanCan::AccessDenied do |exception| @@ -18,4 +17,23 @@ class Api::V1::BaseController < ApplicationController {:page => params[:page], :per_page => per_page} end + def render_json_response(message, status = 200) + id = status != 200 ? nil : @subject.id + + render :json => { + @subject.class.name.downcase.to_sym => { + :id => id, + :message => message + } + }.to_json, :status => status + end + + def render_validation_error(message) + errors = @subject.errors.full_messages.join('. ') + if errors.present? + message << '. ' << errors + end + render_json_response(message, 422) + end + end diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index 2a2ecec78..c1ad76ece 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -1,10 +1,10 @@ # -*- encoding : utf-8 -*- class Api::V1::PlatformsController < Api::V1::BaseController - before_filter :authenticate_user! skip_before_filter :authenticate_user!, :only => [:show, :platforms_for_build, :members] if APP_CONFIG['anonymous_access'] load_and_authorize_resource + before_filter :set_subject, :except => [:index, :platforms_for_build, :show, :members] def index @platforms = @platforms.accessible_by(current_ability, :related). @@ -24,9 +24,9 @@ class Api::V1::PlatformsController < Api::V1::BaseController owner = User.where(:id => platform_params[:owner_id]).first @platform.owner = owner || get_owner if @platform.save - render :json => json_response('Platform has been created successfully') + render_json_response 'Platform has been created successfully' else - render :json => json_response('Platform has not been created', true), :status => 422 + render_validation_error 'Platform has not been created' end end @@ -35,9 +35,9 @@ class Api::V1::PlatformsController < Api::V1::BaseController owner = User.where(:id => platform_params[:owner_id]).first platform_params[:owner] = owner if owner if @platform.update_attributes(platform_params) - render :json => json_response('Platform has been updated successfully') + render_json_response 'Platform has been updated successfully' else - render :json => json_response('Platform has not been updated', true), :status => 422 + render_validation_error 'Platform has not been updated' end end @@ -47,17 +47,17 @@ class Api::V1::PlatformsController < Api::V1::BaseController def add_member if member.present? && @platform.add_member(member) - render :json => json_response("#{member.class.to_s} '#{member.id}' has been added to platform successfully") + render_json_response "#{member.class.to_s} '#{member.id}' has been added to platform successfully" else - render :json => json_response('Member has not been added to platform', true), :status => 422 + render_validation_error 'Member has not been added to platform' end end def remove_member if member.present? && @platform.remove_member(member) - render :json => json_response("#{member.class.to_s} '#{member.id}' has been removed from platform successfully") + render_json_response "#{member.class.to_s} '#{member.id}' has been removed from platform successfully" else - render :json => json_response('Member has not been removed from platform'), :status => 422 + render_validation_error 'Member has not been removed from platform' end end @@ -66,37 +66,24 @@ class Api::V1::PlatformsController < Api::V1::BaseController platform_params[:owner] = current_user @cloned = @platform.full_clone(platform_params) if @cloned.persisted? - render :json => json_response('Platform has been cloned successfully') + render_json_response 'Platform has been cloned successfully' else - render :json => json_response('Platform has not been cloned', true), :status => 422 + render_validation_error 'Platform has not been cloned' end end def clear @platform.clear - render :json => json_response('Platform has been cleared successfully') + render_json_response 'Platform has been cleared successfully' end def destroy @platform.destroy # later with resque - render :json => json_response('Platform has been destroyed successfully') + render_json_response 'Platform has been destroyed successfully' end private - def json_response(message, nullify_id = false) - if nullify_id - errors = @platform.errors.full_messages.join('. ') - if errors.present? - message << '. ' << errors - end - id = nil - else - id = @platform.id - end - { :platform => {:id => id, :message => message} }.to_json - end - def member return @member if @member if params[:type] == 'User' @@ -108,4 +95,8 @@ class Api::V1::PlatformsController < Api::V1::BaseController @member ||= '' end + def set_subject + @subject = @platform + end + end From 6b3bf41c7ac421af98ed619d958e84d44e7a93c0 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Thu, 11 Oct 2012 19:36:14 +0400 Subject: [PATCH 24/39] #671: removed before_filter --- app/controllers/api/v1/base_controller.rb | 12 ++++---- .../api/v1/platforms_controller.rb | 29 ++++++++----------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index c99ce0692..6b4baa981 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -17,23 +17,23 @@ class Api::V1::BaseController < ApplicationController {:page => params[:page], :per_page => per_page} end - def render_json_response(message, status = 200) - id = status != 200 ? nil : @subject.id + def render_json_response(subject, message, status = 200) + id = status != 200 ? nil : subject.id render :json => { - @subject.class.name.downcase.to_sym => { + subject.class.name.downcase.to_sym => { :id => id, :message => message } }.to_json, :status => status end - def render_validation_error(message) - errors = @subject.errors.full_messages.join('. ') + def render_validation_error(subject, message) + errors = subject.errors.full_messages.join('. ') if errors.present? message << '. ' << errors end - render_json_response(message, 422) + render_json_response(subject, message, 422) end end diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index c1ad76ece..ff37d009a 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -4,7 +4,6 @@ class Api::V1::PlatformsController < Api::V1::BaseController skip_before_filter :authenticate_user!, :only => [:show, :platforms_for_build, :members] if APP_CONFIG['anonymous_access'] load_and_authorize_resource - before_filter :set_subject, :except => [:index, :platforms_for_build, :show, :members] def index @platforms = @platforms.accessible_by(current_ability, :related). @@ -24,9 +23,9 @@ class Api::V1::PlatformsController < Api::V1::BaseController owner = User.where(:id => platform_params[:owner_id]).first @platform.owner = owner || get_owner if @platform.save - render_json_response 'Platform has been created successfully' + render_json_response @platform, 'Platform has been created successfully' else - render_validation_error 'Platform has not been created' + render_validation_error @platform, 'Platform has not been created' end end @@ -35,9 +34,9 @@ class Api::V1::PlatformsController < Api::V1::BaseController owner = User.where(:id => platform_params[:owner_id]).first platform_params[:owner] = owner if owner if @platform.update_attributes(platform_params) - render_json_response 'Platform has been updated successfully' + render_json_response @platform, 'Platform has been updated successfully' else - render_validation_error 'Platform has not been updated' + render_validation_error @platform, 'Platform has not been updated' end end @@ -47,17 +46,17 @@ class Api::V1::PlatformsController < Api::V1::BaseController def add_member if member.present? && @platform.add_member(member) - render_json_response "#{member.class.to_s} '#{member.id}' has been added to platform successfully" + render_json_response @platform, "#{member.class.to_s} '#{member.id}' has been added to platform successfully" else - render_validation_error 'Member has not been added to platform' + render_validation_error @platform, 'Member has not been added to platform' end end def remove_member if member.present? && @platform.remove_member(member) - render_json_response "#{member.class.to_s} '#{member.id}' has been removed from platform successfully" + render_json_response @platform, "#{member.class.to_s} '#{member.id}' has been removed from platform successfully" else - render_validation_error 'Member has not been removed from platform' + render_validation_error @platform, 'Member has not been removed from platform' end end @@ -66,20 +65,20 @@ class Api::V1::PlatformsController < Api::V1::BaseController platform_params[:owner] = current_user @cloned = @platform.full_clone(platform_params) if @cloned.persisted? - render_json_response 'Platform has been cloned successfully' + render_json_response @platform, 'Platform has been cloned successfully' else - render_validation_error 'Platform has not been cloned' + render_validation_error @platform, 'Platform has not been cloned' end end def clear @platform.clear - render_json_response 'Platform has been cleared successfully' + render_json_response @platform, 'Platform has been cleared successfully' end def destroy @platform.destroy # later with resque - render_json_response 'Platform has been destroyed successfully' + render_json_response @platform, 'Platform has been destroyed successfully' end private @@ -95,8 +94,4 @@ class Api::V1::PlatformsController < Api::V1::BaseController @member ||= '' end - def set_subject - @subject = @platform - end - end From 7f031ac688596d6ffe680f1f0c7e3f1505f38a7d Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Thu, 11 Oct 2012 16:55:06 +0600 Subject: [PATCH 25/39] [refs #374] added commit validation --- .../api/v1/build_lists_controller.rb | 7 ++-- .../projects/build_lists_controller.rb | 2 -- app/models/build_list.rb | 21 ++++++++++-- config/locales/models/build_list.en.yml | 4 ++- config/locales/models/build_list.ru.yml | 6 ++-- .../api/v1/build_lists_controller_spec.rb | 10 ++++-- .../projects/build_lists_controller_spec.rb | 32 +++++++++++-------- spec/factories/build_lists.rb | 4 +-- spec/spec_helper.rb | 2 +- 9 files changed, 57 insertions(+), 31 deletions(-) diff --git a/app/controllers/api/v1/build_lists_controller.rb b/app/controllers/api/v1/build_lists_controller.rb index 4a783de8b..d6a909c98 100644 --- a/app/controllers/api/v1/build_lists_controller.rb +++ b/app/controllers/api/v1/build_lists_controller.rb @@ -1,12 +1,12 @@ # -*- encoding : utf-8 -*- class Api::V1::BuildListsController < Api::V1::BaseController - + before_filter :authenticate_user! skip_before_filter :authenticate_user!, :only => [:show, :index] if APP_CONFIG['anonymous_access'] - + load_and_authorize_resource :project, :only => :index load_and_authorize_resource :build_list, :only => [:show, :create, :cancel, :publish, :reject_publish] - + def index filter = BuildList::Filter.new(nil, current_user, params[:filter] || {}) @build_lists = filter.find.scoped(:include => [:save_to_platform, :project, :user, :arch]) @@ -23,7 +23,6 @@ class Api::V1::BuildListsController < Api::V1::BaseController bl_params[:auto_publish] = false unless save_to_repository.publish_without_qa? @build_list = project.build_lists.build(bl_params) - @build_list.project_version = @build_list.commit_hash @build_list.user = current_user @build_list.priority = current_user.build_priority # User builds more priority than mass rebuild with zero priority diff --git a/app/controllers/projects/build_lists_controller.rb b/app/controllers/projects/build_lists_controller.rb index 742530992..570814681 100644 --- a/app/controllers/projects/build_lists_controller.rb +++ b/app/controllers/projects/build_lists_controller.rb @@ -57,8 +57,6 @@ class Projects::BuildListsController < Projects::BaseController Arch.where(:id => params[:arches]).each do |arch| Platform.main.where(:id => build_for_platforms).each do |build_for_platform| @build_list = @project.build_lists.build(params[:build_list]) - @build_list.commit_hash = @project.repo.commits(@build_list.project_version.match(/^latest_(.+)/).to_a.last || - @build_list.project_version).first.id if @build_list.project_version @build_list.build_for_platform = build_for_platform; @build_list.arch = arch; @build_list.user = current_user @build_list.include_repos = @build_list.include_repos.select {|ir| @build_list.build_for_platform.repository_ids.include? ir.to_i} @build_list.priority = current_user.build_priority # User builds more priority than mass rebuild with zero priority diff --git a/app/models/build_list.rb b/app/models/build_list.rb index bb8f8a955..37f68a827 100644 --- a/app/models/build_list.rb +++ b/app/models/build_list.rb @@ -32,8 +32,13 @@ class BuildList < ActiveRecord::Base errors.add(:save_to_repository, I18n.t('flash.build_list.wrong_include_repos')) unless build_for_platform.repository_ids.include? ir.to_i } } + validate lambda { + if commit_hash.blank? || project.repo.commit(commit_hash).blank? + errors.add :commit_hash, I18n.t('flash.build_list.wrong_commit_hash', :commit_hash => commit_hash) + end + } - LIVE_TIME = 4.week # for unpublished + LIVE_TIME = 4.week # for unpublished MAX_LIVE_TIME = 3.month # for published # The kernel does not send these statuses directly @@ -110,6 +115,7 @@ class BuildList < ActiveRecord::Base after_commit :place_build after_destroy :delete_container + before_validation :set_commit_and_version @queue = :clone_and_build @@ -204,7 +210,7 @@ class BuildList < ActiveRecord::Base def set_version_and_tag pkg = self.packages.where(:package_type => 'source', :project_id => self.project_id).first # TODO: remove 'return' after deployment ABF kernel 2.0 - return if pkg.nil? # For old client that does not sends data about packages + return if pkg.nil? # For old client that does not sends data about packages self.package_version = "#{pkg.platform.name}-#{pkg.version}-#{pkg.release}" system("cd #{self.project.repo.path} && git tag #{self.package_version} #{self.commit_hash}") # TODO REDO through grit save @@ -293,7 +299,7 @@ class BuildList < ActiveRecord::Base end def in_work? - status == BuildServer::BUILD_STARTED + status == BuildServer::BUILD_STARTED #[WAITING_FOR_RESPONSE, BuildServer::BUILD_PENDING, BuildServer::BUILD_STARTED].include?(status) end @@ -331,4 +337,13 @@ class BuildList < ActiveRecord::Base yield p end end + + def set_commit_and_version + if project_version.present? && commit_hash.blank? + self.commit_hash = project.repo.commits(project_version.match(/^latest_(.+)/).to_a.last || + project_version).try(:first).try(:id) + elsif project_version.blank? && commit_hash.present? + self.project_version = commit_hash + end + end end diff --git a/config/locales/models/build_list.en.yml b/config/locales/models/build_list.en.yml index e605e3658..962155821 100644 --- a/config/locales/models/build_list.en.yml +++ b/config/locales/models/build_list.en.yml @@ -29,6 +29,7 @@ en: started_at: Build started at duration: Build duration in seconds mass_build_id: Mass build + commit_hash: Commit hash build_list/item: name: Name @@ -123,7 +124,7 @@ en: build_log: Build Log not_available: Log not available yet. download: Download log - autoreload: Update log every + autoreload: Update log every load_lines: Load last %{count} lines reload_times: @@ -147,3 +148,4 @@ en: can_not_published: Build can only be published with status "Build complete" frozen_platform: In case of a repository for package storage with frozen platform allowed only bugfix and security updates wrong_include_repos: Include repos have to belongs to build for platform + wrong_commit_hash: Unable find commit '%{commit_hash}' in project diff --git a/config/locales/models/build_list.ru.yml b/config/locales/models/build_list.ru.yml index 621fbfc01..4953d08bd 100644 --- a/config/locales/models/build_list.ru.yml +++ b/config/locales/models/build_list.ru.yml @@ -28,6 +28,7 @@ ru: preferences: Настройки duration: Длительность билда в секундах mass_build_id: Массовая сборка + commit_hash: Хэш коммита build_list/item: name: Название @@ -120,9 +121,9 @@ ru: build_log: Лог сборки not_available: В настоящий момент лог недоступен. download: Загрузить лог - autoreload: Обновлять лог каждые + autoreload: Обновлять лог каждые load_lines: Загружать последние %{count} строк - + reload_times: 10000: "10 сек" 30000: "30 сек" @@ -144,3 +145,4 @@ ru: can_not_published: Опубликовать сборку можно только со статусом "Собран" frozen_platform: В случае выбора репозитория для сохранения пакетов из замороженнной платформы разрешены только bugfix и security обновления wrong_include_repos: Включаемые репозитории должны принадлежать платформе для сборки + wrong_commit_hash: Невозможно найти коммит '%{commit_hash}' в проекте diff --git a/spec/controllers/api/v1/build_lists_controller_spec.rb b/spec/controllers/api/v1/build_lists_controller_spec.rb index ad305faca..37466c139 100644 --- a/spec/controllers/api/v1/build_lists_controller_spec.rb +++ b/spec/controllers/api/v1/build_lists_controller_spec.rb @@ -47,6 +47,10 @@ shared_examples_for 'create build list via api' do #@project.build_lists.last.commit_hash.should == @project.repo.commits('4.7.5.3').last.id @project.build_lists.last.commit_hash.should == @params[:commit_hash] end + + it 'should not create without existing commit hash in project' do + lambda{ post :create, @create_params.deep_merge(:build_list => {:commit_hash => 'wrong'})}.should change{@project.build_lists.count}.by(0) + end end shared_examples_for 'not create build list via api' do @@ -383,13 +387,13 @@ describe Api::V1::BuildListsController do # Build Lists: @build_list1 = FactoryGirl.create(:build_list_core) - + @build_list2 = FactoryGirl.create(:build_list_core) @build_list2.project.update_column(:visibility, 'hidden') - + project = FactoryGirl.create(:project, :visibility => 'hidden', :owner => @user) @build_list3 = FactoryGirl.create(:build_list_core, :project => project) - + @build_list4 = FactoryGirl.create(:build_list_core) @build_list4.project.update_column(:visibility, 'hidden') @build_list4.project.relations.create! :role => 'reader', :actor_id => @user.id, :actor_type => 'User' diff --git a/spec/controllers/projects/build_lists_controller_spec.rb b/spec/controllers/projects/build_lists_controller_spec.rb index 680109fcc..d3c6ed428 100644 --- a/spec/controllers/projects/build_lists_controller_spec.rb +++ b/spec/controllers/projects/build_lists_controller_spec.rb @@ -30,7 +30,6 @@ describe Projects::BuildListsController do shared_examples_for 'create build list' do before { @project.update_attribute(:repositories, @platform.repositories) - test_git_commit(@project) } it 'should be able to perform new action' do @@ -53,12 +52,19 @@ describe Projects::BuildListsController do post :create, {:owner_name => @project.owner.uname, :project_name => @project.name}.merge(@create_params).deep_merge(:build_list => {:project_version => "4.7.5.3"}) @project.build_lists.last.commit_hash.should == @project.repo.commits('4.7.5.3').last.id end + + it 'should not be able to create with wrong project version' do + lambda{ post :create, {:owner_name => @project.owner.uname, :project_name => @project.name}.merge(@create_params).deep_merge(:build_list => {:project_version => "latest_wrong", :commit_hash => nil})}.should change{@project.build_lists.count}.by(0) + end + + it 'should not be able to create with wrong git hash' do + lambda{ post :create, {:owner_name => @project.owner.uname, :project_name => @project.name}.merge(@create_params).deep_merge(:build_list => {:commit_hash => 'wrong'})}.should change{@project.build_lists.count}.by(0) + end end shared_examples_for 'not create build list' do before { @project.update_attribute(:repositories, @platform.repositories) - test_git_commit(@project) } it 'should not be able to perform new action' do @@ -78,7 +84,7 @@ describe Projects::BuildListsController do before(:each) do @platform = FactoryGirl.create(:platform_with_repos) @create_params = { - :build_list => { + :build_list => { :project_version => 'latest_master', :save_to_repository_id => @platform.repositories.first.id, :update_type => 'security', @@ -119,13 +125,13 @@ describe Projects::BuildListsController do context 'for all build lists' do before(:each) do @build_list1 = FactoryGirl.create(:build_list_core) - + @build_list2 = FactoryGirl.create(:build_list_core) @build_list2.project.update_column(:visibility, 'hidden') - + project = FactoryGirl.create(:project, :visibility => 'hidden', :owner => @user) @build_list3 = FactoryGirl.create(:build_list_core, :project => project) - + @build_list4 = FactoryGirl.create(:build_list_core) @build_list4.project.update_column(:visibility, 'hidden') @build_list4.project.relations.create! :role => 'reader', :actor_id => @user.id, :actor_type => 'User' @@ -203,17 +209,17 @@ describe Projects::BuildListsController do @show_params = {:owner_name => @project.owner.uname, :project_name => @project.name, :id => @build_list.id} end - + context 'for all build lists' do before(:each) do @build_list1 = FactoryGirl.create(:build_list_core) - + @build_list2 = FactoryGirl.create(:build_list_core) @build_list2.project.update_column(:visibility, 'hidden') - + project = FactoryGirl.create(:project, :visibility => 'hidden', :owner => @user) @build_list3 = FactoryGirl.create(:build_list_core, :project => project) - + @build_list4 = FactoryGirl.create(:build_list_core) @build_list4.project.update_column(:visibility, 'hidden') @build_list4.project.relations.create! :role => 'reader', :actor_id => @user.id, :actor_type => 'User' @@ -286,13 +292,13 @@ describe Projects::BuildListsController do get :index assigns[:build_server_status].should == {} response.should be_success - end + end end end context 'filter' do - - before(:each) do + + before(:each) do set_session_for FactoryGirl.create(:admin) @build_list1 = FactoryGirl.create(:build_list_core) diff --git a/spec/factories/build_lists.rb b/spec/factories/build_lists.rb index ceab9f725..498e106f7 100644 --- a/spec/factories/build_lists.rb +++ b/spec/factories/build_lists.rb @@ -8,11 +8,11 @@ FactoryGirl.define do association :arch build_for_platform {|bl| bl.save_to_platform} save_to_repository {|bl| bl.save_to_platform.repositories.first} - project_version "1.0" build_requires true update_type 'security' include_repos {|bl| bl.save_to_platform.repositories.map(&:id)} - commit_hash '1234567890abcdef1234567890abcdef12345678' + project_version 'latest_master' + after(:build) {|bl| test_git_commit bl.project } end factory :build_list_core, :parent => :build_list do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 38d73124c..e168084b3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,7 +27,7 @@ RSpec.configure do |config| config.use_transactional_fixtures = true config.filter_run_excluding :anonymous_access => !(APP_CONFIG['anonymous_access']) - + end def set_session_for(user=nil) From 8f3ed58cd8e3641975af1d884b441beae2093a14 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Thu, 11 Oct 2012 20:27:17 +0400 Subject: [PATCH 26/39] #671: added Projects#refs_list API action --- app/controllers/api/v1/projects_controller.rb | 7 +++++-- app/views/api/v1/projects/refs_list.json.jbuilder | 8 ++++++++ config/routes.rb | 3 +++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 app/views/api/v1/projects/refs_list.json.jbuilder diff --git a/app/controllers/api/v1/projects_controller.rb b/app/controllers/api/v1/projects_controller.rb index 2345e4214..7f9182563 100644 --- a/app/controllers/api/v1/projects_controller.rb +++ b/app/controllers/api/v1/projects_controller.rb @@ -2,7 +2,7 @@ class Api::V1::ProjectsController < Api::V1::BaseController before_filter :authenticate_user! - skip_before_filter :authenticate_user!, :only => [:get_id, :show] if APP_CONFIG['anonymous_access'] + skip_before_filter :authenticate_user!, :only => [:get_id, :show, :refs] if APP_CONFIG['anonymous_access'] load_and_authorize_resource @@ -15,6 +15,9 @@ class Api::V1::ProjectsController < Api::V1::BaseController end def show - end + + def refs_list + end + end diff --git a/app/views/api/v1/projects/refs_list.json.jbuilder b/app/views/api/v1/projects/refs_list.json.jbuilder new file mode 100644 index 000000000..7b913f98b --- /dev/null +++ b/app/views/api/v1/projects/refs_list.json.jbuilder @@ -0,0 +1,8 @@ +json.refs_list (@project.repo.branches + @project.repo.tags) do |json_grit, grit| + json_grit.ref grit.name + json_grit.object do |json_object| + json_object.type (grit.class.name =~ /Tag/ ? 'tag' : 'commit') + json_object.sha grit.commit.id + end +end +json.url refs_list_api_v1_project_path(@project.id, :format => :json) \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 03034a36e..9e9038b08 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -35,6 +35,9 @@ Rosa::Application.routes.draw do resources :repositories, :only => [:show] resources :projects, :only => [:show] do collection { get :get_id } + member { + get :refs_list + } end end end From 32dea638657b9589b330bb457349a517a2f33445 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Thu, 11 Oct 2012 21:51:40 +0400 Subject: [PATCH 27/39] #671: added specs for Projects#refs_list API --- .../api/v1/projects_controller_spec.rb | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/spec/controllers/api/v1/projects_controller_spec.rb b/spec/controllers/api/v1/projects_controller_spec.rb index f76e4f468..1f22d28f8 100644 --- a/spec/controllers/api/v1/projects_controller_spec.rb +++ b/spec/controllers/api/v1/projects_controller_spec.rb @@ -24,12 +24,17 @@ end shared_examples_for "api projects user without show rights" do it "should show access violation instead of project data" do get :show, :id => @project.id, :format => :json - response.body.should == {"message" => "Access violation to this page!"}.to_json + response.should_not be_success + end + + it "should show access violation instead of project refs_list" do + get :refs_list, :id => @project.id, :format => :json + response.should_not be_success end it "should access violation instead of project data by get_id" do get :get_id, :name => @project.name, :owner => @project.owner.uname, :format => :json - response.body.should == {"message" => "Access violation to this page!"}.to_json + response.should_not be_success end end @@ -39,6 +44,11 @@ shared_examples_for "api projects user with show rights" do render_template(:show) end + it "should show refs_list of project" do + get :refs_list, :id => @project.id, :format => :json + render_template(:refs_list) + end + context 'project find by get_id' do it "should find project by name and owner name" do @project.reload @@ -70,19 +80,13 @@ describe Api::V1::ProjectsController do context 'for guest' do - it 'should be able to perform get_id action', :anonymous_access => false do - get :get_id, :format => :json - response.status.should == 401 + if APP_CONFIG['anonymous_access'] + it_should_behave_like 'api projects user with reader rights' + it_should_behave_like 'api projects user without reader rights for hidden project' + else + it_should_behave_like 'api projects user without show rights' end - it 'should be able to perform show action', :anonymous_access => false do - get :show, :id => @project.id, :format => :json - response.status.should == 401 - end - - it_should_behave_like 'api projects user with reader rights' if APP_CONFIG['anonymous_access'] - it_should_behave_like 'api projects user without reader rights for hidden project' if APP_CONFIG['anonymous_access'] - end context 'for simple user' do From 72822f976089a5f5d6570b17fa6f80b7e6caba86 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Thu, 11 Oct 2012 22:38:19 +0400 Subject: [PATCH 28/39] #671: updated specs for Platforms API --- spec/controllers/api/v1/platforms_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/controllers/api/v1/platforms_controller_spec.rb b/spec/controllers/api/v1/platforms_controller_spec.rb index 4638a32af..fc906dbae 100644 --- a/spec/controllers/api/v1/platforms_controller_spec.rb +++ b/spec/controllers/api/v1/platforms_controller_spec.rb @@ -241,7 +241,7 @@ describe Api::V1::PlatformsController do end end - it 'should be able to perform members action' do + it 'should be able to perform members action', :anonymous_access => true do get :members, :id => @platform.id, :format => :json response.should render_template(:members) end From 4f21f0cb880cc64958d0cde7e8870a9e95c38239 Mon Sep 17 00:00:00 2001 From: Vladimir Sharshov Date: Fri, 12 Oct 2012 00:16:51 +0400 Subject: [PATCH 29/39] Add new school adress; change email send method to sendmail; add google check page --- config/environments/production.rb | 1 + public/googleac324ed2f70faefc.html | 1 + 2 files changed, 2 insertions(+) create mode 100644 public/googleac324ed2f70faefc.html diff --git a/config/environments/production.rb b/config/environments/production.rb index b3ca044d1..2ab9938fd 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -38,6 +38,7 @@ Rosa::Application.configure do # Disable delivery errors, bad email addresses will be ignored # config.action_mailer.raise_delivery_errors = false config.action_mailer.default_url_options = { :host => 'abf.rosalinux.ru' } + config.delivery_method = :sendmail # Enable threaded mode # config.threadsafe! diff --git a/public/googleac324ed2f70faefc.html b/public/googleac324ed2f70faefc.html new file mode 100644 index 000000000..95843ef02 --- /dev/null +++ b/public/googleac324ed2f70faefc.html @@ -0,0 +1 @@ +google-site-verification: googleac324ed2f70faefc.html \ No newline at end of file From 6981a7f238df6e0f5f5cfee143eb20b4c44b961d Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Fri, 12 Oct 2012 16:20:08 +0600 Subject: [PATCH 30/39] [refs #648] disable submiting the project filter form --- app/views/projects/projects/index.html.haml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/views/projects/projects/index.html.haml b/app/views/projects/projects/index.html.haml index 996951513..920b10daa 100644 --- a/app/views/projects/projects/index.html.haml +++ b/app/views/projects/projects/index.html.haml @@ -124,6 +124,9 @@ $(".div-filter-labels").live('click', function() { oTable.dataTable().fnDraw(); }); + $('#filter_projects').live('submit', function() { + return false; + }); }); =# will_paginate From be8ed0984a8ff3d057dfc3155ae5a2953c9659eb Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Sun, 14 Oct 2012 16:00:49 +0400 Subject: [PATCH 31/39] #672: added #add_member, #remove_member, #update, #destroy actipns for Repository API --- app/controllers/api/v1/base_controller.rb | 29 +--- .../api/v1/platforms_controller.rb | 13 -- .../api/v1/repositories_controller.rb | 29 ++++ app/helpers/api/v1/base_helper.rb | 42 ++++++ config/routes.rb | 7 +- .../api/v1/platforms_controller_spec.rb | 2 +- .../api/v1/repositories_controller_spec.rb | 137 +++++++++++++++++- 7 files changed, 214 insertions(+), 45 deletions(-) create mode 100644 app/helpers/api/v1/base_helper.rb diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 6b4baa981..cb2b8bcdc 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -1,5 +1,6 @@ # -*- encoding : utf-8 -*- class Api::V1::BaseController < ApplicationController + include Api::V1::BaseHelper #respond_to :json rescue_from CanCan::AccessDenied do |exception| @@ -8,32 +9,4 @@ class Api::V1::BaseController < ApplicationController end end - protected - - def paginate_params - per_page = params[:per_page].to_i - per_page = 20 if per_page < 1 - per_page = 100 if per_page >100 - {:page => params[:page], :per_page => per_page} - end - - def render_json_response(subject, message, status = 200) - id = status != 200 ? nil : subject.id - - render :json => { - subject.class.name.downcase.to_sym => { - :id => id, - :message => message - } - }.to_json, :status => status - end - - def render_validation_error(subject, message) - errors = subject.errors.full_messages.join('. ') - if errors.present? - message << '. ' << errors - end - render_json_response(subject, message, 422) - end - end diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index ff37d009a..19efa1611 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -81,17 +81,4 @@ class Api::V1::PlatformsController < Api::V1::BaseController render_json_response @platform, 'Platform has been destroyed successfully' end - private - - def member - return @member if @member - if params[:type] == 'User' - member = User - elsif params[:type] == 'Group' - member = Group - end - @member = member.where(:id => params[:member_id]).first if member - @member ||= '' - end - end diff --git a/app/controllers/api/v1/repositories_controller.rb b/app/controllers/api/v1/repositories_controller.rb index 92546fe97..5b53ba3a4 100644 --- a/app/controllers/api/v1/repositories_controller.rb +++ b/app/controllers/api/v1/repositories_controller.rb @@ -7,7 +7,36 @@ class Api::V1::RepositoriesController < Api::V1::BaseController load_and_authorize_resource :repository, :through => :platform, :shallow => true def show + end + def update + rep_params = params[:repository] || {} + if @repository.update_attributes(rep_params) + render_json_response @repository, 'Repository has been updated successfully' + else + render_validation_error @repository, 'Repository has not been updated' + end + end + + def add_member + if member.present? && @repository.add_member(member) + render_json_response @repository, "#{member.class.to_s} '#{member.id}' has been added to repository successfully" + else + render_validation_error @repository, 'Member has not been added to repository' + end + end + + def remove_member + if member.present? && @repository.remove_member(member) + render_json_response @repository, "#{member.class.to_s} '#{member.id}' has been removed from repository successfully" + else + render_validation_error @repository, 'Member has not been removed from repository' + end + end + + def destroy + @repository.destroy # later with resque + render_json_response @repository, 'Repository has been destroyed successfully' end end diff --git a/app/helpers/api/v1/base_helper.rb b/app/helpers/api/v1/base_helper.rb new file mode 100644 index 000000000..969a3760c --- /dev/null +++ b/app/helpers/api/v1/base_helper.rb @@ -0,0 +1,42 @@ +module Api::V1::BaseHelper + + protected + + def paginate_params + per_page = params[:per_page].to_i + per_page = 20 if per_page < 1 + per_page = 100 if per_page >100 + {:page => params[:page], :per_page => per_page} + end + + def render_json_response(subject, message, status = 200) + id = status != 200 ? nil : subject.id + + render :json => { + subject.class.name.downcase.to_sym => { + :id => id, + :message => message + } + }.to_json, :status => status + end + + def render_validation_error(subject, message) + errors = subject.errors.full_messages.join('. ') + if errors.present? + message << '. ' << errors + end + render_json_response(subject, message, 422) + end + + def member + return @member if @member + if params[:type] == 'User' + member = User + elsif params[:type] == 'Group' + member = Group + end + @member = member.where(:id => params[:member_id]).first if member + @member ||= '' + end + +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 9e9038b08..63df9f4d0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -32,7 +32,12 @@ Rosa::Application.routes.draw do put :clear } end - resources :repositories, :only => [:show] + resources :repositories, :only => [:show, :update, :destroy] do + member { + put :add_member + delete :remove_member + } + end resources :projects, :only => [:show] do collection { get :get_id } member { diff --git a/spec/controllers/api/v1/platforms_controller_spec.rb b/spec/controllers/api/v1/platforms_controller_spec.rb index fc906dbae..682bce637 100644 --- a/spec/controllers/api/v1/platforms_controller_spec.rb +++ b/spec/controllers/api/v1/platforms_controller_spec.rb @@ -52,7 +52,7 @@ shared_examples_for 'api platform user with writer rights' do delete :remove_member, {:member_id => member.id, :type => 'User', :id => @platform.id}, :format => :json end - it 'should be able to perform update action' do + it 'should be able to perform remove_member action' do response.should be_success end it 'ensures that member has been removed from platform' do diff --git a/spec/controllers/api/v1/repositories_controller_spec.rb b/spec/controllers/api/v1/repositories_controller_spec.rb index 7ca1dffc4..2ea7952a7 100644 --- a/spec/controllers/api/v1/repositories_controller_spec.rb +++ b/spec/controllers/api/v1/repositories_controller_spec.rb @@ -35,6 +35,133 @@ shared_examples_for "api repository user without show rights" do end end +shared_examples_for 'api repository user with writer rights' do + + context 'api repository user with update rights' do + before do + put :update, {:repository => {:description => 'new description'}, :id => @repository.id}, :format => :json + end + + it 'should be able to perform update action' do + response.should be_success + end + it 'ensures that repository has been updated' do + @repository.reload + @repository.description.should == 'new description' + end + end + + context 'api repository user with add_member rights' do + let(:member) { FactoryGirl.create(:user) } + before do + put :add_member, {:member_id => member.id, :type => 'User', :id => @repository.id}, :format => :json + end + + it 'should be able to perform add_member action' do + response.should be_success + end + it 'ensures that new member has been added to repository' do + @repository.members.should include(member) + end + end + + context 'api repository user with remove_member rights' do + let(:member) { FactoryGirl.create(:user) } + before do + @repository.add_member(member) + delete :remove_member, {:member_id => member.id, :type => 'User', :id => @repository.id}, :format => :json + end + + it 'should be able to perform remove_member action' do + response.should be_success + end + it 'ensures that member has been removed from repository' do + @repository.members.should_not include(member) + end + end + + context 'api repository user with destroy rights' do + it 'should be able to perform destroy action for main platform' do + delete :destroy, :id => @repository.id, :format => :json + response.should be_success + end + it 'ensures that repository of main platform has been destroyed' do + lambda { delete :destroy, :id => @repository.id, :format => :json }.should change{ Repository.count }.by(-1) + end + it 'should not be able to perform destroy action for repository of personal platform' do + delete :destroy, :id => @personal_repository.id, :format => :json + response.should_not be_success + end + it 'ensures that repository of personal platform has not been destroyed' do + lambda { delete :destroy, :id => @personal_repository.id, :format => :json }.should_not change{ Repository.count } + end + end +end + +shared_examples_for 'api repository user without writer rights' do + + context 'api repository user without update rights' do + before do + put :update, {:repository => {:description => 'new description'}, :id => @repository.id}, :format => :json + end + + it 'should not be able to perform update action' do + response.should_not be_success + end + it 'ensures that repository has not been updated' do + @repository.reload + @repository.description.should_not == 'new description' + end + end + + context 'api repository user without add_member rights' do + let(:member) { FactoryGirl.create(:user) } + before do + put :add_member, {:member_id => member.id, :type => 'User', :id => @repository.id}, :format => :json + end + + it 'should not be able to perform add_member action' do + response.should_not be_success + end + it 'ensures that new member has not been added to repository' do + @repository.members.should_not include(member) + end + end + + context 'api repository user without remove_member rights' do + let(:member) { FactoryGirl.create(:user) } + before do + @repository.add_member(member) + delete :remove_member, {:member_id => member.id, :type => 'User', :id => @repository.id}, :format => :json + end + + it 'should be able to perform update action' do + response.should_not be_success + end + it 'ensures that member has not been removed from repository' do + @repository.members.should include(member) + end + end + + context 'api repository user without destroy rights' do + it 'should not be able to perform destroy action for repository of main platform' do + delete :destroy, :id => @repository.id, :format => :json + response.should_not be_success + end + it 'ensures that repository of main platform has not been destroyed' do + lambda { delete :destroy, :id => @repository.id, :format => :json }.should_not change{ Repository.count } + end + it 'should not be able to perform destroy action for repository of personal platform' do + delete :destroy, :id => @personal_repository.id, :format => :json + response.should_not be_success + end + it 'ensures that repository of personal platform has not been destroyed' do + lambda { delete :destroy, :id => @personal_repository.id, :format => :json }.should_not change{ Repository.count } + end + end +end + + describe Api::V1::RepositoriesController do before(:each) do stub_symlink_methods @@ -52,8 +179,11 @@ describe Api::V1::RepositoriesController do response.status.should == 401 end - it_should_behave_like 'api repository user without reader rights for hidden platform' if APP_CONFIG['anonymous_access'] - it_should_behave_like 'api repository user with show rights' if APP_CONFIG['anonymous_access'] + if APP_CONFIG['anonymous_access'] + it_should_behave_like 'api repository user without reader rights for hidden platform' + it_should_behave_like 'api repository user with show rights' + end + it_should_behave_like 'api repository user without writer rights' end context 'for admin' do @@ -64,6 +194,7 @@ describe Api::V1::RepositoriesController do it_should_behave_like 'api repository user with reader rights' it_should_behave_like 'api repository user with reader rights for hidden platform' + it_should_behave_like 'api repository user with writer rights' end context 'for platform owner user' do @@ -77,6 +208,7 @@ describe Api::V1::RepositoriesController do it_should_behave_like 'api repository user with reader rights' it_should_behave_like 'api repository user with reader rights for hidden platform' + it_should_behave_like 'api repository user with writer rights' end context 'for user' do @@ -88,5 +220,6 @@ describe Api::V1::RepositoriesController do it_should_behave_like 'api repository user with reader rights' it_should_behave_like 'api repository user without reader rights for hidden platform' it_should_behave_like 'api repository user with show rights' + it_should_behave_like 'api repository user without writer rights' end end From 146f72c06202d4f24691a6866f029b0e8db1c85d Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Sun, 14 Oct 2012 16:39:58 +0400 Subject: [PATCH 32/39] #672: small refactoring --- .../api/v1/platforms_controller.rb | 21 +++--------- .../api/v1/repositories_controller.rb | 22 +++--------- app/helpers/api/v1/base_helper.rb | 34 +++++++++++++++++++ 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index 19efa1611..007237092 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -33,11 +33,7 @@ class Api::V1::PlatformsController < Api::V1::BaseController platform_params = params[:platform] || {} owner = User.where(:id => platform_params[:owner_id]).first platform_params[:owner] = owner if owner - if @platform.update_attributes(platform_params) - render_json_response @platform, 'Platform has been updated successfully' - else - render_validation_error @platform, 'Platform has not been updated' - end + update_subject @platform end def members @@ -45,19 +41,11 @@ class Api::V1::PlatformsController < Api::V1::BaseController end def add_member - if member.present? && @platform.add_member(member) - render_json_response @platform, "#{member.class.to_s} '#{member.id}' has been added to platform successfully" - else - render_validation_error @platform, 'Member has not been added to platform' - end + add_member_to_subject @platform end def remove_member - if member.present? && @platform.remove_member(member) - render_json_response @platform, "#{member.class.to_s} '#{member.id}' has been removed from platform successfully" - else - render_validation_error @platform, 'Member has not been removed from platform' - end + remove_member_from_subject @platform end def clone @@ -77,8 +65,7 @@ class Api::V1::PlatformsController < Api::V1::BaseController end def destroy - @platform.destroy # later with resque - render_json_response @platform, 'Platform has been destroyed successfully' + destroy_subject @platform end end diff --git a/app/controllers/api/v1/repositories_controller.rb b/app/controllers/api/v1/repositories_controller.rb index 5b53ba3a4..fa9fa6046 100644 --- a/app/controllers/api/v1/repositories_controller.rb +++ b/app/controllers/api/v1/repositories_controller.rb @@ -10,33 +10,19 @@ class Api::V1::RepositoriesController < Api::V1::BaseController end def update - rep_params = params[:repository] || {} - if @repository.update_attributes(rep_params) - render_json_response @repository, 'Repository has been updated successfully' - else - render_validation_error @repository, 'Repository has not been updated' - end + update_subject @repository end def add_member - if member.present? && @repository.add_member(member) - render_json_response @repository, "#{member.class.to_s} '#{member.id}' has been added to repository successfully" - else - render_validation_error @repository, 'Member has not been added to repository' - end + add_member_to_subject @repository end def remove_member - if member.present? && @repository.remove_member(member) - render_json_response @repository, "#{member.class.to_s} '#{member.id}' has been removed from repository successfully" - else - render_validation_error @repository, 'Member has not been removed from repository' - end + remove_member_from_subject @repository end def destroy - @repository.destroy # later with resque - render_json_response @repository, 'Repository has been destroyed successfully' + destroy_subject @repository end end diff --git a/app/helpers/api/v1/base_helper.rb b/app/helpers/api/v1/base_helper.rb index 969a3760c..0599169b8 100644 --- a/app/helpers/api/v1/base_helper.rb +++ b/app/helpers/api/v1/base_helper.rb @@ -2,6 +2,38 @@ module Api::V1::BaseHelper protected + def add_member_to_subject(subject) + class_name = subject.class.name.downcase + if member.present? && subject.add_member(member) + render_json_response subject, "#{member.class.to_s} '#{member.id}' has been added to #{class_name} successfully" + else + render_validation_error subject, "Member has not been added to #{class_name}" + end + end + + def remove_member_from_subject(subject) + class_name = subject.class.name.downcase + if member.present? && subject.remove_member(member) + render_json_response subject, "#{member.class.to_s} '#{member.id}' has been removed from #{class_name} successfully" + else + render_validation_error subject, "Member has not been removed from #{class_name}" + end + end + + def destroy_subject(subject) + subject.destroy # later with resque + render_json_response subject, "#{subject.class.name} has been destroyed successfully" + end + + def update_subject(subject) + class_name = subject.class.name + if subject.update_attributes(params[class_name.downcase.to_sym] || {}) + render_json_response subject, "#{class_name} has been updated successfully" + else + render_validation_error subject, "#{class_name} has not been updated" + end + end + def paginate_params per_page = params[:per_page].to_i per_page = 20 if per_page < 1 @@ -28,6 +60,8 @@ module Api::V1::BaseHelper render_json_response(subject, message, 422) end + private + def member return @member if @member if params[:type] == 'User' From 00f5d0ce6242f3918ad09a87b6f22d09bacb17b3 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Sun, 14 Oct 2012 17:38:06 +0400 Subject: [PATCH 33/39] #672: updated Repositories API --- .../api/v1/repositories_controller.rb | 34 ++++++++++++- .../platforms/repositories_controller.rb | 3 +- app/models/ability.rb | 2 +- config/routes.rb | 3 ++ .../api/v1/repositories_controller_spec.rb | 49 +++++++++++++++++++ 5 files changed, 87 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/repositories_controller.rb b/app/controllers/api/v1/repositories_controller.rb index fa9fa6046..474b0b648 100644 --- a/app/controllers/api/v1/repositories_controller.rb +++ b/app/controllers/api/v1/repositories_controller.rb @@ -25,4 +25,36 @@ class Api::V1::RepositoriesController < Api::V1::BaseController destroy_subject @repository end -end + def add_project + project = Project.where(:id => params[:project_id]).first + if project + begin + @repository.projects << project + render_json_response @repository, "Project '#{project.id}' has been added to repository successfully" + rescue ActiveRecord::RecordInvalid + render_validation_error @repository, t('flash.repository.project_not_added') + end + else + render_validation_error @repository, "Project has not been added to repository" + end + end + + def remove_project + project_id = params[:project_id] + ProjectToRepository.where(:project_id => project_id, :repository_id => @repository.id).destroy_all + render_json_response @repository, "Project '#{project_id}' has been removed from repository successfully" + end + + def signatures + key_pair = @repository.key_pair + key_pair.destroy if key_pair + key_pair = @repository.key_pair.build(params[:repository]) + key_pair.user_id = current_user.id + if key_pair.save + render_json_response @repository, 'Signatures have been updated for repository successfully' + else + render_validation_error @repository, 'Signatures have not been updated for repository' + end + end + +end \ No newline at end of file diff --git a/app/controllers/platforms/repositories_controller.rb b/app/controllers/platforms/repositories_controller.rb index 7afc34a21..8767fbb94 100644 --- a/app/controllers/platforms/repositories_controller.rb +++ b/app/controllers/platforms/repositories_controller.rb @@ -131,8 +131,7 @@ class Platforms::RepositoriesController < Platforms::BaseController end def remove_project - @project = Project.find(params[:project_id]) - ProjectToRepository.where(:project_id => @project.id, :repository_id => @repository.id).destroy_all + ProjectToRepository.where(:project_id => params[:project_id], :repository_id => @repository.id).destroy_all redirect_to platform_repository_path(@platform, @repository), :notice => t('flash.repository.project_removed') end diff --git a/app/models/ability.rb b/app/models/ability.rb index 24c4a7a6a..382673bc5 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -100,7 +100,7 @@ class Ability can [:read, :projects_list], Repository, :platform => {:owner_type => 'Group', :owner_id => user.group_ids} can([:read, :projects_list], Repository, read_relations_for('repositories', 'platforms')) {|repository| local_reader? repository.platform} can([:create, :edit, :update, :destroy, :projects_list, :add_project, :remove_project], Repository) {|repository| local_admin? repository.platform} - can([:remove_members, :remove_member, :add_member], Repository) {|repository| owner?(repository.platform) || local_admin?(repository.platform)} + can([:remove_members, :remove_member, :add_member, :signatures], Repository) {|repository| owner?(repository.platform) || local_admin?(repository.platform)} can([:add_project, :remove_project], Repository) {|repository| repository.members.exists?(:id => user.id)} can(:clear, Platform) {|platform| local_admin?(platform) && platform.personal?} can([:change_visibility, :settings, :destroy, :edit, :update], Repository) {|repository| owner? repository.platform} diff --git a/config/routes.rb b/config/routes.rb index 63df9f4d0..6f8bd4009 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -36,6 +36,9 @@ Rosa::Application.routes.draw do member { put :add_member delete :remove_member + put :add_project + delete :remove_project + put :signatures } end resources :projects, :only => [:show] do diff --git a/spec/controllers/api/v1/repositories_controller_spec.rb b/spec/controllers/api/v1/repositories_controller_spec.rb index 2ea7952a7..fcff0d10b 100644 --- a/spec/controllers/api/v1/repositories_controller_spec.rb +++ b/spec/controllers/api/v1/repositories_controller_spec.rb @@ -96,6 +96,30 @@ shared_examples_for 'api repository user with writer rights' do lambda { delete :destroy, :id => @personal_repository.id, :format => :json }.should_not change{ Repository.count } end end + + context 'api repository user with add_project rights' do + before { put :add_project, :id => @repository.id, :project_id => @project.id, :format => :json } + it 'should be able to perform add_project action' do + response.should be_success + end + it 'ensures that project has been added to repository' do + @repository.projects.should include(@project) + end + end + + context 'api repository user with remove_project rights' do + before do + @repository.projects << @project + delete :remove_project, :id => @repository.id, :project_id => @project.id, :format => :json + end + it 'should be able to perform remove_project action' do + response.should be_success + end + it 'ensures that project has been removed from repository' do + @repository.reload + @repository.projects.should_not include(@project) + end + end end shared_examples_for 'api repository user without writer rights' do @@ -159,6 +183,31 @@ shared_examples_for 'api repository user without writer rights' do lambda { delete :destroy, :id => @personal_repository.id, :format => :json }.should_not change{ Repository.count } end end + + context 'api repository user without add_project rights' do + before { put :add_project, :id => @repository.id, :project_id => @project.id, :format => :json } + it 'should not be able to perform add_project action' do + response.should_not be_success + end + it 'ensures that project has not been added to repository' do + @repository.projects.should_not include(@project) + end + end + + context 'api repository user without remove_project rights' do + before do + @repository.projects << @project + delete :remove_project, :id => @repository.id, :project_id => @project.id, :format => :json + end + it 'should not be able to perform remove_project action' do + response.should_not be_success + end + it 'ensures that project has not been removed from repository' do + @repository.reload + @repository.projects.should include(@project) + end + end + end From 5c88ac6f3e9d5c2966f0f3cbe50bca4b26aaa48f Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Sun, 14 Oct 2012 18:44:10 +0400 Subject: [PATCH 34/39] #672: updated Repositories#signatures API action, added specs --- .../api/v1/repositories_controller.rb | 7 +++-- .../api/v1/repositories_controller_spec.rb | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/repositories_controller.rb b/app/controllers/api/v1/repositories_controller.rb index 474b0b648..c438478d7 100644 --- a/app/controllers/api/v1/repositories_controller.rb +++ b/app/controllers/api/v1/repositories_controller.rb @@ -48,12 +48,15 @@ class Api::V1::RepositoriesController < Api::V1::BaseController def signatures key_pair = @repository.key_pair key_pair.destroy if key_pair - key_pair = @repository.key_pair.build(params[:repository]) + key_pair = @repository.build_key_pair(params[:repository]) key_pair.user_id = current_user.id if key_pair.save render_json_response @repository, 'Signatures have been updated for repository successfully' else - render_validation_error @repository, 'Signatures have not been updated for repository' + message = 'Signatures have not been updated for repository' + errors = key_pair.errors.full_messages.join('. ') + (message << '. ' << errors) if errors.present? + render_json_response @repository, message, 422 end end diff --git a/spec/controllers/api/v1/repositories_controller_spec.rb b/spec/controllers/api/v1/repositories_controller_spec.rb index fcff0d10b..1aa7b8ffb 100644 --- a/spec/controllers/api/v1/repositories_controller_spec.rb +++ b/spec/controllers/api/v1/repositories_controller_spec.rb @@ -120,6 +120,20 @@ shared_examples_for 'api repository user with writer rights' do @repository.projects.should_not include(@project) end end + + context 'api repository user with update signatures rights' do + before do + stub_key_pairs_calls + put :signatures, :id => @repository.id, :repository => {:public => 'iampublic', :secret => 'iamsecret'}, :format => :json + end + it 'should be able to perform signatures action' do + response.should be_success + end + it 'ensures that signatures has been updated' do + @repository.key_pair.should_not be_nil + end + end + end shared_examples_for 'api repository user without writer rights' do @@ -208,6 +222,19 @@ shared_examples_for 'api repository user without writer rights' do end end + context 'api repository user without update signatures rights' do + before do + stub_key_pairs_calls + put :signatures, :id => @repository.id, :repository => {:public => 'iampublic', :secret => 'iamsecret'}, :format => :json + end + it 'should not be able to perform signatures action' do + response.should_not be_success + end + it 'ensures that signatures has not been updated' do + @repository.key_pair.should be_nil + end + end + end From fae5d3ff2ba3b41af04712b3cd7f06eda4e99883 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 15 Oct 2012 12:28:18 +0400 Subject: [PATCH 35/39] #690: truncate project name before validation --- app/models/project.rb | 6 +++- spec/models/project_spec.rb | 58 +++++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index 1c8bc74a9..43baaa007 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -54,7 +54,7 @@ class Project < ActiveRecord::Base where("(projects.owner_id in (?) AND projects.owner_type = 'Group') OR (projects.owner_id in (?) AND projects.owner_type = 'User')", group_owner_ids, user_owner_ids) } - + before_validation :truncate_name, :on => :create before_create :set_maintainer after_save :attach_to_personal_repository @@ -177,6 +177,10 @@ class Project < ActiveRecord::Base protected + def truncate_name + self.name = name.strip if name + end + def attach_to_personal_repository owner_rep = self.owner.personal_repository if is_package diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index ab13f6ae3..c2892dec4 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -9,17 +9,35 @@ describe Project do @child_child_project = @child_project.fork(FactoryGirl.create(:user)) end - context 'for destroy root' do - before do - @root_project.destroy + context 'for destroy' do + let!(:root_project) { FactoryGirl.create(:project) } + let!(:child_project) { root_project.fork(FactoryGirl.create(:user)) } + let!(:child_child_project) { child_project.fork(FactoryGirl.create(:user)) } + + context 'root project' do + before { root_project.destroy } + + it "should not be delete child" do + Project.where(:id => child_project).count.should == 1 + end + + it "should not be delete child of the child" do + Project.where(:id => child_child_project).count.should == 1 + end end - it "should not be delete child" do - Project.where(:id => @child_project).count.should == 1 - end + pending 'when will be available :orphan_strategy => :adopt' do + context 'middle node' do + before{ child_project.destroy } - it "should not be delete child of the child" do - Project.where(:id => @child_child_project).count.should == 1 + it "should set root project as a parent for orphan child" do + Project.find(child_child_project).ancestry == root_project + end + + it "should not be delete child of the child" do + Project.where(:id => child_child_project).count.should == 1 + end + end end end @@ -48,19 +66,17 @@ describe Project do end end - # uncommit when will be available :orphan_strategy => :adopt + context 'truncates project name before validation' do + let!(:project) { FactoryGirl.build(:project, :name => ' test_name ') } - #context 'for destroy middle node' do - # before(:each) do - # @child_project.destroy - # end + it 'ensures that validation passed' do + project.valid?.should be_true + end + + it 'ensures that name has been truncated' do + project.valid? + project.name.should == 'test_name' + end + end - # it "should set root project as a parent for orphan child" do - # Project.find(@child_child_project).ancestry == @root_project - # end - - # it "should not be delete child of the child" do - # Project.where(:id => @child_child_project).count.should == 1 - # end - #end end From 5d9a1e2586d1dcde6faabe063d534f6004d967b4 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 15 Oct 2012 13:48:25 +0400 Subject: [PATCH 36/39] #672: removed API helper --- app/controllers/api/v1/base_controller.rb | 74 +++++++++++++++++++++- app/helpers/api/v1/base_helper.rb | 76 ----------------------- 2 files changed, 73 insertions(+), 77 deletions(-) delete mode 100644 app/helpers/api/v1/base_helper.rb diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index cb2b8bcdc..b7fea5dd0 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -1,6 +1,5 @@ # -*- encoding : utf-8 -*- class Api::V1::BaseController < ApplicationController - include Api::V1::BaseHelper #respond_to :json rescue_from CanCan::AccessDenied do |exception| @@ -9,4 +8,77 @@ class Api::V1::BaseController < ApplicationController end end + protected + + def add_member_to_subject(subject) + class_name = subject.class.name.downcase + if member.present? && subject.add_member(member) + render_json_response subject, "#{member.class.to_s} '#{member.id}' has been added to #{class_name} successfully" + else + render_validation_error subject, "Member has not been added to #{class_name}" + end + end + + def remove_member_from_subject(subject) + class_name = subject.class.name.downcase + if member.present? && subject.remove_member(member) + render_json_response subject, "#{member.class.to_s} '#{member.id}' has been removed from #{class_name} successfully" + else + render_validation_error subject, "Member has not been removed from #{class_name}" + end + end + + def destroy_subject(subject) + subject.destroy # later with resque + render_json_response subject, "#{subject.class.name} has been destroyed successfully" + end + + def update_subject(subject) + class_name = subject.class.name + if subject.update_attributes(params[class_name.downcase.to_sym] || {}) + render_json_response subject, "#{class_name} has been updated successfully" + else + render_validation_error subject, "#{class_name} has not been updated" + end + end + + def paginate_params + per_page = params[:per_page].to_i + per_page = 20 if per_page < 1 + per_page = 100 if per_page >100 + {:page => params[:page], :per_page => per_page} + end + + def render_json_response(subject, message, status = 200) + id = status != 200 ? nil : subject.id + + render :json => { + subject.class.name.downcase.to_sym => { + :id => id, + :message => message + } + }.to_json, :status => status + end + + def render_validation_error(subject, message) + errors = subject.errors.full_messages.join('. ') + if errors.present? + message << '. ' << errors + end + render_json_response(subject, message, 422) + end + + private + + def member + return @member if @member + if params[:type] == 'User' + member = User + elsif params[:type] == 'Group' + member = Group + end + @member = member.where(:id => params[:member_id]).first if member + @member ||= '' + end + end diff --git a/app/helpers/api/v1/base_helper.rb b/app/helpers/api/v1/base_helper.rb deleted file mode 100644 index 0599169b8..000000000 --- a/app/helpers/api/v1/base_helper.rb +++ /dev/null @@ -1,76 +0,0 @@ -module Api::V1::BaseHelper - - protected - - def add_member_to_subject(subject) - class_name = subject.class.name.downcase - if member.present? && subject.add_member(member) - render_json_response subject, "#{member.class.to_s} '#{member.id}' has been added to #{class_name} successfully" - else - render_validation_error subject, "Member has not been added to #{class_name}" - end - end - - def remove_member_from_subject(subject) - class_name = subject.class.name.downcase - if member.present? && subject.remove_member(member) - render_json_response subject, "#{member.class.to_s} '#{member.id}' has been removed from #{class_name} successfully" - else - render_validation_error subject, "Member has not been removed from #{class_name}" - end - end - - def destroy_subject(subject) - subject.destroy # later with resque - render_json_response subject, "#{subject.class.name} has been destroyed successfully" - end - - def update_subject(subject) - class_name = subject.class.name - if subject.update_attributes(params[class_name.downcase.to_sym] || {}) - render_json_response subject, "#{class_name} has been updated successfully" - else - render_validation_error subject, "#{class_name} has not been updated" - end - end - - def paginate_params - per_page = params[:per_page].to_i - per_page = 20 if per_page < 1 - per_page = 100 if per_page >100 - {:page => params[:page], :per_page => per_page} - end - - def render_json_response(subject, message, status = 200) - id = status != 200 ? nil : subject.id - - render :json => { - subject.class.name.downcase.to_sym => { - :id => id, - :message => message - } - }.to_json, :status => status - end - - def render_validation_error(subject, message) - errors = subject.errors.full_messages.join('. ') - if errors.present? - message << '. ' << errors - end - render_json_response(subject, message, 422) - end - - private - - def member - return @member if @member - if params[:type] == 'User' - member = User - elsif params[:type] == 'Group' - member = Group - end - @member = member.where(:id => params[:member_id]).first if member - @member ||= '' - end - -end \ No newline at end of file From 9087a15627e071373b372e04b6910c0fe72f8904 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 15 Oct 2012 14:07:10 +0400 Subject: [PATCH 37/39] #672: small refactoring Api::V1::BaseController#member method --- app/controllers/api/v1/base_controller.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index b7fea5dd0..207b4ca8d 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -71,14 +71,10 @@ class Api::V1::BaseController < ApplicationController private def member - return @member if @member - if params[:type] == 'User' - member = User - elsif params[:type] == 'Group' - member = Group + if @member.blank? && %w(User Group).include?(params[:type]) + @member = params[:type].constantize.where(:id => params[:member_id]).first end - @member = member.where(:id => params[:member_id]).first if member - @member ||= '' - end + @member + end end From 3e0b3b8bfc87eab1d52a8286712c2aa26df9593b Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 15 Oct 2012 19:24:20 +0400 Subject: [PATCH 38/39] added invitationlink to the table --- app/views/admin/register_requests/index.html.haml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/views/admin/register_requests/index.html.haml b/app/views/admin/register_requests/index.html.haml index a562479a3..90c609fc0 100644 --- a/app/views/admin/register_requests/index.html.haml +++ b/app/views/admin/register_requests/index.html.haml @@ -24,9 +24,14 @@ %td= request.more %td= request.created_at %td - = link_to t("layout.approve"), approve_admin_register_request_path(request) if can? :approve, request - | - = link_to t("layout.reject"), reject_admin_register_request_path(request) if can? :reject, request + - links = [] + - if can? :approve, request + - links << link_to(t("layout.approve"), approve_admin_register_request_path(request)) + - if can? :reject, request + - links << link_to(t("layout.reject"), reject_admin_register_request_path(request)) + - if request.token + - links << link_to('Link', new_user_registration_url(:invitation_token => request.token)) + = raw links.join('|') .actions %input#approve_registration{:type => 'button', :value => "Approve Selected"} From a7a442c4e1bf06fc3f3059e75298177224177a56 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Mon, 15 Oct 2012 21:24:29 +0600 Subject: [PATCH 39/39] [refs #90] fixed rendering diff --- app/views/projects/pull_requests/_pull_diff.html.haml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/views/projects/pull_requests/_pull_diff.html.haml b/app/views/projects/pull_requests/_pull_diff.html.haml index a5c5c8816..b05bab003 100644 --- a/app/views/projects/pull_requests/_pull_diff.html.haml +++ b/app/views/projects/pull_requests/_pull_diff.html.haml @@ -6,4 +6,5 @@ - if pull_diff.b_path.present? .r= link_to "view file @ #{short_hash_id(commit_id)}", blob_path(@project, commit_id, pull_diff.b_path) .clear - .diff_data= render_diff(pull_diff, pull_diff_counter) unless (Grit::Repo.new(@pull.path).tree(commit_id) / pull_diff.b_path).binary? + -if pull_diff.diff.present? && !(Grit::Repo.new(@pull.path).tree(commit_id) / pull_diff.b_path).binary? + .diff_data= render_diff(pull_diff, pull_diff_counter)