GroupControllerTest.php 8.5 KB

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