diff --git a/app/controllers/external_protocols_controller.rb b/app/controllers/external_protocols_controller.rb index f9603c2cc..2a188c67e 100644 --- a/app/controllers/external_protocols_controller.rb +++ b/app/controllers/external_protocols_controller.rb @@ -30,7 +30,21 @@ class ExternalProtocolsController < ApplicationController # GET def show + # TODO: this should be refactored, it's only for placeholding + endpoint_name = Constants::PROTOCOLS_ENDPOINTS.dig(*show_params[:protocol_source] + .split('/').map(&:to_sym)) + api_client = "ProtocolImporters::#{endpoint_name}::ApiClient".constantize.new + html_preview = api_client.protocol_html_preview(show_params[:protocol_id]) + render json: { + protocol_source: show_params[:protocol_source], + protocol_id: show_params[:protocol_id], + html: html_preview + } and return + rescue StandardError => e + render json: { + errors: [show_protocol: e.message] + }, status: 400 end # GET team_build_online_sources_protocol diff --git a/config/routes.rb b/config/routes.rb index 40f0c91c1..9f01c39cd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -209,6 +209,7 @@ Rails.application.routes.draw do # External protocols routes get 'list_external_protocol', to: 'external_protocols#index' + get 'show_external_protocol', to: 'external_protocols#show' get 'build_external_protocol', to: 'external_protocols#new' post 'import_external_protocol', to: 'external_protocols#create' diff --git a/spec/controllers/external_protocols_controller_spec.rb b/spec/controllers/external_protocols_controller_spec.rb index 35625bc40..69e704a90 100644 --- a/spec/controllers/external_protocols_controller_spec.rb +++ b/spec/controllers/external_protocols_controller_spec.rb @@ -36,6 +36,54 @@ describe ExternalProtocolsController, type: :controller do end end + describe 'GET show' do + let(:params) do + { + team_id: team.id, + protocol_source: 'protocolsio/v3', + protocol_id: 'protocolsio_uri' + } + end + + let(:action) { get :show, params: params } + + it 'returns JSON, 200 response when preview was successfully returned' do + html_preview = '' + + allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ApiClient) + .to(receive(:protocol_html_preview)) + .and_return(html_preview) + + # Call action + action + expect(response).to have_http_status(:success) + expect(response.content_type).to eq 'application/json' + 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) + end + + it 'returns error JSON and 400 response when something went wrong' do + allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ApiClient) + .to(receive(:protocol_html_preview)) + .and_raise(StandardError) + + # Call action + action + expect(response).to have_http_status(:bad_request) + expect(JSON.parse(response.body)).to have_key('errors') + end + end + describe 'GET new' do let(:params) do {