Before we begin testing our API endpoints, we need to set up our testing environment properly. This setup will ensure we can test efficiently and systematically.
Let's create a complete testing environment in Postman that we'll use throughout our testing:
baseUrl: http://localhost:8000
xsrfToken: [leave empty initially]
authToken: [leave empty initially]
pm.sendRequest({
url: pm.environment.get("baseUrl") + "/api/csrf/restore",
method: 'GET'
}, function (err, res) {
if (!err) {
pm.environment.set("xsrfToken", res.cookies.get("XSRF-TOKEN"));
}
});
For cURL testing, we'll need to store our CSRF token for repeated use. Let's create a script to help with this:
#!/bin/bash
# Save as get-csrf.sh
# Get CSRF token and save it
TOKEN=$(curl -s -X GET http://localhost:8000/api/csrf/restore \
--cookie-jar cookies.txt \
| grep -o '"XSRF-Token":"[^"]*' \
| cut -d'"' -f4)
echo "CSRF Token: $TOKEN"
echo "Token saved to cookies.txt"
Make the script executable:
chmod +x get-csrf.sh
{
"firstName": "John",
"lastName": "Smith",
"email": "john.smith@gmail.com",
"username": "JohnSmith",
"password": "secret password"
}
pm.test("Status code is 201", function () {
pm.response.to.have.status(201);
});
pm.test("User is created successfully", function () {
const responseJson = pm.response.json();
pm.expect(responseJson.user).to.have.property('id');
pm.expect(responseJson.user.username).to.eql('JohnSmith');
});
if (pm.response.code === 201) {
pm.environment.set("authToken", pm.response.headers.get("Set-Cookie"));
}
./get-csrf.sh
cat > register.json << EOF
{
"firstName": "John",
"lastName": "Smith",
"email": "john.smith@gmail.com",
"username": "JohnSmith",
"password": "secret password"
}
EOF
curl -X POST http://localhost:8000/api/users \
-H "Content-Type: application/json" \
-H "XSRF-TOKEN: $TOKEN" \
-b cookies.txt \
-d @register.json
Test each validation rule using both Postman and cURL:
Postman:
{
"firstName": "John",
"lastName": "Smith",
"email": "invalid-email",
"username": "JohnSmith",
"password": "secret password"
}
cURL:
curl -X POST http://localhost:8000/api/users \
-H "Content-Type: application/json" \
-H "XSRF-TOKEN: $TOKEN" \
-b cookies.txt \
-d '{"firstName":"John","lastName":"Smith","email":"invalid-email","username":"JohnSmith","password":"secret password"}'
{
"credential": "john.smith@gmail.com",
"password": "secret password"
}
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Login successful", function () {
const responseJson = pm.response.json();
pm.expect(responseJson.user).to.have.property('id');
pm.expect(responseJson.user.email).to.eql('john.smith@gmail.com');
});
if (pm.response.code === 200) {
pm.environment.set("authToken", pm.response.headers.get("Set-Cookie"));
}
cat > login.json << EOF
{
"credential": "john.smith@gmail.com",
"password": "secret password"
}
EOF
curl -X POST http://localhost:8000/api/session \
-H "Content-Type: application/json" \
-H "XSRF-TOKEN: $TOKEN" \
-b cookies.txt \
-c cookies.txt \
-d @login.json
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Session user data is correct", function () {
const responseJson = pm.response.json();
pm.expect(responseJson.user).to.not.be.null;
pm.expect(responseJson.user).to.have.property('email');
});
curl http://localhost:8000/api/session \
-b cookies.txt
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Logout successful", function () {
const responseJson = pm.response.json();
pm.expect(responseJson.message).to.eql('success');
});
pm.environment.set("authToken", null);
curl -X DELETE http://localhost:8000/api/session \
-H "XSRF-TOKEN: $TOKEN" \
-b cookies.txt
Now that we understand how to test each endpoint individually, let's create a complete test flow that simulates a user's journey through our application.
#!/bin/bash
# Save as test-flow.sh
echo "Starting API test flow..."
# Get CSRF token
./get-csrf.sh
# Register new user
echo "Testing registration..."
curl -X POST http://localhost:8000/api/users \
-H "Content-Type: application/json" \
-H "XSRF-TOKEN: $TOKEN" \
-b cookies.txt \
-c cookies.txt \
-d @register.json
# Login
echo "Testing login..."
curl -X POST http://localhost:8000/api/session \
-H "Content-Type: application/json" \
-H "XSRF-TOKEN: $TOKEN" \
-b cookies.