AccountsGroupTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace Tests\Feature;
  3. use App\User;
  4. use App\Group;
  5. use Tests\TestCase;
  6. use App\TwoFAccount;
  7. class AccountsGroupTest extends TestCase
  8. {
  9. /** @var \App\User, \App\TwoFAccount, \App\Group */
  10. protected $user, $twofaccounts, $group;
  11. /**
  12. * @test
  13. */
  14. public function setUp(): void
  15. {
  16. parent::setUp();
  17. $this->user = factory(User::class)->create();
  18. $this->twofaccounts = factory(Twofaccount::class, 3)->create();
  19. $this->group = factory(Group::class)->create();
  20. }
  21. /**
  22. * test 2FAccounts creation associated to a user group via API
  23. *
  24. * @test
  25. */
  26. public function testCreateAccountWhenDefaultGroupIsASpecificOne()
  27. {
  28. // Set the default group to the existing one
  29. $response = $this->actingAs($this->user, 'api')
  30. ->json('POST', '/api/settings/options', [
  31. 'defaultGroup' => $this->group->id,
  32. ])
  33. ->assertStatus(200);
  34. // Create the account
  35. $response = $this->actingAs($this->user, 'api')
  36. ->json('POST', '/api/twofaccounts', [
  37. 'service' => 'testCreation',
  38. 'account' => 'test@example.org',
  39. 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHZVRBGY7UIW&issuer=test',
  40. 'icon' => 'test.png',
  41. ])
  42. ->assertStatus(201)
  43. ->assertJsonFragment([
  44. 'group_id' => $this->group->id
  45. ]);
  46. }
  47. /**
  48. * test 2FAccounts creation associated to a user group via API
  49. *
  50. * @test
  51. */
  52. public function testCreateAccountWhenDefaultGroupIsSetToActiveOne()
  53. {
  54. // Set the default group as the active one
  55. $response = $this->actingAs($this->user, 'api')
  56. ->json('POST', '/api/settings/options', [
  57. 'defaultGroup' => -1,
  58. ])
  59. ->assertStatus(200);
  60. // Set the active group
  61. $response = $this->actingAs($this->user, 'api')
  62. ->json('POST', '/api/settings/options', [
  63. 'activeGroup' => 1,
  64. ])
  65. ->assertStatus(200);
  66. // Create the account
  67. $response = $this->actingAs($this->user, 'api')
  68. ->json('POST', '/api/twofaccounts', [
  69. 'service' => 'testCreation',
  70. 'account' => 'test@example.org',
  71. 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHZVRBGY7UIW&issuer=test',
  72. 'icon' => 'test.png',
  73. ])
  74. ->assertStatus(201)
  75. ->assertJsonFragment([
  76. 'group_id' => 1
  77. ]);
  78. }
  79. /**
  80. * test 2FAccounts creation associated to a user group via API
  81. *
  82. * @test
  83. */
  84. public function testCreateAccountWhenDefaultIsNoGroup()
  85. {
  86. // Set the default group to No group
  87. $response = $this->actingAs($this->user, 'api')
  88. ->json('POST', '/api/settings/options', [
  89. 'defaultGroup' => 0,
  90. ])
  91. ->assertStatus(200);
  92. // Create the account
  93. $response = $this->actingAs($this->user, 'api')
  94. ->json('POST', '/api/twofaccounts', [
  95. 'service' => 'testCreation',
  96. 'account' => 'test@example.org',
  97. 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHZVRBGY7UIW&issuer=test',
  98. 'icon' => 'test.png',
  99. ])
  100. ->assertStatus(201)
  101. ->assertJsonMissing([
  102. 'group_id' => null
  103. ]);
  104. }
  105. /**
  106. * test 2FAccounts creation associated to a user group via API
  107. *
  108. * @test
  109. */
  110. public function testCreateAccountWhenDefaultGroupDoNotExists()
  111. {
  112. // Set the default group to a non existing one
  113. $response = $this->actingAs($this->user, 'api')
  114. ->json('POST', '/api/settings/options', [
  115. 'defaultGroup' => 1000,
  116. ])
  117. ->assertStatus(200);
  118. // Create the account
  119. $response = $this->actingAs($this->user, 'api')
  120. ->json('POST', '/api/twofaccounts', [
  121. 'service' => 'testCreation',
  122. 'account' => 'test@example.org',
  123. 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHZVRBGY7UIW&issuer=test',
  124. 'icon' => 'test.png',
  125. ])
  126. ->assertStatus(201)
  127. ->assertJsonMissing([
  128. 'group_id' => null
  129. ]);
  130. }
  131. /**
  132. * test 2FAccounts association with a user group via API
  133. *
  134. * @test
  135. */
  136. public function testMoveAccountsToGroup()
  137. {
  138. // We associate all 3 accounts to the user group
  139. $response = $this->actingAs($this->user, 'api')
  140. ->json('PATCH', '/api/group/accounts/', [
  141. 'groupId' => $this->group->id,
  142. 'accountsIds' => [1,2,3]
  143. ])
  144. ->assertJsonFragment([
  145. 'id' => $this->group->id,
  146. 'name' => $this->group->name
  147. ])
  148. ->assertStatus(200);
  149. // test if the accounts have the correct foreign key
  150. $response = $this->actingAs($this->user, 'api')
  151. ->json('GET', '/api/twofaccounts/1')
  152. ->assertJsonFragment([
  153. 'group_id' => $this->group->id
  154. ]);
  155. $response = $this->actingAs($this->user, 'api')
  156. ->json('GET', '/api/twofaccounts/2')
  157. ->assertJsonFragment([
  158. 'group_id' => $this->group->id
  159. ]);
  160. $response = $this->actingAs($this->user, 'api')
  161. ->json('GET', '/api/twofaccounts/3')
  162. ->assertJsonFragment([
  163. 'group_id' => $this->group->id
  164. ]);
  165. // test the accounts count of the user group
  166. $response = $this->actingAs($this->user, 'api')
  167. ->json('GET', '/api/groups')
  168. ->assertJsonFragment([
  169. 'twofaccounts_count' => 3
  170. ]
  171. );
  172. }
  173. /**
  174. * test 2FAccounts association with a missing group via API
  175. *
  176. * @test
  177. */
  178. public function testMoveAccountsToMissingGroup()
  179. {
  180. $response = $this->actingAs($this->user, 'api')
  181. ->json('PATCH', '/api/group/accounts/', [
  182. 'groupId' => '1000',
  183. 'accountsIds' => $this->twofaccounts->keys()
  184. ])
  185. ->assertStatus(404);
  186. }
  187. /**
  188. * test 2FAccounts association with the pseudo group via API
  189. *
  190. * @test
  191. */
  192. public function testMoveAccountsToPseudoGroup()
  193. {
  194. $response = $this->actingAs($this->user, 'api')
  195. ->json('PATCH', '/api/group/accounts/', [
  196. 'groupId' => $this->group->id,
  197. 'accountsIds' => [1,2,3]
  198. ]);
  199. // We associate the first account to the pseudo group
  200. $response = $this->actingAs($this->user, 'api')
  201. ->json('PATCH', '/api/group/accounts/', [
  202. 'groupId' => 0,
  203. 'accountsIds' => [1]
  204. ])
  205. ->assertStatus(200);
  206. // test if the forein keys are set to NULL
  207. $response = $this->actingAs($this->user, 'api')
  208. ->json('GET', '/api/twofaccounts/1')
  209. ->assertJsonFragment([
  210. 'group_id' => null
  211. ]);
  212. // test the accounts count of the group
  213. $response = $this->actingAs($this->user, 'api')
  214. ->json('GET', '/api/groups')
  215. ->assertJsonFragment([
  216. 'twofaccounts_count' => 3, // the 3 accounts for 'all'
  217. 'twofaccounts_count' => 2 // the 2 accounts that remain in the user group
  218. ]
  219. );
  220. }
  221. }