Skip to content

Commit 958f445

Browse files
author
John Pinto
committed
Issue #3561 - Fix for bug "Discrepancy in Org Admin view of the
Organisation Plans and the related CSV Download". Changes: - where(Role.creator_condition) condition added to Plans retrieved in the Org model's org_admin_plans method. This ensures that Plans returned are only active plans. Note: if an owner or co-owner de-activates a plan the Role of the user is set to active: false, so any plan with creator with role (having active: false) would mean the Plan is removed. - To avoid duplication we removed .where(Role.creator_condition) in org_admin method in app/controllers/paginable/plans_controller.rb from line plans = plans.joins(:template, roles: [user::org]).where(Role.creator_condition). - Updated Rspect tests for Org method org_admin_plans() in spec/models/org_spec.rb.
1 parent 23eb8a8 commit 958f445

File tree

2 files changed

+98
-43
lines changed

2 files changed

+98
-43
lines changed

spec/factories/plans.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,26 @@
6666
obj.roles << create(:role, :creator, user: create(:user, org: create(:org)))
6767
end
6868
end
69+
trait :administrator do
70+
after(:create) do |obj|
71+
obj.roles << create(:role, :administrator, user: create(:user, org: create(:org)))
72+
end
73+
end
74+
trait :editor do
75+
after(:create) do |obj|
76+
obj.roles << create(:role, :editor, user: create(:user, org: create(:org)))
77+
end
78+
end
6979
trait :commenter do
7080
after(:create) do |obj|
7181
obj.roles << create(:role, :commenter, user: create(:user, org: create(:org)))
7282
end
7383
end
84+
trait :reviewer do
85+
after(:create) do |obj|
86+
obj.roles << create(:role, :reviewer, user: create(:user, org: create(:org)))
87+
end
88+
end
7489
trait :organisationally_visible do
7590
after(:create) do |plan|
7691
plan.update(visibility: Plan.visibilities[:organisationally_visible])

spec/models/org_spec.rb

Lines changed: 83 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -344,80 +344,120 @@
344344

345345
describe '#org_admin_plans' do
346346
Rails.configuration.x.plans.org_admins_read_all = true
347-
let!(:org) { create(:org) }
348-
let!(:plan) { create(:plan, org: org, visibility: 'publicly_visible') }
349-
let!(:user) { create(:user, org: org) }
347+
# Two Orgs
348+
let!(:org1) { create(:org) }
349+
let!(:org2) { create(:org) }
350350

351-
subject { org.org_admin_plans }
351+
# Plans for org1
352+
let!(:org1plan1) { create(:plan, :creator, :organisationally_visible, org: org1) }
353+
let!(:org1plan2) { create(:plan, :creator, :privately_visible, org: org1) }
354+
let!(:org1plan3) { create(:plan, :creator, :publicly_visible, org: org1) }
355+
let!(:org1plan4) { create(:plan, :creator, :organisationally_visible, org: org1) }
352356

353-
context 'when user belongs to Org and plan owner with role :creator' do
354-
before do
355-
create(:role, :creator, user: user, plan: plan)
356-
plan.add_user!(user.id, :creator)
357-
end
357+
# Plans for org2
358+
let!(:org2plan1) { create(:plan, :creator, :organisationally_visible, org: org2) }
358359

359-
it { is_expected.to include(plan) }
360+
subject { org1.org_admin_plans }
361+
362+
context 'when user belongs to Org and plan owner with role :creator' do
363+
it { is_expected.to include(org1plan1, org1plan2, org1plan3,org1plan4) }
364+
it { is_expected.not_to include(org2plan1) }
360365
end
361366

362-
context 'when user belongs to Org and plan user with role :administrator' do
367+
context 'when user belongs to Org and a plan removed by creator assuming there are no coowners' do
363368
before do
364-
plan.add_user!(user.id, :administrator)
369+
org1plan4.roles.map { |r| r.update(active: false) if r.user_id == org1plan4.owner.id }
365370
end
366371

367-
it {
368-
is_expected.to include(plan)
369-
}
372+
it { is_expected.to include(org1plan1, org1plan2, org1plan3) }
373+
it { is_expected.not_to include(org1plan4) }
370374
end
371375

372-
context 'user belongs to Org and plan user with role :editor, but not :creator and :admin' do
376+
context 'when user belongs to Org and a plan removed by creator, but cowner still active.' do
373377
before do
374-
plan.add_user!(user.id, :editor)
378+
coowner = create(:user, org: org1)
379+
org1plan4.add_user!(coowner.id, :coowner)
380+
owner_id = org1plan4.owner.id
381+
org1plan4.roles.map { |r| r.update(active: false) if r.user_id == owner_id }
375382
end
376383

377-
it { is_expected.to include(plan) }
384+
it { is_expected.to include(org1plan1, org1plan2, org1plan3) }
385+
it { is_expected.not_to include(org1plan4) }
378386
end
379387

380-
context 'user belongs to Org and plan user with role :commenter, but not :creator and :admin' do
388+
context 'when user belongs to Org, plan user with role :administrator, but plan creator from a different Org' do
381389
before do
382-
plan.add_user!(user.id, :commenter)
390+
# Creator belongs to different org
391+
@plan = create(:plan, :creator, :organisationally_visible, org: create(:org))
392+
@plan.add_user!(create(:user, org: org1).id, :administrator)
383393
end
384394

385-
it { is_expected.to include(plan) }
395+
it {
396+
is_expected.to include(org1plan1, org1plan2, org1plan3, org1plan4, @plan)
397+
}
386398
end
387399

388-
context 'user belongs to Org and plan user with role :reviewer, but not :creator and :admin' do
400+
context 'user belongs to Org and plan user with role :editor, but not :creator and :admin' do
389401
before do
390-
plan.add_user!(user.id, :reviewer)
402+
# Creator and admin belongs to different orgs
403+
@plan = create(:plan, :creator, :organisationally_visible, org: create(:org))
404+
@plan.add_user!(create(:org).id, :administrator)
405+
# Editor belongs to org1
406+
@plan.add_user!(create(:user, org: org1).id, :editor)
391407
end
392408

393-
it { is_expected.to include(plan) }
409+
it { is_expected.not_to include(@plan) }
394410
end
395411

396-
context 'read_all is false, visibility private and user org_admin' do
412+
context 'user belongs to Org and plan user with role :commenter, but not :creator and :admin' do
397413
before do
398-
Rails.configuration.x.plans.org_admins_read_all = false
399-
@perm = build(:perm)
400-
@perm.name = 'grant_permissions'
401-
user.perms << @perm
402-
plan.add_user!(user.id, :reviewer)
403-
plan.privately_visible!
414+
# Creator and admin belongs to different orgs
415+
@plan = create(:plan, :creator, :organisationally_visible, org: create(:org))
416+
@plan.add_user!(create(:org).id, :administrator)
417+
# Commenter belongs to org1
418+
@plan.add_user!(create(:user, org: org1).id, :commentor)
404419
end
405420

406-
it { is_expected.not_to include(plan) }
421+
it { is_expected.not_to include(@plan) }
407422
end
408423

409-
context 'read_all is false, visibility public and user org_admin' do
424+
context 'user belongs to Org and plan user with role :reviewer, but not :creator and :admin' do
410425
before do
411-
Rails.configuration.x.plans.org_admins_read_all = false
412-
@perm = build(:perm)
413-
@perm.name = 'grant_permissions'
414-
user.perms << @perm
415-
plan.add_user!(user.id, :reviewer)
416-
plan.publicly_visible!
417-
end
418-
419-
it { is_expected.to include(plan) }
420-
end
426+
# Creator and admin belongs to different orgs
427+
@plan = create(:plan, :creator, :organisationally_visible, org: create(:org))
428+
@plan.add_user!(create(:org).id, :administrator)
429+
# Reviewer belongs to org1
430+
@plan.add_user!(create(:user, org: org1).id, :reviewer)
431+
end
432+
433+
it { is_expected.not_to include(@plan) }
434+
end
435+
436+
# context 'read_all is false, visibility private and user org_admin' do
437+
# before do
438+
# Rails.configuration.x.plans.org_admins_read_all = false
439+
# @perm = build(:perm)
440+
# @perm.name = 'grant_permissions'
441+
# user.perms << @perm
442+
# plan.add_user!(user.id, :reviewer)
443+
# plan.privately_visible!
444+
# end
445+
446+
# it { is_expected.not_to include(plan) }
447+
# end
448+
449+
# context 'read_all is false, visibility public and user org_admin' do
450+
# before do
451+
# Rails.configuration.x.plans.org_admins_read_all = false
452+
# @perm = build(:perm)
453+
# @perm.name = 'grant_permissions'
454+
# user.perms << @perm
455+
# plan.add_user!(user.id, :reviewer)
456+
# plan.publicly_visible!
457+
# end
458+
459+
# it { is_expected.to include(plan) }
460+
# end
421461
end
422462

423463
context '#grant_api!' do

0 commit comments

Comments
 (0)