diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/factories/commits.rb | 24 | ||||
-rw-r--r-- | spec/models/mail_service_spec.rb | 192 | ||||
-rw-r--r-- | spec/services/notification_service_spec.rb | 56 |
3 files changed, 214 insertions, 58 deletions
diff --git a/spec/factories/commits.rb b/spec/factories/commits.rb index f767cac..156fabe 100644 --- a/spec/factories/commits.rb +++ b/spec/factories/commits.rb @@ -22,8 +22,28 @@ FactoryGirl.define do push_data do { ref: 'refs/heads/master', - before_sha: '76de212e80737a608d939f648d959671fb0a0142', - after_sha: '97de212e80737a608d939f648d959671fb0a0142' + before: '76de212e80737a608d939f648d959671fb0a0142', + after: '97de212e80737a608d939f648d959671fb0a0142', + user_name: 'Git User', + repository: { + name: 'test-data', + url: 'ssh://git@gitlab.com/test/test-data.git', + description: '', + homepage: 'http://gitlab.com/test/test-data' + }, + commits: [ + { + id: '97de212e80737a608d939f648d959671fb0a0142', + message: 'Test commit message', + timestamp: '2014-09-23T13:12:25+02:00', + url: 'https://gitlab.com/test/test-data/commit/97de212e80737a608d939f648d959671fb0a0142', + author: { + name: 'Git User', + email: 'git@user.com' + } + } + ], + total_commits_count: 1 } end end diff --git a/spec/models/mail_service_spec.rb b/spec/models/mail_service_spec.rb new file mode 100644 index 0000000..7a43978 --- /dev/null +++ b/spec/models/mail_service_spec.rb @@ -0,0 +1,192 @@ +require 'spec_helper' + +describe MailService do + describe "Associations" do + it { should belong_to :project } + end + + describe "Validations" do + context "active" do + before do + subject.active = true + end + end + end + + describe 'Sends email for' do + let(:mail) { MailService.new } + + describe 'failed build' do + let(:project) { FactoryGirl.create(:project, email_add_committer: true) } + let(:commit) { FactoryGirl.create(:commit, project: project) } + let(:build) { FactoryGirl.create(:build, status: :failed, commit: commit) } + + before do + mail.stub( + project: project + ) + end + + it do + should_email(commit.git_author_email) + mail.execute(build) + end + + def should_email(email) + Notify.should_receive(:build_fail_email).with(build.id, email) + Notify.should_not_receive(:build_success_email).with(build.id, email) + end + end + + describe 'successfull build' do + let(:project) { FactoryGirl.create(:project, email_add_committer: true, email_only_broken_builds: false) } + let(:commit) { FactoryGirl.create(:commit, project: project) } + let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) } + + before do + mail.stub( + project: project + ) + end + + it do + should_email(commit.git_author_email) + mail.execute(build) + end + + def should_email(email) + Notify.should_receive(:build_success_email).with(build.id, email) + Notify.should_not_receive(:build_fail_email).with(build.id, email) + end + end + + describe 'successfull build and project has email_recipients' do + let(:project) { + FactoryGirl.create(:project, + email_add_committer: true, + email_only_broken_builds: false, + email_recipients: "jeroen@example.com") + } + let(:commit) { FactoryGirl.create(:commit, project: project) } + let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) } + + before do + mail.stub( + project: project + ) + end + + it do + should_email(commit.git_author_email) + should_email("jeroen@example.com") + mail.execute(build) + end + + def should_email(email) + Notify.should_receive(:build_success_email).with(build.id, email) + Notify.should_not_receive(:build_fail_email).with(build.id, email) + end + end + + describe 'successful build and notify only broken builds' do + let(:project) { + FactoryGirl.create(:project, + email_add_committer: true, + email_only_broken_builds: true, + email_recipients: "jeroen@example.com") + } + let(:commit) { FactoryGirl.create(:commit, project: project) } + let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) } + + before do + mail.stub( + project: project + ) + end + + it do + should_email(commit.git_author_email) + should_email("jeroen@example.com") + mail.execute(build) + end + + def should_email(email) + Notify.should_not_receive(:build_success_email).with(build.id, email) + Notify.should_not_receive(:build_fail_email).with(build.id, email) + end + end + + describe 'successful build and can test service' do + let(:project) { + FactoryGirl.create(:project, + email_add_committer: true, + email_only_broken_builds: false, + email_recipients: "jeroen@example.com") + } + let(:commit) { FactoryGirl.create(:commit, project: project) } + let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) } + + before do + mail.stub( + project: project + ) + build + end + + it do + mail.can_test?.should == true + end + end + + describe 'successful build and cannot test service' do + let(:project) { + FactoryGirl.create(:project, + email_add_committer: true, + email_only_broken_builds: true, + email_recipients: "jeroen@example.com") + } + let(:commit) { FactoryGirl.create(:commit, project: project) } + let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) } + + before do + mail.stub( + project: project + ) + build + end + + it do + mail.can_test?.should == false + end + end + + describe 'retried build should not receive email' do + let(:project) { + FactoryGirl.create(:project, + email_add_committer: true, + email_only_broken_builds: true, + email_recipients: "jeroen@example.com") + } + let(:commit) { FactoryGirl.create(:commit, project: project) } + let(:build) { FactoryGirl.create(:build, status: :failed, commit: commit) } + + before do + mail.stub( + project: project + ) + end + + it do + Build.retry(build) + should_email(commit.git_author_email) + should_email("jeroen@example.com") + mail.execute(build) + end + + def should_email(email) + Notify.should_not_receive(:build_success_email).with(build.id, email) + Notify.should_not_receive(:build_fail_email).with(build.id, email) + end + end + end +end diff --git a/spec/services/notification_service_spec.rb b/spec/services/notification_service_spec.rb deleted file mode 100644 index 413523c..0000000 --- a/spec/services/notification_service_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -require 'spec_helper' - -describe NotificationService do - let(:notification) { NotificationService.new } - - describe 'Builds' do - - describe 'failed build' do - let(:project) { FactoryGirl.create(:project) } - let(:commit) { FactoryGirl.create(:commit, project: project) } - let(:build) { FactoryGirl.create(:build, status: :failed, commit: commit) } - - it do - should_email(commit.git_author_email) - notification.build_ended(build) - end - - def should_email(email) - Notify.should_receive(:build_fail_email).with(build.id, email) - Notify.should_not_receive(:build_success_email).with(build.id, email) - end - end - - describe 'successfull build' do - let(:project) { FactoryGirl.create(:project) } - let(:commit) { FactoryGirl.create(:commit, project: project) } - let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) } - it do - should_email(commit.git_author_email) - notification.build_ended(build) - end - - def should_email(email) - Notify.should_receive(:build_success_email).with(build.id, email) - Notify.should_not_receive(:build_fail_email).with(build.id, email) - end - end - - describe 'successfull build and project has email_recipients' do - let(:project) { FactoryGirl.create(:project, email_recipients: "jeroen@example.com") } - let(:commit) { FactoryGirl.create(:commit, project: project) } - let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) } - - it do - should_email(commit.git_author_email) - should_email("jeroen@example.com") - notification.build_ended(build) - end - - def should_email(email) - Notify.should_receive(:build_success_email).with(build.id, email) - Notify.should_not_receive(:build_fail_email).with(build.id, email) - end - end - end -end |