Add ExternalProtocols#create action and tests

This commit is contained in:
Jure Grabnar 2019-06-13 01:02:44 +02:00 committed by Urban Rotnik
parent 9baa66c998
commit 2a61075156
3 changed files with 92 additions and 1 deletions

View file

@ -1,4 +1,7 @@
class ExternalProtocolsController < ApplicationController
before_action :load_vars
before_action :check_import_permissions, only: [:create]
# GET
def index
# list_protocols = SearchService.call(index_params)
@ -29,7 +32,7 @@ class ExternalProtocolsController < ApplicationController
protocol_source: new_params[:protocol_source],
protocol_client_id: new_params[:protocol_client_id],
user_id: current_user.id,
team_id: current_team.id
team_id: @team.id
)
if service_call.succeed?
@ -39,11 +42,30 @@ class ExternalProtocolsController < ApplicationController
end
end
# POST import_external_protocol
def create
service_call = ProtocolImporters::ImportProtocolService.call(
protocol_params: create_params[:protocol_params],
steps_params: create_params[:steps_paramas],
user_id: current_user.id,
team_id: @team.id
)
if service_call.succeed?
render json: service_call.protocol
else
render json: { errors: service_call.errors }, status: 400
end
end
private
def load_vars
@team = Team.find_by_id(params[:team_id])
render_404 unless @team
end
def index_params
params.permit(:protocol_source, :key, :page_id, :page_size, :order_field, :order_dir)
end
@ -51,4 +73,12 @@ class ExternalProtocolsController < ApplicationController
def new_params
params.permit(:protocol_source, :protocol_client_id)
end
def create_params
params.permit(:protocol_params, :steps_params)
end
def check_import_permissions
render_403 unless can_create_protocols_in_repository?(@team)
end
end

View file

@ -211,6 +211,7 @@ Rails.application.routes.draw do
via: [:get, :post, :put, :patch]
get 'build_external_protocol', to: 'external_protocols#new'
post 'import_external_protocol', to: 'external_protocols#create'
end
get 'projects/archive', to: 'projects#archive', as: 'projects_archive'

View file

@ -52,4 +52,64 @@ describe ExternalProtocolsController, type: :controller do
end
end
describe 'POST create' do
context 'when user has import permissions for the team' do
let(:params) do
{
team_id: team.id,
protocol_params: {},
steps_params: {}
}
end
let(:action) { post :create, 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(:protocol)).and_return({})
allow_any_instance_of(ProtocolImporters::ImportProtocolService).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::ImportProtocolService).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
context 'when user has no import permissions for the team' do
let(:user_two) { create :user }
let(:team_two) { create :team, created_by: user_two }
let(:params) do
{
team_id: team_two.id,
protocol_params: {},
steps_params: {}
}
end
it 'returns 403 when trying to import to forbidden team' do
post :create, params: params
expect(response).to have_http_status(:forbidden)
end
end
end
end