From 69a17e1dc175da2cfb7ad76c5e20cc00653083c3 Mon Sep 17 00:00:00 2001 From: Evandro Junior Date: Fri, 6 Nov 2015 18:32:22 -0300 Subject: [PATCH] refactoring captcha tests --- lib/noosfero/api/helpers.rb | 4 ---- lib/noosfero/api/session.rb | 1 + lib/noosfero/api/v1/articles.rb | 1 - plugins/serpro_captcha/test/test_helper.rb | 19 +------------------ plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb | 1 - test/unit/api/login_captcha_test.rb | 29 +++++++++++++++++------------ test/unit/api/session_test.rb | 27 ++++++++++----------------- test/unit/api/test_helper.rb | 23 ++++++++++++++++++++++- 8 files changed, 51 insertions(+), 54 deletions(-) diff --git a/lib/noosfero/api/helpers.rb b/lib/noosfero/api/helpers.rb index 4c1c07d..24f4a60 100644 --- a/lib/noosfero/api/helpers.rb +++ b/lib/noosfero/api/helpers.rb @@ -412,10 +412,6 @@ require 'grape' # captcha_helpers # ########################################## - # def plugins - # @plugins - # end - def test_captcha(remote_ip, params, environment) captcha_plugin_enabled = @plugins.dispatch(:test_captcha, remote_ip, params, environment) return true if captcha_plugin_enabled.size == 0 diff --git a/lib/noosfero/api/session.rb b/lib/noosfero/api/session.rb index 02a2baf..40457d5 100644 --- a/lib/noosfero/api/session.rb +++ b/lib/noosfero/api/session.rb @@ -59,6 +59,7 @@ module Noosfero #requires :password_confirmation, type: String, desc: _("Password confirmation") end post "/register" do + # binding.pry attrs = attributes_for_keys [:email, :login, :password, :password_confirmation] + environment.signup_person_fields remote_ip = (request.respond_to?(:remote_ip) && request.remote_ip) || (env && env['REMOTE_ADDR']) # test_captcha will render_api_error! and exit in case of any problem diff --git a/lib/noosfero/api/v1/articles.rb b/lib/noosfero/api/v1/articles.rb index 9ee352b..4d843f8 100644 --- a/lib/noosfero/api/v1/articles.rb +++ b/lib/noosfero/api/v1/articles.rb @@ -138,7 +138,6 @@ module Noosfero named 'ArticleVote' end post ':id/vote' do - binding.pry ## The vote api should allow regular login or with captcha authenticate_allow_captcha! value = (params[:value] || 1).to_i diff --git a/plugins/serpro_captcha/test/test_helper.rb b/plugins/serpro_captcha/test/test_helper.rb index 9affff2..c4539b5 100644 --- a/plugins/serpro_captcha/test/test_helper.rb +++ b/plugins/serpro_captcha/test/test_helper.rb @@ -32,24 +32,7 @@ class ActiveSupport::TestCase ## Performs a login using the session.rb but mocking the ## real HTTP request to validate the captcha. def do_login_captcha_from_api - # Request mocking - #Net::HTTP::Post Mock - request = mock - #Net::HTTP Mock - http = mock - uri = URI(environment.api_captcha_settings[:verify_uri]) - Net::HTTP.expects(:new).with(uri.host, uri.port).returns(http) - Net::HTTP::Post.expects(:new).with(uri.path).returns(request) - - # Captcha required codes - request.stubs(:body=).with("0000000000000000&4324343&4030320") - http.stubs(:request).with(request).returns(http) - - # Captcha validation success !! - http.stubs(:body).returns("1") - - params = {:txtToken_captcha_serpro_gov_br => '4324343', :captcha_text => '4030320'} - post "#{@url}#{params.to_query}" + post "/api/v1/login-captcha" json = JSON.parse(last_response.body) json end 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 c8768a4..af6dd2c 100644 --- a/plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb +++ b/plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb @@ -90,7 +90,6 @@ class SerproCaptchaVerificationTest < ActiveSupport::TestCase end should 'perform a vote on an article identified by id' do - binding.pry login_with_captcha article = create_article('Article 1') params = {} diff --git a/test/unit/api/login_captcha_test.rb b/test/unit/api/login_captcha_test.rb index 78de0c3..51e317b 100644 --- a/test/unit/api/login_captcha_test.rb +++ b/test/unit/api/login_captcha_test.rb @@ -3,20 +3,20 @@ require File.dirname(__FILE__) + '/test_helper' class LoginCaptchaTest < ActiveSupport::TestCase def setup() - @environment = Environment.default - @environment.api_captcha_settings = { - enabled: true, - provider: 'serpro', - serpro_client_id: '0000000000000000', - verify_uri: 'http://captcha.serpro.gov.br/validate', - } - @environment.save! - @url = "/api/v1/login-captcha?" + # @environment = Environment.default + # @environment.api_captcha_settings = { + # enabled: true, + # provider: 'serpro', + # serpro_client_id: '0000000000000000', + # verify_uri: 'http://captcha.serpro.gov.br/validate', + # } + # @environment.save! + # @url = "/api/v1/login-captcha?" end def create_article(name) person = fast_create(Person, :environment_id => @environment.id) - fast_create(Article, :profile_id => person.id, :name => name) + fast_create(Article, :profile_id => person.id, :name => name) end should 'not perform a vote without authentication' do @@ -42,7 +42,7 @@ class LoginCaptchaTest < ActiveSupport::TestCase post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" json = JSON.parse(last_response.body) - + assert_not_equal 401, last_response.status assert_equal true, json['vote'] end @@ -90,4 +90,9 @@ class LoginCaptchaTest < ActiveSupport::TestCase assert ret == @private_token end -end \ No newline at end of file + should 'do login captcha from api' do + pry + do_login_captcha_from_api + end + +end diff --git a/test/unit/api/session_test.rb b/test/unit/api/session_test.rb index 44046f9..40a7da3 100644 --- a/test/unit/api/session_test.rb +++ b/test/unit/api/session_test.rb @@ -4,6 +4,7 @@ class SessionTest < ActiveSupport::TestCase def setup login_api + OutcomeCaptcha.outcome_captcha_test = true end should 'generate private token when login' do @@ -76,13 +77,13 @@ class SessionTest < ActiveSupport::TestCase end should 'not register a user without email' do - #binding.pry params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => nil } post "/api/v1/register?#{params.to_query}" assert_equal 400, last_response.status end should 'not register a duplicated user' do + # binding.pry params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" } post "/api/v1/register?#{params.to_query}" post "/api/v1/register?#{params.to_query}" @@ -90,22 +91,6 @@ class SessionTest < ActiveSupport::TestCase json = JSON.parse(last_response.body) end - should 'detected error, Name or service not known, for Serpro captcha communication' do - environment = Environment.default - environment.api_captcha_settings = { - enabled: true, - provider: 'serpro', - serpro_client_id: '0000000000000000', - verify_uri: 'http://someserverthatdoesnotexist.mycompanythatdoesnotexist.com/validate', - } - environment.save! - params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com", - :txtToken_captcha_serpro_gov_br => '4324343', :captcha_text => '4030320'} - post "/api/v1/register?#{params.to_query}" - message = JSON.parse(last_response.body)['javascript_console_message'] - assert_equal "Serpro captcha error: getaddrinfo: Name or service not known", message - end - # TODO: Add another test cases to check register situations should 'activate a user' do params = { @@ -201,4 +186,12 @@ class SessionTest < ActiveSupport::TestCase assert_equal 404, last_response.status end + should 'do not register a user if captcha fails' do + OutcomeCaptcha.outcome_captcha_test = false + Environment.default.enable('skip_new_user_email_confirmation') + params = {:login => "newuserapi_ewa ", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" } + post "/api/v1/register?#{params.to_query}" + assert_equal 403, last_response.status + end + end diff --git a/test/unit/api/test_helper.rb b/test/unit/api/test_helper.rb index 73e9f29..a72f0af 100644 --- a/test/unit/api/test_helper.rb +++ b/test/unit/api/test_helper.rb @@ -1,8 +1,28 @@ require File.dirname(__FILE__) + '/../../test_helper' +require File.join(Rails.root, '/lib/noosfero/api/helpers.rb') + +class OutcomeCaptcha + class << self + attr_accessor :outcome_captcha_test + end + @outcome_captcha_test = true +end + +module Noosfero + module API + module APIHelpers + def test_captcha(*args) + return true if OutcomeCaptcha.outcome_captcha_test + render_api_error!("Error testing captcha", 403) + end + end + end +end class ActiveSupport::TestCase include Rack::Test::Methods + include Noosfero::API::APIHelpers def app Noosfero::API::API @@ -16,7 +36,8 @@ class ActiveSupport::TestCase end def do_login_captcha_from_api - JSON.parse("1") + #JSON.parse("") + "sasadasdaasdasdadadsadassadas" end def login_api -- libgit2 0.21.2