#465: Update specs for Platforms::KeyPairsController

This commit is contained in:
Vokhmin Alexey V 2015-04-03 00:47:45 +03:00
parent 2b26e07e95
commit 518e410767
3 changed files with 32 additions and 20 deletions

View File

@ -1,16 +1,14 @@
class Platforms::KeyPairsController < Platforms::BaseController
before_action :authenticate_user!
load_and_authorize_resource :platform
load_and_authorize_resource only: [:create, :destroy]
def index
@key_pair = KeyPair.new
end
def create
@key_pair = KeyPair.new params[:key_pair]
@key_pair.user_id = current_user.id
authorize @key_pair
if @key_pair.save
flash[:notice] = t('flash.key_pairs.saved')
redirect_to platform_key_pairs_path(@key_pair.repository.platform) and return
@ -21,6 +19,7 @@ class Platforms::KeyPairsController < Platforms::BaseController
end
def destroy
authorize @key_pair = @platform.key_pairs.find(params[:id])
if @key_pair.destroy
flash[:notice] = t('flash.key_pairs.destroyed')
else

View File

@ -1,7 +1,8 @@
class KeyPairPolicy < ApplicationPolicy
def create?
key_pair.repository.blank? || local_admin?(record.repository.platform)
return false unless record.repository
is_admin? || local_admin?(record.repository.platform)
end
alias_method :destroy?, :create?

View File

@ -7,16 +7,18 @@ end
shared_examples_for 'key_pair platform owner' do
it 'should be able to perform index action' do
get :index, platform_id: @platform
response.should render_template(:index)
expect(response).to render_template(:index)
end
it 'should be able to perform create action' do
post :create, @create_params
response.should redirect_to(platform_key_pairs_path(@platform))
expect(response).to redirect_to(platform_key_pairs_path(@platform))
end
it 'should create key pair into db on create action' do
lambda { post :create, @create_params }.should change{KeyPair.count}.by(1)
expect do
post :create, @create_params
end.to change(KeyPair, :count).by(1)
end
context "on destroy" do
@ -26,11 +28,13 @@ shared_examples_for 'key_pair platform owner' do
it 'should be able to perform action' do
delete :destroy, platform_id: @platform, id: @key_pair
response.should redirect_to(platform_key_pairs_path(@platform))
expect(response).to redirect_to(platform_key_pairs_path(@platform))
end
it 'should delete key pair into db' do
lambda { delete :destroy, platform_id: @platform, id: @key_pair }.should change{KeyPair.count}.by(-1)
expect do
delete :destroy, platform_id: @platform, id: @key_pair
end.to change(KeyPair, :count).by(-1)
end
end
end
@ -38,16 +42,18 @@ end
shared_examples_for 'key_pair platform reader' do
it 'should be able to perform index action' do
get :index, platform_id: @platform
response.should render_template(:index)
expect(response).to render_template(:index)
end
it 'should not be able to perform create action' do
post :create, @create_params
response.should redirect_to(forbidden_path)
expect(response).to redirect_to(forbidden_path)
end
it 'should not change objects count on create success' do
lambda { post :create, @create_params }.should change{ KeyPair.count }.by(0)
expect do
post :create, @create_params
end.to_not change(KeyPair, :count)
end
context "on destroy" do
@ -57,11 +63,13 @@ shared_examples_for 'key_pair platform reader' do
it 'should not be able to perform action' do
delete :destroy, platform_id: @platform, id: @key_pair
response.should redirect_to(forbidden_path)
expect(response).to redirect_to(forbidden_path)
end
it 'should not change objects count on destroy success' do
lambda { delete :destroy, platform_id: @platform, id: @key_pair }.should change{KeyPair.count}.by(0)
expect do
delete :destroy, platform_id: @platform, id: @key_pair
end.to_not change(KeyPair, :count)
end
end
end
@ -88,12 +96,14 @@ describe Platforms::KeyPairsController, type: :controller do
[:index, :create].each do |action|
it "should not be able to perform #{ action } action" do
get action, platform_id: @platform
response.should redirect_to(new_user_session_path)
expect(response).to redirect_to(new_user_session_path)
end
end
it 'should not change objects count on create success' do
lambda { post :create, @create_params }.should change{ KeyPair.count }.by(0)
expect do
post :create, @create_params
end.to_not change(KeyPair, :count)
end
context 'on destroy' do
@ -102,12 +112,14 @@ describe Platforms::KeyPairsController, type: :controller do
end
it 'should not change objects count on destroy success' do
lambda { delete :destroy, platform_id: @platform, id: @key_pair }.should change{KeyPair.count}.by(0)
expect do
delete :destroy, platform_id: @platform, id: @key_pair
end.to_not change(KeyPair, :count)
end
it "should not be able to perform destroy action" do
delete :destroy, platform_id: @platform, id: @key_pair
response.should redirect_to(new_user_session_path)
expect(response).to redirect_to(new_user_session_path)
end
end
end