Commit 88227abbc3ec9b5fe99aebc4349a410e7468a881

Authored by Evandro Junior
1 parent 5940f0c6
Exists in master

fixed asserting hash as true

lib/recaptcha_verification.rb
... ... @@ -10,7 +10,6 @@ class RecaptchaVerification
10 10 if recaptcha_challenge_field == nil || recaptcha_response_field == nil
11 11 return hash_error(_('Captcha validation error'), 500, nil, _('Missing captcha data'))
12 12 end
13   -
14 13 verify_hash = {
15 14 "privatekey" => private_key,
16 15 "remoteip" => remote_ip,
... ... @@ -22,15 +21,12 @@ class RecaptchaVerification
22 21 https.use_ssl = true
23 22 request = Net::HTTP::Post.new(uri.path)
24 23 request.set_form_data(verify_hash)
25   - # begin
26   - result = https.request(request).body.split("\n")
27   - # rescue Exception => e
28   - # return hash_error(_('Internal captcha validation error'), 500, nil, "Error validating Googles' recaptcha version 1: #{e.message}")
29   - # end
30   - return true if result[0] == "true"
31   - return hash_error(_("Wrong captcha text, please try again"), 403, nil, "Error validating Googles' recaptcha version 1: #{result[1]}") if result[1] == "incorrect-captcha-sol"
  24 + body = https.request(request).body
  25 + captcha_result = JSON.parse(body)
  26 + return true if captcha_result["success"]
  27 + return hash_error(_("Wrong captcha text, please try again"), 403, nil, "Error validating Googles' recaptcha version 1: #{captcha_result["error-codes"]}") if captcha_result["error-codes"] == "incorrect-captcha-sol"
32 28 #Catches all errors at the end
33   - return hash_error(_("Internal recaptcha validation error"), 500, nil, "Error validating Googles' recaptcha version 1: #{result[1]}")
  29 + return hash_error(_("Internal recaptcha validation error"), 500, nil, "Error validating Googles' recaptcha version 1: #{captcha_result["error-codes"]}")
34 30 end
35 31  
36 32 # return true or a hash with the error
... ... @@ -47,40 +43,10 @@ class RecaptchaVerification
47 43 https.use_ssl = true
48 44 request = Net::HTTP::Post.new(uri.path)
49 45 request.set_form_data(verify_hash)
50   - # begin
51   - body = https.request(request).body
52   - # rescue Exception => e
53   - # return hash_error(_('Internal captcha validation error'), 500, nil, "recaptcha error: #{e.message}")
54   - # end
  46 + body = https.request(request).body
55 47 captcha_result = JSON.parse(body)
56 48 return true if captcha_result["success"]
57 49 return hash_error(_("Wrong captcha text, please try again"), 403, body, captcha_result["error-codes"])
58 50 end
59 51  
60   - # return true or a hash with the error
61   - # :user_message, :status, :log_message, :javascript_console_message
62   - def verify_serpro_captcha(client_id, token, captcha_text, verify_uri)
63   - msg_icve = _('Internal captcha validation error')
64   - msg_esca = 'Environment recaptcha_plugin_attributes'
65   - return hash_error(msg_icve, 500, nil, "#{msg_esca} verify_uri not defined") if verify_uri.nil?
66   - return hash_error(msg_icve, 500, nil, "#{msg_esca} client_id not defined") if client_id.nil?
67   - return hash_error(_("Error processing token validation"), 500, nil, _("Missing Serpro's Captcha token")) unless token
68   - return hash_error(_('Captcha text has not been filled'), 403) unless captcha_text
69   - uri = URI(verify_uri)
70   - http = Net::HTTP.new(uri.host, uri.port)
71   - request = Net::HTTP::Post.new(uri.path)
72   - verify_string = "#{client_id}&#{token}&#{captcha_text}"
73   - request.body = verify_string
74   - body = http.request(request).body
75   - return true if body == '1'
76   - return hash_error(_("Internal captcha validation error"), 500, body, "Unable to reach Serpro's Captcha validation service") if body == "Activity timed out"
77   - return hash_error(_("Wrong captcha text, please try again"), 403) if body == '0'
78   - return hash_error(_("Serpro's captcha token not found"), 500) if body == '2'
79   - return hash_error(_("No data sent to validation server or other serious problem"), 500) if body == -1
80   - #Catches all errors at the end
81   - return hash_error(_("Internal captcha validation error"), 500, nil, "Error validating Serpro's captcha service returned: #{body}")
82   - end
83   -
84   -
85   -
86 52 end
... ...
test/test_helper.rb
... ... @@ -30,9 +30,7 @@ class ActiveSupport::TestCase
30 30 remoteip: "127.0.0.1"}
31 31 end
32 32  
33   - return_body = "{
34   - \"success\": #{pass}
35   - }"
  33 + return_body = "{\"success\": #{pass} }"
36 34  
37 35 stub_request(:post, @verify_uri).
38 36 with(:body => body,
... ...
test/unit/recaptcha_verification_test.rb
... ... @@ -17,17 +17,16 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase
17 17 @params = {}
18 18 @params[:remoteip] = @remoteip
19 19 if version.to_i == 1
20   - #wont go to google thanks to webmock
  20 + # won't go to google thanks to webmock
21 21 @verify_uri = 'https://www.google.com/recaptcha/api/verify'
22 22 @params[:privatekey] = @environment.recaptcha_private_key
23 23 @params[:challenge] = "challenge"
24 24 @params[:response] = "response"
25   -
26 25 @params[:recaptcha_challenge_field] = @params[:challenge]
27 26 @params[:recaptcha_response_field] = @params[:response]
28 27 end
29 28 if version.to_i == 2
30   - #wont go to google thanks to webmock
  29 + # won't go to google thanks to webmock
31 30 @verify_uri = 'https://www.google.com/recaptcha/api/siteverify'
32 31 @params[:secret] = @environment.recaptcha_private_key
33 32 @params[:response] = "response"
... ... @@ -55,7 +54,8 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase
55 54 setup_captcha(version)
56 55 validate_captcha(version)
57 56 r = RecaptchaPlugin.new.test_captcha(@remoteip, @params, @environment)
58   - assert r
  57 + assert_not_kind_of Hash, r
  58 + assert_equal true, r
59 59 end
60 60  
61 61 should 'fail recaptcha version 1' do
... ... @@ -63,16 +63,16 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase
63 63 setup_captcha(version)
64 64 validate_captcha(version, false)
65 65 r = RecaptchaPlugin.new.test_captcha(@remoteip, @params, @environment)
66   - assert r
  66 + assert_kind_of Hash, r
67 67 end
68 68  
69 69 should 'pass recaptcha version 2' do
70 70 version = 2
71 71 setup_captcha(version)
72 72 validate_captcha(version)
73   - rp = RecaptchaPlugin.new
74   - r = rp.test_captcha(@remoteip, @params, @environment)
75   - assert r
  73 + r = RecaptchaPlugin.new.test_captcha(@remoteip, @params, @environment)
  74 + assert_not_kind_of Hash, r
  75 + assert_equal true, r
76 76 end
77 77  
78 78 should 'fail recaptcha version 2' do
... ... @@ -80,6 +80,7 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase
80 80 setup_captcha(version)
81 81 validate_captcha(version, false)
82 82 r = RecaptchaPlugin.new.test_captcha(@remoteip, @params, @environment)
  83 + assert_kind_of Hash, r
83 84 assert_equal r[:user_message], _("Wrong captcha text, please try again")
84 85 end
85 86  
... ... @@ -109,7 +110,6 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase
109 110 assert_equal json["message"], _("Wrong captcha text, please try again")
110 111 end
111 112  
112   -
113 113 should 'fail captcha if user has not filled recaptcha_verify_uri v1 text' do
114 114 version = 1
115 115 setup_captcha(version)
... ... @@ -124,17 +124,31 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase
124 124 article = create_article('Article 1')
125 125 params = {}
126 126 params[:value] = 1
  127 + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}"
  128 + json = JSON.parse(last_response.body)
  129 + assert_equal 401, last_response.status
  130 + end
127 131  
  132 + should 'not perform a vote if recaptcha 1 fails' do
  133 + version = 1
  134 + setup_captcha(version)
  135 + validate_captcha(version, false)
  136 + post "/api/v1/login-captcha?#{@params.to_query}"
  137 + json = JSON.parse(last_response.body)
  138 + article = create_article('Article 1')
  139 + params = {}
  140 + params[:private_token] = json['private_token']
  141 + params[:value] = 1
128 142 post "/api/v1/articles/#{article.id}/vote?#{params.to_query}"
129 143 json = JSON.parse(last_response.body)
130 144 assert_equal 401, last_response.status
131 145 end
132 146  
133   - should 'perform a vote on an article identified by id' do
134   - version = 2
  147 + should 'perform a vote on an article identified by id using recaptcha 1' do
  148 + version = 1
135 149 setup_captcha(version)
136 150 validate_captcha(version)
137   - post "/api/v1/login-captcha?#{params.to_query}"
  151 + post "/api/v1/login-captcha?#{@params.to_query}"
138 152 json = JSON.parse(last_response.body)
139 153 article = create_article('Article 1')
140 154 params = {}
... ... @@ -146,10 +160,10 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase
146 160 assert_equal true, json['vote']
147 161 end
148 162  
149   - should 'not perform a vote if recaptcha 2 fails' do
  163 + should 'perform a vote on an article identified by id using recaptcha 2' do
150 164 version = 2
151 165 setup_captcha(version)
152   - validate_captcha(version, false)
  166 + validate_captcha(version)
153 167 post "/api/v1/login-captcha?#{@params.to_query}"
154 168 json = JSON.parse(last_response.body)
155 169 article = create_article('Article 1')
... ... @@ -158,11 +172,12 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase
158 172 params[:value] = 1
159 173 post "/api/v1/articles/#{article.id}/vote?#{params.to_query}"
160 174 json = JSON.parse(last_response.body)
161   - assert_equal 401, last_response.status
  175 + assert_not_equal 401, last_response.status
  176 + assert_equal true, json['vote']
162 177 end
163 178  
164   - should 'not perform a vote if recaptcha 1 fails' do
165   - version = 1
  179 + should 'not perform a vote if recaptcha 2 fails' do
  180 + version = 2
166 181 setup_captcha(version)
167 182 validate_captcha(version, false)
168 183 post "/api/v1/login-captcha?#{@params.to_query}"
... ... @@ -176,5 +191,4 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase
176 191 assert_equal 401, last_response.status
177 192 end
178 193  
179   -
180 194 end
... ...