Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure HTTP exception is thrown when API fails #1310

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

namespace OpenConext\EngineBlockBundle\Controller\Api;

use Exception;
use OpenConext\EngineBlock\Metadata\Entity\Assembler\MetadataAssemblerInterface;
use OpenConext\EngineBlock\Metadata\MetadataRepository\DoctrineMetadataPushRepository;
use OpenConext\EngineBlockBundle\Configuration\FeatureConfiguration;
use OpenConext\EngineBlockBundle\Configuration\FeatureConfigurationInterface;
use OpenConext\EngineBlockBundle\Http\Exception\ApiAccessDeniedHttpException;
use OpenConext\EngineBlockBundle\Http\Exception\ApiInternalServerErrorHttpException;
use OpenConext\EngineBlockBundle\Http\Exception\ApiMethodNotAllowedHttpException;
use OpenConext\EngineBlockBundle\Http\Exception\ApiNotFoundHttpException;
use OpenConext\EngineBlockBundle\Http\Exception\BadApiRequestHttpException;
Expand Down Expand Up @@ -113,7 +115,11 @@ public function pushConnectionsAction(Request $request)

unset($body);

$result = $this->repository->synchronize($roles);
try {
$result = $this->repository->synchronize($roles);
} catch (Exception $exception) {
throw new ApiInternalServerErrorHttpException('Unable to synchronize the assembled roles to the repository', $exception);
}

return new JsonResponse($result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function authentication_is_required_for_pushing_metadata()
*/
public function only_post_requests_are_allowed_when_pushing_metadata($invalidHttpMethod)
{
$client = $client = static::createClient([], [
$client = static::createClient([], [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems quite correct, but I'm curious why the strange construction with the double assignment was there in the first place?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that was a find and replace mess-up. Of course PHP is perfectly happy to do this. 🙄

'PHP_AUTH_USER' => $this->getContainer()->getParameter('api.users.metadataPush.username'),
'PHP_AUTH_PW' => $this->getContainer()->getParameter('api.users.metadataPush.password'),
]);
Expand All @@ -75,7 +75,7 @@ public function only_post_requests_are_allowed_when_pushing_metadata($invalidHtt
*/
public function cannot_push_metadata_if_feature_is_disabled()
{
$client = $client = static::createClient([], [
$client = static::createClient([], [
'PHP_AUTH_USER' => $this->getContainer()->getParameter('api.users.metadataPush.username'),
'PHP_AUTH_PW' => $this->getContainer()->getParameter('api.users.metadataPush.password'),
]);
Expand All @@ -97,7 +97,7 @@ public function cannot_push_metadata_if_feature_is_disabled()
*/
public function cannot_push_metadata_if_user_does_not_have_manage_role()
{
$client = $client = static::createClient([], [
$client = static::createClient([], [
'PHP_AUTH_USER' => 'no_roles',
'PHP_AUTH_PW' => 'no_roles',
]);
Expand All @@ -120,7 +120,7 @@ public function cannot_push_metadata_if_user_does_not_have_manage_role()
*/
public function cannot_push_invalid_content_to_the_metadata_push_api($invalidJsonPayload)
{
$client = $client = static::createClient([], [
$client = static::createClient([], [
'PHP_AUTH_USER' => $this->getContainer()->getParameter('api.users.metadataPush.username'),
'PHP_AUTH_PW' => $this->getContainer()->getParameter('api.users.metadataPush.password'),
]);
Expand Down Expand Up @@ -150,7 +150,7 @@ public function pushing_data_to_engineblock_should_succeed()
{
$this->clearMetadataFixtures();

$client = $client = static::createClient([], [
$client = static::createClient([], [
'PHP_AUTH_USER' => $this->getContainer()->getParameter('api.users.metadataPush.username'),
'PHP_AUTH_PW' => $this->getContainer()->getParameter('api.users.metadataPush.password'),
]);
Expand Down Expand Up @@ -219,7 +219,7 @@ public function pushing_data_with_coins_to_engineblock_should_succeed()
{
$this->clearMetadataFixtures();

$client = $client = static::createClient([], [
$client = static::createClient([], [
'PHP_AUTH_USER' => $this->getContainer()->getParameter('api.users.metadataPush.username'),
'PHP_AUTH_PW' => $this->getContainer()->getParameter('api.users.metadataPush.password'),
]);
Expand Down Expand Up @@ -273,7 +273,7 @@ public function pushing_manage_sfo_data_should_succeed()
{
$this->clearMetadataFixtures();

$client = $client = static::createClient([], [
$client = static::createClient([], [
'PHP_AUTH_USER' => $this->getContainer()->getParameter('api.users.metadataPush.username'),
'PHP_AUTH_PW' => $this->getContainer()->getParameter('api.users.metadataPush.password'),
]);
Expand Down Expand Up @@ -351,6 +351,42 @@ public function pushing_manage_sfo_data_should_succeed()
$this->assertFalse($emptyIdp->getCoins()->stepupConnections()->hasConnections());
}


/**
* @test
* @group Api
* @group Connections
* @group MetadataPush
*/
public function pushing_data_to_engineblock_can_fail()
{
$this->clearMetadataFixtures();

$client = static::createClient([], [
'PHP_AUTH_USER' => $this->getContainer()->getParameter('api.users.metadataPush.username'),
'PHP_AUTH_PW' => $this->getContainer()->getParameter('api.users.metadataPush.password'),
]);

// The second 'step' will overwrite the entity id for the one entry. It only changes some case, resulting in
// a sql error in Engine. That exception results in a 500 JSON response
foreach ($this->failingConnectionsData() as $expectedHttpCode => $step) {

$payload = $this->createJsonData($step);

$client->request(
'POST',
'https://engine-api.vm.openconext.org/api/connections',
[],
[],
[],
$payload
);
$this->assertStatusCode($expectedHttpCode, $client);
$this->assertJson($client->getResponse()->getContent());

}
}

public function invalidHttpMethodProvider()
{
return [
Expand Down Expand Up @@ -602,4 +638,27 @@ private function validConnectionsWithCoinsData()
],
];
}

private function failingConnectionsData()
{
return
[
200 => [
[
'uuid' => '00000000-0000-0000-0000-000000000000',
'entityId' => 'https://my-idp.test/1',
'name' => 'SP0',
'type' => 'saml20-sp',
],
],
500 => [
[
'uuid' => '00000000-0000-0000-0000-000000000000',
'entityId' => 'https://mY-iDp.test/1',
'name' => 'SP0',
'type' => 'saml20-sp',
],
]
];
}
}