summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2016-01-09 19:30:34 +0000
committerDouglas Barbosa Alexandre <dbalexandre@gmail.com>2016-02-02 11:25:44 -0200
commitd20e75a8d80c2828336cd22897ea6868d666f8a5 (patch)
tree9cbf725ad1b523ab0c8d94261d4a02a0677ca450 /spec
parent6cffcb05882b0d3c4a02f9acf21806e25ea09ec3 (diff)
downloadgitlab-ce-d20e75a8d80c2828336cd22897ea6868d666f8a5.tar.gz
Support Akismet spam checking for creation of issues via API
Currently any spam detected by Akismet by non-members via API will be logged in a separate table in the admin page. Closes #5612
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/admin/spam_logs_controller_spec.rb47
-rw-r--r--spec/factories/spam_logs.rb7
-rw-r--r--spec/lib/gitlab/akismet_helper_spec.rb35
-rw-r--r--spec/requests/api/issues_spec.rb22
4 files changed, 111 insertions, 0 deletions
diff --git a/spec/controllers/admin/spam_logs_controller_spec.rb b/spec/controllers/admin/spam_logs_controller_spec.rb
new file mode 100644
index 00000000000..2486298fc78
--- /dev/null
+++ b/spec/controllers/admin/spam_logs_controller_spec.rb
@@ -0,0 +1,47 @@
+require 'spec_helper'
+
+describe Admin::SpamLogsController do
+ let(:admin) { create(:admin) }
+ let(:spam_log) { create(:spam_log, user: admin) }
+
+ before do
+ sign_in(admin)
+ end
+
+ describe '#index' do
+ it 'lists all spam logs' do
+ get :index
+ expect(response.status).to eq(200)
+ end
+ end
+
+ describe '#destroy' do
+ it 'destroys just spam log' do
+ user = spam_log.user
+ delete :destroy, id: spam_log.id
+
+ expect(SpamLog.all.count).to eq(0)
+ expect(User.find(user.id)).to be_truthy
+ expect(response.status).to eq(302)
+ end
+
+ it 'destroys user and his spam logs' do
+ user = spam_log.user
+ delete :destroy, id: spam_log.id, remove_user: true
+
+ expect(SpamLog.all.count).to eq(0)
+ expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
+ expect(response.status).to eq(302)
+ end
+
+ it 'destroys user and his spam logs with JSON format' do
+ user = spam_log.user
+ delete :destroy, id: spam_log.id, remove_user: true, format: :json
+
+ expect(SpamLog.all.count).to eq(0)
+ expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
+ expect(JSON.parse(response.body)).to eq({})
+ expect(response.status).to eq(200)
+ end
+ end
+end
diff --git a/spec/factories/spam_logs.rb b/spec/factories/spam_logs.rb
new file mode 100644
index 00000000000..9e8686d73c2
--- /dev/null
+++ b/spec/factories/spam_logs.rb
@@ -0,0 +1,7 @@
+# Read about factories at https://github.com/thoughtbot/factory_girl
+
+FactoryGirl.define do
+ factory :spam_log do
+ user
+ end
+end
diff --git a/spec/lib/gitlab/akismet_helper_spec.rb b/spec/lib/gitlab/akismet_helper_spec.rb
new file mode 100644
index 00000000000..9858935180a
--- /dev/null
+++ b/spec/lib/gitlab/akismet_helper_spec.rb
@@ -0,0 +1,35 @@
+require 'spec_helper'
+
+describe Gitlab::AkismetHelper, type: :helper do
+ let(:project) { create(:project) }
+ let(:user) { create(:user) }
+
+ before do
+ allow(Gitlab.config.gitlab).to receive(:url).and_return(Settings.send(:build_gitlab_url))
+ current_application_settings.akismet_enabled = true
+ current_application_settings.akismet_api_key = '12345'
+ end
+
+ describe '#check_for_spam?' do
+ it 'returns true for non-member' do
+ expect(helper.check_for_spam?(project, user)).to eq(true)
+ end
+
+ it 'returns false for member' do
+ project.team << [user, :guest]
+ expect(helper.check_for_spam?(project, user)).to eq(false)
+ end
+ end
+
+ describe '#is_spam?' do
+ it 'returns true for spam' do
+ environment = {
+ 'REMOTE_ADDR' => '127.0.0.1',
+ 'HTTP_USER_AGENT' => 'Test User Agent'
+ }
+
+ allow_any_instance_of(::Akismet::Client).to receive(:check).and_return([true, true])
+ expect(helper.is_spam?(environment, user, 'Is this spam?')).to eq(true)
+ end
+ end
+end
diff --git a/spec/requests/api/issues_spec.rb b/spec/requests/api/issues_spec.rb
index 5e65ad18c0e..2e50344c149 100644
--- a/spec/requests/api/issues_spec.rb
+++ b/spec/requests/api/issues_spec.rb
@@ -241,6 +241,28 @@ describe API::API, api: true do
end
end
+ describe 'POST /projects/:id/issues with spam filtering' do
+ before do
+ Grape::Endpoint.before_each do |endpoint|
+ allow(endpoint).to receive(:check_for_spam?).and_return(true)
+ allow(endpoint).to receive(:is_spam?).and_return(true)
+ end
+ end
+
+ it "should create a new project issue" do
+ post api("/projects/#{project.id}/issues", user),
+ title: 'new issue', labels: 'label, label2'
+ expect(response.status).to eq(400)
+ expect(json_response['message']).to eq({ "error" => "Spam detected" })
+ spam_logs = SpamLog.all
+ expect(spam_logs.count).to eq(1)
+ expect(spam_logs[0].title).to eq('new issue')
+ expect(spam_logs[0].user).to eq(user)
+ expect(spam_logs[0].noteable_type).to eq('Issue')
+ expect(spam_logs[0].project_id).to eq(project.id)
+ end
+ end
+
describe "PUT /projects/:id/issues/:issue_id to update only title" do
it "should update a project issue" do
put api("/projects/#{project.id}/issues/#{issue.id}", user),