GroupControllerTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace Tests\Api\v1\Controllers;
  3. use App\Models\User;
  4. use App\Models\Group;
  5. use Tests\FeatureTestCase;
  6. use App\Models\TwoFAccount;
  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
  15. */
  16. protected $user;
  17. /**
  18. * @test
  19. */
  20. public function setUp(): void
  21. {
  22. parent::setUp();
  23. $this->user = User::factory()->create();
  24. }
  25. /**
  26. * @test
  27. */
  28. public function test_index_returns_group_collection_with_pseudo_group()
  29. {
  30. Group::factory()->count(3)->create();
  31. $response = $this->actingAs($this->user, 'api')
  32. ->json('GET', '/api/v1/groups')
  33. ->assertOk()
  34. ->assertJsonCount(4, $key = null)
  35. ->assertJsonStructure([
  36. '*' => [
  37. 'id',
  38. 'name',
  39. 'twofaccounts_count',
  40. ]
  41. ])
  42. ->assertJsonFragment([
  43. 'id' => 0,
  44. 'name' => 'All',
  45. 'twofaccounts_count' => 0,
  46. ]);
  47. }
  48. /**
  49. * @test
  50. */
  51. public function test_store_returns_created_group_resource()
  52. {
  53. $response = $this->actingAs($this->user, 'api')
  54. ->json('POST', '/api/v1/groups', [
  55. 'name' => 'My second group',
  56. ])
  57. ->assertCreated()
  58. ->assertExactJson([
  59. 'id' => 1,
  60. 'name' => 'My second group',
  61. 'twofaccounts_count' => 0,
  62. ]);
  63. }
  64. /**
  65. * @test
  66. */
  67. public function test_store_invalid_data_returns_validation_error()
  68. {
  69. $response = $this->actingAs($this->user, 'api')
  70. ->json('POST', '/api/v1/groups', [
  71. 'name' => null,
  72. ])
  73. ->assertStatus(422);
  74. }
  75. /**
  76. * @test
  77. */
  78. public function test_show_returns_group_resource()
  79. {
  80. $group = Group::factory()->create([
  81. 'name' => 'My group',
  82. ]);
  83. $response = $this->actingAs($this->user, 'api')
  84. ->json('GET', '/api/v1/groups/' . $group->id)
  85. ->assertOk()
  86. ->assertExactJson([
  87. 'id' => 1,
  88. 'name' => 'My group',
  89. 'twofaccounts_count' => 0,
  90. ]);
  91. }
  92. /**
  93. * @test
  94. */
  95. public function test_show_missing_group_returns_not_found()
  96. {
  97. $response = $this->actingAs($this->user, 'api')
  98. ->json('GET', '/api/v1/groups/1000')
  99. ->assertNotFound()
  100. ->assertJsonStructure([
  101. 'message'
  102. ]);
  103. }
  104. /**
  105. * @test
  106. */
  107. public function test_update_returns_updated_group_resource()
  108. {
  109. $group = Group::factory()->create();
  110. $response = $this->actingAs($this->user, 'api')
  111. ->json('PUT', '/api/v1/groups/' . $group->id, [
  112. 'name' => 'name updated',
  113. ])
  114. ->assertOk()
  115. ->assertExactJson([
  116. 'id' => 1,
  117. 'name' => 'name updated',
  118. 'twofaccounts_count' => 0,
  119. ]);
  120. }
  121. /**
  122. * @test
  123. */
  124. public function test_update_missing_group_returns_not_found()
  125. {
  126. $response = $this->actingAs($this->user, 'api')
  127. ->json('PUT', '/api/v1/groups/1000', [
  128. 'name' => 'testUpdate',
  129. ])
  130. ->assertNotFound()
  131. ->assertJsonStructure([
  132. 'message'
  133. ]);
  134. }
  135. /**
  136. * @test
  137. */
  138. public function test_update_with_invalid_data_returns_validation_error()
  139. {
  140. $group = Group::factory()->create();
  141. $response = $this->actingAs($this->user, 'api')
  142. ->json('PUT', '/api/v1/groups/' . $group->id, [
  143. 'name' => null,
  144. ])
  145. ->assertStatus(422);
  146. }
  147. /**
  148. * @test
  149. */
  150. public function test_assign_accounts_returns_updated_group_resource()
  151. {
  152. $group = Group::factory()->create();
  153. $accounts = TwoFAccount::factory()->count(2)->create();
  154. $response = $this->actingAs($this->user, 'api')
  155. ->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
  156. 'ids' => [1,2],
  157. ])
  158. ->assertOk()
  159. ->assertExactJson([
  160. 'id' => $group->id,
  161. 'name' => $group->name,
  162. 'twofaccounts_count' => 2,
  163. ]);
  164. }
  165. /**
  166. * @test
  167. */
  168. public function test_assign_accounts_to_missing_group_returns_not_found()
  169. {
  170. $accounts = TwoFAccount::factory()->count(2)->create();
  171. $response = $this->actingAs($this->user, 'api')
  172. ->json('POST', '/api/v1/groups/1000/assign', [
  173. 'ids' => [1,2],
  174. ])
  175. ->assertNotFound()
  176. ->assertJsonStructure([
  177. 'message'
  178. ]);
  179. }
  180. /**
  181. * @test
  182. */
  183. public function test_assign_invalid_accounts_returns_validation_error()
  184. {
  185. $group = Group::factory()->create();
  186. $accounts = TwoFAccount::factory()->count(2)->create();
  187. $response = $this->actingAs($this->user, 'api')
  188. ->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
  189. 'ids' => 1,
  190. ])
  191. ->assertStatus(422);
  192. }
  193. /**
  194. * @test
  195. */
  196. public function test_get_assigned_accounts_returns_twofaccounts_collection()
  197. {
  198. $group = Group::factory()->create();
  199. $accounts = TwoFAccount::factory()->count(2)->create();
  200. $assign = $this->actingAs($this->user, 'api')
  201. ->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
  202. 'ids' => [1,2],
  203. ]);
  204. $response = $this->actingAs($this->user, 'api')
  205. ->json('GET', '/api/v1/groups/' . $group->id . '/twofaccounts')
  206. ->assertOk()
  207. ->assertJsonCount(2)
  208. ->assertJsonStructure([
  209. '*' => [
  210. 'group_id',
  211. 'service',
  212. 'account',
  213. 'icon',
  214. 'otp_type',
  215. 'digits',
  216. 'algorithm',
  217. 'period',
  218. 'counter'
  219. ]
  220. ]);
  221. }
  222. /**
  223. * @test
  224. */
  225. public function test_get_assigned_accounts_returns_twofaccounts_collection_with_secret()
  226. {
  227. $group = Group::factory()->create();
  228. $accounts = TwoFAccount::factory()->count(2)->create();
  229. $assign = $this->actingAs($this->user, 'api')
  230. ->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
  231. 'ids' => [1,2],
  232. ]);
  233. $response = $this->actingAs($this->user, 'api')
  234. ->json('GET', '/api/v1/groups/' . $group->id . '/twofaccounts?withSecret=1')
  235. ->assertOk()
  236. ->assertJsonCount(2)
  237. ->assertJsonStructure([
  238. '*' => [
  239. 'group_id',
  240. 'service',
  241. 'account',
  242. 'icon',
  243. 'secret',
  244. 'otp_type',
  245. 'digits',
  246. 'algorithm',
  247. 'period',
  248. 'counter'
  249. ]
  250. ]);
  251. }
  252. /**
  253. * @test
  254. */
  255. public function test_get_assigned_accounts_of_missing_group_returns_not_found()
  256. {
  257. $response = $this->actingAs($this->user, 'api')
  258. ->json('GET', '/api/v1/groups/1000/twofaccounts')
  259. ->assertNotFound()
  260. ->assertJsonStructure([
  261. 'message'
  262. ]);
  263. }
  264. /**
  265. * test Group deletion via API
  266. *
  267. * @test
  268. */
  269. public function test_destroy_group_returns_success()
  270. {
  271. $group = Group::factory()->create();
  272. $response = $this->actingAs($this->user, 'api')
  273. ->json('DELETE', '/api/v1/groups/' . $group->id)
  274. ->assertNoContent();
  275. }
  276. /**
  277. * test Group deletion via API
  278. *
  279. * @test
  280. */
  281. public function test_destroy_missing_group_returns_not_found()
  282. {
  283. $response = $this->actingAs($this->user, 'api')
  284. ->json('DELETE', '/api/v1/groups/1000')
  285. ->assertNotFound()
  286. ->assertJsonStructure([
  287. 'message'
  288. ]);
  289. }
  290. }