[SCI-3660] Protocols preview (main pane): All the links in the… (#1911)

[SCI-3660] Protocols preview (main pane): All the links in the preview are relative
This commit is contained in:
Jure Grabnar 2019-07-10 23:19:36 +02:00 committed by GitHub
commit 28ba03b27c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 8 deletions

View file

@ -4,6 +4,7 @@
function applyClickCallbackOnProtocolCards() {
$('.protocol-card').off('click').on('click', function(e) {
var currProtocolCard = $(this);
var baseTag = document.createElement('base');
// Check whether this card is already active and deactivate it
if ($(currProtocolCard).hasClass('active')) {
@ -30,7 +31,14 @@ function applyClickCallbackOnProtocolCards() {
$('.empty-preview-panel').hide();
$('.full-preview-panel').show();
$('.btn-holder').html($(currProtocolCard).find('.external-import-btn').clone());
// Set base tag to account for relative links in the iframe
baseTag.href = data.base_uri;
iFrame.contents().find('head').html(baseTag);
// Set iframe content
iFrame.contents().find('body').html(data.html);
scrollbox.scrollTo(0);
iFrame.contents().find('body').find('table.htCore').css('width', '100%');
iFrame.contents().find('body').find('span').css('word-break', 'break-word');

View file

@ -31,10 +31,13 @@ class ExternalProtocolsController < ApplicationController
api_client = "ProtocolImporters::#{endpoint_name}::ApiClient".constantize.new
html_preview = api_client.protocol_html_preview(show_params[:protocol_id])
base_uri = URI.parse(html_preview.request.last_uri.to_s)
base_uri = "#{base_uri.scheme}://#{base_uri.host}"
render json: {
protocol_source: show_params[:protocol_source],
protocol_id: show_params[:protocol_id],
base_uri: base_uri,
html: html_preview
}
rescue StandardError => e
@ -103,15 +106,15 @@ class ExternalProtocolsController < ApplicationController
end
def index_params
params.permit(:protocol_source, :key, :page_id, :page_size, :sort_by)
params.permit(:team_id, :protocol_source, :key, :page_id, :page_size, :sort_by)
end
def show_params
params.permit(:protocol_source, :protocol_id)
params.permit(:team_id, :protocol_source, :protocol_id)
end
def new_params
params.permit(:protocol_source, :protocol_client_id)
params.permit(:team_id, :protocol_source, :protocol_client_id)
end
def create_protocol_params

View file

@ -71,9 +71,14 @@ describe ExternalProtocolsController, type: :controller do
let(:action) { get :show, params: params }
it 'returns JSON, 200 response when preview was successfully returned' do
html_preview = '<html></html>'
let(:html_preview) { double('html_preview') }
before do
allow(html_preview).to(receive(:as_json).and_return('<html></html>'))
allow(html_preview).to(receive_message_chain(:request, :last_uri, :to_s).and_return('http://www.protocols.io/test_protocol'))
end
it 'returns JSON, 200 response when preview was successfully returned' do
allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ApiClient)
.to(receive(:protocol_html_preview)).and_return(html_preview)
@ -84,14 +89,12 @@ describe ExternalProtocolsController, type: :controller do
end
it 'should return html preview in the JSON' do
html_preview = '<html></html>'
allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ApiClient)
.to(receive(:protocol_html_preview)).and_return(html_preview)
# Call action
action
expect(JSON.parse(response.body)['html']).to eq(html_preview)
expect(JSON.parse(response.body)['html']).to eq('<html></html>')
end
it 'returns error JSON and 400 response when something went wrong' do