import Image from ‘next/image’;
negative.test.js Test Suite Documentation
Overview
This Jest test suite focuses on negative testing scenarios for the PHPR API to validate the system’s robustness and error handling. It ensures that the API correctly responds to invalid inputs, unauthorized access, expired tokens, and other error conditions for every entity defined in metadata-cache.json.
Test Scope
- Authenticates once before running tests to obtain a valid JWT token.
- Uses a hardcoded expired token to test authorization failures.
- Tests various failure scenarios for HTTP
GET,PUT,POST, andDELETEmethods on entities. - Covers invalid IDs, missing or invalid authentication, unauthorized modifications, bad request payloads, and edge cases for relational data and type validation.
- Uses snapshots to verify error response payload structures where applicable.
Test source
The list of endpoints to test is hardcoded, but repeted for every entity.
Key Functionalities Tested
1. Authentication and Setup
- Authenticates using environment variables for a valid token.
- Uses a fixed expired token for testing token expiration handling.
- Retrieves at least one valid resource ID per entity for testing.
2. GET Requests
- Requests using non-existent and non-numeric IDs return
404 Not Found. - Requests without authentication or with expired token return expected error status codes (
401).
3. PUT Requests (Update)
- Updates attempted on non-existent or non-numeric IDs return
404or405. - Updates without authentication or with expired token return
401or405. - Attempts to update non-writable or non-existent properties return
400or405.
4. POST Requests (Create)
- Calls without authentication return proper error status.
- Attempts with extra undefined parameters return
405. - Tests missing or invalid parameters where applicable (some TODOs due to complexity).
- Posts with invalid data types for various fields (string, datetime, integer, decimal, boolean, and relations) assert error responses and snapshot error bodies.
- Tests improper content types (e.g.,
text/plain) return405. - Uses snapshots to verify error message consistency.
5. DELETE Requests
- Deletes with non-existent or invalid IDs return
404. - Deletes without authentication or with expired token return error status codes.
- Cleans up resources after test runs when applicable.
6. Relation Field Validation
- Sends POST requests with invalid IDs in relationship fields (e.g.,
-99,0,99999999). - Expects error response codes with snapshots.
7. Data Type Validation
- Verifies type validation by attempting to POST wrong data types on various fields, including strings instead of numbers, numbers instead of strings, invalid dates, and non-boolean values.
- Each case asserts correct error handling and response snapshots.
Environmental Setup
- Uses environment variables:
PHPR_BASE_URL- Base URL for API requests.PHPR_USERNAMEandPHPR_PASSWORD- Credentials for login.
- Loads test configuration from
metadata-cache.json.
Notes
- Tests run sequentially for each entity.
- Extensive use of snapshot testing helps monitor error response structures and messages.
- The suite includes a basic sanity check test (
adds 1 + 1 to equal 2).
Last updated on