rosa-build/spec/models/comment_for_commit_spec.rb

260 lines
9.8 KiB
Ruby
Raw Normal View History

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-14 12:36:00 +00:00
@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(@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
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?(@user.email).should == true
end
end
context 'for disabled notify setting in project' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@project.commit_comments_subscribes.where(:user_id => @user).first.destroy # FIXME
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 # cache project.commit_comments_subscribes ...
end
end
context 'for disabled notify setting' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_repo_owner, 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 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)
2012-01-20 06:24:55 +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
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
context 'for disabled notify setting in project' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
2012-01-20 16:38:45 +00:00
Subscribe.first.destroy # FIXME
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 # cache project.commit_comments_subscribes ...
end
end
context 'for disabled notify setting' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@project.owner.notifier.update_attribute :new_comment_commit_repo_owner, 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 disabled global notify setting' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@project.owner.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 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-19 18:20:03 +00:00
end
context 'for subscribe in project' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
@project.owner.notifier.update_attribute :can_notify, false
@project.commit_comments_subscribes.create(: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 = []
@project.owner.notifier.update_attribute :can_notify, false
@project.commit_comments_subscribes.create(: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-14 12:36:00 +00:00
end
2012-01-20 16:38:45 +00:00
end