BigW Consortium Gitlab

pushover_service_spec.rb 1.68 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe PushoverService, models: true do
4
  describe 'Associations' do
5 6
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
7 8 9
  end

  describe 'Validations' do
10 11
    context 'when service is active' do
      before { subject.active = true }
12

13 14 15 16 17 18 19 20 21 22 23
      it { is_expected.to validate_presence_of(:api_key) }
      it { is_expected.to validate_presence_of(:user_key) }
      it { is_expected.to validate_presence_of(:priority) }
    end

    context 'when service is inactive' do
      before { subject.active = false }

      it { is_expected.not_to validate_presence_of(:api_key) }
      it { is_expected.not_to validate_presence_of(:user_key) }
      it { is_expected.not_to validate_presence_of(:priority) }
24 25 26 27 28 29
    end
  end

  describe 'Execute' do
    let(:pushover) { PushoverService.new }
    let(:user) { create(:user) }
30
    let(:project) { create(:project, :repository) }
31
    let(:sample_data) do
32
      Gitlab::DataBuilder::Push.build_sample(project, user)
33
    end
34 35 36 37 38 39 40 41 42

    let(:api_key) { 'verySecret' }
    let(:user_key) { 'verySecret' }
    let(:device) { 'myDevice' }
    let(:priority) { 0 }
    let(:sound) { 'bike' }
    let(:api_url) { 'https://api.pushover.net/1/messages.json' }

    before do
43
      allow(pushover).to receive_messages(
44 45 46 47 48 49 50 51 52 53 54 55 56
        project: project,
        project_id: project.id,
        service_hook: true,
        api_key: api_key,
        user_key: user_key,
        device: device,
        priority: priority,
        sound: sound
      )

      WebMock.stub_request(:post, api_url)
    end

57
    it 'calls Pushover API' do
58 59
      pushover.execute(sample_data)

60
      expect(WebMock).to have_requested(:post, api_url).once
61 62 63
    end
  end
end