Commit 69a17e1dc175da2cfb7ad76c5e20cc00653083c3
1 parent
be8ecf2b
Exists in
captcha_serpro_plugin
refactoring captcha tests
Showing
8 changed files
with
51 additions
and
54 deletions
Show diff stats
lib/noosfero/api/helpers.rb
@@ -412,10 +412,6 @@ require 'grape' | @@ -412,10 +412,6 @@ require 'grape' | ||
412 | # captcha_helpers # | 412 | # captcha_helpers # |
413 | ########################################## | 413 | ########################################## |
414 | 414 | ||
415 | - # def plugins | ||
416 | - # @plugins | ||
417 | - # end | ||
418 | - | ||
419 | def test_captcha(remote_ip, params, environment) | 415 | def test_captcha(remote_ip, params, environment) |
420 | captcha_plugin_enabled = @plugins.dispatch(:test_captcha, remote_ip, params, environment) | 416 | captcha_plugin_enabled = @plugins.dispatch(:test_captcha, remote_ip, params, environment) |
421 | return true if captcha_plugin_enabled.size == 0 | 417 | return true if captcha_plugin_enabled.size == 0 |
lib/noosfero/api/session.rb
@@ -59,6 +59,7 @@ module Noosfero | @@ -59,6 +59,7 @@ module Noosfero | ||
59 | #requires :password_confirmation, type: String, desc: _("Password confirmation") | 59 | #requires :password_confirmation, type: String, desc: _("Password confirmation") |
60 | end | 60 | end |
61 | post "/register" do | 61 | post "/register" do |
62 | + # binding.pry | ||
62 | attrs = attributes_for_keys [:email, :login, :password, :password_confirmation] + environment.signup_person_fields | 63 | attrs = attributes_for_keys [:email, :login, :password, :password_confirmation] + environment.signup_person_fields |
63 | remote_ip = (request.respond_to?(:remote_ip) && request.remote_ip) || (env && env['REMOTE_ADDR']) | 64 | remote_ip = (request.respond_to?(:remote_ip) && request.remote_ip) || (env && env['REMOTE_ADDR']) |
64 | # test_captcha will render_api_error! and exit in case of any problem | 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,7 +138,6 @@ module Noosfero | ||
138 | named 'ArticleVote' | 138 | named 'ArticleVote' |
139 | end | 139 | end |
140 | post ':id/vote' do | 140 | post ':id/vote' do |
141 | - binding.pry | ||
142 | ## The vote api should allow regular login or with captcha | 141 | ## The vote api should allow regular login or with captcha |
143 | authenticate_allow_captcha! | 142 | authenticate_allow_captcha! |
144 | value = (params[:value] || 1).to_i | 143 | value = (params[:value] || 1).to_i |
plugins/serpro_captcha/test/test_helper.rb
@@ -32,24 +32,7 @@ class ActiveSupport::TestCase | @@ -32,24 +32,7 @@ class ActiveSupport::TestCase | ||
32 | ## Performs a login using the session.rb but mocking the | 32 | ## Performs a login using the session.rb but mocking the |
33 | ## real HTTP request to validate the captcha. | 33 | ## real HTTP request to validate the captcha. |
34 | def do_login_captcha_from_api | 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 | json = JSON.parse(last_response.body) | 36 | json = JSON.parse(last_response.body) |
54 | json | 37 | json |
55 | end | 38 | end |
plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb
@@ -90,7 +90,6 @@ class SerproCaptchaVerificationTest < ActiveSupport::TestCase | @@ -90,7 +90,6 @@ class SerproCaptchaVerificationTest < ActiveSupport::TestCase | ||
90 | end | 90 | end |
91 | 91 | ||
92 | should 'perform a vote on an article identified by id' do | 92 | should 'perform a vote on an article identified by id' do |
93 | - binding.pry | ||
94 | login_with_captcha | 93 | login_with_captcha |
95 | article = create_article('Article 1') | 94 | article = create_article('Article 1') |
96 | params = {} | 95 | params = {} |
test/unit/api/login_captcha_test.rb
@@ -3,20 +3,20 @@ require File.dirname(__FILE__) + '/test_helper' | @@ -3,20 +3,20 @@ require File.dirname(__FILE__) + '/test_helper' | ||
3 | class LoginCaptchaTest < ActiveSupport::TestCase | 3 | class LoginCaptchaTest < ActiveSupport::TestCase |
4 | 4 | ||
5 | def setup() | 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 | end | 15 | end |
16 | 16 | ||
17 | def create_article(name) | 17 | def create_article(name) |
18 | person = fast_create(Person, :environment_id => @environment.id) | 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 | end | 20 | end |
21 | 21 | ||
22 | should 'not perform a vote without authentication' do | 22 | should 'not perform a vote without authentication' do |
@@ -42,7 +42,7 @@ class LoginCaptchaTest < ActiveSupport::TestCase | @@ -42,7 +42,7 @@ class LoginCaptchaTest < ActiveSupport::TestCase | ||
42 | 42 | ||
43 | post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" | 43 | post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" |
44 | json = JSON.parse(last_response.body) | 44 | json = JSON.parse(last_response.body) |
45 | - | 45 | + |
46 | assert_not_equal 401, last_response.status | 46 | assert_not_equal 401, last_response.status |
47 | assert_equal true, json['vote'] | 47 | assert_equal true, json['vote'] |
48 | end | 48 | end |
@@ -90,4 +90,9 @@ class LoginCaptchaTest < ActiveSupport::TestCase | @@ -90,4 +90,9 @@ class LoginCaptchaTest < ActiveSupport::TestCase | ||
90 | assert ret == @private_token | 90 | assert ret == @private_token |
91 | end | 91 | end |
92 | 92 | ||
93 | -end | ||
94 | \ No newline at end of file | 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 < ActiveSupport::TestCase | @@ -4,6 +4,7 @@ class SessionTest < ActiveSupport::TestCase | ||
4 | 4 | ||
5 | def setup | 5 | def setup |
6 | login_api | 6 | login_api |
7 | + OutcomeCaptcha.outcome_captcha_test = true | ||
7 | end | 8 | end |
8 | 9 | ||
9 | should 'generate private token when login' do | 10 | should 'generate private token when login' do |
@@ -76,13 +77,13 @@ class SessionTest < ActiveSupport::TestCase | @@ -76,13 +77,13 @@ class SessionTest < ActiveSupport::TestCase | ||
76 | end | 77 | end |
77 | 78 | ||
78 | should 'not register a user without email' do | 79 | should 'not register a user without email' do |
79 | - #binding.pry | ||
80 | params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => nil } | 80 | params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => nil } |
81 | post "/api/v1/register?#{params.to_query}" | 81 | post "/api/v1/register?#{params.to_query}" |
82 | assert_equal 400, last_response.status | 82 | assert_equal 400, last_response.status |
83 | end | 83 | end |
84 | 84 | ||
85 | should 'not register a duplicated user' do | 85 | should 'not register a duplicated user' do |
86 | + # binding.pry | ||
86 | params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" } | 87 | params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" } |
87 | post "/api/v1/register?#{params.to_query}" | 88 | post "/api/v1/register?#{params.to_query}" |
88 | post "/api/v1/register?#{params.to_query}" | 89 | post "/api/v1/register?#{params.to_query}" |
@@ -90,22 +91,6 @@ class SessionTest < ActiveSupport::TestCase | @@ -90,22 +91,6 @@ class SessionTest < ActiveSupport::TestCase | ||
90 | json = JSON.parse(last_response.body) | 91 | json = JSON.parse(last_response.body) |
91 | end | 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 | # TODO: Add another test cases to check register situations | 94 | # TODO: Add another test cases to check register situations |
110 | should 'activate a user' do | 95 | should 'activate a user' do |
111 | params = { | 96 | params = { |
@@ -201,4 +186,12 @@ class SessionTest < ActiveSupport::TestCase | @@ -201,4 +186,12 @@ class SessionTest < ActiveSupport::TestCase | ||
201 | assert_equal 404, last_response.status | 186 | assert_equal 404, last_response.status |
202 | end | 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 | end | 197 | end |
test/unit/api/test_helper.rb
1 | require File.dirname(__FILE__) + '/../../test_helper' | 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 | class ActiveSupport::TestCase | 22 | class ActiveSupport::TestCase |
4 | 23 | ||
5 | include Rack::Test::Methods | 24 | include Rack::Test::Methods |
25 | + include Noosfero::API::APIHelpers | ||
6 | 26 | ||
7 | def app | 27 | def app |
8 | Noosfero::API::API | 28 | Noosfero::API::API |
@@ -16,7 +36,8 @@ class ActiveSupport::TestCase | @@ -16,7 +36,8 @@ class ActiveSupport::TestCase | ||
16 | end | 36 | end |
17 | 37 | ||
18 | def do_login_captcha_from_api | 38 | def do_login_captcha_from_api |
19 | - JSON.parse("1") | 39 | + #JSON.parse("") |
40 | + "sasadasdaasdasdadadsadassadas" | ||
20 | end | 41 | end |
21 | 42 | ||
22 | def login_api | 43 | def login_api |