You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.6 KiB
85 lines
2.6 KiB
<?php
|
|
namespace App\Tests;
|
|
|
|
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
|
|
use App\Factory\DojoFactory;
|
|
use App\Factory\UserFactory;
|
|
use App\Repository\UserRepository;
|
|
use Zenstruck\Foundry\Test\Factories;
|
|
use Zenstruck\Foundry\Test\ResetDatabase;
|
|
use DateTimeImmutable;
|
|
use DateTimeZone;
|
|
|
|
class DojoTest extends ApiTestCase
|
|
{
|
|
// This trait provided by Foundry will take care of refreshing the database content to a known state before each test
|
|
use ResetDatabase, Factories;
|
|
|
|
private function generateAuthToken(string $authName)
|
|
{
|
|
$sign_seed = sodium_base642bin($_ENV['AUTH_SEED'], SODIUM_BASE64_VARIANT_ORIGINAL);
|
|
$sign_pair = sodium_crypto_sign_seed_keypair($sign_seed);
|
|
$sign_secret = sodium_crypto_sign_secretkey($sign_pair);
|
|
$now = new DateTimeImmutable("now", new DateTimeZone("UTC"));
|
|
$message = $authName . "|" . $now->format("c");
|
|
|
|
return sodium_bin2base64(sodium_crypto_sign($message, $sign_secret), SODIUM_BASE64_VARIANT_URLSAFE);
|
|
}
|
|
|
|
/**
|
|
* Requirement: A user should be able to create a dojo!
|
|
*/
|
|
public function testCreateDojo(): void
|
|
{
|
|
$userName = "FooBarFigher";
|
|
$dojoName = "BigFightDojo";
|
|
$userRepository = $this->getContainer()->get(UserRepository::class);
|
|
|
|
$this->assertCount(0, $userRepository->findByAuthName($userName));
|
|
|
|
static::createClient()->request('POST', '/api/dojos',
|
|
[
|
|
'headers' => [
|
|
'accept' => 'application/json',
|
|
'X-AUTH-TOKEN' => $this->generateAuthToken($userName)
|
|
],
|
|
'json' => [
|
|
'name' => $dojoName
|
|
]
|
|
]);
|
|
|
|
$this->assertResponseStatusCodeSame(201);
|
|
|
|
$this->assertCount(1, $userRepository->findByAuthName($userName));
|
|
}
|
|
|
|
/**
|
|
* Requirement: A user should NOT be able to create more than one dojos!
|
|
*/
|
|
public function testUserCannotCreateMultipleDojos(): void
|
|
{
|
|
$userName = "FooBarFigher";
|
|
$dojoName = "BigFightDojo";
|
|
DojoFactory::createOne([
|
|
'name' => $dojoName,
|
|
'owner' => UserFactory::createOne([
|
|
'authName' => $userName
|
|
])
|
|
]);
|
|
|
|
static::createClient()->request('POST', '/api/dojos',
|
|
[
|
|
'headers' => [
|
|
'accept' => 'application/json',
|
|
'X-AUTH-TOKEN' => $this->generateAuthToken($userName)
|
|
],
|
|
'json' => [
|
|
'name' => $dojoName
|
|
]
|
|
]);
|
|
|
|
$this->assertResponseStatusCodeSame(409); // 409 Conflict
|
|
}
|
|
}
|
|
|