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

Adding validation to the AddProductReview endpoint #388

Merged
merged 7 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
34 changes: 34 additions & 0 deletions src/Resources/config/validation/AddProductReviewByCodeRequest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--

This file is part of the Sylius package.

(c) Paweł Jędrzejewski

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

-->

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\ShopApiPlugin\Request\AddProductReviewByCodeRequest">
<property name="title">
<constraint name="NotNull" />
</property>
<property name="rating">
<constraint name="Type">
<option name="type">integer</option>
</constraint>
<constraint name="Range">
<option name="min">0</option>
<option name="max">5</option>
</constraint>
</property>
<property name="email">
<constraint name="Email" />
</property>
</class>
</constraint-mapping>
34 changes: 34 additions & 0 deletions src/Resources/config/validation/AddProductReviewBySlugRequest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--

This file is part of the Sylius package.

(c) Paweł Jędrzejewski

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

-->

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\ShopApiPlugin\Request\AddProductReviewBySlugRequest">
<property name="title">
<constraint name="NotNull" />
</property>
<property name="rating">
<constraint name="Type">
<option name="type">integer</option>
</constraint>
<constraint name="Range">
<option name="min">0</option>
<option name="max">5</option>
</constraint>
</property>
<property name="email">
<constraint name="Email" />
</property>
</class>
</constraint-mapping>
61 changes: 61 additions & 0 deletions tests/Controller/AddProductReviewByCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\ShopApiPlugin\Controller\Product;

use Symfony\Component\HttpFoundation\Response;
use Tests\Sylius\ShopApiPlugin\Controller\JsonApiTestCase;

final class AddProductReviewByCodeTest extends JsonApiTestCase
lchrusciel marked this conversation as resolved.
Show resolved Hide resolved
{
private static $acceptAndContentTypeHeader = ['CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json'];

/**
* @test
*/
public function fails_to_add_review_because_rating_is_out_of_bounds()
{
$this->loadFixturesFromFiles(['channel.yml', 'shop.yml']);

$data =
<<<EOT
{
"comment": "Hello",
"rating": 100,
"email": "test@test.com",
"title": "Testing",
"code": "LOGAN_MUG_CODE"
}
EOT;

$this->client->request('POST', '/shop-api/WEB_GB/products/LOGAN_MUG_CODE/reviews', [], [], self::$acceptAndContentTypeHeader, $data);

$response = $this->client->getResponse();
$this->assertResponse($response, 'reviews/add_review_failed_rating', Response::HTTP_BAD_REQUEST);
}

/**
* @test
*/
public function fails_to_add_review_because_email_is_not_valid()
{
$this->loadFixturesFromFiles(['channel.yml', 'shop.yml']);

$data =
<<<EOT
{
"comment": "Hello",
"rating": 4,
"email": "test.com",
"title": "Testing",
"code": "LOGAN_MUG_CODE"
}
EOT;

$this->client->request('POST', '/shop-api/WEB_GB/products/LOGAN_MUG_CODE/reviews', [], [], self::$acceptAndContentTypeHeader, $data);

$response = $this->client->getResponse();
$this->assertResponse($response, 'reviews/add_review_failed_email', Response::HTTP_BAD_REQUEST);
}
}
59 changes: 59 additions & 0 deletions tests/Controller/AddProductReviewBySlugTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\ShopApiPlugin\Controller\Product;

use Symfony\Component\HttpFoundation\Response;
use Tests\Sylius\ShopApiPlugin\Controller\JsonApiTestCase;

final class AddProductReviewBySlugTest extends JsonApiTestCase
lchrusciel marked this conversation as resolved.
Show resolved Hide resolved
{
private static $acceptAndContentTypeHeader = ['CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json'];

/**
* @test
*/
public function fails_to_add_review_because_rating_is_out_of_bounds()
{
$this->loadFixturesFromFiles(['channel.yml', 'shop.yml']);

$data =
<<<EOT
{
"comment": "Hello",
"rating": 100,
"email": "test@test.com",
"title": "Testing"
}
EOT;

$this->client->request('POST', '/shop-api/WEB_GB/product-reviews-by-slug/mug', [], [], self::$acceptAndContentTypeHeader, $data);

$response = $this->client->getResponse();
$this->assertResponse($response, 'reviews/add_review_failed_rating', Response::HTTP_BAD_REQUEST);
}

/**
* @test
*/
public function fails_to_add_review_because_rating_email_is_not_valid()
{
$this->loadFixturesFromFiles(['channel.yml', 'shop.yml']);

$data =
<<<EOT
{
"comment": "Hello",
"rating": 4,
"email": "test.com",
"title": "Testing"
}
EOT;

$this->client->request('POST', '/shop-api/WEB_GB/product-reviews-by-slug/mug', [], [], self::$acceptAndContentTypeHeader, $data);

$response = $this->client->getResponse();
$this->assertResponse($response, 'reviews/add_review_failed_email', Response::HTTP_BAD_REQUEST);
}
}
2 changes: 1 addition & 1 deletion tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function it_has_view_classes(): void
'page_links' => View\PageLinksView::class,
'payment' => View\PaymentView::class,
'payment_method' => View\PaymentMethodView::class,
'placed_order' => View\PlacedOrderView::class,
'placed_order' => View\PlacedOrderView::class,
lchrusciel marked this conversation as resolved.
Show resolved Hide resolved
'price' => View\PriceView::class,
'product' => View\ProductView::class,
'product_attribute_value' => View\ProductAttributeValueView::class,
Expand Down
9 changes: 9 additions & 0 deletions tests/Responses/Expected/reviews/add_review_failed_email.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"code": 400,
"message": "Validation failed",
"errors": {
"email": [
"This value is not a valid email address."
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"code": 400,
"message": "Validation failed",
"errors": {
"rating": [
"This value should be 5 or less."
]
}
}