Commit 69a17e1dc175da2cfb7ad76c5e20cc00653083c3

Authored by Evandro Junior
1 parent be8ecf2b

refactoring captcha tests

lib/noosfero/api/helpers.rb
... ... @@ -412,10 +412,6 @@ require 'grape'
412 412 # captcha_helpers #
413 413 ##########################################
414 414  
415   - # def plugins
416   - # @plugins
417   - # end
418   -
419 415 def test_captcha(remote_ip, params, environment)
420 416 captcha_plugin_enabled = @plugins.dispatch(:test_captcha, remote_ip, params, environment)
421 417 return true if captcha_plugin_enabled.size == 0
... ...
lib/noosfero/api/session.rb
... ... @@ -59,6 +59,7 @@ module Noosfero
59 59 #requires :password_confirmation, type: String, desc: _("Password confirmation")
60 60 end
61 61 post "/register" do
  62 + # binding.pry
62 63 attrs = attributes_for_keys [:email, :login, :password, :password_confirmation] + environment.signup_person_fields
63 64 remote_ip = (request.respond_to?(:remote_ip) && request.remote_ip) || (env && env['REMOTE_ADDR'])
64 65 # test_captcha will render_api_error! and exit in case of any problem
... ...
lib/noosfero/api/v1/articles.rb
... ... @@ -138,7 +138,6 @@ module Noosfero
138 138 named 'ArticleVote'
139 139 end
140 140 post ':id/vote' do
141   - binding.pry
142 141 ## The vote api should allow regular login or with captcha
143 142 authenticate_allow_captcha!
144 143 value = (params[:value] || 1).to_i
... ...
plugins/serpro_captcha/test/test_helper.rb
... ... @@ -32,24 +32,7 @@ class ActiveSupport::TestCase
32 32 ## Performs a login using the session.rb but mocking the
33 33 ## real HTTP request to validate the captcha.
34 34 def do_login_captcha_from_api
35   - # Request mocking
36   - #Net::HTTP::Post Mock
37   - request = mock
38   - #Net::HTTP Mock
39   - http = mock
40   - uri = URI(environment.api_captcha_settings[:verify_uri])
41   - Net::HTTP.expects(:new).with(uri.host, uri.port).returns(http)
42   - Net::HTTP::Post.expects(:new).with(uri.path).returns(request)
43   -
44   - # Captcha required codes
45   - request.stubs(:body=).with("0000000000000000&4324343&4030320")
46   - http.stubs(:request).with(request).returns(http)
47   -
48   - # Captcha validation success !!
49   - http.stubs(:body).returns("1")
50   -
51   - params = {:txtToken_captcha_serpro_gov_br => '4324343', :captcha_text => '4030320'}
52   - post "#{@url}#{params.to_query}"
  35 + post "/api/v1/login-captcha"
53 36 json = JSON.parse(last_response.body)
54 37 json
55 38 end
... ...
plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb
... ... @@ -90,7 +90,6 @@ class SerproCaptchaVerificationTest < ActiveSupport::TestCase
90 90 end
91 91  
92 92 should 'perform a vote on an article identified by id' do
93   - binding.pry
94 93 login_with_captcha
95 94 article = create_article('Article 1')
96 95 params = {}
... ...
test/unit/api/login_captcha_test.rb
... ... @@ -3,20 +3,20 @@ require File.dirname(__FILE__) + '/test_helper'
3 3 class LoginCaptchaTest < ActiveSupport::TestCase
4 4  
5 5 def setup()
6   - @environment = Environment.default
7   - @environment.api_captcha_settings = {
8   - enabled: true,
9   - provider: 'serpro',
10   - serpro_client_id: '0000000000000000',
11   - verify_uri: 'http://captcha.serpro.gov.br/validate',
12   - }
13   - @environment.save!
14   - @url = "/api/v1/login-captcha?"
  6 + # @environment = Environment.default
  7 + # @environment.api_captcha_settings = {
  8 + # enabled: true,
  9 + # provider: 'serpro',
  10 + # serpro_client_id: '0000000000000000',
  11 + # verify_uri: 'http://captcha.serpro.gov.br/validate',
  12 + # }
  13 + # @environment.save!
  14 + # @url = "/api/v1/login-captcha?"
15 15 end
16 16  
17 17 def create_article(name)
18 18 person = fast_create(Person, :environment_id => @environment.id)
19   - fast_create(Article, :profile_id => person.id, :name => name)
  19 + fast_create(Article, :profile_id => person.id, :name => name)
20 20 end
21 21  
22 22 should 'not perform a vote without authentication' do
... ... @@ -42,7 +42,7 @@ class LoginCaptchaTest &lt; ActiveSupport::TestCase
42 42  
43 43 post "/api/v1/articles/#{article.id}/vote?#{params.to_query}"
44 44 json = JSON.parse(last_response.body)
45   -
  45 +
46 46 assert_not_equal 401, last_response.status
47 47 assert_equal true, json['vote']
48 48 end
... ... @@ -90,4 +90,9 @@ class LoginCaptchaTest &lt; ActiveSupport::TestCase
90 90 assert ret == @private_token
91 91 end
92 92  
93   -end
94 93 \ No newline at end of file
  94 + should 'do login captcha from api' do
  95 + pry
  96 + do_login_captcha_from_api
  97 + end
  98 +
  99 +end
... ...
test/unit/api/session_test.rb
... ... @@ -4,6 +4,7 @@ class SessionTest &lt; ActiveSupport::TestCase
4 4  
5 5 def setup
6 6 login_api
  7 + OutcomeCaptcha.outcome_captcha_test = true
7 8 end
8 9  
9 10 should 'generate private token when login' do
... ... @@ -76,13 +77,13 @@ class SessionTest &lt; ActiveSupport::TestCase
76 77 end
77 78  
78 79 should 'not register a user without email' do
79   - #binding.pry
80 80 params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => nil }
81 81 post "/api/v1/register?#{params.to_query}"
82 82 assert_equal 400, last_response.status
83 83 end
84 84  
85 85 should 'not register a duplicated user' do
  86 + # binding.pry
86 87 params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" }
87 88 post "/api/v1/register?#{params.to_query}"
88 89 post "/api/v1/register?#{params.to_query}"
... ... @@ -90,22 +91,6 @@ class SessionTest &lt; ActiveSupport::TestCase
90 91 json = JSON.parse(last_response.body)
91 92 end
92 93  
93   - should 'detected error, Name or service not known, for Serpro captcha communication' do
94   - environment = Environment.default
95   - environment.api_captcha_settings = {
96   - enabled: true,
97   - provider: 'serpro',
98   - serpro_client_id: '0000000000000000',
99   - verify_uri: 'http://someserverthatdoesnotexist.mycompanythatdoesnotexist.com/validate',
100   - }
101   - environment.save!
102   - params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com",
103   - :txtToken_captcha_serpro_gov_br => '4324343', :captcha_text => '4030320'}
104   - post "/api/v1/register?#{params.to_query}"
105   - message = JSON.parse(last_response.body)['javascript_console_message']
106   - assert_equal "Serpro captcha error: getaddrinfo: Name or service not known", message
107   - end
108   -
109 94 # TODO: Add another test cases to check register situations
110 95 should 'activate a user' do
111 96 params = {
... ... @@ -201,4 +186,12 @@ class SessionTest &lt; ActiveSupport::TestCase
201 186 assert_equal 404, last_response.status
202 187 end
203 188  
  189 + should 'do not register a user if captcha fails' do
  190 + OutcomeCaptcha.outcome_captcha_test = false
  191 + Environment.default.enable('skip_new_user_email_confirmation')
  192 + params = {:login => "newuserapi_ewa ", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" }
  193 + post "/api/v1/register?#{params.to_query}"
  194 + assert_equal 403, last_response.status
  195 + end
  196 +
204 197 end
... ...
test/unit/api/test_helper.rb
1 1 require File.dirname(__FILE__) + '/../../test_helper'
  2 +require File.join(Rails.root, '/lib/noosfero/api/helpers.rb')
  3 +
  4 +class OutcomeCaptcha
  5 + class << self
  6 + attr_accessor :outcome_captcha_test
  7 + end
  8 + @outcome_captcha_test = true
  9 +end
  10 +
  11 +module Noosfero
  12 + module API
  13 + module APIHelpers
  14 + def test_captcha(*args)
  15 + return true if OutcomeCaptcha.outcome_captcha_test
  16 + render_api_error!("Error testing captcha", 403)
  17 + end
  18 + end
  19 + end
  20 +end
2 21  
3 22 class ActiveSupport::TestCase
4 23  
5 24 include Rack::Test::Methods
  25 + include Noosfero::API::APIHelpers
6 26  
7 27 def app
8 28 Noosfero::API::API
... ... @@ -16,7 +36,8 @@ class ActiveSupport::TestCase
16 36 end
17 37  
18 38 def do_login_captcha_from_api
19   - JSON.parse("1")
  39 + #JSON.parse("")
  40 + "sasadasdaasdasdadadsadassadas"
20 41 end
21 42  
22 43 def login_api
... ...