-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support to render widgets partial without any layout furniture #3989
Open
abujeda
wants to merge
2
commits into
OSC:master
Choose a base branch
from
hmdc:render_widgets_partial
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
# The Controller to render widget templates without any layout furniture | ||
class WidgetsController < ApplicationController | ||
|
||
def show | ||
widget_path = File.join('/widgets', params[:widget_path]) | ||
|
||
unless valid_path?(widget_path) | ||
render plain: "400 Bad Request. Invalid widget path: #{widget_path}", status: :bad_request | ||
return | ||
end | ||
|
||
|
||
widget_exists = lookup_context.exists?(widget_path, [], true) | ||
unless widget_exists | ||
render plain: "404 Widget not found: #{widget_path}", status: :not_found | ||
return | ||
end | ||
|
||
render partial: widget_path, layout: false | ||
end | ||
|
||
private | ||
|
||
# Checks if the widget path contains only allowed characters | ||
def valid_path?(widget_path) | ||
widget_path.match?(/\A[a-zA-Z0-9_\-\/]+\z/) | ||
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
45 changes: 45 additions & 0 deletions
45
apps/dashboard/test/controllers/widgets_controller_test.rb
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,45 @@ | ||
require "test_helper" | ||
|
||
class WidgetsControllerTest < ActiveSupport::TestCase | ||
|
||
def setup | ||
@controller = WidgetsController.new | ||
end | ||
|
||
test 'valid_path? validates widget paths' do | ||
refute @controller.send(:valid_path?, '/test!') | ||
refute @controller.send(:valid_path?, '/test/../../outside_dir') | ||
refute @controller.send(:valid_path?, '@user:pwd/dir') | ||
|
||
assert @controller.send(:valid_path?, 'test') | ||
assert @controller.send(:valid_path?, '/test') | ||
assert @controller.send(:valid_path?, '/test/path/widget') | ||
assert @controller.send(:valid_path?, '/test_path/widget') | ||
assert @controller.send(:valid_path?, '/test-path/widget_under/name') | ||
end | ||
|
||
test 'show should return HTTP 400 when invalid widget path is used' do | ||
@params = ActionController::Parameters.new({ widget_path: '!!invalid' }) | ||
@controller.stubs(:params).returns(@params) | ||
@controller.expects(:render).with(plain: '400 Bad Request. Invalid widget path: /widgets/!!invalid', status: :bad_request) | ||
|
||
@controller.show | ||
end | ||
|
||
test 'show should return HTTP 404 when valid widget path is not found in the system' do | ||
@params = ActionController::Parameters.new({ widget_path: '/valid/path' }) | ||
@controller.stubs(:params).returns(@params) | ||
@controller.expects(:render).with(plain: '404 Widget not found: /widgets/valid/path', status: :not_found) | ||
|
||
@controller.show | ||
end | ||
|
||
test 'show should render widget when valid widget path is found in the system' do | ||
@params = ActionController::Parameters.new({ widget_path: '/valid/path' }) | ||
@controller.stubs(:params).returns(@params) | ||
@controller.lookup_context.stubs(:exists?).returns(true) | ||
@controller.expects(:render).with(partial: '/widgets/valid/path', layout: false) | ||
|
||
@controller.show | ||
end | ||
end |
1 change: 1 addition & 0 deletions
1
apps/dashboard/test/fixtures/config/views/widgets/_widgets_partial_test.html.erb
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 @@ | ||
<h3>test response from widget partial</h3> |
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,18 @@ | ||
require 'html_helper' | ||
require 'test_helper' | ||
|
||
class WidgetsPartialTest < ActionDispatch::IntegrationTest | ||
|
||
test 'should render widget partial without any layout furniture' do | ||
get widgets_url('widgets_partial_test') | ||
|
||
assert_response :ok | ||
assert_equal '<h3>test response from widget partial</h3>', @response.body | ||
end | ||
|
||
test 'should render return 404 response when widget is missing' do | ||
get widgets_url('missing_widget') | ||
|
||
assert_response :not_found | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay on this. Why do we need
post
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for completeness. There might be complex widgets that might require to submit data from the frontend to the backend for an update of the state/data of the widget after the initial render. Adding a POST will provide a more REST friendly way of getting the data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't seem to be passing the body or query params to the partial. That seems super risky to do so, because we don't know them up front and so can't filter them. I have to admit that scares me a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, there is no need to pass them and this is not adding something that is not already possible. The body and query params are available in any template through the
params
object.This is a valid and working widget template (tested locally to confirm)
/pun/sys/dashboard?parameter=Aday
Doing this with a POST will be more REST friendly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To mitigate the security concerns, we could make this a experimental feature for now to give us time to try and discover any issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please. I just tagged 4.0.0 but this can make it into 4.0.1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes completed