test: add unit tests for env config and route schemas#44
Merged
Conversation
Cover validation logic that was previously only exercised indirectly through integration tests (which need a mocked browser/S3): - env.ts: production API_KEY guard, SSRF_PROTECTION secure-by-default transform, numeric coercion/defaults, S3 bucket and AWS region rules, and the fail-fast process.exit(1) on invalid configuration - pdf/schema.ts and screenshot/schema.ts: content bounds, defaults, enums, UUID params, and the $schema stripping that keeps Fastify's AJV (draft-07) able to consume the generated JSON schemas Export envSchema from env.ts so it can be parsed in isolation without triggering the module's import-time validation side effect.
Move the env/schema unit tests out of src into a dedicated test/unit directory, mirroring the existing test/integration layout (flat, importing from ../../src). Import paths updated accordingly. Add variant C — handler unit tests for the generate and screenshot routes with mocked services, env, and the SSRF guard. These cover the branching logic that lives in the handler rather than the schema: - html XOR url validation (neither / both) - SSRF block returns 400 and skips the service; non-SsrfError re-throws - stream vs. upload paths - success/error metrics recording (generate)
Relocate the last co-located unit tests (auth, ssrf, and the metrics/pdf/ screenshot/storage services) from src into test/unit so there is a single home for unit tests, alongside test/integration. Relative imports and the relative module mocks (env in StorageService and auth) updated to resolve from the new location.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Was
Zwei Dinge:
test/unit/-Verzeichnis (flach, importiert via../../src/...), konsistent mittest/integration/. Vorher waren sie zwischensrc/(co-located) und neu verteilt.Neue Tests
test/unit/env.test.tsAPI_KEY-Pflicht,SSRF_PROTECTIONsecure-by-default,coerce-Defaults & Positiv-Int-Regeln, S3-Bucket-/AWS-Region-Regex, Endpoint-URLs, Fail-Fastprocess.exit(1)test/unit/pdf-schema.test.tsgenerate-Grenzen & Defaults, Paper/Scale, Cookies/Headers, UUID-Params,merge/split/compress/pdfa,$schema-Strippingtest/unit/screenshot-schema.test.tstest/unit/pdf-handler.test.tstest/unit/screenshot-handler.test.tsVerschobene Tests (unverändert, nur Pfade)
auth,ssrf,MetricsService,PdfService,PdfOperationsService,ScreenshotService,StorageService→ vonsrc/nachtest/unit/. Relative Imports und relative Modul-Mocks (envinStorageService/auth) auf den neuen Ort angepasst.Produktionsänderung
Eine einzige, minimale Änderung:
const envSchema→export const envSchemainsrc/config/env.ts, damit das Schema isoliert geparst werden kann, ohne den Import-Zeit-Validierungs-Side-Effect auszulösen.Verifikation
test/unit/tsc --noEmit: sauber ·eslint src test: sauber*.test.tsmehr insrc/Notierenswert
env,assertSafeUrlund die Services, um die Verzweigungen deterministisch ohne DNS/Browser/S3 durchzuspielen.API_KEY-Pflicht, SSRF-Schutz an by default, SSRF-Block im Handler) statt nur implizit beim Server-Boot.z.toJSONSchemamuss das$schema-Feld gestrippt bekommen (sonst scheitert Fastifys AJV an der draft/2020-12-Metaschema-Auflösung).