slack_service_spec.rb 1.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# == Schema Information
#
# Table name: services
#
#  id         :integer          not null, primary key
#  type       :string(255)
#  title      :string(255)
#  project_id :integer          not null
#  created_at :datetime
#  updated_at :datetime
#  active     :boolean          default(FALSE), not null
#  properties :text
#

require 'spec_helper'

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17
describe Ci::SlackService do
18
  describe "Associations" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
19
    it { is_expected.to belong_to :project }
20 21 22 23 24 25 26 27
  end

  describe "Validations" do
    context "active" do
      before do
        subject.active = true
      end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
28
      it { is_expected.to validate_presence_of :webhook }
29 30 31 32
    end
  end

  describe "Execute" do
Valery Sizov's avatar
Valery Sizov committed
33
    let(:slack)   { Ci::SlackService.new }
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
34
    let(:commit)  { FactoryGirl.create :ci_commit }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
35
    let(:build)   { FactoryGirl.create :ci_build, commit: commit, status: 'failed' }
36 37 38 39
    let(:webhook_url) { 'https://hooks.slack.com/services/SVRWFV0VVAR97N/B02R25XN3/ZBqu7xMupaEEICInN685' }
    let(:notify_only_broken_builds) { false }

    before do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
40
      allow(slack).to receive_messages(
Kamil Trzcinski's avatar
Kamil Trzcinski committed
41 42
        project: commit.project,
        project_id: commit.project_id,
43 44 45 46 47 48 49 50 51
        webhook: webhook_url,
        notify_only_broken_builds: notify_only_broken_builds
      )

      WebMock.stub_request(:post, webhook_url)
    end

    it "should call Slack API" do
      slack.execute(build)
Valery Sizov's avatar
Valery Sizov committed
52
      Ci::SlackNotifierWorker.drain
53

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
54
      expect(WebMock).to have_requested(:post, webhook_url).once
55 56 57
    end
  end
end