forked from gsamokovarov/web-console
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a request wrappers that logs out why a console isn't displayed
- Loading branch information
1 parent
7b38e47
commit 58bc8f6
Showing
7 changed files
with
117 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module WebConsole | ||
# Noisy wrapper around +Request+. | ||
# | ||
# If any calls to +from_whitelisted_ip?+ and +acceptable_content_type?+ | ||
# return false, an info log message will be displayed in users' logs. | ||
class WhinyRequest < SimpleDelegator | ||
def from_whitelited_ip? | ||
whine_unless request.from_whitelited_ip? do | ||
"Cannot render console from #{request.remote_ip}! " \ | ||
"Allowed networks: #{request.whitelisted_ips}" | ||
end | ||
end | ||
|
||
def acceptable_content_type? | ||
whine_unless request.acceptable_content_type? do | ||
"Cannot render console with content type #{request.content_type}" \ | ||
"Allowed content types: #{request.acceptable_content_types}" | ||
end | ||
end | ||
|
||
private | ||
|
||
def whine_unless(condition) | ||
unless condition | ||
logger.info { yield } | ||
end | ||
condition | ||
end | ||
|
||
def logger | ||
env['action_dispatch.logger'] || WebConsole.logger | ||
end | ||
|
||
def request | ||
__getobj__ | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'test_helper' | ||
|
||
module WebConsole | ||
class WhinyRequestTest < ActiveSupport::TestCase | ||
test '#from_whitelited_ip? logs out to stderr' do | ||
Request.stubs(:whitelisted_ips).returns(IPAddr.new('127.0.0.1')) | ||
assert_output_to_stderr do | ||
req = request('http://example.com', 'REMOTE_ADDR' => '0.0.0.0') | ||
assert_not req.from_whitelited_ip? | ||
end | ||
end | ||
|
||
test '#acceptable_content_type? logs out to stderr' do | ||
Request.stubs(:acceptable_content_types).returns([]) | ||
assert_output_to_stderr do | ||
req = request('http://example.com', 'CONTENT_TYPE' => 'application/json') | ||
assert_not req.acceptable_content_type? | ||
end | ||
end | ||
|
||
private | ||
|
||
def assert_output_to_stderr | ||
output = capture(:stderr) { yield } | ||
assert_not output.blank? | ||
end | ||
|
||
def request(*args) | ||
request = Request.new(Rack::MockRequest.env_for(*args)) | ||
WhinyRequest.new(request) | ||
end | ||
end | ||
end |