|
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 | 350 |
|
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) } |
352 | 356 |
|
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) } |
358 | 359 |
|
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) } |
360 | 365 | end |
361 | 366 |
|
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 |
363 | 368 | 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 } |
365 | 370 | end |
366 | 371 |
|
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) } |
370 | 374 | end |
371 | 375 |
|
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 |
373 | 377 | 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 } |
375 | 382 | end |
376 | 383 |
|
377 | | - it { is_expected.to include(plan) } |
| 384 | + it { is_expected.to include(org1plan1, org1plan2, org1plan3) } |
| 385 | + it { is_expected.not_to include(org1plan4) } |
378 | 386 | end |
379 | 387 |
|
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 |
381 | 389 | 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) |
383 | 393 | end |
384 | 394 |
|
385 | | - it { is_expected.to include(plan) } |
| 395 | + it { |
| 396 | + is_expected.to include(org1plan1, org1plan2, org1plan3, org1plan4, @plan) |
| 397 | + } |
386 | 398 | end |
387 | 399 |
|
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 |
389 | 401 | 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) |
391 | 407 | end |
392 | 408 |
|
393 | | - it { is_expected.to include(plan) } |
| 409 | + it { is_expected.not_to include(@plan) } |
394 | 410 | end |
395 | 411 |
|
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 |
397 | 413 | 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) |
404 | 419 | end |
405 | 420 |
|
406 | | - it { is_expected.not_to include(plan) } |
| 421 | + it { is_expected.not_to include(@plan) } |
407 | 422 | end |
408 | 423 |
|
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 |
410 | 425 | 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 |
421 | 461 | end |
422 | 462 |
|
423 | 463 | context '#grant_api!' do |
|
0 commit comments