2020-02-06 22:08:59 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Spam::HamService do
|
2020-02-06 22:08:59 -05:00
|
|
|
let_it_be(:user) { create(:user) }
|
2020-02-14 19:08:48 -05:00
|
|
|
let!(:spam_log) { create(:spam_log, user: user, submitted_as_ham: false) }
|
2020-02-06 22:08:59 -05:00
|
|
|
let(:fake_akismet_service) { double(:akismet_service) }
|
|
|
|
|
2020-02-14 19:08:48 -05:00
|
|
|
subject { described_class.new(spam_log) }
|
2020-02-06 22:08:59 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Spam::AkismetService).to receive(:new).and_return fake_akismet_service
|
|
|
|
end
|
|
|
|
|
2020-02-10 10:08:54 -05:00
|
|
|
describe '#execute' do
|
2020-02-06 22:08:59 -05:00
|
|
|
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
|
2020-02-10 10:08:54 -05:00
|
|
|
expect(subject.execute).to be_falsey
|
2020-02-06 22:08:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update the record' do
|
2020-02-14 19:08:48 -05:00
|
|
|
expect { subject.execute }.not_to change { spam_log.submitted_as_ham }
|
2020-02-06 22:08:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'if spam log record has already been marked as spam' do
|
|
|
|
before do
|
2020-02-14 19:08:48 -05:00
|
|
|
spam_log.update_attribute(:submitted_as_ham, true)
|
2020-02-06 22:08:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update the record' do
|
2020-02-14 19:08:48 -05:00
|
|
|
expect { subject.execute }.not_to change { spam_log.submitted_as_ham }
|
2020-02-06 22:08:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'Akismet ham submission is successful' do
|
|
|
|
before do
|
2020-02-14 19:08:48 -05:00
|
|
|
spam_log.update_attribute(:submitted_as_ham, false)
|
2020-02-06 22:08:59 -05:00
|
|
|
allow(fake_akismet_service).to receive(:submit_ham).and_return true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true' do
|
2020-02-10 10:08:54 -05:00
|
|
|
expect(subject.execute).to be_truthy
|
2020-02-06 22:08:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the record' do
|
2020-02-14 19:08:48 -05:00
|
|
|
expect { subject.execute }.to change { spam_log.submitted_as_ham }.from(false).to(true)
|
2020-02-06 22:08:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|