add api v1 procedure controller
This commit is contained in:
parent
e40e1299ad
commit
f474c8e8e1
7 changed files with 137 additions and 0 deletions
12
app/controllers/api/v1/procedures_controller.rb
Normal file
12
app/controllers/api/v1/procedures_controller.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
class API::V1::ProceduresController < APIController
|
||||
def show
|
||||
|
||||
@procedure = current_administrateur.procedures.find(params[:id]).decorate
|
||||
|
||||
render json: @procedure
|
||||
rescue ActiveRecord::RecordNotFound => e
|
||||
Rails.logger.error(e.message)
|
||||
render json: {}, status: 404
|
||||
end
|
||||
|
||||
end
|
17
app/controllers/api_controller.rb
Normal file
17
app/controllers/api_controller.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
class APIController < ApplicationController
|
||||
before_action :authenticate_user
|
||||
|
||||
def authenticate_user
|
||||
render json: {}, status: 401 unless valid_token?
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def valid_token?
|
||||
!current_administrateur.nil?
|
||||
end
|
||||
|
||||
def current_administrateur
|
||||
@administrateur ||= Administrateur.find_by_api_token(params[:token])
|
||||
end
|
||||
end
|
11
app/serializers/procedure_serializer.rb
Normal file
11
app/serializers/procedure_serializer.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
class ProcedureSerializer < ActiveModel::Serializer
|
||||
attributes :id,
|
||||
:libelle,
|
||||
:description,
|
||||
:organisation,
|
||||
:direction,
|
||||
:lien_demarche,
|
||||
:archived
|
||||
has_many :types_de_champ, serializer: TypeDeChampSerializer
|
||||
has_many :types_de_piece_justificative, serializer: TypeDePieceJustificativeSerializer
|
||||
end
|
7
app/serializers/type_de_champ_serializer.rb
Normal file
7
app/serializers/type_de_champ_serializer.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
class TypeDeChampSerializer < ActiveModel::Serializer
|
||||
attributes :id,
|
||||
:libelle,
|
||||
:type_champ,
|
||||
:order_place,
|
||||
:description
|
||||
end
|
|
@ -0,0 +1,5 @@
|
|||
class TypeDePieceJustificativeSerializer < ActiveModel::Serializer
|
||||
attributes :id,
|
||||
:libelle,
|
||||
:description
|
||||
end
|
56
spec/controllers/api/v1/procedures_controller_spec.rb
Normal file
56
spec/controllers/api/v1/procedures_controller_spec.rb
Normal file
|
@ -0,0 +1,56 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe API::V1::ProceduresController do
|
||||
let(:admin) { create(:administrateur) }
|
||||
it { expect(described_class).to be < APIController }
|
||||
describe 'GET show' do
|
||||
context 'when procedure does not exist' do
|
||||
subject { get :show, id: 999_999_999, token: admin.api_token }
|
||||
it { expect(subject.status).to eq(404) }
|
||||
end
|
||||
context 'when procedure does not belong to administrateur' do
|
||||
let(:procedure) { create(:procedure, administrateur: create(:administrateur)) }
|
||||
subject { get :show, id: procedure, token: admin.api_token }
|
||||
it { expect(subject.status).to eq(404) }
|
||||
end
|
||||
context 'when procedure exist' do
|
||||
let(:procedure) { create(:procedure, administrateur: admin) }
|
||||
subject { get :show, id: procedure, token: admin.api_token }
|
||||
it { expect(subject.status).to eq(200) }
|
||||
describe 'body' do
|
||||
let(:procedure) { create(:procedure, :with_type_de_champ, :with_two_type_de_piece_justificative, administrateur: admin) }
|
||||
let(:response) { get :show, id: procedure.id, token: admin.api_token }
|
||||
subject { JSON.parse(response.body, symbolize_names: true)[:procedure] }
|
||||
|
||||
it { expect(subject[:id]).to eq(procedure.id) }
|
||||
it { expect(subject[:libelle]).to eq(procedure.libelle) }
|
||||
it { expect(subject[:description]).to eq(procedure.description) }
|
||||
it { expect(subject[:organisation]).to eq(procedure.organisation) }
|
||||
it { expect(subject[:direction]).to eq(procedure.direction) }
|
||||
it { expect(subject[:lien_demarche]).to eq(procedure.lien_demarche) }
|
||||
it { expect(subject[:archived]).to eq(procedure.archived) }
|
||||
it { is_expected.to have_key(:types_de_champ) }
|
||||
it { expect(subject[:types_de_champ]).to be_an(Array) }
|
||||
describe 'type_de_champ' do
|
||||
subject { super()[:types_de_champ][0] }
|
||||
let(:champ) { procedure.types_de_champ.first }
|
||||
it { expect(subject[:id]).to eq(champ.id) }
|
||||
it { expect(subject[:libelle]).to eq(champ.libelle) }
|
||||
it { expect(subject[:type_champ]).to eq(champ.type_champ) }
|
||||
it { expect(subject[:order_place]).to eq(champ.order_place) }
|
||||
it { expect(subject[:description]).to eq(champ.description) }
|
||||
end
|
||||
|
||||
it { is_expected.to have_key(:types_de_piece_justificative) }
|
||||
it { expect(subject[:types_de_piece_justificative]).to be_an(Array) }
|
||||
describe 'type_de_piece_jointe' do
|
||||
subject { super()[:types_de_piece_justificative][0] }
|
||||
let(:pj) { procedure.types_de_piece_justificative.first }
|
||||
it { expect(subject[:id]).to eq(pj.id) }
|
||||
it { expect(subject[:libelle]).to eq(pj.libelle) }
|
||||
it { expect(subject[:description]).to eq(pj.description) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
29
spec/controllers/api_controller_spec.rb
Normal file
29
spec/controllers/api_controller_spec.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe APIController, type: :controller do
|
||||
controller(APIController) do
|
||||
def show
|
||||
render json: {}, satus: 200
|
||||
end
|
||||
def index
|
||||
render json: {}, satus: 200
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET index' do
|
||||
context 'when token is missing' do
|
||||
subject { get :index }
|
||||
it { expect(subject.status).to eq(401) }
|
||||
end
|
||||
context 'when token does not exist' do
|
||||
let(:token) { 'invalid_token' }
|
||||
subject { get :index, token: token }
|
||||
it { expect(subject.status).to eq(401) }
|
||||
end
|
||||
context 'when token exist' do
|
||||
let(:administrateur) { create(:administrateur) }
|
||||
subject { get :index, token: administrateur.api_token }
|
||||
it { expect(subject.status).to eq(200) }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue