rosa-build/spec/models/comment_for_commit_spec.rb

387 lines
17 KiB
Ruby
Raw Normal View History

2012-01-30 20:39:34 +00:00
# -*- encoding : utf-8 -*-
2012-01-14 12:36:00 +00:00
require 'spec_helper'
require "cancan/matchers"
2012-01-16 13:43:23 +00:00
def set_comments_data_for_commit
2012-01-14 12:36:00 +00:00
@ability = Ability.new(@user)
2012-01-19 18:20:03 +00:00
@project = Factory(:project, :owner => @user)
2012-01-14 12:36:00 +00:00
%x(cp -Rf #{Rails.root}/spec/tests.git/* #{@project.git_repository.path}) # maybe FIXME ?
@commit = @project.git_repository.commits.first
@comment = Factory(:comment, :user => @user)
@comment.update_attributes(:commentable_type => @commit.class.name, :commentable_id => @commit.id)
@stranger_comment = Factory(:comment, :user => @stranger)
@stranger_comment.update_attributes(:commentable_type => @commit.class.name, :commentable_id => @commit.id, :project => @project)
@create_params = {:commentable_type => @commit.class.name, :commentable_id => @commit.id, :user => @user, :project => @project}
any_instance_of(Project, :versions => ['v1.0', 'v2.0'])
end
describe Comment do
context 'for global admin user' do
before(:each) do
@user = Factory(:admin)
@stranger = Factory(:user)
2012-01-16 13:43:23 +00:00
set_comments_data_for_commit
2012-01-14 12:36:00 +00:00
end
it 'should create comment' do
@ability.should be_able_to(:create, Comment.new(@create_params))
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)
2012-01-16 13:43:23 +00:00
set_comments_data_for_commit
2012-01-24 12:15:56 +00:00
#~ #@project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin')
2012-01-14 12:36:00 +00:00
end
it 'should create comment' do
@ability.should be_able_to(:create, Comment.new(@create_params))
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
2012-01-19 18:20:03 +00:00
2012-01-21 19:13:33 +00:00
context 'for default settings' do
it 'should send an e-mail' do
2012-01-19 18:20:03 +00:00
ActionMailer::Base.deliveries = []
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
2012-01-19 18:20:03 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1
ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true
end
end
2012-01-21 19:13:33 +00:00
context 'for disabled notify setting new_comment_commit_repo_owner' do
2012-01-24 12:15:56 +00:00
it 'should not send an e-mail' do
2012-01-21 19:13:33 +00:00
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_repo_owner, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
2012-01-24 12:15:56 +00:00
ActionMailer::Base.deliveries.count.should == 0
2012-01-21 19:13:33 +00:00
end
end
context 'for disabled notify setting new_comment_commit_owner' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_owner, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
2012-01-24 12:15:56 +00:00
ActionMailer::Base.deliveries.count.should == 1
2012-01-21 19:13:33 +00:00
ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true
end
end
context 'for disabled notify setting new_comment_commit_commentor' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_commentor, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
2012-01-24 12:15:56 +00:00
ActionMailer::Base.deliveries.count.should == 1
2012-01-21 19:13:33 +00:00
ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true
end
end
context 'for disabled all notify setting expect global' do
2012-01-19 18:20:03 +00:00
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
2012-01-21 19:13:33 +00:00
@user.notifier.update_attribute :new_comment_commit_repo_owner, false
@user.notifier.update_attribute :new_comment_commit_owner, false
@user.notifier.update_attribute :new_comment_commit_commentor, false
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
2012-01-19 18:20:03 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
2012-01-24 12:15:56 +00:00
ActionMailer::Base.deliveries.count.should == 0
2012-01-19 18:20:03 +00:00
end
end
2012-01-24 12:15:56 +00:00
context 'for unsubscribe commit' do
2012-01-19 18:20:03 +00:00
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
Subscribe.unsubscribe_from_commit(:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @user.id)
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
2012-01-19 18:20:03 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
2012-01-21 19:13:33 +00:00
ActionMailer::Base.deliveries.count.should == 0 # cache project.commit_comments_subscribes ...
2012-01-19 18:20:03 +00:00
end
end
context 'for disabled global notify setting' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :can_notify, false
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
2012-01-19 18:20:03 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0
end
end
2012-01-14 12:36:00 +00:00
end
context 'for project owner user' do
before(:each) do
@user = Factory(:user)
@stranger = Factory(:user)
2012-01-16 13:43:23 +00:00
set_comments_data_for_commit
2012-01-14 12:36:00 +00:00
@project.update_attribute(:owner, @user)
end
it 'should create comment' do
2012-01-20 16:38:45 +00:00
@ability.should be_able_to(:create, Comment.create(@create_params))
2012-01-14 12:36:00 +00:00
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
2012-01-19 18:20:03 +00:00
context 'for default enabled settings' do
it 'should send an e-mail by default settings' do
ActionMailer::Base.deliveries = []
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
2012-01-19 18:20:03 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1
ActionMailer::Base.deliveries.last.to.include?(@project.owner.email).should == true
end
end
2012-01-21 19:13:33 +00:00
context 'for disabled notify setting new_comment_commit_repo_owner' do
2012-01-19 18:20:03 +00:00
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
2012-01-21 19:13:33 +00:00
@user.notifier.update_attribute :new_comment_commit_repo_owner, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
2012-01-24 12:15:56 +00:00
ActionMailer::Base.deliveries.count.should == 0
2012-01-21 19:13:33 +00:00
end
end
context 'for disabled notify setting new_comment_commit_owner' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_owner, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
2012-01-31 15:43:42 +00:00
ActionMailer::Base.deliveries.count.should == 1
2012-01-21 19:13:33 +00:00
ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true
end
end
context 'for disabled notify setting new_comment_commit_commentor' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_commentor, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
2012-01-31 15:43:42 +00:00
ActionMailer::Base.deliveries.count.should == 1
2012-01-21 19:13:33 +00:00
ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true
end
end
context 'for disabled all notify setting expect global' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_repo_owner, false
@user.notifier.update_attribute :new_comment_commit_owner, false
@user.notifier.update_attribute :new_comment_commit_commentor, false
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
2012-01-19 18:20:03 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
2012-01-24 12:15:56 +00:00
ActionMailer::Base.deliveries.count.should == 0
2012-01-19 18:20:03 +00:00
end
end
2012-01-21 19:13:33 +00:00
context 'for unsubscribe project' do
2012-01-19 18:20:03 +00:00
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
Subscribe.unsubscribe_from_commit(:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @user.id)
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
2012-01-19 18:20:03 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
2012-01-24 12:15:56 +00:00
ActionMailer::Base.deliveries.count.should == 0
2012-01-19 18:20:03 +00:00
end
end
context 'for disabled global notify setting' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
2012-01-21 19:13:33 +00:00
@user.notifier.update_attribute :can_notify, false
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
2012-01-19 18:20:03 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0
end
end
context 'for own commit' do
it 'should send a one e-mail' do
ActionMailer::Base.deliveries = []
@project.owner.update_attribute :email, 'code@tpope.net'
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
2012-01-21 19:13:33 +00:00
ActionMailer::Base.deliveries.count.should == 1
ActionMailer::Base.deliveries.last.to.include?(@project.owner.email).should == true
end
end
2012-01-14 12:36:00 +00:00
end
context 'for simple user' do
before(:each) do
@user = Factory(:user)
2012-01-20 06:24:55 +00:00
@simple = Factory(:user)
2012-01-14 12:36:00 +00:00
@stranger = Factory(:user)
2012-01-16 13:43:23 +00:00
set_comments_data_for_commit
2012-01-20 06:24:55 +00:00
@create_params = {:commentable_type => @commit.class.name, :commentable_id => @commit.id,
:user => @simple, :project => @project}
@comment = Factory(:comment, :user => @simple)
@comment.update_attributes(:commentable_type => @commit.class.name, :commentable_id => @commit.id)
@ability = Ability.new(@simple)
2012-01-14 12:36:00 +00:00
end
it 'should create comment' do
2012-01-20 16:38:45 +00:00
@ability.should be_able_to(:create, Comment.create(@create_params))
2012-01-14 12:36:00 +00:00
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
2012-01-19 18:20:03 +00:00
context 'for default enabled settings' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
2012-01-19 18:20:03 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1
ActionMailer::Base.deliveries.last.to.include?(@stranger.email).should == false
end
2012-01-20 15:17:05 +00:00
it 'should send an e-mail for comments after his comment' do
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @simple, :body => 'hello!', :project => @project,
2012-01-20 15:17:05 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
2012-01-20 16:38:45 +00:00
2012-01-20 15:17:05 +00:00
ActionMailer::Base.deliveries = []
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @user, :body => 'owner comment', :project => @project,
2012-01-20 15:17:05 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1
ActionMailer::Base.deliveries.last.to.include?(@simple.email).should == true
end
2012-01-22 17:49:41 +00:00
it 'should send an e-mail when subscribed to project' do
2012-01-19 18:20:03 +00:00
ActionMailer::Base.deliveries = []
@project.owner.notifier.update_attribute :can_notify, false
2012-01-21 19:13:33 +00:00
@stranger.notifier.update_attribute :new_comment_commit_repo_owner, false
@stranger.notifier.update_attribute :new_comment_commit_owner, false
Subscribe.subscribe_to_commit(:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @stranger.id)
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @project.owner, :body => 'hello!', :project => @project,
2012-01-19 18:20:03 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1
ActionMailer::Base.deliveries.last.to.include?(@stranger.email).should == true
end
2012-01-20 06:24:55 +00:00
it 'should not send an e-mail for own comment' do
2012-01-19 18:20:03 +00:00
ActionMailer::Base.deliveries = []
Subscribe.subscribe_to_commit(:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @stranger.id)
2012-01-20 16:38:45 +00:00
comment = Comment.create(:user => @owner, :body => 'hello!', :project => @project,
2012-01-19 18:20:03 +00:00
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0
end
end
2012-01-24 12:15:56 +00:00
context 'for committer' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
@stranger.update_attribute :email, 'code@tpope.net'
2012-01-24 12:15:56 +00:00
comment = Comment.create(:user => @user, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1
ActionMailer::Base.deliveries.last.to.include?(@stranger.email).should == true
end
it 'should send a one e-mail when subscribed to commit' do
ActionMailer::Base.deliveries = []
Subscribe.subscribe_to_commit(:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @stranger.id)
@stranger.update_attribute :email, 'code@tpope.net'
2012-01-24 12:15:56 +00:00
comment = Comment.create(:user => @user, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1
ActionMailer::Base.deliveries.last.to.include?(@stranger.email).should == true
end
it 'should not send an e-mail for own comment' do
ActionMailer::Base.deliveries = []
@stranger.update_attribute :email, 'code@tpope.net'
2012-01-24 12:15:56 +00:00
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1
ActionMailer::Base.deliveries.last.to.include?(@stranger.email).should == false
end
it 'should not send an e-mail if global notify off' do
ActionMailer::Base.deliveries = []
@project.owner.notifier.update_attribute :can_notify, false
@stranger.update_attribute :email, 'code@tpope.net'
2012-01-24 12:15:56 +00:00
@stranger.notifier.update_attribute :can_notify, false
comment = Comment.create(:user => @user, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0
end
it 'should not send an e-mail if notify for my commits off' do
ActionMailer::Base.deliveries = []
@stranger.notifier.update_attribute :new_comment_commit_owner, false
@stranger.update_attribute :email, 'code@tpope.net'
2012-01-24 12:15:56 +00:00
comment = Comment.create(:user => @user, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0
end
end
2012-01-14 12:36:00 +00:00
end
end