Use User#owns_or_invite? instead of Dossier#owner_or_invite?

This commit is contained in:
gregoirenovel 2018-05-30 18:31:02 +02:00
parent 4e9565a5df
commit 0329b1db68
11 changed files with 58 additions and 96 deletions

View file

@ -148,7 +148,7 @@ module NewUser
end
def ensure_ownership_or_invitation!
if !dossier.owner_or_invite?(current_user)
if !current_user.owns_or_invite?(dossier)
forbidden!
end
end

View file

@ -10,7 +10,7 @@ class UsersController < ApplicationController
dossier = Dossier.find(dossier_id)
if !dossier.owner_or_invite?(current_user)
if !current_user.owns_or_invite?(dossier)
raise ActiveRecord::RecordNotFound
end

View file

@ -163,10 +163,6 @@ class Dossier < ApplicationRecord
en_instruction? || accepte? || refuse? || sans_suite?
end
def owner_or_invite?(user)
self.user == user || invite_for_user(user).present?
end
def invite_for_user(user)
invites_user.find_by(user_id: user.id)
end

View file

@ -45,4 +45,8 @@ class User < ApplicationRecord
def invite?(dossier_id)
invites.pluck(:dossier_id).include?(dossier_id.to_i)
end
def owns_or_invite?(dossier)
owns?(dossier) || invite?(dossier.id)
end
end

View file

@ -1,5 +1,5 @@
- if !@facade.dossier.read_only?
- if user_signed_in? && (@facade.dossier.owner_or_invite?(current_user))
- if user_signed_in? && (current_user.owns_or_invite?(@facade.dossier))
%a#maj_carte.action{ href: "/users/dossiers/#{@facade.dossier.id}/carte" }
.col-lg-2.col-md-2.col-sm-2.col-xs-2.action
= 'ÉDITER'

View file

@ -1,5 +1,5 @@
- if !@facade.dossier.read_only?
- if user_signed_in? && (@facade.dossier.owner_or_invite?(current_user))
- if user_signed_in? && (current_user.owns_or_invite?(@facade.dossier))
= link_to modifier_dossier_path(@facade.dossier), class: 'action', id: 'maj_infos' do
#edit-dossier.col-lg-2.col-md-2.col-sm-2.col-xs-2.action
= "ÉDITER"

View file

@ -1,5 +1,5 @@
- if !@facade.dossier.read_only?
- if user_signed_in? && (@facade.dossier.owner_or_invite?(current_user))
- if user_signed_in? && (current_user.owns_or_invite?(@facade.dossier))
- if @facade.procedure.cerfa_flag? || @facade.dossier.types_de_piece_justificative.size > 0
.col-lg-4.col-md-4.col-sm-4.col-xs-4.action
%a#maj_pj.action{ "data-target" => "#upload-pj-modal",

View file

@ -101,7 +101,7 @@
Pièce non fournie
- if !@facade.dossier.read_only?
- if user_signed_in? && (@facade.dossier.owner_or_invite?(current_user))
- if user_signed_in? && (current_user.owns_or_invite?(@facade.dossier))
- if @facade.procedure.cerfa_flag? || @facade.dossier.types_de_piece_justificative.size > 0
.row
.col-xs-4

View file

@ -397,44 +397,6 @@ describe Dossier do
end
end
describe '#owner_or_invite?' do
let(:owner) { create(:user) }
let(:dossier) { create(:dossier, user: owner) }
let(:invite_user) { create(:user) }
let(:invite_gestionnaire) { create(:user) }
before do
create(:invite, dossier: dossier, user: invite_user, type: 'InviteUser')
create(:invite, dossier: dossier, user: invite_gestionnaire, type: 'InviteGestionnaire')
end
subject { dossier.owner_or_invite?(user) }
context 'when user is owner' do
let(:user) { owner }
it { is_expected.to be_truthy }
end
context 'when user was invited by user' do
let(:user) { invite_user }
it { is_expected.to be_truthy }
end
context 'when user was invited by gestionnaire (legacy, no new invitations happen)' do
let(:user) { invite_gestionnaire }
it { is_expected.to be_falsey }
end
context 'when user is quidam' do
let(:user) { create(:user) }
it { is_expected.to be_falsey }
end
end
describe "#text_summary" do
let(:procedure) { create(:procedure, libelle: "Procédure", organisation: "Organisme") }

View file

@ -81,6 +81,37 @@ describe User, type: :model do
end
end
describe '#owns_or_invite?' do
let(:owner) { create(:user) }
let(:dossier) { create(:dossier, user: owner) }
let(:invite_user) { create(:user) }
let(:invite_gestionnaire) { create(:user) }
subject { user.owns_or_invite?(dossier) }
context 'when user is owner' do
let(:user) { owner }
it { is_expected.to be_truthy }
end
context 'when user was invited by user' do
before do
create(:invite, dossier: dossier, user: invite_user, type: 'InviteUser')
end
let(:user) { invite_user }
it { is_expected.to be_truthy }
end
context 'when user is quidam' do
let(:user) { create(:user) }
it { is_expected.to be_falsey }
end
end
context 'unified login' do
it 'syncs credentials to associated gestionnaire' do
user = create(:user)

View file

@ -72,61 +72,30 @@ describe 'users/recapitulatif/show.html.haml', type: :view do
end
context 'when invite is logged' do
context 'when invite is by Gestionnaire' do
let!(:invite_user) { create(:user, email: 'invite@octo.com') }
let!(:invite_user) { create(:user, email: 'invite@octo.com') }
before do
create(:invite) { create(:invite, email: invite_user.email, user: invite_user, dossier: dossier) }
sign_out dossier.user
sign_in invite_user
render
end
describe 'les liens de modifications' do
it 'describe link is not present' do
expect(rendered).not_to have_css('#maj_infos')
end
it 'map link is not present' do
expect(rendered).not_to have_css('#maj_carte')
end
it 'PJ link is not present' do
expect(rendered).not_to have_css('#maj_pj')
end
it 'archive link is not present' do
expect(rendered).not_to have_content('Archiver')
end
end
before do
create(:invite) { create(:invite, email: invite_user.email, user: invite_user, dossier: dossier, type: 'InviteUser') }
sign_out dossier.user
sign_in invite_user
render
end
context 'invite is by User' do
let!(:invite_user) { create(:user, email: 'invite@octo.com') }
before do
create(:invite) { create(:invite, email: invite_user.email, user: invite_user, dossier: dossier, type: 'InviteUser') }
sign_out dossier.user
sign_in invite_user
render
describe 'les liens de modifications' do
it 'describe link is not present' do
expect(rendered).to have_css('#maj_infos')
end
describe 'les liens de modifications' do
it 'describe link is not present' do
expect(rendered).to have_css('#maj_infos')
end
it 'map link is present' do
expect(rendered).to have_css('#maj_carte')
end
it 'map link is present' do
expect(rendered).to have_css('#maj_carte')
end
it 'PJ link is present' do
expect(rendered).to have_css('#maj_pj')
end
it 'PJ link is present' do
expect(rendered).to have_css('#maj_pj')
end
it 'archive link is present' do
expect(rendered).not_to have_content('Archiver')
end
it 'archive link is present' do
expect(rendered).not_to have_content('Archiver')
end
end
end