HOW TO TEST MAGENTO 2 API WITH POSTMAN? [FRONTEND]
We shared our experience on testing Magento 2 Backed API with Postman, but this is not enough to make sure API works well.
So we come back with Frontend API testing for Magento 2 Product Attachments.
What are the specifics here?
While the Backend API testing checks that admin users’ interactions with the site work well, frontend API testing does the same for website users – registered customers and people who are just browsing. Hence, the operations they perform are different.
We’ll discuss Frontend API testing in detail.
Article summary [hide]
- Frontend API testing with Postman: basics explained
- How to get the customer token?
- How to test getting files for Product (Default Store View, logged in Customer)?
- How to test getting files for Product in Default Store View / Germany Store View
- How to test getting files only for Products included in the order for Default Store View?
- How to test getting files for Product with extra URL parameters?
- How to test getting files for Category for Default Store View?
- How to test getting files by File ID?
- How to test downloading the file?
- Summing up
FRONTEND API TESTING WITH POSTMAN: BASICS EXPLAINED
To set off with frontend API testing for registered users, we should log in as a customer and get the customer token.
HOW TO GET THE CUSTOMER TOKEN?
So we’ve got the Customer Token:
Done? So now we can turn to testing basic API interactions registered customers have.
As we’ve stated above, registered customers and visitors can only view/download the product attachments. These actions are tested with the help of Get queries in Postman.
HOW TO TEST GETTING FILES FOR PRODUCT (DEFAULT STORE VIEW, LOGGED IN CUSTOMER)?
Now we’ll extract the info on the files attached to a given product available for a registered customer for a given Store View. We’ll use the Get method.
In the example above, there are 2 files attached to the product with the ID=6
Let’s see how it works on the frontend.
Below we offer our approach to testing NO Logged In Customer interactions for Magento 2 Product Attachments.
HOW TO TEST GETTING FILES FOR PRODUCT IN DEFAULT STORE VIEW / GERMANY STORE VIEW
File display may be different for different store views.
Let’s check how it works for Default and, say, Germany Store Views.
For this testing task, we’ll need the Get query again.
Input the Store View and the product ID in the URL for Default Store View and Germany Store View:
The info on attached files for each Store View is displayed correctly:
Though Frontend API testing only requires Get queries, some cases may be tricky. We describe one below.
HOW TO TEST GETTING FILES ONLY FOR PRODUCTS INCLUDED IN THE ORDER FOR DEFAULT STORE VIEW?
This query tests getting the info on the files attached to a product in the required Store View. In the frontend, these files are displayed at the Order page.
HOW TO TEST GETTING FILES FOR PRODUCT WITH EXTRA URL PARAMETERS?
Here we’ll look into the case when outputting the info on attached files in the necessary Store View requires adding extra parameters to the URL (product, category, order).
Once again we use the Get query.
What opportunity do we get with extra URL parameter?
Viewing/getting the files attached to products is not the only option in Magento 2 Product Attachments. Users can also output/view the info on the files attached to the whole category.
HOW TO TEST GETTING FILES FOR CATEGORY FOR DEFAULT STORE VIEW?
In the URL, we specify the Store View and the ID of the categories we aim to extract file info from. Here are the expected results based on the module settings:
Now let’s check how the info is rendered via Postman:
As we can see, there are 2 files attached to the ID 4 category and only one attached to ID 5 category. This agrees with the module settings. So the information is displayed correctly.
HOW TO TEST GETTING FILES BY FILE ID?
In this case, we need to extract the file info for the required Store View (default in our case), relying on the file ID. Which query type should we use? Get query, right you are.
We added the ID of a non-existing file (1111) on purpose. As you can see, Postman didn’t report an error. The tool just output the files that exist.
HOW TO TEST DOWNLOADING THE FILE?
We output the info in Base64 format. Then we copy the Base64 code and insert it into decoders. And voila! We get the product picture. We again use Get query to output the file data:
In the URL we input the necessary Store View, in the Params section – the file/product/category ID.
There are several ways to get the file content. We provide the file settings in the module settings and their representation in Postman.
Testing Frontend API boils down to acting as customers do. For our Magento 2 Product Attachments, this meant viewing and downloading the files attached to products and categories.
Having tested Backend and Frontend API, we’ve concluded that our Magento 2 extension is fully functional and successfully.