Merge branch 'dev'

This commit is contained in:
Mathieu Magnin 2017-07-11 11:41:37 +02:00
commit f0273d6512
21 changed files with 284 additions and 185 deletions

View file

@ -6,7 +6,7 @@ class Admin::ProceduresController < AdminController
def index def index
@procedures = smart_listing_create :procedures, @procedures = smart_listing_create :procedures,
current_administrateur.procedures.where(published: true, archived: false).order(created_at: :desc), current_administrateur.procedures.published.not_archived.order(created_at: :desc),
partial: "admin/procedures/list", partial: "admin/procedures/list",
array: true array: true
@ -15,7 +15,7 @@ class Admin::ProceduresController < AdminController
def archived def archived
@procedures = smart_listing_create :procedures, @procedures = smart_listing_create :procedures,
current_administrateur.procedures.where(archived: true).order(created_at: :desc), current_administrateur.procedures.archived.order(created_at: :desc),
partial: "admin/procedures/list", partial: "admin/procedures/list",
array: true array: true
@ -26,7 +26,7 @@ class Admin::ProceduresController < AdminController
def draft def draft
@procedures = smart_listing_create :procedures, @procedures = smart_listing_create :procedures,
current_administrateur.procedures.where(published: false, archived: false).order(created_at: :desc), current_administrateur.procedures.not_published.not_archived.order(created_at: :desc),
partial: "admin/procedures/list", partial: "admin/procedures/list",
array: true array: true

View file

@ -9,10 +9,10 @@ class API::StatistiquesController < ApplicationController
private private
def total_dossiers def total_dossiers
Dossier.where.not(state: :draft).size Dossier.state_not_brouillon.size
end end
def dossiers_mois def dossiers_mois
Dossier.where.not(state: :draft).where(created_at: (1.month.ago)..Time.now).size Dossier.state_not_brouillon.where(created_at: (1.month.ago)..Time.now).size
end end
end end

View file

@ -7,7 +7,7 @@ class API::V1::DossiersController < APIController
def index def index
procedure = current_administrateur.procedures.find(params[:procedure_id]) procedure = current_administrateur.procedures.find(params[:procedure_id])
dossiers = procedure.dossiers.where.not(state: :draft).paginate(page: params[:page]) dossiers = procedure.dossiers.state_not_brouillon.paginate(page: params[:page])
render json: { dossiers: dossiers.map{ |dossier| DossiersSerializer.new(dossier) }, pagination: pagination(dossiers) }, status: 200 render json: { dossiers: dossiers.map{ |dossier| DossiersSerializer.new(dossier) }, pagination: pagination(dossiers) }, status: 200
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound

View file

@ -13,14 +13,16 @@ class CommentairesController < ApplicationController
def create def create
@commentaire = Commentaire.new @commentaire = Commentaire.new
@commentaire.dossier = Dossier.find(params['dossier_id'])
@commentaire.champ = @commentaire.dossier.champs.find(params[:champ_id]) if params[:champ_id] @commentaire.champ = @commentaire.dossier.champs.find(params[:champ_id]) if params[:champ_id]
dossier_id = params['dossier_id']
if is_gestionnaire? if is_gestionnaire?
@commentaire.email = current_gestionnaire.email @commentaire.email = current_gestionnaire.email
@commentaire.dossier = current_gestionnaire.dossiers.find_by(id: dossier_id) || current_gestionnaire.avis.find_by!(dossier_id: dossier_id).dossier
@commentaire.dossier.next_step! 'gestionnaire', 'comment' @commentaire.dossier.next_step! 'gestionnaire', 'comment'
else else
@commentaire.email = current_user.email @commentaire.email = current_user.email
@commentaire.dossier = current_user.dossiers.find_by(id: dossier_id) || current_user.invites.find_by!(dossier_id: dossier_id).dossier
@commentaire.dossier.next_step! 'user', 'comment' if current_user.email == @commentaire.dossier.user.email @commentaire.dossier.next_step! 'user', 'comment' if current_user.email == @commentaire.dossier.user.email
end end

View file

@ -6,7 +6,7 @@ class DemoController < ApplicationController
return redirect_to root_path if Rails.env.production? return redirect_to root_path if Rails.env.production?
smart_listing_create :procedures, smart_listing_create :procedures,
Procedure.where(archived: false, published: true).order("id DESC"), Procedure.published.not_archived.order("id DESC"),
partial: "demo/list", partial: "demo/list",
array: true array: true
end end

View file

@ -5,11 +5,12 @@ class InvitesController < ApplicationController
email_sender = @current_devise_profil.email email_sender = @current_devise_profil.email
class_var = @current_devise_profil.class == User ? InviteUser : InviteGestionnaire class_var = @current_devise_profil.class == User ? InviteUser : InviteGestionnaire
dossier = @current_devise_profil.dossiers.find(params[:dossier_id])
email = params[:email].downcase email = params[:email].downcase
user = User.find_by_email(email) user = User.find_by_email(email)
invite = class_var.create(dossier_id: params[:dossier_id], user: user, email: email, email_sender: email_sender) invite = class_var.create(dossier: dossier, user: user, email: email, email_sender: email_sender)
if invite.valid? if invite.valid?
InviteMailer.invite_user(invite).deliver_now! unless invite.user.nil? InviteMailer.invite_user(invite).deliver_now! unless invite.user.nil?

View file

@ -4,7 +4,7 @@ class StatsController < ApplicationController
MEAN_NUMBER_OF_CHAMPS_IN_A_FORM = 24.0 MEAN_NUMBER_OF_CHAMPS_IN_A_FORM = 24.0
def index def index
procedures = Procedure.where(:published => true) procedures = Procedure.published
dossiers = Dossier.where.not(:state => :draft) dossiers = Dossier.where.not(:state => :draft)
@procedures_count = procedures.count @procedures_count = procedures.count

View file

@ -60,7 +60,7 @@ class Users::DossiersController < UsersController
end end
def new def new
procedure = Procedure.where(archived: false, published: true).find(params[:procedure_id]) procedure = Procedure.not_archived.published.find(params[:procedure_id])
dossier = Dossier.create(procedure: procedure, user: current_user, state: 'draft') dossier = Dossier.create(procedure: procedure, user: current_user, state: 'draft')
siret = params[:siret] || current_user.siret siret = params[:siret] || current_user.siret

View file

@ -8,11 +8,11 @@ class AdminProceduresShowFacades
end end
def dossiers def dossiers
@procedure.dossiers.where.not(state: :draft) @procedure.dossiers.state_not_brouillon
end end
def dossiers_for_pie_highchart def dossiers_for_pie_highchart
dossiers.where.not(state: :draft, archived: true).group(:state).count dossiers.state_not_brouillon.not_archived.group(:state).count
.reduce({}) do |acc, (key, val)| .reduce({}) do |acc, (key, val)|
translated_key = DossierDecorator.case_state_fr(key) translated_key = DossierDecorator.case_state_fr(key)
acc[translated_key].nil? ? acc[translated_key] = val : acc[translated_key] += val acc[translated_key].nil? ? acc[translated_key] = val : acc[translated_key] += val
@ -21,11 +21,11 @@ class AdminProceduresShowFacades
end end
def dossiers_archived_by_state_total def dossiers_archived_by_state_total
dossiers.select('state, count(*) as total').where(archived: true).where.not(state: :termine).group(:state).order(:state).decorate dossiers.select('state, count(*) as total').archived.where.not(state: :termine).group(:state).order(:state).decorate
end end
def dossiers_archived_total def dossiers_archived_total
dossiers.where(archived: true).where.not(state: :termine).size dossiers.archived.where.not(state: :termine).size
end end
def dossiers_total def dossiers_total

View file

@ -14,7 +14,7 @@ class DossiersListFacades
end end
def total_dossier def total_dossier
current_devise_profil.dossiers.where(archived: false).count current_devise_profil.dossiers.not_archived.count
end end
def total_dossier_follow def total_dossier_follow
@ -22,11 +22,11 @@ class DossiersListFacades
end end
def total_new_dossier def total_new_dossier
current_devise_profil.dossiers.where(state: :initiated, archived: false).count current_devise_profil.dossiers.state_nouveaux.not_archived.count
end end
def new_dossier_number procedure_id def new_dossier_number procedure_id
current_devise_profil.dossiers.where(state: :initiated, archived: false, procedure_id: procedure_id).count current_devise_profil.dossiers.state_nouveaux.not_archived.where(procedure_id: procedure_id).count
end end
def gestionnaire_procedures_name_and_id_list def gestionnaire_procedures_name_and_id_list

View file

@ -8,7 +8,7 @@ class Gestionnaire < ActiveRecord::Base
has_many :assign_to, dependent: :destroy has_many :assign_to, dependent: :destroy
has_many :procedures, through: :assign_to has_many :procedures, through: :assign_to
has_many :dossiers, -> { where.not(state: :draft) }, through: :procedures has_many :dossiers, -> { state_not_brouillon }, through: :procedures
has_many :followed_dossiers, through: :follows, source: :dossier has_many :followed_dossiers, through: :follows, source: :dossier
has_many :follows has_many :follows
has_many :preference_list_dossiers has_many :preference_list_dossiers
@ -108,8 +108,7 @@ class Gestionnaire < ActiveRecord::Base
start_date = DateTime.now.beginning_of_week start_date = DateTime.now.beginning_of_week
active_procedure_overviews = procedures active_procedure_overviews = procedures
.where(published: true) .published
.all
.map { |procedure| procedure.procedure_overview(start_date) } .map { |procedure| procedure.procedure_overview(start_date) }
.select(&:had_some_activities?) .select(&:had_some_activities?)

View file

@ -32,6 +32,9 @@ class Procedure < ActiveRecord::Base
mount_uploader :logo, ProcedureLogoUploader mount_uploader :logo, ProcedureLogoUploader
default_scope { where(hidden_at: nil) } default_scope { where(hidden_at: nil) }
scope :published, -> { where(published: true) }
scope :not_published, -> { where(published: false) }
scope :archived, -> { where(archived: true) }
scope :not_archived, -> { where(archived: false) } scope :not_archived, -> { where(archived: false) }
scope :by_libelle, -> { order(libelle: :asc) } scope :by_libelle, -> { order(libelle: :asc) }
@ -61,7 +64,7 @@ class Procedure < ActiveRecord::Base
end end
def self.active id def self.active id
not_archived.where(published: true).find(id) not_archived.published.find(id)
end end
def switch_types_de_champ index_of_first_element def switch_types_de_champ index_of_first_element
@ -127,7 +130,7 @@ class Procedure < ActiveRecord::Base
end end
def total_dossier def total_dossier
self.dossiers.where.not(state: :draft).size self.dossiers.state_not_brouillon.size
end end
def generate_export def generate_export

View file

@ -46,8 +46,8 @@ class Search < ActiveRecord::Base
dossier_ids = @gestionnaire.dossiers dossier_ids = @gestionnaire.dossiers
.select(:id) .select(:id)
.where(archived: false) .not_archived
.where.not(state: "draft") .state_not_brouillon
q = Search q = Search
.select("DISTINCT(searches.dossier_id)") .select("DISTINCT(searches.dossier_id)")

View file

@ -18,10 +18,10 @@
= admin.last_sign_in_at.localtime.strftime('%d/%m/%Y') = admin.last_sign_in_at.localtime.strftime('%d/%m/%Y')
) )
%td %td
= admin.procedures.where(published: true).count = admin.procedures.published.count
%td %td
- total_dossier = 0 - total_dossier = 0
- admin.procedures.each do |procedure| total_dossier += procedure.dossiers.where.not(state: :draft).count end - admin.procedures.each do |procedure| total_dossier += procedure.dossiers.state_not_brouillon.count end
= total_dossier = total_dossier
= smart_listing.paginate = smart_listing.paginate
= smart_listing.pagination_per_page_links = smart_listing.pagination_per_page_links

View file

@ -1,2 +1,2 @@
%data.mj-w-data{ "data-apikey" => "1v5T", "data-base" => "https://app.mailjet.com", "data-height" => "328", "data-lang" => "fr_FR", "data-statics" => "statics", "data-token" => "11c89e7ddb46fbcdcb7f8fe5fdfca818", "data-w-id" => "39b", "data-width" => "640" } %data.mj-w-data{ "data-apikey" => "1v5T", "data-base" => "https://app.mailjet.com", "data-height" => "328", "data-lang" => "fr_FR", "data-statics" => "statics", "data-token" => "11c89e7ddb46fbcdcb7f8fe5fdfca818", "data-w-id" => "39b", "data-width" => "640" }
%script{ src: 'https://app.mailjet.com/statics/js/widget.modal.js' } %script{ src: 'https://app.mailjet.com/statics/js/widget.modal.js', 'data-turbolinks-eval': 'false' }

View file

@ -13,19 +13,19 @@
.procedure-list-element{ class: @draft_class } .procedure-list-element{ class: @draft_class }
Brouillons Brouillons
.badge.progress-bar-default .badge.progress-bar-default
= current_administrateur.procedures.where(published: false, archived: false).count = current_administrateur.procedures.not_published.not_archived.count
%a#active-procedures{ :href => "#{url_for :admin_procedures}" } %a#active-procedures{ :href => "#{url_for :admin_procedures}" }
.procedure-list-element{ class: @active_class } .procedure-list-element{ class: @active_class }
Actives Actives
.badge.progress-bar-success .badge.progress-bar-success
= current_administrateur.procedures.where(published: true, archived: false).count = current_administrateur.procedures.published.not_archived.count
%a#archived-procedures{ :href => "#{url_for :admin_procedures_archived}" } %a#archived-procedures{ :href => "#{url_for :admin_procedures_archived}" }
.procedure-list-element{ class: @archived_class } .procedure-list-element{ class: @archived_class }
Archivées Archivées
.badge.progress-bar-purple .badge.progress-bar-purple
= current_administrateur.procedures.where(archived: true).count = current_administrateur.procedures.archived.count
.split-hr-left .split-hr-left

View file

@ -35,7 +35,7 @@
= render partial: "layouts/mailjet_newsletter" = render partial: "layouts/mailjet_newsletter"
= render partial: "layouts/crisp" = render partial: "layouts/crisp"
= javascript_include_tag "new_design/application", "data-turbolinks-track": true = javascript_include_tag "new_design/application", "data-turbolinks-eval": false
= yield :charts_js = yield :charts_js
- if Rails.env == "test" - if Rails.env == "test"
%script{ type: "text/javascript" } %script{ type: "text/javascript" }

View file

@ -80,7 +80,7 @@
%ul.numbers %ul.numbers
%li.number %li.number
.number-value .number-value
= number_with_delimiter(Procedure.where(:published => true).count, :locale => :fr) = number_with_delimiter(Procedure.published.count, :locale => :fr)
.number-label< .number-label<
procédures procédures
%br<> %br<>

View file

@ -1,7 +1,7 @@
require 'spec_helper' require 'spec_helper'
describe Backoffice::CommentairesController, type: :controller do describe Backoffice::CommentairesController, type: :controller do
let(:dossier) { create(:dossier) } let(:dossier) { create(:dossier, :replied) }
let(:dossier_id) { dossier.id } let(:dossier_id) { dossier.id }
let(:email_commentaire) { 'test@test.com' } let(:email_commentaire) { 'test@test.com' }
let(:texte_commentaire) { 'Commentaire de test' } let(:texte_commentaire) { 'Commentaire de test' }
@ -16,116 +16,136 @@ describe Backoffice::CommentairesController, type: :controller do
sign_in gestionnaire sign_in gestionnaire
end end
context "création correct d'un commentaire" do context "when gestionnaire has no access to dossier" do
subject { post :create, params: {dossier_id: dossier_id, email_commentaire: email_commentaire, texte_commentaire: texte_commentaire} } subject { post :create, params: { dossier_id: dossier_id, texte_commentaire: texte_commentaire } }
it 'depuis la page admin' do it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
expect(subject).to redirect_to("/backoffice/dossiers/#{dossier_id}") it { expect { subject rescue nil }.to change(Commentaire, :count).by(0) }
end
context "when gestionnaire is invited for avis on dossier" do
subject { post :create, params: { dossier_id: dossier_id, texte_commentaire: texte_commentaire } }
before { Avis.create(dossier: dossier, gestionnaire: gestionnaire, claimant: create(:gestionnaire)) }
it { expect{ subject }.to change(Commentaire, :count).by(1) }
end
context "when gestionnaire has access to dossier" do
before do
gestionnaire.procedures << dossier.procedure
end end
it 'gestionnaire is automatically affect to follow the dossier' do context "création correct d'un commentaire" do
expect { subject }.to change(Follow, :count).by(1) subject { post :create, params: {dossier_id: dossier_id, email_commentaire: email_commentaire, texte_commentaire: texte_commentaire} }
end
context 'when gestionnaire already follow dossier' do it 'depuis la page admin' do
before do expect(subject).to redirect_to("/backoffice/dossiers/#{dossier_id}")
create :follow, gestionnaire_id: gestionnaire.id, dossier_id: dossier_id
end end
it 'gestionnaire is automatically affect to follow the dossier' do it 'gestionnaire is automatically affect to follow the dossier' do
expect { subject }.to change(Follow, :count).by(0) expect { subject }.to change(Follow, :count).by(1)
end
end
it 'Internal notification is not create' do
expect { subject }.to change(Notification, :count).by (0)
end
end
context 'when document is upload whith a commentaire', vcr: {cassette_name: 'controllers_backoffice_commentaires_controller_doc_upload_with_comment'} do
let(:document_upload) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') }
subject do
post :create, params: {dossier_id: dossier_id, email_commentaire: email_commentaire, texte_commentaire: texte_commentaire, piece_justificative: {content: document_upload}}
end
it 'create a new piece justificative' do
expect { subject }.to change(PieceJustificative, :count).by(1)
end
it 'clamav check the pj' do
expect(ClamavService).to receive(:safe_file?)
subject
end
it 'Internal notification is not create' do
expect { subject }.to change(Notification, :count).by (0)
end
describe 'piece justificative created' do
let(:pj) { PieceJustificative.last }
before do
subject
end end
it 'not have a type de pj' do context 'when gestionnaire already follow dossier' do
expect(pj.type_de_piece_justificative).to be_nil
end
it 'content not be nil' do
expect(pj.content).not_to be_nil
end
end
describe 'commentaire created' do
let(:commentaire) { Commentaire.last }
before do
subject
end
it 'have a piece justificative reference' do
expect(commentaire.piece_justificative).not_to be_nil
expect(commentaire.piece_justificative).to eq PieceJustificative.last
end
end
end
describe 'change dossier state after post a comment' do
context 'gestionnaire is connected' do
context 'when dossier is at state updated' do
before do before do
sign_in create(:gestionnaire) create :follow, gestionnaire_id: gestionnaire.id, dossier_id: dossier_id
dossier.updated!
post :create, params: {dossier_id: dossier_id, texte_commentaire: texte_commentaire}
dossier.reload
end end
subject { dossier.state } it 'gestionnaire is automatically affect to follow the dossier' do
expect { subject }.to change(Follow, :count).by(0)
end
end
it { is_expected.to eq('replied') } it 'Internal notification is not create' do
expect { subject }.to change(Notification, :count).by (0)
end
end
it 'Notification email is send' do context 'when document is upload whith a commentaire', vcr: {cassette_name: 'controllers_backoffice_commentaires_controller_doc_upload_with_comment'} do
expect(NotificationMailer).to receive(:new_answer).and_return(NotificationMailer) let(:document_upload) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') }
expect(NotificationMailer).to receive(:deliver_now!)
post :create, params: {dossier_id: dossier_id, texte_commentaire: texte_commentaire} subject do
post :create, params: {dossier_id: dossier_id, email_commentaire: email_commentaire, texte_commentaire: texte_commentaire, piece_justificative: {content: document_upload}}
end
it 'create a new piece justificative' do
expect { subject }.to change(PieceJustificative, :count).by(1)
end
it 'clamav check the pj' do
expect(ClamavService).to receive(:safe_file?)
subject
end
it 'Internal notification is created' do
expect { subject }.to change(Notification, :count).by (1)
end
describe 'piece justificative created' do
let(:pj) { PieceJustificative.last }
before do
subject
end
it 'not have a type de pj' do
expect(pj.type_de_piece_justificative).to be_nil
end
it 'content not be nil' do
expect(pj.content).not_to be_nil
end
end
describe 'commentaire created' do
let(:commentaire) { Commentaire.last }
before do
subject
end
it 'have a piece justificative reference' do
expect(commentaire.piece_justificative).not_to be_nil
expect(commentaire.piece_justificative).to eq PieceJustificative.last
end end
end end
end end
end
describe 'comment cannot be saved' do describe 'change dossier state after post a comment' do
before do context 'gestionnaire is connected' do
allow_any_instance_of(Commentaire).to receive(:save).and_return(false) context 'when dossier is at state updated' do
before do
sign_in gestionnaire
dossier.updated!
post :create, params: {dossier_id: dossier_id, texte_commentaire: texte_commentaire}
dossier.reload
end
subject { dossier.state }
it { is_expected.to eq('replied') }
it 'Notification email is send' do
expect(NotificationMailer).to receive(:new_answer).and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_now!)
post :create, params: {dossier_id: dossier_id, texte_commentaire: texte_commentaire}
end
end
end
end end
it 'Notification email is not sent' do
expect(NotificationMailer).not_to receive(:new_answer)
expect(NotificationMailer).not_to receive(:deliver_now!)
post :create, params: {dossier_id: dossier_id, texte_commentaire: texte_commentaire} describe 'comment cannot be saved' do
before do
allow_any_instance_of(Commentaire).to receive(:save).and_return(false)
end
it 'Notification email is not sent' do
expect(NotificationMailer).not_to receive(:new_answer)
expect(NotificationMailer).not_to receive(:deliver_now!)
post :create, params: {dossier_id: dossier_id, texte_commentaire: texte_commentaire}
end
end end
end end
end end

View file

@ -1,102 +1,154 @@
require 'spec_helper' require 'spec_helper'
describe InvitesController, type: :controller do describe InvitesController, type: :controller do
let(:dossier) { create(:dossier) } let(:dossier) { create(:dossier, :replied) }
let(:email) { 'plop@octo.com' } let(:email) { 'plop@octo.com' }
describe '#POST create' do describe '#POST create' do
let(:invite) { Invite.last } let(:invite) { Invite.last }
before do before do
sign_in create(:gestionnaire) sign_in signed_in_profile
end end
subject { post :create, params: {dossier_id: dossier.id, email: email} } subject { post :create, params: {dossier_id: dossier.id, email: email} }
it { expect { subject }.to change(InviteGestionnaire, :count).by(1) } context "when gestionnaire is signed_in" do
let(:signed_in_profile) { create(:gestionnaire) }
context 'when is a user who is loged' do shared_examples_for "he can not create invitation" do
before do it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
sign_in create(:user) it { expect { subject rescue nil }.to change(InviteGestionnaire, :count).by(0) }
end end
it { expect { subject }.to change(InviteGestionnaire, :count).by(1) } context 'when gestionnaire has no access to dossier' do
end it_behaves_like "he can not create invitation"
context 'when email is assign to an user' do
let! (:user) { create(:user, email: email) }
before do
subject
end end
describe 'Invite information' do context 'when gestionnaire is invited for avis on dossier' do
let(:email) { 'PLIP@octo.com' } before { Avis.create(gestionnaire: signed_in_profile, claimant: create(:gestionnaire), dossier: dossier) }
let(:invite) { Invite.last }
it 'email is on lower case' do it_behaves_like "he can not create invitation"
expect(invite.email).to eq 'plip@octo.com'
end
end end
it { expect(invite.user).to eq user } context 'when gestionnaire has access to dossier' do
it { expect(flash[:notice]).to be_present }
end
context 'when email is not assign to an user' do
before do
subject
end
it { expect(invite.user).to be_nil }
it { expect(flash[:notice]).to be_present }
end
describe 'not an email' do
context 'when email is not valid' do
let(:email) { 'plip.com' }
before do before do
subject signed_in_profile.procedures << dossier.procedure
end end
it { expect { subject }.not_to change(Invite, :count) } it { expect { subject }.to change(InviteGestionnaire, :count).by(1) }
it { expect(flash[:alert]).to be_present }
end
context 'when email is already used' do context 'when is a user who is loged' do
let!(:invite) { create(:invite, dossier: dossier) } before do
sign_in create(:user)
end
before do it { expect { subject }.to change(InviteGestionnaire, :count).by(1) }
subject
end end
it { expect { subject }.not_to change(Invite, :count) } context 'when email is assign to an user' do
it { expect(flash[:alert]).to be_present } let! (:user) { create(:user, email: email) }
before do
subject
end
describe 'Invite information' do
let(:email) { 'PLIP@octo.com' }
let(:invite) { Invite.last }
it 'email is on lower case' do
expect(invite.email).to eq 'plip@octo.com'
end
end
it { expect(invite.user).to eq user }
it { expect(flash[:notice]).to be_present }
end
context 'when email is not assign to an user' do
before do
subject
end
it { expect(invite.user).to be_nil }
it { expect(flash[:notice]).to be_present }
end
describe 'not an email' do
context 'when email is not valid' do
let(:email) { 'plip.com' }
before do
subject
end
it { expect { subject }.not_to change(Invite, :count) }
it { expect(flash[:alert]).to be_present }
end
context 'when email is already used' do
let!(:invite) { create(:invite, dossier: dossier) }
before do
subject
end
it { expect { subject }.not_to change(Invite, :count) }
it { expect(flash[:alert]).to be_present }
end
end
describe 'send invitation email' do
context 'when user does not exist' do
it 'send email' do
expect(InviteMailer).to receive(:invite_guest).and_return(InviteMailer)
expect(InviteMailer).to receive(:deliver_now!)
subject
end
end
context 'when user exist' do
before do
create :user, email: email
end
it 'send email' do
expect(InviteMailer).to receive(:invite_user).and_return(InviteMailer)
expect(InviteMailer).to receive(:deliver_now!)
subject
end
end
end
end end
end end
describe 'send invitation email' do context "when user is signed_in" do
context 'when user does not exist' do let(:signed_in_profile) { create(:user) }
it 'send email' do
expect(InviteMailer).to receive(:invite_guest).and_return(InviteMailer)
expect(InviteMailer).to receive(:deliver_now!)
subject shared_examples_for "he can not create a invite" do
end it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
it { expect { subject rescue nil }.to change(InviteUser, :count).by(0) }
end end
context 'when user exist' do context 'when user has no access to dossier' do
it_behaves_like "he can not create a invite"
end
context 'when user is invited on dossier' do
before { Invite.create(user: signed_in_profile, email: signed_in_profile.email, dossier: dossier) }
it_behaves_like "he can not create a invite"
end
context 'when user has access to dossier' do
before do before do
create :user, email: email dossier.update_attributes(user: signed_in_profile)
end end
it 'send email' do it { expect { subject }.to change(InviteUser, :count).by(1) }
expect(InviteMailer).to receive(:invite_user).and_return(InviteMailer)
expect(InviteMailer).to receive(:deliver_now!)
subject
end
end end
end end
end end

View file

@ -11,6 +11,28 @@ describe Users::CommentairesController, type: :controller do
end end
describe '#POST create' do describe '#POST create' do
context "when user has no access to dossier" do
before do
sign_in create(:user)
end
subject { post :create, params: { dossier_id: dossier_id, texte_commentaire: texte_commentaire } }
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
it { expect { subject rescue nil }.to change(Commentaire, :count).by(0) }
end
context "when user is invited on dossier" do
let(:user) { create(:user) }
subject { post :create, params: { dossier_id: dossier_id, texte_commentaire: texte_commentaire } }
before do
sign_in user
InviteUser.create(dossier: dossier, user: user, email: user.email, email_sender: "test@test.com")
end
it { expect{ subject }.to change(Commentaire, :count).by(1) }
end
context 'création correct d\'un commentaire' do context 'création correct d\'un commentaire' do
subject do subject do
sign_in dossier.user sign_in dossier.user