Skip to content

Commit

Permalink
Club tests improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
froozeify committed Nov 11, 2024
1 parent 6c2e067 commit 47f253f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 43 deletions.
27 changes: 27 additions & 0 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Symfony\Bundle\Test\Client;
use App\Enum\ClubRole;
use App\Enum\UserRole;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\ResponseInterface;
Expand Down Expand Up @@ -59,6 +61,31 @@ protected function loggedAs(string $email, string $password): bool {
return true;
}

public function makeAllLoggedRequests(\Closure $requestFunction, bool $excludeClub2 = false): void {
// Super admin
$this->loggedAsSuperAdmin();
$requestFunction(UserRole::super_admin->value, null);

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

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

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

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

public function loggedAsSuperAdmin(): bool {
return $this->loggedAs('admin@admin.com', 'admin123');
}
Expand Down
21 changes: 9 additions & 12 deletions tests/Controller/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@
namespace App\Tests\Controller;

use App\Tests\AbstractTestCase;
use Symfony\Component\HttpFoundation\Response;

class LoginTest extends AbstractTestCase {
public function testLoginAsSuperAdmin(): void {
$this->loggedAsSuperAdmin(); // We log as super admin
$this->assertResponseIsSuccessful();
public function testSuccessful(): void {
$this->makeAllLoggedRequests(function () {
$this->assertResponseIsSuccessful();
});
}

// public function testLoginAsClubAdmin(): void { }
//
// public function testLoginAsClubSupervisor(): void { }
//
// public function testLoginAsClubMember(): void { }
//
// TODO: Add login test as a not activated account, should fail
// public function testLoginAsNotActivated(): void { }

public function testLoginWithWrongPassword(): void {
$this->loggedAs("admin@admin.com", "wrong");
$this->assertResponseStatusCodeSame(401);
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);

// We request a PRIVATE resource
$this->makeGetRequest('/activities');
$this->assertResponseStatusCodeSame(401);
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
}

/**
Expand All @@ -34,6 +31,6 @@ public function testLoginWithWrongPassword(): void {
*/
public function testLoginAsUnknown(): void {
$this->loggedAs("notexisting@test.fr", "test");
$this->assertResponseStatusCodeSame(401);
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
}
}
62 changes: 31 additions & 31 deletions tests/Entity/ClubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace App\Tests\Entity;

use App\Entity\Club;
use App\Enum\ClubRole;
use App\Enum\UserRole;
use App\Tests\Story\InitStory;
use Symfony\Component\HttpFoundation\Response;

class ClubTest extends AbstractEntityTestCase {
protected int $TOTAL_SUPER_ADMIN = 5;
Expand All @@ -16,10 +19,23 @@ protected function getRootUri(): string {
return '/clubs';
}

// TODO: Create test to check only superadmin and club_admin can see the `badgerToken` field

public function testCreate(): void {
self::markTestSkipped('to implement');
$payload = [
"name" => 'Club de test',
];

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

if ($level === UserRole::super_admin->value) {
$this->assertResponseIsSuccessful();
$this->assertJsonContains($payload);
} else {
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
}
});

}

public function testPatch(): void {
Expand All @@ -34,34 +50,18 @@ public function testBadgerTokenFieldVisibility(): void {
$club1 = InitStory::club_1();
$iri = $this->getIriFromResource($club1);

// Super admin
$this->loggedAsSuperAdmin();
$this->makeGetRequest($iri);
self::assertResponseIsSuccessful();
self::assertJsonContains([
'@id' => $iri,
'badgerToken' => $club1->getBadgerToken(),
]);

// Admin club 1
$this->loggedAsAdminClub1();
$this->makeGetRequest($iri);
self::assertResponseIsSuccessful();
self::assertJsonContains([
'@id' => $iri,
'badgerToken' => $club1->getBadgerToken(),
]);

// Supervisor club 1
$this->loggedAsSupervisorClub1();
$response = $this->makeGetRequest($iri);
self::assertResponseIsSuccessful();
self::assertJsonNotHasKey('badgerToken', $response);
$this->makeAllLoggedRequests(function (string $level, ?int $id) use ($iri, $club1) {
$response = $this->makeGetRequest($iri);
$this->assertResponseIsSuccessful();

// Member
$this->loggedAsMemberClub1();
$response = $this->makeGetRequest($iri);
self::assertResponseIsSuccessful();
self::assertJsonNotHasKey('badgerToken', $response);
if (in_array($level, [UserRole::super_admin->value, ClubRole::admin->value])) {
$this->assertJsonContains([
'@id' => $iri,
'badgerToken' => $club1->getBadgerToken(),
]);
} else {
$this->assertJsonNotHasKey('badgerToken', $response);
}
}, true);
}
}

0 comments on commit 47f253f

Please sign in to comment.