2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2011-12-19 15:30:14 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2012-08-15 14:52:32 +01:00
|
|
|
shared_context "comments controller" do
|
|
|
|
before(:each) do
|
|
|
|
stub_symlink_methods
|
|
|
|
|
|
|
|
@project = FactoryGirl.create(:project)
|
|
|
|
@issue = FactoryGirl.create(:issue, :project_id => @project.id, :user => FactoryGirl.create(:user))
|
|
|
|
@comment = FactoryGirl.create(:comment, :commentable => @issue, :project_id => @project.id)
|
|
|
|
|
|
|
|
@user = FactoryGirl.create(:user)
|
|
|
|
@own_comment = FactoryGirl.create(:comment, :commentable => @issue, :user => @user, :project_id => @project.id)
|
2012-09-06 11:53:03 +01:00
|
|
|
|
|
|
|
set_session_for(@user)
|
2012-08-15 14:52:32 +01:00
|
|
|
|
2012-09-06 11:53:03 +01:00
|
|
|
@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)
|
2012-08-15 14:52:32 +01:00
|
|
|
end
|
2012-09-06 11:53:03 +01:00
|
|
|
|
2012-08-15 14:52:32 +01:00
|
|
|
end
|
|
|
|
|
2011-12-29 11:16:54 +00:00
|
|
|
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))
|
|
|
|
end
|
|
|
|
|
2012-09-06 11:53:03 +01:00
|
|
|
it 'should create comment in the database' do
|
2011-12-29 11:16:54 +00:00
|
|
|
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.should redirect_to([@project, @issue])
|
|
|
|
end
|
|
|
|
|
2012-09-06 11:53:03 +01:00
|
|
|
it 'should update comment body' do
|
2011-12-29 11:16:54 +00:00
|
|
|
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.should redirect_to([@project, @issue])
|
|
|
|
end
|
|
|
|
|
2012-08-15 14:52:32 +01:00
|
|
|
it 'should update comment body' do
|
2011-12-29 11:16:54 +00:00
|
|
|
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
|
|
|
|
|
2012-08-15 14:52:32 +01:00
|
|
|
it 'should not update comment body' do
|
2011-12-29 11:16:54 +00:00
|
|
|
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
|
2012-09-06 11:53:03 +01:00
|
|
|
delete :destroy, {:id => @comment.id}.merge(@address)
|
2011-12-29 11:16:54 +00:00
|
|
|
response.should redirect_to(forbidden_path)
|
|
|
|
end
|
|
|
|
|
2012-09-06 11:53:03 +01:00
|
|
|
it 'should not delete comment from database' do
|
|
|
|
lambda{ delete :destroy, {:id => @comment.id}.merge(@address)}.should change{ Issue.count }.by(0)
|
2011-12-29 11:16:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-06 11:53:03 +01:00
|
|
|
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
|
2011-12-29 11:16:54 +00:00
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
describe Projects::CommentsController do
|
2012-08-15 14:52:32 +01:00
|
|
|
include_context "comments controller"
|
2011-12-29 11:16:54 +00:00
|
|
|
|
2012-09-06 11:53:03 +01:00
|
|
|
context 'for global admin user' do
|
|
|
|
before(:each) do
|
|
|
|
@user.role = "admin"
|
|
|
|
@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'
|
|
|
|
end
|
|
|
|
|
2011-12-29 11:16:54 +00:00
|
|
|
context 'for project admin user' do
|
|
|
|
before(:each) do
|
2012-04-26 02:38:33 +01:00
|
|
|
@project.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'admin')
|
2011-12-29 11:16:54 +00:00
|
|
|
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 without destroy comment rights'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for project owner user' do
|
|
|
|
before(:each) do
|
2012-09-06 11:53:03 +01:00
|
|
|
set_session_for(@project.owner) # owner should be user
|
2011-12-29 11:16:54 +00:00
|
|
|
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 without destroy comment rights'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for project reader user' do
|
|
|
|
before(:each) do
|
2012-04-26 02:38:33 +01:00
|
|
|
@project.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'reader')
|
2011-12-29 11:16:54 +00:00
|
|
|
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'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for project writer user' do
|
|
|
|
before(:each) do
|
2012-04-26 02:38:33 +01:00
|
|
|
@project.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'writer')
|
2011-12-29 11:16:54 +00:00
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
|
2011-12-29 11:16:54 +00:00
|
|
|
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'
|
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|