forked from ecamp/ecamp3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for accessing personal invitations
- Loading branch information
1 parent
31560d6
commit d5019d9
Showing
12 changed files
with
1,057 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
api/tests/Api/PersonalInvitations/AcceptPersonalInvitationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
api/tests/Api/PersonalInvitations/DeletePersonalInvitationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
69
api/tests/Api/PersonalInvitations/FindPersonalInvitationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
api/tests/Api/PersonalInvitations/PersonalInvitationGraphQLTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
], | ||
]); | ||
} | ||
} |
Oops, something went wrong.