Commit eea77aeae7de3f4cc4088e155f7a30238c0fdb21
1 parent
197f5da4
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
new tests for captcha
Showing
2 changed files
with
44 additions
and
7 deletions
Show diff stats
lib/noosfero/api/helpers.rb
... | ... | @@ -314,7 +314,13 @@ |
314 | 314 | https.use_ssl = true |
315 | 315 | request = Net::HTTP::Post.new(uri.path) |
316 | 316 | request.set_form_data(verify_hash) |
317 | - body = https.request(request).body | |
317 | + begin | |
318 | + body = https.request(request).body | |
319 | + rescue Exception => e | |
320 | + logger.error e | |
321 | + return _("Google recaptcha error: #{e.message}") | |
322 | + end | |
323 | + body = JSON.parse(body) | |
318 | 324 | body == "true\nsuccess" ? true : body |
319 | 325 | end |
320 | 326 | |
... | ... | @@ -333,20 +339,32 @@ |
333 | 339 | https.use_ssl = true |
334 | 340 | request = Net::HTTP::Post.new(uri.path) |
335 | 341 | request.set_form_data(verify_hash) |
336 | - captcha_result = JSON.parse(https.request(request).body) | |
342 | + begin | |
343 | + body = https.request(request).body | |
344 | + rescue Exception => e | |
345 | + logger.error e | |
346 | + return _("Google recaptcha error: #{e.message}") | |
347 | + end | |
348 | + captcha_result = JSON.parse(body) | |
337 | 349 | captcha_result["success"] ? true : captcha_result |
338 | 350 | end |
339 | 351 | |
340 | 352 | def verify_serpro_captcha(client_id, token, captcha_text, verify_uri) |
341 | - if token == nil || captcha_text == nil | |
342 | - return _('Missing captcha data') | |
343 | - end | |
353 | + return _('Missing Serpro Captcha token') if token == nil | |
354 | + return _('Captcha text has not been filled') if captcha_text == nil | |
344 | 355 | uri = URI(verify_uri) |
345 | 356 | http = Net::HTTP.new(uri.host, uri.port) |
346 | 357 | request = Net::HTTP::Post.new(uri.path) |
347 | 358 | verify_string = "#{client_id}&#{token}&#{captcha_text}" |
348 | 359 | request.body = verify_string |
349 | - body = http.request(request).body | |
360 | + begin | |
361 | + body = http.request(request).body | |
362 | + rescue Exception => e | |
363 | + logger.error e | |
364 | + return _("Serpro captcha error: #{e.message}") | |
365 | + end | |
366 | + return _("Wrong captcha text, please try again") if body == 0 | |
367 | + return _("Token not found") if body == 2 | |
350 | 368 | body == '1' ? true : body |
351 | 369 | end |
352 | 370 | ... | ... |
test/unit/api/helpers_test.rb
... | ... | @@ -209,7 +209,9 @@ class APIHelpersTest < ActiveSupport::TestCase |
209 | 209 | serpro_client_id: '0000000000000000', |
210 | 210 | verify_uri: 'http://localhost/api/verify', |
211 | 211 | } |
212 | - assert_equal test_captcha("127.0.0.1", {}, environment), "Missing captcha data" | |
212 | + params = {} | |
213 | + params[:txtToken_captcha_serpro_gov_br] = '4324343' | |
214 | + assert_equal test_captcha("127.0.0.1", params, environment), _('Captcha text has not been filled') | |
213 | 215 | end |
214 | 216 | |
215 | 217 | should 'render not_found if endpoint is unavailable' do |
... | ... | @@ -234,6 +236,23 @@ class APIHelpersTest < ActiveSupport::TestCase |
234 | 236 | |
235 | 237 | end |
236 | 238 | |
239 | + should 'captcha serpro say Name or service not known' do | |
240 | + environment = Environment.new | |
241 | + environment.api_captcha_settings = { | |
242 | + enabled: true, | |
243 | + provider: 'serpro', | |
244 | + serpro_client_id: '0000000000000000', | |
245 | + verify_uri: 'http://someserverthatdoesnotexist.mycompanythatdoesnotexist.com/validate', | |
246 | + } | |
247 | + params = {} | |
248 | + params[:txtToken_captcha_serpro_gov_br] = '4324343' | |
249 | + params[:captcha_text] = '4324343' | |
250 | + logger = Logger.new(File.join(Rails.root, 'log', 'test_api.log')) | |
251 | + stubs(:logger).returns(logger) | |
252 | + assert_equal test_captcha('127.0.0.1', params, environment), 'Serpro captcha error: getaddrinfo: Name or service not known' | |
253 | + end | |
254 | + | |
255 | + | |
237 | 256 | protected |
238 | 257 | |
239 | 258 | def error!(info, status) | ... | ... |