domain-watchdog/tests/Controller/ConnectorControllerTest.php

128 lines
4.7 KiB
PHP
Raw Normal View History

2025-10-14 17:40:48 +02:00
<?php
namespace App\Tests\Controller;
2025-10-14 18:48:40 +02:00
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
2025-10-14 17:40:48 +02:00
use App\Entity\Connector;
use App\Factory\UserFactory;
use App\Message\ValidateConnectorCredentials;
use App\MessageHandler\ValidateConnectorCredentialsHandler;
2025-10-14 18:48:40 +02:00
use App\Tests\AuthenticatedUserTrait;
use PHPUnit\Framework\Attributes\Depends;
2025-10-14 17:40:48 +02:00
use Zenstruck\Foundry\Test\Factories;
2025-10-14 18:48:40 +02:00
final class ConnectorControllerTest extends ApiTestCase
2025-10-14 17:40:48 +02:00
{
use Factories;
2025-10-14 18:48:40 +02:00
use AuthenticatedUserTrait;
2025-10-14 17:40:48 +02:00
public function testGetConnectorCollection(): void
{
2025-10-16 02:31:08 +02:00
$client = ConnectorControllerTest::createClientWithCredentials(ConnectorControllerTest::getToken(UserFactory::createOne()));
2025-10-14 17:40:48 +02:00
$response = $client->request('GET', '/api/connectors');
$this->assertResponseIsSuccessful();
$this->assertMatchesResourceCollectionJsonSchema(Connector::class);
$this->assertCount(0, $response->toArray()['hydra:member']);
}
2025-10-16 02:31:08 +02:00
public function testCreateConnectorInvalidAuthData(): void
{
$client = ConnectorControllerTest::createClientWithCredentials(ConnectorControllerTest::getToken(UserFactory::createOne()));
$client->request('POST', '/api/connectors', ['json' => [
'authData' => [
'waiveRetractationPeriod' => true,
'acceptConditions' => true,
'ownerLegalAge' => true,
2025-10-17 12:39:40 +02:00
'token' => 'test',
2025-10-16 02:31:08 +02:00
],
'provider' => 'gandi',
]]);
$this->assertResponseStatusCodeSame(400);
}
public function testCreateConnectorInvalidConsent(): void
{
$client = ConnectorControllerTest::createClientWithCredentials(ConnectorControllerTest::getToken(UserFactory::createOne()));
$client->request('POST', '/api/connectors', ['json' => [
'authData' => [
'waiveRetractationPeriod' => true,
'acceptConditions' => true,
'ownerLegalAge' => false,
'token' => '',
],
'provider' => 'gandi',
]]);
$this->assertResponseStatusCodeSame(451);
}
public function testCreateConnectorInvalidAuthDataAdditionalKey(): void
{
$client = ConnectorControllerTest::createClientWithCredentials(ConnectorControllerTest::getToken(UserFactory::createOne()));
$client->request('POST', '/api/connectors', ['json' => [
'authData' => [
'waiveRetractationPeriod' => true,
'acceptConditions' => true,
'ownerLegalAge' => true,
'token' => '',
'unknownKey' => 'hello',
],
'provider' => 'gandi',
]]);
$this->assertResponseStatusCodeSame(400);
}
2025-10-17 12:39:40 +02:00
public function testCreateConnectorValidAuthData(): void
{
$gandiToken = static::getContainer()->getParameter('gandi_pat_token');
if (!$gandiToken) {
$this->markTestSkipped('Missing Gandi PAT token');
}
$client = ConnectorControllerTest::createClientWithCredentials(ConnectorControllerTest::getToken(UserFactory::createOne()));
$client->request('POST', '/api/connectors', ['json' => [
'authData' => [
'waiveRetractationPeriod' => true,
'acceptConditions' => true,
'ownerLegalAge' => true,
'token' => $gandiToken,
],
'provider' => 'gandi',
]]);
$this->assertResponseStatusCodeSame(201);
}
#[Depends('testCreateConnectorValidAuthData')]
public function testValidateConnectorCredentials()
{
$validateConnectorCredentialsHandler = self::getContainer()->get(ValidateConnectorCredentialsHandler::class);
$message = new ValidateConnectorCredentials();
$validateConnectorCredentialsHandler($message);
$this->expectNotToPerformAssertions();
}
2025-10-17 20:08:56 +02:00
public function testCreateAndDeleteConnector()
{
$gandiToken = static::getContainer()->getParameter('gandi_pat_token');
if (!$gandiToken) {
$this->markTestSkipped('Missing Gandi PAT token');
}
$client = ConnectorControllerTest::createClientWithCredentials(ConnectorControllerTest::getToken(UserFactory::createOne()));
$response = $client->request('POST', '/api/connectors', ['json' => [
'authData' => [
'waiveRetractationPeriod' => true,
'acceptConditions' => true,
'ownerLegalAge' => true,
'token' => $gandiToken,
],
'provider' => 'gandi',
]]);
$this->assertResponseStatusCodeSame(201);
$client->request('DELETE', '/api/connectors/'.$response->toArray()['id']);
$this->assertResponseStatusCodeSame(204);
}
2025-10-14 17:40:48 +02:00
}