Commit 1d5fb8d91aa61e047e8e07b6402cfc3171afed55
1 parent
6b8b1783
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Fixed all tests for test/unit/api/helpers_test.rb
Showing
2 changed files
with
95 additions
and
121 deletions
Show diff stats
lib/noosfero/api/helpers.rb
... | ... | @@ -243,26 +243,19 @@ require 'grape' |
243 | 243 | render_api_error!(_('Method Not Allowed'), 405) |
244 | 244 | end |
245 | 245 | |
246 | - # render_api_error!(message, status) | |
247 | - # error!({'message' => message, :code => status}, status) | |
248 | - # end | |
249 | - | |
250 | 246 | # javascript_console_message is supposed to be executed as console.log() |
251 | 247 | def render_api_error!(user_message, status, log_message = nil, javascript_console_message = nil) |
252 | - status||= 400 | |
253 | 248 | message_hash = {'message' => user_message, :code => status} |
254 | 249 | message_hash[:javascript_console_message] = javascript_console_message if javascript_console_message.present? |
255 | 250 | log_msg = "#{status}, User message: #{user_message}" |
256 | 251 | log_msg = "#{log_message}, #{log_msg}" if log_message.present? |
257 | 252 | log_msg = "#{log_msg}, Javascript Console Message: #{javascript_console_message}" if javascript_console_message.present? |
258 | -# headers = { Grape::Http::Headers::CONTENT_TYPE => content_type }.merge(headers) | |
259 | -# rack_response(format_message(message, backtrace), status, headers) | |
260 | -# raise log_msg | |
261 | - #Since throw :error is not logging the errors I had to manually log it! | |
262 | - #log(log_msg) | |
263 | 253 | logger.error log_msg |
264 | - error!(message_hash, status) | |
265 | -# throw :error, message: message_hash, status: status, headers: headers | |
254 | + if javascript_console_message.present? | |
255 | + error!(message_hash, status) | |
256 | + else | |
257 | + error!(user_message, status) | |
258 | + end | |
266 | 259 | end |
267 | 260 | |
268 | 261 | def render_api_errors!(messages) |
... | ... | @@ -331,11 +324,11 @@ require 'grape' |
331 | 324 | return true unless d[:enabled] == true |
332 | 325 | msg_icve = _('Internal captcha validation error') |
333 | 326 | msg_eacs = 'Environment api_captcha_settings' |
334 | - s = 503 | |
327 | + s = 500 | |
335 | 328 | |
336 | 329 | if d[:provider] == 'google' |
337 | - render_api_error!(msg_icve, s, nil, "#{msg_eacs} private_key not defined") if d[:private_key].nil? | |
338 | - render_api_error!(msg_icve, s, nil, "#{msg_eacs} version not defined") unless d[:version] == 1 || d[:version] == 2 | |
330 | + return render_api_error!(msg_icve, s, nil, "#{msg_eacs} private_key not defined") if d[:private_key].nil? | |
331 | + return render_api_error!(msg_icve, s, nil, "#{msg_eacs} version not defined") unless d[:version] == 1 || d[:version] == 2 | |
339 | 332 | if d[:version] == 1 |
340 | 333 | d[:verify_uri] ||= 'https://www.google.com/recaptcha/api/verify' |
341 | 334 | return verify_recaptcha_v1(remote_ip, d[:private_key], d[:verify_uri], params[:recaptcha_challenge_field], params[:recaptcha_response_field]) |
... | ... | @@ -346,15 +339,15 @@ require 'grape' |
346 | 339 | end |
347 | 340 | end |
348 | 341 | if d[:provider] == 'serpro' |
349 | - render_api_error!(msg_icve, s, nil, "#{msg_eacs} verify_uri not defined") if d[:verify_uri].nil? | |
342 | + return render_api_error!(msg_icve, s, nil, "#{msg_eacs} verify_uri not defined") if d[:verify_uri].nil? | |
350 | 343 | return verify_serpro_captcha(d[:serpro_client_id], params[:txtToken_captcha_serpro_gov_br], params[:captcha_text], d[:verify_uri]) |
351 | 344 | end |
352 | - render_api_error!(msg_icve, s, nil, "#{msg_eacs} provider not defined") | |
345 | + return render_api_error!(msg_icve, s, nil, "#{msg_eacs} provider not defined") | |
353 | 346 | end |
354 | 347 | |
355 | 348 | def verify_recaptcha_v1(remote_ip, private_key, api_recaptcha_verify_uri, recaptcha_challenge_field, recaptcha_response_field) |
356 | 349 | if recaptcha_challenge_field == nil || recaptcha_response_field == nil |
357 | - render_api_error!(_('Captcha validation error'), 503, nil, _('Missing captcha data')) | |
350 | + return render_api_error!(_('Captcha validation error'), 500, nil, _('Missing captcha data')) | |
358 | 351 | end |
359 | 352 | |
360 | 353 | verify_hash = { |
... | ... | @@ -371,16 +364,14 @@ require 'grape' |
371 | 364 | begin |
372 | 365 | body = https.request(request).body |
373 | 366 | rescue Exception => e |
374 | - logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log")) | |
375 | - logger.error e | |
376 | - render_api_error!(_('Internal captcha validation error'), 503, nil, "recaptcha error: #{e.message}") | |
367 | + return render_api_error!(_('Internal captcha validation error'), 500, nil, "recaptcha error: #{e.message}") | |
377 | 368 | end |
378 | 369 | body = JSON.parse(body) |
379 | 370 | body == "true\nsuccess" ? true : body |
380 | 371 | end |
381 | 372 | |
382 | 373 | def verify_recaptcha_v2(remote_ip, private_key, api_recaptcha_verify_uri, g_recaptcha_response) |
383 | - render_api_error!(_('Captcha validation error'), 503, nil, _('Missing captcha data')) if g_recaptcha_response == nil | |
374 | + return render_api_error!(_('Captcha validation error'), 500, nil, _('Missing captcha data')) if g_recaptcha_response == nil | |
384 | 375 | verify_hash = { |
385 | 376 | "secret" => private_key, |
386 | 377 | "remoteip" => remote_ip, |
... | ... | @@ -394,15 +385,15 @@ require 'grape' |
394 | 385 | begin |
395 | 386 | body = https.request(request).body |
396 | 387 | rescue Exception => e |
397 | - render_api_error!(_('Internal captcha validation error'), 503, nil, "recaptcha error: #{e.message}") | |
388 | + return render_api_error!(_('Internal captcha validation error'), 500, nil, "recaptcha error: #{e.message}") | |
398 | 389 | end |
399 | 390 | captcha_result = JSON.parse(body) |
400 | 391 | captcha_result["success"] ? true : captcha_result |
401 | 392 | end |
402 | 393 | |
403 | 394 | def verify_serpro_captcha(client_id, token, captcha_text, verify_uri) |
404 | - return _('Missing Serpro Captcha token') if token == nil | |
405 | - return _('Captcha text has not been filled') if captcha_text == nil | |
395 | + return render_api_error!(_("Error processing token validation"), 500, nil, _("Missing Serpro's Captcha token")) unless token | |
396 | + return render_api_error!(_('Captcha text has not been filled'), 403) unless captcha_text | |
406 | 397 | uri = URI(verify_uri) |
407 | 398 | http = Net::HTTP.new(uri.host, uri.port) |
408 | 399 | request = Net::HTTP::Post.new(uri.path) |
... | ... | @@ -411,28 +402,14 @@ require 'grape' |
411 | 402 | begin |
412 | 403 | body = http.request(request).body |
413 | 404 | rescue Exception => e |
414 | - render_api_error!(_('Internal captcha validation error'), 503, nil, "Serpro captcha error: #{e.message}") | |
405 | + return render_api_error!(_('Internal captcha validation error'), 500, nil, "Serpro captcha error: #{e.message}") | |
415 | 406 | end |
416 | - render_api_error!("Wrong captcha text, please try again") if body == 0 | |
417 | - render_api_error!("Token not found") if body == 2 | |
407 | + return render_api_error!(_("Wrong captcha text, please try again"), 403) if body == 0 | |
408 | + return render_api_error!(_("Token not found"), 500) if body == 2 | |
409 | + return render_api_error!(_("No data sent to validation server or other serious problem"), 500) if body == -1 | |
418 | 410 | body == '1' ? true : body |
419 | 411 | end |
420 | 412 | |
421 | - # custom_message[:prepend2log] -> Prepend2log gives more details to the application log | |
422 | - def log_exception(e, prepend_message2log=nil) | |
423 | - logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log")) | |
424 | - logger.formatter = GrapeLogging::Formatters::Default.new | |
425 | - e.message = "#{prepend_message2log} e.message" if prepend_message2log.present? | |
426 | - puts e.message | |
427 | - logger.error e | |
428 | - end | |
429 | - | |
430 | - def log(message) | |
431 | - logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log")) | |
432 | - logger.formatter = GrapeLogging::Formatters::Default.new | |
433 | - logger.error message | |
434 | - end | |
435 | - | |
436 | 413 | end |
437 | 414 | end |
438 | 415 | end | ... | ... |
test/unit/api/helpers_test.rb
... | ... | @@ -113,7 +113,6 @@ class APIHelpersTest < ActiveSupport::TestCase |
113 | 113 | p = fast_create(Profile) |
114 | 114 | a = fast_create(Article, :published => false, :profile_id => p.id) |
115 | 115 | fast_create(Article, :profile_id => p.id) |
116 | - | |
117 | 116 | user.generate_private_token! |
118 | 117 | User.expects(:find_by_private_token).returns(user) |
119 | 118 | assert_equal 403, find_article(p.articles, a.id).last |
... | ... | @@ -162,61 +161,6 @@ class APIHelpersTest < ActiveSupport::TestCase |
162 | 161 | assert_nil make_conditions_with_parameter[:type] |
163 | 162 | end |
164 | 163 | |
165 | - should 'do not test captcha when there are no settings' do | |
166 | - environment = Environment.new | |
167 | - assert test_captcha("127.0.0.1", {}, environment) | |
168 | - end | |
169 | - | |
170 | - should 'do not test captcha when captcha is disabled on settings' do | |
171 | - environment = Environment.new | |
172 | - environment.api_captcha_settings = { | |
173 | - enabled: false, | |
174 | - } | |
175 | - assert test_captcha("127.0.0.1", {}, environment) | |
176 | - end | |
177 | - | |
178 | - should 'fail display recaptcha v1' do | |
179 | - environment = Environment.new | |
180 | - environment.api_captcha_settings = { | |
181 | - enabled: true, | |
182 | - provider: 'google', | |
183 | - version: 1, | |
184 | - private_key: '6LdsWAcTAAAAAB6maB_HalVyCc4asDAxPxloIMvY', | |
185 | - public_key: '6LdsWAcTAAAAAChTUUD6yu9fCDhdIZzNd7F53zf-', | |
186 | - verify_uri: 'https://www.google.com/recaptcha/api/verify', | |
187 | - } | |
188 | - r = test_captcha('127.0.0.1', params, environment) | |
189 | - assert_equal 'Missing captcha data', JSON.parse(r)['console_message'] | |
190 | - end | |
191 | - | |
192 | - should 'fail display recaptcha v2' do | |
193 | - environment = Environment.new | |
194 | - environment.api_captcha_settings = { | |
195 | - enabled: true, | |
196 | - provider: 'google', | |
197 | - version: 2, | |
198 | - private_key: '6LdsWAcTAAAAAB6maB_HalVyCc4asDAxPxloIMvY', | |
199 | - public_key: '6LdsWAcTAAAAAChTUUD6yu9fCDhdIZzNd7F53zf-', | |
200 | - verify_uri: 'https://www.google.com/recaptcha/api/siteverify', | |
201 | - } | |
202 | - r = test_captcha('127.0.0.1', params, environment) | |
203 | - assert_equal 'Missing captcha data', JSON.parse(r)['console_message'] | |
204 | - end | |
205 | - | |
206 | - | |
207 | - | |
208 | - should 'fail display Serpro captcha' do | |
209 | - environment = Environment.new | |
210 | - environment.api_captcha_settings = { | |
211 | - enabled: true, | |
212 | - provider: 'serpro', | |
213 | - serpro_client_id: '0000000000000000', | |
214 | - verify_uri: 'http://localhost/api/verify', | |
215 | - } | |
216 | - params = {} | |
217 | - params[:txtToken_captcha_serpro_gov_br] = '4324343' | |
218 | - assert_equal test_captcha("127.0.0.1", params, environment), _('Captcha text has not been filled') | |
219 | - end | |
220 | 164 | |
221 | 165 | should 'render not_found if endpoint is unavailable' do |
222 | 166 | Noosfero::API::API.stubs(:endpoint_unavailable?).returns(true) |
... | ... | @@ -238,7 +182,77 @@ class APIHelpersTest < ActiveSupport::TestCase |
238 | 182 | #assert_equals [article1, article2], present_articles |
239 | 183 | end |
240 | 184 | |
241 | - should 'captcha serpro say name or service not known' do | |
185 | +###### Captcha tests ###### | |
186 | + | |
187 | +should 'do not test captcha when there are no settings' do | |
188 | + environment = Environment.new | |
189 | + assert test_captcha("127.0.0.1", {}, environment) | |
190 | +end | |
191 | + | |
192 | +should 'do not test captcha when captcha is disabled on settings' do | |
193 | + environment = Environment.new | |
194 | + environment.api_captcha_settings = { | |
195 | + enabled: false, | |
196 | + } | |
197 | + assert test_captcha("127.0.0.1", {}, environment) | |
198 | +end | |
199 | + | |
200 | +should 'fail display recaptcha v1' do | |
201 | + environment = Environment.new | |
202 | + environment.api_captcha_settings = { | |
203 | + enabled: true, | |
204 | + provider: 'google', | |
205 | + version: 1, | |
206 | + private_key: '6LdsWAcTAAAAAB6maB_HalVyCc4asDAxPxloIMvY', | |
207 | + public_key: '6LdsWAcTAAAAAChTUUD6yu9fCDhdIZzNd7F53zf-', | |
208 | + verify_uri: 'https://www.google.com/recaptcha/api/verify', | |
209 | + } | |
210 | + r = test_captcha('127.0.0.1', params, environment) | |
211 | + assert_equal(_("Missing captcha data"), r[0][:javascript_console_message]) | |
212 | +end | |
213 | + | |
214 | +should 'fail display recaptcha v2' do | |
215 | + environment = Environment.new | |
216 | + environment.api_captcha_settings = { | |
217 | + enabled: true, | |
218 | + provider: 'google', | |
219 | + version: 2, | |
220 | + private_key: '6LdsWAcTAAAAAB6maB_HalVyCc4asDAxPxloIMvY', | |
221 | + public_key: '6LdsWAcTAAAAAChTUUD6yu9fCDhdIZzNd7F53zf-', | |
222 | + verify_uri: 'https://www.google.com/recaptcha/api/siteverify', | |
223 | + } | |
224 | + r = test_captcha('127.0.0.1', params, environment) | |
225 | + assert_equal(_("Missing captcha data"), r[0][:javascript_console_message]) | |
226 | +end | |
227 | + | |
228 | +should 'verify if user filled Serpro\' captcha text' do | |
229 | + environment = Environment.new | |
230 | + environment.api_captcha_settings = { | |
231 | + enabled: true, | |
232 | + provider: 'serpro', | |
233 | + serpro_client_id: '0000000000000000', | |
234 | + verify_uri: 'http://localhost/api/verify', | |
235 | + } | |
236 | + params = {} | |
237 | + params[:txtToken_captcha_serpro_gov_br] = '4324343' | |
238 | + assert_equal(_('Captcha text has not been filled'), test_captcha('127.0.0.1', params, environment)[0]) | |
239 | +end | |
240 | + | |
241 | +should 'verify if Serpro\' captcha token has been sent' do | |
242 | + environment = Environment.new | |
243 | + environment.api_captcha_settings = { | |
244 | + enabled: true, | |
245 | + provider: 'serpro', | |
246 | + serpro_client_id: '0000000000000000', | |
247 | + verify_uri: 'http://localhost/api/verify', | |
248 | + } | |
249 | + params = {} | |
250 | + params[:captcha_text] = '4324343' | |
251 | + r = test_captcha('127.0.0.1', params, environment) | |
252 | + assert_equal(_("Missing Serpro's Captcha token"), r[0][:javascript_console_message]) | |
253 | +end | |
254 | + | |
255 | +should 'captcha serpro say name or service not known' do | |
242 | 256 | environment = Environment.new |
243 | 257 | environment.api_captcha_settings = { |
244 | 258 | enabled: true, |
... | ... | @@ -249,19 +263,11 @@ class APIHelpersTest < ActiveSupport::TestCase |
249 | 263 | params = {} |
250 | 264 | params[:txtToken_captcha_serpro_gov_br] = '4324343' |
251 | 265 | params[:captcha_text] = '4324343' |
252 | - binding.pry | |
253 | - expects(:render_api_error!).with(_('Internal captcha validation error'), 503, nil, "recaptcha error: #{e.message}") | |
254 | -# r = test_captcha('127.0.0.1', params, environment) | |
255 | -# assert_equal 'Serpro captcha error: getaddrinfo: Name or service not known', JSON.parse(r)['console_message'] | |
256 | - end | |
257 | - | |
266 | + r = test_captcha('127.0.0.1', params, environment) | |
267 | + assert_equal(_("Serpro captcha error: getaddrinfo: Name or service not known"), r[0][:javascript_console_message]) | |
268 | +end | |
258 | 269 | |
259 | - # def render_api_error!(user_message, status, log_message = nil, javascript_console_message = nil) | |
260 | - # message_hash = {'message' => user_message, :code => status} | |
261 | - # message_hash[:javascript_console_message] = javascript_console_message if javascript_console_message.present? | |
262 | - # self.status(status || namespace_inheritable(:default_error_status)) | |
263 | - # throw :error, message: message_hash, status: self.status, headers: headers | |
264 | - # end | |
270 | +###### END Captcha tests ###### | |
265 | 271 | |
266 | 272 | protected |
267 | 273 | |
... | ... | @@ -277,13 +283,4 @@ class APIHelpersTest < ActiveSupport::TestCase |
277 | 283 | @params = value |
278 | 284 | end |
279 | 285 | |
280 | - def render_api_error!(user_message, status, log_message = nil, javascript_console_message = nil) | |
281 | - status||= 400 | |
282 | - log_msg = "#{status}, User message: #{user_message}" | |
283 | - log_msg = "#{log_message}, #{log_msg}" if log_message.present? | |
284 | - log_msg = "#{log_msg}, Javascript Console Message: #{javascript_console_message}" if javascript_console_message.present? | |
285 | - return log_msg | |
286 | - end | |
287 | - | |
288 | - | |
289 | 286 | end | ... | ... |