Merge pull request #2792 from betagouv/dev

2018-10-10-01
This commit is contained in:
Frederic Merizen 2018-10-10 09:24:57 +02:00 committed by GitHub
commit e0a8aeab41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 594 additions and 516 deletions

View file

@ -28,7 +28,9 @@ module NewGestionnaire
@current_filters = current_filters
@available_fields_to_filters = available_fields_to_filters
@displayed_fields = procedure_presentation.displayed_fields
# Technically, procedure_presentation already sets the attribute.
# Setting it here to make clear that it is used by the view
@procedure_presentation = procedure_presentation
@displayed_fields_values = displayed_fields_values
@a_suivre_dossiers = procedure
@ -67,11 +69,10 @@ module NewGestionnaire
@archived_dossiers
end
dossier_field_service = DossierFieldService.new
sorted_ids = dossier_field_service.sorted_ids(@dossiers, procedure_presentation, current_gestionnaire)
sorted_ids = procedure_presentation.sorted_ids(@dossiers, current_gestionnaire)
if @current_filters.count > 0
filtered_ids = dossier_field_service.filtered_ids(@dossiers, current_filters)
filtered_ids = procedure_presentation.filtered_ids(@dossiers, statut)
filtered_sorted_ids = sorted_ids.select { |id| filtered_ids.include?(id) }
else
filtered_sorted_ids = sorted_ids
@ -103,7 +104,7 @@ module NewGestionnaire
fields = values.map do |value|
table, column = value.split("/")
procedure.fields.find do |field|
procedure_presentation.fields.find do |field|
field['table'] == table && field['column'] == column
end
end
@ -144,7 +145,7 @@ module NewGestionnaire
if params[:value].present?
filters = procedure_presentation.filters
table, column = params[:field].split('/')
label = procedure.fields.find { |c| c['table'] == table && c['column'] == column }['label']
label = procedure_presentation.fields.find { |c| c['table'] == table && c['column'] == column }['label']
filters[statut] << {
'label' => label,
@ -225,13 +226,13 @@ module NewGestionnaire
"#{field['table']}/#{field['column']}"
end
procedure.fields_for_select.reject do |field|
procedure_presentation.fields_for_select.reject do |field|
current_filters_fields_ids.include?(field[1])
end
end
def eager_load_displayed_fields
@displayed_fields
procedure_presentation.displayed_fields
.reject { |field| field['table'] == 'self' }
.group_by do |field|
if ['type_de_champ', 'type_de_champ_private'].include?(field['table'])

View file

@ -17,10 +17,6 @@ class DossierFacades
@dossier.etablissement
end
def pieces_justificatives
@dossier.ordered_pieces_justificatives
end
def types_de_pieces_justificatives
@dossier.types_de_piece_justificative.order('order_place ASC')
end

View file

@ -118,10 +118,6 @@ class Dossier < ApplicationRecord
end
end
def ordered_pieces_justificatives
champs.joins(', types_de_piece_justificative').where("pieces_justificatives.type_de_piece_justificative_id = types_de_piece_justificative.id AND types_de_piece_justificative.procedure_id = #{procedure.id}").order('order_place ASC')
end
def en_construction_ou_instruction?
EN_CONSTRUCTION_OU_INSTRUCTION.include?(state)
end

View file

@ -296,16 +296,6 @@ class Procedure < ApplicationRecord
without_continuation_mail || Mails::WithoutContinuationMail.default_for_procedure(self)
end
def fields
DossierFieldService.new.fields(self)
end
def fields_for_select
fields.map do |field|
[field['label'], "#{field['table']}/#{field['column']}"]
end
end
def self.default_sort
{
'table' => 'self',

View file

@ -1,7 +1,7 @@
class ProcedurePresentation < ApplicationRecord
EXTRA_SORT_COLUMNS = {
'notifications' => Set['notifications'],
'self' => Set['id', 'state']
'notifications' => ['notifications'],
'self' => ['id', 'state']
}
belongs_to :assign_to
@ -10,13 +10,137 @@ class ProcedurePresentation < ApplicationRecord
validate :check_allowed_displayed_fields
validate :check_allowed_sort_column
validate :check_allowed_sort_order
validate :check_allowed_filter_columns
def fields
fields = [
field_hash('Créé le', 'self', 'created_at'),
field_hash('Mis à jour le', 'self', 'updated_at'),
field_hash('Demandeur', 'user', 'email')
]
if !procedure.for_individual || (procedure.for_individual && procedure.individual_with_siret)
fields.push(
field_hash('SIREN', 'etablissement', 'entreprise_siren'),
field_hash('Forme juridique', 'etablissement', 'entreprise_forme_juridique'),
field_hash('Nom commercial', 'etablissement', 'entreprise_nom_commercial'),
field_hash('Raison sociale', 'etablissement', 'entreprise_raison_sociale'),
field_hash('SIRET siège social', 'etablissement', 'entreprise_siret_siege_social'),
field_hash('Date de création', 'etablissement', 'entreprise_date_creation')
)
fields.push(
field_hash('SIRET', 'etablissement', 'siret'),
field_hash('Libellé NAF', 'etablissement', 'libelle_naf'),
field_hash('Code postal', 'etablissement', 'code_postal')
)
end
explanatory_types_de_champ = [:header_section, :explication].map{ |k| TypeDeChamp.type_champs.fetch(k) }
fields.concat procedure.types_de_champ
.reject { |tdc| explanatory_types_de_champ.include?(tdc.type_champ) }
.map { |type_de_champ| field_hash(type_de_champ.libelle, 'type_de_champ', type_de_champ.id.to_s) }
fields.concat procedure.types_de_champ_private
.reject { |tdc| explanatory_types_de_champ.include?(tdc.type_champ) }
.map { |type_de_champ| field_hash(type_de_champ.libelle, 'type_de_champ_private', type_de_champ.id.to_s) }
fields
end
def fields_for_select
fields.map do |field|
[field['label'], "#{field['table']}/#{field['column']}"]
end
end
def displayed_field_values(dossier)
assert_matching_procedure(dossier)
displayed_fields.map { |field| get_value(dossier, field['table'], field['column']) }
end
def sorted_ids(dossiers, gestionnaire)
dossiers.each { |dossier| assert_matching_procedure(dossier) }
table = sort['table']
column = sanitized_column(sort)
order = sort['order']
case table
when 'notifications'
dossiers_id_with_notification = gestionnaire.notifications_for_procedure(procedure)
if order == 'desc'
return dossiers_id_with_notification +
(dossiers.order('dossiers.updated_at desc').ids - dossiers_id_with_notification)
else
return (dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) +
dossiers_id_with_notification
end
when 'self'
return dossiers
.order("#{column} #{order}")
.pluck(:id)
when 'type_de_champ', 'type_de_champ_private'
return dossiers
.includes(table == 'type_de_champ' ? :champs : :champs_private)
.where("champs.type_de_champ_id = #{sort['column'].to_i}")
.order("champs.value #{order}")
.pluck(:id)
else
return dossiers
.includes(table)
.order("#{column} #{order}")
.pluck(:id)
end
end
def filtered_ids(dossiers, statut)
dossiers.each { |dossier| assert_matching_procedure(dossier) }
filters[statut].map do |filter|
table = filter['table']
column = sanitized_column(filter)
case table
when 'self'
dossiers.where("? ILIKE ?", filter['column'], "%#{filter['value']}%")
when 'france_connect_information'
dossiers
.includes(user: :france_connect_information)
.where("? ILIKE ?", "france_connect_informations.#{filter['column']}", "%#{filter['value']}%")
when 'type_de_champ', 'type_de_champ_private'
relation = table == 'type_de_champ' ? :champs : :champs_private
dossiers
.includes(relation)
.where("champs.type_de_champ_id = ?", filter['column'].to_i)
.where("champs.value ILIKE ?", "%#{filter['value']}%")
when 'etablissement'
if filter['column'] == 'entreprise_date_creation'
date = filter['value'].to_date rescue nil
dossiers
.includes(table)
.where("#{column} = ?", date)
else
dossiers
.includes(table)
.where("#{column} ILIKE ?", "%#{filter['value']}%")
end
when 'user'
dossiers
.includes(table)
.where("#{column} ILIKE ?", "%#{filter['value']}%")
end.pluck(:id)
end.reduce(:&)
end
private
def check_allowed_displayed_fields
displayed_fields.each do |field|
table = field['table']
column = field['column']
if !dossier_field_service.valid_column?(procedure, table, column)
if !valid_column?(table, column)
errors.add(:filters, "#{table}.#{column} nest pas une colonne permise")
end
end
@ -25,30 +149,85 @@ class ProcedurePresentation < ApplicationRecord
def check_allowed_sort_column
table = sort['table']
column = sort['column']
if !valid_sort_column?(procedure, table, column)
if !valid_sort_column?(table, column)
errors.add(:sort, "#{table}.#{column} nest pas une colonne permise")
end
end
def check_allowed_sort_order
order = sort['order']
if !["asc", "desc"].include?(order)
errors.add(:sort, "#{order} nest pas une ordre permis")
end
end
def check_allowed_filter_columns
filters.each do |_, columns|
columns.each do |column|
table = column['table']
column = column['column']
if !dossier_field_service.valid_column?(procedure, table, column)
if !valid_column?(table, column)
errors.add(:filters, "#{table}.#{column} nest pas une colonne permise")
end
end
end
end
private
def assert_matching_procedure(dossier)
if dossier.procedure != procedure
raise "Procedure mismatch (expected #{procedure.id}, got #{dossier.procedure.id})"
end
end
def get_value(dossier, table, column)
case table
when 'self'
dossier.send(column)
when 'user'
dossier.user.send(column)
when 'etablissement'
dossier.etablissement&.send(column)
when 'type_de_champ'
dossier.champs.find { |c| c.type_de_champ_id == column.to_i }.value
when 'type_de_champ_private'
dossier.champs_private.find { |c| c.type_de_champ_id == column.to_i }.value
end
end
def field_hash(label, table, column)
{
'label' => label,
'table' => table,
'column' => column
}
end
def valid_column?(table, column)
valid_columns_for_table(table).include?(column)
end
def valid_columns_for_table(table)
@column_whitelist ||= fields
.group_by { |field| field['table'] }
.map { |table, fields| [table, Set.new(fields.map { |field| field['column'] }) ] }
.to_h
@column_whitelist[table] || []
end
def sanitized_column(field)
table = field['table']
table = ActiveRecord::Base.connection.quote_column_name((table == 'self' ? 'dossier' : table).pluralize)
column = ActiveRecord::Base.connection.quote_column_name(field['column'])
table + '.' + column
end
def dossier_field_service
@dossier_field_service ||= DossierFieldService.new
end
def valid_sort_column?(procedure, table, column)
dossier_field_service.valid_column?(procedure, table, column) || EXTRA_SORT_COLUMNS[table]&.include?(column)
def valid_sort_column?(table, column)
valid_column?(table, column) || EXTRA_SORT_COLUMNS[table]&.include?(column)
end
end

View file

@ -1,172 +0,0 @@
class DossierFieldService
def initialize
@column_whitelist = {}
end
def fields(procedure)
fields = [
field_hash('Créé le', 'self', 'created_at'),
field_hash('Mis à jour le', 'self', 'updated_at'),
field_hash('Demandeur', 'user', 'email')
]
if !procedure.for_individual || (procedure.for_individual && procedure.individual_with_siret)
fields.push(
field_hash('SIREN', 'etablissement', 'entreprise_siren'),
field_hash('Forme juridique', 'etablissement', 'entreprise_forme_juridique'),
field_hash('Nom commercial', 'etablissement', 'entreprise_nom_commercial'),
field_hash('Raison sociale', 'etablissement', 'entreprise_raison_sociale'),
field_hash('SIRET siège social', 'etablissement', 'entreprise_siret_siege_social'),
field_hash('Date de création', 'etablissement', 'entreprise_date_creation')
)
fields.push(
field_hash('SIRET', 'etablissement', 'siret'),
field_hash('Libellé NAF', 'etablissement', 'libelle_naf'),
field_hash('Code postal', 'etablissement', 'code_postal')
)
end
explanatory_types_de_champ = [:header_section, :explication].map{ |k| TypeDeChamp.type_champs.fetch(k) }
fields.concat procedure.types_de_champ
.reject { |tdc| explanatory_types_de_champ.include?(tdc.type_champ) }
.map { |type_de_champ| field_hash(type_de_champ.libelle, 'type_de_champ', type_de_champ.id.to_s) }
fields.concat procedure.types_de_champ_private
.reject { |tdc| explanatory_types_de_champ.include?(tdc.type_champ) }
.map { |type_de_champ| field_hash(type_de_champ.libelle, 'type_de_champ_private', type_de_champ.id.to_s) }
fields
end
def get_value(dossier, table, column)
assert_valid_column(dossier.procedure, table, column)
case table
when 'self'
dossier.send(column)
when 'user'
dossier.user.send(column)
when 'etablissement'
dossier.etablissement&.send(column)
when 'type_de_champ'
dossier.champs.find { |c| c.type_de_champ_id == column.to_i }.value
when 'type_de_champ_private'
dossier.champs_private.find { |c| c.type_de_champ_id == column.to_i }.value
end
end
def assert_valid_column(procedure, table, column)
if !valid_column?(procedure, table, column)
raise "Invalid column #{table}.#{column}"
end
end
def valid_column?(procedure, table, column)
valid_columns_for_table(procedure, table).include?(column)
end
def filtered_ids(dossiers, filters)
filters.map do |filter|
table = filter['table']
column = sanitized_column(filter)
case table
when 'self'
dossiers.where("? ILIKE ?", filter['column'], "%#{filter['value']}%")
when 'type_de_champ', 'type_de_champ_private'
relation = table == 'type_de_champ' ? :champs : :champs_private
dossiers
.includes(relation)
.where("champs.type_de_champ_id = ?", filter['column'].to_i)
.where("champs.value ILIKE ?", "%#{filter['value']}%")
when 'etablissement'
if filter['column'] == 'entreprise_date_creation'
date = filter['value'].to_date rescue nil
dossiers
.includes(table)
.where("#{column} = ?", date)
else
dossiers
.includes(table)
.where("#{column} ILIKE ?", "%#{filter['value']}%")
end
when 'user'
dossiers
.includes(table)
.where("#{column} ILIKE ?", "%#{filter['value']}%")
end.pluck(:id)
end.reduce(:&)
end
def sorted_ids(dossiers, procedure_presentation, gestionnaire)
table = procedure_presentation.sort['table']
column = sanitized_column(procedure_presentation.sort)
order = procedure_presentation.sort['order']
assert_valid_order(order)
case table
when 'notifications'
procedure = procedure_presentation.assign_to.procedure
dossiers_id_with_notification = gestionnaire.notifications_for_procedure(procedure)
if order == 'desc'
return dossiers_id_with_notification +
(dossiers.order('dossiers.updated_at desc').ids - dossiers_id_with_notification)
else
return (dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) +
dossiers_id_with_notification
end
when 'self'
return dossiers
.order("#{column} #{order}")
.pluck(:id)
when 'type_de_champ', 'type_de_champ_private'
return dossiers
.includes(table == 'type_de_champ' ? :champs : :champs_private)
.where("champs.type_de_champ_id = #{procedure_presentation.sort['column'].to_i}")
.order("champs.value #{order}")
.pluck(:id)
else
return dossiers
.includes(table)
.order("#{column} #{order}")
.pluck(:id)
end
end
private
def valid_columns_for_table(procedure, table)
if !@column_whitelist.key?(procedure.id)
@column_whitelist[procedure.id] = fields(procedure)
.group_by { |field| field['table'] }
.map { |table, fields| [table, Set.new(fields.map { |field| field['column'] }) ] }
.to_h
end
@column_whitelist[procedure.id][table] || []
end
def sanitized_column(field)
table = field['table']
table = ActiveRecord::Base.connection.quote_column_name((table == 'self' ? 'dossier' : table).pluralize)
column = ActiveRecord::Base.connection.quote_column_name(field['column'])
table + '.' + column
end
def assert_valid_order(order)
if !["asc", "desc"].include?(order)
raise "Invalid order #{order}"
end
end
def field_hash(label, table, column)
{
'label' => label,
'table' => table,
'column' => column
}
end
end

View file

@ -73,7 +73,7 @@
= render partial: "header_field", locals: { field: { "label" => "Nº dossier", "table" => "self", "column" => "id" }, classname: "number-col" }
- @displayed_fields.each do |field|
- @procedure_presentation.displayed_fields.each do |field|
= render partial: "header_field", locals: { field: field, classname: "" }
= render partial: "header_field", locals: { field: { "label" => "Statut", "table" => "self", "column" => "state" }, classname: "status-col" }
@ -85,14 +85,13 @@
.dropdown-content.fade-in-down
= form_tag update_displayed_fields_gestionnaire_procedure_path(@procedure), method: :patch, class: 'dropdown-form columns-form' do
= select_tag :values,
options_for_select(@procedure.fields_for_select,
options_for_select(@procedure_presentation.fields_for_select,
selected: @displayed_fields_values),
multiple: true,
class: 'select2-limited'
= submit_tag "Enregistrer", class: 'button'
%tbody
- dossier_field_service = DossierFieldService.new
- @dossiers.each do |dossier|
%tr
%td.folder-col
@ -105,10 +104,9 @@
= link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do
= dossier.id
- @displayed_fields.each do |field|
- @procedure_presentation.displayed_field_values(dossier).each do |value|
%td
= link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do
= dossier_field_service.get_value(dossier, field['table'], field['column'])
= link_to(value, gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link')
%td.status-col
= link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do

View file

@ -1,26 +1,66 @@
{
"ignored_warnings": [
{
"warning_type": "Redirect",
"warning_code": 18,
"fingerprint": "a0a4cede6d50308b90bd747efd0a2ebd58947fbd5d100349ccd640c60413b1a9",
"check_name": "Redirect",
"message": "Possible unprotected redirect",
"file": "app/controllers/france_connect/particulier_controller.rb",
"line": 3,
"link": "http://brakemanscanner.org/docs/warning_types/redirect/",
"code": "redirect_to(FranceConnectParticulierClient.new.authorization_uri)",
"warning_type": "SQL Injection",
"warning_code": 0,
"fingerprint": "030551e51e29561702bcb9760bdeeed15e1936d4a9537f27e5d1d13a0ebb34ef",
"check_name": "SQL",
"message": "Possible SQL injection",
"file": "app/models/procedure_presentation.rb",
"line": 130,
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
"code": "dossiers.includes(sort[\"table\"]).order(\"#{sanitized_column(sort)} #{sort[\"order\"]}\")",
"render_path": null,
"location": {
"type": "method",
"class": "FranceConnect::ParticulierController",
"method": "login"
"class": "ProcedurePresentation",
"method": "sorted_ids"
},
"user_input": "FranceConnectParticulierClient.new.authorization_uri",
"confidence": "High",
"note": "We trust FC OpenId implem"
"user_input": "sanitized_column(sort)",
"confidence": "Weak",
"note": "Not an injection because of `sanitized_column`"
},
{
"warning_type": "SQL Injection",
"warning_code": 0,
"fingerprint": "1840f5340630814ea86311e850ebd91b966e6bccd0b6856133528e7745c0695a",
"check_name": "SQL",
"message": "Possible SQL injection",
"file": "app/models/procedure_presentation.rb",
"line": 119,
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
"code": "dossiers.order(\"#{sanitized_column(sort)} #{sort[\"order\"]}\")",
"render_path": null,
"location": {
"type": "method",
"class": "ProcedurePresentation",
"method": "sorted_ids"
},
"user_input": "sanitized_column(sort)",
"confidence": "Weak",
"note": "Not an injection because of `sanitized_column`"
},
{
"warning_type": "SQL Injection",
"warning_code": 0,
"fingerprint": "b2feda5e5ae668cdbf0653f134c40bcb9e45499c1b607450e43a0166c4098364",
"check_name": "SQL",
"message": "Possible SQL injection",
"file": "app/models/procedure_presentation.rb",
"line": 125,
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
"code": "dossiers.includes(((\"type_de_champ\" == \"type_de_champ\") ? (:champs) : (:champs_private))).where(\"champs.type_de_champ_id = #{sort[\"column\"].to_i}\").order(\"champs.value #{sort[\"order\"]}\")",
"render_path": null,
"location": {
"type": "method",
"class": "ProcedurePresentation",
"method": "sorted_ids"
},
"user_input": "sort[\"order\"]",
"confidence": "Weak",
"note": "Not an injection because `sort[\"order\"]` has passed `check_allowed_sort_order`"
}
],
"updated": "2018-01-11 15:53:22 +0100",
"brakeman_version": "3.7.0"
"updated": "2018-10-05 16:12:32 +0200",
"brakeman_version": "4.3.1"
}

View file

@ -3,17 +3,19 @@ require Rails.root.join("lib", "tasks", "task_helper")
namespace :support do
desc <<~EOD
Give procedure #PROCEDURE_ID a new owner.
The owner can be specified with NEW_OWNER_ID or NEW_OWNER_MAIL.
The new owner can be specified with NEW_OWNER_ID or NEW_OWNER_EMAIL.
EOD
task transfer_procedure_ownership: :environment do
new_owner_id = ENV['NEW_OWNER_ID']
new_owner_mail = ENV['NEW_OWNER_MAIL']
new_owner_email = ENV['NEW_OWNER_EMAIL']
new_owner = nil
if new_owner_id.present?
rake_puts("Looking for new owner by id\n")
new_owner = Administrateur.find(new_owner_id)
elsif new_owner_mail.present?
new_owner = Administrateur.find_by(email: new_owner_mail)
elsif new_owner_email.present?
rake_puts("Looking for new owner by email\n")
new_owner = Administrateur.find_by('LOWER(email) = LOWER(?)', new_owner_email)
end
if new_owner.blank?
@ -33,15 +35,64 @@ namespace :support do
end
desc <<~EOD
Delete the user account for a given USER_MAIL.
Give all procedures owned by OLD_OWNER_ID or OLD_OWNER_EMAIL a new owner.
The new owner can be specified with NEW_OWNER_ID or NEW_OWNER_EMAIL.
EOD
task transfer_all_procedures_ownership: :environment do
old_owner_id = ENV['OLD_OWNER_ID']
old_owner_email = ENV['OLD_OWNER_EMAIL']
new_owner_id = ENV['NEW_OWNER_ID']
new_owner_email = ENV['NEW_OWNER_EMAIL']
old_owner = nil
if old_owner_id.present?
rake_puts("Looking for old owner by id\n")
old_owner = Administrateur.find(old_owner_id)
elsif old_owner_email.present?
rake_puts("Looking for old owner by email\n")
old_owner = Administrateur.find_by('LOWER(email) = LOWER(?)', old_owner_email)
end
if old_owner.blank?
fail "Must specify an old owner"
end
procedures = old_owner.procedures
new_owner = nil
if new_owner_id.present?
rake_puts("Looking for new owner by id\n")
new_owner = Administrateur.find(new_owner_id)
elsif new_owner_email.present?
rake_puts("Looking for new owner by email\n")
new_owner = Administrateur.find_by('LOWER(email) = LOWER(?)', new_owner_email)
end
if new_owner.blank?
fail "Must specify a new owner"
end
procedures.update_all(administrateur_id: new_owner.id)
procedures.pluck(:id).each do |procedure_id|
ProcedurePath.where(procedure_id: procedure_id).each do |pp|
rake_puts("Changing owner of procedure_path #{pp.path} from ##{pp.administrateur_id} to ##{new_owner.id}")
pp.update(administrateur: new_owner)
end
end
end
desc <<~EOD
Delete the user account for a given USER_EMAIL.
Only works if the user has no dossier where the instruction has started.
EOD
task delete_user_account: :environment do
user_mail = ENV['USER_MAIL']
if user_mail.nil?
fail "Must specify a USER_MAIL"
user_email = ENV['USER_EMAIL']
if user_email.nil?
fail "Must specify a USER_EMAIL"
end
user = User.find_by(email: user_mail)
user = User.find_by(email: user_email)
if user.dossiers.state_instruction_commencee.any?
fail "Cannot delete this user because instruction has started for some dossiers"
end

View file

@ -1,7 +1,8 @@
require 'spec_helper'
describe ProcedurePresentation do
let(:assign_to) { create(:assign_to, procedure: create(:procedure, :with_type_de_champ)) }
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
let(:assign_to) { create(:assign_to, procedure: procedure) }
let(:first_type_de_champ_id) { assign_to.procedure.types_de_champ.first.id.to_s }
let (:procedure_presentation_id) {
ProcedurePresentation.create(
@ -46,4 +47,280 @@ describe ProcedurePresentation do
it { expect(build(:procedure_presentation, filters: { "suivis" => [{ "table" => "user", "column" => "reset_password_token", "order" => "asc" }] })).to be_invalid }
end
end
describe "#fields" do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :types_de_champ_count => 4, :types_de_champ_private_count => 4) }
let(:tdc_1) { procedure.types_de_champ[0] }
let(:tdc_2) { procedure.types_de_champ[1] }
let(:tdc_private_1) { procedure.types_de_champ_private[0] }
let(:tdc_private_2) { procedure.types_de_champ_private[1] }
let(:expected) {
[
{ "label" => 'Créé le', "table" => 'self', "column" => 'created_at' },
{ "label" => 'Mis à jour le', "table" => 'self', "column" => 'updated_at' },
{ "label" => 'Demandeur', "table" => 'user', "column" => 'email' },
{ "label" => 'SIREN', "table" => 'etablissement', "column" => 'entreprise_siren' },
{ "label" => 'Forme juridique', "table" => 'etablissement', "column" => 'entreprise_forme_juridique' },
{ "label" => 'Nom commercial', "table" => 'etablissement', "column" => 'entreprise_nom_commercial' },
{ "label" => 'Raison sociale', "table" => 'etablissement', "column" => 'entreprise_raison_sociale' },
{ "label" => 'SIRET siège social', "table" => 'etablissement', "column" => 'entreprise_siret_siege_social' },
{ "label" => 'Date de création', "table" => 'etablissement', "column" => 'entreprise_date_creation' },
{ "label" => 'SIRET', "table" => 'etablissement', "column" => 'siret' },
{ "label" => 'Libellé NAF', "table" => 'etablissement', "column" => 'libelle_naf' },
{ "label" => 'Code postal', "table" => 'etablissement', "column" => 'code_postal' },
{ "label" => tdc_1.libelle, "table" => 'type_de_champ', "column" => tdc_1.id.to_s },
{ "label" => tdc_2.libelle, "table" => 'type_de_champ', "column" => tdc_2.id.to_s },
{ "label" => tdc_private_1.libelle, "table" => 'type_de_champ_private', "column" => tdc_private_1.id.to_s },
{ "label" => tdc_private_2.libelle, "table" => 'type_de_champ_private', "column" => tdc_private_2.id.to_s }
]
}
before do
procedure.types_de_champ[2].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:header_section))
procedure.types_de_champ[3].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:explication))
procedure.types_de_champ_private[2].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:header_section))
procedure.types_de_champ_private[3].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:explication))
end
subject { create(:procedure_presentation, assign_to: create(:assign_to, procedure: procedure)) }
it { expect(subject.fields).to eq(expected) }
end
describe "#fields_for_select" do
subject { create(:procedure_presentation) }
before do
allow(subject).to receive(:fields).and_return([
{
"label" => "label1",
"table" => "table1",
"column" => "column1"
},
{
"label" => "label2",
"table" => "table2",
"column" => "column2"
}
])
end
it { expect(subject.fields_for_select).to eq([["label1", "table1/column1"], ["label2", "table2/column2"]]) }
end
describe '#get_value' do
let(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to, displayed_fields: [{ 'table' => table, 'column' => column }]) }
subject { procedure_presentation.displayed_field_values(dossier).first }
context 'for self table' do
let(:table) { 'self' }
let(:column) { 'updated_at' } # All other columns work the same, no extra test required
let(:dossier) { create(:dossier, procedure: procedure) }
before { dossier.touch(time: DateTime.new(2018, 9, 25)) }
it { is_expected.to eq(DateTime.new(2018, 9, 25)) }
end
context 'for user table' do
let(:table) { 'user' }
let(:column) { 'email' }
let(:dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'bla@yopmail.com')) }
it { is_expected.to eq('bla@yopmail.com') }
end
context 'for etablissement table' do
let(:table) { 'etablissement' }
let(:column) { 'code_postal' } # All other columns work the same, no extra test required
let!(:dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75008')) }
it { is_expected.to eq('75008') }
end
context 'for type_de_champ table' do
let(:table) { 'type_de_champ' }
let(:column) { procedure.types_de_champ.first.id.to_s }
let(:dossier) { create(:dossier, procedure: procedure) }
before { dossier.champs.first.update(value: 'kale') }
it { is_expected.to eq('kale') }
end
context 'for type_de_champ_private table' do
let(:table) { 'type_de_champ_private' }
let(:column) { procedure.types_de_champ_private.first.id.to_s }
let(:dossier) { create(:dossier, procedure: procedure) }
before { dossier.champs_private.first.update(value: 'quinoa') }
it { is_expected.to eq('quinoa') }
end
end
describe '#sorted_ids' do
let(:gestionnaire) { create(:gestionnaire) }
let(:assign_to) { create(:assign_to, procedure: procedure, gestionnaire: gestionnaire) }
let(:sort) { { 'table' => table, 'column' => column, 'order' => order } }
let(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to, sort: sort) }
subject { procedure_presentation.sorted_ids(procedure.dossiers, gestionnaire) }
context 'for notifications table' do
let(:table) { 'notifications' }
let(:column) { 'notifications' }
let!(:notified_dossier) { create(:dossier, :en_construction, procedure: procedure) }
let!(:recent_dossier) { create(:dossier, :en_construction, procedure: procedure) }
let!(:older_dossier) { create(:dossier, :en_construction, procedure: procedure) }
before do
notified_dossier.champs.first.touch(time: DateTime.new(2018, 9, 20))
create(:follow, gestionnaire: gestionnaire, dossier: notified_dossier, demande_seen_at: DateTime.new(2018, 9, 10))
recent_dossier.touch(time: DateTime.new(2018, 9, 25))
older_dossier.touch(time: DateTime.new(2018, 5, 13))
end
context 'in ascending order' do
let(:order) { 'asc' }
it { is_expected.to eq([older_dossier, recent_dossier, notified_dossier].map(&:id)) }
end
context 'in descending order' do
let(:order) { 'desc' }
it { is_expected.to eq([notified_dossier, recent_dossier, older_dossier].map(&:id)) }
end
end
context 'for self table' do
let(:table) { 'self' }
let(:column) { 'updated_at' } # All other columns work the same, no extra test required
let(:order) { 'asc' } # Desc works the same, no extra test required
let(:recent_dossier) { create(:dossier, procedure: procedure) }
let(:older_dossier) { create(:dossier, procedure: procedure) }
before do
recent_dossier.touch(time: DateTime.new(2018, 9, 25))
older_dossier.touch(time: DateTime.new(2018, 5, 13))
end
it { is_expected.to eq([older_dossier, recent_dossier].map(&:id)) }
end
context 'for type_de_champ table' do
let(:table) { 'type_de_champ' }
let(:column) { procedure.types_de_champ.first.id.to_s }
let(:order) { 'desc' } # Asc works the same, no extra test required
let(:beurre_dossier) { create(:dossier, procedure: procedure) }
let(:tartine_dossier) { create(:dossier, procedure: procedure) }
before do
beurre_dossier.champs.first.update(value: 'beurre')
tartine_dossier.champs.first.update(value: 'tartine')
end
it { is_expected.to eq([tartine_dossier, beurre_dossier].map(&:id)) }
end
context 'for type_de_champ_private table' do
let(:table) { 'type_de_champ_private' }
let(:column) { procedure.types_de_champ_private.first.id.to_s }
let(:order) { 'asc' } # Desc works the same, no extra test required
let(:biere_dossier) { create(:dossier, procedure: procedure) }
let(:vin_dossier) { create(:dossier, procedure: procedure) }
before do
biere_dossier.champs_private.first.update(value: 'biere')
vin_dossier.champs_private.first.update(value: 'vin')
end
it { is_expected.to eq([biere_dossier, vin_dossier].map(&:id)) }
end
context 'for other tables' do
# All other columns and tables work the same so its ok to test only one
let(:table) { 'etablissement' }
let(:column) { 'code_postal' }
let(:order) { 'asc' } # Desc works the same, no extra test required
let!(:huitieme_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75008')) }
let!(:vingtieme_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75020')) }
it { is_expected.to eq([huitieme_dossier, vingtieme_dossier].map(&:id)) }
end
end
describe '#filtered_ids' do
let(:procedure_presentation) { create(:procedure_presentation, assign_to: create(:assign_to, procedure: procedure), filters: { "suivis" => filter }) }
subject { procedure_presentation.filtered_ids(procedure.dossiers, 'suivis') }
context 'for type_de_champ table' do
let(:kept_dossier) { create(:dossier, procedure: procedure) }
let(:discarded_dossier) { create(:dossier, procedure: procedure) }
let(:type_de_champ) { procedure.types_de_champ.first }
let(:filter) { [{ 'table' => 'type_de_champ', 'column' => type_de_champ.id.to_s, 'value' => 'keep' }] }
before do
type_de_champ.champ.create(dossier: kept_dossier, value: 'keep me')
type_de_champ.champ.create(dossier: discarded_dossier, value: 'discard me')
end
it { is_expected.to contain_exactly(kept_dossier.id) }
end
context 'for type_de_champ_private table' do
let(:kept_dossier) { create(:dossier, procedure: procedure) }
let(:discarded_dossier) { create(:dossier, procedure: procedure) }
let(:type_de_champ_private) { procedure.types_de_champ_private.first }
let(:filter) { [{ 'table' => 'type_de_champ_private', 'column' => type_de_champ_private.id.to_s, 'value' => 'keep' }] }
before do
type_de_champ_private.champ.create(dossier: kept_dossier, value: 'keep me')
type_de_champ_private.champ.create(dossier: discarded_dossier, value: 'discard me')
end
it { is_expected.to contain_exactly(kept_dossier.id) }
end
context 'for etablissement table' do
context 'for entreprise_date_creation column' do
let!(:kept_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, entreprise_date_creation: DateTime.new(2018, 6, 21))) }
let!(:discarded_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, entreprise_date_creation: DateTime.new(2008, 6, 21))) }
let(:filter) { [{ 'table' => 'etablissement', 'column' => 'entreprise_date_creation', 'value' => '21/6/2018' }] }
it { is_expected.to contain_exactly(kept_dossier.id) }
end
context 'for code_postal column' do
# All columns except entreprise_date_creation work exacly the same, just testing one
let!(:kept_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75017')) }
let!(:discarded_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '25000')) }
let(:filter) { [{ 'table' => 'etablissement', 'column' => 'code_postal', 'value' => '75017' }] }
it { is_expected.to contain_exactly(kept_dossier.id) }
end
end
context 'for user table' do
let!(:kept_dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'me@keepmail.com')) }
let!(:discarded_dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'me@discard.com')) }
let(:filter) { [{ 'table' => 'user', 'column' => 'email', 'value' => 'keepmail' }] }
it { is_expected.to contain_exactly(kept_dossier.id) }
end
end
end

View file

@ -628,64 +628,6 @@ describe Procedure do
end
end
describe "#fields" do
subject { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :types_de_champ_count => 4, :types_de_champ_private_count => 4) }
let(:tdc_1) { subject.types_de_champ[0] }
let(:tdc_2) { subject.types_de_champ[1] }
let(:tdc_private_1) { subject.types_de_champ_private[0] }
let(:tdc_private_2) { subject.types_de_champ_private[1] }
let(:expected) {
[
{ "label" => 'Créé le', "table" => 'self', "column" => 'created_at' },
{ "label" => 'Mis à jour le', "table" => 'self', "column" => 'updated_at' },
{ "label" => 'Demandeur', "table" => 'user', "column" => 'email' },
{ "label" => 'SIREN', "table" => 'etablissement', "column" => 'entreprise_siren' },
{ "label" => 'Forme juridique', "table" => 'etablissement', "column" => 'entreprise_forme_juridique' },
{ "label" => 'Nom commercial', "table" => 'etablissement', "column" => 'entreprise_nom_commercial' },
{ "label" => 'Raison sociale', "table" => 'etablissement', "column" => 'entreprise_raison_sociale' },
{ "label" => 'SIRET siège social', "table" => 'etablissement', "column" => 'entreprise_siret_siege_social' },
{ "label" => 'Date de création', "table" => 'etablissement', "column" => 'entreprise_date_creation' },
{ "label" => 'SIRET', "table" => 'etablissement', "column" => 'siret' },
{ "label" => 'Libellé NAF', "table" => 'etablissement', "column" => 'libelle_naf' },
{ "label" => 'Code postal', "table" => 'etablissement', "column" => 'code_postal' },
{ "label" => tdc_1.libelle, "table" => 'type_de_champ', "column" => tdc_1.id.to_s },
{ "label" => tdc_2.libelle, "table" => 'type_de_champ', "column" => tdc_2.id.to_s },
{ "label" => tdc_private_1.libelle, "table" => 'type_de_champ_private', "column" => tdc_private_1.id.to_s },
{ "label" => tdc_private_2.libelle, "table" => 'type_de_champ_private', "column" => tdc_private_2.id.to_s }
]
}
before do
subject.types_de_champ[2].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:header_section))
subject.types_de_champ[3].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:explication))
subject.types_de_champ_private[2].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:header_section))
subject.types_de_champ_private[3].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:explication))
end
it { expect(subject.fields).to eq(expected) }
end
describe "#fields_for_select" do
subject { create(:procedure) }
before do
allow(subject).to receive(:fields).and_return([
{
"label" => "label1",
"table" => "table1",
"column" => "column1"
},
{
"label" => "label2",
"table" => "table2",
"column" => "column2"
}
])
end
it { expect(subject.fields_for_select).to eq([["label1", "table1/column1"], ["label2", "table2/column2"]]) }
end
describe ".default_sort" do
it { expect(Procedure.default_sort).to eq({ "table" => "self", "column" => "id", "order" => "desc" }) }
end

View file

@ -1,220 +0,0 @@
require 'spec_helper'
describe DossierFieldService do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
describe '#filtered_ids' do
context 'for type_de_champ table' do
let(:kept_dossier) { create(:dossier, procedure: procedure) }
let(:discarded_dossier) { create(:dossier, procedure: procedure) }
let(:type_de_champ) { procedure.types_de_champ.first }
before do
type_de_champ.champ.create(dossier: kept_dossier, value: 'keep me')
type_de_champ.champ.create(dossier: discarded_dossier, value: 'discard me')
end
subject { described_class.new.filtered_ids(procedure.dossiers, [{ 'table' => 'type_de_champ', 'column' => type_de_champ.id, 'value' => 'keep' }]) }
it { is_expected.to contain_exactly(kept_dossier.id) }
end
context 'for type_de_champ_private table' do
let(:kept_dossier) { create(:dossier, procedure: procedure) }
let(:discarded_dossier) { create(:dossier, procedure: procedure) }
let(:type_de_champ_private) { procedure.types_de_champ_private.first }
before do
type_de_champ_private.champ.create(dossier: kept_dossier, value: 'keep me')
type_de_champ_private.champ.create(dossier: discarded_dossier, value: 'discard me')
end
subject { described_class.new.filtered_ids(procedure.dossiers, [{ 'table' => 'type_de_champ_private', 'column' => type_de_champ_private.id, 'value' => 'keep' }]) }
it { is_expected.to contain_exactly(kept_dossier.id) }
end
context 'for etablissement table' do
context 'for entreprise_date_creation column' do
let!(:kept_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, entreprise_date_creation: DateTime.new(2018, 6, 21))) }
let!(:discarded_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, entreprise_date_creation: DateTime.new(2008, 6, 21))) }
subject { described_class.new.filtered_ids(procedure.dossiers, [{ 'table' => 'etablissement', 'column' => 'entreprise_date_creation', 'value' => '21/6/2018' }]) }
it { is_expected.to contain_exactly(kept_dossier.id) }
end
context 'for code_postal column' do
# All columns except entreprise_date_creation work exacly the same, just testing one
let!(:kept_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75017')) }
let!(:discarded_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '25000')) }
subject { described_class.new.filtered_ids(procedure.dossiers, [{ 'table' => 'etablissement', 'column' => 'code_postal', 'value' => '75017' }]) }
it { is_expected.to contain_exactly(kept_dossier.id) }
end
end
context 'for user table' do
let!(:kept_dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'me@keepmail.com')) }
let!(:discarded_dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'me@discard.com')) }
subject { described_class.new.filtered_ids(procedure.dossiers, [{ 'table' => 'user', 'column' => 'email', 'value' => 'keepmail' }]) }
it { is_expected.to contain_exactly(kept_dossier.id) }
end
end
describe '#sorted_ids' do
let(:gestionnaire) { create(:gestionnaire) }
let(:assign_to) { create(:assign_to, procedure: procedure, gestionnaire: gestionnaire) }
let(:sort) { { 'table' => table, 'column' => column, 'order' => order } }
let(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to, sort: sort) }
subject { described_class.new.sorted_ids(procedure.dossiers, procedure_presentation, gestionnaire) }
context 'for notifications table' do
let(:table) { 'notifications' }
let(:column) { 'notifications' }
let!(:notified_dossier) { create(:dossier, :en_construction, procedure: procedure) }
let!(:recent_dossier) { create(:dossier, :en_construction, procedure: procedure) }
let!(:older_dossier) { create(:dossier, :en_construction, procedure: procedure) }
before do
notified_dossier.champs.first.touch(time: DateTime.new(2018, 9, 20))
create(:follow, gestionnaire: gestionnaire, dossier: notified_dossier, demande_seen_at: DateTime.new(2018, 9, 10))
recent_dossier.touch(time: DateTime.new(2018, 9, 25))
older_dossier.touch(time: DateTime.new(2018, 5, 13))
end
context 'in ascending order' do
let(:order) { 'asc' }
it { is_expected.to eq([older_dossier, recent_dossier, notified_dossier].map(&:id)) }
end
context 'in descending order' do
let(:order) { 'desc' }
it { is_expected.to eq([notified_dossier, recent_dossier, older_dossier].map(&:id)) }
end
end
context 'for self table' do
let(:table) { 'self' }
let(:column) { 'updated_at' } # All other columns work the same, no extra test required
let(:order) { 'asc' } # Desc works the same, no extra test required
let(:recent_dossier) { create(:dossier, procedure: procedure) }
let(:older_dossier) { create(:dossier, procedure: procedure) }
before do
recent_dossier.touch(time: DateTime.new(2018, 9, 25))
older_dossier.touch(time: DateTime.new(2018, 5, 13))
end
it { is_expected.to eq([older_dossier, recent_dossier].map(&:id)) }
end
context 'for type_de_champ table' do
let(:table) { 'type_de_champ' }
let(:column) { procedure.types_de_champ.first.id.to_s }
let(:order) { 'desc' } # Asc works the same, no extra test required
let(:beurre_dossier) { create(:dossier, procedure: procedure) }
let(:tartine_dossier) { create(:dossier, procedure: procedure) }
before do
beurre_dossier.champs.first.update(value: 'beurre')
tartine_dossier.champs.first.update(value: 'tartine')
end
it { is_expected.to eq([tartine_dossier, beurre_dossier].map(&:id)) }
end
context 'for type_de_champ_private table' do
let(:table) { 'type_de_champ_private' }
let(:column) { procedure.types_de_champ_private.first.id.to_s }
let(:order) { 'asc' } # Desc works the same, no extra test required
let(:biere_dossier) { create(:dossier, procedure: procedure) }
let(:vin_dossier) { create(:dossier, procedure: procedure) }
before do
biere_dossier.champs_private.first.update(value: 'biere')
vin_dossier.champs_private.first.update(value: 'vin')
end
it { is_expected.to eq([biere_dossier, vin_dossier].map(&:id)) }
end
context 'for other tables' do
# All other columns and tables work the same so its ok to test only one
let(:table) { 'etablissement' }
let(:column) { 'code_postal' }
let(:order) { 'asc' } # Desc works the same, no extra test required
let!(:huitieme_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75008')) }
let!(:vingtieme_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75020')) }
it { is_expected.to eq([huitieme_dossier, vingtieme_dossier].map(&:id)) }
end
end
describe '#get_value' do
subject { described_class.new.get_value(dossier, table, column) }
context 'for self table' do
let(:table) { 'self' }
let(:column) { 'updated_at' } # All other columns work the same, no extra test required
let(:dossier) { create(:dossier, procedure: procedure) }
before { dossier.touch(time: DateTime.new(2018, 9, 25)) }
it { is_expected.to eq(DateTime.new(2018, 9, 25)) }
end
context 'for user table' do
let(:table) { 'user' }
let(:column) { 'email' }
let(:dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'bla@yopmail.com')) }
it { is_expected.to eq('bla@yopmail.com') }
end
context 'for etablissement table' do
let(:table) { 'etablissement' }
let(:column) { 'code_postal' } # All other columns work the same, no extra test required
let!(:dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75008')) }
it { is_expected.to eq('75008') }
end
context 'for type_de_champ table' do
let(:table) { 'type_de_champ' }
let(:column) { procedure.types_de_champ.first.id.to_s }
let(:dossier) { create(:dossier, procedure: procedure) }
before { dossier.champs.first.update(value: 'kale') }
it { is_expected.to eq('kale') }
end
context 'for type_de_champ_private table' do
let(:table) { 'type_de_champ_private' }
let(:column) { procedure.types_de_champ_private.first.id.to_s }
let(:dossier) { create(:dossier, procedure: procedure) }
before { dossier.champs_private.first.update(value: 'quinoa') }
it { is_expected.to eq('quinoa') }
end
end
end