OnlineSourcesController skeleton and #new action tested

This commit is contained in:
Jure Grabnar 2019-06-11 17:04:59 +02:00 committed by Urban Rotnik
parent 7adef9cc9e
commit 94e7fe1e6c
3 changed files with 111 additions and 0 deletions

View file

@ -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

View file

@ -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'

View file

@ -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