|
343 | 343 | end |
344 | 344 |
|
345 | 345 | 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) |
368 | 349 | end |
369 | 350 |
|
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 |
375 | 355 |
|
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 | + } |
378 | 363 | end |
379 | 364 |
|
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 |
388 | 381 | end |
389 | | - |
390 | | - it { is_expected.to include(org1plan1, org1plan2, org1plan3) } |
391 | | - it { is_expected.not_to include(org1plan4) } |
392 | 382 | end |
393 | 383 |
|
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) } |
401 | 385 |
|
402 | | - it { |
403 | | - is_expected.to include(org1plan1, org1plan2, org1plan3, org1plan4, @plan) |
404 | | - } |
405 | | - end |
| 386 | + subject { org.org_admin_plans } |
406 | 387 |
|
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| |
408 | 389 | 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 |
415 | 391 | end |
416 | 392 |
|
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 |
418 | 397 | end |
419 | 398 |
|
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 } |
428 | 403 | end |
429 | | - |
430 | | - it { is_expected.not_to include(@plan) } |
431 | 404 | end |
432 | 405 |
|
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 |
441 | 411 | end |
442 | 412 |
|
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 |
444 | 417 | end |
445 | 418 |
|
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]] } |
447 | 422 | 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) |
452 | 424 | end |
453 | 425 |
|
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 |
455 | 430 | end |
456 | 431 |
|
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]] } |
458 | 435 | 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) |
463 | 437 | end |
464 | 438 |
|
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 |
466 | 443 | end |
467 | 444 | end |
468 | 445 |
|
|
0 commit comments