Merge branch 'develop' of github.com:sgmap/tps into develop
This commit is contained in:
commit
1a3aebe6ab
11 changed files with 150 additions and 54 deletions
3
Gemfile
3
Gemfile
|
@ -31,6 +31,9 @@ gem 'unicorn'
|
||||||
# Use Capistrano for deployment
|
# Use Capistrano for deployment
|
||||||
# gem 'capistrano-rails', group: :development
|
# gem 'capistrano-rails', group: :development
|
||||||
|
|
||||||
|
# serializer
|
||||||
|
gem 'active_model_serializers'
|
||||||
|
|
||||||
#haml
|
#haml
|
||||||
gem 'haml-rails'
|
gem 'haml-rails'
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,8 @@ GEM
|
||||||
erubis (~> 2.7.0)
|
erubis (~> 2.7.0)
|
||||||
rails-dom-testing (~> 1.0, >= 1.0.5)
|
rails-dom-testing (~> 1.0, >= 1.0.5)
|
||||||
rails-html-sanitizer (~> 1.0, >= 1.0.1)
|
rails-html-sanitizer (~> 1.0, >= 1.0.1)
|
||||||
|
active_model_serializers (0.8.3)
|
||||||
|
activemodel (>= 3.0)
|
||||||
activejob (4.2.0)
|
activejob (4.2.0)
|
||||||
activesupport (= 4.2.0)
|
activesupport (= 4.2.0)
|
||||||
globalid (>= 0.3.0)
|
globalid (>= 0.3.0)
|
||||||
|
@ -432,6 +434,7 @@ PLATFORMS
|
||||||
ruby
|
ruby
|
||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
|
active_model_serializers
|
||||||
bootstrap-datepicker-rails
|
bootstrap-datepicker-rails
|
||||||
bootstrap-sass (~> 3.3.5)
|
bootstrap-sass (~> 3.3.5)
|
||||||
byebug
|
byebug
|
||||||
|
|
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
|
|
@ -84,58 +84,9 @@ Rails.application.routes.draw do
|
||||||
resources :commentaires, only: [:create]
|
resources :commentaires, only: [:create]
|
||||||
end
|
end
|
||||||
|
|
||||||
# The priority is based upon order of creation: first created -> highest priority.
|
namespace :api do
|
||||||
# See how all your routes lay out with "rake routes".
|
namespace :v1 do
|
||||||
|
resources :procedures, only: [:index, :show]
|
||||||
# You can have the root of your site routed with "root"
|
end
|
||||||
# root 'welcome#index'
|
end
|
||||||
|
|
||||||
# Example of regular route:
|
|
||||||
# get 'products/:id' => 'catalog#view'
|
|
||||||
|
|
||||||
# Example of named route that can be invoked with purchase_url(id: product.id)
|
|
||||||
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
|
|
||||||
|
|
||||||
# Example resource route (maps HTTP verbs to controller actions automatically):
|
|
||||||
# resources :products
|
|
||||||
|
|
||||||
# Example resource route with options:
|
|
||||||
# resources :products do
|
|
||||||
# member do
|
|
||||||
# get 'short'
|
|
||||||
# post 'toggle'
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# collection do
|
|
||||||
# get 'sold'
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
|
|
||||||
# Example resource route with sub-resources:
|
|
||||||
# resources :products do
|
|
||||||
# resources :comments, :sales
|
|
||||||
# resource :seller
|
|
||||||
# end
|
|
||||||
|
|
||||||
# Example resource route with more complex sub-resources:
|
|
||||||
# resources :products do
|
|
||||||
# resources :comments
|
|
||||||
# resources :sales do
|
|
||||||
# get 'recent', on: :collection
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
|
|
||||||
# Example resource route with concerns:
|
|
||||||
# concern :toggleable do
|
|
||||||
# post 'toggle'
|
|
||||||
# end
|
|
||||||
# resources :posts, concerns: :toggleable
|
|
||||||
# resources :photos, concerns: :toggleable
|
|
||||||
|
|
||||||
# Example resource route within a namespace:
|
|
||||||
# namespace :admin do
|
|
||||||
# # Directs /admin/products/* to Admin::ProductsController
|
|
||||||
# # (app/controllers/admin/products_controller.rb)
|
|
||||||
# resources :products
|
|
||||||
# end
|
|
||||||
end
|
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
|
|
@ -3,6 +3,8 @@ FactoryGirl.define do
|
||||||
lien_demarche 'http://localhost'
|
lien_demarche 'http://localhost'
|
||||||
libelle 'Demande de subvention'
|
libelle 'Demande de subvention'
|
||||||
description "Demande de subvention à l'intention des associations"
|
description "Demande de subvention à l'intention des associations"
|
||||||
|
organisation "Orga SGMAP"
|
||||||
|
direction "direction SGMAP"
|
||||||
|
|
||||||
after(:build) do |procedure, _evaluator|
|
after(:build) do |procedure, _evaluator|
|
||||||
if procedure.module_api_carto.nil?
|
if procedure.module_api_carto.nil?
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue