1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
# frozen_string_literal: true
RSpec.describe InvisibleCaptcha::ControllerExt, type: :controller do
render_views
before(:each) do
@controller = TopicsController.new
request.env['HTTP_REFERER'] = 'http://test.host/topics'
InvisibleCaptcha.init!
InvisibleCaptcha.timestamp_threshold = 1
InvisibleCaptcha.spinner_enabled = false
end
context 'without invisible_captcha_timestamp in session' do
it 'fails like if it was submitted too fast' do
post :create, params: { topic: { title: 'foo' } }
expect(response).to redirect_to 'http://test.host/topics'
expect(flash[:error]).to eq(InvisibleCaptcha.timestamp_error_message)
end
it 'passes if disabled at action level' do
post :copy, params: { topic: { title: 'foo' } }
expect(flash[:error]).not_to be_present
expect(response.body).to be_present
end
it 'passes if disabled at app level' do
InvisibleCaptcha.timestamp_enabled = false
post :create, params: { topic: { title: 'foo' } }
expect(flash[:error]).not_to be_present
expect(response.body).to be_present
end
end
context 'submission timestamp_threshold' do
before(:each) do
session[:invisible_captcha_timestamp] = Time.zone.now.iso8601
end
it 'fails if submission before timestamp_threshold' do
post :create, params: { topic: { title: 'foo' } }
expect(response).to redirect_to 'http://test.host/topics'
expect(flash[:error]).to eq(InvisibleCaptcha.timestamp_error_message)
# Make sure session is cleared
expect(session[:invisible_captcha_timestamp]).to be_nil
end
it 'allows a custom on_timestamp_spam callback' do
put :update, params: { id: 1, topic: { title: 'bar' } }
expect(response.status).to eq(204)
end
it 'allows a new timestamp to be set in the on_timestamp_spam callback' do
@controller.singleton_class.class_eval do
def custom_timestamp_callback
session[:invisible_captcha_timestamp] = 2.seconds.from_now(Time.zone.now).iso8601
head(204)
end
end
expect { put :update, params: { id: 1, topic: { title: 'bar' } } }
.to change { session[:invisible_captcha_timestamp] }
.to be_present
end
it 'runs on_spam callback if on_timestamp_spam callback is defined but passes' do
put :test_passthrough, params: { id: 1, topic: { title: 'bar', subtitle: 'foo' } }
expect(response.status).to eq(204)
end
context 'successful submissions' do
it 'passes if submission on or after timestamp_threshold' do
sleep InvisibleCaptcha.timestamp_threshold
post :create, params: {
topic: {
title: 'foobar',
author: 'author',
body: 'body that passes validation'
}
}
expect(flash[:error]).not_to be_present
expect(response.body).to redirect_to(new_topic_path)
# Make sure session is cleared
expect(session[:invisible_captcha_timestamp]).to be_nil
end
it 'allow to set a custom timestamp_threshold per action' do
sleep 2 # custom threshold
post :publish, params: { id: 1 }
expect(flash[:error]).not_to be_present
expect(response.body).to redirect_to(new_topic_path)
end
it 'passes if on_timestamp_spam doesn\'t perform' do
put :test_passthrough, params: { id: 1, topic: { title: 'bar' } }
expect(response.body).to redirect_to(new_topic_path)
end
end
end
context 'honeypot attribute' do
before(:each) do
session[:invisible_captcha_timestamp] = Time.zone.now.iso8601
# Wait for valid submission
sleep InvisibleCaptcha.timestamp_threshold
end
it 'fails with spam' do
post :create, params: { topic: { subtitle: 'foo' } }
expect(response.body).to be_blank
end
it 'passes with no spam' do
post :create, params: { topic: { title: 'foo' } }
expect(response.body).to be_present
end
context 'with random honeypot' do
context 'auto-scoped' do
it 'passes with no spam' do
post :categorize, params: { topic: { title: 'foo' } }
expect(response.body).to redirect_to(new_topic_path)
end
it 'fails with spam' do
post :categorize, params: { topic: { "#{InvisibleCaptcha.honeypots.sample}": 'foo' } }
expect(response.body).not_to redirect_to(new_topic_path)
end
end
context 'with no scope' do
it 'passes with no spam' do
post :categorize
expect(response.body).to redirect_to(new_topic_path)
end
it 'fails with spam' do
post :categorize, params: { "#{InvisibleCaptcha.honeypots.sample}": 'foo' }
expect(response.body).not_to redirect_to(new_topic_path)
end
end
context 'with scope' do
it 'fails with spam' do
post :rename, params: { topic: { "#{InvisibleCaptcha.honeypots.sample}": 'foo' } }
expect(response.body).to be_blank
end
it 'passes with no spam' do
post :rename, params: { topic: { title: 'foo' } }
expect(response.body).to be_blank
end
end
end
it 'allow a custom on_spam callback' do
put :update, params: { id: 1, topic: { subtitle: 'foo' } }
expect(response.body).to redirect_to(new_topic_path)
end
it 'honeypot is removed from params if you use a custom honeypot' do
post :create, params: { topic: { title: 'foo', subtitle: '' } }
expect(flash[:error]).not_to be_present
expect(@controller.params[:topic].key?(:subtitle)).to eq(false)
end
describe 'ActiveSupport::Notifications' do
let(:dummy_handler) { double(handle_event: nil) }
let!(:subscriber) do
subscriber = ActiveSupport::Notifications.subscribe('invisible_captcha.spam_detected') do |*args, data|
dummy_handler.handle_event(data)
end
subscriber
end
after { ActiveSupport::Notifications.unsubscribe(subscriber) }
it 'dispatches an `invisible_captcha.spam_detected` event' do
expect(dummy_handler).to receive(:handle_event).once.with({
message: "[Invisible Captcha] Potential spam detected for IP 0.0.0.0. Honeypot param 'subtitle' was present.",
remote_ip: '0.0.0.0',
user_agent: 'Rails Testing',
controller: 'topics',
action: 'create',
url: 'http://test.host/topics',
params: {
topic: { subtitle: "foo"},
controller: 'topics',
action: 'create'
}
})
post :create, params: { topic: { subtitle: 'foo' } }
end
end
end
context 'spinner attribute' do
before(:each) do
InvisibleCaptcha.spinner_enabled = true
InvisibleCaptcha.secret = 'secret'
session[:invisible_captcha_timestamp] = Time.zone.now.iso8601
session[:invisible_captcha_spinner] = '32ab649161f9f6faeeb323746de1a25d'
# Wait for valid submission
sleep InvisibleCaptcha.timestamp_threshold
end
it 'fails with no spam, but mismatch of spinner' do
post :create, params: { topic: { title: 'foo' }, spinner: 'mismatch' }
expect(response.body).to be_blank
end
it 'passes with no spam and spinner match' do
post :create, params: { topic: { title: 'foo' }, spinner: '32ab649161f9f6faeeb323746de1a25d' }
expect(response.body).to be_present
end
end
end
|