From 94e7fe1e6c5d409e00cc0e5751f371fd15585beb Mon Sep 17 00:00:00 2001 From: Jure Grabnar Date: Tue, 11 Jun 2019 17:04:59 +0200 Subject: [PATCH] OnlineSourcesController skeleton and #new action tested --- app/controllers/online_sources_controller.rb | 54 ++++++++++++++++++ config/routes.rb | 2 + .../online_sources_controller_spec.rb | 55 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 app/controllers/online_sources_controller.rb create mode 100644 spec/controllers/online_sources_controller_spec.rb diff --git a/app/controllers/online_sources_controller.rb b/app/controllers/online_sources_controller.rb new file mode 100644 index 000000000..0078cbd10 --- /dev/null +++ b/app/controllers/online_sources_controller.rb @@ -0,0 +1,54 @@ +class OnlineSourcesController < ApplicationController + # GET + def index + # list_protocols = SearchService.call(index_params) + succeed = false + list_protocols = [ + { name: 'Protocol1' }, + { name: 'Protocol2' }, + { name: 'Protocol3' } + ] + + if succeed + render json: list_protocols + else + render json: { + errors: { protocol: 'error_placeholder' } + }, status: 400 + end + end + + # GET + def show + + end + + # GET team_build_online_sources_protocol + def new + service_call = ProtocolImporters::BuildProtocolFromClientService.call( + protocol_source: new_params[:protocol_source], + protocol_client_id: new_params[:protocol_client_id], + user_id: current_user.id, + team_id: current_team.id + ) + + if service_call.succeed? + render json: service_call.pio_protocol + else + render json: { errors: service_call.errors }, status: 400 + end + end + + def create + end + + private + + def index_params + params.permit(:protocol_source, :key, :page_id, :page_size, :order_field, :order_dir) + end + + def new_params + params.permit(:protocol_source, :protocol_client_id) + end +end diff --git a/config/routes.rb b/config/routes.rb index 260961435..36784f962 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -209,6 +209,8 @@ Rails.application.routes.draw do match '*path', to: 'teams#routing_error', via: [:get, :post, :put, :patch] + + get 'build_online_sources_protocol', to: 'online_sources#new' end get 'projects/archive', to: 'projects#archive', as: 'projects_archive' diff --git a/spec/controllers/online_sources_controller_spec.rb b/spec/controllers/online_sources_controller_spec.rb new file mode 100644 index 000000000..bfcab6a0f --- /dev/null +++ b/spec/controllers/online_sources_controller_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe OnlineSourcesController, type: :controller do + login_user + + let(:user) { subject.current_user } + let(:team) { create :team, created_by: user } + let!(:user_team) { create :user_team, :admin, user: user, team: team } + + describe 'GET new' do + let(:params) do + { + team_id: team.id, + protocol_source: 'protocolsio/v3', + page_id: 1, + page_size: 10, + order_field: 'activity', + order_dir: 'desc' + } + end + + let(:action) { get :new, params: params } + + it 'returns JSON, 200 response when protocol parsing was valid' do + # Setup double + service = double('success_service') + allow(service).to(receive(:succeed?)).and_return(true) + allow(service).to(receive(:pio_protocol)).and_return({}) + + allow_any_instance_of(ProtocolImporters::BuildProtocolFromClientService).to(receive(:call)).and_return(service) + + # Call action + action + expect(response).to have_http_status(:success) + expect(response.content_type).to eq 'application/json' + end + + it 'returns JSON, 400 response when protocol parsing was invalid' do + # Setup double + service = double('success_service') + allow(service).to(receive(:succeed?)).and_return(false) + allow(service).to(receive(:errors)).and_return({}) + + allow_any_instance_of(ProtocolImporters::BuildProtocolFromClientService).to(receive(:call)).and_return(service) + + # Call action + action + expect(response).to have_http_status(:bad_request) + expect(response.content_type).to eq 'application/json' + end + + end +end