From eea77aeae7de3f4cc4088e155f7a30238c0fdb21 Mon Sep 17 00:00:00 2001 From: Evandro Junior Date: Mon, 13 Jul 2015 16:23:25 -0300 Subject: [PATCH] new tests for captcha --- lib/noosfero/api/helpers.rb | 30 ++++++++++++++++++++++++------ test/unit/api/helpers_test.rb | 21 ++++++++++++++++++++- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/noosfero/api/helpers.rb b/lib/noosfero/api/helpers.rb index 069795b..a1a3645 100644 --- a/lib/noosfero/api/helpers.rb +++ b/lib/noosfero/api/helpers.rb @@ -314,7 +314,13 @@ https.use_ssl = true request = Net::HTTP::Post.new(uri.path) request.set_form_data(verify_hash) - body = https.request(request).body + begin + body = https.request(request).body + rescue Exception => e + logger.error e + return _("Google recaptcha error: #{e.message}") + end + body = JSON.parse(body) body == "true\nsuccess" ? true : body end @@ -333,20 +339,32 @@ https.use_ssl = true request = Net::HTTP::Post.new(uri.path) request.set_form_data(verify_hash) - captcha_result = JSON.parse(https.request(request).body) + begin + body = https.request(request).body + rescue Exception => e + logger.error e + return _("Google recaptcha error: #{e.message}") + end + captcha_result = JSON.parse(body) captcha_result["success"] ? true : captcha_result end def verify_serpro_captcha(client_id, token, captcha_text, verify_uri) - if token == nil || captcha_text == nil - return _('Missing captcha data') - end + return _('Missing Serpro Captcha token') if token == nil + return _('Captcha text has not been filled') if captcha_text == nil uri = URI(verify_uri) http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri.path) verify_string = "#{client_id}&#{token}&#{captcha_text}" request.body = verify_string - body = http.request(request).body + begin + body = http.request(request).body + rescue Exception => e + logger.error e + return _("Serpro captcha error: #{e.message}") + end + return _("Wrong captcha text, please try again") if body == 0 + return _("Token not found") if body == 2 body == '1' ? true : body end diff --git a/test/unit/api/helpers_test.rb b/test/unit/api/helpers_test.rb index e4c5418..cf44186 100644 --- a/test/unit/api/helpers_test.rb +++ b/test/unit/api/helpers_test.rb @@ -209,7 +209,9 @@ class APIHelpersTest < ActiveSupport::TestCase serpro_client_id: '0000000000000000', verify_uri: 'http://localhost/api/verify', } - assert_equal test_captcha("127.0.0.1", {}, environment), "Missing captcha data" + params = {} + params[:txtToken_captcha_serpro_gov_br] = '4324343' + assert_equal test_captcha("127.0.0.1", params, environment), _('Captcha text has not been filled') end should 'render not_found if endpoint is unavailable' do @@ -234,6 +236,23 @@ class APIHelpersTest < ActiveSupport::TestCase end + should 'captcha serpro say Name or service not known' do + environment = Environment.new + environment.api_captcha_settings = { + enabled: true, + provider: 'serpro', + serpro_client_id: '0000000000000000', + verify_uri: 'http://someserverthatdoesnotexist.mycompanythatdoesnotexist.com/validate', + } + params = {} + params[:txtToken_captcha_serpro_gov_br] = '4324343' + params[:captcha_text] = '4324343' + logger = Logger.new(File.join(Rails.root, 'log', 'test_api.log')) + stubs(:logger).returns(logger) + assert_equal test_captcha('127.0.0.1', params, environment), 'Serpro captcha error: getaddrinfo: Name or service not known' + end + + protected def error!(info, status) -- libgit2 0.21.2