2011-12-19 15:30:14 +00:00
|
|
|
require 'spec_helper'
|
2011-12-30 14:21:08 +00:00
|
|
|
require "cancan/matchers"
|
|
|
|
|
2012-01-17 08:17:12 +00:00
|
|
|
def set_commentable_data
|
2011-12-30 14:21:08 +00:00
|
|
|
@ability = Ability.new(@user)
|
|
|
|
|
2012-03-29 21:34:22 +01:00
|
|
|
@project = FactoryGirl.create(:project)
|
2014-01-21 04:51:49 +00:00
|
|
|
@issue = FactoryGirl.create(:issue, project_id: @project.id, user: @user)
|
2011-12-30 14:21:08 +00:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
@comment = FactoryGirl.create(:comment, commentable: @issue, user: @user, project: @project)
|
|
|
|
@stranger_comment = FactoryGirl.create(:comment, commentable: @issue, user: @stranger, project: @project)
|
2011-12-30 14:21:08 +00:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
any_instance_of(Project, versions: ['v1.0', 'v2.0'])
|
2011-12-30 14:21:08 +00:00
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
|
2013-03-27 10:37:35 +00:00
|
|
|
def create_comment_in_issue issue, body
|
2014-01-21 04:51:49 +00:00
|
|
|
FactoryGirl.create(:comment, user: issue.user, commentable: issue, project: issue.project, body: body)
|
2013-03-27 10:37:35 +00:00
|
|
|
end
|
|
|
|
|
2011-12-19 15:30:14 +00:00
|
|
|
describe Comment do
|
2012-05-16 16:29:28 +01:00
|
|
|
before { stub_symlink_methods }
|
2011-12-30 14:21:08 +00:00
|
|
|
context 'for global admin user' do
|
|
|
|
before(:each) do
|
2012-03-29 21:34:22 +01:00
|
|
|
@user = FactoryGirl.create(:admin)
|
|
|
|
@stranger = FactoryGirl.create(:user)
|
2011-12-30 14:21:08 +00:00
|
|
|
|
2012-01-17 08:17:12 +00:00
|
|
|
set_commentable_data
|
2011-12-30 14:21:08 +00:00
|
|
|
end
|
|
|
|
|
2012-12-13 11:30:49 +00:00
|
|
|
it_should_behave_like 'user with create comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with update own comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with update stranger comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with destroy comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with destroy stranger comment ability (for model)'
|
2011-12-30 14:21:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for project admin user' do
|
|
|
|
before(:each) do
|
2012-03-29 21:34:22 +01:00
|
|
|
@user = FactoryGirl.create(:user)
|
|
|
|
@stranger = FactoryGirl.create(:user)
|
2011-12-30 14:21:08 +00:00
|
|
|
|
2012-01-17 08:17:12 +00:00
|
|
|
set_commentable_data
|
2014-03-18 09:31:01 +00:00
|
|
|
create_relation(@project, @user, 'admin')
|
2011-12-30 14:21:08 +00:00
|
|
|
end
|
|
|
|
|
2012-12-13 11:30:49 +00:00
|
|
|
it_should_behave_like 'user with create comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with update own comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with update stranger comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with destroy comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with destroy stranger comment ability (for model)'
|
2011-12-30 14:21:08 +00:00
|
|
|
|
2012-12-13 11:30:49 +00:00
|
|
|
pending "sends an e-mail" do
|
|
|
|
ActionMailer::Base.deliveries.last.to.include?(@stranger.email).should == true
|
2011-12-30 14:21:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for project owner user' do
|
|
|
|
before(:each) do
|
2012-03-29 21:34:22 +01:00
|
|
|
@user = FactoryGirl.create(:user)
|
|
|
|
@stranger = FactoryGirl.create(:user)
|
2011-12-30 14:21:08 +00:00
|
|
|
|
2012-01-17 08:17:12 +00:00
|
|
|
set_commentable_data
|
2011-12-30 14:21:08 +00:00
|
|
|
|
2012-09-06 19:48:36 +01:00
|
|
|
@project.owner = @user
|
|
|
|
@project.save
|
2014-03-18 09:31:01 +00:00
|
|
|
create_relation(@project, @user, 'admin')
|
2011-12-30 14:21:08 +00:00
|
|
|
end
|
|
|
|
|
2012-12-13 11:30:49 +00:00
|
|
|
it_should_behave_like 'user with create comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with update own comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with update stranger comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with destroy comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with destroy stranger comment ability (for model)'
|
2011-12-30 14:21:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for simple user' do
|
|
|
|
before(:each) do
|
2012-03-29 21:34:22 +01:00
|
|
|
@user = FactoryGirl.create(:user)
|
|
|
|
@stranger = FactoryGirl.create(:user)
|
2011-12-30 14:21:08 +00:00
|
|
|
|
2012-01-17 08:17:12 +00:00
|
|
|
set_commentable_data
|
2011-12-30 14:21:08 +00:00
|
|
|
end
|
|
|
|
|
2012-12-13 11:30:49 +00:00
|
|
|
it_should_behave_like 'user with create comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with update own comment ability (for model)'
|
|
|
|
it_should_behave_like 'user without update stranger comment ability (for model)'
|
|
|
|
it_should_behave_like 'user with destroy comment ability (for model)'
|
|
|
|
it_should_behave_like 'user without destroy stranger comment ability (for model)'
|
2012-03-27 17:42:12 +01:00
|
|
|
|
|
|
|
context 'with mass assignment' do
|
|
|
|
it 'should not be able to update commentable' do
|
2014-01-21 04:51:49 +00:00
|
|
|
@comment.update_attributes({commentable_type: 'Grit::Commit', commentable_id: 0})
|
2012-03-27 17:42:12 +01:00
|
|
|
@comment.reload.commentable_id.should eql @issue.id
|
|
|
|
@comment.reload.commentable_type.should eql @issue.class.name
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not be able to update owner' do
|
|
|
|
@comment.should_not allow_mass_assignment_of :user_id
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not be able to update project' do
|
|
|
|
@comment.should_not allow_mass_assignment_of :project_id
|
|
|
|
end
|
|
|
|
end
|
2013-03-27 10:37:35 +00:00
|
|
|
|
|
|
|
context 'automatic issue linking' do
|
|
|
|
before(:each) do
|
2014-01-21 04:51:49 +00:00
|
|
|
@same_name_project = FactoryGirl.create(:project, name: @project.name)
|
|
|
|
@issue_in_same_name_project = FactoryGirl.create(:issue, project: @same_name_project, user: @same_name_project.owner)
|
|
|
|
@another_project = FactoryGirl.create(:project, owner: @user)
|
2013-03-27 10:37:35 +00:00
|
|
|
@other_user_project = FactoryGirl.create(:project)
|
2014-01-21 04:51:49 +00:00
|
|
|
@issue = FactoryGirl.create(:issue, project: @project, user: @user)
|
|
|
|
@second_issue = FactoryGirl.create(:issue, project: @project, user: @user)
|
|
|
|
@issue_in_another_project = FactoryGirl.create(:issue, project: @another_project, user: @user)
|
|
|
|
@issue_in_other_user_project = FactoryGirl.create(:issue, project: @other_user_project, user: @other_user_project.owner)
|
2013-03-27 10:37:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create automatic comment' do
|
|
|
|
create_comment_in_issue(@issue, "test link to ##{@issue.serial_id}; [##{@second_issue.serial_id}]")
|
2014-01-21 04:51:49 +00:00
|
|
|
Comment.where(automatic: true, commentable_type: 'Issue',
|
|
|
|
commentable_id: @second_issue.id,
|
|
|
|
created_from_issue_id: @issue.id).count.should == 1
|
2013-03-27 10:37:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not create automatic comment to the same issue' do
|
|
|
|
create_comment_in_issue(@issue, "test link to ##{@issue.serial_id}; [##{@second_issue.serial_id}]")
|
2014-01-21 04:51:49 +00:00
|
|
|
Comment.where(automatic: true,
|
|
|
|
created_from_issue_id: @issue.id).count.should == 1
|
2013-03-27 10:37:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create automatic comment in the another project issue' do
|
|
|
|
body = "[#{@another_project.name_with_owner}##{@issue_in_another_project.serial_id}]"
|
|
|
|
create_comment_in_issue(@issue, body)
|
2014-01-21 04:51:49 +00:00
|
|
|
Comment.where(automatic: true, commentable_type: 'Issue',
|
|
|
|
commentable_id: @issue_in_another_project.id,
|
|
|
|
created_from_issue_id: @issue.id).count.should == 1
|
2013-03-27 10:37:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create automatic comment in the same name project issue' do
|
|
|
|
body = "[#{@same_name_project.owner.uname}##{@issue_in_same_name_project.serial_id}]"
|
|
|
|
create_comment_in_issue(@issue, body)
|
2014-01-21 04:51:49 +00:00
|
|
|
Comment.where(automatic: true, commentable_type: 'Issue',
|
|
|
|
commentable_id: @issue_in_same_name_project.id,
|
|
|
|
created_from_issue_id: @issue.id).count.should == 1
|
2013-03-27 10:37:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not create duplicate automatic comment' do
|
|
|
|
create_comment_in_issue(@issue, "test link to [##{@second_issue.serial_id}]")
|
|
|
|
create_comment_in_issue(@issue, "test duplicate link to [##{@second_issue.serial_id}]")
|
2014-01-21 04:51:49 +00:00
|
|
|
Comment.where(automatic: true, commentable_type: 'Issue',
|
|
|
|
commentable_id: @second_issue.id,
|
|
|
|
created_from_issue_id: @issue.id).count.should == 1
|
2013-03-27 10:37:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not create duplicate automatic comment from one' do
|
|
|
|
create_comment_in_issue(@issue, "test link to [##{@second_issue.serial_id}]; ##{@second_issue.serial_id}")
|
2014-01-21 04:51:49 +00:00
|
|
|
Comment.where(automatic: true, commentable_type: 'Issue',
|
|
|
|
commentable_id: @second_issue.id,
|
|
|
|
created_from_issue_id: @issue.id).count.should == 1
|
2013-03-27 10:37:35 +00:00
|
|
|
end
|
2013-04-09 19:46:58 +01:00
|
|
|
|
2013-03-27 10:37:35 +00:00
|
|
|
it 'should create two automatic comment' do
|
|
|
|
body = "test ##{@second_issue.serial_id}" +
|
|
|
|
" && [#{@another_project.name_with_owner}##{@issue_in_another_project.serial_id}]"
|
|
|
|
create_comment_in_issue(@issue, body)
|
2014-01-21 04:51:49 +00:00
|
|
|
Comment.where(automatic: true,
|
|
|
|
created_from_issue_id: @issue.id).count.should == 2
|
2013-03-27 10:37:35 +00:00
|
|
|
end
|
2013-04-09 19:46:58 +01:00
|
|
|
|
|
|
|
it 'should create automatic comment by issue title' do
|
2014-01-21 04:51:49 +00:00
|
|
|
issue = FactoryGirl.create(:issue, project: @project, user: @user,
|
|
|
|
title: "link to ##{@issue.serial_id}")
|
|
|
|
Comment.where(automatic: true,
|
2014-03-17 21:03:26 +00:00
|
|
|
created_from_issue_id: issue.id).should have(1).item
|
2013-04-09 19:46:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create automatic comment from issue body' do
|
2014-01-21 04:51:49 +00:00
|
|
|
issue = FactoryGirl.create(:issue, project: @project, user: @user,
|
|
|
|
body: "link to ##{@issue.serial_id}")
|
|
|
|
Comment.where(automatic: true,
|
|
|
|
created_from_issue_id: issue.id).count.should == 1
|
2013-04-09 19:46:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create only one automatic comment from issue title and body' do
|
2014-01-21 04:51:49 +00:00
|
|
|
issue = FactoryGirl.create(:issue, project: @project, user: @user,
|
|
|
|
title: "link to ##{@issue.serial_id} in title",
|
2013-04-09 19:46:58 +01:00
|
|
|
:body => "link to ##{@issue.serial_id} in body")
|
2014-01-21 04:51:49 +00:00
|
|
|
Comment.where(automatic: true,
|
|
|
|
created_from_issue_id: issue.id).count.should == 1
|
2013-04-09 19:46:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-27 10:37:35 +00:00
|
|
|
end
|
2011-12-30 14:21:08 +00:00
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|