Skip to content

Commit 58be1ff

Browse files
committed
Rework org_admin_plans spec
Redesign org_admin_plans tests for clarity and maintainability: - Introduce helper methods for plan creation and role deactivation. - Organize plans into native vs affiliated hashes for clearer context. - Use shared examples to consolidate repetitive expectations.
1 parent 983b86a commit 58be1ff

File tree

1 file changed

+67
-90
lines changed

1 file changed

+67
-90
lines changed

spec/models/org_spec.rb

Lines changed: 67 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -343,126 +343,103 @@
343343
end
344344

345345
describe '#org_admin_plans' do
346-
Rails.configuration.x.plans.org_admins_read_all = true
347-
# Two Orgs
348-
let!(:org1) { create(:org) }
349-
let!(:org2) { create(:org) }
350-
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) }
356-
357-
# Plans for org2
358-
let!(:org2plan1) { create(:plan, :creator, :organisationally_visible, org: org2) }
359-
360-
subject { org1.org_admin_plans }
361-
362-
context 'when user belongs to Org and plan owner with role :creator' do
363-
before do
364-
Rails.configuration.x.plans.org_admins_read_all = true
365-
end
366-
it { is_expected.to include(org1plan1, org1plan2, org1plan3, org1plan4) }
367-
it { is_expected.not_to include(org2plan1) }
346+
# Helper for returning all plans from the `plans` hash
347+
def all_plans
348+
plans.values.flat_map(&:values)
368349
end
369350

370-
context 'when user belongs to Org and a plan removed by creator assuming there are no coowners' do
371-
before do
372-
Rails.configuration.x.plans.org_admins_read_all = true
373-
org1plan4.roles.map { |r| r.update(active: false) if r.user_id == org1plan4.owner.id }
374-
end
351+
# Helper for deactivating roles (creator or administrator) from plans
352+
def deactivate_roles_for_plans(plans, role_condition)
353+
Role.where(plan_id: plans.map(&:id)).where(role_condition).update_all(active: false)
354+
end
375355

376-
it { is_expected.to include(org1plan1, org1plan2, org1plan3) }
377-
it { is_expected.not_to include(org1plan4) }
356+
def create_plans_for(org)
357+
{
358+
public: create(:plan, :creator, :publicly_visible, org: org),
359+
org: create(:plan, :creator, :organisationally_visible, org: org),
360+
private: create(:plan, :creator, :privately_visible, org: org),
361+
test: create(:plan, :creator, :is_test, org: org)
362+
}
378363
end
379364

380-
context 'when user belongs to Org and a plan removed by creator, but coowner still active.' do
381-
before do
382-
Rails.configuration.x.plans.org_admins_read_all = true
383-
coowner = create(:user, org: org1)
384-
# Add coowner to the plan by giving user the role administrator
385-
org1plan4.add_user!(coowner.id, :administrator)
386-
owner_id = org1plan4.owner.id
387-
org1plan4.roles.map { |r| r.update(active: false) if r.user_id == owner_id }
365+
let!(:org) { create(:org) }
366+
let!(:org_user) { create(:user, org: org) }
367+
let!(:other_org) { create(:org) }
368+
369+
# org_admin_plans consists of "native" and "affiliated" plans
370+
# - native plans have plan.org == org
371+
# - affiliated plans have an active administrator role for a user where user.org == org
372+
let!(:plans) do
373+
{
374+
native: create_plans_for(org),
375+
affiliated: create_plans_for(other_org)
376+
}.tap do |hash|
377+
# Add the required administrator role for the `affiliated` plans
378+
hash[:affiliated].each_value do |plan|
379+
plan.add_user!(org_user.id, :administrator)
380+
end
388381
end
389-
390-
it { is_expected.to include(org1plan1, org1plan2, org1plan3) }
391-
it { is_expected.not_to include(org1plan4) }
392382
end
393383

394-
context 'when user belongs to Org, plan user with role :administrator, but plan creator from a different Org' do
395-
before do
396-
Rails.configuration.x.plans.org_admins_read_all = true
397-
# Creator belongs to different org
398-
@plan = create(:plan, :creator, :organisationally_visible, org: create(:org))
399-
@plan.add_user!(create(:user, org: org1).id, :administrator)
400-
end
384+
let!(:other_org_plan) { create(:plan, :creator, :publicly_visible, org: other_org) }
401385

402-
it {
403-
is_expected.to include(org1plan1, org1plan2, org1plan3, org1plan4, @plan)
404-
}
405-
end
386+
subject { org.org_admin_plans }
406387

407-
context 'user belongs to Org and plan user with role :editor, but not :creator and :admin' do
388+
shared_examples 'org_admin_plans expectations' do |org_admins_read_all: true|
408389
before do
409-
Rails.configuration.x.plans.org_admins_read_all = true
410-
# Creator and admin belongs to different orgs
411-
@plan = create(:plan, :creator, :organisationally_visible, org: create(:org))
412-
@plan.add_user!(create(:org).id, :administrator)
413-
# Editor belongs to org1
414-
@plan.add_user!(create(:user, org: org1).id, :editor)
390+
Rails.configuration.x.plans.org_admins_read_all = org_admins_read_all
415391
end
416392

417-
it { is_expected.not_to include(@plan) }
393+
it 'includes/excludes the expected plans' do
394+
expect(subject).to include(*Array(included))
395+
expect(subject).not_to include(*Array(excluded))
396+
end
418397
end
419398

420-
context 'user belongs to Org and plan user with role :commenter, but not :creator and :admin' do
421-
before do
422-
Rails.configuration.x.plans.org_admins_read_all = true
423-
# Creator and admin belongs to different orgs
424-
@plan = create(:plan, :creator, :organisationally_visible, org: create(:org))
425-
@plan.add_user!(create(:org).id, :administrator)
426-
# Commenter belongs to org1
427-
@plan.add_user!(create(:user, org: org1).id, :commentor)
399+
context 'default context with org_admins_read_all = true' do
400+
include_examples 'org_admin_plans expectations' do
401+
let(:included) { all_plans }
402+
let(:excluded) { other_org_plan }
428403
end
429-
430-
it { is_expected.not_to include(@plan) }
431404
end
432405

433-
context 'user belongs to Org and plan user with role :reviewer, but not :creator and :admin' do
434-
before do
435-
Rails.configuration.x.plans.org_admins_read_all = true
436-
# Creator and admin belongs to different orgs
437-
@plan = create(:plan, :creator, :organisationally_visible, org: create(:org))
438-
@plan.add_user!(create(:org).id, :administrator)
439-
# Reviewer belongs to org1
440-
@plan.add_user!(create(:user, org: org1).id, :reviewer)
406+
context 'default context with org_admins_read_all = false' do
407+
let(:private_and_test_plans) do
408+
plans.fetch_values(:native, :affiliated).flat_map do |h|
409+
h.values_at(:private, :test)
410+
end
441411
end
442412

443-
it { is_expected.not_to include(@plan) }
413+
include_examples 'org_admin_plans expectations', org_admins_read_all: false do
414+
let(:included) { (all_plans - private_and_test_plans).flatten }
415+
let(:excluded) { private_and_test_plans + [other_org_plan] }
416+
end
444417
end
445418

446-
context 'read_all is false, visibility private and user org_admin' do
419+
context 'creator role is deactivated for some native and affiliated plans' do
420+
# Deactivate the creator role for both a native and an affiliated plan
421+
let(:plans_to_deactivate) { [plans[:native][:public], plans[:affiliated][:public]] }
447422
before do
448-
Rails.configuration.x.plans.org_admins_read_all = false
449-
@user = create(:user, :org_admin, org: org1)
450-
@plan = create(:plan, :creator, :privately_visible, org: org1)
451-
@plan.add_user!(@user.id, :reviewer)
423+
deactivate_roles_for_plans(plans_to_deactivate, Role.creator_condition)
452424
end
453425

454-
it { is_expected.not_to include(@plan) }
426+
include_examples 'org_admin_plans expectations' do
427+
let(:included) { (all_plans - plans_to_deactivate).flatten }
428+
let(:excluded) { plans_to_deactivate + [other_org_plan] }
429+
end
455430
end
456431

457-
context 'read_all is false, visibility public and user org_admin' do
432+
context 'administrator role is deactivated for some affiliated plans' do
433+
# Deactivate the administrator role for some affiliated plans
434+
let(:plans_to_deactivate) { [plans[:affiliated][:public], plans[:affiliated][:org]] }
458435
before do
459-
Rails.configuration.x.plans.org_admins_read_all = false
460-
@user = create(:user, :org_admin, org: org1)
461-
@plan = create(:plan, :creator, :publicly_visible, org: org1)
462-
@plan.add_user!(@user.id, :reviewer)
436+
deactivate_roles_for_plans(plans_to_deactivate, Role.administrator_condition)
463437
end
464438

465-
it { is_expected.to include(@plan) }
439+
include_examples 'org_admin_plans expectations' do
440+
let(:included) { (all_plans - plans_to_deactivate).flatten }
441+
let(:excluded) { plans_to_deactivate + [other_org_plan] }
442+
end
466443
end
467444
end
468445

0 commit comments

Comments
 (0)