#671: added specs for API Platforms#clone action
This commit is contained in:
parent
e6bb63db08
commit
d9c583e226
|
@ -18,8 +18,4 @@ class Api::V1::BaseController < ApplicationController
|
||||||
{:page => params[:page], :per_page => per_page}
|
{:page => params[:page], :per_page => per_page}
|
||||||
end
|
end
|
||||||
|
|
||||||
def validation_failed(subject)
|
|
||||||
{:message => "Validation Failed", :errors => subject.errors.messages}.to_json
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,14 +27,9 @@ class Api::V1::PlatformsController < Api::V1::BaseController
|
||||||
platform_params[:released] = p[:released] if p[:released]
|
platform_params[:released] = p[:released] if p[:released]
|
||||||
platform_params[:description] = p[:description] if p[:description]
|
platform_params[:description] = p[:description] if p[:description]
|
||||||
if @platform.update_attributes(p)
|
if @platform.update_attributes(p)
|
||||||
render :json => {
|
render :json => json_response('Platform has been updated successfully')
|
||||||
:platform => {
|
|
||||||
:id => @platform.id,
|
|
||||||
:message => 'Platform has been updated successfully'
|
|
||||||
}
|
|
||||||
}.to_json
|
|
||||||
else
|
else
|
||||||
render :json => validation_failed(@platform), :status => 422
|
render :json => json_response('Platform has not been updated', true), :status => 422
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -44,32 +39,46 @@ class Api::V1::PlatformsController < Api::V1::BaseController
|
||||||
|
|
||||||
def add_member
|
def add_member
|
||||||
if member.present? && @platform.add_member(member)
|
if member.present? && @platform.add_member(member)
|
||||||
render :json => {
|
render :json => json_response("#{member.class.to_s} '#{member.id}' has been added to platform successfully")
|
||||||
:platform => {
|
|
||||||
:id => @platform.id,
|
|
||||||
:message => "#{member.class.to_s} '#{member.id}' has been added to platform successfully"
|
|
||||||
}
|
|
||||||
}.to_json
|
|
||||||
else
|
else
|
||||||
render :json => validation_failed(@platform), :status => 422
|
render :json => json_response('Member has not been added to platform', true), :status => 422
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_member
|
def remove_member
|
||||||
if member.present? && @platform.remove_member(member)
|
if member.present? && @platform.remove_member(member)
|
||||||
render :json => {
|
render :json => json_response("#{member.class.to_s} '#{member.id}' has been removed from platform successfully")
|
||||||
:platform => {
|
|
||||||
:id => @platform.id,
|
|
||||||
:message => "#{member.class.to_s} '#{member.id}' has been removed from platform successfully"
|
|
||||||
}
|
|
||||||
}.to_json
|
|
||||||
else
|
else
|
||||||
render :json => validation_failed(@platform), :status => 422
|
render :json => json_response('Member has not been removed from platform'), :status => 422
|
||||||
end
|
end
|
||||||
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
|
private
|
||||||
|
|
||||||
|
def json_response(message, nullify_id = false)
|
||||||
|
id = nullify_id ? nil : @platform.id
|
||||||
|
{ :platform => {:id => id, :message => message} }.to_json
|
||||||
|
end
|
||||||
|
|
||||||
def member
|
def member
|
||||||
return @member if @member
|
return @member if @member
|
||||||
if params[:type] == 'User'
|
if params[:type] == 'User'
|
||||||
|
|
|
@ -28,6 +28,8 @@ Rosa::Application.routes.draw do
|
||||||
get :members
|
get :members
|
||||||
put :add_member
|
put :add_member
|
||||||
delete :remove_member
|
delete :remove_member
|
||||||
|
post :clone
|
||||||
|
put :clear
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
resources :repositories, :only => [:show]
|
resources :repositories, :only => [:show]
|
||||||
|
|
|
@ -59,7 +59,6 @@ shared_examples_for 'api platform user with writer rights' do
|
||||||
@platform.members.should_not include(member)
|
@platform.members.should_not include(member)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
shared_examples_for 'api platform user without writer rights' do
|
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
|
||||||
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
|
end
|
||||||
|
|
||||||
shared_examples_for 'api platform user with reader rights for hidden platform' do
|
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'
|
||||||
it_should_behave_like 'api platform user with reader rights for hidden platform'
|
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 writer rights'
|
||||||
|
it_should_behave_like 'api platform user with clone rights'
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'for owner user' do
|
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'
|
||||||
it_should_behave_like 'api platform user with reader rights for hidden platform'
|
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 writer rights'
|
||||||
|
it_should_behave_like 'api platform user without clone rights'
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'for reader user' do
|
context 'for reader user' do
|
||||||
|
|
Loading…
Reference in New Issue