2015-11-03 10:48:40 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-11-03 15:27:49 +01:00
|
|
|
describe Champ do
|
2016-08-08 12:52:30 +02:00
|
|
|
require 'models/champ_shared_example.rb'
|
2015-11-03 10:48:40 +01:00
|
|
|
|
2016-08-08 12:52:30 +02:00
|
|
|
it_should_behave_like "champ_spec"
|
2017-07-27 20:54:02 +02:00
|
|
|
|
2018-02-09 17:38:30 +01:00
|
|
|
describe '#public?' do
|
2018-02-13 17:00:19 +01:00
|
|
|
let(:type_de_champ) { build(:type_de_champ) }
|
2018-02-09 17:38:30 +01:00
|
|
|
let(:champ) { type_de_champ.champ.build }
|
|
|
|
|
|
|
|
it { expect(champ.public?).to be_truthy }
|
|
|
|
it { expect(champ.private?).to be_falsey }
|
|
|
|
end
|
|
|
|
|
2018-02-14 18:13:23 +01:00
|
|
|
describe '#public_only' do
|
|
|
|
let(:dossier) { create(:dossier) }
|
|
|
|
|
|
|
|
it 'partition public and private' do
|
|
|
|
expect(dossier.champs.count).to eq(1)
|
|
|
|
expect(dossier.champs_private.count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-19 17:42:55 +01:00
|
|
|
describe '#format_datetime' do
|
2018-02-14 15:12:57 +01:00
|
|
|
let(:type_de_champ) { build(:type_de_champ_datetime) }
|
2018-02-09 17:38:30 +01:00
|
|
|
let(:champ) { type_de_champ.champ.build(value: value) }
|
2017-07-27 20:54:02 +02:00
|
|
|
|
|
|
|
before { champ.save }
|
|
|
|
|
2018-01-19 17:42:55 +01:00
|
|
|
context 'when the value is sent by a modern browser' do
|
|
|
|
let(:value) { '2017-12-31 10:23' }
|
2017-07-27 20:54:02 +02:00
|
|
|
|
|
|
|
it { expect(champ.value).to eq(value) }
|
|
|
|
end
|
|
|
|
|
2018-01-19 17:42:55 +01:00
|
|
|
context 'when the value is sent by a old browser' do
|
|
|
|
let(:value) { '31/12/2018 09:26' }
|
2017-07-27 20:54:02 +02:00
|
|
|
|
2018-01-19 17:42:55 +01:00
|
|
|
it { expect(champ.value).to eq('2018-12-31 09:26') }
|
2017-07-27 20:54:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#multiple_select_to_string' do
|
2018-02-14 15:12:57 +01:00
|
|
|
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list) }
|
2018-02-09 17:38:30 +01:00
|
|
|
let(:champ) { type_de_champ.champ.build(value: value) }
|
2017-07-27 20:54:02 +02:00
|
|
|
|
|
|
|
before { champ.save }
|
|
|
|
|
|
|
|
# when using the old form, and the ChampsService Class
|
|
|
|
# TODO: to remove
|
|
|
|
context 'when the value is already deserialized' do
|
|
|
|
let(:value) { '["1", "2"]' }
|
|
|
|
|
|
|
|
it { expect(champ.value).to eq(value) }
|
|
|
|
|
|
|
|
context 'when the value is nil' do
|
|
|
|
let(:value) { nil }
|
|
|
|
|
|
|
|
it { expect(champ.value).to eq(value) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# for explanation for the "" entry, see
|
|
|
|
# https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
|
|
|
|
# GOTCHA
|
|
|
|
context 'when the value is not already deserialized' do
|
|
|
|
context 'when a choice is selected' do
|
|
|
|
let(:value) { '["", "1", "2"]' }
|
|
|
|
|
|
|
|
it { expect(champ.value).to eq('["1", "2"]') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when all choices are removed' do
|
|
|
|
let(:value) { '[""]' }
|
|
|
|
|
|
|
|
it { expect(champ.value).to eq(nil) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-10-26 16:54:10 +02:00
|
|
|
|
|
|
|
describe 'for_export' do
|
2018-02-14 15:12:57 +01:00
|
|
|
let(:type_de_champ) { create(:type_de_champ) }
|
2018-02-09 17:38:30 +01:00
|
|
|
let(:champ) { type_de_champ.champ.build(value: value) }
|
2017-10-26 16:54:10 +02:00
|
|
|
|
|
|
|
before { champ.save }
|
|
|
|
|
|
|
|
context 'when type_de_champ is text' do
|
|
|
|
let(:value) { '123' }
|
|
|
|
|
|
|
|
it { expect(champ.for_export).to eq('123') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when type_de_champ is textarea' do
|
2018-02-14 15:12:57 +01:00
|
|
|
let(:type_de_champ) { create(:type_de_champ_textarea) }
|
2017-10-26 16:54:10 +02:00
|
|
|
let(:value) { '<b>gras<b>' }
|
|
|
|
|
|
|
|
it { expect(champ.for_export).to eq('gras') }
|
|
|
|
end
|
|
|
|
|
2017-10-26 17:45:35 +02:00
|
|
|
context 'when type_de_champ is yes_no' do
|
2018-02-14 15:12:57 +01:00
|
|
|
let(:type_de_champ) { create(:type_de_champ_yes_no) }
|
2017-10-26 17:45:35 +02:00
|
|
|
|
|
|
|
context 'if yes' do
|
2017-11-21 10:28:21 +01:00
|
|
|
let(:value) { 'true' }
|
2017-10-26 17:45:35 +02:00
|
|
|
|
|
|
|
it { expect(champ.for_export).to eq('oui') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if no' do
|
2017-11-21 10:28:21 +01:00
|
|
|
let(:value) { 'false' }
|
2017-10-26 17:45:35 +02:00
|
|
|
|
|
|
|
it { expect(champ.for_export).to eq('non') }
|
|
|
|
end
|
2017-11-21 10:28:21 +01:00
|
|
|
|
|
|
|
context 'if nil' do
|
|
|
|
let(:value) { nil }
|
|
|
|
|
|
|
|
it { expect(champ.for_export).to eq(nil) }
|
|
|
|
end
|
2017-10-26 17:45:35 +02:00
|
|
|
end
|
2017-10-27 10:01:53 +02:00
|
|
|
|
|
|
|
context 'when type_de_champ is multiple_drop_down_list' do
|
2018-02-14 15:12:57 +01:00
|
|
|
let(:type_de_champ) { create(:type_de_champ_multiple_drop_down_list) }
|
2017-10-27 10:01:53 +02:00
|
|
|
let(:value) { '["Crétinier", "Mousserie"]' }
|
|
|
|
|
|
|
|
it { expect(champ.for_export).to eq('Crétinier, Mousserie') }
|
|
|
|
end
|
2017-10-26 16:54:10 +02:00
|
|
|
end
|
2018-05-11 14:59:20 +02:00
|
|
|
|
|
|
|
describe '#enqueue_virus_check' do
|
|
|
|
let(:champ) { type_de_champ.champ.build(value: nil) }
|
|
|
|
|
|
|
|
context 'when type_champ is type_de_champ_piece_justificative' do
|
|
|
|
let(:type_de_champ) { create(:type_de_champ_piece_justificative) }
|
|
|
|
|
|
|
|
context 'and there is a blob' do
|
|
|
|
before { champ.piece_justificative_file.attach(io: StringIO.new("toto"), filename: "toto.txt", content_type: "text/plain") }
|
|
|
|
|
|
|
|
it { expect{ champ.save }.to change(VirusScan, :count).by(1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and there is no blob' do
|
|
|
|
it { expect{ champ.save }.to_not change(VirusScan, :count) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when type_champ is not type_de_champ_piece_justificative' do
|
|
|
|
let(:type_de_champ) { create(:type_de_champ_textarea) }
|
|
|
|
|
|
|
|
it { expect{ champ.save }.to_not change(VirusScan, :count) }
|
|
|
|
end
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|