Commit 2035704a authored by charlieablett's avatar charlieablett

Move AkismetService into spam folder

Part of a general tidy of spam logic
parent a15388de
# frozen_string_literal: true
class AkismetService
attr_accessor :text, :options
def initialize(owner_name, owner_email, text, options = {})
@owner_name = owner_name
@owner_email = owner_email
@text = text
@options = options
end
def spam?
return false unless akismet_enabled?
params = {
type: 'comment',
text: text,
created_at: DateTime.now,
author: owner_name,
author_email: owner_email,
referrer: options[:referrer]
}
begin
is_spam, is_blatant = akismet_client.check(options[:ip_address], options[:user_agent], params)
is_spam || is_blatant
rescue => e
Rails.logger.error("Unable to connect to Akismet: #{e}, skipping check") # rubocop:disable Gitlab/RailsLogger
false
end
end
def submit_ham
submit(:ham)
end
def submit_spam
submit(:spam)
end
private
attr_accessor :owner_name, :owner_email
def akismet_client
@akismet_client ||= ::Akismet::Client.new(Gitlab::CurrentSettings.akismet_api_key,
Gitlab.config.gitlab.url)
end
def akismet_enabled?
Gitlab::CurrentSettings.akismet_enabled
end
def submit(type)
return false unless akismet_enabled?
params = {
type: 'comment',
text: text,
author: owner_name,
author_email: owner_email
}
begin
akismet_client.public_send(type, options[:ip_address], options[:user_agent], params) # rubocop:disable GitlabSecurity/PublicSend
true
rescue => e
Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!") # rubocop:disable Gitlab/RailsLogger
false
end
end
end
......@@ -6,7 +6,7 @@ module AkismetMethods
end
def akismet
@akismet ||= AkismetService.new(
@akismet ||= Spam::AkismetService.new(
spammable_owner.name,
spammable_owner.email,
spammable.spammable_text,
......
# frozen_string_literal: true
module Spam
class AkismetService
attr_accessor :text, :options
def initialize(owner_name, owner_email, text, options = {})
@owner_name = owner_name
@owner_email = owner_email
@text = text
@options = options
end
def spam?
return false unless akismet_enabled?
params = {
type: 'comment',
text: text,
created_at: DateTime.now,
author: owner_name,
author_email: owner_email,
referrer: options[:referrer]
}
begin
is_spam, is_blatant = akismet_client.check(options[:ip_address], options[:user_agent], params)
is_spam || is_blatant
rescue => e
Rails.logger.error("Unable to connect to Akismet: #{e}, skipping check") # rubocop:disable Gitlab/RailsLogger
false
end
end
def submit_ham
submit(:ham)
end
def submit_spam
submit(:spam)
end
private
attr_accessor :owner_name, :owner_email
def akismet_client
@akismet_client ||= ::Akismet::Client.new(Gitlab::CurrentSettings.akismet_api_key,
Gitlab.config.gitlab.url)
end
def akismet_enabled?
Gitlab::CurrentSettings.akismet_enabled
end
def submit(type)
return false unless akismet_enabled?
params = {
type: 'comment',
text: text,
author: owner_name,
author_email: owner_email
}
begin
akismet_client.public_send(type, options[:ip_address], options[:user_agent], params) # rubocop:disable GitlabSecurity/PublicSend
true
rescue => e
Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!") # rubocop:disable Gitlab/RailsLogger
false
end
end
end
end
......@@ -39,7 +39,7 @@ describe Admin::SpamLogsController do
describe '#mark_as_ham' do
before do
allow_next_instance_of(AkismetService) do |instance|
allow_next_instance_of(Spam::AkismetService) do |instance|
allow(instance).to receive(:submit_ham).and_return(true)
end
end
......
......@@ -422,7 +422,7 @@ describe Projects::IssuesController do
context 'when Akismet is enabled and the issue is identified as spam' do
before do
stub_application_setting(recaptcha_enabled: true)
expect_next_instance_of(AkismetService) do |akismet_service|
expect_next_instance_of(Spam::AkismetService) do |akismet_service|
expect(akismet_service).to receive_messages(spam?: true)
end
end
......@@ -702,7 +702,7 @@ describe Projects::IssuesController do
context 'when an issue is not identified as spam' do
before do
expect_next_instance_of(AkismetService) do |akismet_service|
expect_next_instance_of(Spam::AkismetService) do |akismet_service|
expect(akismet_service).to receive_messages(spam?: false)
end
end
......@@ -715,7 +715,7 @@ describe Projects::IssuesController do
context 'when an issue is identified as spam' do
context 'when captcha is not verified' do
before do
expect_next_instance_of(AkismetService) do |akismet_service|
expect_next_instance_of(Spam::AkismetService) do |akismet_service|
expect(akismet_service).to receive_messages(spam?: true)
end
end
......@@ -954,7 +954,7 @@ describe Projects::IssuesController do
before do
stub_feature_flags(allow_possible_spam: false)
expect_next_instance_of(AkismetService) do |akismet_service|
expect_next_instance_of(Spam::AkismetService) do |akismet_service|
expect(akismet_service).to receive_messages(spam?: false)
end
end
......@@ -971,7 +971,7 @@ describe Projects::IssuesController do
end
before do
expect_next_instance_of(AkismetService) do |akismet_service|
expect_next_instance_of(Spam::AkismetService) do |akismet_service|
expect(akismet_service).to receive_messages(spam?: true)
end
end
......@@ -1096,7 +1096,7 @@ describe Projects::IssuesController do
describe 'POST #mark_as_spam' do
context 'properly submits to Akismet' do
before do
expect_next_instance_of(AkismetService) do |akismet_service|
expect_next_instance_of(Spam::AkismetService) do |akismet_service|
expect(akismet_service).to receive_messages(submit_spam: true)
end
expect_next_instance_of(ApplicationSetting) do |setting|
......
......@@ -92,7 +92,7 @@ describe Projects::SnippetsController do
context 'when the snippet is spam' do
before do
allow_next_instance_of(AkismetService) do |instance|
allow_next_instance_of(Spam::AkismetService) do |instance|
allow(instance).to receive(:spam?).and_return(true)
end
end
......@@ -172,7 +172,7 @@ describe Projects::SnippetsController do
context 'when the snippet is spam' do
before do
allow_next_instance_of(AkismetService) do |instance|
allow_next_instance_of(Spam::AkismetService) do |instance|
allow(instance).to receive(:spam?).and_return(true)
end
end
......@@ -282,7 +282,7 @@ describe Projects::SnippetsController do
let(:snippet) { create(:project_snippet, :private, project: project, author: user) }
before do
allow_next_instance_of(AkismetService) do |instance|
allow_next_instance_of(Spam::AkismetService) do |instance|
allow(instance).to receive_messages(submit_spam: true)
end
stub_application_setting(akismet_enabled: true)
......
......@@ -275,7 +275,7 @@ describe SnippetsController do
context 'when the snippet is spam' do
before do
allow_next_instance_of(AkismetService) do |instance|
allow_next_instance_of(Spam::AkismetService) do |instance|
allow(instance).to receive(:spam?).and_return(true)
end
end
......@@ -349,7 +349,7 @@ describe SnippetsController do
context 'when the snippet is spam' do
before do
allow_next_instance_of(AkismetService) do |instance|
allow_next_instance_of(Spam::AkismetService) do |instance|
allow(instance).to receive(:spam?).and_return(true)
end
end
......@@ -459,7 +459,7 @@ describe SnippetsController do
let(:snippet) { create(:personal_snippet, :public, author: user) }
before do
allow_next_instance_of(AkismetService) do |instance|
allow_next_instance_of(Spam::AkismetService) do |instance|
allow(instance).to receive_messages(submit_spam: true)
end
stub_application_setting(akismet_enabled: true)
......
......@@ -392,7 +392,7 @@ describe API::Issues do
expect_next_instance_of(Spam::SpamCheckService) do |spam_service|
expect(spam_service).to receive_messages(check_for_spam?: true)
end
expect_next_instance_of(AkismetService) do |akismet_service|
expect_next_instance_of(Spam::AkismetService) do |akismet_service|
expect(akismet_service).to receive_messages(spam?: true)
end
end
......
......@@ -197,7 +197,7 @@ describe API::Issues do
expect_next_instance_of(Spam::SpamCheckService) do |spam_service|
expect(spam_service).to receive_messages(check_for_spam?: true)
end
expect_next_instance_of(AkismetService) do |akismet_service|
expect_next_instance_of(Spam::AkismetService) do |akismet_service|
expect(akismet_service).to receive_messages(spam?: true)
end
end
......
......@@ -179,7 +179,7 @@ describe API::ProjectSnippets do
end
before do
allow_next_instance_of(AkismetService) do |instance|
allow_next_instance_of(Spam::AkismetService) do |instance|
allow(instance).to receive(:spam?).and_return(true)
end
end
......@@ -271,7 +271,7 @@ describe API::ProjectSnippets do
end
before do
allow_next_instance_of(AkismetService) do |instance|
allow_next_instance_of(Spam::AkismetService) do |instance|
allow(instance).to receive(:spam?).and_return(true)
end
end
......
......@@ -238,7 +238,7 @@ describe API::Snippets do
end
before do
allow_next_instance_of(AkismetService) do |instance|
allow_next_instance_of(Spam::AkismetService) do |instance|
allow(instance).to receive(:spam?).and_return(true)
end
end
......@@ -327,7 +327,7 @@ describe API::Snippets do
end
before do
allow_next_instance_of(AkismetService) do |instance|
allow_next_instance_of(Spam::AkismetService) do |instance|
allow(instance).to receive(:spam?).and_return(true)
end
end
......
......@@ -355,7 +355,7 @@ describe Issues::CreateService do
opts[:recaptcha_verified] = true
opts[:spam_log_id] = spam_logs.last.id
expect(AkismetService).not_to receive(:new)
expect(Spam::AkismetService).not_to receive(:new)
end
it 'does no mark an issue as a spam ' do
......@@ -392,7 +392,7 @@ describe Issues::CreateService do
context 'when akismet detects spam' do
before do
expect_next_instance_of(AkismetService) do |akismet_service|
expect_next_instance_of(Spam::AkismetService) do |akismet_service|
expect(akismet_service).to receive_messages(spam?: true)
end
end
......@@ -442,7 +442,7 @@ describe Issues::CreateService do
context 'when akismet does not detect spam' do
before do
expect_next_instance_of(AkismetService) do |akismet_service|
expect_next_instance_of(Spam::AkismetService) do |akismet_service|
expect(akismet_service).to receive_messages(spam?: false)
end
end
......
......@@ -99,7 +99,7 @@ describe Snippets::CreateService do
end
before do
expect_next_instance_of(AkismetService) do |akismet_service|
expect_next_instance_of(Spam::AkismetService) do |akismet_service|
expect(akismet_service).to receive_messages(spam?: true)
end
end
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
describe AkismetService do
describe Spam::AkismetService do
let(:fake_akismet_client) { double(:akismet_client) }
let_it_be(:text) { "Would you like to buy some tinned meat product?" }
......
......@@ -85,7 +85,7 @@ describe Spam::SpamCheckService do
before do
issue.closed_at = Time.zone.now
allow(AkismetService).to receive(:new).and_return(double(spam?: true))
allow(Spam::AkismetService).to receive(:new).and_return(double(spam?: true))
end
it 'returns false' do
......@@ -105,7 +105,7 @@ describe Spam::SpamCheckService do
context 'when indicated as spam by akismet' do
before do
allow(AkismetService).to receive(:new).and_return(double(spam?: true))
allow(Spam::AkismetService).to receive(:new).and_return(double(spam?: true))
end
context 'when allow_possible_spam feature flag is false' do
......@@ -135,7 +135,7 @@ describe Spam::SpamCheckService do
context 'when not indicated as spam by akismet' do
before do
allow(AkismetService).to receive(:new).and_return(double(spam?: false))
allow(Spam::AkismetService).to receive(:new).and_return(double(spam?: false))
end
it 'returns false' do
......
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