|
344 | 344 |
|
345 | 345 | describe '#org_admin_plans' do |
346 | 346 | 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) } |
| 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) } |
350 | 356 |
|
351 | | - subject { org.org_admin_plans } |
| 357 | + # Plans for org2 |
| 358 | + let!(:org2plan1) { create(:plan, :creator, :organisationally_visible, org: org2) } |
| 359 | + |
| 360 | + subject { org1.org_admin_plans } |
352 | 361 |
|
353 | 362 | context 'when user belongs to Org and plan owner with role :creator' do |
354 | 363 | before do |
355 | | - create(:role, :creator, user: user, plan: plan) |
356 | | - plan.add_user!(user.id, :creator) |
| 364 | + Rails.configuration.x.plans.org_admins_read_all = true |
357 | 365 | end |
| 366 | + it { is_expected.to include(org1plan1, org1plan2, org1plan3, org1plan4) } |
| 367 | + it { is_expected.not_to include(org2plan1) } |
| 368 | + end |
358 | 369 |
|
359 | | - it { is_expected.to include(plan) } |
| 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 |
| 375 | + |
| 376 | + it { is_expected.to include(org1plan1, org1plan2, org1plan3) } |
| 377 | + it { is_expected.not_to include(org1plan4) } |
360 | 378 | end |
361 | 379 |
|
362 | | - context 'when user belongs to Org and plan user with role :administrator' do |
| 380 | + context 'when user belongs to Org and a plan removed by creator, but cowner still active.' do |
363 | 381 | before do |
364 | | - plan.add_user!(user.id, :administrator) |
| 382 | + Rails.configuration.x.plans.org_admins_read_all = true |
| 383 | + coowner = create(:user, org: org1) |
| 384 | + org1plan4.add_user!(coowner.id, :coowner) |
| 385 | + owner_id = org1plan4.owner.id |
| 386 | + org1plan4.roles.map { |r| r.update(active: false) if r.user_id == owner_id } |
| 387 | + end |
| 388 | + |
| 389 | + it { is_expected.to include(org1plan1, org1plan2, org1plan3) } |
| 390 | + it { is_expected.not_to include(org1plan4) } |
| 391 | + end |
| 392 | + |
| 393 | + context 'when user belongs to Org, plan user with role :administrator, but plan creator from a different Org' do |
| 394 | + before do |
| 395 | + Rails.configuration.x.plans.org_admins_read_all = true |
| 396 | + # Creator belongs to different org |
| 397 | + @plan = create(:plan, :creator, :organisationally_visible, org: create(:org)) |
| 398 | + @plan.add_user!(create(:user, org: org1).id, :administrator) |
365 | 399 | end |
366 | 400 |
|
367 | 401 | it { |
368 | | - is_expected.to include(plan) |
| 402 | + is_expected.to include(org1plan1, org1plan2, org1plan3, org1plan4, @plan) |
369 | 403 | } |
370 | 404 | end |
371 | 405 |
|
372 | 406 | context 'user belongs to Org and plan user with role :editor, but not :creator and :admin' do |
373 | 407 | before do |
374 | | - plan.add_user!(user.id, :editor) |
| 408 | + Rails.configuration.x.plans.org_admins_read_all = true |
| 409 | + # Creator and admin belongs to different orgs |
| 410 | + @plan = create(:plan, :creator, :organisationally_visible, org: create(:org)) |
| 411 | + @plan.add_user!(create(:org).id, :administrator) |
| 412 | + # Editor belongs to org1 |
| 413 | + @plan.add_user!(create(:user, org: org1).id, :editor) |
375 | 414 | end |
376 | 415 |
|
377 | | - it { is_expected.to include(plan) } |
| 416 | + it { is_expected.not_to include(@plan) } |
378 | 417 | end |
379 | 418 |
|
380 | 419 | context 'user belongs to Org and plan user with role :commenter, but not :creator and :admin' do |
381 | 420 | before do |
382 | | - plan.add_user!(user.id, :commenter) |
| 421 | + Rails.configuration.x.plans.org_admins_read_all = true |
| 422 | + # Creator and admin belongs to different orgs |
| 423 | + @plan = create(:plan, :creator, :organisationally_visible, org: create(:org)) |
| 424 | + @plan.add_user!(create(:org).id, :administrator) |
| 425 | + # Commenter belongs to org1 |
| 426 | + @plan.add_user!(create(:user, org: org1).id, :commentor) |
383 | 427 | end |
384 | 428 |
|
385 | | - it { is_expected.to include(plan) } |
| 429 | + it { is_expected.not_to include(@plan) } |
386 | 430 | end |
387 | 431 |
|
388 | 432 | context 'user belongs to Org and plan user with role :reviewer, but not :creator and :admin' do |
389 | 433 | before do |
390 | | - plan.add_user!(user.id, :reviewer) |
| 434 | + Rails.configuration.x.plans.org_admins_read_all = true |
| 435 | + # Creator and admin belongs to different orgs |
| 436 | + @plan = create(:plan, :creator, :organisationally_visible, org: create(:org)) |
| 437 | + @plan.add_user!(create(:org).id, :administrator) |
| 438 | + # Reviewer belongs to org1 |
| 439 | + @plan.add_user!(create(:user, org: org1).id, :reviewer) |
391 | 440 | end |
392 | 441 |
|
393 | | - it { is_expected.to include(plan) } |
| 442 | + it { is_expected.not_to include(@plan) } |
394 | 443 | end |
395 | 444 |
|
396 | 445 | context 'read_all is false, visibility private and user org_admin' do |
397 | 446 | before do |
398 | 447 | 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! |
| 448 | + @user = create(:user, :org_admin, org: org1) |
| 449 | + @plan = create(:plan, :creator, :privately_visible, org: org1) |
| 450 | + @plan.add_user!(@user.id, :reviewer) |
404 | 451 | end |
405 | 452 |
|
406 | | - it { is_expected.not_to include(plan) } |
| 453 | + it { is_expected.not_to include(@plan) } |
407 | 454 | end |
408 | 455 |
|
409 | 456 | context 'read_all is false, visibility public and user org_admin' do |
410 | 457 | before do |
411 | 458 | 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! |
| 459 | + @user = create(:user, :org_admin, org: org1) |
| 460 | + @plan = create(:plan, :creator, :publicly_visible, org: org1) |
| 461 | + @plan.add_user!(@user.id, :reviewer) |
417 | 462 | end |
418 | 463 |
|
419 | | - it { is_expected.to include(plan) } |
| 464 | + it { is_expected.to include(@plan) } |
420 | 465 | end |
421 | 466 | end |
422 | 467 |
|
|
0 commit comments