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,7 +314,13 @@ | ||
| 314 | https.use_ssl = true | 314 | https.use_ssl = true |
| 315 | request = Net::HTTP::Post.new(uri.path) | 315 | request = Net::HTTP::Post.new(uri.path) |
| 316 | request.set_form_data(verify_hash) | 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 | body == "true\nsuccess" ? true : body | 324 | body == "true\nsuccess" ? true : body |
| 319 | end | 325 | end |
| 320 | 326 | ||
| @@ -333,20 +339,32 @@ | @@ -333,20 +339,32 @@ | ||
| 333 | https.use_ssl = true | 339 | https.use_ssl = true |
| 334 | request = Net::HTTP::Post.new(uri.path) | 340 | request = Net::HTTP::Post.new(uri.path) |
| 335 | request.set_form_data(verify_hash) | 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 | captcha_result["success"] ? true : captcha_result | 349 | captcha_result["success"] ? true : captcha_result |
| 338 | end | 350 | end |
| 339 | 351 | ||
| 340 | def verify_serpro_captcha(client_id, token, captcha_text, verify_uri) | 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 | uri = URI(verify_uri) | 355 | uri = URI(verify_uri) |
| 345 | http = Net::HTTP.new(uri.host, uri.port) | 356 | http = Net::HTTP.new(uri.host, uri.port) |
| 346 | request = Net::HTTP::Post.new(uri.path) | 357 | request = Net::HTTP::Post.new(uri.path) |
| 347 | verify_string = "#{client_id}&#{token}&#{captcha_text}" | 358 | verify_string = "#{client_id}&#{token}&#{captcha_text}" |
| 348 | request.body = verify_string | 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 | body == '1' ? true : body | 368 | body == '1' ? true : body |
| 351 | end | 369 | end |
| 352 | 370 |
test/unit/api/helpers_test.rb
| @@ -209,7 +209,9 @@ class APIHelpersTest < ActiveSupport::TestCase | @@ -209,7 +209,9 @@ class APIHelpersTest < ActiveSupport::TestCase | ||
| 209 | serpro_client_id: '0000000000000000', | 209 | serpro_client_id: '0000000000000000', |
| 210 | verify_uri: 'http://localhost/api/verify', | 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 | end | 215 | end |
| 214 | 216 | ||
| 215 | should 'render not_found if endpoint is unavailable' do | 217 | should 'render not_found if endpoint is unavailable' do |
| @@ -234,6 +236,23 @@ class APIHelpersTest < ActiveSupport::TestCase | @@ -234,6 +236,23 @@ class APIHelpersTest < ActiveSupport::TestCase | ||
| 234 | 236 | ||
| 235 | end | 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 | protected | 256 | protected |
| 238 | 257 | ||
| 239 | def error!(info, status) | 258 | def error!(info, status) |