mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-31 03:59:51 +08:00
Add external_protocols#show action
This commit is contained in:
parent
f7c6f78925
commit
c8b8bcffdb
3 changed files with 63 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
|
||||
|
|
|
@ -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 = '<html></html>'
|
||||
|
||||
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 = '<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)
|
||||
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
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue