Skip to content

Commit

Permalink
Add tests for accessing personal invitations
Browse files Browse the repository at this point in the history
  • Loading branch information
carlobeltrame committed Apr 3, 2024
1 parent 31560d6 commit d5019d9
Show file tree
Hide file tree
Showing 12 changed files with 1,057 additions and 2 deletions.
4 changes: 2 additions & 2 deletions api/tests/Api/Invitations/AcceptInvitationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

use function PHPUnit\Framework\assertThat;
use function PHPUnit\Framework\greaterThanOrEqual;
use function PHPUnit\Framework\lessThanOrEqual;

/**
* @internal
Expand Down Expand Up @@ -60,7 +60,7 @@ public function testAcceptInvitationDoesNotHitDBWhenNotLoggedIn() {
* SAVEPOINT
* RELEASE SAVEPOINT
*/
assertThat($collector->getQueryCount(), greaterThanOrEqual(3));
assertThat($collector->getQueryCount(), lessThanOrEqual(3));
}

/**
Expand Down
202 changes: 202 additions & 0 deletions api/tests/Api/PersonalInvitations/AcceptPersonalInvitationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php

namespace App\Tests\Api\PersonalInvitations;

use App\DTO\PersonalInvitation;
use App\Entity\CampCollaboration;
use App\Entity\Profile;
use App\Tests\Api\ECampApiTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

use function PHPUnit\Framework\assertThat;
use function PHPUnit\Framework\lessThanOrEqual;

/**
* @internal
*/
class AcceptPersonalInvitationTest extends ECampApiTestCase {
/**
* @throws TransportExceptionInterface
*/
public function testAcceptPersonalInvitationFailsWhenNotLoggedIn() {
/** @var CampCollaboration $campCollaboration */
$campCollaboration = static::getFixture('campCollaboration6invitedWithUser');
static::createBasicClient()->request(
'PATCH',
"/personal_invitations/{$campCollaboration->getId()}/".PersonalInvitation::ACCEPT,
[
'json' => [],
'headers' => ['Content-Type' => 'application/merge-patch+json'],
]
);
$this->assertResponseStatusCodeSame(401);
}

/**
* @throws TransportExceptionInterface
*/
public function testAcceptPersonalInvitationDoesNotHitDBWhenNotLoggedIn() {
/** @var CampCollaboration $campCollaboration */
$campCollaboration = static::getFixture('campCollaboration6invitedWithUser');
$client = static::createBasicClient();
$client->enableProfiler();
$client->request(
'PATCH',
"/personal_invitations/{$campCollaboration->getId()}/".PersonalInvitation::ACCEPT,
[
'json' => [],
'headers' => ['Content-Type' => 'application/merge-patch+json'],
]
);

$collector = $client->getProfile()->getCollector('db');
/*
* 3 is:
* BEGIN TRANSACTION
* SAVEPOINT
* RELEASE SAVEPOINT
*/
assertThat($collector->getQueryCount(), lessThanOrEqual(3));
}

/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testAcceptPersonalInvitationSuccess() {
/** @var CampCollaboration $campCollaboration */
$campCollaboration = static::getFixture('campCollaboration6invitedWithUser');

/** @var Profile $profile */
$profile = static::getFixture('profile6invited');
static::createClientWithCredentials(['email' => $profile->email])->request(
'PATCH',
"/personal_invitations/{$campCollaboration->getId()}/".PersonalInvitation::ACCEPT,
[
'json' => [],
'headers' => ['Content-Type' => 'application/merge-patch+json'],
]
);
$this->assertResponseStatusCodeSame(200);
$this->assertJsonContains([
'_links' => [
'self' => ['href' => "/personal_invitations/{$campCollaboration->getId()}"],
],
]);
}

/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testCannotFindPersonalInvitationAfterSuccessfulAccept() {
/** @var CampCollaboration $campCollaboration */
$campCollaboration = static::getFixture('campCollaboration6invitedWithUser');

/** @var Profile $profile */
$profile = static::getFixture('profile6invited');
$client = static::createClientWithCredentials(['email' => $profile->email]);
$client->disableReboot();
$client->request(
'PATCH',
"/personal_invitations/{$campCollaboration->getId()}/".PersonalInvitation::ACCEPT,
[
'json' => [],
'headers' => ['Content-Type' => 'application/merge-patch+json'],
]
);
$this->assertResponseStatusCodeSame(200);
$this->assertJsonContains([
/*
'id' => $campCollaboration->getId(),
'campId' => $campCollaboration->camp->getId(),
'campTitle' => $campCollaboration->camp->title, */
'_links' => [
'self' => ['href' => "/personal_invitations/{$campCollaboration->getId()}"],
],
]);

$client->request('GET', "/personal_invitations/{$campCollaboration->getId()}");
$this->assertResponseStatusCodeSame(404);
}

/**
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testAcceptPersonalInvitationFailsWithExtraAttribute() {
/** @var CampCollaboration $campCollaboration */
$campCollaboration = static::getFixture('campCollaboration6invitedWithUser');

/** @var Profile $profile */
$profile = static::getFixture('profile6invited');
static::createClientWithCredentials(['email' => $profile->email])->request(
'PATCH',
"/personal_invitations/{$campCollaboration->getId()}/".PersonalInvitation::ACCEPT,
[
'json' => [
'userAlreadyInCamp' => true,
],
'headers' => ['Content-Type' => 'application/merge-patch+json'],
]
);
$this->assertResponseStatusCodeSame(400);
}

/**
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
#[DataProvider('invalidMethods')]
public function testInvalidRequestWhenWrongMethod(string $method) {
/** @var CampCollaboration $campCollaboration */
$campCollaboration = static::getFixture('campCollaboration6invitedWithUser');
static::createClientWithCredentials()->request($method, "/personal_invitations/{$campCollaboration->getId()}/".PersonalInvitation::ACCEPT);
$this->assertResponseStatusCodeSame(405);
}

public static function invalidMethods(): array {
return ['GET' => ['GET'], 'PUT' => ['PUT'], 'POST' => ['POST'], 'DELETE' => ['DELETE'], 'OPTIONS' => ['OPTIONS']];
}

/**
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public function testNotFoundWhenIdDoesNotMatch() {
/** @var Profile $profile */
$profile = static::getFixture('profile6invited');
static::createClientWithCredentials(['email' => $profile->email])->request('PATCH', '/personal_invitations/notExisting/'.PersonalInvitation::ACCEPT);
$this->assertResponseStatusCodeSame(404);
}

/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function testMethodNotAllowedWhenNoId() {
/** @var Profile $profile */
$profile = static::getFixture('profile6invited');
static::createClientWithCredentials(['email' => $profile->email])->request('PATCH', '/personal_invitations/'.PersonalInvitation::ACCEPT);
$this->assertResponseStatusCodeSame(405);
}
}
23 changes: 23 additions & 0 deletions api/tests/Api/PersonalInvitations/DeletePersonalInvitationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Tests\Api\PersonalInvitations;

use App\Entity\CampCollaboration;
use App\Entity\Profile;
use App\Tests\Api\ECampApiTestCase;

/**
* @internal
*/
class DeletePersonalInvitationTest extends ECampApiTestCase {
public function testDeleteIsNotAllowed() {
/** @var CampCollaboration $campCollaboration */
$campCollaboration = static::getFixture('campCollaboration2invitedCampUnrelated');

/** @var Profile $profile */
$profile = static::getFixture('profile6invited');
static::createClientWithCredentials(['email' => $profile->email])->request('DELETE', '/invitations/'.$campCollaboration->getId());

$this->assertResponseStatusCodeSame(404);
}
}
69 changes: 69 additions & 0 deletions api/tests/Api/PersonalInvitations/FindPersonalInvitationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Tests\Api\PersonalInvitations;

use App\Entity\CampCollaboration;
use App\Entity\Profile;
use App\Tests\Api\ECampApiTestCase;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

/**
* @internal
*/
class FindPersonalInvitationTest extends ECampApiTestCase {
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function testDoesNotFindPersonalInvitationWhenNotLoggedIn() {
/** @var CampCollaboration $campCollaboration */
$campCollaboration = static::getFixture('campCollaboration6invitedWithUser');
static::createBasicClient()->request('GET', "/personal_invitations/{$campCollaboration->getId()}");
$this->assertResponseStatusCodeSame(401);
}

/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testFindOwnPersonalInvitationWhenLoggedIn() {
/** @var CampCollaboration $campCollaboration */
$campCollaboration = static::getFixture('campCollaboration6invitedWithUser');

/** @var Profile $profile */
$profile = static::getFixture('profile6invited');
static::createClientWithCredentials(['email' => $profile->email])->request('GET', "/personal_invitations/{$campCollaboration->getId()}");
$this->assertResponseStatusCodeSame(200);
$this->assertJsonContains([
'id' => $campCollaboration->getId(),
'campId' => $campCollaboration->camp->getId(),
'campTitle' => $campCollaboration->camp->title,
'_links' => [
'self' => ['href' => "/personal_invitations/{$campCollaboration->getId()}"],
],
]);
}

/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testDoesNotFindOtherPersonalInvitationWhenLoggedIn() {
/** @var CampCollaboration $campCollaboration */
$campCollaboration = static::getFixture('campCollaboration6invitedWithUser');
static::createClientWithCredentials()->request('GET', "/personal_invitations/{$campCollaboration->getId()}");
$this->assertResponseStatusCodeSame(404);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Tests\Api\PersonalInvitations;

use App\Entity\CampCollaboration;
use App\Entity\Profile;
use App\Tests\Api\ECampApiTestCase;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

/**
* @internal
*/
class PersonalInvitationGraphQLTest extends ECampApiTestCase {
/**
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public function testFindPersonalInvitationWhenNotLoggedIn() {
/** @var CampCollaboration $campCollaboration */
$campCollaboration = static::getFixture('campCollaboration6invitedWithUser');
$query = "
{
personalInvitation(id: \"personal_invitations/{$campCollaboration->getId()}\") {
id
campTitle
campId
}
}
";

static::createClient()->request('GET', '/graphql?'.http_build_query(['query' => $query]));

$this->assertResponseStatusCodeSame(401);
}

/**
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public function testFindPersonalInvitationWhenLoggedIn() {
/** @var CampCollaboration $campCollaboration */
$campCollaboration = static::getFixture('campCollaboration6invitedWithUser');
$query = "
{
personalInvitation(id: \"personal_invitations/{$campCollaboration->getId()}\") {
id
campTitle
campId
}
}
";

/** @var Profile $profile */
$profile = static::getFixture('profile6invited');
static::createClientWithCredentials(['email' => $profile->email])->request('GET', '/graphql?'.http_build_query(['query' => $query]));

$this->assertResponseStatusCodeSame(200);
$this->assertJsonContains([
'data' => [
'personalInvitation' => [
'id' => '/personal_invitations/'.$campCollaboration->getId(),
'campId' => $campCollaboration->camp->getId(),
'campTitle' => $campCollaboration->camp->title,
],
],
]);
}
}
Loading

0 comments on commit d5019d9

Please sign in to comment.