GroupServiceTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace Tests\Feature\Services;
  3. use App\Models\Group;
  4. use App\Models\TwoFAccount;
  5. use Tests\FeatureTestCase;
  6. use App\Facades\Groups;
  7. use App\Facades\Settings;
  8. /**
  9. * @covers \App\Services\GroupService
  10. */
  11. class GroupServiceTest extends FeatureTestCase
  12. {
  13. /**
  14. * App\Models\Group $groupOne, $groupTwo
  15. */
  16. protected $groupOne, $groupTwo;
  17. /**
  18. * App\Models\Group $twofaccountOne, $twofaccountTwo
  19. */
  20. protected $twofaccountOne, $twofaccountTwo;
  21. private const NEW_GROUP_NAME = 'MyNewGroup';
  22. private const TWOFACCOUNT_COUNT = 2;
  23. private const ACCOUNT = 'account';
  24. private const SERVICE = 'service';
  25. private const SECRET = 'A4GRFHVVRBGY7UIW';
  26. private const ALGORITHM_CUSTOM = 'sha256';
  27. private const DIGITS_CUSTOM = 7;
  28. private const PERIOD_CUSTOM = 40;
  29. private const IMAGE = 'https%3A%2F%2Fen.opensuse.org%2Fimages%2F4%2F44%2FButton-filled-colour.png';
  30. private const ICON = 'test.png';
  31. private const TOTP_FULL_CUSTOM_URI = 'otpauth://totp/'.self::SERVICE.':'.self::ACCOUNT.'?secret='.self::SECRET.'&issuer='.self::SERVICE.'&digits='.self::DIGITS_CUSTOM.'&period='.self::PERIOD_CUSTOM.'&algorithm='.self::ALGORITHM_CUSTOM.'&image='.self::IMAGE;
  32. /**
  33. * @test
  34. */
  35. public function setUp() : void
  36. {
  37. parent::setUp();
  38. $this->groupOne = new Group;
  39. $this->groupOne->name = 'MyGroupOne';
  40. $this->groupOne->save();
  41. $this->groupTwo = new Group;
  42. $this->groupTwo->name = 'MyGroupTwo';
  43. $this->groupTwo->save();
  44. $this->twofaccountOne = new TwoFAccount;
  45. $this->twofaccountOne->legacy_uri = self::TOTP_FULL_CUSTOM_URI;
  46. $this->twofaccountOne->service = self::SERVICE;
  47. $this->twofaccountOne->account = self::ACCOUNT;
  48. $this->twofaccountOne->icon = self::ICON;
  49. $this->twofaccountOne->otp_type = 'totp';
  50. $this->twofaccountOne->secret = self::SECRET;
  51. $this->twofaccountOne->digits = self::DIGITS_CUSTOM;
  52. $this->twofaccountOne->algorithm = self::ALGORITHM_CUSTOM;
  53. $this->twofaccountOne->period = self::PERIOD_CUSTOM;
  54. $this->twofaccountOne->counter = null;
  55. $this->twofaccountOne->save();
  56. $this->twofaccountTwo = new TwoFAccount;
  57. $this->twofaccountTwo->legacy_uri = self::TOTP_FULL_CUSTOM_URI;
  58. $this->twofaccountTwo->service = self::SERVICE;
  59. $this->twofaccountTwo->account = self::ACCOUNT;
  60. $this->twofaccountTwo->icon = self::ICON;
  61. $this->twofaccountTwo->otp_type = 'totp';
  62. $this->twofaccountTwo->secret = self::SECRET;
  63. $this->twofaccountTwo->digits = self::DIGITS_CUSTOM;
  64. $this->twofaccountTwo->algorithm = self::ALGORITHM_CUSTOM;
  65. $this->twofaccountTwo->period = self::PERIOD_CUSTOM;
  66. $this->twofaccountTwo->counter = null;
  67. $this->twofaccountTwo->save();
  68. }
  69. /**
  70. * @test
  71. */
  72. public function test_getAll_returns_a_collection()
  73. {
  74. $this->assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, Groups::getAll());
  75. }
  76. /**
  77. * @test
  78. */
  79. public function test_getAll_adds_pseudo_group_on_top_of_user_groups()
  80. {
  81. $groups = Groups::getAll();
  82. $this->assertEquals(0, $groups->first()->id);
  83. $this->assertEquals(__('commons.all'), $groups->first()->name);
  84. }
  85. /**
  86. * @test
  87. */
  88. public function test_getAll_returns_pseudo_group_with_all_twofaccounts_count()
  89. {
  90. $groups = Groups::getAll();
  91. $this->assertEquals(self::TWOFACCOUNT_COUNT, $groups->first()->twofaccounts_count);
  92. }
  93. /**
  94. * @test
  95. */
  96. public function test_create_persists_and_returns_created_group()
  97. {
  98. $newGroup = Groups::create(['name' => self::NEW_GROUP_NAME]);
  99. $this->assertDatabaseHas('groups', ['name' => self::NEW_GROUP_NAME]);
  100. $this->assertInstanceOf(\App\Models\Group::class, $newGroup);
  101. $this->assertEquals(self::NEW_GROUP_NAME, $newGroup->name);
  102. }
  103. /**
  104. * @test
  105. */
  106. public function test_update_persists_and_returns_updated_group()
  107. {
  108. $this->groupOne = Groups::update($this->groupOne, ['name' => self::NEW_GROUP_NAME]);
  109. $this->assertDatabaseHas('groups', ['name' => self::NEW_GROUP_NAME]);
  110. $this->assertInstanceOf(\App\Models\Group::class, $this->groupOne);
  111. $this->assertEquals(self::NEW_GROUP_NAME, $this->groupOne->name);
  112. }
  113. /**
  114. * @test
  115. */
  116. public function test_delete_a_groupId_clear_db_and_returns_deleted_count()
  117. {
  118. $deleted = Groups::delete($this->groupOne->id);
  119. $this->assertDatabaseMissing('groups', ['id' => $this->groupOne->id]);
  120. $this->assertEquals(1, $deleted);
  121. }
  122. /**
  123. * @test
  124. */
  125. public function test_delete_an_array_of_ids_clear_db_and_returns_deleted_count()
  126. {
  127. $deleted = Groups::delete([$this->groupOne->id, $this->groupTwo->id]);
  128. $this->assertDatabaseMissing('groups', ['id' => $this->groupOne->id]);
  129. $this->assertDatabaseMissing('groups', ['id' => $this->groupTwo->id]);
  130. $this->assertEquals(2, $deleted);
  131. }
  132. /**
  133. * @test
  134. */
  135. public function test_delete_default_group_reset_defaultGroup_setting()
  136. {
  137. Settings::set('defaultGroup', $this->groupOne->id);
  138. $deleted = Groups::delete($this->groupOne->id);
  139. $this->assertDatabaseHas('options', [
  140. 'key' => 'defaultGroup',
  141. 'value' => 0
  142. ]);
  143. }
  144. /**
  145. * @test
  146. */
  147. public function test_delete_active_group_reset_activeGroup_setting()
  148. {
  149. Settings::set('rememberActiveGroup', true);
  150. Settings::set('activeGroup', $this->groupOne->id);
  151. $deleted = Groups::delete($this->groupOne->id);
  152. $this->assertDatabaseHas('options', [
  153. 'key' => 'activeGroup',
  154. 'value' => 0
  155. ]);
  156. }
  157. /**
  158. * @test
  159. */
  160. public function test_assign_a_twofaccountid_to_a_specified_group_persists_the_relation()
  161. {
  162. Groups::assign($this->twofaccountOne->id, $this->groupOne);
  163. $this->assertDatabaseHas('twofaccounts', [
  164. 'id' => $this->twofaccountOne->id,
  165. 'group_id' => $this->groupOne->id,
  166. ]);
  167. }
  168. /**
  169. * @test
  170. */
  171. public function test_assign_multiple_twofaccountid_to_a_specified_group_persists_the_relation()
  172. {
  173. Groups::assign([$this->twofaccountOne->id, $this->twofaccountTwo->id], $this->groupOne);
  174. $this->assertDatabaseHas('twofaccounts', [
  175. 'id' => $this->twofaccountOne->id,
  176. 'group_id' => $this->groupOne->id,
  177. ]);
  178. $this->assertDatabaseHas('twofaccounts', [
  179. 'id' => $this->twofaccountTwo->id,
  180. 'group_id' => $this->groupOne->id,
  181. ]);
  182. }
  183. /**
  184. * @test
  185. */
  186. public function test_assign_a_twofaccountid_to_no_group_assigns_to_default_group()
  187. {
  188. Settings::set('defaultGroup', $this->groupTwo->id);
  189. Groups::assign($this->twofaccountOne->id);
  190. $this->assertDatabaseHas('twofaccounts', [
  191. 'id' => $this->twofaccountOne->id,
  192. 'group_id' => $this->groupTwo->id,
  193. ]);
  194. }
  195. /**
  196. * @test
  197. */
  198. public function test_assign_a_twofaccountid_to_no_group_assigns_to_active_group()
  199. {
  200. Settings::set('defaultGroup', -1);
  201. Settings::set('activeGroup', $this->groupTwo->id);
  202. Groups::assign($this->twofaccountOne->id);
  203. $this->assertDatabaseHas('twofaccounts', [
  204. 'id' => $this->twofaccountOne->id,
  205. 'group_id' => $this->groupTwo->id,
  206. ]);
  207. }
  208. /**
  209. * @test
  210. */
  211. public function test_assign_a_twofaccountid_to_missing_active_group_does_not_fails()
  212. {
  213. Settings::set('defaultGroup', -1);
  214. Settings::set('activeGroup', 100000);
  215. Groups::assign($this->twofaccountOne->id);
  216. $this->assertDatabaseHas('twofaccounts', [
  217. 'id' => $this->twofaccountOne->id,
  218. 'group_id' => null,
  219. ]);
  220. }
  221. /**
  222. * @test
  223. */
  224. public function test_getAccounts_returns_accounts()
  225. {
  226. Groups::assign([$this->twofaccountOne->id, $this->twofaccountTwo->id], $this->groupOne);
  227. $accounts = Groups::getAccounts($this->groupOne);
  228. $this->assertEquals(2, $accounts->count());
  229. }
  230. }