How asauvage/apisentinel works
Project Type and Purpose
• This is an open-source, mapping-driven API testing tool written in Python.
• It reads JSON/YAML mappings that describe expected API responses, issues HTTP requests, validates responses, and stores results in MongoDB.
File Structure Analysis
• Top-level entrypoints:
• launcher.py and shortcut.py – CLI/bootstrap modules
• data/ – holds service mapping JSONs, YAMLs and a central service_config.py
• models/ – core domain logic: GlobalTester, Tester, Service, ResultsManager, Errors
• utils/ – helper libraries: commons, mapping loader, mapping tester, HTTP request, MongoDB client
• tests/ – unit tests covering models and utils
• Configuration files: requirements.txt, .github/workflows/unit_test.yaml
• Data folder follows a per-service subfolder pattern, with optional generics folder for reusable schemas.
README Insights
• Describes mapping DSL for JSON response validation (Number, String, Boolean, Object, Array types).
• Shows how to configure a new service and extend via subclassing GlobalTester.
• External dependencies: Python ≥3.9, a MongoDB server, and the target API under test.
Main Components
a. CLI Layer
• launcher.py / shortcut.py
• Parses command-line args (environment, custom headers/options)
• Instantiates a GlobalTester subclass
b. Configuration Layer
• data/service_config.py (SERVICE definitions)
• Mapping files (data/mapping/<service>/<mapping>.json / .yaml)
• Generics subfolder for shared schemas
c. Core Test Engine
• models.GlobalTester: orchestrates test session
• models.Tester: per-endpoint validation logic
• models.Service: holds service metadata (paths, URLs, headers)
• utils.mapping & utils.mapping_tester: parse mappings and perform schema checks
• utils.request_api: issues HTTP GET calls to external API
d. Persistence Layer
• models.ResultsManager: aggregates test results
• utils.mongodb: writes test sessions and results into MongoDB
e. External Systems
• Target API Endpoints (e.g., pokeapi.co)
• MongoDB Server
• (Optional) APISentinel Dashboard for results visualization
Relationships and Data Flow
1) User invokes CLI → GlobalTester
2) GlobalTester reads service_config & mapping files
3) For each mapping / endpoint:
• Build full URL (service.url + path + extended_path)
• Call utils.request_api → HTTP response (JSON)
• Pass JSON + mapping to utils.mapping_tester → validation results/errors
• Send results to ResultsManager
4) ResultsManager uses utils.mongodb to persist session, response, errors
5) CLI prints session ID / summary; dashboard can query MongoDB
Architectural Patterns and Design Principles
• Layered architecture (CLI → Configuration → Core Engine → Persistence)
• Single Responsibility modules (mapping vs HTTP vs DB vs domain logic)
• Extensibility via subclassing (GlobalTester) and mapping generics
• Configuration-driven behavior (data folder controls tests, no code change)
Technologies and Frameworks
• Python 3.9+
• requests library (via utils/request_api)
• PyYAML or ruamel.yaml (for YAML endpoint specs)
• pymongo (MongoDB client)
• unittest / pytest (in tests/)
• GitHub Actions for CI (.github/workflows)