slack_service_spec.rb 1.96 KB
Newer Older
1 2 3 4
# == Schema Information
#
# Table name: services
#
Valery Sizov's avatar
Valery Sizov committed
5 6 7 8 9 10 11 12
#  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
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#

require 'spec_helper'

describe SlackService do
  describe "Associations" do
    it { should belong_to :project }
    it { should have_one :service_hook }
  end

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

Marin Jankovski's avatar
Marin Jankovski committed
29
      it { should validate_presence_of :webhook }
30 31 32 33 34
    end
  end

  describe "Execute" do
    let(:slack) { SlackService.new }
35
    let(:slack_service) { SlackService.new }
36 37 38
    let(:user) { create(:user) }
    let(:project) { create(:project) }
    let(:sample_data) { GitPushService.new.sample_data(project, user) }
Marin Jankovski's avatar
Marin Jankovski committed
39
    let(:webhook) { 'https://gitlabhq.slack.com/services/hooks?token=cdIj4r4LfXUOySDUjp0tk3OI' }
40
    let(:new_webhook) { 'https://hooks.gitlabhq.slack.com/services/cdIj4r4LfXUOySDUjp0tk3OI' }
41
    let(:api_url) {
Marin Jankovski's avatar
Marin Jankovski committed
42
      'https://gitlabhq.slack.com/services/hooks/incoming-webhook?token=cdIj4r4LfXUOySDUjp0tk3OI'
43 44 45 46 47 48 49
    }

    before do
      slack.stub(
        project: project,
        project_id: project.id,
        service_hook: true,
Marin Jankovski's avatar
Marin Jankovski committed
50
        webhook: webhook
51 52 53 54 55 56 57 58 59 60
      )

      WebMock.stub_request(:post, api_url)
    end

    it "should call Slack API" do
      slack.execute(sample_data)

      WebMock.should have_requested(:post, api_url).once
    end
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

    context 'with new webhook syntax' do
      before do
        slack_service.stub(
          project: project,
          project_id: project.id,
          service_hook: true,
          webhook: new_webhook
        )

        WebMock.stub_request(:post, api_url)
      end

      it "should call Slack API" do
        slack_service.execute(sample_data)

        WebMock.should have_requested(:post, api_url).once
      end
    end
80 81
  end
end