GroupServiceTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace Tests\Feature\Services;
  3. use App\Facades\Groups;
  4. use App\Models\Group;
  5. use App\Models\TwoFAccount;
  6. use App\Models\User;
  7. use Illuminate\Auth\Access\AuthorizationException;
  8. use Tests\FeatureTestCase;
  9. /**
  10. * @covers \App\Services\GroupService
  11. * @covers \App\Facades\Groups
  12. */
  13. class GroupServiceTest extends FeatureTestCase
  14. {
  15. /**
  16. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  17. */
  18. protected $user;
  19. /**
  20. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  21. */
  22. protected $otherUser;
  23. /**
  24. * App\Models\Group $groupOne, $groupTwo, $groupThree
  25. */
  26. protected $groupOne;
  27. protected $groupTwo;
  28. protected $groupThree;
  29. /**
  30. * App\Models\Group $twofaccountOne, $twofaccountTwo, $twofaccountThree
  31. */
  32. protected $twofaccountOne;
  33. protected $twofaccountTwo;
  34. protected $twofaccountThree;
  35. private const NEW_GROUP_NAME = 'MyNewGroup';
  36. /**
  37. * @test
  38. */
  39. public function setUp() : void
  40. {
  41. parent::setUp();
  42. $this->user = User::factory()->create();
  43. $this->otherUser = User::factory()->create();
  44. $this->groupOne = Group::factory()->for($this->user)->create();
  45. $this->groupTwo = Group::factory()->for($this->user)->create();
  46. $this->groupThree = Group::factory()->for($this->otherUser)->create();
  47. Group::factory()->count(2)->for($this->otherUser)->create();
  48. $this->twofaccountOne = TwoFAccount::factory()->for($this->user)->create([
  49. 'group_id' => $this->groupOne->id,
  50. ]);
  51. $this->twofaccountTwo = TwoFAccount::factory()->for($this->user)->create([
  52. 'group_id' => $this->groupTwo->id,
  53. ]);
  54. $this->twofaccountThree = TwoFAccount::factory()->for($this->otherUser)->create([
  55. 'group_id' => $this->groupThree->id,
  56. ]);
  57. }
  58. /**
  59. * @test
  60. */
  61. public function test_assign_a_twofaccount_to_a_group_persists_the_relation()
  62. {
  63. Groups::assign($this->twofaccountOne->id, $this->user, $this->groupTwo);
  64. $this->assertDatabaseHas('twofaccounts', [
  65. 'id' => $this->twofaccountOne->id,
  66. 'group_id' => $this->groupTwo->id,
  67. ]);
  68. }
  69. /**
  70. * @test
  71. */
  72. public function test_assign_multiple_twofaccounts_to_group_persists_the_relation()
  73. {
  74. Groups::assign([$this->twofaccountOne->id, $this->twofaccountTwo->id], $this->user, $this->groupTwo);
  75. $this->assertDatabaseHas('twofaccounts', [
  76. 'id' => $this->twofaccountOne->id,
  77. 'group_id' => $this->groupTwo->id,
  78. ]);
  79. $this->assertDatabaseHas('twofaccounts', [
  80. 'id' => $this->twofaccountTwo->id,
  81. 'group_id' => $this->groupTwo->id,
  82. ]);
  83. }
  84. /**
  85. * @test
  86. */
  87. public function test_assign_a_twofaccount_to_no_group_assigns_to_user_default_group()
  88. {
  89. $this->user['preferences->defaultGroup'] = $this->groupTwo->id;
  90. $this->user->save();
  91. Groups::assign($this->twofaccountOne->id, $this->user);
  92. $this->assertDatabaseHas('twofaccounts', [
  93. 'id' => $this->twofaccountOne->id,
  94. 'group_id' => $this->groupTwo->id,
  95. ]);
  96. }
  97. /**
  98. * @test
  99. */
  100. public function test_assign_a_twofaccount_to_no_group_assigns_to_user_active_group()
  101. {
  102. $this->user['preferences->defaultGroup'] = -1;
  103. $this->user['preferences->activeGroup'] = $this->groupTwo->id;
  104. $this->user->save();
  105. Groups::assign($this->twofaccountOne->id, $this->user);
  106. $this->assertDatabaseHas('twofaccounts', [
  107. 'id' => $this->twofaccountOne->id,
  108. 'group_id' => $this->groupTwo->id,
  109. ]);
  110. }
  111. /**
  112. * @test
  113. */
  114. public function test_assign_a_twofaccount_to_missing_active_group_returns_not_found()
  115. {
  116. $orginalGroup = $this->twofaccountOne->group_id;
  117. $this->user['preferences->defaultGroup'] = -1;
  118. $this->user['preferences->activeGroup'] = 1000;
  119. $this->user->save();
  120. Groups::assign($this->twofaccountOne->id, $this->user);
  121. $this->assertDatabaseHas('twofaccounts', [
  122. 'id' => $this->twofaccountOne->id,
  123. 'group_id' => $orginalGroup,
  124. ]);
  125. }
  126. /**
  127. * @test
  128. */
  129. public function test_user_can_assign_an_account()
  130. {
  131. $this->expectException(AuthorizationException::class);
  132. Groups::assign($this->twofaccountThree->id, $this->user, $this->user->groups()->first());
  133. }
  134. /**
  135. * @test
  136. */
  137. public function test_user_can_assign_multiple_accounts()
  138. {
  139. $this->expectException(AuthorizationException::class);
  140. Groups::assign([$this->twofaccountOne->id, $this->twofaccountThree->id], $this->user, $this->user->groups()->first());
  141. }
  142. /**
  143. * @test
  144. */
  145. public function test_prependTheAllGroup_add_the_group_on_top_of_groups()
  146. {
  147. $groups = Groups::prependTheAllGroup($this->user->groups, $this->user);
  148. $this->assertCount(3, $groups);
  149. $this->assertEquals(0, $groups->first()->id);
  150. $this->assertEquals(2, $groups->first()->twofaccounts_count);
  151. }
  152. }