From fcc17da1ad66433b685fc720da43e52124c4820d Mon Sep 17 00:00:00 2001 From: "konstantin.grabar" Date: Tue, 6 Dec 2011 19:24:04 +0400 Subject: [PATCH] [refs #2249] Add products specs. Fix some things in other specs --- app/controllers/collaborators_controller.rb | 2 +- .../collaborators_controller_spec.rb | 12 +- .../personal_repositories_controller_spec.rb | 9 +- spec/controllers/platforms_controller_spec.rb | 6 +- spec/controllers/products_controller_spec.rb | 115 ++++++++++++++++++ spec/controllers/projects_controller_spec.rb | 9 +- .../repositories_controller_spec.rb | 6 +- spec/spec_helper.rb | 2 +- 8 files changed, 131 insertions(+), 30 deletions(-) diff --git a/app/controllers/collaborators_controller.rb b/app/controllers/collaborators_controller.rb index 2eb1cf4fd..e7a42e304 100644 --- a/app/controllers/collaborators_controller.rb +++ b/app/controllers/collaborators_controller.rb @@ -30,7 +30,7 @@ class CollaboratorsController < ApplicationController def update all_user_ids = [] all_groups_ids = [] - puts params.inspect + Relation::ROLES.each { |r| all_user_ids = all_user_ids | params['user'][r.to_sym].keys if params['user'][r.to_sym] all_groups_ids = all_groups_ids | params['group'][r.to_sym].keys if params['group'][r.to_sym] diff --git a/spec/controllers/collaborators_controller_spec.rb b/spec/controllers/collaborators_controller_spec.rb index fbf47a2bb..2e2b80ffc 100644 --- a/spec/controllers/collaborators_controller_spec.rb +++ b/spec/controllers/collaborators_controller_spec.rb @@ -36,8 +36,7 @@ describe CollaboratorsController do @user = Factory(:user) @user.relations set_session_for(@user) - r = @project.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'reader') - r.save! + @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader') end it_should_behave_like 'be_able_to_perform_index#collaborators' @@ -50,8 +49,7 @@ describe CollaboratorsController do @user = Factory(:user) set_session_for(@user) @project.update_attribute(:owner, @user) - r = @project.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'admin') - r.save! + @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') end it_should_behave_like 'be_able_to_perform_index#collaborators' @@ -64,8 +62,7 @@ describe CollaboratorsController do @user = Factory(:user) set_session_for(@user) @project.update_attribute(:owner, @user) - r = @project.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'reader') - r.save! + @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader') end it_should_behave_like 'not_be_able_to_perform_index#collaborators' @@ -78,8 +75,7 @@ describe CollaboratorsController do @user = Factory(:user) set_session_for(@user) @project.update_attribute(:owner, @user) - r = @project.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'writer') - r.save! + @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'writer') end it_should_behave_like 'not_be_able_to_perform_index#collaborators' diff --git a/spec/controllers/personal_repositories_controller_spec.rb b/spec/controllers/personal_repositories_controller_spec.rb index edb7c4753..a944ad78a 100644 --- a/spec/controllers/personal_repositories_controller_spec.rb +++ b/spec/controllers/personal_repositories_controller_spec.rb @@ -50,12 +50,10 @@ describe PersonalRepositoriesController do set_session_for(@user) @repository.update_attribute(:owner, @user) - r = @repository.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'admin') - r.save! + @repository.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') @repository.platform.update_attribute(:owner, @user) - p = @repository.platform.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'admin') - p.save! + @repository.platform.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') end it_should_behave_like 'be_able_to_perform_settings#personal_repositories' @@ -73,8 +71,7 @@ describe PersonalRepositoriesController do before(:each) do @user = Factory(:user) set_session_for(@user) - r = @repository.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'reader') - r.save! + @repository.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader') end it_should_behave_like 'be_able_to_perform_show#personal_repositories' diff --git a/spec/controllers/platforms_controller_spec.rb b/spec/controllers/platforms_controller_spec.rb index f5c2b2bbb..935f0298a 100644 --- a/spec/controllers/platforms_controller_spec.rb +++ b/spec/controllers/platforms_controller_spec.rb @@ -67,8 +67,7 @@ describe PlatformsController do @user = Factory(:user) set_session_for(@user) @platform.update_attribute(:owner, @user) - r = @platform.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'admin') - r.save! + @platform.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') end it_should_behave_like 'able_to_perform_index#platforms' @@ -93,8 +92,7 @@ describe PlatformsController do before(:each) do @user = Factory(:user) set_session_for(@user) - r = @platform.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'reader') - r.save! + @platform.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader') end it_should_behave_like 'able_to_perform_index#platforms' diff --git a/spec/controllers/products_controller_spec.rb b/spec/controllers/products_controller_spec.rb index 77af8e7cd..4dc30c407 100644 --- a/spec/controllers/products_controller_spec.rb +++ b/spec/controllers/products_controller_spec.rb @@ -1,5 +1,120 @@ require 'spec_helper' describe ProductsController do + before(:each) do + @another_user = Factory(:user) + @platform = Factory(:platform) + @product = Factory(:product, :platform => @platform) + @create_params = {:product => {:name => 'pro'}, :platform_id => @platform.id} + @update_params = {:product => {:name => 'pro2'}, :platform_id => @platform.id} + end + + context 'for guest' do + [:create].each do |action| + it "should not be able to perform #{ action } action" do + get action, :platform_id => @platform.id + response.should redirect_to(new_user_session_path) + end + end + + [:show, :new, :edit, :update, :destroy].each do |action| + it "should not be able to perform #{ action } action" do + get action, :id => @product.id, :platform_id => @platform.id + response.should redirect_to(new_user_session_path) + end + end + end + + context 'for global admin' do + before(:each) do + @admin = Factory(:admin) + set_session_for(@admin) + end + + it 'should be able to perform create action' do + post :create, @create_params + response.should redirect_to(platform_path( Product.last.platform.id )) + end + + it 'should change objects count on create' do + lambda { post :create, @create_params }.should change{ Product.count }.by(1) + end + + it 'should be able to perform update action' do + put :update, {:id => @product.id}.merge(@update_params) + response.should redirect_to(platform_path(@platform)) + end + + it 'should change objects count on destroy success' do + lambda { delete :destroy, :id => @product.id, :platform_id => @platform }.should change{ Product.count }.by(-1) + end + + it 'should be able to perform destroy action' do + delete :destroy, :platform_id => @platform.id, :id => @product.id + response.should redirect_to(platform_path(@platform)) + end + end + + context 'for admin relation user' do + before(:each) do + @user = Factory(:user) + set_session_for(@user) + r = @product.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') + r = @platform.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') + end + + it 'should be able to perform create action' do + post :create, @create_params + response.should redirect_to(platform_path( Product.last.platform.id )) + end + + it 'should change objects count on create' do + lambda { post :create, @create_params }.should change{ Product.count }.by(1) + end + + it 'should be able to perform update action' do + put :update, {:id => @product.id}.merge(@update_params) + response.should redirect_to(platform_path(@platform)) + end + + it 'should change objects count on destroy success' do + lambda { delete :destroy, :id => @product.id, :platform_id => @platform }.should change{ Product.count }.by(-1) + end + + it 'should be able to perform destroy action' do + delete :destroy, :platform_id => @platform.id, :id => @product.id + response.should redirect_to(platform_path(@platform)) + end + end + + context 'for no relation user' do + before(:each) do + @user = Factory(:user) + set_session_for(@user) + 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' do + lambda { post :create, @create_params }.should change{ Product.count }.by(0) + end + + it 'should not be able to perform update action' do + put :update, {:id => @product.id}.merge(@update_params) + response.should redirect_to(forbidden_path) + end + + it 'should not change objects count on destroy success' do + lambda { delete :destroy, :id => @product.id, :platform_id => @platform }.should change{ Product.count }.by(0) + end + + it 'should not be able to perform destroy action' do + delete :destroy, :platform_id => @platform.id, :id => @product.id + response.should redirect_to(forbidden_path) + end + end end diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb index 7894a925f..dba60a9ca 100644 --- a/spec/controllers/projects_controller_spec.rb +++ b/spec/controllers/projects_controller_spec.rb @@ -58,8 +58,7 @@ describe ProjectsController do @user = Factory(:user) set_session_for(@user) @project.update_attribute(:owner, @user) - r = @project.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'admin') - r.save! + @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') end it_should_behave_like 'be_able_to_perform_update#projects' @@ -86,8 +85,7 @@ describe ProjectsController do before(:each) do @user = Factory(:user) set_session_for(@user) - r = @project.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'reader') - r.save! + @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader') end it_should_behave_like 'be_able_to_perform_index#projects' @@ -104,8 +102,7 @@ describe ProjectsController do before(:each) do @user = Factory(:user) set_session_for(@user) - r = @project.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'writer') - r.save! + @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'writer') end it_should_behave_like 'be_able_to_perform_update#projects' diff --git a/spec/controllers/repositories_controller_spec.rb b/spec/controllers/repositories_controller_spec.rb index 45849c3bf..6521b6292 100644 --- a/spec/controllers/repositories_controller_spec.rb +++ b/spec/controllers/repositories_controller_spec.rb @@ -88,8 +88,7 @@ describe RepositoriesController do @user = Factory(:user) set_session_for(@user) @repository.update_attribute(:owner, @user) - r = @repository.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'admin') - r.save! + @repository.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') end it_should_behave_like 'be_able_to_perform_index#repositories' @@ -107,8 +106,7 @@ describe RepositoriesController do before(:each) do @user = Factory(:user) set_session_for(@user) - r = @repository.relations.build(:object_type => 'User', :object_id => @user.id, :role => 'reader') - r.save! + @repository.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader') end it_should_behave_like 'be_able_to_perform_index#repositories' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b316a1d9b..a7804d8f5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -35,4 +35,4 @@ end # Add testing root_path %x(rm -Rf #{Rails.root}/tmp/test_root) %x(mkdir -p #{Rails.root}/tmp/test_root) -APP_CONFIG['root_path'] = "#{Rails.root}/tmp/test_root" \ No newline at end of file +APP_CONFIG['root_path'] = "#{Rails.root}/tmp/test_root"