GroupControllerTest.php 12 KB

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