Browse Source

Check params for spam flags, and return blank when found

tags/v0.2.0^2
Bèr Kessels 2 years ago
parent
commit
039b44e323
2 changed files with 38 additions and 4 deletions
  1. 17
    4
      lib/contact.rb
  2. 21
    0
      test/integration/spam_message_test.rb

+ 17
- 4
lib/contact.rb View File

@@ -15,7 +15,8 @@ error BadRequest do |error|
end

post '/messages' do
validate(params)
return status(201) if spam?
validate
raise BadRequest, errors.join("\n") if errors.any?

@name = params['name']
@@ -33,14 +34,26 @@ post '/messages' do
end
end

def validate(params)
def spam?
!params['contact_captcha'].nil?
end

def validate
{ email: 255, name: 255, message: 1000 }.each do |field, length|
errors << "#{field} cannot be over #{length} characters" if
params[field.to_s].to_s.length > length
errors << "#{field} cannot be empty" if params[field].to_s.empty?
too_long?(field, length)
errors << "#{field} cannot be empty" if empty?(field)
end
end

def too_long?(field, length)
params[field.to_s].to_s.length > length
end

def empty?(field)
params[field].to_s.empty?
end

def errors
@errors ||= []
end

+ 21
- 0
test/integration/spam_message_test.rb View File

@@ -0,0 +1,21 @@
require 'test_helper'

describe 'attempt to spam message through REST' do
let(:params) do
{
name: 'Harry Potter',
email: 'harry@hogwards.edu.wizard',
message: 'Wingardium Leviosar',
contact_captcha: 'true'
}
end

describe 'POST /messages' do
it 'does not send the message over SMTP but fakes the response' do
assert_no_changes 'Mail::TestMailer.deliveries.length' do
post '/messages', params
assert_response 201
end
end
end
end

Loading…
Cancel
Save