GroupControllerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?php
  2. namespace Tests\Api\v1\Controllers;
  3. use App\Api\v1\Controllers\GroupController;
  4. use App\Api\v1\Resources\GroupResource;
  5. use App\Listeners\ResetUsersPreference;
  6. use App\Models\Group;
  7. use App\Models\TwoFAccount;
  8. use App\Models\User;
  9. use App\Policies\GroupPolicy;
  10. use PHPUnit\Framework\Attributes\CoversClass;
  11. use Tests\FeatureTestCase;
  12. #[CoversClass(GroupController::class)]
  13. #[CoversClass(GroupResource::class)]
  14. #[CoversClass(ResetUsersPreference::class)]
  15. #[CoversClass(GroupPolicy::class)]
  16. #[CoversClass(Group::class)]
  17. class GroupControllerTest extends FeatureTestCase
  18. {
  19. /**
  20. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  21. */
  22. protected $user;
  23. protected $anotherUser;
  24. /**
  25. * @var App\Models\Group
  26. */
  27. protected $userGroupA;
  28. protected $userGroupB;
  29. protected $anotherUserGroupA;
  30. protected $anotherUserGroupB;
  31. /**
  32. * @var App\Models\TwoFAccount
  33. */
  34. protected $twofaccountA;
  35. protected $twofaccountB;
  36. protected $twofaccountC;
  37. protected $twofaccountD;
  38. private const NEW_GROUP_NAME = 'MyNewGroup';
  39. /**
  40. * @test
  41. */
  42. public function setUp() : void
  43. {
  44. parent::setUp();
  45. $this->user = User::factory()->create();
  46. $this->userGroupA = Group::factory()->for($this->user)->create();
  47. $this->userGroupB = Group::factory()->for($this->user)->create();
  48. $this->twofaccountA = TwoFAccount::factory()->for($this->user)->create([
  49. 'group_id' => $this->userGroupA->id,
  50. ]);
  51. $this->twofaccountB = TwoFAccount::factory()->for($this->user)->create([
  52. 'group_id' => $this->userGroupA->id,
  53. ]);
  54. $this->anotherUser = User::factory()->create();
  55. $this->anotherUserGroupA = Group::factory()->for($this->anotherUser)->create();
  56. $this->anotherUserGroupB = Group::factory()->for($this->anotherUser)->create();
  57. $this->twofaccountC = TwoFAccount::factory()->for($this->anotherUser)->create([
  58. 'group_id' => $this->anotherUserGroupA->id,
  59. ]);
  60. $this->twofaccountD = TwoFAccount::factory()->for($this->anotherUser)->create([
  61. 'group_id' => $this->anotherUserGroupB->id,
  62. ]);
  63. }
  64. /**
  65. * @test
  66. */
  67. public function test_index_returns_user_groups_only_with_pseudo_group()
  68. {
  69. $this->actingAs($this->user, 'api-guard')
  70. ->json('GET', '/api/v1/groups')
  71. ->assertOk()
  72. ->assertExactJson([
  73. '0' => [
  74. 'id' => 0,
  75. 'name' => 'All',
  76. 'twofaccounts_count' => 2,
  77. ],
  78. '1' => [
  79. 'id' => $this->userGroupA->id,
  80. 'name' => $this->userGroupA->name,
  81. 'twofaccounts_count' => 2,
  82. ],
  83. '2' => [
  84. 'id' => $this->userGroupB->id,
  85. 'name' => $this->userGroupB->name,
  86. 'twofaccounts_count' => 0,
  87. ],
  88. ]);
  89. }
  90. /**
  91. * @test
  92. */
  93. public function test_store_returns_created_group_resource()
  94. {
  95. $this->actingAs($this->user, 'api-guard')
  96. ->json('POST', '/api/v1/groups', [
  97. 'name' => self::NEW_GROUP_NAME,
  98. ])
  99. ->assertCreated()
  100. ->assertJsonFragment([
  101. 'name' => self::NEW_GROUP_NAME,
  102. 'twofaccounts_count' => 0,
  103. ]);
  104. $this->assertDatabaseHas('groups', [
  105. 'name' => self::NEW_GROUP_NAME,
  106. 'user_id' => $this->user->id,
  107. ]);
  108. }
  109. /**
  110. * @test
  111. */
  112. public function test_store_invalid_data_returns_validation_error()
  113. {
  114. $this->actingAs($this->user, 'api-guard')
  115. ->json('POST', '/api/v1/groups', [
  116. 'name' => null,
  117. ])
  118. ->assertStatus(422);
  119. }
  120. /**
  121. * @test
  122. */
  123. public function test_show_returns_group_resource()
  124. {
  125. $group = Group::factory()->for($this->user)->create([
  126. 'name' => 'My group',
  127. ]);
  128. $response = $this->actingAs($this->user, 'api-guard')
  129. ->json('GET', '/api/v1/groups/' . $group->id)
  130. ->assertOk()
  131. ->assertJsonFragment([
  132. 'name' => 'My group',
  133. 'twofaccounts_count' => 0,
  134. ]);
  135. }
  136. /**
  137. * @test
  138. */
  139. public function test_show_missing_group_returns_not_found()
  140. {
  141. $response = $this->actingAs($this->user, 'api-guard')
  142. ->json('GET', '/api/v1/groups/1000')
  143. ->assertNotFound()
  144. ->assertJsonStructure([
  145. 'message',
  146. ]);
  147. }
  148. /**
  149. * @test
  150. */
  151. public function test_show_group_of_another_user_is_forbidden()
  152. {
  153. $response = $this->actingAs($this->anotherUser, 'api-guard')
  154. ->json('GET', '/api/v1/groups/' . $this->userGroupA->id)
  155. ->assertForbidden()
  156. ->assertJsonStructure([
  157. 'message',
  158. ]);
  159. }
  160. /**
  161. * @test
  162. */
  163. public function test_update_returns_updated_group_resource()
  164. {
  165. $group = Group::factory()->for($this->user)->create();
  166. $response = $this->actingAs($this->user, 'api-guard')
  167. ->json('PUT', '/api/v1/groups/' . $group->id, [
  168. 'name' => 'name updated',
  169. ])
  170. ->assertOk()
  171. ->assertJsonFragment([
  172. 'name' => 'name updated',
  173. 'twofaccounts_count' => 0,
  174. ]);
  175. }
  176. /**
  177. * @test
  178. */
  179. public function test_update_missing_group_returns_not_found()
  180. {
  181. $response = $this->actingAs($this->user, 'api-guard')
  182. ->json('PUT', '/api/v1/groups/1000', [
  183. 'name' => 'testUpdate',
  184. ])
  185. ->assertNotFound()
  186. ->assertJsonStructure([
  187. 'message',
  188. ]);
  189. }
  190. /**
  191. * @test
  192. */
  193. public function test_update_with_invalid_data_returns_validation_error()
  194. {
  195. $group = Group::factory()->for($this->user)->create();
  196. $response = $this->actingAs($this->user, 'api-guard')
  197. ->json('PUT', '/api/v1/groups/' . $group->id, [
  198. 'name' => null,
  199. ])
  200. ->assertStatus(422);
  201. }
  202. /**
  203. * @test
  204. */
  205. public function test_update_group_of_another_user_is_forbidden()
  206. {
  207. $response = $this->actingAs($this->anotherUser, 'api-guard')
  208. ->json('PUT', '/api/v1/groups/' . $this->userGroupA->id, [
  209. 'name' => 'name updated',
  210. ])
  211. ->assertForbidden()
  212. ->assertJsonStructure([
  213. 'message',
  214. ]);
  215. }
  216. /**
  217. * @test
  218. */
  219. public function test_assign_accounts_returns_updated_group_resource()
  220. {
  221. $group = Group::factory()->for($this->user)->create();
  222. $accounts = TwoFAccount::factory()->count(2)->for($this->user)->create();
  223. $response = $this->actingAs($this->user, 'api-guard')
  224. ->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
  225. 'ids' => [$accounts[0]->id, $accounts[1]->id],
  226. ])
  227. ->assertOk()
  228. ->assertExactJson([
  229. 'id' => $group->id,
  230. 'name' => $group->name,
  231. 'twofaccounts_count' => 2,
  232. ]);
  233. }
  234. /**
  235. * @test
  236. */
  237. public function test_assign_accounts_to_missing_group_returns_not_found()
  238. {
  239. $accounts = TwoFAccount::factory()->count(2)->for($this->user)->create();
  240. $response = $this->actingAs($this->user, 'api-guard')
  241. ->json('POST', '/api/v1/groups/1000/assign', [
  242. 'ids' => [$accounts[0]->id, $accounts[1]->id],
  243. ])
  244. ->assertNotFound()
  245. ->assertJsonStructure([
  246. 'message',
  247. ]);
  248. }
  249. /**
  250. * @test
  251. */
  252. public function test_assign_invalid_accounts_returns_validation_error()
  253. {
  254. $group = Group::factory()->for($this->user)->create();
  255. $accounts = TwoFAccount::factory()->count(2)->for($this->user)->create();
  256. $response = $this->actingAs($this->user, 'api-guard')
  257. ->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
  258. 'ids' => 1,
  259. ])
  260. ->assertStatus(422);
  261. }
  262. /**
  263. * @test
  264. */
  265. public function test_assign_to_group_of_another_user_is_forbidden()
  266. {
  267. $response = $this->actingAs($this->anotherUser, 'api-guard')
  268. ->json('POST', '/api/v1/groups/' . $this->userGroupA->id . '/assign', [
  269. 'ids' => [$this->twofaccountC->id, $this->twofaccountD->id],
  270. ])
  271. ->assertForbidden()
  272. ->assertJsonStructure([
  273. 'message',
  274. ]);
  275. }
  276. /**
  277. * @test
  278. */
  279. public function test_assign_accounts_of_another_user_is_forbidden()
  280. {
  281. $response = $this->actingAs($this->user, 'api-guard')
  282. ->json('POST', '/api/v1/groups/' . $this->userGroupA->id . '/assign', [
  283. 'ids' => [$this->twofaccountC->id, $this->twofaccountD->id],
  284. ])
  285. ->assertForbidden()
  286. ->assertJsonStructure([
  287. 'message',
  288. ]);
  289. }
  290. /**
  291. * @test
  292. */
  293. public function test_accounts_returns_twofaccounts_collection()
  294. {
  295. $response = $this->actingAs($this->user, 'api-guard')
  296. ->json('GET', '/api/v1/groups/' . $this->userGroupA->id . '/twofaccounts')
  297. ->assertOk()
  298. ->assertJsonCount(2)
  299. ->assertJsonStructure([
  300. '*' => [
  301. 'group_id',
  302. 'service',
  303. 'account',
  304. 'icon',
  305. 'otp_type',
  306. 'digits',
  307. 'algorithm',
  308. 'period',
  309. 'counter',
  310. ],
  311. ])
  312. ->assertJsonFragment([
  313. 'account' => $this->twofaccountA->account,
  314. ])
  315. ->assertJsonFragment([
  316. 'account' => $this->twofaccountB->account,
  317. ]);
  318. }
  319. /**
  320. * @test
  321. */
  322. public function test_accounts_returns_twofaccounts_collection_with_secret()
  323. {
  324. $response = $this->actingAs($this->user, 'api-guard')
  325. ->json('GET', '/api/v1/groups/' . $this->userGroupA->id . '/twofaccounts?withSecret=1')
  326. ->assertOk()
  327. ->assertJsonCount(2)
  328. ->assertJsonStructure([
  329. '*' => [
  330. 'group_id',
  331. 'service',
  332. 'account',
  333. 'icon',
  334. 'secret',
  335. 'otp_type',
  336. 'digits',
  337. 'algorithm',
  338. 'period',
  339. 'counter',
  340. ],
  341. ]);
  342. }
  343. /**
  344. * @test
  345. */
  346. public function test_accounts_of_missing_group_returns_not_found()
  347. {
  348. $response = $this->actingAs($this->user, 'api-guard')
  349. ->json('GET', '/api/v1/groups/1000/twofaccounts')
  350. ->assertNotFound()
  351. ->assertJsonStructure([
  352. 'message',
  353. ]);
  354. }
  355. /**
  356. * @test
  357. */
  358. public function test_accounts_of_another_user_group_is_forbidden()
  359. {
  360. $response = $this->actingAs($this->anotherUser, 'api-guard')
  361. ->json('GET', '/api/v1/groups/' . $this->userGroupA->id . '/twofaccounts')
  362. ->assertForbidden()
  363. ->assertJsonStructure([
  364. 'message',
  365. ]);
  366. }
  367. /**
  368. * test Group deletion via API
  369. *
  370. * @test
  371. */
  372. public function test_destroy_group_returns_success()
  373. {
  374. $group = Group::factory()->for($this->user)->create();
  375. $this->actingAs($this->user, 'api-guard')
  376. ->json('DELETE', '/api/v1/groups/' . $group->id)
  377. ->assertNoContent();
  378. }
  379. /**
  380. * test Group deletion via API
  381. *
  382. * @test
  383. */
  384. public function test_destroy_missing_group_returns_not_found()
  385. {
  386. $this->actingAs($this->user, 'api-guard')
  387. ->json('DELETE', '/api/v1/groups/1000')
  388. ->assertNotFound()
  389. ->assertJsonStructure([
  390. 'message',
  391. ]);
  392. }
  393. /**
  394. * @test
  395. */
  396. public function test_destroy_group_of_another_user_is_forbidden()
  397. {
  398. $response = $this->actingAs($this->anotherUser, 'api-guard')
  399. ->json('DELETE', '/api/v1/groups/' . $this->userGroupA->id)
  400. ->assertForbidden()
  401. ->assertJsonStructure([
  402. 'message',
  403. ]);
  404. }
  405. /**
  406. * @test
  407. */
  408. public function test_destroy_group_resets_user_preferences()
  409. {
  410. // Set the default group to a specific one
  411. $this->user['preferences->defaultGroup'] = $this->userGroupA->id;
  412. // Set the active group
  413. $this->user['preferences->activeGroup'] = $this->userGroupA->id;
  414. $this->user->save();
  415. $this->assertEquals($this->userGroupA->id, $this->user->preferences['defaultGroup']);
  416. $this->assertEquals($this->userGroupA->id, $this->user->preferences['activeGroup']);
  417. $this->actingAs($this->user, 'api-guard')
  418. ->json('DELETE', '/api/v1/groups/' . $this->userGroupA->id);
  419. $this->user->refresh();
  420. $this->assertEquals(0, $this->user->preferences['defaultGroup']);
  421. $this->assertEquals(0, $this->user->preferences['activeGroup']);
  422. }
  423. /**
  424. * @test
  425. */
  426. public function test_twofaccount_is_released_on_group_destroy()
  427. {
  428. $this->actingAs($this->user, 'api-guard')
  429. ->json('DELETE', '/api/v1/groups/' . $this->userGroupA->id)
  430. ->assertNoContent();
  431. $this->twofaccountA->refresh();
  432. $this->twofaccountB->refresh();
  433. $this->assertNull($this->twofaccountA->group_id);
  434. $this->assertNull($this->twofaccountB->group_id);
  435. }
  436. }