Skip to content

Commit 789acf1

Browse files
authored
Merge pull request #172 from PrestaShop/restrictCampaign
Endpoint `/import/report/playwright` : Restrict the campaign
2 parents b5ad85e + f2708dc commit 789acf1

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

src/Controller/ImportController.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct(
5252
#[Route('/hook/reports/import', methods: ['GET'])]
5353
public function importReportMocha(Request $request): JsonResponse
5454
{
55-
$response = $this->checkAuth($request, ReportMochaImporter::FILTER_CAMPAIGNS);
55+
$response = $this->checkAuth($request, ReportMochaImporter::FILTER_CAMPAIGNS, true);
5656
if ($response instanceof JsonResponse) {
5757
return $response;
5858
}
@@ -75,7 +75,7 @@ public function importReportMocha(Request $request): JsonResponse
7575
#[Route('/import/report/playwright', methods: ['GET'])]
7676
public function importReportPlaywright(Request $request): JsonResponse
7777
{
78-
$response = $this->checkAuth($request, ReportPlaywrightImporter::FILTER_CAMPAIGNS);
78+
$response = $this->checkAuth($request, ReportPlaywrightImporter::FILTER_CAMPAIGNS, false);
7979
if ($response instanceof JsonResponse) {
8080
return $response;
8181
}
@@ -98,7 +98,7 @@ public function importReportPlaywright(Request $request): JsonResponse
9898
/**
9999
* @param array<string> $allowedCampaigns
100100
*/
101-
private function checkAuth(Request $request, array $allowedCampaigns): ?JsonResponse
101+
private function checkAuth(Request $request, array $allowedCampaigns, bool $forceCampaign): ?JsonResponse
102102
{
103103
$token = $request->query->get('token');
104104
$this->filename = $request->query->get('filename');
@@ -155,7 +155,19 @@ private function checkAuth(Request $request, array $allowedCampaigns): ?JsonResp
155155
$this->platform = in_array($this->platform, ReportMochaImporter::FILTER_PLATFORMS) ? $this->platform : ReportMochaImporter::FILTER_PLATFORMS[0];
156156

157157
$this->campaign = $request->query->has('campaign') ? $request->query->get('campaign') : null;
158-
$this->campaign = in_array($this->campaign, $allowedCampaigns) ? $this->campaign : $allowedCampaigns[0];
158+
if (!in_array($this->campaign, $allowedCampaigns)) {
159+
if ($forceCampaign) {
160+
$this->campaign = $allowedCampaigns[0];
161+
} else {
162+
return new JsonResponse([
163+
'message' => sprintf(
164+
'The campaign "%s" is not allowed (%s).',
165+
$this->campaign,
166+
implode(', ', $allowedCampaigns),
167+
),
168+
], Response::HTTP_FORBIDDEN);
169+
}
170+
}
159171

160172
$this->startDate = \DateTime::createFromFormat(\DateTime::RFC3339_EXTENDED, $this->jsonContent->stats->start ?? $this->jsonContent->stats->startTime);
161173

src/Service/ReportPlaywrightImporter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
class ReportPlaywrightImporter extends AbstractReportImporter
1313
{
14-
public const FILTER_CAMPAIGNS = ['blockwishlist'];
14+
public const FILTER_CAMPAIGNS = [
15+
'blockwishlist',
16+
'ps_cashondelivery',
17+
];
1518

1619
private EntityManagerInterface $entityManager;
1720

tests/Controller/ImportControllerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,22 @@ public function testReportPlaywrightWithNotExistingFilename(): void
285285
$this->assertEquals('Unable to retrieve content from GCP URL', $content['message']);
286286
}
287287

288+
public function testReportPlaywrightWithNoValidCampaign(): void
289+
{
290+
$client = static::createClient();
291+
$client->request('GET', '/import/report/playwright?filename=blockwishlist_' . self::DATE_RESOURCE . '-develop.json&token=AZERTY&campaign=ps_notAllowedCampaign&platform=chromium');
292+
$response = $client->getResponse();
293+
294+
$this->assertEquals(403, $response->getStatusCode());
295+
$this->assertTrue($response->headers->has('content-type'));
296+
$this->assertEquals('application/json', $response->headers->get('content-type'));
297+
298+
$content = $response->getContent();
299+
$content = json_decode($content, true);
300+
$this->assertArrayHasKey('message', $content);
301+
$this->assertEquals('The campaign "ps_notAllowedCampaign" is not allowed (blockwishlist, ps_cashondelivery).', $content['message']);
302+
}
303+
288304
public function testReportPlaywrightOk(): void
289305
{
290306
$client = static::createClient();

tests/Controller/ReportControllerTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Service\ReportMochaImporter;
66
use App\Service\ReportPlaywrightImporter;
7+
use PHPUnit\Framework\Attributes\DataProvider;
78
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
89

910
class ReportControllerTest extends WebTestCase
@@ -40,10 +41,9 @@ public function testCorsReports(): void
4041
}
4142

4243
/**
43-
* @dataProvider dataProviderReportFilters
44-
*
4544
* @param array<string, string> $query
4645
*/
46+
#[DataProvider('dataProviderReportFilters')]
4747
public function testReportsFilters(array $query, int $count): void
4848
{
4949
$client = static::createClient();
@@ -150,10 +150,9 @@ public function testCorsReportID(): void
150150
}
151151

152152
/**
153-
* @dataProvider dataProviderReportID
154-
*
155153
* @param array<string> $campaigns
156154
*/
155+
#[DataProvider('dataProviderReportID')]
157156
public function testReportID(int $reportId, array $campaigns): void
158157
{
159158
$client = static::createClient();

tests/bootstrap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Symfony\Component\Dotenv\Dotenv;
4+
use Symfony\Component\ErrorHandler\ErrorHandler;
45

56
require dirname(__DIR__) . '/vendor/autoload.php';
67

@@ -13,3 +14,8 @@
1314
if ($_SERVER['APP_DEBUG']) {
1415
umask(0000);
1516
}
17+
18+
/*
19+
* @todo : https://github.com/symfony/symfony/issues/53812
20+
*/
21+
ErrorHandler::register(null, false);

0 commit comments

Comments
 (0)