From ff9bfa3f9d0e59294d0d74ee803584afaf091355 Mon Sep 17 00:00:00 2001 From: "konstantin.grabar" Date: Tue, 29 Nov 2011 18:36:51 +0400 Subject: [PATCH] [refs #2249] Add corrections to rspec controllers tests --- db/schema.rb | 11 ++- lib/modules/models/personal_repository.rb | 4 +- .../collaborators_controller_spec.rb | 76 ++++----------- spec/controllers/platforms_controller_spec.rb | 64 +++---------- spec/controllers/projects_controller_spec.rb | 96 +++++-------------- spec/factories/platform_factory.rb | 6 +- spec/factories/project_factory.rb | 6 +- spec/factories/repository_factory.rb | 10 +- .../collaborators_controller.rb | 39 ++++++++ spec/shared_examples/platforms_controller.rb | 34 +++++++ spec/shared_examples/projects_controller.rb | 40 ++++++++ 11 files changed, 186 insertions(+), 200 deletions(-) create mode 100644 spec/shared_examples/collaborators_controller.rb create mode 100644 spec/shared_examples/platforms_controller.rb create mode 100644 spec/shared_examples/projects_controller.rb diff --git a/db/schema.rb b/db/schema.rb index 74e364bc6..1e6684487 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -220,6 +220,7 @@ ActiveRecord::Schema.define(:version => 20111128140341) do t.string "object_type" t.integer "target_id" t.string "target_type" + t.integer "role_id" t.datetime "created_at" t.datetime "updated_at" t.string "role" @@ -248,16 +249,16 @@ ActiveRecord::Schema.define(:version => 20111128140341) do create_table "users", :force => true do |t| t.string "name" - t.string "email", :default => "", :null => false - t.string "encrypted_password", :limit => 128, :default => "", :null => false - t.string "password_salt", :default => "", :null => false + t.string "email", :default => "", :null => false + t.string "encrypted_password", :limit => 128, :default => "", :null => false t.string "reset_password_token" - t.string "remember_token" + t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at" t.datetime "updated_at" - t.text "ssh_key" t.string "uname" + t.text "ssh_key" + t.integer "role_id" t.string "role" end diff --git a/lib/modules/models/personal_repository.rb b/lib/modules/models/personal_repository.rb index db0b41df5..1487f58df 100644 --- a/lib/modules/models/personal_repository.rb +++ b/lib/modules/models/personal_repository.rb @@ -12,7 +12,7 @@ module Modules pl = platforms.build pl.owner = self pl.name = "#{self.uname}_personal" - pl.name = "#{self.uname}_personal" + pl.description = "#{self.uname}_personal" pl.platform_type = 'personal' pl.distrib_type = APP_CONFIG['distr_types'].first pl.visibility = 'hidden' @@ -21,7 +21,7 @@ module Modules rep = pl.repositories.build rep.owner = pl.owner rep.name = 'main' - rep.name = 'main' + rep.description = 'main' rep.save! end diff --git a/spec/controllers/collaborators_controller_spec.rb b/spec/controllers/collaborators_controller_spec.rb index 729f7a891..0b82106c9 100644 --- a/spec/controllers/collaborators_controller_spec.rb +++ b/spec/controllers/collaborators_controller_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'shared_examples/collaborators_controller' describe CollaboratorsController do before(:each) do @@ -25,20 +26,9 @@ describe CollaboratorsController do set_session_for(@admin) end - it 'should be able to perform index action' do - get :index, :project_id => @project.id - response.should redirect_to(edit_project_collaborators_path(@project)) - end - - it 'should be able to perform update action' do - post :update, {:project_id => @project.id}.merge(@update_params) - response.should redirect_to(project_path(@project)) - end - - it 'should set flash notice on update success' do - post :update, {:project_id => @project.id}.merge(@update_params) - flash[:notice].should_not be_blank - end + it_should_behave_like 'be_able_to_perform_index_action' + it_should_behave_like 'be_able_to_perform_update_action' + it_should_behave_like 'update_collaborator_relation' end context 'for admin user' do @@ -50,20 +40,9 @@ describe CollaboratorsController do r.save! end - it 'should be able to perform index action' do - get :index, :project_id => @project.id - response.should redirect_to(edit_project_collaborators_path(@project)) - end - - it 'should be able to perform update action' do - post :update, {:project_id => @project.id}.merge(@update_params) - response.should redirect_to(project_path(@project)) - end - - it 'should set flash notice on update success' do - post :update, {:project_id => @project.id}.merge(@update_params) - flash[:notice].should_not be_blank - end + it_should_behave_like 'be_able_to_perform_index_action' + it_should_behave_like 'be_able_to_perform_update_action' + it_should_behave_like 'update_collaborator_relation' end context 'for owner user' do @@ -75,20 +54,9 @@ describe CollaboratorsController do r.save! end - it 'should be able to perform index action' do - get :index, :project_id => @project.id - response.should redirect_to(edit_project_collaborators_path(@project)) - end - - it 'should be able to perform update action' do - post :update, {:project_id => @project.id}.merge(@update_params) - response.should redirect_to(project_path(@project)) - end - - it 'should set flash notice on update success' do - post :update, {:project_id => @project.id}.merge(@update_params) - flash[:notice].should_not be_blank - end + it_should_behave_like 'be_able_to_perform_index_action' + it_should_behave_like 'be_able_to_perform_update_action' + it_should_behave_like 'update_collaborator_relation' end context 'for reader user' do @@ -100,15 +68,9 @@ describe CollaboratorsController do r.save! end - it 'should not be able to perform index action' do - get :index, :project_id => @project.id - response.should redirect_to(edit_project_collaborators_path(@project)) - end - - it 'should not be able to perform update action' do - post :update, {:project_id => @project.id}.merge(@update_params) - response.should redirect_to(project_path(@project)) - end + it_should_behave_like 'not_be_able_to_perform_index_action' + it_should_behave_like 'not_be_able_to_perform_update_action' + it_should_behave_like 'not_update_collaborator_relation' end context 'for writer user' do @@ -120,14 +82,8 @@ describe CollaboratorsController do r.save! end - it 'should not be able to perform index action' do - get :index, :project_id => @project.id - response.should redirect_to(edit_project_collaborators_path(@project)) - end - - it 'should not be able to perform update action' do - post :update, {:project_id => @project.id}.merge(@update_params) - response.should redirect_to(project_path(@project)) - end + it_should_behave_like 'not_be_able_to_perform_index_action' + it_should_behave_like 'not_be_able_to_perform_update_action' + it_should_behave_like 'not_update_collaborator_relation' end end diff --git a/spec/controllers/platforms_controller_spec.rb b/spec/controllers/platforms_controller_spec.rb index b114d822a..e691dacac 100644 --- a/spec/controllers/platforms_controller_spec.rb +++ b/spec/controllers/platforms_controller_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require "shared_examples/platforms_controller" describe PlatformsController do before(:each) do @@ -7,7 +8,7 @@ describe PlatformsController do @user = Factory(:user) @create_params = {:platform => { :name => 'pl1', - :unixname => 'pl1', + :description => 'pl1', :platform_type => 'main', :distrib_type => APP_CONFIG['distr_types'].first }} @@ -40,10 +41,7 @@ describe PlatformsController do set_session_for(@admin) end - it 'should be able to perform index action' do - get :index - response.should render_template(:index) - end + it_should_behave_like 'able_to_perform_index_action' it 'should be able to perform new action' do get :new @@ -55,20 +53,13 @@ describe PlatformsController do response.should redirect_to(platform_path(Platform.last)) end - it 'should set flash notice on create success' do - post :create, @create_params - flash[:notice].should_not be_blank + it 'should change objects count on create success' do + lambda { post :create, @create_params }.should change{ Platform.count }.by(1) end - it 'should be able to perform destroy action' do - delete :destroy, :id => @platform.id - response.should redirect_to(root_path) - end - - it 'should not be able to destroy personal platform' do - delete :destroy, :id => @personal_platform.id - response.should redirect_to(forbidden_path) - end + it_should_behave_like 'be_able_to_perform_destroy_action' + it_should_behave_like 'change_objects_count_on_destroy_success' + it_should_behave_like 'not_be_able_to_destroy_personal_platform' end context 'for owner user' do @@ -80,10 +71,11 @@ describe PlatformsController do r.save! end - it 'should be able to perform index action' do - get :index - response.should render_template(:index) - end + it_should_behave_like 'able_to_perform_index_action' + it_should_behave_like 'not_be_able_to_perform_create_action' + it_should_behave_like 'be_able_to_perform_destroy_action' + it_should_behave_like 'change_objects_count_on_destroy_success' + it_should_behave_like 'not_be_able_to_destroy_personal_platform' it 'should be able to perform new action' do get :new @@ -95,20 +87,6 @@ describe PlatformsController do response.should redirect_to(forbidden_path) end - it 'should set flash notice on create success' do - post :create, @create_params - response.should redirect_to(forbidden_path) - end - - it 'should be able to perform destroy action' do - delete :destroy, :id => @platform.id - response.should redirect_to(root_path) - end - - it 'should not be able to destroy personal platform' do - delete :destroy, :id => @personal_platform.id - response.should redirect_to(forbidden_path) - end end context 'for reader user' do @@ -119,20 +97,8 @@ describe PlatformsController do r.save! end - it 'should be able to perform index action' do - get :index - 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 set flash notice on create success' do - post :create, @create_params - response.should redirect_to(forbidden_path) - end + it_should_behave_like 'able_to_perform_index_action' + it_should_behave_like 'not_be_able_to_perform_create_action' it 'should not be able to perform destroy action' do delete :destroy, :id => @platform.id diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb index 95387ea27..acd3260e7 100644 --- a/spec/controllers/projects_controller_spec.rb +++ b/spec/controllers/projects_controller_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'shared_examples/projects_controller' describe ProjectsController do before(:each) do @@ -36,35 +37,20 @@ describe ProjectsController do set_session_for(@admin) end - it 'should be able to perform index action' do - get :index - response.should render_template(:index) - end - - it 'should be able to perform update action' do - put :update, {:id => @project.id}.merge(@update_params) - response.should redirect_to(project_path(@project)) - end - - it 'should set flash notice on update success' do - put :update, {:id => @project.id}.merge(@update_params) - flash[:notice].should_not be_blank - end + it_should_behave_like 'be_able_to_perform_index_action' + it_should_behave_like 'be_able_to_perform_update_action' + it_should_behave_like 'update_collaborator_relation' it 'should be able to perform create action' do post :create, @create_params response.should redirect_to(project_path( Project.last.id )) end - it 'should set flash notice on create success' do - post :create, @create_params - flash[:notice].should_not be_blank + it 'should change objects count on create' do + lambda { post :create, @create_params }.should change{ Project.count }.by(1) end - it 'should be able to fork project' do - post :fork, :id => @project.id - response.should redirect_to(project_path(Project.last)) - end + it_should_behave_like 'be_able_to_fork_project' end context 'for owner user' do @@ -76,31 +62,20 @@ describe ProjectsController do r.save! end - it 'should be able to perform update action' do - put :update, {:id => @project.id}.merge(@update_params) - response.should redirect_to(project_path(@project)) - end - - it 'should set flash notice on update success' do - put :update, {:id => @project.id}.merge(@update_params) - flash[:notice].should_not be_blank - end - - it 'should be able to perform build action' do - get :build, :id => @project.id - response.should render_template(:build) - end - - it 'should be able to perform process_build action' do - post :process_build, {:id => @project.id}.merge(@process_build_params) - response.should redirect_to(project_path(@project)) - end + it_should_behave_like 'be_able_to_perform_update_action' + it_should_behave_like 'update_collaborator_relation' + it_should_behave_like 'be_able_to_perform_build_action' + it_should_behave_like 'be_able_to_perform_process_build_action' it 'should be able to perform destroy action' do delete :destroy, {:id => @project.id} response.should redirect_to(@project.owner) end + it 'should change objects count on destroy' do + lambda { post :create, @create_params }.should change{ Project.count }.by(-1) + end + it 'should not be able to fork project' do post :fork, :id => @project.id response.should redirect_to(forbidden_path) @@ -115,20 +90,14 @@ describe ProjectsController do r.save! end - it 'should not be able to perform index action' do - get :index - response.should render_template(:index) - end + it_should_behave_like 'be_able_to_perform_index_action' - it 'should not be able to perform show action' do + it 'should be able to perform show action' do get :show, :id => @project.id response.should render_template(:show) end - it 'should be able to fork project' do - post :fork, :id => @project.id - response.should redirect_to(project_path(Project.last)) - end + it_should_behave_like 'be_able_to_fork_project' end context 'for writer user' do @@ -139,29 +108,10 @@ describe ProjectsController do r.save! end - it 'should not be able to perform update action' do - put :update, {:id => @project.id}.merge(@update_params) - response.should redirect_to(project_path(@project)) - end - - it 'should set flash notice on update success' do - put :update, {:id => @project.id}.merge(@update_params) - flash[:notice].should_not be_blank - end - - it 'shoud be able to perform build action' do - get :build, :id => @project.id - response.should render_template(:build) - end - - it 'shoud be able to perform process_build action' do - post :process_build, {:id => @project.id}.merge(@process_build_params) - response.should redirect_to(project_path(@project)) - end - - it 'should be able to fork project' do - post :fork, :id => @project.id - response.should redirect_to(project_path(Project.last)) - end + it_should_behave_like 'be_able_to_perform_update_action' + it_should_behave_like 'update_collaborator_relation' + it_should_behave_like 'be_able_to_perform_build_action' + it_should_behave_like 'be_able_to_perform_process_build_action' + it_should_behave_like 'be_able_to_fork_project' end end diff --git a/spec/factories/platform_factory.rb b/spec/factories/platform_factory.rb index 040f97a7a..9fab7a443 100644 --- a/spec/factories/platform_factory.rb +++ b/spec/factories/platform_factory.rb @@ -1,7 +1,7 @@ Factory.define(:platform) do |p| - p.name { Factory.next(:string) } - p.unixname { Factory.next(:unixname) } + p.description { Factory.next(:string) } + p.name { Factory.next(:unixname) } p.platform_type 'main' p.distrib_type APP_CONFIG['distr_types'].first p.association :owner, :factory => :user -end \ No newline at end of file +end diff --git a/spec/factories/project_factory.rb b/spec/factories/project_factory.rb index 54e9aeecb..66adbdea9 100644 --- a/spec/factories/project_factory.rb +++ b/spec/factories/project_factory.rb @@ -1,5 +1,5 @@ Factory.define(:project) do |p| - p.name { Factory.next(:string) } - p.unixname { Factory.next(:unixname) } + p.description { Factory.next(:string) } + p.name { Factory.next(:unixname) } p.association :owner, :factory => :user -end \ No newline at end of file +end diff --git a/spec/factories/repository_factory.rb b/spec/factories/repository_factory.rb index 152e7203d..a730f7f4b 100644 --- a/spec/factories/repository_factory.rb +++ b/spec/factories/repository_factory.rb @@ -1,17 +1,17 @@ Factory.define(:repository) do |p| - p.name { Factory.next(:string) } - p.unixname { Factory.next(:unixname) } + p.description { Factory.next(:string) } + p.name { Factory.next(:unixname) } p.association :platform, :factory => :platform p.association :owner, :factory => :user end Factory.define(:personal_repository, :class => Repository) do |p| - p.name { Factory.next(:string) } - p.unixname { Factory.next(:unixname) } + p.description { Factory.next(:string) } + p.name { Factory.next(:unixname) } p.association :platform, :factory => :platform p.association :owner, :factory => :user p.after_create { |rep| rep.platform.platform_type = 'personal' } -end \ No newline at end of file +end diff --git a/spec/shared_examples/collaborators_controller.rb b/spec/shared_examples/collaborators_controller.rb new file mode 100644 index 000000000..a0a6a2d43 --- /dev/null +++ b/spec/shared_examples/collaborators_controller.rb @@ -0,0 +1,39 @@ +shared_examples_for 'be_able_to_perform_index_action' do + it 'should be able to perform index action' do + get :index, :project_id => @project.id + response.should redirect_to(edit_project_collaborators_path(@project)) + end +end + +shared_examples_for 'be_able_to_perform_update_action' do + it 'should be able to perform update action' do + post :update, {:project_id => @project.id}.merge(@update_params) + response.should redirect_to(project_path(@project)) + end +end + +shared_examples_for 'update_collaborator_relation' do + it 'should update collaborator relation' do + @another_user.relations.exists? :target_id => @project.id, :target_type => 'Project', :role => 'read' + end +end + +shared_examples_for 'not_be_able_to_perform_index_action' do + it 'should be able to perform index action' do + get :index, :project_id => @project.id + response.should redirect_to(edit_project_collaborators_path(@project)) + end +end + +shared_examples_for 'not_be_able_to_perform_update_action' do + it 'should be able to perform update action' do + post :update, {:project_id => @project.id}.merge(@update_params) + response.should redirect_to(project_path(@project)) + end +end + +shared_examples_for 'not_update_collaborator_relation' do + it 'should set flash notice on update success' do + !@another_user.relations.exists? :target_id => @project.id, :target_type => 'Project', :role => 'read' + end +end diff --git a/spec/shared_examples/platforms_controller.rb b/spec/shared_examples/platforms_controller.rb new file mode 100644 index 000000000..890e63b9e --- /dev/null +++ b/spec/shared_examples/platforms_controller.rb @@ -0,0 +1,34 @@ +shared_examples_for 'able_to_perform_index_action' do + it 'should be able to perform index action' do + get :index + response.should render_template(:index) + end +end + +shared_examples_for 'not_be_able_to_perform_create_action' do + it 'should be able to perform create action' do + post :create, @create_params + response.should redirect_to(forbidden_path) + end +end + +shared_examples_for 'not_be_able_to_destroy_personal_platform' do + it 'should be able to perform create action' do + delete :destroy, :id => @personal_platform.id + response.should redirect_to(forbidden_path) + end +end + +shared_examples_for 'change_objects_count_on_destroy_success' do + it 'should change objects count on destroy success' do + lambda { delete :destroy, :id => @platform.id }.should change{ Platform.count }.by(-1) + end +end + +shared_examples_for 'be_able_to_perform_destroy_action' do + it 'should be able to perform destroy action' do + delete :destroy, :id => @platform.id + response.should redirect_to(root_path) + end + +end diff --git a/spec/shared_examples/projects_controller.rb b/spec/shared_examples/projects_controller.rb new file mode 100644 index 000000000..4f391afa1 --- /dev/null +++ b/spec/shared_examples/projects_controller.rb @@ -0,0 +1,40 @@ +shared_examples_for 'be_able_to_perform_index_action' do + it 'should be able to perform index action' do + get :index + response.should render_template(:index) + end +end + +shared_examples_for 'be_able_to_perform_update_action' do + it 'should be able to perform update action' do + put :update, {:project_id => @project.id}.merge(@update_params) + response.should redirect_to(project_path(@project)) + end +end + +shared_examples_for 'update_collaborator_relation' do + it 'should update collaborator relation' do + @another_user.relations.exists? :target_id => @project.id, :target_type => 'Project', :role => 'read' + end +end + +shared_examples_for 'be_able_to_fork_project' do + it 'should be able to fork project' do + post :fork, :id => @project.id + response.should redirect_to(project_path(Project.last)) + end +end + +shared_examples_for 'be_able_to_perform_build_action' do + it 'should be able to perform build action' do + get :build, :id => @project.id + response.should render_template(:build) + end +end + +shared_examples_for 'be_able_to_perform_process_build_action' do + it 'should be able to perform process_build action' do + post :process_build, {:id => @project.id}.merge(@process_build_params) + response.should redirect_to(project_path(@project)) + end +end