From 5eb197127f760638340ae28e66e12ae57039ddb1 Mon Sep 17 00:00:00 2001 From: Jure Grabnar Date: Tue, 9 Jul 2019 13:21:06 +0200 Subject: [PATCH 1/2] Fix relative links in protocolsio iframe Closes SCI-3660 --- .../javascripts/protocols/external_protocols_tab.js | 8 ++++++++ app/controllers/external_protocols_controller.rb | 9 ++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/protocols/external_protocols_tab.js b/app/assets/javascripts/protocols/external_protocols_tab.js index 4a623afa9..bcc0c2b12 100644 --- a/app/assets/javascripts/protocols/external_protocols_tab.js +++ b/app/assets/javascripts/protocols/external_protocols_tab.js @@ -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'); diff --git a/app/controllers/external_protocols_controller.rb b/app/controllers/external_protocols_controller.rb index 751ae2373..07960ce63 100644 --- a/app/controllers/external_protocols_controller.rb +++ b/app/controllers/external_protocols_controller.rb @@ -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 From ed5eb39bb22cb6e348fc20f386b971d8bfeb4cbd Mon Sep 17 00:00:00 2001 From: Jure Grabnar Date: Wed, 10 Jul 2019 19:02:21 +0200 Subject: [PATCH 2/2] Fix tests for GET show on protocols.io --- .../external_protocols_controller_spec.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/spec/controllers/external_protocols_controller_spec.rb b/spec/controllers/external_protocols_controller_spec.rb index ce47cc857..28f684d9a 100644 --- a/spec/controllers/external_protocols_controller_spec.rb +++ b/spec/controllers/external_protocols_controller_spec.rb @@ -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 = '' + let(:html_preview) { double('html_preview') } + before do + allow(html_preview).to(receive(:as_json).and_return('')) + 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 = '' - 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('') end it 'returns error JSON and 400 response when something went wrong' do