From 61c44cf5458778d0c343b1d8a0687d43c37fb725 Mon Sep 17 00:00:00 2001 From: Evandro Junior Date: Tue, 3 Nov 2015 18:21:33 -0300 Subject: [PATCH] Main tests passing --- plugins/serpro_captcha/lib/serpro_captcha_plugin.rb | 4 ++-- plugins/serpro_captcha/lib/serpro_captcha_verification.rb | 12 ++++-------- plugins/serpro_captcha/test/test_helper.rb | 18 +++++++----------- plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb | 54 +++++++++++++++++++++++++++++++----------------------- plugins/serpro_captcha/views/ldap_plugin_admin/index.html.erb | 27 --------------------------- test/unit/api/helpers_test.rb | 80 +------------------------------------------------------------------------------- 6 files changed, 45 insertions(+), 150 deletions(-) delete mode 100644 plugins/serpro_captcha/views/ldap_plugin_admin/index.html.erb diff --git a/plugins/serpro_captcha/lib/serpro_captcha_plugin.rb b/plugins/serpro_captcha/lib/serpro_captcha_plugin.rb index 5212779..6b7fd7b 100644 --- a/plugins/serpro_captcha/lib/serpro_captcha_plugin.rb +++ b/plugins/serpro_captcha/lib/serpro_captcha_plugin.rb @@ -13,8 +13,8 @@ class SerproCaptchaPlugin < Noosfero::Plugin end def test_captcha(remote_ip, params, environment) - spv = SerproCaptchaVerification.new - return spv.verify_serpro_captcha(environment.serpro_captcha_client_id, params[:txtToken_captcha_serpro_gov_br], params[:captcha_text], environment.serpro_captcha_verify_uri) + scv = SerproCaptchaVerification.new + return scv.verify_serpro_captcha(environment.serpro_captcha_client_id, params[:txtToken_captcha_serpro_gov_br], params[:captcha_text], environment.serpro_captcha_verify_uri) end end diff --git a/plugins/serpro_captcha/lib/serpro_captcha_verification.rb b/plugins/serpro_captcha/lib/serpro_captcha_verification.rb index 6a7d1a5..1af51f5 100644 --- a/plugins/serpro_captcha/lib/serpro_captcha_verification.rb +++ b/plugins/serpro_captcha/lib/serpro_captcha_verification.rb @@ -14,18 +14,14 @@ class SerproCaptchaVerification request = Net::HTTP::Post.new(uri.path) verify_string = "#{client_id}&#{token}&#{captcha_text}" request.body = verify_string - begin - body = http.request(request).body - rescue Exception => e - return hash_error(_('Internal captcha validation error'), 500, nil, "Serpro captcha error: #{e.message}") - end + body = http.request(request).body return true if body == '1' return hash_error(_("Internal captcha validation error"), 500, body, "Unable to reach Serpro's Captcha validation service") if body == "Activity timed out" - return hash_error(_("Wrong captcha text, please try again"), 403) if body == 0 - return hash_error(_("Serpro's captcha token not found"), 500) if body == 2 + return hash_error(_("Wrong captcha text, please try again"), 403) if body == '0' + return hash_error(_("Serpro's captcha token not found"), 500) if body == '2' return hash_error(_("No data sent to validation server or other serious problem"), 500) if body == -1 #Catches all errors at the end - return hash_error(_("Internal captcha validation error"), 500, nil, "Error validating Serpro's captcha #{body}") + return hash_error(_("Internal captcha validation error"), 500, nil, "Error validating Serpro's captcha service returned: #{body}") end def hash_error(user_message, status, log_message=nil, javascript_console_message=nil) diff --git a/plugins/serpro_captcha/test/test_helper.rb b/plugins/serpro_captcha/test/test_helper.rb index 481e398..9affff2 100644 --- a/plugins/serpro_captcha/test/test_helper.rb +++ b/plugins/serpro_captcha/test/test_helper.rb @@ -8,22 +8,18 @@ class ActiveSupport::TestCase Noosfero::API::API end - def pass_captcha - stub_request(:post, "http://www.somecompany.com:443/validate"). - with(:body => "323232&642646&44641441", + def pass_captcha(mocked_url, captcha_verification_body) + stub_request(:post, mocked_url). + with(:body => captcha_verification_body, :headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}). to_return(:status => 200, :body => "1", :headers => {'Content-Length' => 1}) - spv = SerproCaptchaVerification.new - assert spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, '642646', '44641441', @environment.serpro_captcha_verify_uri) end - def fail_captcha - stub_request(:post, "http://www.somecompany.com:443/validate"). - with(:body => "323232&642646&44641441", + def fail_captcha_text(mocked_url, captcha_verification_body) + stub_request(:post, mocked_url). + with(:body => captcha_verification_body, :headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}). - to_return(:status => 200, :body => "2", :headers => {'Content-Length' => 1}) - spv = SerproCaptchaVerification.new - assert spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, '642646', '44641441', @environment.serpro_captcha_verify_uri) + to_return(:status => 200, :body => "0", :headers => {'Content-Length' => 1}) end def login_with_captcha diff --git a/plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb b/plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb index 5849f4e..aec88f3 100644 --- a/plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb +++ b/plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb @@ -8,53 +8,61 @@ class SerproCaptchaVerificationTest < ActiveSupport::TestCase def setup @environment = Environment.default @environment.enabled_plugins = ['SerproCaptchaPlugin'] - @environment.serpro_captcha_verify_uri='https://www.somecompany.com/validate' + @environment.serpro_captcha_verify_uri="http://www.somecompany.com:443/validate" @environment.serpro_captcha_client_id='323232' @environment.save! + @captcha_token = "642646" + @captcha_text = "44641441" + @captcha_verification_body = "#{@environment.serpro_captcha_client_id}&#{@captcha_token}&#{@captcha_text}" end should 'register a user when there are no enabled captcha pluging' do @environment.enabled_plugins = [] + @environment.save! Environment.default.enable('skip_new_user_email_confirmation') params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" } post "/api/v1/register?#{params.to_query}" assert_equal 201, last_response.status json = JSON.parse(last_response.body) assert User['newuserapi'].activated? - assert json['activated'] - assert json['private_token'].present? + assert json['user']['private_token'].present? end - # should 'not register a user if captcha fails' do - # fail_captcha - # Environment.default.enable('skip_new_user_email_confirmation') - # params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" } - # post "/api/v1/register?#{params.to_query}" - # assert_equal 201, last_response.status - # json = JSON.parse(last_response.body) - # refute User['newuserapi'].activated? - # refute !json['activated'] - # refute !json['private_token'].present? - # end + should 'not register a user if captcha fails' do + fail_captcha_text @environment.serpro_captcha_verify_uri, @captcha_verification_body + Environment.default.enable('skip_new_user_email_confirmation') + params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com", :txtToken_captcha_serpro_gov_br => @captcha_token, :captcha_text => @captcha_text} + post "/api/v1/register?#{params.to_query}" + assert_equal 403, last_response.status + json = JSON.parse(last_response.body) + assert_equal json["message"], _("Wrong captcha text, please try again") + end should 'verify_serpro_captcha' do - pass_captcha - spv = SerproCaptchaVerification.new - assert spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, '642646', '44641441', @environment.serpro_captcha_verify_uri) + pass_captcha @environment.serpro_captcha_verify_uri, @captcha_verification_body + scv = SerproCaptchaVerification.new + assert scv.verify_serpro_captcha(@environment.serpro_captcha_client_id, @captcha_token, '44641441', @environment.serpro_captcha_verify_uri) end should 'fail captcha if user has not filled Serpro\' captcha text' do - pass_captcha - spv = SerproCaptchaVerification.new - hash = spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, '642646', nil, @environment.serpro_captcha_verify_uri) + pass_captcha @environment.serpro_captcha_verify_uri, @captcha_verification_body + scv = SerproCaptchaVerification.new + hash = scv.verify_serpro_captcha(@environment.serpro_captcha_client_id, @captcha_token, nil, @environment.serpro_captcha_verify_uri) assert hash[:user_message], _('Captcha text has not been filled') end should 'fail captcha if Serpro\' captcha token has not been sent' do - pass_captcha - spv = SerproCaptchaVerification.new - hash = spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, nil, '76876846', @environment.serpro_captcha_verify_uri) + pass_captcha @environment.serpro_captcha_verify_uri, @captcha_verification_body + scv = SerproCaptchaVerification.new + hash = scv.verify_serpro_captcha(@environment.serpro_captcha_client_id, nil, @captcha_text, @environment.serpro_captcha_verify_uri) assert hash[:javascript_console_message], _("Missing Serpro's Captcha token") end + should 'fail captcha text' do + fail_captcha_text @environment.serpro_captcha_verify_uri, @captcha_verification_body + scv = SerproCaptchaVerification.new + hash = scv.verify_serpro_captcha(@environment.serpro_captcha_client_id, nil, @captcha_text, @environment.serpro_captcha_verify_uri) + assert hash[:javascript_console_message], _("Wrong captcha text, please try again") + end + end diff --git a/plugins/serpro_captcha/views/ldap_plugin_admin/index.html.erb b/plugins/serpro_captcha/views/ldap_plugin_admin/index.html.erb deleted file mode 100644 index e5de360..0000000 --- a/plugins/serpro_captcha/views/ldap_plugin_admin/index.html.erb +++ /dev/null @@ -1,27 +0,0 @@ -

<%= _("Serpro's Captcha Management") %>

- -<%= labelled_form_for(:environment, :url => {:action => 'update'}) do |f| %> - - - - - - - - - - - - - - -
<%= c_('Configuration') %><%= _('Value') %>
<%= _('Host') %><%= text_field :environment, :serpro_captcha_verify_uri %>
<%= _('Port') %><%= text_field :environment, :serpro_captcha_client_id %>
- -
- <% button_bar do %> - <%= submit_button('save', c_('Save changes')) %> - <%= button :back, _('Back to plugins administration panel'), :controller => 'plugins' %> - <% end %> -
- -<% end %> diff --git a/test/unit/api/helpers_test.rb b/test/unit/api/helpers_test.rb index 07c89ca..6fa97c5 100644 --- a/test/unit/api/helpers_test.rb +++ b/test/unit/api/helpers_test.rb @@ -216,89 +216,11 @@ class APIHelpersTest < ActiveSupport::TestCase ###### Captcha tests ###### -should 'do not test captcha when there are no settings' do +should 'do not test captcha when there is no captcha plugin enabled' do environment = Environment.new assert test_captcha("127.0.0.1", {}, environment) end -should 'do not test captcha when captcha is disabled on settings' do - environment = Environment.new - environment.api_captcha_settings = { - enabled: false, - } - assert test_captcha("127.0.0.1", {}, environment) -end - -should 'fail display recaptcha v1' do - environment = Environment.new - environment.api_captcha_settings = { - enabled: true, - provider: 'google', - version: 1, - private_key: '6LdsWAcTAAAAAB6maB_HalVyCc4asDAxPxloIMvY', - public_key: '6LdsWAcTAAAAAChTUUD6yu9fCDhdIZzNd7F53zf-', - verify_uri: 'https://www.google.com/recaptcha/api/verify', - } - r = test_captcha('127.0.0.1', params, environment) - assert_equal(_("Missing captcha data"), r[0][:javascript_console_message]) -end - -should 'fail display recaptcha v2' do - environment = Environment.new - environment.api_captcha_settings = { - enabled: true, - provider: 'google', - version: 2, - private_key: '6LdsWAcTAAAAAB6maB_HalVyCc4asDAxPxloIMvY', - public_key: '6LdsWAcTAAAAAChTUUD6yu9fCDhdIZzNd7F53zf-', - verify_uri: 'https://www.google.com/recaptcha/api/siteverify', - } - r = test_captcha('127.0.0.1', params, environment) - assert_equal(_("Missing captcha data"), r[0][:javascript_console_message]) -end - -should 'verify if user filled Serpro\' captcha text' do - environment = Environment.new - environment.api_captcha_settings = { - enabled: true, - provider: 'serpro', - serpro_client_id: '0000000000000000', - verify_uri: 'http://localhost/api/verify', - } - params = {} - params[:txtToken_captcha_serpro_gov_br] = '4324343' - assert_equal(_('Captcha text has not been filled'), test_captcha('127.0.0.1', params, environment)[0]['message']) -end - -should 'verify if Serpro\' captcha token has been sent' do - environment = Environment.new - environment.api_captcha_settings = { - enabled: true, - provider: 'serpro', - serpro_client_id: '0000000000000000', - verify_uri: 'http://localhost/api/verify', - } - params = {} - params[:captcha_text] = '4324343' - r = test_captcha('127.0.0.1', params, environment) - assert_equal(_("Missing Serpro's Captcha token"), r[0][:javascript_console_message]) -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' - r = test_captcha('127.0.0.1', params, environment) - assert (r[0][:javascript_console_message]).starts_with?("Serpro captcha error: getaddrinfo") -end - ###### END Captcha tests ###### protected -- libgit2 0.21.2