From 3f72082e0ec4511423f8beacdf551a0db2da7ea0 Mon Sep 17 00:00:00 2001 From: "konstantin.grabar" Date: Fri, 30 Dec 2011 18:21:08 +0400 Subject: [PATCH] [refs #54] Add comments, subscribes models specs. Finish subscribes controller specs. Some ability.rb fixes --- app/models/ability.rb | 2 + .../controllers/subscribes_controller_spec.rb | 86 +++++++++--- spec/models/comment_spec.rb | 122 +++++++++++++++++- spec/models/subscribe_spec.rb | 74 ++++++++++- 4 files changed, 262 insertions(+), 22 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index a0bd61c8d..1b7509a2b 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -13,6 +13,8 @@ class Ability if user.admin? can :manage, :all + cannot :destroy, Subscribe + cannot :create, Subscribe else # Shared rights between guests and registered users can :forbidden, Platform diff --git a/spec/controllers/subscribes_controller_spec.rb b/spec/controllers/subscribes_controller_spec.rb index 7358127ee..378af911d 100644 --- a/spec/controllers/subscribes_controller_spec.rb +++ b/spec/controllers/subscribes_controller_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -shared_examples_for 'user with create subscribe rights' do +shared_examples_for 'can subscribe' do it 'should be able to perform create action' do post :create, @create_params response.should redirect_to(project_issue_path(@project, @issue)) @@ -11,14 +11,42 @@ shared_examples_for 'user with create subscribe rights' do end end -shared_examples_for 'user without destroy subscribe rights' do +shared_examples_for 'can not subscribe' do + it 'should not be able to perform create action' do + post :create, @create_params + response.should redirect_to(forbidden_path) + end + + it 'should not create subscribe object into db' do + lambda{ post :create, @create_params }.should change{ Subscribe.count }.by(0) + end +end + +shared_examples_for 'can unsubscribe' do + it 'should be able to perform destroy action' do + #set_objects_to_destroy + delete :destroy, @destroy_params + + response.should redirect_to([@project, @issue]) + end + + it 'should reduce subscribes count' do + #set_objects_to_destroy + lambda{ delete :destroy, @destroy_params }.should change{ Subscribe.count }.by(-1) + end +end + +shared_examples_for 'can not unsubscribe' do it 'should not be able to perform destroy action' do - delete :destroy, :id => @subscribe.id, :issue_id => @issue.id, :project_id => @project.id + #set_objects_to_destroy + delete :destroy, @destroy_params + response.should redirect_to(forbidden_path) end it 'should not reduce subscribes count' do - lambda{ delete :destroy, :id => @subscribe.id, :issue_id => @issue.id, :project_id => @project.id }.should change{ Subscribe.count }.by(0) + #set_objects_to_destroy + lambda{ delete :destroy, @destroy_params }.should change{ Subscribe.count }.by(0) end end @@ -28,7 +56,9 @@ describe SubscribesController do @project = Factory(:project) @issue = Factory(:issue, :project_id => @project.id) - @subscribe = Factory(:subscribe, :subscribeable => @issue, :user => @user) + + @create_params = {:issue_id => @issue.serial_id, :project_id => @project.id} + @destroy_params = {:issue_id => @issue.serial_id, :project_id => @project.id} any_instance_of(Project, :versions => ['v1.0', 'v2.0']) @@ -40,30 +70,46 @@ describe SubscribesController do @user = Factory(:admin) set_session_for(@user) @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') + @destroy_params = @destroy_params.merge({:id => @user.id}) end - it 'should be able to perform create action' do - post :create, :project_id => @project.id, :issue_id => @issue.id - response.should redirect_to(project_issue_path(@project, @issue)) + context 'subscribed' do + before(:each) do + ss = @issue.subscribes.build(:user => @user) + ss.save! + end + + it_should_behave_like 'can unsubscribe' + it_should_behave_like 'can not subscribe' end - it 'should create issue object into db' do - lambda{ post :create, :project_id => @project.id, :issue_id => @issue.id }.should change{ Subscribe.count }.by(1) + context 'not subscribed' do + it_should_behave_like 'can subscribe' + #it_should_behave_like 'can not unsubscribe' + end + end + + context 'for simple user' do + before(:each) do + @user = Factory(:user) + set_session_for(@user) + @destroy_params = @destroy_params.merge({:id => @user.id}) end - it 'should be able to perform destroy action' do - delete :destroy, :id => @subscribe.id, :issue_id => @issue.id, :project_id => @project.id - response.should redirect_to(forbidden_path) + context 'subscribed' do + before(:each) do + ss = @issue.subscribes.build(:user => @user) + ss.save! + end + + it_should_behave_like 'can unsubscribe' + it_should_behave_like 'can not subscribe' end - it 'should reduce subscribes count' do - lambda{ delete :destroy, :id => @subscribe.id, :issue_id => @issue.id, :project_id => @project.id }.should change{ Issue.count }.by(-1) + context 'not subscribed' do + it_should_behave_like 'can subscribe' + #it_should_behave_like 'can not unsubscribe' end - - #it_should_behave_like 'user with create subscribe rights' - #it_should_behave_like 'user with update stranger subscribe rights' - #it_should_behave_like 'user with update own subscribe rights' - #it_should_behave_like 'user without destroy subscribe rights' end end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 505e33d36..0b969c134 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -1,5 +1,125 @@ require 'spec_helper' +require "cancan/matchers" + +def set_testable_data + @ability = Ability.new(@user) + + @project = Factory(:project) + @issue = Factory(:issue, :project_id => @project.id) + + @comment = Factory(:comment, :commentable => @issue, :user => @user) + @stranger_comment = Factory(:comment, :commentable => @issue, :user => @stranger) + + any_instance_of(Project, :versions => ['v1.0', 'v2.0']) +end describe Comment do - pending "add some examples to (or delete) #{__FILE__}" + context 'for global admin user' do + before(:each) do + @user = Factory(:admin) + @stranger = Factory(:user) + + set_testable_data + end + + it 'should create comment' do + @ability.should be_able_to(:create, Comment.new(:commentable => @issue, :user => @user)) + end + + it 'should update comment' do + @ability.should be_able_to(:update, @comment) + end + + it 'should update stranger comment' do + @ability.should be_able_to(:update, @stranger_comment) + end + + it 'should destroy own comment' do + @ability.should be_able_to(:destroy, @comment) + end + + it 'should destroy stranger comment' do + @ability.should be_able_to(:destroy, @stranger_comment) + end + end + + context 'for project admin user' do + before(:each) do + @user = Factory(:user) + @stranger = Factory(:user) + + set_testable_data + + @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') + end + + it 'should create comment' do + @ability.should be_able_to(:create, Comment.new(:commentable => @issue, :user => @user)) + end + + it 'should update comment' do + @ability.should be_able_to(:update, @comment) + end + + it 'should update stranger comment' do + @ability.should be_able_to(:update, @stranger_comment) + end + + it 'should not destroy comment' do + @ability.should_not be_able_to(:destroy, @comment) + end + end + + context 'for project owner user' do + before(:each) do + @user = Factory(:user) + @stranger = Factory(:user) + + set_testable_data + + @project.update_attribute(:owner, @user) + @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') + end + + it 'should create comment' do + @ability.should be_able_to(:create, Comment.new(:commentable => @issue, :user => @user)) + end + + it 'should update comment' do + @ability.should be_able_to(:update, @comment) + end + + it 'should update stranger comment' do + @ability.should be_able_to(:update, @stranger_comment) + end + + it 'should not destroy comment' do + @ability.should_not be_able_to(:destroy, @comment) + end + end + + context 'for simple user' do + before(:each) do + @user = Factory(:user) + @stranger = Factory(:user) + + set_testable_data + end + + it 'should create comment' do + @ability.should be_able_to(:create, Comment.new(:commentable => @issue, :user => @user)) + end + + it 'should update comment' do + @ability.should be_able_to(:update, @comment) + end + + it 'should not update stranger comment' do + @ability.should_not be_able_to(:update, @stranger_comment) + end + + it 'should not destroy comment' do + @ability.should_not be_able_to(:destroy, @comment) + end + end end diff --git a/spec/models/subscribe_spec.rb b/spec/models/subscribe_spec.rb index 0df4fbb42..7d496da07 100644 --- a/spec/models/subscribe_spec.rb +++ b/spec/models/subscribe_spec.rb @@ -1,5 +1,77 @@ require 'spec_helper' +require "cancan/matchers" + +def set_testable_data + @ability = Ability.new(@user) + + @project = Factory(:project) + @issue = Factory(:issue, :project_id => @project.id) + + any_instance_of(Project, :versions => ['v1.0', 'v2.0']) +end describe Subscribe do - pending "add some examples to (or delete) #{__FILE__}" + context 'for global admin user' do + before(:each) do + @user = Factory(:admin) + @stranger = Factory(:user) + + set_testable_data + end + + it 'should create subscribe' do + @ability.should be_able_to(:create, Subscribe.new(:subscribeable => @issue, :user => @user)) + end + + context 'destroy' do + before(:each) do + @subscribe = Factory(:subscribe, :subscribeable => @issue, :user => @user) + @stranger_subscribe = Factory(:subscribe, :subscribeable => @issue, :user => @stranger) + end + + context 'own subscribe' do + it 'should destroy subscribe' do + @ability.should be_able_to(:destroy, @subscribe) + end + end + + context 'stranger subscribe' do + it 'should not destroy subscribe' do + @ability.should_not be_able_to(:destroy, @stranger_subscribe) + end + end + end + end + + context 'for simple user' do + before(:each) do + @user = Factory(:user) + @stranger = Factory(:user) + + set_testable_data + end + + it 'should create subscribe' do + @ability.should be_able_to(:create, Subscribe.new(:subscribeable => @issue, :user => @user)) + end + + context 'destroy' do + before(:each) do + @subscribe = Factory(:subscribe, :subscribeable => @issue, :user => @user) + @stranger_subscribe = Factory(:subscribe, :subscribeable => @issue, :user => @stranger) + end + + context 'own subscribe' do + it 'should destroy subscribe' do + @ability.should be_able_to(:destroy, @subscribe) + end + end + + context 'stranger subscribe' do + it 'should not destroy subscribe' do + @ability.should_not be_able_to(:destroy, @stranger_subscribe) + end + end + end + end end