Commit 6b8b1783f9d4565298dc9517ad40135a70513fc5
1 parent
7fb4da5b
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
broken tests
Showing
3 changed files
with
36 additions
and
20 deletions
Show diff stats
lib/noosfero/api/helpers.rb
@@ -39,6 +39,10 @@ require 'grape' | @@ -39,6 +39,10 @@ require 'grape' | ||
39 | @environment | 39 | @environment |
40 | end | 40 | end |
41 | 41 | ||
42 | + def logger | ||
43 | + Noosfero::API::API.logger | ||
44 | + end | ||
45 | + | ||
42 | def limit | 46 | def limit |
43 | limit = params[:limit].to_i | 47 | limit = params[:limit].to_i |
44 | limit = default_limit if limit <= 0 | 48 | limit = default_limit if limit <= 0 |
@@ -239,6 +243,10 @@ require 'grape' | @@ -239,6 +243,10 @@ require 'grape' | ||
239 | render_api_error!(_('Method Not Allowed'), 405) | 243 | render_api_error!(_('Method Not Allowed'), 405) |
240 | end | 244 | end |
241 | 245 | ||
246 | + # render_api_error!(message, status) | ||
247 | + # error!({'message' => message, :code => status}, status) | ||
248 | + # end | ||
249 | + | ||
242 | # javascript_console_message is supposed to be executed as console.log() | 250 | # javascript_console_message is supposed to be executed as console.log() |
243 | def render_api_error!(user_message, status, log_message = nil, javascript_console_message = nil) | 251 | def render_api_error!(user_message, status, log_message = nil, javascript_console_message = nil) |
244 | status||= 400 | 252 | status||= 400 |
@@ -247,9 +255,14 @@ require 'grape' | @@ -247,9 +255,14 @@ require 'grape' | ||
247 | log_msg = "#{status}, User message: #{user_message}" | 255 | log_msg = "#{status}, User message: #{user_message}" |
248 | log_msg = "#{log_message}, #{log_msg}" if log_message.present? | 256 | log_msg = "#{log_message}, #{log_msg}" if log_message.present? |
249 | log_msg = "#{log_msg}, Javascript Console Message: #{javascript_console_message}" if javascript_console_message.present? | 257 | 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 | ||
250 | #Since throw :error is not logging the errors I had to manually log it! | 261 | #Since throw :error is not logging the errors I had to manually log it! |
251 | #log(log_msg) | 262 | #log(log_msg) |
252 | - throw :error, message: message_hash, status: status, headers: headers | 263 | + logger.error log_msg |
264 | + error!(message_hash, status) | ||
265 | +# throw :error, message: message_hash, status: status, headers: headers | ||
253 | end | 266 | end |
254 | 267 | ||
255 | def render_api_errors!(messages) | 268 | def render_api_errors!(messages) |
lib/noosfero/api/session.rb
@@ -124,9 +124,9 @@ module Noosfero | @@ -124,9 +124,9 @@ module Noosfero | ||
124 | end | 124 | end |
125 | end | 125 | end |
126 | 126 | ||
127 | - # post "/error" do | ||
128 | - # render_api_error!("show on screen", 400, "this is the log message", "go to JS") | ||
129 | - # end | 127 | + get "/stop" do |
128 | + stop!("show on screen", 400, "this is the log message", "go to JS") | ||
129 | + end | ||
130 | 130 | ||
131 | end | 131 | end |
132 | end | 132 | end |
test/unit/api/helpers_test.rb
@@ -185,7 +185,8 @@ class APIHelpersTest < ActiveSupport::TestCase | @@ -185,7 +185,8 @@ class APIHelpersTest < ActiveSupport::TestCase | ||
185 | public_key: '6LdsWAcTAAAAAChTUUD6yu9fCDhdIZzNd7F53zf-', | 185 | public_key: '6LdsWAcTAAAAAChTUUD6yu9fCDhdIZzNd7F53zf-', |
186 | verify_uri: 'https://www.google.com/recaptcha/api/verify', | 186 | verify_uri: 'https://www.google.com/recaptcha/api/verify', |
187 | } | 187 | } |
188 | - assert_equal test_captcha("127.0.0.1", {}, environment), "Missing captcha data" | 188 | + r = test_captcha('127.0.0.1', params, environment) |
189 | + assert_equal 'Missing captcha data', JSON.parse(r)['console_message'] | ||
189 | end | 190 | end |
190 | 191 | ||
191 | should 'fail display recaptcha v2' do | 192 | should 'fail display recaptcha v2' do |
@@ -198,9 +199,12 @@ class APIHelpersTest < ActiveSupport::TestCase | @@ -198,9 +199,12 @@ class APIHelpersTest < ActiveSupport::TestCase | ||
198 | public_key: '6LdsWAcTAAAAAChTUUD6yu9fCDhdIZzNd7F53zf-', | 199 | public_key: '6LdsWAcTAAAAAChTUUD6yu9fCDhdIZzNd7F53zf-', |
199 | verify_uri: 'https://www.google.com/recaptcha/api/siteverify', | 200 | verify_uri: 'https://www.google.com/recaptcha/api/siteverify', |
200 | } | 201 | } |
201 | - assert_equal test_captcha("127.0.0.1", {}, environment), "Missing captcha data" | 202 | + r = test_captcha('127.0.0.1', params, environment) |
203 | + assert_equal 'Missing captcha data', JSON.parse(r)['console_message'] | ||
202 | end | 204 | end |
203 | 205 | ||
206 | + | ||
207 | + | ||
204 | should 'fail display Serpro captcha' do | 208 | should 'fail display Serpro captcha' do |
205 | environment = Environment.new | 209 | environment = Environment.new |
206 | environment.api_captcha_settings = { | 210 | environment.api_captcha_settings = { |
@@ -232,8 +236,6 @@ class APIHelpersTest < ActiveSupport::TestCase | @@ -232,8 +236,6 @@ class APIHelpersTest < ActiveSupport::TestCase | ||
232 | #assert_equal 403, find_article(p.articles, a.id).last | 236 | #assert_equal 403, find_article(p.articles, a.id).last |
233 | 237 | ||
234 | #assert_equals [article1, article2], present_articles | 238 | #assert_equals [article1, article2], present_articles |
235 | - | ||
236 | - | ||
237 | end | 239 | end |
238 | 240 | ||
239 | should 'captcha serpro say name or service not known' do | 241 | should 'captcha serpro say name or service not known' do |
@@ -247,11 +249,10 @@ class APIHelpersTest < ActiveSupport::TestCase | @@ -247,11 +249,10 @@ class APIHelpersTest < ActiveSupport::TestCase | ||
247 | params = {} | 249 | params = {} |
248 | params[:txtToken_captcha_serpro_gov_br] = '4324343' | 250 | params[:txtToken_captcha_serpro_gov_br] = '4324343' |
249 | params[:captcha_text] = '4324343' | 251 | params[:captcha_text] = '4324343' |
250 | - assert_throws :error do | ||
251 | - r = test_captcha('127.0.0.1', params, environment) | ||
252 | - puts r.inspect | ||
253 | - end | ||
254 | - # assert_equal 'Serpro captcha error: getaddrinfo: Name or service not known', JSON.parse(r)['console_message'] | 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'] | ||
255 | end | 256 | end |
256 | 257 | ||
257 | 258 | ||
@@ -262,13 +263,6 @@ class APIHelpersTest < ActiveSupport::TestCase | @@ -262,13 +263,6 @@ class APIHelpersTest < ActiveSupport::TestCase | ||
262 | # throw :error, message: message_hash, status: self.status, headers: headers | 263 | # throw :error, message: message_hash, status: self.status, headers: headers |
263 | # end | 264 | # end |
264 | 265 | ||
265 | - | ||
266 | - # should 'display user message' do | ||
267 | - # r=render_api_error!('Error to the user', '403', 'detailed log_message', 'show this on user\'s javascript console') | ||
268 | - # puts r.inspect | ||
269 | - # tsil | ||
270 | - # end | ||
271 | - | ||
272 | protected | 266 | protected |
273 | 267 | ||
274 | def error!(info, status) | 268 | def error!(info, status) |
@@ -283,4 +277,13 @@ class APIHelpersTest < ActiveSupport::TestCase | @@ -283,4 +277,13 @@ class APIHelpersTest < ActiveSupport::TestCase | ||
283 | @params = value | 277 | @params = value |
284 | end | 278 | end |
285 | 279 | ||
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 | + | ||
286 | end | 289 | end |