Skip to content

Commit

Permalink
Better and easier to user makeAllLoggedRequests test + update Activit…
Browse files Browse the repository at this point in the history
…yTest
  • Loading branch information
froozeify committed Nov 22, 2024
1 parent 74e0c44 commit 25b3ea6
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 108 deletions.
38 changes: 31 additions & 7 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ApiPlatform\Symfony\Bundle\Test\Client;
use App\Enum\ClubRole;
use App\Enum\UserRole;
use App\Tests\Enum\ResponseCodeEnum;
use JetBrains\PhpStorm\NoReturn;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -71,28 +72,51 @@ protected function loggedAs(string $email, string $password): bool {
return true;
}

public function makeAllLoggedRequests(\Closure $requestFunction, bool $excludeClub2 = false): void {
private function checkRequestResponse(ResponseCodeEnum $responseCode, ?array $payloadToValidate): void {
$this->assertResponseStatusCodeSame($responseCode->value);

if ($responseCode->isSuccess()) {
if ($payloadToValidate) {
$this->assertJsonContains($payloadToValidate);
}
}
}

public function makeAllLoggedRequests(
?array $payloadToValidate = null,
ResponseCodeEnum $memberClub1Code = ResponseCodeEnum::forbidden,
ResponseCodeEnum $supervisorClub1Code = ResponseCodeEnum::ok,
ResponseCodeEnum $adminClub1Code = ResponseCodeEnum::ok,
?ResponseCodeEnum $adminClub2Code = ResponseCodeEnum::not_found,
ResponseCodeEnum $superAdminCode = ResponseCodeEnum::ok,
?\Closure $requestFunction = null
): void {
// Super admin
$this->loggedAsSuperAdmin();
$requestFunction(UserRole::super_admin->value, null);
if ($requestFunction) $requestFunction(UserRole::super_admin->value, null);
$this->checkRequestResponse($superAdminCode, $payloadToValidate);

// Admin club 1
$this->loggedAsAdminClub1();
$requestFunction(ClubRole::admin->value, 1);
if ($requestFunction) $requestFunction(ClubRole::admin->value, 1);
$this->checkRequestResponse($adminClub1Code, $payloadToValidate);

// Supervisor club 1
$this->loggedAsSupervisorClub1();
$requestFunction(ClubRole::supervisor->value, 1);
if ($requestFunction) $requestFunction(ClubRole::supervisor->value, 1);
$this->checkRequestResponse($supervisorClub1Code, $payloadToValidate);

// member club 1
$this->loggedAsMemberClub1();
$requestFunction(ClubRole::member->value, 1);
if ($requestFunction) $requestFunction(ClubRole::member->value, 1);
$this->checkRequestResponse($memberClub1Code, $payloadToValidate);

// Club 2
if (!$excludeClub2) {
if ($adminClub2Code) {
// Admin club 2
$this->loggedAsAdminClub2();
$requestFunction(ClubRole::admin->value, 2);
if ($requestFunction) $requestFunction(ClubRole::admin->value, 2);
$this->checkRequestResponse($adminClub2Code, $payloadToValidate);
}
}

Expand Down
12 changes: 9 additions & 3 deletions tests/Controller/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
namespace App\Tests\Controller;

use App\Tests\AbstractTestCase;
use App\Tests\Enum\ResponseCodeEnum;
use App\Tests\Factory\UserFactory;
use Symfony\Component\HttpFoundation\Response;

class LoginTest extends AbstractTestCase {
public function testSuccessful(): void {
$this->makeAllLoggedRequests(function () {
$this->assertResponseIsSuccessful();
});
$this->makeAllLoggedRequests(
null,
ResponseCodeEnum::ok,
ResponseCodeEnum::ok,
ResponseCodeEnum::ok,
ResponseCodeEnum::ok,
ResponseCodeEnum::ok
);
}

public function testLoginAsNotActivated(): void {
Expand Down
84 changes: 38 additions & 46 deletions tests/Entity/ClubDependent/ActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use App\Entity\ClubDependent\Activity;
use App\Enum\ClubRole;
use App\Enum\UserRole;
use App\Tests\Entity\Abstract\AbstractEntityClubLinkedTestCase;
use App\Tests\Enum\ResponseCodeEnum;
use App\Tests\Factory\ActivityFactory;
use App\Tests\Story\ActivityStory;
use App\Tests\Story\InitStory;
use Symfony\Component\HttpFoundation\Response;

class ActivityTest extends AbstractEntityClubLinkedTestCase {
protected int $TOTAL_SUPER_ADMIN = 9;
protected int $TOTAL_SUPER_ADMIN = 9; // 9 = count ActivityStory::activities_club1
protected int $TOTAL_ADMIN_CLUB_1 = 9;
protected int $TOTAL_ADMIN_CLUB_2 = 1;
protected int $TOTAL_SUPERVISOR_CLUB_1 = 9;
Expand Down Expand Up @@ -45,22 +45,22 @@ public function testCreate(): void {
"club" => $iri,
];

// Only super admin and Club Admin can make the request
$this->makeAllLoggedRequests(function (string $level, ?int $id) use ($payload) {
$this->makePostRequest($this->getRootUrl(), $payload);

// For the check we update the payload value
$payload["club"] = [
'@id' => $payload["club"],
];
$payloadCheck = $payload;
// For the check we update the payload value
$payloadCheck["club"] = [
'@id' => $payload["club"],
];

if (in_array($level, [UserRole::super_admin->value, ClubRole::admin->value])) {
$this->assertResponseIsSuccessful();
$this->assertJsonContains($payload);
} else {
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
$this->makeAllLoggedRequests(
$payloadCheck,
supervisorClub1Code: ResponseCodeEnum::forbidden,
adminClub1Code: ResponseCodeEnum::created,
adminClub2Code: ResponseCodeEnum::bad_request,
superAdminCode: ResponseCodeEnum::created,
requestFunction: function (string $level, ?int $id) use ($payload) {
$this->makePostRequest($this->getRootUrl(), $payload);
}
}, true);
);
}

public function testPatch(): void {
Expand All @@ -71,40 +71,32 @@ public function testPatch(): void {
"name" => 'Update activity'
];

// Only super admin and Club Admin can make the request
$this->makeAllLoggedRequests(function (string $level, ?int $id) use ($iri, $payload) {
$payload["name"] .= $id;
$this->makePatchRequest($iri, $payload);

if (in_array($level, [UserRole::super_admin->value, ClubRole::admin->value])) {
$this->assertResponseIsSuccessful();
$this->assertJsonContains($payload);
} else {
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
$this->makeAllLoggedRequests(
$payload,
supervisorClub1Code: ResponseCodeEnum::forbidden,
requestFunction: function (string $level, ?int $id) use ($iri, &$payload) {
$this->makePatchRequest($iri, $payload);
}
}, true);
);
}

public function testDelete(): void {
$activity = ActivityStory::getRandom("activities_club1");
$iri = $this->getIriFromResource($activity);

// Admin club 2 can't delete
$this->loggedAsAdminClub2();
$this->makeDeleteRequest($iri);
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);

// Supervisor club 1 can't
$this->loggedAsSupervisorClub1();
$this->makeDeleteRequest($iri);
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);

// Admin club 1 can
$this->loggedAsAdminClub1();
$this->makeDeleteRequest($iri);
$this->assertResponseIsSuccessful();
$this->makeAllLoggedRequests(
null,
supervisorClub1Code: ResponseCodeEnum::forbidden,
adminClub1Code: ResponseCodeEnum::no_content,
superAdminCode: ResponseCodeEnum::no_content,
requestFunction: function (string $level, ?int $id) {
$activity = ActivityFactory::createOne([
'name' => 'Test activity to remove',
'club' => InitStory::club_1()
]);
$iri = $this->getIriFromResource($activity);

$this->makeDeleteRequest($iri);
}
);
}

// TODO: Add activity merge test

}
109 changes: 57 additions & 52 deletions tests/Entity/ClubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace App\Tests\Entity;

use App\Entity\Club;
use App\Enum\ClubRole;
use App\Enum\UserRole;
use App\Tests\Entity\Abstract\AbstractEntityTestCase;
use App\Tests\Enum\ResponseCodeEnum;
use App\Tests\Factory\ClubFactory;
use App\Tests\Story\InitStory;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -27,16 +26,17 @@ public function testCreate(): void {
];

// Only super admin can create
$this->makeAllLoggedRequests(function (string $level, ?int $id) use ($payload) {
$this->makePostRequest($this->getRootUrl(), $payload);

if ($level === UserRole::super_admin->value) {
$this->assertResponseIsSuccessful();
$this->assertJsonContains($payload);
} else {
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
$this->makeAllLoggedRequests(
$payload,
ResponseCodeEnum::forbidden,
ResponseCodeEnum::forbidden,
ResponseCodeEnum::forbidden,
ResponseCodeEnum::forbidden,
ResponseCodeEnum::created,
requestFunction: function (string $level, ?int $id) use ($payload) {
$this->makePostRequest($this->getRootUrl(), $payload);
}
});
);

}

Expand All @@ -49,41 +49,35 @@ public function testPatch(): void {
];

// Only super admin can update
$this->makeAllLoggedRequests(function (string $level, ?int $club) use ($iri, $payload) {
$this->makePatchRequest($iri, $payload);

if ($level === UserRole::super_admin->value) {
$this->assertResponseIsSuccessful();
$this->assertJsonContains($payload);
} else {
if ($club === 1) {
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
} else {
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
}
$this->makeAllLoggedRequests(
$payload,
supervisorClub1Code: ResponseCodeEnum::forbidden,
adminClub1Code: ResponseCodeEnum::forbidden,
requestFunction: function (string $level, ?int $id) use ($iri, $payload) {
$this->makePatchRequest($iri, $payload);
}
});
);
}

public function testDelete(): void {
$club = ClubFactory::createOne([
'name' => 'Club to delete',
'salesEnabled' => true,
'smtpEnabled' => true,
'badgerToken' => 'clubbadger',
]);
$iri = $this->getIriFromResource($club);

// Only super admin can delete
$this->makeAllLoggedRequests(function (string $level, ?int $club) use ($iri) {
$this->makeDeleteRequest($iri);

if ($level === UserRole::super_admin->value) {
$this->assertResponseIsSuccessful();
} else {
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
$this->makeAllLoggedRequests(
null,
ResponseCodeEnum::not_found,
ResponseCodeEnum::not_found,
ResponseCodeEnum::not_found,
superAdminCode: ResponseCodeEnum::no_content,
requestFunction: function (string $level, ?int $id) {
$club = ClubFactory::createOne([
'name' => 'Club to delete',
'salesEnabled' => true,
'smtpEnabled' => true,
'badgerToken' => 'clubbadger',
]);
$iri = $this->getIriFromResource($club);
$this->makeDeleteRequest($iri);
}
});
);
}

public function testCascadeDelete(): void {
Expand Down Expand Up @@ -114,18 +108,29 @@ public function testBadgerTokenFieldVisibility(): void {
$club1 = InitStory::club_1();
$iri = $this->getIriFromResource($club1);

$this->makeAllLoggedRequests(function (string $level, ?int $id) use ($iri, $club1) {
$response = $this->makeGetRequest($iri);
$this->assertResponseIsSuccessful();
$payloadMatch = [
'@id' => $iri,
'badgerToken' => $club1->getBadgerToken(),
];

if (in_array($level, [UserRole::super_admin->value, ClubRole::admin->value])) {
$this->assertJsonContains([
'@id' => $iri,
'badgerToken' => $club1->getBadgerToken(),
]);
} else {
$this->assertJsonNotHasKey('badgerToken', $response);
}
}, true);
$success = [
'loggedAsSuperAdmin',
'loggedAsAdminClub1',
];
foreach ($success as $function) {
$this->$function();
$this->makeGetRequest($iri);
$this->assertJsonContains($payloadMatch);
}

$denied = [
'loggedAsSupervisorClub1',
'loggedAsMemberClub1',
];
foreach ($denied as $function) {
$this->$function();
$response = $this->makeGetRequest($iri);
$this->assertJsonNotHasKey('badgerToken', $response);
}
}
}
20 changes: 20 additions & 0 deletions tests/Enum/ResponseCodeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Tests\Enum;

use Symfony\Component\HttpFoundation\Response;

enum ResponseCodeEnum: int {
case created = Response::HTTP_CREATED;
case ok = Response::HTTP_OK;
case no_content = Response::HTTP_NO_CONTENT;

case not_found = Response::HTTP_NOT_FOUND;
case not_allowed = Response::HTTP_METHOD_NOT_ALLOWED;
case bad_request = Response::HTTP_BAD_REQUEST;
case forbidden = Response::HTTP_FORBIDDEN;

public function isSuccess(): bool {
return ($this->value >= 200 && $this->value <= 299);
}
}

0 comments on commit 25b3ea6

Please sign in to comment.