Validation and JSON Path Finder for User Management Module - Notes By ShariqSP
Validation and JSON Path Finder for User Management Module
Understanding JSON Path Finder
JSON Path Finder is a tool that helps developers navigate through and access specific values in a JSON response structure. JSON Path, similar to XPath for XML, is a syntax that allows for selecting nodes (data points) from JSON objects. In API testing, JSON Path Finder is useful to quickly identify the exact location of key fields in a JSON response, enabling precise validation in Postman or other testing tools.
How to Use JSON Path Finder in Postman
In Postman, JSON Path Finder can be used to assert the presence, structure, or values of certain keys within a JSON response. This can be done by writing tests
in the Postman Tests tab. Using JSON path expressions in assertions, we can validate specific properties of API responses, ensuring that the returned data meets expected values and formats.
Module Validation Using JSON Path
Below, we will validate each endpoint for the User Management Module using JSON Path syntax for thorough testing.
1. Register User
Endpoint: POST {{base_url}}/api/users/register
To validate the Register User endpoint, we want to confirm that the user creation is successful and that the response contains the new user’s details.
// Postman Tests for Register User
pm.test("Status code is 201", function () {
pm.response.to.have.status(200);
});
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.username).to.eql("test");
});
pm.test("Response contains user ID", function () {
pm.expect(pm.response.json().data).to.have.property("id");
});
2. Login User
Endpoint: POST {{base_url}}/api/users/login
For the Login User endpoint, we want to ensure that the user receives an authentication token upon successful login.
// Postman Tests for Login User
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response contains a token", function () {
pm.expect(pm.response.json().data).to.have.property("token");
});
pm.test("Token is valid format", function () {
pm.expect(pm.response.json().data.token).to.match(/^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/);
});
3. Get All Users
Endpoint: GET {{base_url}}/api/users/all
This endpoint should return a list of users. We want to ensure the response format and data integrity.
// Postman Tests for Get All Users
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response contains an array of users", function () {
pm.expect(pm.response.json().data).to.be.an("array");
});
pm.test("Each user has a 'username' field", function () {
pm.response.json().data.forEach(function (user) {
pm.expect(user).to.have.property("username");
});
});
4. Update User
Endpoint: PUT {{base_url}}/api/users/update
For the Update User endpoint, we want to verify that the updated details are returned in the response.
// Postman Tests for Update User
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Updated email is correct", function () {
pm.expect(pm.response.json().data.email).to.eql("john_updated@example.com");
});
pm.test("Updated mobile is correct", function () {
pm.expect(pm.response.json().data.mobile).to.eql("9876543210");
});
5. Delete User
Endpoint: DELETE {{base_url}}/api/users/delete/{id}
For the Delete User endpoint, validate the deletion and ensure the response reflects this action.
// Postman Tests for Delete User
pm.test("Status code is 204", function () {
pm.response.to.have.status(204);
});
pm.test("Response confirms deletion", function () {
pm.expect(pm.response.json().message).to.eql("User deleted successfully");
});
Using JSON Path Finder in Postman tests, we can set up assertions for each endpoint to verify the correctness of responses and ensure each endpoint functions as expected.