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