From 3e8d54a4ab190201837b70d641e43c23d4f55d12 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Fri, 14 Dec 2012 00:19:24 +0600 Subject: [PATCH] [refs #777] refactoring comments controller specs --- .../comments_controller_for_commit_spec.rb | 118 ++++-------------- .../projects/comments_controller_spec.rb | 118 ++++-------------- spec/support/shared_examples/comments.rb | 66 +++++++++- 3 files changed, 108 insertions(+), 194 deletions(-) diff --git a/spec/controllers/projects/comments_controller_for_commit_spec.rb b/spec/controllers/projects/comments_controller_for_commit_spec.rb index 8c135ed99..8a1596571 100644 --- a/spec/controllers/projects/comments_controller_for_commit_spec.rb +++ b/spec/controllers/projects/comments_controller_for_commit_spec.rb @@ -1,79 +1,6 @@ # -*- encoding : utf-8 -*- require 'spec_helper' -def create_comment user - FactoryGirl.create(:comment, :user => user, :commentable => @commit, :project => @project) -end - -shared_examples_for 'user with create comment rights for commits' do - it 'should be able to perform create action' do - post :create, @create_params - response.should redirect_to(commit_path(@project, @commit.id)+"#comment#{Comment.last.id}") - end - - it 'should create subscribe object into db' do - lambda{ post :create, @create_params }.should change{ Comment.count }.by(1) - end -end - -shared_examples_for 'user with update own comment rights for commits' do - it 'should be able to perform update action' do - put :update, {:id => @own_comment.id}.merge(@update_params) - response.status.should == 200 - end - - it 'should update subscribe body' do - put :update, {:id => @own_comment.id}.merge(@update_params) - @own_comment.reload.body.should == 'updated' - end -end - -shared_examples_for 'user with update stranger comment rights for commits' do - it 'should be able to perform update action' do - put :update, {:id => @stranger_comment.id}.merge(@update_params) - response.status.should == 200 - end - - it 'should update comment title' do - put :update, {:id => @stranger_comment.id}.merge(@update_params) - @stranger_comment.reload.body.should == 'updated' - end -end - -shared_examples_for 'user without update stranger comment rights for commits' do - it 'should not be able to perform update action' do - put :update, {:id => @stranger_comment.id}.merge(@update_params) - response.should redirect_to(forbidden_path) - end - - it 'should not update comment title' do - put :update, {:id => @stranger_comment.id}.merge(@update_params) - @stranger_comment.reload.body.should_not == 'updated' - end -end - -shared_examples_for 'user without destroy comment rights for commits' do - it 'should not be able to perform destroy action' do - delete :destroy, :id => @stranger_comment.id, :commit_id => @commit.id, :owner_name => @project.owner.uname, :project_name => @project.name - response.should redirect_to(forbidden_path) - end - - it 'should not reduce comments count' do - lambda{ delete :destroy, :id => @stranger_comment.id, :commit_id => @commit.id, :owner_name => @project.owner.uname, :project_name => @project.name }.should change{ Comment.count }.by(0) - end -end - -shared_examples_for 'user with destroy comment rights for commits' do - it 'should be able to perform destroy action' do - delete :destroy, :id => @stranger_comment.id, :commit_id => @commit.id, :owner_name => @project.owner.uname, :project_name => @project.name - response.should redirect_to(commit_path(@project, @commit.id)) - end - - it 'should reduce comments count' do - lambda{ delete :destroy, :id => @stranger_comment.id, :commit_id => @commit.id, :owner_name => @project.owner.uname, :project_name => @project.name }.should change{ Comment.count }.by(-1) - end -end - describe Projects::CommentsController do before(:each) do stub_symlink_methods @@ -85,10 +12,12 @@ describe Projects::CommentsController do @update_params = {:comment => {:body => 'updated'}, :owner_name => @project.owner.uname, :project_name => @project.name, :commit_id => @commit.id} any_instance_of(Project, :versions => ['v1.0', 'v2.0']) - @stranger_comment = create_comment FactoryGirl.create(:user) + @comment = FactoryGirl.create(:comment, :commentable => @commit, :project => @project) @user = FactoryGirl.create(:user) - @own_comment = create_comment @user + @own_comment = FactoryGirl.create(:comment, :commentable => @commit, :user => @user, :project => @project) set_session_for(@user) + @path = {:owner_name => @project.owner.uname, :project_name => @project.name, :commit_id => @commit.id} + @return_path = commit_path(@project, @commit.id) end context 'for project admin user' do @@ -96,25 +25,22 @@ describe Projects::CommentsController do @project.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'admin') end - it_should_behave_like 'user with create comment rights for commits' - it_should_behave_like 'user with update stranger comment rights for commits' - it_should_behave_like 'user with update own comment rights for commits' - it_should_behave_like 'user with destroy comment rights for commits' - #it_should_behave_like 'user with destroy rights' + it_should_behave_like 'user with create comment ability' + it_should_behave_like 'user with update stranger comment ability' + it_should_behave_like 'user with update own comment ability' + it_should_behave_like 'user with destroy comment ability' + #it_should_behave_like 'user with destroy ability' end context 'for project owner user' do before(:each) do - @user.destroy - @user = @project.owner - set_session_for(@user) - @own_comment = create_comment @user + set_session_for(@project.owner) end - it_should_behave_like 'user with create comment rights for commits' - it_should_behave_like 'user with update stranger comment rights for commits' - it_should_behave_like 'user with update own comment rights for commits' - it_should_behave_like 'user with destroy comment rights for commits' + it_should_behave_like 'user with create comment ability' + it_should_behave_like 'user with update stranger comment ability' + it_should_behave_like 'user with update own comment ability' + it_should_behave_like 'user with destroy comment ability' end context 'for project reader user' do @@ -122,10 +48,10 @@ describe Projects::CommentsController do @project.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'reader') end - it_should_behave_like 'user with create comment rights for commits' - it_should_behave_like 'user without update stranger comment rights for commits' - it_should_behave_like 'user with update own comment rights for commits' - it_should_behave_like 'user without destroy comment rights for commits' + it_should_behave_like 'user with create comment ability' + it_should_behave_like 'user without update stranger comment ability' + it_should_behave_like 'user with update own comment ability' + it_should_behave_like 'user without destroy comment ability' end context 'for project writer user' do @@ -133,9 +59,9 @@ describe Projects::CommentsController do @project.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'writer') end - it_should_behave_like 'user with create comment rights for commits' - it_should_behave_like 'user without update stranger comment rights for commits' - it_should_behave_like 'user with update own comment rights for commits' - it_should_behave_like 'user without destroy comment rights for commits' + it_should_behave_like 'user with create comment ability' + it_should_behave_like 'user without update stranger comment ability' + it_should_behave_like 'user with update own comment ability' + it_should_behave_like 'user without destroy comment ability' end end diff --git a/spec/controllers/projects/comments_controller_spec.rb b/spec/controllers/projects/comments_controller_spec.rb index b81445a8b..e25280c95 100644 --- a/spec/controllers/projects/comments_controller_spec.rb +++ b/spec/controllers/projects/comments_controller_spec.rb @@ -6,7 +6,7 @@ shared_context "comments controller" do stub_symlink_methods @project = FactoryGirl.create(:project) - @issue = FactoryGirl.create(:issue, :project_id => @project.id, :user => FactoryGirl.create(:user)) + @issue = FactoryGirl.create(:issue, :project_id => @project.id) @comment = FactoryGirl.create(:comment, :commentable => @issue, :project_id => @project.id) @user = FactoryGirl.create(:user) @@ -14,82 +14,14 @@ shared_context "comments controller" do set_session_for(@user) - @address = {:owner_name => @project.owner.uname, :project_name => @project.name, :issue_id => @issue.serial_id} - @create_params = {:comment => {:body => 'I am a comment!'}}.merge(@address) - @update_params = {:comment => {:body => 'updated'}}.merge(@address) + @path = {:owner_name => @project.owner.uname, :project_name => @project.name, :issue_id => @issue.serial_id} + @return_path = project_issue_path(@project, @issue) + @create_params = {:comment => {:body => 'I am a comment!'}}.merge(@path) + @update_params = {:comment => {:body => 'updated'}}.merge(@path) end end -shared_examples_for 'user with create comment rights' do - it 'should be able to perform create action' do - post :create, @create_params - response.should redirect_to(project_issue_path(@project, @issue)+"#comment#{Comment.last.id}") - end - - it 'should create comment in the database' do - lambda{ post :create, @create_params }.should change{ Comment.count }.by(1) - end -end - -shared_examples_for 'user with update own comment rights' do - it 'should be able to perform update action' do - put :update, {:id => @own_comment.id}.merge(@update_params) - response.status.should == 200 - end - - it 'should update comment body' do - put :update, {:id => @own_comment.id}.merge(@update_params) - @own_comment.reload.body.should == 'updated' - end -end - -shared_examples_for 'user with update stranger comment rights' do - it 'should be able to perform update action' do - put :update, {:id => @comment.id}.merge(@update_params) - response.status.should == 200 - end - - it 'should update comment body' do - put :update, {:id => @comment.id}.merge(@update_params) - @comment.reload.body.should == 'updated' - end -end - -shared_examples_for 'user without update stranger comment rights' do - it 'should not be able to perform update action' do - put :update, {:id => @comment.id}.merge(@update_params) - response.should redirect_to(forbidden_path) - end - - it 'should not update comment body' do - put :update, {:id => @comment.id}.merge(@update_params) - @comment.reload.body.should_not == 'updated' - end -end - -shared_examples_for 'user without destroy comment rights' do - it 'should not be able to perform destroy action' do - delete :destroy, {:id => @comment.id}.merge(@address) - response.should redirect_to(forbidden_path) - end - - it 'should not delete comment from database' do - lambda{ delete :destroy, {:id => @comment.id}.merge(@address)}.should change{ Issue.count }.by(0) - end -end - -shared_examples_for 'user with destroy comment rights' do - it 'should be able to perform destroy action' do - delete :destroy, {:id => @comment.id}.merge(@address) - response.should redirect_to([@project, @issue]) - end - - it 'should delete comment from database' do - lambda{ delete :destroy, {:id => @comment.id}.merge(@address)}.should change{ Comment.count }.by(-1) - end -end - describe Projects::CommentsController do include_context "comments controller" @@ -99,10 +31,10 @@ describe Projects::CommentsController do @user.save end - it_should_behave_like 'user with create comment rights' - it_should_behave_like 'user with update stranger comment rights' - it_should_behave_like 'user with update own comment rights' - it_should_behave_like 'user with destroy comment rights' + it_should_behave_like 'user with create comment ability' + it_should_behave_like 'user with update stranger comment ability' + it_should_behave_like 'user with update own comment ability' + it_should_behave_like 'user with destroy comment ability' end context 'for project admin user' do @@ -110,10 +42,10 @@ describe Projects::CommentsController do @project.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'admin') end - it_should_behave_like 'user with create comment rights' - it_should_behave_like 'user with update stranger comment rights' - it_should_behave_like 'user with update own comment rights' - it_should_behave_like 'user with destroy comment rights' + it_should_behave_like 'user with create comment ability' + it_should_behave_like 'user with update stranger comment ability' + it_should_behave_like 'user with update own comment ability' + it_should_behave_like 'user with destroy comment ability' end context 'for project owner user' do @@ -121,10 +53,10 @@ describe Projects::CommentsController do set_session_for(@project.owner) # owner should be user end - it_should_behave_like 'user with create comment rights' - it_should_behave_like 'user with update stranger comment rights' - it_should_behave_like 'user with update own comment rights' - it_should_behave_like 'user with destroy comment rights' + it_should_behave_like 'user with create comment ability' + it_should_behave_like 'user with update stranger comment ability' + it_should_behave_like 'user with update own comment ability' + it_should_behave_like 'user with destroy comment ability' end context 'for project reader user' do @@ -132,10 +64,10 @@ describe Projects::CommentsController do @project.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'reader') end - it_should_behave_like 'user with create comment rights' - it_should_behave_like 'user without update stranger comment rights' - it_should_behave_like 'user with update own comment rights' - it_should_behave_like 'user without destroy comment rights' + it_should_behave_like 'user with create comment ability' + it_should_behave_like 'user without update stranger comment ability' + it_should_behave_like 'user with update own comment ability' + it_should_behave_like 'user without destroy comment ability' end context 'for project writer user' do @@ -143,9 +75,9 @@ describe Projects::CommentsController do @project.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'writer') end - it_should_behave_like 'user with create comment rights' - it_should_behave_like 'user without update stranger comment rights' - it_should_behave_like 'user with update own comment rights' - it_should_behave_like 'user without destroy comment rights' + it_should_behave_like 'user with create comment ability' + it_should_behave_like 'user without update stranger comment ability' + it_should_behave_like 'user with update own comment ability' + it_should_behave_like 'user without destroy comment ability' end end diff --git a/spec/support/shared_examples/comments.rb b/spec/support/shared_examples/comments.rb index 9080eb85e..4aaff9ea3 100644 --- a/spec/support/shared_examples/comments.rb +++ b/spec/support/shared_examples/comments.rb @@ -36,10 +36,66 @@ shared_examples_for 'user without destroy stranger comment ability (for model)' end end -shared_examples_for 'by default settings' do - it 'should send an e-mail' do - comment = create_comment(@stranger) - ActionMailer::Base.deliveries.count.should == 1 - ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true +shared_examples_for 'user with create comment ability' do + it 'should be able to perform create action' do + post :create, @create_params + response.should redirect_to(@return_path+"#comment#{Comment.last.id}") + end + + it 'should create comment in the database' do + lambda{ post :create, @create_params }.should change{ Comment.count }.by(1) + end +end +shared_examples_for 'user with update own comment ability' do + it 'should be able to perform update action' do + put :update, {:id => @own_comment.id}.merge(@update_params) + response.status.should == 200 + end + + it 'should update subscribe body' do + put :update, {:id => @own_comment.id}.merge(@update_params) + @own_comment.reload.body.should == 'updated' + end +end +shared_examples_for 'user with update stranger comment ability' do + it 'should be able to perform update action' do + put :update, {:id => @comment.id}.merge(@update_params) + response.status.should == 200 + end + + it 'should update comment body' do + put :update, {:id => @comment.id}.merge(@update_params) + @comment.reload.body.should == 'updated' + end +end +shared_examples_for 'user without update stranger comment ability' do + it 'should not be able to perform update action' do + put :update, {:id => @comment.id}.merge(@update_params) + response.should redirect_to(forbidden_path) + end + + it 'should not update comment body' do + put :update, {:id => @comment.id}.merge(@update_params) + @comment.reload.body.should_not == 'updated' + end +end +shared_examples_for 'user with destroy comment ability' do + it 'should be able to perform destroy action' do + delete :destroy, {:id => @comment.id}.merge(@path) + response.should redirect_to(@return_path) + end + + it 'should delete comment from database' do + lambda{ delete :destroy, {:id => @comment.id}.merge(@path)}.should change{ Comment.count }.by(-1) + end +end +shared_examples_for 'user without destroy comment ability' do + it 'should not be able to perform destroy action' do + delete :destroy, {:id => @comment.id}.merge(@path) + response.should redirect_to(forbidden_path) + end + + it 'should not delete comment from database' do + lambda{ delete :destroy, {:id => @comment.id}.merge(@path)}.should change{ Issue.count }.by(0) end end