2FAuth/tests/Api/v1/Controllers/GroupControllerTest.php

321 lines
8.6 KiB
PHP
Raw Normal View History

2021-11-14 01:52:46 +01:00
<?php
namespace Tests\Api\v1\Controllers;
2021-12-02 13:15:53 +01:00
use App\Models\Group;
use App\Models\TwoFAccount;
2022-11-22 15:15:52 +01:00
use App\Models\User;
use Tests\FeatureTestCase;
2021-11-22 01:09:54 +01:00
/**
* @covers \App\Api\v1\Controllers\GroupController
2021-11-30 17:39:33 +01:00
* @covers \App\Api\v1\Resources\GroupResource
2021-11-22 01:09:54 +01:00
*/
2021-11-14 01:52:46 +01:00
class GroupControllerTest extends FeatureTestCase
{
/**
2021-12-02 13:15:53 +01:00
* @var \App\Models\User
2022-11-22 15:15:52 +01:00
*/
2021-11-14 01:52:46 +01:00
protected $user;
/**
* @test
*/
2022-11-22 15:15:52 +01:00
public function setUp() : void
2021-11-14 01:52:46 +01:00
{
parent::setUp();
2021-12-02 13:15:53 +01:00
$this->user = User::factory()->create();
2021-11-14 01:52:46 +01:00
}
/**
* @test
*/
public function test_index_returns_group_collection_with_pseudo_group()
{
2021-12-02 13:15:53 +01:00
Group::factory()->count(3)->create();
2021-11-14 01:52:46 +01:00
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('GET', '/api/v1/groups')
->assertOk()
->assertJsonCount(4, $key = null)
->assertJsonStructure([
'*' => [
'id',
'name',
'twofaccounts_count',
2022-11-22 15:15:52 +01:00
],
2021-11-14 01:52:46 +01:00
])
->assertJsonFragment([
2022-11-22 15:15:52 +01:00
'id' => 0,
'name' => 'All',
2021-11-14 01:52:46 +01:00
'twofaccounts_count' => 0,
]);
}
/**
* @test
*/
public function test_store_returns_created_group_resource()
{
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('POST', '/api/v1/groups', [
'name' => 'My second group',
])
->assertCreated()
2022-04-01 13:35:59 +02:00
->assertJsonFragment([
2022-11-22 15:15:52 +01:00
'name' => 'My second group',
2021-11-14 01:52:46 +01:00
'twofaccounts_count' => 0,
]);
}
/**
* @test
*/
public function test_store_invalid_data_returns_validation_error()
{
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('POST', '/api/v1/groups', [
'name' => null,
])
->assertStatus(422);
}
/**
* @test
*/
public function test_show_returns_group_resource()
{
2021-12-02 13:15:53 +01:00
$group = Group::factory()->create([
2021-11-14 01:52:46 +01:00
'name' => 'My group',
]);
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('GET', '/api/v1/groups/' . $group->id)
->assertOk()
2022-04-01 13:35:59 +02:00
->assertJsonFragment([
2022-11-22 15:15:52 +01:00
'name' => 'My group',
2021-11-14 01:52:46 +01:00
'twofaccounts_count' => 0,
]);
}
/**
* @test
*/
public function test_show_missing_group_returns_not_found()
{
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('GET', '/api/v1/groups/1000')
->assertNotFound()
->assertJsonStructure([
2022-11-22 15:15:52 +01:00
'message',
2021-11-14 01:52:46 +01:00
]);
}
/**
* @test
*/
public function test_update_returns_updated_group_resource()
{
2021-12-02 13:15:53 +01:00
$group = Group::factory()->create();
2021-11-14 01:52:46 +01:00
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('PUT', '/api/v1/groups/' . $group->id, [
'name' => 'name updated',
])
2021-11-14 01:52:46 +01:00
->assertOk()
2022-04-01 13:35:59 +02:00
->assertJsonFragment([
2022-11-22 15:15:52 +01:00
'name' => 'name updated',
2021-11-14 01:52:46 +01:00
'twofaccounts_count' => 0,
]);
}
/**
* @test
*/
public function test_update_missing_group_returns_not_found()
{
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('PUT', '/api/v1/groups/1000', [
'name' => 'testUpdate',
])
2021-11-14 01:52:46 +01:00
->assertNotFound()
->assertJsonStructure([
2022-11-22 15:15:52 +01:00
'message',
2021-11-14 01:52:46 +01:00
]);
}
/**
* @test
*/
public function test_update_with_invalid_data_returns_validation_error()
{
2021-12-02 13:15:53 +01:00
$group = Group::factory()->create();
2021-11-14 01:52:46 +01:00
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('PUT', '/api/v1/groups/' . $group->id, [
'name' => null,
])
2021-11-14 01:52:46 +01:00
->assertStatus(422);
}
/**
* @test
*/
public function test_assign_accounts_returns_updated_group_resource()
{
2022-11-22 15:15:52 +01:00
$group = Group::factory()->create();
2021-12-02 13:15:53 +01:00
$accounts = TwoFAccount::factory()->count(2)->create();
2021-11-14 01:52:46 +01:00
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
2022-04-01 13:35:59 +02:00
'ids' => [$accounts[0]->id, $accounts[1]->id],
])
2021-11-14 01:52:46 +01:00
->assertOk()
->assertExactJson([
2022-11-22 15:15:52 +01:00
'id' => $group->id,
'name' => $group->name,
2021-11-14 01:52:46 +01:00
'twofaccounts_count' => 2,
]);
}
/**
* @test
*/
public function test_assign_accounts_to_missing_group_returns_not_found()
{
2021-12-02 13:15:53 +01:00
$accounts = TwoFAccount::factory()->count(2)->create();
2021-11-14 01:52:46 +01:00
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('POST', '/api/v1/groups/1000/assign', [
2022-04-01 13:35:59 +02:00
'ids' => [$accounts[0]->id, $accounts[1]->id],
])
2021-11-14 01:52:46 +01:00
->assertNotFound()
->assertJsonStructure([
2022-11-22 15:15:52 +01:00
'message',
2021-11-14 01:52:46 +01:00
]);
}
/**
* @test
*/
public function test_assign_invalid_accounts_returns_validation_error()
{
2022-11-22 15:15:52 +01:00
$group = Group::factory()->create();
2021-12-02 13:15:53 +01:00
$accounts = TwoFAccount::factory()->count(2)->create();
2021-11-14 01:52:46 +01:00
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
'ids' => 1,
])
2021-11-14 01:52:46 +01:00
->assertStatus(422);
}
/**
* @test
*/
public function test_get_assigned_accounts_returns_twofaccounts_collection()
{
2022-11-22 15:15:52 +01:00
$group = Group::factory()->create();
2021-12-02 13:15:53 +01:00
$accounts = TwoFAccount::factory()->count(2)->create();
2021-11-14 01:52:46 +01:00
2022-03-31 08:38:35 +02:00
$assign = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
2022-04-01 13:35:59 +02:00
'ids' => [$accounts[0]->id, $accounts[1]->id],
2021-11-14 01:52:46 +01:00
]);
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('GET', '/api/v1/groups/' . $group->id . '/twofaccounts')
->assertOk()
->assertJsonCount(2)
->assertJsonStructure([
'*' => [
'group_id',
'service',
'account',
'icon',
'otp_type',
'digits',
'algorithm',
'period',
2022-11-22 15:15:52 +01:00
'counter',
],
2021-11-14 01:52:46 +01:00
]);
}
/**
* @test
*/
public function test_get_assigned_accounts_returns_twofaccounts_collection_with_secret()
{
2022-11-22 15:15:52 +01:00
$group = Group::factory()->create();
2021-12-02 13:15:53 +01:00
$accounts = TwoFAccount::factory()->count(2)->create();
2021-11-14 01:52:46 +01:00
2022-03-31 08:38:35 +02:00
$assign = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
2022-04-01 13:35:59 +02:00
'ids' => [$accounts[0]->id, $accounts[1]->id],
2021-11-14 01:52:46 +01:00
]);
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('GET', '/api/v1/groups/' . $group->id . '/twofaccounts?withSecret=1')
->assertOk()
->assertJsonCount(2)
->assertJsonStructure([
'*' => [
'group_id',
'service',
'account',
'icon',
'secret',
'otp_type',
'digits',
'algorithm',
'period',
2022-11-22 15:15:52 +01:00
'counter',
],
2021-11-14 01:52:46 +01:00
]);
}
/**
* @test
*/
public function test_get_assigned_accounts_of_missing_group_returns_not_found()
{
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('GET', '/api/v1/groups/1000/twofaccounts')
->assertNotFound()
->assertJsonStructure([
2022-11-22 15:15:52 +01:00
'message',
2021-11-14 01:52:46 +01:00
]);
}
/**
* test Group deletion via API
*
* @test
*/
public function test_destroy_group_returns_success()
{
2021-12-02 13:15:53 +01:00
$group = Group::factory()->create();
2021-11-14 01:52:46 +01:00
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('DELETE', '/api/v1/groups/' . $group->id)
->assertNoContent();
}
/**
* test Group deletion via API
*
* @test
*/
public function test_destroy_missing_group_returns_not_found()
{
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-14 01:52:46 +01:00
->json('DELETE', '/api/v1/groups/1000')
->assertNotFound()
->assertJsonStructure([
2022-11-22 15:15:52 +01:00
'message',
2021-11-14 01:52:46 +01:00
]);
}
}