Skip to content

Commit

Permalink
Add tests for API endpoint (#380)
Browse files Browse the repository at this point in the history
* Fix API endpoints

* Update API route

* Fix MediaRepository methods

* Add tests for API endpoints

* Fix tests

* Fix for CR

* Fix for CR

* Fix for CR

* Fix for CR
  • Loading branch information
em411 authored Jul 28, 2021
1 parent 6740cb9 commit eae9329
Show file tree
Hide file tree
Showing 35 changed files with 1,061 additions and 44 deletions.
23 changes: 23 additions & 0 deletions features/api/viewing_blocks.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@shop_blocks
Feature: Getting data from cms blocks
In order to present dynamic content in my store
As an API user
I want to be able to display blocks

Background:
Given the store operates on a single channel in "United States"
And there is a block in the store
And there is a block with "block-1" code and "Hi there!" content

@api
Scenario: Browsing blocks
Given I want to browse blocks
Then I should see 2 blocks in the list
And I should see block with code "block-1"

@api
Scenario: Displaying block
Given I view block with code "block-1"
Then I should see block name
And I should see block content

23 changes: 23 additions & 0 deletions features/api/viewing_frequently_asked_questions.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@shop_frequently_asked_questions
Feature: Getting data from cms faq
In order to present dynamic content in my store
As an API user
I want to be able to display FAQ

Background:
Given the store operates on a single channel in "United States"
And there are 10 FAQs in the store
And there is an existing frequently asked question with "faq-1" code

@api
Scenario: Browsing FAQs
Given I want to browse FAQs
Then I should see 11 questions in the list
And I should see the "faq-1" question

@api
Scenario: Displaying question
Given I view faq with code "faq-1"
Then I should see question with random text
And I should see answer with random text

21 changes: 21 additions & 0 deletions features/api/viewing_media.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@shop_media
Feature: Getting data from cms media
In order to present dynamic content in my store
As an API user
I want to be able to display media files

Background:
Given the store operates on a single channel in "United States"
And there is an existing media with "media-1" code
And there is an existing "image" media with "image-1" code

@api
Scenario: Browsing media
Given I want to browse media
Then I should see 2 media in the list
And I should see media with code "media-1"

@api
Scenario: Displaying media
Given I view media with code "image-1"
Then I should see media name
30 changes: 30 additions & 0 deletions features/api/viewing_pages.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@shop_pages
Feature: Getting data from cms pages
In order to present dynamic content in my store
As an API user
I want to be able to display custom pages

Background:
Given the store operates on a single channel in "United States"
And there are 10 pages in the store
And the store has "iPhone 8" and "iPhone X" products
And there is a page in the store
And this page has these products associated with it
And there are existing sections named "Blog" and "General"
And this page has these sections associated with it
And this page has "About us" name
And this page has "about" code
And this page also has "about-us" slug
And this page also has "We are the best!" content

@api
Scenario: Browsing defined pages
Given I want to browse pages
Then I should see 11 pages in the list
And I should see the "About us" page

@api
Scenario: Viewing a detailed page
Given I view page with code "about"
Then I should see the page name "About us"
And I should see the page content "We are the best!"
21 changes: 21 additions & 0 deletions features/api/viewing_sections.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@shop_sections
Feature: Getting data from cms sections
In order to present dynamic content in my store
As an API user
I want to be able to display sections

Background:
Given the store operates on a single channel in "United States"
And there is a section in the store
And there is an existing section with "section-1" code

@api
Scenario: Browsing sections
Given I want to browse sections
Then I should see 2 sections in the list
And I should see section with code "section-1"

@api
Scenario: Displaying section
Given I view section with code "section-1"
Then I should see section name
25 changes: 15 additions & 10 deletions src/Repository/MediaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,48 @@ public function createListQueryBuilder(string $locale): QueryBuilder
;
}

public function findOneEnabledByCode(string $code, string $channelCode): ?MediaInterface
public function findOneEnabledByCode(string $code, string $localeCode): ?MediaInterface
{
return $this->createQueryBuilder('o')
->innerJoin('o.channels', 'channel')
->where('o.code = :code')
->leftJoin('o.translations', 'translation')
->where('translation.locale = :localeCode')
->andWhere('o.code = :code')
->andWhere('o.enabled = true')
->setParameter('code', $code)
->setParameter('localeCode', $localeCode)
->getQuery()
->getOneOrNullResult()
;
}

public function findBySectionCode(string $sectionCode, string $channelCode): array
public function findBySectionCode(string $sectionCode, ?string $localeCode): array
{
return $this->createQueryBuilder('o')
->innerJoin('o.channels', 'channel')
->innerJoin('o.sections', 'section')
->where('channel.code = :channelCode')
->where('translation.locale = :localeCode')
->andWhere('section.code = :sectionCode')
->andWhere('o.enabled = true')
->setParameter('channelCode', $channelCode)
->setParameter('localeCode', $localeCode)
->setParameter('sectionCode', $sectionCode)
->getQuery()
->getResult()
;
}

public function findByProductCode(string $productCode, string $channelCode): array
public function findByProductCode(string $productCode, string $channelCode, ?string $localeCode): array
{
return $this->createQueryBuilder('o')
->innerJoin('o.channels', 'channel')
->leftJoin('o.translations', 'translation')
->innerJoin('o.products', 'product')
->where('channel.code = :channelCode')
->innerJoin('o.channels', 'channels')
->andWhere('translation.locale = :localeCode')
->andWhere('product.code = :productCode')
->andWhere('o.enabled = true')
->setParameter('channelCode', $channelCode)
->andWhere('channels.code = :channelCode')
->setParameter('localeCode', $localeCode)
->setParameter('productCode', $productCode)
->setParameter('channelCode', $channelCode)
->getQuery()
->getResult()
;
Expand Down
6 changes: 3 additions & 3 deletions src/Repository/MediaRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ interface MediaRepositoryInterface extends RepositoryInterface
{
public function createListQueryBuilder(string $locale): QueryBuilder;

public function findOneEnabledByCode(string $code, string $channelCode): ?MediaInterface;
public function findOneEnabledByCode(string $code, string $localeCode): ?MediaInterface;

public function findBySectionCode(string $sectionCode, string $channelCode): array;
public function findBySectionCode(string $sectionCode, ?string $localeCode): array;

public function findByProductCode(string $productCode, string $channelCode): array;
public function findByProductCode(string $productCode, string $channelCode, ?string $localeCode): array;
}
4 changes: 2 additions & 2 deletions src/Resources/config/api_resources/Block.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
<attribute name="validation_groups">sylius</attribute>

<collectionOperations>
<collectionOperation name="shop_get_media">
<collectionOperation name="shop_get_block">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/cms-plugin/blocks</attribute>
</collectionOperation>

</collectionOperations>

<itemOperations>
<itemOperation name="shop_get_wishlist">
<itemOperation name="shop_get_block">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/cms-plugin/blocks/{id}</attribute>
</itemOperation>
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/config/api_resources/Faq.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
<attribute name="validation_groups">sylius</attribute>

<collectionOperations>
<collectionOperation name="shop_get_media">
<collectionOperation name="shop_get_faq">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/cms-plugin/faq</attribute>
</collectionOperation>

</collectionOperations>

<itemOperations>
<itemOperation name="shop_get_wishlist">
<itemOperation name="shop_get_faq">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/cms-plugin/faq/{id}</attribute>
</itemOperation>
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/api_resources/Media.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</collectionOperations>

<itemOperations>
<itemOperation name="shop_get_wishlist">
<itemOperation name="shop_get_media">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/cms-plugin/media/{id}</attribute>
</itemOperation>
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/config/api_resources/Page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
<attribute name="validation_groups">sylius</attribute>

<collectionOperations>
<collectionOperation name="shop_get_media">
<collectionOperation name="shop_get_page">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/cms-plugin/pages</attribute>
</collectionOperation>

</collectionOperations>

<itemOperations>
<itemOperation name="shop_get_wishlist">
<itemOperation name="shop_get_page">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/cms-plugin/pages/{id}</attribute>
</itemOperation>
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/config/api_resources/Section.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd"
>
<resource class="%bitbag_sylius_cms_plugin.model.section.class%" shortName="Block">
<resource class="%bitbag_sylius_cms_plugin.model.section.class%" shortName="Section">
<attribute name="normalization_context">
<attribute name="groups">
<attribute>shop:cms:read</attribute>
Expand All @@ -19,15 +19,15 @@
<attribute name="validation_groups">sylius</attribute>

<collectionOperations>
<collectionOperation name="shop_get_media">
<collectionOperation name="shop_get_section">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/cms-plugin/sections</attribute>
</collectionOperation>

</collectionOperations>

<itemOperations>
<itemOperation name="shop_get_wishlist">
<itemOperation name="shop_get_section">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/cms-plugin/sections/{id}</attribute>
</itemOperation>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:
sylius.security.admin_regex: "^/admin"
sylius.security.shop_regex: "^/(?!admin|new-api|api/.*|api$|media/.*)[^/]++"
sylius.security.new_api_route: "/new-api"
sylius.security.new_api_route: "/api/v2"
sylius.security.new_api_regex: "^%sylius.security.new_api_route%"

security:
Expand Down
31 changes: 11 additions & 20 deletions tests/Application/config/sylius/1.9/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ parameters:
sylius.security.admin_regex: "^/%sylius_admin.path_name%"
sylius.security.api_regex: "^/api"
sylius.security.shop_regex: "^/(?!%sylius_admin.path_name%|new-api|api/.*|api$|media/.*)[^/]++"
sylius.security.new_api_route: "/new-api"
sylius.security.new_api_route: "/api/v2"
sylius.security.new_api_regex: "^%sylius.security.new_api_route%"
sylius.security.new_api_admin_route: "%sylius.security.new_api_route%/admin"
sylius.security.new_api_admin_regex: "^%sylius.security.new_api_admin_route%"
Expand Down Expand Up @@ -55,12 +55,12 @@ security:
anonymous: true

new_api_admin_user:
pattern: "%sylius.security.new_api_route%/admin-user-authentication-token"
provider: sylius_admin_user_provider
pattern: "%sylius.security.new_api_admin_regex%/.*"
provider: sylius_api_admin_user_provider
stateless: true
anonymous: true
json_login:
check_path: "%sylius.security.new_api_route%/admin-user-authentication-token"
check_path: "%sylius.security.new_api_route%/admin/authentication-token"
username_path: email
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
Expand All @@ -70,12 +70,12 @@ security:
- lexik_jwt_authentication.jwt_token_authenticator

new_api_shop_user:
pattern: "%sylius.security.new_api_route%/shop-user-authentication-token"
provider: sylius_shop_user_provider
pattern: "%sylius.security.new_api_shop_regex%/.*"
provider: sylius_api_shop_user_provider
stateless: true
anonymous: true
json_login:
check_path: "%sylius.security.new_api_route%/shop-user-authentication-token"
check_path: "%sylius.security.new_api_route%/shop/authentication-token"
username_path: email
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
Expand All @@ -84,15 +84,6 @@ security:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator

new_api:
pattern: "%sylius.security.new_api_regex%/*"
provider: sylius_api_chain_provider
stateless: true
anonymous: lazy
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator

shop:
switch_user: { role: ROLE_ALLOWED_TO_SWITCH }
context: shop
Expand Down Expand Up @@ -128,21 +119,21 @@ security:
security: false

access_control:
- { path: "%sylius.security.admin_regex%/_partial", role: IS_AUTHENTICATED_ANONYMOUSLY, ips: [127.0.0.1, ::1] }
- { path: "%sylius.security.admin_regex%/_partial", role: IS_AUTHENTICATED_ANONYMOUSLY, ips: [ 127.0.0.1, ::1 ] }
- { path: "%sylius.security.admin_regex%/_partial", role: ROLE_NO_ACCESS }
- { path: "%sylius.security.shop_regex%/_partial", role: IS_AUTHENTICATED_ANONYMOUSLY, ips: [127.0.0.1, ::1] }
- { path: "%sylius.security.shop_regex%/_partial", role: IS_AUTHENTICATED_ANONYMOUSLY, ips: [ 127.0.0.1, ::1 ] }
- { path: "%sylius.security.shop_regex%/_partial", role: ROLE_NO_ACCESS }

- { path: "%sylius.security.admin_regex%/login", role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: "%sylius.security.api_regex%/login", role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: "%sylius.security.shop_regex%/login", role: IS_AUTHENTICATED_ANONYMOUSLY }

- { path: "%sylius.security.shop_regex%/register", role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: "%sylius.security.shop_regex%/verify", role: IS_AUTHENTICATED_ANONYMOUSLY }

- { path: "%sylius.security.admin_regex%", role: ROLE_ADMINISTRATION_ACCESS }
- { path: "%sylius.security.api_regex%/.*", role: ROLE_API_ACCESS }
- { path: "%sylius.security.shop_regex%/account", role: ROLE_USER }

- { path: "%sylius.security.new_api_admin_regex%/.*", role: ROLE_API_ACCESS }
- { path: "%sylius.security.new_api_route%/admin/authentication-token", role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: "%sylius.security.new_api_shop_regex%/.*", role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: "%sylius.security.new_api_route%/shop/authentication-token", role: IS_AUTHENTICATED_ANONYMOUSLY }
Loading

0 comments on commit eae9329

Please sign in to comment.