Commit becf7b82 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch '199671-ham-service-tests' into 'master'

Add HamService unit tests

Closes #199671

See merge request gitlab-org/gitlab!24628
parents bbcd604f fb368c29
# frozen_string_literal: true
require 'spec_helper'
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 '#mark_as_ham!' 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.mark_as_ham!).to be_falsey
end
it 'does not update the record' do
expect { subject.mark_as_ham! }.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.mark_as_ham! }.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.mark_as_ham!).to be_truthy
end
it 'updates the record' do
expect { subject.mark_as_ham! }.to change { spam_log.submitted_as_ham }.from(false).to(true)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment