# encoding: utf-8
require 'fileutils'
require 'spec_helper'
require_relative '../../../lib/notifiers/system-cve-slack-notifier'

describe SystemCVESlackNotifier do
  let(:cves) do
    [{ title: 'LibXML Security', description: 'Parses bits improperly' },
     { title: 'OpenSSL Error', description: 'Realistic error' }]
  end
  let(:related_to_stacks) { true }
  let(:slack_client)      { double(:slack_client) }

  subject { described_class.notify! cves, related_to_stacks }

  before(:each) do
    allow(SlackClient).to receive(:new).and_return(slack_client)
  end

  describe '#notify!' do
    context 'no cves' do
      let(:cves) { [] }

      it 'does not post to slack' do
        expect(slack_client).to_not receive(:post_to_slack)
        subject
      end
    end

    context 'cves are related to stacks' do
      it 'posts to slack' do
        expect(slack_client).to receive(:post_to_slack)
          .with("There are *2* new CVEs:\n- LibXML Security*\n- OpenSSL Error*\n")
        subject
      end
    end

    context 'cves are not related to stacks' do
      let(:related_to_stacks) { false }

      it 'does not post to slack' do
        expect(slack_client).to_not receive(:post_to_slack)
        subject
      end
    end
  end
end
