We’ve already posted you on how to start working with API in Magento 2 and Magento 2.3 API updates.

Now it’s time to turn to another critical question concerning API in Magento – testing.

GET A FREE GUIDE ABOUT:
– M2 API USER TYPES & HOW TO CREATE A NEW INTEGRATION
– M2 API AUTHENTICATION TYPES

So how to test API efficiently? There exists a range of tools that facilitate testing and make it faster, and we tried one of them. Satisfied with the results, we are ready to share our experience.

So meet Postman!

Testing Magento 2 API: what is Postman?

Postman is an API development environment and a handy HTTP client for testing websites. With Postman, you can create diverse queries that vary in purpose (Get, Put, Post and more) and unite them in collections. You can distribute the collections you create among your colleagues. These collections are a sort of test suites that you can move from one project to another with the relevant adjustments. Besides, the tool can be used for automated testing of apps, including Magento extensions. You just need to use the app interface to set up and save your tests.

We’ve tried this approach on testing Magento 2 Product Attachments, the extension that provides for tracking and managing the files you attach to products via API.

So there we go.

Setting up the environment

It’s the starting point of any testing activity. With Postman, you can set up the environment in 3 relatively simple steps:

  1. Open Manage environments form:

Magento-2-product-attachments-testing

2. Now we need to set variables. In our case they are:

  •  magento_api_url: https://magentourl/index.php/rest
  •  admin_username: your admin username
  •  admin_password: your admin password

M2-postman-product-attachments-set-variables

3. Now we need to select the created environment:

m2-api-testing-environment

Now we can use the variables we’ve just set in further queries.

Testing Magento 2 Product Attachments: backend API

In our examples, interaction with API requires authorization. So the first thing you need to do is to get the admin token. With the help of this token, you’ll get access to your Magento 2 backend via Postman.

To get the token, start a Post request: 

m2-product-attachment-authorization-token

Now click Send.

In response to the request, Postman generates Admin Token:

m2-product-attachments-tokin

We will use this token to test Magento 2 Product Attachments with Postman.

So what are the basic operations admins perform in Magento 2 Product Attachments?

These operations are uploading, viewing, editing, and deleting the file. To mock them in Postman, we’ll need Get, Post, Put, Search and Delete queries.

Let’s consider some of them.

How to test uploading the file?

This requires the Post query and authorization. To use the Admin Token, you should insert {{adminToken}} into the Token field. Use the same algorithm every time you have to perform some action as Administrator. Here’s how it looks:

m2-pa-post,man-authorization

First of all, you should encrypt the picture in Base64. To upload the file, insert “string” into “base64_encoded_data”. This is how it reads: “base64_encoded_data”: “string”:

m2-pa-postman-base64

 

 

 

 

 

We check if the file is uploaded successfully and set it for use in further requests:

 

magento-2-product-attach-api-testing

The response is positive, and we can use the uploaded file for testing the next query (Save Uploaded file):

Magento-2-pa-api-test-positive

How to test saving the uploaded file?

Now we have to save the previously uploaded file, and we again need the Post query. Here’s how it looks:

magento-2-pa-api-testing-save-file

We add the loaded file “tmp_file”: “{{lastUploadedFile}}”, and write out the file data: name, label, etc.

Before saving the file, we need to make sure it was loaded. So we check it with the help of a pre-request script. This script is run before the actual request:

m2-pa-postman-pre-request

We unset the uploaded file and configure settings to use the saved file in the latter requests:

magento-2-prod-attach-testing-unset-set

The file saved successfully:

magento-2-prod-attach-postman-file-saved

The last, but not the least. We need to check if the data on the file match our Magento 2 Product Attachments settings:

m2-api-testing-prod-attach-check-settings

That’s a success!

There’s also another option to consider: you can save the uploaded file with the help of its URL (using a link field).

How to test saving the file data by the URL?

Once again we need a Post request. Now we input the URL that points to the file location. Then we set the file name, label, etc., and click Send:

m2-prod-attach-postman-save-file-url

The response is positive:

m2-prod-attach-save-by-url-test-positive

So the file is saved.

Now let’s move to Get queries.

How to test getting file data by the file id?

Get queries are used to output the information on a certain file attached to a product. As an admin, you can perform this operation for Default Store View and for All Store Views.

We’ll try to get the data on LastSavedId via a Get query. First of all, we set the URL indicating the needed Store View and the file id:

magento-2-get-query-file-id

Here a Pre-request Script comes in handy as well. Before we output the info about the file, we should make sure it was saved:

magento-2-prod-attach-pre-req

In case the file is missing, we need to upload and save it.

Now we check if the information in the Magento 2 Product Attachments settings matches the info for the file uploaded:

m2-api-testing-prod-attachments-check

m2-prod-attach-api-check

 

The response request is positive:

 

magento-2-prod-attachments-get-query

We’ll turn to another important operation admins runs –  search. Why search for files attached to products? They may need some updates, editing or replacement.

So there we go.

How to test searching for the file?

In Postman, we can create search queries by the required/any attribute. We can also sort the search results by any value and order and set the number of the results to be displayed in the response section:

Magento-2-Api=-testing-search-queries

For illustrative purposes, we provide an example.

We’ll search for the file with the name starting with”filename” and which is greater than 3000 Bytes in size. And the results should be sorted by id in the ascending order with 5 files per page. Difficult? Let’s make it simpler.

First of all, we’ll break the query into lines:

{{magento_api_url}}/all/V1/amasty_product_attachment/filemanage/files?

  1. File name starts with “name”:
    searchCriteria[filterGroups][0][filters][0][field]=filename&

    searchCriteria[filterGroups][0][filters][0][value]=name%&
    searchCriteria[filterGroups][0][filters][0][conditionType]=like&
  2. The size is greater than 3000 Bytes:
     searchCriteria[filterGroups][0][filters][1][field]=size&
    searchCriteria[filterGroups][0][filters][1][value]=3000&
    searchCriteria[filterGroups][0][filters][1][conditionType]=gt&
  3. The results are sorted by id in ascending order: 
    searchCriteria[sortOrders][0][field]=file_id&

    searchCriteria[sortOrders][0][direction]=ASC&
  4. The number of files per page -is 5 and The number of page for viewing is 1:
    searchCriteria[pageSize]=5& searchCriteria[currentPage]=1

So we’ve uploaded 3 files, and 2 of them meet the search query:

m2-api-prod-attach-testing

magento-2-pa-search-query

Is the query correct? Compare it with Magento 2 Product attachments settings:

m2-prod-attach-search-check

The results match. Bingo!

And here comes the last and yet important operation an admin runs – deleting product attachments.

So let’s check if it works in Magento 2 Product Attachments.

How to test deleting a file?

There always comes a time when a file needs to be deleted. So making sure an admin can delete a file is a must.

For testing purposes, we chose to delete the last saved file. To do so, we input the file id in the URL and check if the file is saved with a pre-request script:

m2-prod-attach-api-testing-delete

When the file is saved, we can safely return to testing the delete function and unset the file:

m2-prod-attach-delete-testing

We’ve just deleted the last saved file.

Now we check the Magento 2 Product Attachments settings to make sure the file was deleted:

Beforem2-api-testing-delete-file

Afterm2-product-attach-api-backend-test-delete

So Delete function runs successfully.

To be continued…

We’ve described testing Magento 2 API with Postman from the point of view of admin interactions.

Thanks to the joint efforts of all our team members, we made sure our API is fully functional, so users can work with it avoiding complications.

But what about the Frontend API?

It’ll make the 2nd part of our Magento 2 API testing series, so stay tuned.

Any ideas on testing backend API?

Feel free to drop us a line below!