rosa-build/spec/controllers/platforms/key_pairs_controller_spec.rb

158 lines
4.3 KiB
Ruby
Raw Normal View History

2012-07-13 12:18:12 +01:00
require 'spec_helper'
2012-07-16 17:36:47 +01:00
def create_key_pair(repository, user)
2014-01-21 04:51:49 +00:00
@key_pair = FactoryGirl.create(:key_pair, repository: repository, user: user)
2012-07-16 17:36:47 +01:00
end
shared_examples_for 'key_pair platform owner' do
it 'should be able to perform index action' do
2014-01-21 04:51:49 +00:00
get :index, platform_id: @platform
2012-07-16 17:36:47 +01:00
response.should 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))
end
it 'should create key pair into db on create action' do
lambda { post :create, @create_params }.should change{KeyPair.count}.by(1)
end
context "on destroy" do
before(:each) do
create_key_pair @repository, @user
end
it 'should be able to perform action' do
2014-01-21 04:51:49 +00:00
delete :destroy, platform_id: @platform, id: @key_pair
2012-07-16 17:36:47 +01:00
response.should redirect_to(platform_key_pairs_path(@platform))
end
it 'should delete key pair into db' do
2014-01-21 04:51:49 +00:00
lambda { delete :destroy, platform_id: @platform, id: @key_pair }.should change{KeyPair.count}.by(-1)
2012-07-16 17:36:47 +01:00
end
end
end
shared_examples_for 'key_pair platform reader' do
it 'should be able to perform index action' do
2014-01-21 04:51:49 +00:00
get :index, platform_id: @platform
2012-07-16 17:36:47 +01:00
response.should render_template(:index)
end
it 'should not be able to perform create action' do
post :create, @create_params
response.should 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)
end
context "on destroy" do
before(:each) do
create_key_pair @repository, @user
end
it 'should not be able to perform action' do
2014-01-21 04:51:49 +00:00
delete :destroy, platform_id: @platform, id: @key_pair
2012-07-16 17:36:47 +01:00
response.should redirect_to(forbidden_path)
end
it 'should not change objects count on destroy success' do
2014-01-21 04:51:49 +00:00
lambda { delete :destroy, platform_id: @platform, id: @key_pair }.should change{KeyPair.count}.by(0)
2012-07-16 17:36:47 +01:00
end
end
end
2015-02-19 01:12:08 +00:00
describe Platforms::KeyPairsController, type: :controller do
2012-07-16 17:36:47 +01:00
before(:each) do
stub_symlink_methods
@platform = FactoryGirl.create(:platform)
2014-01-21 04:51:49 +00:00
@repository = FactoryGirl.create(:repository, platform: @platform)
2012-07-16 17:36:47 +01:00
@user = FactoryGirl.create(:user)
2012-12-20 18:50:55 +00:00
kp = FactoryGirl.build(:key_pair)
2012-07-16 17:36:47 +01:00
@create_params = {
2014-01-21 04:51:49 +00:00
platform_id: @platform,
key_pair: {
repository_id: @repository.id,
public: kp.public,
secret: kp.secret
2012-07-16 17:36:47 +01:00
}
}
end
context 'for guest' do
[:index, :create].each do |action|
2012-07-16 17:36:47 +01:00
it "should not be able to perform #{ action } action" do
2014-01-21 04:51:49 +00:00
get action, platform_id: @platform
2012-07-16 17:36:47 +01:00
response.should 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)
end
context 'on destroy' do
before(:each) do
create_key_pair @repository, @user
end
it 'should not change objects count on destroy success' do
2014-01-21 04:51:49 +00:00
lambda { delete :destroy, platform_id: @platform, id: @key_pair }.should change{KeyPair.count}.by(0)
end
it "should not be able to perform destroy action" do
2014-01-21 04:51:49 +00:00
delete :destroy, platform_id: @platform, id: @key_pair
response.should redirect_to(new_user_session_path)
end
2012-07-16 17:36:47 +01:00
end
end
context 'for global admin' do
before(:each) do
@admin = FactoryGirl.create(:admin)
@user = FactoryGirl.create(:user)
set_session_for(@admin)
end
it_should_behave_like 'key_pair platform owner'
end
context 'for owner user' do
before(:each) do
@user = FactoryGirl.create(:user)
set_session_for(@user)
2014-01-21 04:51:49 +00:00
2012-09-06 11:53:03 +01:00
@platform.owner = @user
@platform.save
2012-07-16 17:36:47 +01:00
end
it_should_behave_like 'key_pair platform owner'
end
context 'for admin user' do
before(:each) do
@user = FactoryGirl.create(:user)
set_session_for(@user)
create_relation(@platform, @user, 'admin')
2012-07-16 17:36:47 +01:00
end
it_should_behave_like 'key_pair platform owner'
end
context 'for reader user' do
before(:each) do
@user = FactoryGirl.create(:user)
set_session_for(@user)
create_relation(@platform, @user, 'reader')
2012-07-16 17:36:47 +01:00
end
it_should_behave_like 'key_pair platform reader'
end
2012-07-13 12:18:12 +01:00
end