add api v1 procedure controller

This commit is contained in:
Tanguy PATTE 2015-12-21 17:51:49 +01:00
parent e40e1299ad
commit f474c8e8e1
7 changed files with 137 additions and 0 deletions

View 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

View 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

View 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

View file

@ -0,0 +1,7 @@
class TypeDeChampSerializer < ActiveModel::Serializer
attributes :id,
:libelle,
:type_champ,
:order_place,
:description
end

View file

@ -0,0 +1,5 @@
class TypeDePieceJustificativeSerializer < ActiveModel::Serializer
attributes :id,
:libelle,
:description
end

View 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

View 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