From 79c17ca0bef39f93eee146b7e3c886a3cd885419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Skowro=C5=84ski?= Date: Sat, 30 Dec 2023 14:58:39 +0100 Subject: [PATCH] Add attendance api tests --- .../Attendance/AttendanceFixtures.php | 43 ++++- .../Api/Attendance/CreateAttendanceCest.php | 114 ++++++++++++ .../Api/Attendance/ListAttendancesCest.php | 162 ++++++++++++++++++ tests/functional/Web/Api/IndexCest.php | 45 +++++ 4 files changed, 360 insertions(+), 4 deletions(-) create mode 100644 tests/functional/Api/Attendance/CreateAttendanceCest.php create mode 100644 tests/functional/Api/Attendance/ListAttendancesCest.php create mode 100644 tests/functional/Web/Api/IndexCest.php diff --git a/src/DataFixtures/Attendance/AttendanceFixtures.php b/src/DataFixtures/Attendance/AttendanceFixtures.php index b9f19140..d4da21b9 100644 --- a/src/DataFixtures/Attendance/AttendanceFixtures.php +++ b/src/DataFixtures/Attendance/AttendanceFixtures.php @@ -5,19 +5,54 @@ namespace App\DataFixtures\Attendance; use App\Entity\Attendance\Attendance; +use App\Test\Traits\TimeTrait; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Persistence\ObjectManager; use Ramsey\Uuid\Uuid; class AttendanceFixtures extends Fixture { + use TimeTrait; + public function load(ObjectManager $manager): void { - for ($i = 1; $i <= 10; ++$i) { - $attendance = new Attendance(Uuid::uuid4(), "mission_{$i}", 76561198048200529); + $this->withTimeFrozenAt('2020-01-01T00:00:00+00:00', function () use ($manager): void { + $attendance = new Attendance( + Uuid::fromString('2694264f-0e6f-40a6-9596-bc22d1eaa2c7'), + 'mission_1', + 76561198048200529 + ); + $manager->persist($attendance); + + $attendance = new Attendance( + Uuid::fromString('d2a60cd4-d3f0-497a-9840-69fa5c388f1e'), + 'mission_2', + 76561198048200529 + ); + $manager->persist($attendance); + + $attendance = new Attendance( + Uuid::fromString('0a918bf1-d20f-4856-aa0d-f093f6e90bf7'), + 'mission_3', + 76561198048200529 + ); + $manager->persist($attendance); + + $attendance = new Attendance( + Uuid::fromString('2a3aa170-3a79-471f-b6ec-e194bf8d6c9e'), + 'mission_4', + 76561198048200529 + ); + $manager->persist($attendance); + + $attendance = new Attendance( + Uuid::fromString('ac22546b-9d5c-4a54-a713-01dcc5ee40e6'), + 'mission_5', + 76561198048200529 + ); $manager->persist($attendance); - } - $manager->flush(); + $manager->flush(); + }); } } diff --git a/tests/functional/Api/Attendance/CreateAttendanceCest.php b/tests/functional/Api/Attendance/CreateAttendanceCest.php new file mode 100644 index 00000000..da95a8fb --- /dev/null +++ b/tests/functional/Api/Attendance/CreateAttendanceCest.php @@ -0,0 +1,114 @@ +haveHttpHeader('Accept', 'application/json'); + $I->haveHttpHeader('Content-Type', 'application/json'); + } + + public function createAttendanceWithoutApiKey(FunctionalTester $I): void + { + $I->sendPost('/api/attendances', [ + 'missionId' => 'mission_99', + 'playerId' => 76561198048200529, + ]); + + $I->seeResponseCodeIs(403); + $I->seeResponseContainsJson([ + 'detail' => 'Invalid or missing API key provided!', + ]); + + $I->dontSeeInRepository(Attendance::class, ['missionId' => 'mission_99']); + } + + public function createAttendanceUsingInvalidApiKey(FunctionalTester $I): void + { + $I->haveHttpHeader('X-API-KEY', 'invalid_key'); + $I->sendPost('/api/attendances', [ + 'missionId' => 'mission_99', + 'playerId' => 76561198048200529, + ]); + + $I->seeResponseCodeIs(403); + $I->seeResponseContainsJson([ + 'detail' => 'Invalid or missing API key provided!', + ]); + + $I->dontSeeInRepository(Attendance::class, ['missionId' => 'mission_99']); + } + + public function createAttendanceUsingApiKey(FunctionalTester $I): void + { + $I->haveHttpHeader('X-API-KEY', 'test_key'); + $I->sendPost('/api/attendances', [ + 'missionId' => 'mission_99', + 'playerId' => 76561198048200529, + ]); + + $I->seeResponseCodeIs(201); + + /** @var Attendance $attendance */ + $attendance = $I->grabEntityFromRepository(Attendance::class, ['missionId' => 'mission_99']); + $I->assertSame('mission_99', $attendance->getMissionId()); + $I->assertSame(76561198048200529, $attendance->getPlayerId()); + } + + public function createAttendanceUsingApiKeyWhenAttendanceAlreadyExist(FunctionalTester $I): void + { + $I->haveHttpHeader('X-API-KEY', 'test_key'); + $I->sendPost('/api/attendances', [ + 'missionId' => 'mission_1', + 'playerId' => 76561198048200529, + ]); + + $I->seeResponseCodeIs(422); + $I->seeResponseContainsJson([ + 'detail' => 'Attendance of player "76561198048200529" in mission "mission_1" already exists.', + ]); + + /** @var Attendance[] $attendances */ + $attendances = $I->grabEntitiesFromRepository(Attendance::class, ['missionId' => 'mission_1']); + $I->assertCount(1, $attendances); + } + + public function createAttendanceUsingApiKeyWithDataTooLong(FunctionalTester $I): void + { + $I->haveHttpHeader('X-API-KEY', 'test_key'); + $I->sendPost('/api/attendances', [ + 'missionId' => str_repeat('a', 256), + 'playerId' => 76561198048200529, + ]); + + $I->seeResponseCodeIs(422); + $I->seeResponseContainsJson([ + 'detail' => 'missionId: This value is too long. It should have 255 characters or less.', + ]); + + $I->dontSeeInRepository(Attendance::class, ['missionId' => 'mission_99']); + } + + public function createAttendanceUsingApiKeyWithInvalidPlayerId(FunctionalTester $I): void + { + $I->haveHttpHeader('X-API-KEY', 'test_key'); + $I->sendPost('/api/attendances', [ + 'missionId' => 'mission_99', + 'playerId' => 123, + ]); + + $I->seeResponseCodeIs(422); + $I->seeResponseContainsJson([ + 'detail' => 'playerId: Invalid Steam profile ID.', + ]); + + $I->dontSeeInRepository(Attendance::class, ['missionId' => 'mission_99']); + } +} diff --git a/tests/functional/Api/Attendance/ListAttendancesCest.php b/tests/functional/Api/Attendance/ListAttendancesCest.php new file mode 100644 index 00000000..8a4b075b --- /dev/null +++ b/tests/functional/Api/Attendance/ListAttendancesCest.php @@ -0,0 +1,162 @@ +haveHttpHeader('Accept', 'application/json'); + $I->haveHttpHeader('Content-Type', 'application/json'); + } + + public function listAttendancesWithoutApiKey(FunctionalTester $I): void + { + $I->sendGet('/api/attendances', [ + 'order[missionId]' => 'ASC', + ]); + + $I->seeResponseCodeIs(200); + $I->seeResponseContainsJson([ + 'data' => [ + [ + 'id' => '2694264f-0e6f-40a6-9596-bc22d1eaa2c7', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_1', + 'playerId' => 76561198048200529, + ], + [ + 'id' => 'd2a60cd4-d3f0-497a-9840-69fa5c388f1e', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_2', + 'playerId' => 76561198048200529, + ], + [ + 'id' => '0a918bf1-d20f-4856-aa0d-f093f6e90bf7', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_3', + 'playerId' => 76561198048200529, + ], + [ + 'id' => '2a3aa170-3a79-471f-b6ec-e194bf8d6c9e', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_4', + 'playerId' => 76561198048200529, + ], + [ + 'id' => 'ac22546b-9d5c-4a54-a713-01dcc5ee40e6', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_5', + 'playerId' => 76561198048200529, + ], + ], + 'items' => 5, + 'totalItems' => 5.0, + 'currentPage' => 1.0, + 'lastPage' => 1.0, + 'itemsPerPage' => 30.0, + ]); + } + + public function listAttendancesUsingInvalidApiKey(FunctionalTester $I): void + { + $I->haveHttpHeader('X-API-KEY', 'invalid_key'); + $I->sendGet('/api/attendances', [ + 'order[missionId]' => 'ASC', + ]); + + $I->seeResponseCodeIs(200); + $I->seeResponseContainsJson([ + 'data' => [ + [ + 'id' => '2694264f-0e6f-40a6-9596-bc22d1eaa2c7', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_1', + 'playerId' => 76561198048200529, + ], + [ + 'id' => 'd2a60cd4-d3f0-497a-9840-69fa5c388f1e', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_2', + 'playerId' => 76561198048200529, + ], + [ + 'id' => '0a918bf1-d20f-4856-aa0d-f093f6e90bf7', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_3', + 'playerId' => 76561198048200529, + ], + [ + 'id' => '2a3aa170-3a79-471f-b6ec-e194bf8d6c9e', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_4', + 'playerId' => 76561198048200529, + ], + [ + 'id' => 'ac22546b-9d5c-4a54-a713-01dcc5ee40e6', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_5', + 'playerId' => 76561198048200529, + ], + ], + 'items' => 5, + 'totalItems' => 5.0, + 'currentPage' => 1.0, + 'lastPage' => 1.0, + 'itemsPerPage' => 30.0, + ]); + } + + public function listAttendancesUsingValidApiKey(FunctionalTester $I): void + { + $I->haveHttpHeader('X-API-KEY', 'test_key'); + $I->sendGet('/api/attendances', [ + 'order[missionId]' => 'ASC', + ]); + + $I->seeResponseCodeIs(200); + $I->seeResponseContainsJson([ + 'data' => [ + [ + 'id' => '2694264f-0e6f-40a6-9596-bc22d1eaa2c7', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_1', + 'playerId' => 76561198048200529, + ], + [ + 'id' => 'd2a60cd4-d3f0-497a-9840-69fa5c388f1e', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_2', + 'playerId' => 76561198048200529, + ], + [ + 'id' => '0a918bf1-d20f-4856-aa0d-f093f6e90bf7', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_3', + 'playerId' => 76561198048200529, + ], + [ + 'id' => '2a3aa170-3a79-471f-b6ec-e194bf8d6c9e', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_4', + 'playerId' => 76561198048200529, + ], + [ + 'id' => 'ac22546b-9d5c-4a54-a713-01dcc5ee40e6', + 'createdAt' => '2020-01-01T00:00:00+00:00', + 'missionId' => 'mission_5', + 'playerId' => 76561198048200529, + ], + ], + 'items' => 5, + 'totalItems' => 5.0, + 'currentPage' => 1.0, + 'lastPage' => 1.0, + 'itemsPerPage' => 30.0, + ]); + } +} diff --git a/tests/functional/Web/Api/IndexCest.php b/tests/functional/Web/Api/IndexCest.php new file mode 100644 index 00000000..5a3fddee --- /dev/null +++ b/tests/functional/Web/Api/IndexCest.php @@ -0,0 +1,45 @@ +haveHttpHeader('Accept', 'text/html'); + } + + public function visitApiDocsPageAsUnauthenticatedUser(FunctionalTester $I): void + { + $I->amOnPage('/api'); + + $I->seeResponseCodeIs(200); + $I->see('ArmaForces Website Web API'); + } + + public function visitApiDocsPageAsRegisteredUser(FunctionalTester $I): void + { + $I->amDiscordAuthenticatedAs(User1Fixture::ID); + + $I->amOnPage('/api'); + + $I->seeResponseCodeIs(200); + $I->see('ArmaForces Website Web API'); + } + + public function visitApiDocsPageAsRegisteredUserWithFullPermissions(FunctionalTester $I): void + { + $I->amDiscordAuthenticatedAs(AdminFixture::ID); + + $I->amOnPage('/api'); + + $I->seeResponseCodeIs(200); + $I->see('ArmaForces Website Web API'); + } +}