summaryrefslogtreecommitdiff
path: root/spec/services/spam/ham_service_spec.rb
blob: 0101a8e77047aa67ee61935b56aadda70a6a82c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Spam::HamService do
  let_it_be(:user) { create(:user) }

  let!(:spam_log) { create(:spam_log, user: user, submitted_as_ham: false) }
  let(:fake_akismet_service) { double(:akismet_service) }

  subject { described_class.new(spam_log) }

  before do
    allow(Spam::AkismetService).to receive(:new).and_return fake_akismet_service
  end

  describe '#execute' do
    context 'AkismetService returns false (Akismet cannot be reached, etc)' do
      before do
        allow(fake_akismet_service).to receive(:submit_ham).and_return false
      end

      it 'returns false' do
        expect(subject.execute).to be_falsey
      end

      it 'does not update the record' do
        expect { subject.execute }.not_to change { spam_log.submitted_as_ham }
      end

      context 'if spam log record has already been marked as spam' do
        before do
          spam_log.update_attribute(:submitted_as_ham, true)
        end

        it 'does not update the record' do
          expect { subject.execute }.not_to change { spam_log.submitted_as_ham }
        end
      end
    end

    context 'Akismet ham submission is successful' do
      before do
        spam_log.update_attribute(:submitted_as_ham, false)
        allow(fake_akismet_service).to receive(:submit_ham).and_return true
      end

      it 'returns true' do
        expect(subject.execute).to be_truthy
      end

      it 'updates the record' do
        expect { subject.execute }.to change { spam_log.submitted_as_ham }.from(false).to(true)
      end
    end
  end
end