@stackonward/cli 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +4588 -205
- package/dist/index.js.map +1 -1
- package/dist/resources/apple/.claude/docs/architecture-template.md +54 -0
- package/dist/resources/apple/.claude/docs/architecture.md +52 -0
- package/dist/resources/apple/.claude/docs/index.md +13 -0
- package/dist/resources/apple/.claude/docs/security-lifecycle.md +57 -0
- package/dist/resources/apple/.claude/docs/stack.md +93 -0
- package/dist/resources/apple/.claude/docs/verification.md +63 -0
- package/dist/resources/apple/AGENTS.md +397 -0
- package/dist/resources/apple/CLAUDE.md +397 -0
- package/dist/resources/apple/Gemfile +2 -0
- package/dist/resources/apple/Gemfile.lock +365 -0
- package/dist/resources/apple/_gitignore +14 -0
- package/dist/resources/apple/fastlane/Fastfile +123 -0
- package/dist/resources/components/nuxt/site-source/app/app.vue +42 -0
- package/dist/resources/components/nuxt/site-source/app/assets/css/site-shell.css +1035 -0
- package/dist/resources/components/nuxt/site-source/app/assets/icons/brand/.gitkeep +0 -0
- package/dist/resources/components/nuxt/site-source/app/assets/icons/common/.gitkeep +0 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/ApplicationHeader.vue +35 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/ApplicationShell.vue +56 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/ApplicationSidebar.vue +40 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/BackToTop.vue +42 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/ContentArticlePage.vue +40 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/ContentListPage.vue +30 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/CookieConsent.vue +18 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/MarketingFooter.vue +52 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/MarketingHeader.vue +106 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/MarketingShell.vue +35 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/SiteBrand.vue +10 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/StaticPage.vue +21 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/ToastViewport.vue +25 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/BaseButton.vue +86 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/BaseGradientBorder.vue +59 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/BaseModal.vue +131 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/BasePopover.vue +150 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/BaseText.vue +138 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/CheckoutStatus.vue +112 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/CheckoutSummary.vue +172 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/GoogleSignInButton.vue +113 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/InfiniteScroll.vue +123 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/YoutubeEmbed.vue +126 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useAnalytics.ts +24 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useCheckout.ts +121 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useConsent.ts +31 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useGoogleIdentity.ts +163 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useNavigation.ts +11 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useSeo.ts +33 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useSiteShell.ts +58 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useToasts.ts +48 -0
- package/dist/resources/components/nuxt/site-source/app/layouts/app.vue +5 -0
- package/dist/resources/components/nuxt/site-source/app/layouts/default.vue +5 -0
- package/dist/resources/components/nuxt/site-source/app/plugins/analytics.client.ts +121 -0
- package/dist/resources/components/nuxt/site-source/app/plugins/google-identity.client.ts +24 -0
- package/dist/resources/components/nuxt/site-source/app/theme/accordion.ts +9 -0
- package/dist/resources/components/nuxt/site-source/app/theme/breadcrumb.ts +8 -0
- package/dist/resources/components/nuxt/site-source/app/theme/button.ts +92 -0
- package/dist/resources/components/nuxt/site-source/app/theme/carousel.ts +11 -0
- package/dist/resources/components/nuxt/site-source/app/theme/checkbox.ts +12 -0
- package/dist/resources/components/nuxt/site-source/app/theme/index.ts +30 -0
- package/dist/resources/components/nuxt/site-source/app/theme/input.ts +47 -0
- package/dist/resources/components/nuxt/site-source/app/theme/modal.ts +9 -0
- package/dist/resources/components/nuxt/site-source/app/theme/pagination.ts +6 -0
- package/dist/resources/components/nuxt/site-source/app/theme/select.ts +23 -0
- package/dist/resources/components/nuxt/site-source/app/theme/stepper.ts +10 -0
- package/dist/resources/components/nuxt/site-source/app/theme/toast.ts +8 -0
- package/dist/resources/components/nuxt/site-source/app/theme/tooltip.ts +6 -0
- package/dist/resources/components/nuxt/site-source/app/types/site-shell.ts +85 -0
- package/dist/resources/components/react/ui/Button.tsx +73 -0
- package/dist/resources/components/react/ui/Input.tsx +44 -0
- package/dist/resources/components/registry.json +355 -0
- package/dist/resources/components/vue/feedback/Toast.vue +120 -0
- package/dist/resources/components/vue/feedback/useToast.ts +70 -0
- package/dist/resources/components/vue/media/FileUpload.vue +229 -0
- package/dist/resources/components/vue/media/VideoPlayer.vue +157 -0
- package/dist/resources/components/vue/ui/AppImage.vue +147 -0
- package/dist/resources/components/vue/ui/BottomSheet.vue +217 -0
- package/dist/resources/components/vue/ui/Button.vue +154 -0
- package/dist/resources/components/vue/ui/Card.vue +111 -0
- package/dist/resources/components/vue/ui/Input.vue +113 -0
- package/dist/resources/components/vue/ui/Modal.vue +193 -0
- package/dist/resources/components/vue/ui/ScrollArea.vue +163 -0
- package/dist/resources/components/vue/ui/Tooltip.vue +115 -0
- package/dist/resources/registry.json +158 -0
- package/dist/resources/snippets/node/cache.ts +116 -0
- package/dist/resources/snippets/node/date.ts +126 -0
- package/dist/resources/snippets/node/logger.ts +104 -0
- package/dist/resources/snippets/node/validation.ts +132 -0
- package/dist/resources/snippets/react/useDebounce.ts +90 -0
- package/dist/resources/snippets/registry.json +65 -0
- package/dist/resources/snippets/vue/useAsync.ts +96 -0
- package/dist/resources/snippets/vue/usePagination.ts +102 -0
- package/dist/resources/templates/base/.claude/docs/architecture-template.md +459 -0
- package/dist/resources/templates/base/.claude/docs/architecture.md +137 -0
- package/dist/resources/templates/base/.claude/docs/index.md +42 -0
- package/dist/resources/templates/base/.claude/docs/security-lifecycle.md +129 -0
- package/dist/resources/templates/base/.claude/docs/verification.md +121 -0
- package/dist/resources/templates/base/AGENTS.md +415 -0
- package/dist/resources/templates/base/CLAUDE.md +415 -0
- package/dist/resources/templates/go-backend/.claude/docs/api-design.md +486 -0
- package/dist/resources/templates/go-backend/.claude/docs/architecture-template.md +449 -0
- package/dist/resources/templates/go-backend/.claude/docs/check-list.md +155 -0
- package/dist/resources/templates/go-backend/.claude/docs/database.md +369 -0
- package/dist/resources/templates/go-backend/.claude/docs/e2e-standards.md +161 -0
- package/dist/resources/templates/go-backend/.claude/docs/error-handling.md +558 -0
- package/dist/resources/templates/go-backend/.claude/docs/event-runtime-boundary.md +124 -0
- package/dist/resources/templates/go-backend/.claude/docs/identifier-contract.md +195 -0
- package/dist/resources/templates/go-backend/.claude/docs/index.md +22 -0
- package/dist/resources/templates/go-backend/.claude/docs/logging.md +314 -0
- package/dist/resources/templates/go-backend/.claude/docs/mediagen-provider-onboarding-sop.md +917 -0
- package/dist/resources/templates/go-backend/.claude/docs/mediagen-provider-quick-reference.md +195 -0
- package/dist/resources/templates/go-backend/.claude/docs/module-standards.md +309 -0
- package/dist/resources/templates/go-backend/.claude/docs/openapi-contract.md +141 -0
- package/dist/resources/templates/go-backend/.claude/docs/payment-gateway-onboarding.md +283 -0
- package/dist/resources/templates/go-backend/.claude/docs/platform-boundary.md +191 -0
- package/dist/resources/templates/go-backend/.claude/docs/quality-gates.md +112 -0
- package/dist/resources/templates/go-backend/.dockerignore +6 -0
- package/dist/resources/templates/go-backend/.env.example +13 -0
- package/dist/resources/templates/go-backend/.gitattributes +2 -0
- package/dist/resources/templates/go-backend/AGENTS.md +1139 -0
- package/dist/resources/templates/go-backend/CLAUDE.md +1139 -0
- package/dist/resources/templates/go-backend/Dockerfile +14 -0
- package/dist/resources/templates/go-backend/Makefile +71 -0
- package/dist/resources/templates/go-backend/README.md +66 -0
- package/dist/resources/templates/go-backend/_gitignore +10 -0
- package/dist/resources/templates/go-backend/api/openapi/README.md +29 -0
- package/dist/resources/templates/go-backend/api/openapi/dist/artifact-manifest.json +19 -0
- package/dist/resources/templates/go-backend/api/openapi/dist/conformance-matrix.json +276 -0
- package/dist/resources/templates/go-backend/api/openapi/document.go +30 -0
- package/dist/resources/templates/go-backend/api/openapi/package.json +28 -0
- package/dist/resources/templates/go-backend/api/openapi/pnpm-lock.yaml +3052 -0
- package/dist/resources/templates/go-backend/api/openapi/redocly-source.yaml +18 -0
- package/dist/resources/templates/go-backend/api/openapi/redocly.yaml +8 -0
- package/dist/resources/templates/go-backend/api/openapi/sdk-generators.json +16 -0
- package/dist/resources/templates/go-backend/api/openapi/service-registry.json +39 -0
- package/dist/resources/templates/go-backend/api/openapi/services/product-api/conformance/runtime.arazzo.yaml +65 -0
- package/dist/resources/templates/go-backend/api/openapi/services/product-api/dist/internal.openapi.json +327 -0
- package/dist/resources/templates/go-backend/api/openapi/services/product-api/dist/runtime-contract.json +56 -0
- package/dist/resources/templates/go-backend/api/openapi/services/product-api/openapi.yaml +222 -0
- package/dist/resources/templates/go-backend/api/openapi/services/worker/conformance/runtime.arazzo.yaml +65 -0
- package/dist/resources/templates/go-backend/api/openapi/services/worker/dist/internal.openapi.json +327 -0
- package/dist/resources/templates/go-backend/api/openapi/services/worker/dist/runtime-contract.json +56 -0
- package/dist/resources/templates/go-backend/api/openapi/services/worker/openapi.yaml +222 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/breaking.mjs +416 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/build.mjs +103 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/conformance_coverage.mjs +111 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/contract_compiler.mjs +62 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/contract_policy.mjs +208 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/delegated_contract.mjs +302 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/deterministic_archive.mjs +69 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/documentation_example_privacy.mjs +93 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/documentation_projection.mjs +498 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/documentation_quality.mjs +346 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/git_artifact_history.mjs +188 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/manifest.mjs +170 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/overlay.mjs +98 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/project.mjs +549 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/route_contract.mjs +156 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/runtime_conformance.mjs +193 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/scalar_asset_localization.mjs +164 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/sdk_generator.mjs +229 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/semantic_version.mjs +64 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/service_contract_inputs.mjs +16 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/breaking.test.mjs +365 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/build.test.mjs +42 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/conformance_coverage.test.mjs +88 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/contract_policy.test.mjs +257 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/delegated_contract.test.mjs +205 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/documentation_example_privacy.test.mjs +51 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/documentation_projection.test.mjs +368 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/documentation_quality.test.mjs +151 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/git_artifact_history.test.mjs +60 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/head_policy.test.mjs +8 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/overlay.test.mjs +44 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/projection.test.mjs +270 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/redocly_documentation_rules.test.mjs +146 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/release_manifest.test.mjs +76 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/route_contract.test.mjs +119 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/runtime_conformance.test.mjs +75 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/scalar_asset_localization.test.mjs +50 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/sdk_generator.test.mjs +31 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/semantic_version.test.mjs +60 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/service_contract_inputs.test.mjs +27 -0
- package/dist/resources/templates/go-backend/cmd/internal/runtime/resources.go +125 -0
- package/dist/resources/templates/go-backend/cmd/internal/runtime/runtime.go +102 -0
- package/dist/resources/templates/go-backend/cmd/internal/runtime/runtime_test.go +70 -0
- package/dist/resources/templates/go-backend/cmd/product-api/lifecycle_test.go +58 -0
- package/dist/resources/templates/go-backend/cmd/product-api/main.go +32 -0
- package/dist/resources/templates/go-backend/cmd/product-api/wire.go +13 -0
- package/dist/resources/templates/go-backend/cmd/product-api/wire_gen.go +94 -0
- package/dist/resources/templates/go-backend/cmd/worker/main.go +47 -0
- package/dist/resources/templates/go-backend/cmd/worker/wire.go +14 -0
- package/dist/resources/templates/go-backend/cmd/worker/wire_gen.go +111 -0
- package/dist/resources/templates/go-backend/configs/config.yaml +37 -0
- package/dist/resources/templates/go-backend/deployments/compose.yaml +71 -0
- package/dist/resources/templates/go-backend/docs/openapi-provenance.json +489 -0
- package/dist/resources/templates/go-backend/docs/runtime-provenance.json +175 -0
- package/dist/resources/templates/go-backend/docs/source-provenance.json +3568 -0
- package/dist/resources/templates/go-backend/docs/standards-provenance.json +1624 -0
- package/dist/resources/templates/go-backend/go.mod +256 -0
- package/dist/resources/templates/go-backend/go.sum +805 -0
- package/dist/resources/templates/go-backend/internal/common/canonicaljson/canonical.go +370 -0
- package/dist/resources/templates/go-backend/internal/common/canonicaljson/canonical_test.go +223 -0
- package/dist/resources/templates/go-backend/internal/common/checkoutbff/contracts.go +125 -0
- package/dist/resources/templates/go-backend/internal/common/checkoutbff/platform_client.go +212 -0
- package/dist/resources/templates/go-backend/internal/common/checkoutbff/platform_client_test.go +319 -0
- package/dist/resources/templates/go-backend/internal/common/contentaccess/contentaccess.go +50 -0
- package/dist/resources/templates/go-backend/internal/common/contracts/contact.go +85 -0
- package/dist/resources/templates/go-backend/internal/common/ctxutil/identity.go +20 -0
- package/dist/resources/templates/go-backend/internal/common/ctxutil/product.go +53 -0
- package/dist/resources/templates/go-backend/internal/common/enumdomain/payment.go +12 -0
- package/dist/resources/templates/go-backend/internal/common/enums/dto_admin.go +9 -0
- package/dist/resources/templates/go-backend/internal/common/enums/enum.go +1247 -0
- package/dist/resources/templates/go-backend/internal/common/enums/handler_admin.go +66 -0
- package/dist/resources/templates/go-backend/internal/common/enums/module.go +46 -0
- package/dist/resources/templates/go-backend/internal/common/enums/provider.go +10 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register.go +11 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_audit.go +274 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_command_replay.go +12 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_config.go +27 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_locale.go +37 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_ops.go +14 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_outbox.go +15 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_rule_expression.go +63 -0
- package/dist/resources/templates/go-backend/internal/common/enums/registry.go +77 -0
- package/dist/resources/templates/go-backend/internal/common/httporigin/origin.go +62 -0
- package/dist/resources/templates/go-backend/internal/common/httporigin/origin_test.go +38 -0
- package/dist/resources/templates/go-backend/internal/common/iamcredential/plaintext.go +35 -0
- package/dist/resources/templates/go-backend/internal/common/iamcredential/plaintext_test.go +29 -0
- package/dist/resources/templates/go-backend/internal/common/iamscope/scope.go +212 -0
- package/dist/resources/templates/go-backend/internal/common/iamscope/scope_binding.go +26 -0
- package/dist/resources/templates/go-backend/internal/common/iamscope/scope_filter.go +173 -0
- package/dist/resources/templates/go-backend/internal/common/iamscope/scope_filter_test.go +279 -0
- package/dist/resources/templates/go-backend/internal/common/internalapi/authorization_details.go +10 -0
- package/dist/resources/templates/go-backend/internal/common/internalapi/route_permission_policies.go +660 -0
- package/dist/resources/templates/go-backend/internal/common/internalapi/route_permission_policies_test.go +331 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/audience_token_source.go +166 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/bootstrap_token_exchanger.go +183 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/bound_service_token_signer.go +52 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/delegated_access_token_exchanger.go +189 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/delegated_access_token_exchanger_test.go +159 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/product_scoped_service_token_signer.go +92 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/provider.go +25 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/request_signer.go +86 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/resolver.go +69 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/resolver_test.go +42 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/route_scope.go +95 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/route_scope_test.go +78 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/service_identity_test.go +387 -0
- package/dist/resources/templates/go-backend/internal/common/jsonschema/compiler.go +100 -0
- package/dist/resources/templates/go-backend/internal/common/jsonschema/compiler_test.go +41 -0
- package/dist/resources/templates/go-backend/internal/common/jwksapi/handler.go +83 -0
- package/dist/resources/templates/go-backend/internal/common/jwksapi/handler_test.go +104 -0
- package/dist/resources/templates/go-backend/internal/common/locale/locale.go +145 -0
- package/dist/resources/templates/go-backend/internal/common/locale/locale_test.go +75 -0
- package/dist/resources/templates/go-backend/internal/common/mediaref/mediaref.go +54 -0
- package/dist/resources/templates/go-backend/internal/common/mediaref/mediaref_test.go +77 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/apikey_auth.go +320 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/apikey_throttle.go +158 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/auth.go +40 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/auth_test.go +50 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/client_context.go +86 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/cors.go +68 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/event_context.go +18 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/gateway_identity.go +279 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/helpers.go +159 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/iam_delegated_route_permission.go +171 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/iam_delegated_token_auth.go +151 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/iam_delegated_token_auth_test.go +99 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/iam_service_identity.go +138 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/iam_service_token_auth.go +74 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/idempotent.go +273 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/idempotent_test.go +83 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/internal_route_permission.go +45 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/internal_route_permission_test.go +186 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/logging.go +191 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/metrics.go +45 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/middleware_test.go +1516 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/principal_context.go +160 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/principal_context_test.go +355 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/product_context.go +100 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/recovery.go +40 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/referral_attribution.go +178 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/requestid.go +48 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/resource_permission.go +790 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/resource_permission_test.go +440 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/service_credential_auth.go +67 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/tenant.go +148 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/throttle.go +310 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/trusted_client_ip.go +23 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/trusted_client_ip_test.go +60 -0
- package/dist/resources/templates/go-backend/internal/common/money/currency.go +25 -0
- package/dist/resources/templates/go-backend/internal/common/money/errors.go +23 -0
- package/dist/resources/templates/go-backend/internal/common/money/format.go +123 -0
- package/dist/resources/templates/go-backend/internal/common/money/format_test.go +204 -0
- package/dist/resources/templates/go-backend/internal/common/money/gateway.go +144 -0
- package/dist/resources/templates/go-backend/internal/common/money/gateway_test.go +165 -0
- package/dist/resources/templates/go-backend/internal/common/money/rounding.go +56 -0
- package/dist/resources/templates/go-backend/internal/common/money/rounding_test.go +51 -0
- package/dist/resources/templates/go-backend/internal/common/principal/context.go +56 -0
- package/dist/resources/templates/go-backend/internal/common/principal/principal.go +49 -0
- package/dist/resources/templates/go-backend/internal/common/productcode/code.go +24 -0
- package/dist/resources/templates/go-backend/internal/common/productcode/code_test.go +35 -0
- package/dist/resources/templates/go-backend/internal/common/providerconfig/schema.go +577 -0
- package/dist/resources/templates/go-backend/internal/common/providerconfig/schema_test.go +47 -0
- package/dist/resources/templates/go-backend/internal/common/requestprivacy/request.go +246 -0
- package/dist/resources/templates/go-backend/internal/common/requestprivacy/request_test.go +170 -0
- package/dist/resources/templates/go-backend/internal/common/response/pagination.go +60 -0
- package/dist/resources/templates/go-backend/internal/common/response/response.go +110 -0
- package/dist/resources/templates/go-backend/internal/common/response/response_test.go +463 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/ast.go +141 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/ast_test.go +81 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/canonical.go +184 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/canonical_test.go +319 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/decoder.go +555 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/decoder_test.go +260 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/errors.go +55 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/evaluator.go +96 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/evaluator_test.go +220 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/fuzz_test.go +116 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/limits.go +331 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/schema.go +412 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/schema_test.go +164 -0
- package/dist/resources/templates/go-backend/internal/common/runtimeinput/runtime_input.go +627 -0
- package/dist/resources/templates/go-backend/internal/common/runtimeinput/runtime_input_test.go +245 -0
- package/dist/resources/templates/go-backend/internal/common/semver/errors.go +6 -0
- package/dist/resources/templates/go-backend/internal/common/semver/semver.go +125 -0
- package/dist/resources/templates/go-backend/internal/common/semver/semver_test.go +205 -0
- package/dist/resources/templates/go-backend/internal/common/serviceidentity/audience.go +48 -0
- package/dist/resources/templates/go-backend/internal/common/serviceidentity/audience_test.go +35 -0
- package/dist/resources/templates/go-backend/internal/common/serviceidentity/resource_identifier.go +33 -0
- package/dist/resources/templates/go-backend/internal/common/serviceidentity/resource_identifier_test.go +32 -0
- package/dist/resources/templates/go-backend/internal/common/tradeno/generator.go +99 -0
- package/dist/resources/templates/go-backend/internal/common/tradeno/generator_test.go +396 -0
- package/dist/resources/templates/go-backend/internal/common/tradeno/prefix.go +247 -0
- package/dist/resources/templates/go-backend/internal/common/types/asset_url.go +11 -0
- package/dist/resources/templates/go-backend/internal/common/types/billing.go +89 -0
- package/dist/resources/templates/go-backend/internal/common/types/billing_test.go +77 -0
- package/dist/resources/templates/go-backend/internal/common/types/country.go +74 -0
- package/dist/resources/templates/go-backend/internal/common/types/country_test.go +28 -0
- package/dist/resources/templates/go-backend/internal/common/types/currency.go +60 -0
- package/dist/resources/templates/go-backend/internal/common/types/identifiers.go +41 -0
- package/dist/resources/templates/go-backend/internal/common/types/operator.go +28 -0
- package/dist/resources/templates/go-backend/internal/common/types/subscription_entitlement.go +24 -0
- package/dist/resources/templates/go-backend/internal/common/types/subscription_entitlement_test.go +35 -0
- package/dist/resources/templates/go-backend/internal/common/types/tier.go +72 -0
- package/dist/resources/templates/go-backend/internal/common/types/types.go +411 -0
- package/dist/resources/templates/go-backend/internal/common/types/types_test.go +295 -0
- package/dist/resources/templates/go-backend/internal/common/utils/constants.go +100 -0
- package/dist/resources/templates/go-backend/internal/common/utils/crypto.go +158 -0
- package/dist/resources/templates/go-backend/internal/common/utils/crypto_test.go +345 -0
- package/dist/resources/templates/go-backend/internal/common/utils/duration.go +25 -0
- package/dist/resources/templates/go-backend/internal/common/utils/helpers.go +18 -0
- package/dist/resources/templates/go-backend/internal/common/utils/json.go +62 -0
- package/dist/resources/templates/go-backend/internal/common/utils/json_test.go +76 -0
- package/dist/resources/templates/go-backend/internal/common/utils/pagination.go +51 -0
- package/dist/resources/templates/go-backend/internal/common/utils/pagination_test.go +45 -0
- package/dist/resources/templates/go-backend/internal/common/utils/pointer.go +107 -0
- package/dist/resources/templates/go-backend/internal/common/utils/pointer_test.go +15 -0
- package/dist/resources/templates/go-backend/internal/common/utils/slice.go +24 -0
- package/dist/resources/templates/go-backend/internal/common/utils/snowflake.go +110 -0
- package/dist/resources/templates/go-backend/internal/common/utils/snowflake_test.go +316 -0
- package/dist/resources/templates/go-backend/internal/common/utils/string.go +16 -0
- package/dist/resources/templates/go-backend/internal/common/utils/timefmt.go +42 -0
- package/dist/resources/templates/go-backend/internal/common/validator/validator.go +136 -0
- package/dist/resources/templates/go-backend/internal/common/validator/validator_test.go +240 -0
- package/dist/resources/templates/go-backend/internal/common/youtubeapi/client.go +224 -0
- package/dist/resources/templates/go-backend/internal/core/apicontract/document.go +269 -0
- package/dist/resources/templates/go-backend/internal/core/apicontract/document_test.go +104 -0
- package/dist/resources/templates/go-backend/internal/core/apicontract/route_validator.go +78 -0
- package/dist/resources/templates/go-backend/internal/core/apicontract/route_validator_test.go +108 -0
- package/dist/resources/templates/go-backend/internal/core/apidocs/assets/scalar-api-reference.js +2385 -0
- package/dist/resources/templates/go-backend/internal/core/apidocs/assets/scalar-api-reference.metadata.json +10 -0
- package/dist/resources/templates/go-backend/internal/core/apidocs/route.go +107 -0
- package/dist/resources/templates/go-backend/internal/core/apidocs/route_test.go +200 -0
- package/dist/resources/templates/go-backend/internal/core/apidocs/scalar.go +117 -0
- package/dist/resources/templates/go-backend/internal/core/apidocs/scalar_test.go +99 -0
- package/dist/resources/templates/go-backend/internal/core/audit/access_log_policy.go +91 -0
- package/dist/resources/templates/go-backend/internal/core/audit/access_log_policy_test.go +51 -0
- package/dist/resources/templates/go-backend/internal/core/audit/actor_resolver.go +36 -0
- package/dist/resources/templates/go-backend/internal/core/audit/constants.go +30 -0
- package/dist/resources/templates/go-backend/internal/core/audit/context.go +119 -0
- package/dist/resources/templates/go-backend/internal/core/audit/context_test.go +310 -0
- package/dist/resources/templates/go-backend/internal/core/audit/dto.go +468 -0
- package/dist/resources/templates/go-backend/internal/core/audit/dto_admin.go +172 -0
- package/dist/resources/templates/go-backend/internal/core/audit/enum.go +460 -0
- package/dist/resources/templates/go-backend/internal/core/audit/enum_test.go +314 -0
- package/dist/resources/templates/go-backend/internal/core/audit/errors.go +9 -0
- package/dist/resources/templates/go-backend/internal/core/audit/handler.go +568 -0
- package/dist/resources/templates/go-backend/internal/core/audit/listener.go +319 -0
- package/dist/resources/templates/go-backend/internal/core/audit/listener_test.go +98 -0
- package/dist/resources/templates/go-backend/internal/core/audit/middleware.go +669 -0
- package/dist/resources/templates/go-backend/internal/core/audit/middleware_test.go +268 -0
- package/dist/resources/templates/go-backend/internal/core/audit/model.go +306 -0
- package/dist/resources/templates/go-backend/internal/core/audit/partition_job.go +316 -0
- package/dist/resources/templates/go-backend/internal/core/audit/ports.go +9 -0
- package/dist/resources/templates/go-backend/internal/core/audit/provider.go +30 -0
- package/dist/resources/templates/go-backend/internal/core/audit/repository.go +1007 -0
- package/dist/resources/templates/go-backend/internal/core/audit/repository_test.go +934 -0
- package/dist/resources/templates/go-backend/internal/core/audit/router.go +85 -0
- package/dist/resources/templates/go-backend/internal/core/audit/service.go +770 -0
- package/dist/resources/templates/go-backend/internal/core/audit/service_test.go +331 -0
- package/dist/resources/templates/go-backend/internal/core/audit/vo_admin.go +475 -0
- package/dist/resources/templates/go-backend/internal/core/cache/config.go +30 -0
- package/dist/resources/templates/go-backend/internal/core/cache/key.go +41 -0
- package/dist/resources/templates/go-backend/internal/core/cache/key_test.go +62 -0
- package/dist/resources/templates/go-backend/internal/core/cache/l1.go +153 -0
- package/dist/resources/templates/go-backend/internal/core/cache/l1_test.go +212 -0
- package/dist/resources/templates/go-backend/internal/core/cache/manager.go +278 -0
- package/dist/resources/templates/go-backend/internal/core/cache/manager_provider.go +24 -0
- package/dist/resources/templates/go-backend/internal/core/cache/metrics.go +126 -0
- package/dist/resources/templates/go-backend/internal/core/cache/namespace.go +46 -0
- package/dist/resources/templates/go-backend/internal/core/cache/namespace_test.go +90 -0
- package/dist/resources/templates/go-backend/internal/core/cache/options.go +39 -0
- package/dist/resources/templates/go-backend/internal/core/cache/options_test.go +96 -0
- package/dist/resources/templates/go-backend/internal/core/cache/redis.go +311 -0
- package/dist/resources/templates/go-backend/internal/core/cache/redis_test.go +51 -0
- package/dist/resources/templates/go-backend/internal/core/cache/serializer.go +31 -0
- package/dist/resources/templates/go-backend/internal/core/cache/serializer_test.go +139 -0
- package/dist/resources/templates/go-backend/internal/core/cache/singleflight.go +53 -0
- package/dist/resources/templates/go-backend/internal/core/cache/singleflight_test.go +211 -0
- package/dist/resources/templates/go-backend/internal/core/cache/warming.go +86 -0
- package/dist/resources/templates/go-backend/internal/core/clock/clock.go +66 -0
- package/dist/resources/templates/go-backend/internal/core/clock/clock_test.go +26 -0
- package/dist/resources/templates/go-backend/internal/core/clock/runtime_clock.go +283 -0
- package/dist/resources/templates/go-backend/internal/core/clock/runtime_clock_test.go +98 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/errors.go +6 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/model.go +90 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/model_test.go +79 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/namespace.go +7 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/persistence.go +269 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/persistence_test.go +185 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/provider.go +9 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/repository.go +22 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/service.go +853 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/service_persistence_test.go +432 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/service_test.go +891 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/types.go +121 -0
- package/dist/resources/templates/go-backend/internal/core/config/config.go +2069 -0
- package/dist/resources/templates/go-backend/internal/core/config/config_test.go +1122 -0
- package/dist/resources/templates/go-backend/internal/core/config/contract/contract_test.go +388 -0
- package/dist/resources/templates/go-backend/internal/core/config/contract/docs.go +198 -0
- package/dist/resources/templates/go-backend/internal/core/config/contract/verify.go +424 -0
- package/dist/resources/templates/go-backend/internal/core/config/enum.go +46 -0
- package/dist/resources/templates/go-backend/internal/core/config/metadata.go +717 -0
- package/dist/resources/templates/go-backend/internal/core/config/metadata_test.go +228 -0
- package/dist/resources/templates/go-backend/internal/core/config/product_admin_config_test.go +210 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/aes_gcm.go +111 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/aes_gcm_test.go +364 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/encryptor.go +12 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/envelope_encryptor.go +134 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/envelope_encryptor_test.go +118 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/pem.go +112 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/pem_test.go +44 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/provider.go +130 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/provider_test.go +92 -0
- package/dist/resources/templates/go-backend/internal/core/database/base_model.go +24 -0
- package/dist/resources/templates/go-backend/internal/core/database/clickhouse.go +121 -0
- package/dist/resources/templates/go-backend/internal/core/database/database.go +112 -0
- package/dist/resources/templates/go-backend/internal/core/database/database_logger_test.go +95 -0
- package/dist/resources/templates/go-backend/internal/core/database/errors.go +25 -0
- package/dist/resources/templates/go-backend/internal/core/database/metrics_plugin.go +105 -0
- package/dist/resources/templates/go-backend/internal/core/database/pool_collector.go +71 -0
- package/dist/resources/templates/go-backend/internal/core/database/read_replica.go +520 -0
- package/dist/resources/templates/go-backend/internal/core/database/transaction.go +170 -0
- package/dist/resources/templates/go-backend/internal/core/database/transaction_test.go +282 -0
- package/dist/resources/templates/go-backend/internal/core/distlock/lock.go +353 -0
- package/dist/resources/templates/go-backend/internal/core/distlock/lock_test.go +106 -0
- package/dist/resources/templates/go-backend/internal/core/errors/handler.go +189 -0
- package/dist/resources/templates/go-backend/internal/core/errors/handler_test.go +515 -0
- package/dist/resources/templates/go-backend/internal/core/eventbus/eventbus.go +667 -0
- package/dist/resources/templates/go-backend/internal/core/eventbus/eventbus_test.go +792 -0
- package/dist/resources/templates/go-backend/internal/core/eventstream/noop.go +22 -0
- package/dist/resources/templates/go-backend/internal/core/eventstream/provider.go +20 -0
- package/dist/resources/templates/go-backend/internal/core/eventstream/publisher.go +38 -0
- package/dist/resources/templates/go-backend/internal/core/eventstream/redis.go +109 -0
- package/dist/resources/templates/go-backend/internal/core/eventstream/routing.go +104 -0
- package/dist/resources/templates/go-backend/internal/core/eventstream/routing_test.go +71 -0
- package/dist/resources/templates/go-backend/internal/core/geoip/model.go +19 -0
- package/dist/resources/templates/go-backend/internal/core/geoip/service.go +88 -0
- package/dist/resources/templates/go-backend/internal/core/health/health.go +333 -0
- package/dist/resources/templates/go-backend/internal/core/health/health_test.go +27 -0
- package/dist/resources/templates/go-backend/internal/core/httpclient/client.go +772 -0
- package/dist/resources/templates/go-backend/internal/core/httpclient/client_test.go +287 -0
- package/dist/resources/templates/go-backend/internal/core/httpclient/download.go +578 -0
- package/dist/resources/templates/go-backend/internal/core/httpclient/download_test.go +374 -0
- package/dist/resources/templates/go-backend/internal/core/jwtkeyring/jwk.go +38 -0
- package/dist/resources/templates/go-backend/internal/core/jwtkeyring/keyring.go +144 -0
- package/dist/resources/templates/go-backend/internal/core/jwtkeyring/keyring_test.go +128 -0
- package/dist/resources/templates/go-backend/internal/core/jwtkeyring/registry.go +60 -0
- package/dist/resources/templates/go-backend/internal/core/logger/logger.go +53 -0
- package/dist/resources/templates/go-backend/internal/core/logger/logger_test.go +194 -0
- package/dist/resources/templates/go-backend/internal/core/observability/metrics.go +358 -0
- package/dist/resources/templates/go-backend/internal/core/observability/sentry.go +445 -0
- package/dist/resources/templates/go-backend/internal/core/observability/sentry_privacy_test.go +175 -0
- package/dist/resources/templates/go-backend/internal/core/observability/tracing.go +187 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/model.go +314 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/model_test.go +77 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/persistence.go +51 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/provider.go +23 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/relay.go +1138 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/relay_postgres_test.go +257 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/relay_test.go +690 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/rescheduler.go +164 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/rescheduler_test.go +209 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/writer.go +249 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/writer_postgres_test.go +170 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/writer_test.go +123 -0
- package/dist/resources/templates/go-backend/internal/core/provider.go +198 -0
- package/dist/resources/templates/go-backend/internal/core/queue/metrics_middleware.go +38 -0
- package/dist/resources/templates/go-backend/internal/core/queue/metrics_middleware_test.go +12 -0
- package/dist/resources/templates/go-backend/internal/core/queue/queue.go +288 -0
- package/dist/resources/templates/go-backend/internal/core/queue/queue_test.go +159 -0
- package/dist/resources/templates/go-backend/internal/core/resilience/bulkhead.go +514 -0
- package/dist/resources/templates/go-backend/internal/core/resilience/bulkhead_test.go +984 -0
- package/dist/resources/templates/go-backend/internal/core/resilience/circuitbreaker.go +358 -0
- package/dist/resources/templates/go-backend/internal/core/resilience/circuitbreaker_test.go +788 -0
- package/dist/resources/templates/go-backend/internal/core/resilience/retry.go +318 -0
- package/dist/resources/templates/go-backend/internal/core/resilience/retry_test.go +735 -0
- package/dist/resources/templates/go-backend/internal/core/schedule/recorder.go +305 -0
- package/dist/resources/templates/go-backend/internal/core/schedule/run_context.go +124 -0
- package/dist/resources/templates/go-backend/internal/core/schedule/run_context_test.go +85 -0
- package/dist/resources/templates/go-backend/internal/core/schedule/scheduler.go +1589 -0
- package/dist/resources/templates/go-backend/internal/core/schedule/scheduler_control_test.go +808 -0
- package/dist/resources/templates/go-backend/internal/core/storage/oss_provider.go +398 -0
- package/dist/resources/templates/go-backend/internal/core/storage/oss_provider_test.go +89 -0
- package/dist/resources/templates/go-backend/internal/core/storage/path.go +110 -0
- package/dist/resources/templates/go-backend/internal/core/storage/path_test.go +301 -0
- package/dist/resources/templates/go-backend/internal/core/storage/s3_provider.go +489 -0
- package/dist/resources/templates/go-backend/internal/core/storage/s3_provider_test.go +60 -0
- package/dist/resources/templates/go-backend/internal/core/storage/storage.go +268 -0
- package/dist/resources/templates/go-backend/internal/core/storage/validation.go +225 -0
- package/dist/resources/templates/go-backend/internal/core/storage/validation_test.go +495 -0
- package/dist/resources/templates/go-backend/internal/core/temporal/logger.go +38 -0
- package/dist/resources/templates/go-backend/internal/core/temporal/provider.go +126 -0
- package/dist/resources/templates/go-backend/internal/core/temporal/provider_test.go +67 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/client.go +282 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/client_test.go +81 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/config.go +58 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/handler.go +53 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/hub.go +269 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/message.go +273 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/notifier.go +92 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/provider.go +33 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/action.go +77 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/action_test.go +238 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/condition.go +107 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/decision.go +256 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/enum.go +643 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/errors.go +102 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/principal_record.go +21 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/product_context.go +486 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/resource.go +215 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/resource_test.go +242 -0
- package/dist/resources/templates/go-backend/internal/products/README.md +5 -0
- package/dist/resources/templates/go-backend/internal/sharedevents/README.md +5 -0
- package/dist/resources/templates/go-backend/migrations/000001_infrastructure.down.sql +13 -0
- package/dist/resources/templates/go-backend/migrations/000001_infrastructure.up.sql +886 -0
- package/dist/resources/templates/go-backend/pkg/apierror/error.go +327 -0
- package/dist/resources/templates/go-backend/pkg/apierror/error_code.go +65 -0
- package/dist/resources/templates/go-backend/pkg/apierror/error_test.go +41 -0
- package/dist/resources/templates/go-backend/pkg/apierror/error_type.go +14 -0
- package/dist/resources/templates/go-backend/pkg/apierror/http_response.go +216 -0
- package/dist/resources/templates/go-backend/pkg/apierror/http_response_test.go +216 -0
- package/dist/resources/templates/go-backend/scripts/check-openapi-clean-deletion.sh +54 -0
- package/dist/resources/templates/go-backend/scripts/test-migrations.sh +32 -0
- package/dist/resources/templates/go-backend/tests/infrastructure.sql +69 -0
- package/dist/resources/templates/mcp-server/.claude/docs/stack.md +338 -0
- package/dist/resources/templates/mcp-server/.editorconfig +13 -0
- package/dist/resources/templates/mcp-server/.env.example +8 -0
- package/dist/resources/templates/mcp-server/.github/ISSUE_TEMPLATE/bug_report.yml +104 -0
- package/dist/resources/templates/mcp-server/.github/ISSUE_TEMPLATE/feature_request.yml +54 -0
- package/dist/resources/templates/mcp-server/.github/PULL_REQUEST_TEMPLATE.md +36 -0
- package/dist/resources/templates/mcp-server/.github/workflows/ci.yml +78 -0
- package/dist/resources/templates/mcp-server/.husky/commit-msg +1 -0
- package/dist/resources/templates/mcp-server/.husky/pre-commit +1 -0
- package/dist/resources/templates/mcp-server/.lintstagedrc.json +4 -0
- package/dist/resources/templates/mcp-server/.nvmrc +1 -0
- package/dist/resources/templates/mcp-server/.prettierrc +8 -0
- package/dist/resources/templates/mcp-server/CONTRIBUTING.md +184 -0
- package/dist/resources/templates/mcp-server/Dockerfile +23 -0
- package/dist/resources/templates/mcp-server/LICENSE +21 -0
- package/dist/resources/templates/mcp-server/README.md +123 -0
- package/dist/resources/templates/mcp-server/_gitignore +26 -0
- package/dist/resources/templates/mcp-server/commitlint.config.js +24 -0
- package/dist/resources/templates/mcp-server/eslint.config.js +30 -0
- package/dist/resources/templates/mcp-server/package.json +72 -0
- package/dist/resources/templates/mcp-server/src/index.ts +27 -0
- package/dist/resources/templates/mcp-server/src/resources.ts +33 -0
- package/dist/resources/templates/mcp-server/src/services/.gitkeep +0 -0
- package/dist/resources/templates/mcp-server/src/tools.ts +43 -0
- package/dist/resources/templates/mcp-server/src/types/index.ts +19 -0
- package/dist/resources/templates/mcp-server/src/utils/index.ts +31 -0
- package/dist/resources/templates/mcp-server/tsconfig.json +22 -0
- package/dist/resources/templates/mcp-server/tsup.config.ts +14 -0
- package/dist/resources/templates/mcp-server/vitest.config.ts +15 -0
- package/dist/resources/templates/nuxt/.claude/docs/stack.md +666 -0
- package/dist/resources/templates/nuxt/.editorconfig +15 -0
- package/dist/resources/templates/nuxt/.env.example +22 -0
- package/dist/resources/templates/nuxt/.github/workflows/ci.yml +34 -0
- package/dist/resources/templates/nuxt/.husky/commit-msg +1 -0
- package/dist/resources/templates/nuxt/.husky/pre-commit +1 -0
- package/dist/resources/templates/nuxt/.nvmrc +1 -0
- package/dist/resources/templates/nuxt/.prettierignore +9 -0
- package/dist/resources/templates/nuxt/.prettierrc +1 -0
- package/dist/resources/templates/nuxt/README.md +23 -0
- package/dist/resources/templates/nuxt/_gitignore +7 -0
- package/dist/resources/templates/nuxt/app/app.config.ts +105 -0
- package/dist/resources/templates/nuxt/app/i18n/locales/en.json +5 -0
- package/dist/resources/templates/nuxt/app/pages/app/index.vue +224 -0
- package/dist/resources/templates/nuxt/app/pages/authors/[slug].vue +37 -0
- package/dist/resources/templates/nuxt/app/pages/blog/[slug].vue +55 -0
- package/dist/resources/templates/nuxt/app/pages/blog/category/[category].vue +38 -0
- package/dist/resources/templates/nuxt/app/pages/blog/index.vue +46 -0
- package/dist/resources/templates/nuxt/app/pages/index.vue +626 -0
- package/dist/resources/templates/nuxt/app/pages/legal/privacy.vue +21 -0
- package/dist/resources/templates/nuxt/app/pages/legal/terms.vue +21 -0
- package/dist/resources/templates/nuxt/app/pages/ui/index.vue +397 -0
- package/dist/resources/templates/nuxt/commitlint.config.mjs +1 -0
- package/dist/resources/templates/nuxt/eslint.config.mjs +4 -0
- package/dist/resources/templates/nuxt/lint-staged.config.mjs +8 -0
- package/dist/resources/templates/nuxt/nuxt.config.ts +71 -0
- package/dist/resources/templates/nuxt/package.json +67 -0
- package/dist/resources/templates/nuxt/public/apple-touch-icon.png +0 -0
- package/dist/resources/templates/nuxt/public/favicon.ico +0 -0
- package/dist/resources/templates/nuxt/public/favicon.svg +5 -0
- package/dist/resources/templates/nuxt/public/icon-16x16.png +0 -0
- package/dist/resources/templates/nuxt/public/icon-192x192.png +0 -0
- package/dist/resources/templates/nuxt/public/icon-32x32.png +0 -0
- package/dist/resources/templates/nuxt/public/icon-512x512.png +0 -0
- package/dist/resources/templates/nuxt/public/logo.svg +6 -0
- package/dist/resources/templates/nuxt/public/media/hero-product-gallery.png +0 -0
- package/dist/resources/templates/nuxt/server/api/content/sitemap-urls.get.ts +12 -0
- package/dist/resources/templates/nuxt/server/api/preview.get.ts +9 -0
- package/dist/resources/templates/nuxt/stylelint.config.mjs +3 -0
- package/dist/resources/templates/nuxt/test/e2e/rendering.test.ts +13 -0
- package/dist/resources/templates/nuxt/tsconfig.json +3 -0
- package/dist/resources/templates/nuxt/vitest.config.ts +8 -0
- package/package.json +16 -12
|
@@ -0,0 +1,1516 @@
|
|
|
1
|
+
package middleware
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"encoding/json"
|
|
6
|
+
"errors"
|
|
7
|
+
"fmt"
|
|
8
|
+
"net/http"
|
|
9
|
+
"net/http/httptest"
|
|
10
|
+
"strings"
|
|
11
|
+
"testing"
|
|
12
|
+
"time"
|
|
13
|
+
|
|
14
|
+
"github.com/gin-gonic/gin"
|
|
15
|
+
"github.com/stretchr/testify/assert"
|
|
16
|
+
"github.com/stretchr/testify/require"
|
|
17
|
+
"go.uber.org/zap"
|
|
18
|
+
"go.uber.org/zap/zaptest/observer"
|
|
19
|
+
|
|
20
|
+
"{{module}}/internal/common/ctxutil"
|
|
21
|
+
"{{module}}/internal/common/iamscope"
|
|
22
|
+
"{{module}}/internal/common/types"
|
|
23
|
+
coreclock "{{module}}/internal/core/clock"
|
|
24
|
+
"{{module}}/internal/core/config"
|
|
25
|
+
iamdomain "{{module}}/internal/integrations/onex/iam"
|
|
26
|
+
"{{module}}/pkg/apierror"
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
func init() {
|
|
30
|
+
gin.SetMode(gin.TestMode)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
func TestLoggingRedactsSensitiveQueryValues(t *testing.T) {
|
|
34
|
+
core, logs := observer.New(zap.InfoLevel)
|
|
35
|
+
logger := zap.New(core)
|
|
36
|
+
engine := gin.New()
|
|
37
|
+
engine.Use(LoggingWithConfig(logger, config.LogConfig{
|
|
38
|
+
RequestSuccessLevel: "info",
|
|
39
|
+
RequestSuccessSampleRate: 1,
|
|
40
|
+
RequestSlowThreshold: time.Second,
|
|
41
|
+
}))
|
|
42
|
+
engine.GET("/api/v1/ws", func(c *gin.Context) {
|
|
43
|
+
c.Status(http.StatusOK)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
req := httptest.NewRequest(http.MethodGet, "/api/v1/ws?token=jwt-secret&page=1", nil)
|
|
47
|
+
w := httptest.NewRecorder()
|
|
48
|
+
engine.ServeHTTP(w, req)
|
|
49
|
+
|
|
50
|
+
require.Equal(t, http.StatusOK, w.Code)
|
|
51
|
+
require.Len(t, logs.All(), 1)
|
|
52
|
+
fields := logs.All()[0].ContextMap()
|
|
53
|
+
assert.Equal(t, "/api/v1/ws?page=1&token=redacted", fields["path"])
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
func TestLoggingUsesInboundURLSnapshot(t *testing.T) {
|
|
57
|
+
core, logs := observer.New(zap.InfoLevel)
|
|
58
|
+
logger := zap.New(core)
|
|
59
|
+
engine := gin.New()
|
|
60
|
+
engine.Use(LoggingWithConfig(logger, config.LogConfig{
|
|
61
|
+
RequestSuccessLevel: "info",
|
|
62
|
+
RequestSuccessSampleRate: 1,
|
|
63
|
+
RequestSlowThreshold: time.Second,
|
|
64
|
+
}))
|
|
65
|
+
engine.GET("/api/v1/ws", func(c *gin.Context) {
|
|
66
|
+
c.Request.URL.RawQuery = "token=handler-secret"
|
|
67
|
+
c.Status(http.StatusOK)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
req := httptest.NewRequest(http.MethodGet, "/api/v1/ws?page=1", nil)
|
|
71
|
+
w := httptest.NewRecorder()
|
|
72
|
+
engine.ServeHTTP(w, req)
|
|
73
|
+
|
|
74
|
+
require.Equal(t, http.StatusOK, w.Code)
|
|
75
|
+
require.Len(t, logs.All(), 1)
|
|
76
|
+
fields := logs.All()[0].ContextMap()
|
|
77
|
+
assert.Equal(t, "/api/v1/ws?page=1", fields["path"])
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
func TestLoggingRedactsSensitivePathParameter(t *testing.T) {
|
|
81
|
+
core, logs := observer.New(zap.InfoLevel)
|
|
82
|
+
logger := zap.New(core)
|
|
83
|
+
engine := gin.New()
|
|
84
|
+
engine.Use(LoggingWithConfig(logger, config.LogConfig{
|
|
85
|
+
RequestSuccessLevel: "info",
|
|
86
|
+
RequestSuccessSampleRate: 1,
|
|
87
|
+
RequestSlowThreshold: time.Second,
|
|
88
|
+
}))
|
|
89
|
+
engine.GET("/iap/google/orders/by-token/:purchaseToken", func(c *gin.Context) {
|
|
90
|
+
c.Status(http.StatusOK)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
req := httptest.NewRequest(http.MethodGet, "/iap/google/orders/by-token/secret-purchase-token", nil)
|
|
94
|
+
w := httptest.NewRecorder()
|
|
95
|
+
engine.ServeHTTP(w, req)
|
|
96
|
+
|
|
97
|
+
require.Equal(t, http.StatusOK, w.Code)
|
|
98
|
+
require.Len(t, logs.All(), 1)
|
|
99
|
+
fields := logs.All()[0].ContextMap()
|
|
100
|
+
assert.Equal(t, "/iap/google/orders/by-token/:purchaseToken", fields["path"])
|
|
101
|
+
assert.NotContains(t, fields["path"], "secret-purchase-token")
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
func TestLoggingRecordsErrorMetadataWithoutRawErrorText(t *testing.T) {
|
|
105
|
+
core, logs := observer.New(zap.DebugLevel)
|
|
106
|
+
logger := zap.New(core)
|
|
107
|
+
engine := gin.New()
|
|
108
|
+
engine.Use(LoggingWithConfig(logger, config.LogConfig{
|
|
109
|
+
RequestSuccessLevel: "debug",
|
|
110
|
+
RequestSuccessSampleRate: 0,
|
|
111
|
+
RequestSlowThreshold: time.Second,
|
|
112
|
+
}))
|
|
113
|
+
engine.GET("/api/v1/orders/:id", func(c *gin.Context) {
|
|
114
|
+
_ = c.Error(fmt.Errorf("pq: duplicate key users_email_key email=user@example.com token=token-secret")).SetType(gin.ErrorTypePrivate)
|
|
115
|
+
_ = c.Error(apierror.BadRequest(apierror.CodeValidationFailed, "invalid email user@example.com with token-secret")).SetType(gin.ErrorTypeBind)
|
|
116
|
+
c.Status(http.StatusBadRequest)
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
req := httptest.NewRequest(http.MethodGet, "/api/v1/orders/order-1", nil)
|
|
120
|
+
w := httptest.NewRecorder()
|
|
121
|
+
engine.ServeHTTP(w, req)
|
|
122
|
+
|
|
123
|
+
require.Equal(t, http.StatusBadRequest, w.Code)
|
|
124
|
+
require.Len(t, logs.All(), 1)
|
|
125
|
+
fields := logs.All()[0].ContextMap()
|
|
126
|
+
assert.EqualValues(t, 2, fields["error_count"])
|
|
127
|
+
assert.EqualValues(t, uint64(gin.ErrorTypePrivate|gin.ErrorTypeBind), fields["error_type"])
|
|
128
|
+
assert.Equal(t, apierror.CodeValidationFailed, fields["error_code"])
|
|
129
|
+
assert.NotContains(t, fields, "error")
|
|
130
|
+
encoded, err := json.Marshal(fields)
|
|
131
|
+
require.NoError(t, err)
|
|
132
|
+
for _, secret := range []string{"duplicate key", "users_email_key", "user@example.com", "token-secret"} {
|
|
133
|
+
assert.NotContains(t, string(encoded), secret)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
func TestRecoveryUsesRouteTemplateForPaymentResultToken(t *testing.T) {
|
|
138
|
+
core, logs := observer.New(zap.ErrorLevel)
|
|
139
|
+
logger := zap.New(core)
|
|
140
|
+
engine := gin.New()
|
|
141
|
+
engine.Use(Recovery(logger))
|
|
142
|
+
engine.GET("/api/v1/payment-results/:paymentResultToken", func(*gin.Context) {
|
|
143
|
+
panic("payment result handler failure")
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
req := httptest.NewRequest(http.MethodGet, "/api/v1/payment-results/secret-result-token", nil)
|
|
147
|
+
w := httptest.NewRecorder()
|
|
148
|
+
engine.ServeHTTP(w, req)
|
|
149
|
+
|
|
150
|
+
require.Equal(t, http.StatusInternalServerError, w.Code)
|
|
151
|
+
require.Len(t, logs.All(), 1)
|
|
152
|
+
fields := logs.All()[0].ContextMap()
|
|
153
|
+
assert.Equal(t, "/api/v1/payment-results/:paymentResultToken", fields["path"])
|
|
154
|
+
assert.NotContains(t, fields["path"], "secret-result-token")
|
|
155
|
+
assert.Equal(t, "string", fields["panic_type"])
|
|
156
|
+
assert.NotContains(t, fields, "panic")
|
|
157
|
+
assert.NotContains(t, w.Body.String(), "payment result handler failure")
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
func TestLoggingSuppressesNormalSuccessfulReads(t *testing.T) {
|
|
161
|
+
core, logs := observer.New(zap.DebugLevel)
|
|
162
|
+
logger := zap.New(core)
|
|
163
|
+
engine := gin.New()
|
|
164
|
+
engine.Use(LoggingWithConfig(logger, config.LogConfig{
|
|
165
|
+
RequestSuccessLevel: "debug",
|
|
166
|
+
RequestSuccessSampleRate: 0,
|
|
167
|
+
RequestSlowThreshold: time.Second,
|
|
168
|
+
}))
|
|
169
|
+
engine.GET("/api/v1/templates", func(c *gin.Context) {
|
|
170
|
+
c.Status(http.StatusOK)
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
req := httptest.NewRequest(http.MethodGet, "/api/v1/templates", nil)
|
|
174
|
+
w := httptest.NewRecorder()
|
|
175
|
+
engine.ServeHTTP(w, req)
|
|
176
|
+
|
|
177
|
+
require.Equal(t, http.StatusOK, w.Code)
|
|
178
|
+
assert.Empty(t, logs.All())
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
func TestLoggingKeepsWriteAndFailureSignals(t *testing.T) {
|
|
182
|
+
core, logs := observer.New(zap.DebugLevel)
|
|
183
|
+
logger := zap.New(core)
|
|
184
|
+
engine := gin.New()
|
|
185
|
+
engine.Use(LoggingWithConfig(logger, config.LogConfig{
|
|
186
|
+
RequestSuccessLevel: "debug",
|
|
187
|
+
RequestSuccessSampleRate: 0,
|
|
188
|
+
RequestSlowThreshold: time.Second,
|
|
189
|
+
}))
|
|
190
|
+
engine.POST("/test/resources", func(c *gin.Context) {
|
|
191
|
+
c.Status(http.StatusCreated)
|
|
192
|
+
})
|
|
193
|
+
engine.GET("/test/resources/:id", func(c *gin.Context) {
|
|
194
|
+
c.Status(http.StatusInternalServerError)
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
postReq := httptest.NewRequest(http.MethodPost, "/test/resources", nil)
|
|
198
|
+
postW := httptest.NewRecorder()
|
|
199
|
+
engine.ServeHTTP(postW, postReq)
|
|
200
|
+
|
|
201
|
+
getReq := httptest.NewRequest(http.MethodGet, "/test/resources/1", nil)
|
|
202
|
+
getW := httptest.NewRecorder()
|
|
203
|
+
engine.ServeHTTP(getW, getReq)
|
|
204
|
+
|
|
205
|
+
require.Equal(t, http.StatusCreated, postW.Code)
|
|
206
|
+
require.Equal(t, http.StatusInternalServerError, getW.Code)
|
|
207
|
+
require.Len(t, logs.All(), 2)
|
|
208
|
+
assert.Equal(t, zap.InfoLevel, logs.All()[0].Level)
|
|
209
|
+
assert.Equal(t, zap.ErrorLevel, logs.All()[1].Level)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
func TestSummarizeScopeConstraintUsesBoundedShape(t *testing.T) {
|
|
213
|
+
scope := iamscope.ScopeConstraint{
|
|
214
|
+
AllowGroups: [][]iamscope.DimPredicate{
|
|
215
|
+
{
|
|
216
|
+
{Dimension: iamscope.ScopeDimensionProduct, Operator: iamscope.ScopeOpEquals, Values: []string{"catalog"}},
|
|
217
|
+
{Dimension: iamscope.ScopeDimensionCreatedBySelf, Operator: iamscope.ScopeOpSelf},
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
DenyGroups: [][]iamscope.DimPredicate{
|
|
221
|
+
{
|
|
222
|
+
{Dimension: iamscope.ScopeDimensionTenant, Operator: iamscope.ScopeOpEquals, Values: []string{"1"}},
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
require.Equal(t, "allow_groups=1;deny_groups=1;dimensions=created_by_self,product,tenant", summarizeScopeConstraint(scope))
|
|
228
|
+
require.Equal(t, "unbounded", summarizeScopeConstraint(iamscope.NewUnboundedScope()))
|
|
229
|
+
require.Equal(t, "empty", summarizeScopeConstraint(iamscope.ScopeConstraint{}))
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// newTestContext 创建带有真实 http.Request 的测试用 gin.Context
|
|
233
|
+
//
|
|
234
|
+
//nolint:unparam // method 保留参数以支持未来不同 HTTP 方法的测试
|
|
235
|
+
func newTestContext(method, path string) (*gin.Context, *httptest.ResponseRecorder) {
|
|
236
|
+
w := httptest.NewRecorder()
|
|
237
|
+
c, _ := gin.CreateTestContext(w)
|
|
238
|
+
c.Request = httptest.NewRequest(method, path, nil)
|
|
239
|
+
return c, w
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
var testGatewayTokenExpiresAt = time.Unix(2_200_003_600, 0).UTC()
|
|
243
|
+
|
|
244
|
+
type fakeGatewayBlacklistChecker struct {
|
|
245
|
+
revokedJTIs map[string]bool
|
|
246
|
+
err error
|
|
247
|
+
sessionErr error
|
|
248
|
+
sessionOK *bool
|
|
249
|
+
sessionID int64
|
|
250
|
+
identityID types.IdentityID
|
|
251
|
+
userID types.UserID
|
|
252
|
+
userPublicID string
|
|
253
|
+
authProductCode string
|
|
254
|
+
accessMode GatewayIdentityAccessMode
|
|
255
|
+
accessErr error
|
|
256
|
+
resolveCalls int
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
func (f *fakeGatewayBlacklistChecker) ResolveGatewayAuthState(_ context.Context, req GatewayAuthStateRequest) (GatewayAuthState, error) {
|
|
260
|
+
f.resolveCalls++
|
|
261
|
+
if f.err != nil {
|
|
262
|
+
return GatewayAuthState{}, f.err
|
|
263
|
+
}
|
|
264
|
+
if f.revokedJTIs[req.TokenJTI] {
|
|
265
|
+
return GatewayAuthState{}, nil
|
|
266
|
+
}
|
|
267
|
+
if f.sessionErr != nil {
|
|
268
|
+
return GatewayAuthState{}, f.sessionErr
|
|
269
|
+
}
|
|
270
|
+
f.sessionID = req.SessionID
|
|
271
|
+
f.identityID = req.IdentityID
|
|
272
|
+
f.userPublicID = req.UserPublicID
|
|
273
|
+
f.authProductCode = req.AuthProductCode
|
|
274
|
+
var resolvedUserID int64
|
|
275
|
+
if _, scanErr := fmt.Sscanf(req.UserPublicID, "usr_%d", &resolvedUserID); scanErr == nil && resolvedUserID > 0 {
|
|
276
|
+
f.userID = types.UserID(resolvedUserID)
|
|
277
|
+
} else {
|
|
278
|
+
f.userID = types.UserID(12)
|
|
279
|
+
}
|
|
280
|
+
if f.sessionOK != nil && !*f.sessionOK {
|
|
281
|
+
return GatewayAuthState{}, nil
|
|
282
|
+
}
|
|
283
|
+
if f.accessErr != nil {
|
|
284
|
+
return GatewayAuthState{}, f.accessErr
|
|
285
|
+
}
|
|
286
|
+
accessMode := f.accessMode
|
|
287
|
+
if accessMode == "" {
|
|
288
|
+
accessMode = GatewayIdentityAccessModeAllowed
|
|
289
|
+
}
|
|
290
|
+
return GatewayAuthState{Active: true, UserID: f.userID, AccessMode: accessMode}, nil
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
func newGatewayIdentityMiddleware(checker GatewayAuthStateChecker) gin.HandlerFunc {
|
|
294
|
+
return GatewayIdentity(checker)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
func applyGatewayAuthHeaders(req *http.Request, userID, sessionID int64, jti, tenantID string) {
|
|
298
|
+
applyGatewayAuthHeadersWithIdentity(req, userID, userID, sessionID, jti, tenantID)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
func applyGatewayAuthHeadersWithIdentity(req *http.Request, userID, identityID, sessionID int64, jti, tenantID string) {
|
|
302
|
+
req.Header.Set(types.HeaderUserPublicID, fmt.Sprintf("usr_%d", userID))
|
|
303
|
+
req.Header.Set(types.HeaderIdentityID, fmt.Sprintf("%d", identityID))
|
|
304
|
+
req.Header.Set(types.HeaderUserType, "user")
|
|
305
|
+
req.Header.Set(types.HeaderSessionID, fmt.Sprintf("%d", sessionID))
|
|
306
|
+
req.Header.Set(types.HeaderTokenJTI, jti)
|
|
307
|
+
req.Header.Set(types.HeaderTokenExpiresAt, fmt.Sprintf("%d", testGatewayTokenExpiresAt.Unix()))
|
|
308
|
+
req.Header.Set(types.HeaderTenantID, tenantID)
|
|
309
|
+
req.Header.Set(types.HeaderAuthProductCode, "test-product")
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// ═══════════════════════════════════════════════════════════════
|
|
313
|
+
// GatewayIdentity 测试
|
|
314
|
+
// ═══════════════════════════════════════════════════════════════
|
|
315
|
+
|
|
316
|
+
// TestGatewayIdentity_NoIdentityHeadersPasses 验证公开路由在无网关注入身份时保持放行
|
|
317
|
+
func TestGatewayIdentity_NoIdentityHeadersPasses(t *testing.T) {
|
|
318
|
+
engine := gin.New()
|
|
319
|
+
engine.Use(newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{}))
|
|
320
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
321
|
+
identity := GetUserIdentity(ctx)
|
|
322
|
+
sessionID, hasSession := GetSessionID(ctx)
|
|
323
|
+
assert.Nil(t, identity, "公开路由不应注入身份")
|
|
324
|
+
assert.False(t, hasSession)
|
|
325
|
+
assert.Zero(t, sessionID)
|
|
326
|
+
ctx.Status(http.StatusOK)
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
w := httptest.NewRecorder()
|
|
330
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
331
|
+
engine.ServeHTTP(w, req)
|
|
332
|
+
|
|
333
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// TestGatewayIdentity_ValidHeadersInjectIdentityAndSession 验证合法网关头可注入 identity 与会话上下文
|
|
337
|
+
func TestGatewayIdentity_ValidHeadersInjectIdentityAndSession(t *testing.T) {
|
|
338
|
+
engine := gin.New()
|
|
339
|
+
checker := &fakeGatewayBlacklistChecker{}
|
|
340
|
+
engine.Use(newGatewayIdentityMiddleware(checker))
|
|
341
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
342
|
+
identity := GetUserIdentity(ctx)
|
|
343
|
+
sessionID, hasSession := GetSessionID(ctx)
|
|
344
|
+
tokenJTI, hasJTI := GetAccessTokenJTI(ctx)
|
|
345
|
+
expiresAt, hasExpiresAt := GetAccessTokenExpiresAt(ctx)
|
|
346
|
+
|
|
347
|
+
require.NotNil(t, identity)
|
|
348
|
+
assert.True(t, hasSession)
|
|
349
|
+
assert.True(t, hasJTI)
|
|
350
|
+
assert.True(t, hasExpiresAt)
|
|
351
|
+
assert.Equal(t, types.UserID(12), identity.UserID)
|
|
352
|
+
assert.Equal(t, "usr_12", identity.UserPublicID)
|
|
353
|
+
assert.Equal(t, types.IdentityID(11), identity.IdentityID)
|
|
354
|
+
assert.Equal(t, "user", identity.Type)
|
|
355
|
+
assert.Equal(t, "tenant-abc", identity.TenantID)
|
|
356
|
+
assert.Equal(t, int64(9001), sessionID)
|
|
357
|
+
assert.Equal(t, "jti-valid", tokenJTI)
|
|
358
|
+
assert.Equal(t, testGatewayTokenExpiresAt, expiresAt)
|
|
359
|
+
assert.Equal(t, int64(11), ctxutil.IdentityIDFromContext(ctx.Request.Context()))
|
|
360
|
+
ctx.Status(http.StatusOK)
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
w := httptest.NewRecorder()
|
|
364
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
365
|
+
applyGatewayAuthHeadersWithIdentity(req, 12, 11, 9001, "jti-valid", "tenant-abc")
|
|
366
|
+
engine.ServeHTTP(w, req)
|
|
367
|
+
|
|
368
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
369
|
+
assert.Equal(t, int64(9001), checker.sessionID)
|
|
370
|
+
assert.Equal(t, types.IdentityID(11), checker.identityID)
|
|
371
|
+
assert.Equal(t, types.UserID(12), checker.userID)
|
|
372
|
+
assert.Equal(t, "usr_12", checker.userPublicID)
|
|
373
|
+
assert.Equal(t, "test-product", checker.authProductCode)
|
|
374
|
+
assert.Empty(t, req.Header.Get(types.HeaderProductCode))
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// TestGatewayIdentity_RevokedTokenReturnsUnauthorized 验证命中黑名单的访问令牌被拒绝
|
|
378
|
+
func TestGatewayIdentity_RevokedTokenReturnsUnauthorized(t *testing.T) {
|
|
379
|
+
engine := gin.New()
|
|
380
|
+
engine.Use(newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{
|
|
381
|
+
revokedJTIs: map[string]bool{
|
|
382
|
+
"jti-revoked": true,
|
|
383
|
+
},
|
|
384
|
+
}))
|
|
385
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
386
|
+
ctx.Status(http.StatusOK)
|
|
387
|
+
})
|
|
388
|
+
|
|
389
|
+
w := httptest.NewRecorder()
|
|
390
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
391
|
+
applyGatewayAuthHeaders(req, 42, 9001, "jti-revoked", "tenant-abc")
|
|
392
|
+
engine.ServeHTTP(w, req)
|
|
393
|
+
|
|
394
|
+
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
|
395
|
+
|
|
396
|
+
var body apierror.ErrorResponse
|
|
397
|
+
err := json.Unmarshal(w.Body.Bytes(), &body)
|
|
398
|
+
require.NoError(t, err)
|
|
399
|
+
assert.Equal(t, apierror.CodeUnauthorized, body.Error.Code)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// TestGatewayIdentity_InvalidGatewayHeadersReturnsUnauthorized 验证网关认证事实缺失或非法时请求被拒绝
|
|
403
|
+
func TestGatewayIdentity_InvalidGatewayHeadersReturnsUnauthorized(t *testing.T) {
|
|
404
|
+
engine := gin.New()
|
|
405
|
+
engine.Use(newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{}))
|
|
406
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
407
|
+
ctx.Status(http.StatusOK)
|
|
408
|
+
})
|
|
409
|
+
|
|
410
|
+
w := httptest.NewRecorder()
|
|
411
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
412
|
+
req.Header.Set(types.HeaderUserPublicID, "usr_12")
|
|
413
|
+
req.Header.Set(types.HeaderIdentityID, "11")
|
|
414
|
+
req.Header.Set(types.HeaderSessionID, "not-a-number")
|
|
415
|
+
req.Header.Set(types.HeaderTokenJTI, "jti-invalid")
|
|
416
|
+
req.Header.Set(types.HeaderTokenExpiresAt, fmt.Sprintf("%d", testGatewayTokenExpiresAt.Unix()))
|
|
417
|
+
engine.ServeHTTP(w, req)
|
|
418
|
+
|
|
419
|
+
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
|
420
|
+
|
|
421
|
+
var body apierror.ErrorResponse
|
|
422
|
+
err := json.Unmarshal(w.Body.Bytes(), &body)
|
|
423
|
+
require.NoError(t, err)
|
|
424
|
+
assert.Equal(t, apierror.CodeUnauthorized, body.Error.Code)
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// TestGatewayIdentity_BlacklistServiceUnavailableReturns503 验证撤销态查询失败时按 fail-closed 返回 503
|
|
428
|
+
func TestGatewayIdentity_BlacklistServiceUnavailableReturns503(t *testing.T) {
|
|
429
|
+
engine := gin.New()
|
|
430
|
+
engine.Use(newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{
|
|
431
|
+
err: assert.AnError,
|
|
432
|
+
}))
|
|
433
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
434
|
+
ctx.Status(http.StatusOK)
|
|
435
|
+
})
|
|
436
|
+
|
|
437
|
+
w := httptest.NewRecorder()
|
|
438
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
439
|
+
applyGatewayAuthHeaders(req, 42, 9001, "jti-blacklist-error", "tenant-abc")
|
|
440
|
+
engine.ServeHTTP(w, req)
|
|
441
|
+
|
|
442
|
+
assert.Equal(t, http.StatusServiceUnavailable, w.Code)
|
|
443
|
+
|
|
444
|
+
var body apierror.ErrorResponse
|
|
445
|
+
err := json.Unmarshal(w.Body.Bytes(), &body)
|
|
446
|
+
require.NoError(t, err)
|
|
447
|
+
assert.Equal(t, apierror.CodeServiceUnavailable, body.Error.Code)
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// TestGatewayIdentity_InactiveSessionReturnsUnauthorized 验证会话撤销后访问立即失效
|
|
451
|
+
func TestGatewayIdentity_InactiveSessionReturnsUnauthorized(t *testing.T) {
|
|
452
|
+
inactive := false
|
|
453
|
+
engine := gin.New()
|
|
454
|
+
engine.Use(newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{
|
|
455
|
+
sessionOK: &inactive,
|
|
456
|
+
}))
|
|
457
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
458
|
+
ctx.Status(http.StatusOK)
|
|
459
|
+
})
|
|
460
|
+
|
|
461
|
+
w := httptest.NewRecorder()
|
|
462
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
463
|
+
applyGatewayAuthHeaders(req, 42, 9001, "jti-inactive-session", "tenant-abc")
|
|
464
|
+
engine.ServeHTTP(w, req)
|
|
465
|
+
|
|
466
|
+
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// TestGatewayIdentity_PendingDeletionBlocksNormalRoutes 验证冷静期账号无法访问普通受保护路由
|
|
470
|
+
func TestGatewayIdentity_PendingDeletionBlocksNormalRoutes(t *testing.T) {
|
|
471
|
+
engine := gin.New()
|
|
472
|
+
engine.Use(newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{
|
|
473
|
+
accessMode: GatewayIdentityAccessModePendingDeletion,
|
|
474
|
+
}))
|
|
475
|
+
engine.GET("/api/v1/viewer", func(ctx *gin.Context) {
|
|
476
|
+
ctx.Status(http.StatusOK)
|
|
477
|
+
})
|
|
478
|
+
|
|
479
|
+
w := httptest.NewRecorder()
|
|
480
|
+
req := httptest.NewRequest(http.MethodGet, "/api/v1/viewer", nil)
|
|
481
|
+
applyGatewayAuthHeaders(req, 42, 9001, "jti-pending-deletion", "tenant-abc")
|
|
482
|
+
engine.ServeHTTP(w, req)
|
|
483
|
+
|
|
484
|
+
assert.Equal(t, http.StatusForbidden, w.Code)
|
|
485
|
+
var body apierror.ErrorResponse
|
|
486
|
+
err := json.Unmarshal(w.Body.Bytes(), &body)
|
|
487
|
+
require.NoError(t, err)
|
|
488
|
+
assert.Equal(t, apierror.CodeAccountPendingDeletion, body.Error.Code)
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// TestGatewayIdentity_PendingDeletionAllowsCancelRoute 验证冷静期账号仍可取消注销
|
|
492
|
+
func TestGatewayIdentity_PendingDeletionAllowsCancelRoute(t *testing.T) {
|
|
493
|
+
engine := gin.New()
|
|
494
|
+
engine.Use(newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{
|
|
495
|
+
accessMode: GatewayIdentityAccessModePendingDeletion,
|
|
496
|
+
}))
|
|
497
|
+
engine.POST("/api/v1/deletion/cancel", func(ctx *gin.Context) {
|
|
498
|
+
ctx.Status(http.StatusOK)
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
w := httptest.NewRecorder()
|
|
502
|
+
req := httptest.NewRequest(http.MethodPost, "/api/v1/deletion/cancel", nil)
|
|
503
|
+
applyGatewayAuthHeaders(req, 42, 9001, "jti-cancel-allowed", "tenant-abc")
|
|
504
|
+
engine.ServeHTTP(w, req)
|
|
505
|
+
|
|
506
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
func TestGatewayIdentity_VerificationRequiredUsesMethodBoundAllowlist(t *testing.T) {
|
|
510
|
+
t.Parallel()
|
|
511
|
+
tests := []struct {
|
|
512
|
+
name string
|
|
513
|
+
method string
|
|
514
|
+
path string
|
|
515
|
+
wantStatus int
|
|
516
|
+
wantCode string
|
|
517
|
+
}{
|
|
518
|
+
{name: "email verification", method: http.MethodPost, path: "/api/v1/auth/verify-email", wantStatus: http.StatusOK},
|
|
519
|
+
{name: "verification resend", method: http.MethodPost, path: "/api/v1/auth/resend-verification", wantStatus: http.StatusOK},
|
|
520
|
+
{name: "logout", method: http.MethodPost, path: "/api/v1/auth/logout", wantStatus: http.StatusOK},
|
|
521
|
+
{name: "platform user info", method: http.MethodGet, path: "/api/v1/users/me", wantStatus: http.StatusOK},
|
|
522
|
+
{name: "browser session", method: http.MethodGet, path: "/api/onex/session", wantStatus: http.StatusOK},
|
|
523
|
+
{name: "guest activation completion", method: http.MethodPost, path: "/api/onex/auth/password-recovery/verify-code", wantStatus: http.StatusOK},
|
|
524
|
+
{name: "wrong verification method", method: http.MethodGet, path: "/api/v1/auth/verify-email", wantStatus: http.StatusForbidden, wantCode: apierror.CodeAccountNotVerified},
|
|
525
|
+
{name: "viewer denied", method: http.MethodGet, path: "/api/onex/viewer", wantStatus: http.StatusForbidden, wantCode: apierror.CodeAccountNotVerified},
|
|
526
|
+
{name: "checkout denied", method: http.MethodPost, path: "/api/v1/checkout-sessions", wantStatus: http.StatusForbidden, wantCode: apierror.CodeAccountNotVerified},
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
for _, tt := range tests {
|
|
530
|
+
t.Run(tt.name, func(t *testing.T) {
|
|
531
|
+
engine := gin.New()
|
|
532
|
+
engine.Use(newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{
|
|
533
|
+
accessMode: GatewayIdentityAccessModeVerificationRequired,
|
|
534
|
+
}))
|
|
535
|
+
engine.Handle(tt.method, tt.path, func(ctx *gin.Context) { ctx.Status(http.StatusOK) })
|
|
536
|
+
|
|
537
|
+
w := httptest.NewRecorder()
|
|
538
|
+
req := httptest.NewRequest(tt.method, tt.path, nil)
|
|
539
|
+
applyGatewayAuthHeaders(req, 42, 9001, "jti-verification-required", "tenant-abc")
|
|
540
|
+
engine.ServeHTTP(w, req)
|
|
541
|
+
|
|
542
|
+
assert.Equal(t, tt.wantStatus, w.Code)
|
|
543
|
+
if tt.wantCode != "" {
|
|
544
|
+
var body apierror.ErrorResponse
|
|
545
|
+
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
|
546
|
+
assert.Equal(t, tt.wantCode, body.Error.Code)
|
|
547
|
+
}
|
|
548
|
+
})
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
func TestGatewayIdentity_UnknownAccessModeFailsClosed(t *testing.T) {
|
|
553
|
+
engine := gin.New()
|
|
554
|
+
engine.Use(newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{accessMode: "future_state"}))
|
|
555
|
+
engine.GET("/api/v1/users/me", func(ctx *gin.Context) { ctx.Status(http.StatusOK) })
|
|
556
|
+
|
|
557
|
+
w := httptest.NewRecorder()
|
|
558
|
+
req := httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil)
|
|
559
|
+
applyGatewayAuthHeaders(req, 42, 9001, "jti-unknown-mode", "tenant-abc")
|
|
560
|
+
engine.ServeHTTP(w, req)
|
|
561
|
+
|
|
562
|
+
assert.Equal(t, http.StatusForbidden, w.Code)
|
|
563
|
+
var body apierror.ErrorResponse
|
|
564
|
+
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
|
565
|
+
assert.Equal(t, apierror.CodeAccessDenied, body.Error.Code)
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// TestCORS_ExposeCheckoutClientHeaders 验证本地联调 CORS 允许新的 checkout 客户端上下文头。
|
|
569
|
+
func TestCORS_ExposeCheckoutClientHeaders(t *testing.T) {
|
|
570
|
+
engine := gin.New()
|
|
571
|
+
engine.Use(CORS())
|
|
572
|
+
engine.OPTIONS("/", func(ctx *gin.Context) {
|
|
573
|
+
ctx.Status(http.StatusOK)
|
|
574
|
+
})
|
|
575
|
+
|
|
576
|
+
w := httptest.NewRecorder()
|
|
577
|
+
req := httptest.NewRequest(http.MethodOptions, "/", nil)
|
|
578
|
+
engine.ServeHTTP(w, req)
|
|
579
|
+
|
|
580
|
+
assert.Equal(t, http.StatusNoContent, w.Code)
|
|
581
|
+
allowHeaders := w.Header().Get("Access-Control-Allow-Headers")
|
|
582
|
+
assert.True(t, strings.Contains(allowHeaders, types.HeaderClientLocale))
|
|
583
|
+
assert.True(t, strings.Contains(allowHeaders, types.HeaderClientTimezone))
|
|
584
|
+
assert.True(t, strings.Contains(allowHeaders, types.HeaderClientTimezoneOffset))
|
|
585
|
+
assert.True(t, strings.Contains(allowHeaders, types.HeaderClientScreenWidth))
|
|
586
|
+
assert.True(t, strings.Contains(allowHeaders, types.HeaderClientJavaScriptEnabled))
|
|
587
|
+
assert.True(t, strings.Contains(allowHeaders, types.HeaderClientCookiesEnabled))
|
|
588
|
+
assert.True(t, strings.Contains(allowHeaders, types.HeaderProductCode))
|
|
589
|
+
assert.True(t, strings.Contains(allowHeaders, types.HeaderDeviceID))
|
|
590
|
+
assert.True(t, strings.Contains(allowHeaders, "Checkout-Client-Nonce"))
|
|
591
|
+
assert.True(t, strings.Contains(allowHeaders, "Checkout-Presentation-Accept"))
|
|
592
|
+
assert.True(t, strings.Contains(allowHeaders, types.HeaderIdempotencyKey))
|
|
593
|
+
for _, header := range strings.Split(allowHeaders, ", ") {
|
|
594
|
+
assert.NotEqual(t, "Idempotency-Key", header)
|
|
595
|
+
}
|
|
596
|
+
assert.True(t, strings.Contains(allowHeaders, types.HeaderAttributionSource))
|
|
597
|
+
assert.True(t, strings.Contains(allowHeaders, types.HeaderAttributionTouchID))
|
|
598
|
+
assert.True(t, strings.Contains(allowHeaders, types.HeaderAttributionCampaignID))
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// ═══════════════════════════════════════════════════════════════
|
|
602
|
+
// RequireIdentity 测试
|
|
603
|
+
// ═══════════════════════════════════════════════════════════════
|
|
604
|
+
|
|
605
|
+
// TestRequireIdentity_WithIdentity 验证已设置 identity 时请求通过
|
|
606
|
+
func TestRequireIdentity_WithIdentity(t *testing.T) {
|
|
607
|
+
engine := gin.New()
|
|
608
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
609
|
+
ctx.Set(string(types.ContextKeyUserIdentity), &types.UserIdentity{UserID: 1, Type: "user"})
|
|
610
|
+
}, RequireIdentity(), func(ctx *gin.Context) {
|
|
611
|
+
ctx.Status(http.StatusOK)
|
|
612
|
+
})
|
|
613
|
+
|
|
614
|
+
// 先用 GatewayIdentity 注入 identity,再用 RequireIdentity 验证
|
|
615
|
+
engine2 := gin.New()
|
|
616
|
+
engine2.Use(newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{}), RequireIdentity())
|
|
617
|
+
engine2.GET("/", func(ctx *gin.Context) {
|
|
618
|
+
ctx.Status(http.StatusOK)
|
|
619
|
+
})
|
|
620
|
+
|
|
621
|
+
w := httptest.NewRecorder()
|
|
622
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
623
|
+
applyGatewayAuthHeaders(req, 1, 1001, "jti-req-identity", "tenant-1")
|
|
624
|
+
engine2.ServeHTTP(w, req)
|
|
625
|
+
|
|
626
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// TestRequireIdentity_WithoutIdentity 验证无 identity 时返回 401
|
|
630
|
+
func TestRequireIdentity_WithoutIdentity(t *testing.T) {
|
|
631
|
+
engine := gin.New()
|
|
632
|
+
engine.Use(newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{}), RequireIdentity())
|
|
633
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
634
|
+
ctx.Status(http.StatusOK)
|
|
635
|
+
})
|
|
636
|
+
|
|
637
|
+
w := httptest.NewRecorder()
|
|
638
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
639
|
+
// 不设置网关认证事实,GatewayIdentity 放行,RequireIdentity 应返回 401
|
|
640
|
+
engine.ServeHTTP(w, req)
|
|
641
|
+
|
|
642
|
+
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
|
643
|
+
|
|
644
|
+
var body apierror.ErrorResponse
|
|
645
|
+
err := json.Unmarshal(w.Body.Bytes(), &body)
|
|
646
|
+
require.NoError(t, err)
|
|
647
|
+
assert.Equal(t, apierror.CodeUnauthorized, body.Error.Code)
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// ═══════════════════════════════════════════════════════════════
|
|
651
|
+
// ProductContext 测试
|
|
652
|
+
// ═══════════════════════════════════════════════════════════════
|
|
653
|
+
|
|
654
|
+
type middlewareProductContextReader struct {
|
|
655
|
+
snapshots map[string]iamdomain.ProductContextSnapshot
|
|
656
|
+
err error
|
|
657
|
+
resolvedAt []time.Time
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
func (r *middlewareProductContextReader) ReadProductContextAt(
|
|
661
|
+
_ context.Context,
|
|
662
|
+
productCode string,
|
|
663
|
+
resolvedAt time.Time,
|
|
664
|
+
) (iamdomain.ProductContextSnapshot, error) {
|
|
665
|
+
r.resolvedAt = append(r.resolvedAt, resolvedAt)
|
|
666
|
+
if r.err != nil {
|
|
667
|
+
return iamdomain.ProductContextSnapshot{}, r.err
|
|
668
|
+
}
|
|
669
|
+
snapshot, ok := r.snapshots[productCode]
|
|
670
|
+
if !ok {
|
|
671
|
+
return iamdomain.ProductContextSnapshot{}, iamdomain.ErrProductContextNotFound
|
|
672
|
+
}
|
|
673
|
+
return snapshot, nil
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
func TestProductContext_UsesInjectedResolutionBoundary(t *testing.T) {
|
|
677
|
+
fixedAt := time.Date(2026, time.July, 10, 8, 30, 0, 0, time.UTC)
|
|
678
|
+
tests := []struct {
|
|
679
|
+
name string
|
|
680
|
+
resolvedAt time.Time
|
|
681
|
+
wantStatus int
|
|
682
|
+
}{
|
|
683
|
+
{name: "exact boundary", resolvedAt: fixedAt, wantStatus: http.StatusOK},
|
|
684
|
+
{name: "future by one nanosecond", resolvedAt: fixedAt.Add(time.Nanosecond), wantStatus: http.StatusServiceUnavailable},
|
|
685
|
+
}
|
|
686
|
+
for _, tc := range tests {
|
|
687
|
+
t.Run(tc.name, func(t *testing.T) {
|
|
688
|
+
reader := productContextReader("catalog")
|
|
689
|
+
snapshot := reader.snapshots["catalog"]
|
|
690
|
+
snapshot.ResolvedAt = tc.resolvedAt
|
|
691
|
+
reader.snapshots["catalog"] = snapshot
|
|
692
|
+
engine := gin.New()
|
|
693
|
+
engine.Use(ProductContext(reader, coreclock.NewManualClock(fixedAt)))
|
|
694
|
+
engine.GET("/", func(ctx *gin.Context) { ctx.Status(http.StatusOK) })
|
|
695
|
+
|
|
696
|
+
w := httptest.NewRecorder()
|
|
697
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
698
|
+
req.Header.Set(types.HeaderProductCode, "catalog")
|
|
699
|
+
engine.ServeHTTP(w, req)
|
|
700
|
+
|
|
701
|
+
assert.Equal(t, tc.wantStatus, w.Code)
|
|
702
|
+
require.Equal(t, []time.Time{fixedAt}, reader.resolvedAt)
|
|
703
|
+
})
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
func productContextReader(productCodes ...string) *middlewareProductContextReader {
|
|
708
|
+
snapshots := make(map[string]iamdomain.ProductContextSnapshot, len(productCodes))
|
|
709
|
+
resolvedAt := time.Now().UTC().Add(-time.Minute)
|
|
710
|
+
for _, productCode := range productCodes {
|
|
711
|
+
snapshots[productCode] = iamdomain.ProductContextSnapshot{
|
|
712
|
+
ProductCode: productCode,
|
|
713
|
+
ProductKind: "application",
|
|
714
|
+
LifecycleStatus: "active",
|
|
715
|
+
ContextVersion: 1,
|
|
716
|
+
ContextFingerprint: strings.Repeat("a", 64),
|
|
717
|
+
Attributes: []iamdomain.ProductAttributeFact{
|
|
718
|
+
{Code: "product_kind", Value: json.RawMessage(`"application"`)},
|
|
719
|
+
},
|
|
720
|
+
Capabilities: make([]iamdomain.ProductCapabilityFact, 0),
|
|
721
|
+
ResolvedAt: resolvedAt,
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
return &middlewareProductContextReader{snapshots: snapshots}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
// TestProductContext_ValidCode 验证合法产品码被设置到 context
|
|
728
|
+
func TestProductContext_ValidCode(t *testing.T) {
|
|
729
|
+
validCodes := []types.Product{
|
|
730
|
+
types.ProductPlatform,
|
|
731
|
+
types.Product("catalog"),
|
|
732
|
+
types.Product("new_dynamic_product"),
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
for _, code := range validCodes {
|
|
736
|
+
t.Run(string(code), func(t *testing.T) {
|
|
737
|
+
engine := gin.New()
|
|
738
|
+
engine.Use(ProductContext(productContextReader(string(code)), coreclock.NewSystemClock()))
|
|
739
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
740
|
+
got := GetProductCode(ctx)
|
|
741
|
+
assert.Equal(t, code, got)
|
|
742
|
+
ctx.Status(http.StatusOK)
|
|
743
|
+
})
|
|
744
|
+
|
|
745
|
+
w := httptest.NewRecorder()
|
|
746
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
747
|
+
req.Header.Set(types.HeaderProductCode, string(code))
|
|
748
|
+
engine.ServeHTTP(w, req)
|
|
749
|
+
|
|
750
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
751
|
+
})
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
func TestProductContext_ValidOneXProductCodeForIAMServiceToken(t *testing.T) {
|
|
756
|
+
dynamicProduct := types.Product("external-product")
|
|
757
|
+
engine := gin.New()
|
|
758
|
+
engine.Use(ProductContext(productContextReader(string(dynamicProduct)), coreclock.NewSystemClock()))
|
|
759
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
760
|
+
assert.Equal(t, dynamicProduct, GetProductCode(ctx))
|
|
761
|
+
ctx.Status(http.StatusOK)
|
|
762
|
+
})
|
|
763
|
+
|
|
764
|
+
w := httptest.NewRecorder()
|
|
765
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
766
|
+
req.Header.Set("Authorization", "Bearer iamt_test.token")
|
|
767
|
+
req.Header.Set(types.HeaderOneXProductCode, string(dynamicProduct))
|
|
768
|
+
engine.ServeHTTP(w, req)
|
|
769
|
+
|
|
770
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
func TestProductContext_IgnoresOneXProductCodeWithoutIAMServiceToken(t *testing.T) {
|
|
774
|
+
dynamicProduct := types.Product("external-product")
|
|
775
|
+
engine := gin.New()
|
|
776
|
+
engine.Use(ProductContext(productContextReader(), coreclock.NewSystemClock()))
|
|
777
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
778
|
+
assert.Equal(t, types.Product(""), GetProductCode(ctx))
|
|
779
|
+
ctx.Status(http.StatusOK)
|
|
780
|
+
})
|
|
781
|
+
|
|
782
|
+
w := httptest.NewRecorder()
|
|
783
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
784
|
+
req.Header.Set(types.HeaderOneXProductCode, string(dynamicProduct))
|
|
785
|
+
engine.ServeHTTP(w, req)
|
|
786
|
+
|
|
787
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
func TestProductContext_RejectsMismatchedProductHeaders(t *testing.T) {
|
|
791
|
+
dynamicProduct := types.Product("external-product")
|
|
792
|
+
engine := gin.New()
|
|
793
|
+
engine.Use(ProductContext(
|
|
794
|
+
productContextReader(string(types.Product("catalog")), string(dynamicProduct)),
|
|
795
|
+
coreclock.NewSystemClock(),
|
|
796
|
+
))
|
|
797
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
798
|
+
ctx.Status(http.StatusOK)
|
|
799
|
+
})
|
|
800
|
+
|
|
801
|
+
w := httptest.NewRecorder()
|
|
802
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
803
|
+
req.Header.Set(types.HeaderProductCode, string(types.Product("catalog")))
|
|
804
|
+
req.Header.Set(types.HeaderOneXProductCode, string(dynamicProduct))
|
|
805
|
+
engine.ServeHTTP(w, req)
|
|
806
|
+
|
|
807
|
+
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// TestProductContext_MissingHeader 验证无 X-Product-Code 时请求被放行,context 中无产品码
|
|
811
|
+
func TestProductContext_MissingHeader(t *testing.T) {
|
|
812
|
+
engine := gin.New()
|
|
813
|
+
engine.Use(ProductContext(productContextReader(), coreclock.NewSystemClock()))
|
|
814
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
815
|
+
got := GetProductCode(ctx)
|
|
816
|
+
assert.Equal(t, types.Product(""), got)
|
|
817
|
+
ctx.Status(http.StatusOK)
|
|
818
|
+
})
|
|
819
|
+
|
|
820
|
+
w := httptest.NewRecorder()
|
|
821
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
822
|
+
engine.ServeHTTP(w, req)
|
|
823
|
+
|
|
824
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
// TestProductContext_UnknownCode 验证未知产品码返回 400
|
|
828
|
+
func TestProductContext_UnknownCode(t *testing.T) {
|
|
829
|
+
engine := gin.New()
|
|
830
|
+
engine.Use(ProductContext(productContextReader(), coreclock.NewSystemClock()))
|
|
831
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
832
|
+
ctx.Status(http.StatusOK)
|
|
833
|
+
})
|
|
834
|
+
|
|
835
|
+
w := httptest.NewRecorder()
|
|
836
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
837
|
+
req.Header.Set(types.HeaderProductCode, "unknown_product")
|
|
838
|
+
engine.ServeHTTP(w, req)
|
|
839
|
+
|
|
840
|
+
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
841
|
+
|
|
842
|
+
var body apierror.ErrorResponse
|
|
843
|
+
err := json.Unmarshal(w.Body.Bytes(), &body)
|
|
844
|
+
require.NoError(t, err)
|
|
845
|
+
assert.Equal(t, apierror.CodeInvalidOperation, body.Error.Code)
|
|
846
|
+
assert.Contains(t, body.Error.Message, "unknown_product")
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
func TestProductContext_ReaderFailureFailsClosed(t *testing.T) {
|
|
850
|
+
engine := gin.New()
|
|
851
|
+
engine.Use(ProductContext(
|
|
852
|
+
&middlewareProductContextReader{err: errors.New("product database unavailable")},
|
|
853
|
+
coreclock.NewSystemClock(),
|
|
854
|
+
))
|
|
855
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
856
|
+
ctx.Status(http.StatusOK)
|
|
857
|
+
})
|
|
858
|
+
|
|
859
|
+
w := httptest.NewRecorder()
|
|
860
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
861
|
+
req.Header.Set(types.HeaderProductCode, "catalog")
|
|
862
|
+
engine.ServeHTTP(w, req)
|
|
863
|
+
|
|
864
|
+
assert.Equal(t, http.StatusServiceUnavailable, w.Code)
|
|
865
|
+
var body apierror.ErrorResponse
|
|
866
|
+
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
|
867
|
+
assert.Equal(t, apierror.CodeServiceUnavailable, body.Error.Code)
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
func TestProductContext_RejectsSnapshotAdmissionInvariantViolations(t *testing.T) {
|
|
871
|
+
valid := productContextReader("catalog").snapshots["catalog"]
|
|
872
|
+
now := time.Now().UTC()
|
|
873
|
+
expiredAt := now.Add(-time.Second)
|
|
874
|
+
futureResolvedAt := now.Add(time.Minute)
|
|
875
|
+
|
|
876
|
+
tests := []struct {
|
|
877
|
+
name string
|
|
878
|
+
mutate func(*iamdomain.ProductContextSnapshot)
|
|
879
|
+
}{
|
|
880
|
+
{name: "zero version", mutate: func(snapshot *iamdomain.ProductContextSnapshot) { snapshot.ContextVersion = 0 }},
|
|
881
|
+
{name: "empty fingerprint", mutate: func(snapshot *iamdomain.ProductContextSnapshot) { snapshot.ContextFingerprint = "" }},
|
|
882
|
+
{name: "zero resolved time", mutate: func(snapshot *iamdomain.ProductContextSnapshot) { snapshot.ResolvedAt = time.Time{} }},
|
|
883
|
+
{name: "future resolved time", mutate: func(snapshot *iamdomain.ProductContextSnapshot) { snapshot.ResolvedAt = futureResolvedAt }},
|
|
884
|
+
{name: "expired snapshot", mutate: func(snapshot *iamdomain.ProductContextSnapshot) { snapshot.ExpiresAt = &expiredAt }},
|
|
885
|
+
{name: "unresolved attributes", mutate: func(snapshot *iamdomain.ProductContextSnapshot) { snapshot.Attributes = nil }},
|
|
886
|
+
{name: "unresolved capabilities", mutate: func(snapshot *iamdomain.ProductContextSnapshot) { snapshot.Capabilities = nil }},
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
for _, tc := range tests {
|
|
890
|
+
t.Run(tc.name, func(t *testing.T) {
|
|
891
|
+
snapshot := valid
|
|
892
|
+
snapshot.Attributes = append([]iamdomain.ProductAttributeFact(nil), valid.Attributes...)
|
|
893
|
+
snapshot.Capabilities = append([]iamdomain.ProductCapabilityFact(nil), valid.Capabilities...)
|
|
894
|
+
tc.mutate(&snapshot)
|
|
895
|
+
engine := gin.New()
|
|
896
|
+
engine.Use(ProductContext(
|
|
897
|
+
&middlewareProductContextReader{snapshots: map[string]iamdomain.ProductContextSnapshot{"catalog": snapshot}},
|
|
898
|
+
coreclock.NewSystemClock(),
|
|
899
|
+
))
|
|
900
|
+
engine.GET("/", func(ctx *gin.Context) { ctx.Status(http.StatusOK) })
|
|
901
|
+
|
|
902
|
+
w := httptest.NewRecorder()
|
|
903
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
904
|
+
req.Header.Set(types.HeaderProductCode, "catalog")
|
|
905
|
+
engine.ServeHTTP(w, req)
|
|
906
|
+
|
|
907
|
+
assert.Equal(t, http.StatusServiceUnavailable, w.Code)
|
|
908
|
+
})
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
// ═══════════════════════════════════════════════════════════════
|
|
913
|
+
// ClientContext 测试
|
|
914
|
+
// ═══════════════════════════════════════════════════════════════
|
|
915
|
+
|
|
916
|
+
// TestClientContext_AllHeaders 验证所有客户端 Header 被正确提取
|
|
917
|
+
func TestClientContext_AllHeaders(t *testing.T) {
|
|
918
|
+
engine := gin.New()
|
|
919
|
+
engine.Use(ClientContext())
|
|
920
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
921
|
+
cc := GetClientContext(ctx)
|
|
922
|
+
require.NotNil(t, cc)
|
|
923
|
+
assert.Equal(t, "mobile", cc.Type)
|
|
924
|
+
assert.Equal(t, "1.2.3", cc.Version)
|
|
925
|
+
assert.Equal(t, "iOS", cc.Platform)
|
|
926
|
+
assert.Equal(t, "device-123", cc.DeviceID)
|
|
927
|
+
ctx.Status(http.StatusOK)
|
|
928
|
+
})
|
|
929
|
+
|
|
930
|
+
w := httptest.NewRecorder()
|
|
931
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
932
|
+
req.Header.Set(types.HeaderClientType, "mobile")
|
|
933
|
+
req.Header.Set(types.HeaderClientVersion, "1.2.3")
|
|
934
|
+
req.Header.Set(types.HeaderClientPlatform, "iOS")
|
|
935
|
+
req.Header.Set(types.HeaderDeviceID, "device-123")
|
|
936
|
+
engine.ServeHTTP(w, req)
|
|
937
|
+
|
|
938
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
// TestClientContext_AlwaysSetsContext 验证无 Header 时 ClientContext 仍然被设置(非 nil)
|
|
942
|
+
func TestClientContext_AlwaysSetsContext(t *testing.T) {
|
|
943
|
+
engine := gin.New()
|
|
944
|
+
engine.Use(ClientContext())
|
|
945
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
946
|
+
cc := GetClientContext(ctx)
|
|
947
|
+
// ClientContext 总是注入,即使 Header 为空
|
|
948
|
+
require.NotNil(t, cc)
|
|
949
|
+
assert.Equal(t, "", cc.Type)
|
|
950
|
+
assert.Equal(t, "", cc.Version)
|
|
951
|
+
assert.Equal(t, "", cc.Platform)
|
|
952
|
+
assert.Equal(t, "", cc.DeviceID)
|
|
953
|
+
ctx.Status(http.StatusOK)
|
|
954
|
+
})
|
|
955
|
+
|
|
956
|
+
w := httptest.NewRecorder()
|
|
957
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
958
|
+
engine.ServeHTTP(w, req)
|
|
959
|
+
|
|
960
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
// TestClientContext_LegacyAppVersionFallback 验证旧版 X-App-Version 可回退到 Version
|
|
964
|
+
func TestClientContext_LegacyAppVersionFallback(t *testing.T) {
|
|
965
|
+
engine := gin.New()
|
|
966
|
+
engine.Use(ClientContext())
|
|
967
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
968
|
+
cc := GetClientContext(ctx)
|
|
969
|
+
require.NotNil(t, cc)
|
|
970
|
+
assert.Equal(t, "2.3.4", cc.Version)
|
|
971
|
+
ctx.Status(http.StatusOK)
|
|
972
|
+
})
|
|
973
|
+
|
|
974
|
+
w := httptest.NewRecorder()
|
|
975
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
976
|
+
req.Header.Set(types.HeaderAppVersion, "2.3.4")
|
|
977
|
+
engine.ServeHTTP(w, req)
|
|
978
|
+
|
|
979
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
// TestClientContext_XRealIPTakesPrecedence 验证 X-Real-IP 优先于 ClientIP
|
|
983
|
+
func TestClientContext_XRealIPTakesPrecedence(t *testing.T) {
|
|
984
|
+
engine := gin.New()
|
|
985
|
+
engine.Use(ClientContext())
|
|
986
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
987
|
+
cc := GetClientContext(ctx)
|
|
988
|
+
require.NotNil(t, cc)
|
|
989
|
+
assert.Equal(t, "203.0.113.1", cc.IP)
|
|
990
|
+
ctx.Status(http.StatusOK)
|
|
991
|
+
})
|
|
992
|
+
|
|
993
|
+
w := httptest.NewRecorder()
|
|
994
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
995
|
+
req.Header.Set(types.HeaderXRealIP, "203.0.113.1")
|
|
996
|
+
engine.ServeHTTP(w, req)
|
|
997
|
+
|
|
998
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1002
|
+
// RequestID 测试
|
|
1003
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1004
|
+
|
|
1005
|
+
// TestRequestID_UseExistingHeader 验证已有 X-Request-ID 时直接使用
|
|
1006
|
+
func TestRequestID_UseExistingHeader(t *testing.T) {
|
|
1007
|
+
existingID := "test-request-id-12345"
|
|
1008
|
+
|
|
1009
|
+
engine := gin.New()
|
|
1010
|
+
engine.Use(RequestID())
|
|
1011
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1012
|
+
id := GetRequestID(ctx)
|
|
1013
|
+
assert.Equal(t, existingID, id)
|
|
1014
|
+
ctx.Status(http.StatusOK)
|
|
1015
|
+
})
|
|
1016
|
+
|
|
1017
|
+
w := httptest.NewRecorder()
|
|
1018
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1019
|
+
req.Header.Set(types.HeaderRequestID, existingID)
|
|
1020
|
+
engine.ServeHTTP(w, req)
|
|
1021
|
+
|
|
1022
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
1023
|
+
// 验证响应头包含相同的 request ID
|
|
1024
|
+
assert.Equal(t, existingID, w.Header().Get(types.HeaderRequestID))
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
// TestRequestID_UseCorrelationIDFallback 验证无 X-Request-ID 时使用 X-Correlation-ID
|
|
1028
|
+
func TestRequestID_UseCorrelationIDFallback(t *testing.T) {
|
|
1029
|
+
correlationID := "corr-id-abc"
|
|
1030
|
+
|
|
1031
|
+
engine := gin.New()
|
|
1032
|
+
engine.Use(RequestID())
|
|
1033
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1034
|
+
id := GetRequestID(ctx)
|
|
1035
|
+
assert.Equal(t, correlationID, id)
|
|
1036
|
+
ctx.Status(http.StatusOK)
|
|
1037
|
+
})
|
|
1038
|
+
|
|
1039
|
+
w := httptest.NewRecorder()
|
|
1040
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1041
|
+
req.Header.Set(types.HeaderCorrelationID, correlationID)
|
|
1042
|
+
engine.ServeHTTP(w, req)
|
|
1043
|
+
|
|
1044
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
1045
|
+
assert.Equal(t, correlationID, w.Header().Get(types.HeaderRequestID))
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
// TestRequestID_GeneratesUUIDWhenMissing 验证无任何 ID Header 时生成 UUID
|
|
1049
|
+
func TestRequestID_GeneratesUUIDWhenMissing(t *testing.T) {
|
|
1050
|
+
engine := gin.New()
|
|
1051
|
+
engine.Use(RequestID())
|
|
1052
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1053
|
+
id := GetRequestID(ctx)
|
|
1054
|
+
assert.NotEmpty(t, id, "自动生成的 request ID 不应为空")
|
|
1055
|
+
// UUID 格式:8-4-4-4-12 十六进制
|
|
1056
|
+
assert.Len(t, id, 36, "UUID 长度应为 36 字符")
|
|
1057
|
+
ctx.Status(http.StatusOK)
|
|
1058
|
+
})
|
|
1059
|
+
|
|
1060
|
+
w := httptest.NewRecorder()
|
|
1061
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1062
|
+
engine.ServeHTTP(w, req)
|
|
1063
|
+
|
|
1064
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
1065
|
+
assert.NotEmpty(t, w.Header().Get(types.HeaderRequestID), "响应头应包含生成的 request ID")
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
// TestRequestID_XRequestIDPriorityOverCorrelationID 验证 X-Request-ID 优先级高于 X-Correlation-ID
|
|
1069
|
+
func TestRequestID_XRequestIDPriorityOverCorrelationID(t *testing.T) {
|
|
1070
|
+
requestID := "primary-id"
|
|
1071
|
+
correlationID := "secondary-id"
|
|
1072
|
+
|
|
1073
|
+
engine := gin.New()
|
|
1074
|
+
engine.Use(RequestID())
|
|
1075
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1076
|
+
id := GetRequestID(ctx)
|
|
1077
|
+
assert.Equal(t, requestID, id)
|
|
1078
|
+
ctx.Status(http.StatusOK)
|
|
1079
|
+
})
|
|
1080
|
+
|
|
1081
|
+
w := httptest.NewRecorder()
|
|
1082
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1083
|
+
req.Header.Set(types.HeaderRequestID, requestID)
|
|
1084
|
+
req.Header.Set(types.HeaderCorrelationID, correlationID)
|
|
1085
|
+
engine.ServeHTTP(w, req)
|
|
1086
|
+
|
|
1087
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
// TestGetRequestID_WhenNotSet 验证 context 中无 request ID 时返回空字符串
|
|
1091
|
+
func TestGetRequestID_WhenNotSet(t *testing.T) {
|
|
1092
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1093
|
+
id := GetRequestID(c)
|
|
1094
|
+
assert.Equal(t, "", id)
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1098
|
+
// helpers.go 测试
|
|
1099
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1100
|
+
|
|
1101
|
+
// TestGetUserIdentity_WhenSet 验证 context 中有 identity 时正确返回
|
|
1102
|
+
func TestGetUserIdentity_WhenSet(t *testing.T) {
|
|
1103
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1104
|
+
expected := &types.UserIdentity{UserID: 99, Type: "admin"}
|
|
1105
|
+
c.Set(string(types.ContextKeyUserIdentity), expected)
|
|
1106
|
+
|
|
1107
|
+
got := GetUserIdentity(c)
|
|
1108
|
+
require.NotNil(t, got)
|
|
1109
|
+
assert.Equal(t, expected.UserID, got.UserID)
|
|
1110
|
+
assert.Equal(t, expected.Type, got.Type)
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
// TestGetUserIdentity_WhenNotSet 验证 context 中无 identity 时返回 nil
|
|
1114
|
+
func TestGetUserIdentity_WhenNotSet(t *testing.T) {
|
|
1115
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1116
|
+
got := GetUserIdentity(c)
|
|
1117
|
+
assert.Nil(t, got)
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
// TestGetUserIdentity_WrongType 验证 context 中类型不匹配时返回 nil
|
|
1121
|
+
func TestGetUserIdentity_WrongType(t *testing.T) {
|
|
1122
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1123
|
+
c.Set(string(types.ContextKeyUserIdentity), "wrong-type-value")
|
|
1124
|
+
got := GetUserIdentity(c)
|
|
1125
|
+
assert.Nil(t, got)
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
// TestGetUserID_WhenIdentitySet 验证有 identity 时正确返回 user ID
|
|
1129
|
+
func TestGetUserID_WhenIdentitySet(t *testing.T) {
|
|
1130
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1131
|
+
c.Set(string(types.ContextKeyUserIdentity), &types.UserIdentity{UserID: 55})
|
|
1132
|
+
assert.Equal(t, types.UserID(55), GetUserID(c))
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
// TestGetUserID_WhenNotSet 验证无 identity 时返回 0
|
|
1136
|
+
func TestGetUserID_WhenNotSet(t *testing.T) {
|
|
1137
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1138
|
+
assert.Equal(t, types.UserID(0), GetUserID(c))
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
// TestGetProductCode_WhenSet 验证 context 中有产品码时正确返回
|
|
1142
|
+
func TestGetProductCode_WhenSet(t *testing.T) {
|
|
1143
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1144
|
+
c.Set(string(types.ContextKeyProductCode), types.ProductPlatform)
|
|
1145
|
+
got := GetProductCode(c)
|
|
1146
|
+
assert.Equal(t, types.ProductPlatform, got)
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
// TestGetProductCode_WhenNotSet 验证 context 中无产品码时返回空字符串
|
|
1150
|
+
func TestGetProductCode_WhenNotSet(t *testing.T) {
|
|
1151
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1152
|
+
got := GetProductCode(c)
|
|
1153
|
+
assert.Equal(t, types.Product(""), got)
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
// TestGetProductCode_WrongType 验证 context 中类型不匹配时返回空字符串
|
|
1157
|
+
func TestGetProductCode_WrongType(t *testing.T) {
|
|
1158
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1159
|
+
c.Set(string(types.ContextKeyProductCode), 12345)
|
|
1160
|
+
got := GetProductCode(c)
|
|
1161
|
+
assert.Equal(t, types.Product(""), got)
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
// TestGetClientContext_WhenSet 验证 context 中有 ClientContext 时正确返回
|
|
1165
|
+
func TestGetClientContext_WhenSet(t *testing.T) {
|
|
1166
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1167
|
+
expected := &types.ClientContext{Type: "web", Version: "2.0.0", Platform: "Web"}
|
|
1168
|
+
c.Set(string(types.ContextKeyClientContext), expected)
|
|
1169
|
+
|
|
1170
|
+
got := GetClientContext(c)
|
|
1171
|
+
require.NotNil(t, got)
|
|
1172
|
+
assert.Equal(t, "web", got.Type)
|
|
1173
|
+
assert.Equal(t, "2.0.0", got.Version)
|
|
1174
|
+
assert.Equal(t, "Web", got.Platform)
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
// TestGetClientContext_WhenNotSet 验证 context 中无 ClientContext 时返回 nil
|
|
1178
|
+
func TestGetClientContext_WhenNotSet(t *testing.T) {
|
|
1179
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1180
|
+
got := GetClientContext(c)
|
|
1181
|
+
assert.Nil(t, got)
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
// TestGetClientContext_WrongType 验证 context 中类型不匹配时返回 nil
|
|
1185
|
+
func TestGetClientContext_WrongType(t *testing.T) {
|
|
1186
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1187
|
+
c.Set(string(types.ContextKeyClientContext), "bad-type")
|
|
1188
|
+
got := GetClientContext(c)
|
|
1189
|
+
assert.Nil(t, got)
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1193
|
+
// Recovery 测试
|
|
1194
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1195
|
+
|
|
1196
|
+
// TestRecovery_HandlesStringPanic 验证字符串 panic 被捕获并返回 500
|
|
1197
|
+
func TestRecovery_HandlesStringPanic(t *testing.T) {
|
|
1198
|
+
logger := zap.NewNop()
|
|
1199
|
+
|
|
1200
|
+
engine := gin.New()
|
|
1201
|
+
engine.Use(Recovery(logger))
|
|
1202
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1203
|
+
panic("something went terribly wrong")
|
|
1204
|
+
})
|
|
1205
|
+
|
|
1206
|
+
w := httptest.NewRecorder()
|
|
1207
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1208
|
+
engine.ServeHTTP(w, req)
|
|
1209
|
+
|
|
1210
|
+
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
|
1211
|
+
|
|
1212
|
+
var body apierror.ErrorResponse
|
|
1213
|
+
err := json.Unmarshal(w.Body.Bytes(), &body)
|
|
1214
|
+
require.NoError(t, err)
|
|
1215
|
+
assert.Equal(t, apierror.CodeInternalError, body.Error.Code)
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
// TestRecovery_HandlesErrorPanic 验证 error panic 被捕获并返回 500
|
|
1219
|
+
func TestRecovery_HandlesErrorPanic(t *testing.T) {
|
|
1220
|
+
logger := zap.NewNop()
|
|
1221
|
+
|
|
1222
|
+
engine := gin.New()
|
|
1223
|
+
engine.Use(Recovery(logger))
|
|
1224
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1225
|
+
panic(fmt.Errorf("db connection lost"))
|
|
1226
|
+
})
|
|
1227
|
+
|
|
1228
|
+
w := httptest.NewRecorder()
|
|
1229
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1230
|
+
engine.ServeHTTP(w, req)
|
|
1231
|
+
|
|
1232
|
+
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
// TestRecovery_NoPanicPassesThrough 验证无 panic 时正常请求通过
|
|
1236
|
+
func TestRecovery_NoPanicPassesThrough(t *testing.T) {
|
|
1237
|
+
logger := zap.NewNop()
|
|
1238
|
+
|
|
1239
|
+
engine := gin.New()
|
|
1240
|
+
engine.Use(Recovery(logger))
|
|
1241
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1242
|
+
ctx.Status(http.StatusOK)
|
|
1243
|
+
})
|
|
1244
|
+
|
|
1245
|
+
w := httptest.NewRecorder()
|
|
1246
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1247
|
+
engine.ServeHTTP(w, req)
|
|
1248
|
+
|
|
1249
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
// TestRecovery_HandlesIntPanic 验证整型 panic 被捕获并返回 500
|
|
1253
|
+
func TestRecovery_HandlesIntPanic(t *testing.T) {
|
|
1254
|
+
logger := zap.NewNop()
|
|
1255
|
+
|
|
1256
|
+
engine := gin.New()
|
|
1257
|
+
engine.Use(Recovery(logger))
|
|
1258
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1259
|
+
panic(42)
|
|
1260
|
+
})
|
|
1261
|
+
|
|
1262
|
+
w := httptest.NewRecorder()
|
|
1263
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1264
|
+
engine.ServeHTTP(w, req)
|
|
1265
|
+
|
|
1266
|
+
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1270
|
+
// Tenant 中间件测试(无 DB/Redis 依赖)
|
|
1271
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1272
|
+
|
|
1273
|
+
// TestTenant_ValidIDFromHeader 验证从 X-Tenant-ID Header 提取租户 ID
|
|
1274
|
+
func TestTenant_ValidIDFromHeader(t *testing.T) {
|
|
1275
|
+
engine := gin.New()
|
|
1276
|
+
engine.Use(Tenant())
|
|
1277
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1278
|
+
tenantID := GetTenantID(ctx)
|
|
1279
|
+
assert.Equal(t, "valid-tenant-id", tenantID)
|
|
1280
|
+
ctx.Status(http.StatusOK)
|
|
1281
|
+
})
|
|
1282
|
+
|
|
1283
|
+
w := httptest.NewRecorder()
|
|
1284
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1285
|
+
req.Header.Set(types.HeaderTenantID, "valid-tenant-id")
|
|
1286
|
+
engine.ServeHTTP(w, req)
|
|
1287
|
+
|
|
1288
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
// TestTenant_MissingTenantID 验证无租户 ID 时返回 400
|
|
1292
|
+
func TestTenant_MissingTenantID(t *testing.T) {
|
|
1293
|
+
engine := gin.New()
|
|
1294
|
+
engine.Use(Tenant())
|
|
1295
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1296
|
+
ctx.Status(http.StatusOK)
|
|
1297
|
+
})
|
|
1298
|
+
|
|
1299
|
+
w := httptest.NewRecorder()
|
|
1300
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1301
|
+
engine.ServeHTTP(w, req)
|
|
1302
|
+
|
|
1303
|
+
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
// TestTenant_InvalidIDFormat 验证格式非法的租户 ID 返回 400
|
|
1307
|
+
func TestTenant_InvalidIDFormat(t *testing.T) {
|
|
1308
|
+
engine := gin.New()
|
|
1309
|
+
engine.Use(Tenant())
|
|
1310
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1311
|
+
ctx.Status(http.StatusOK)
|
|
1312
|
+
})
|
|
1313
|
+
|
|
1314
|
+
w := httptest.NewRecorder()
|
|
1315
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1316
|
+
// 长度小于 minTenantIDLength(3)
|
|
1317
|
+
req.Header.Set(types.HeaderTenantID, "ab")
|
|
1318
|
+
engine.ServeHTTP(w, req)
|
|
1319
|
+
|
|
1320
|
+
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
// TestTenant_ValidUUID 验证 UUID 格式的租户 ID 通过
|
|
1324
|
+
func TestTenant_ValidUUID(t *testing.T) {
|
|
1325
|
+
engine := gin.New()
|
|
1326
|
+
engine.Use(Tenant())
|
|
1327
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1328
|
+
tenantID := GetTenantID(ctx)
|
|
1329
|
+
assert.Equal(t, "550e8400-e29b-41d4-a716-446655440000", tenantID)
|
|
1330
|
+
ctx.Status(http.StatusOK)
|
|
1331
|
+
})
|
|
1332
|
+
|
|
1333
|
+
w := httptest.NewRecorder()
|
|
1334
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1335
|
+
req.Header.Set(types.HeaderTenantID, "550e8400-e29b-41d4-a716-446655440000")
|
|
1336
|
+
engine.ServeHTTP(w, req)
|
|
1337
|
+
|
|
1338
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
// TestTenant_SuperAdminCanAccessAnyTenant 验证超级管理员可访问任意租户
|
|
1342
|
+
func TestTenant_SuperAdminCanAccessAnyTenant(t *testing.T) {
|
|
1343
|
+
engine := gin.New()
|
|
1344
|
+
engine.Use(func(ctx *gin.Context) {
|
|
1345
|
+
ctx.Set(string(types.ContextKeyUserIdentity), &types.UserIdentity{
|
|
1346
|
+
UserID: 1,
|
|
1347
|
+
Type: "super_admin",
|
|
1348
|
+
TenantID: "my-tenant",
|
|
1349
|
+
})
|
|
1350
|
+
ctx.Next()
|
|
1351
|
+
})
|
|
1352
|
+
engine.Use(Tenant())
|
|
1353
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1354
|
+
ctx.Status(http.StatusOK)
|
|
1355
|
+
})
|
|
1356
|
+
|
|
1357
|
+
w := httptest.NewRecorder()
|
|
1358
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1359
|
+
// super_admin 访问其他租户
|
|
1360
|
+
req.Header.Set(types.HeaderTenantID, "other-tenant")
|
|
1361
|
+
engine.ServeHTTP(w, req)
|
|
1362
|
+
|
|
1363
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1366
|
+
// TestTenant_UserCannotAccessOtherTenant 验证普通用户无法访问不属于自己的租户
|
|
1367
|
+
func TestTenant_UserCannotAccessOtherTenant(t *testing.T) {
|
|
1368
|
+
engine := gin.New()
|
|
1369
|
+
engine.Use(func(ctx *gin.Context) {
|
|
1370
|
+
ctx.Set(string(types.ContextKeyUserIdentity), &types.UserIdentity{
|
|
1371
|
+
UserID: 1,
|
|
1372
|
+
Type: "user",
|
|
1373
|
+
TenantID: "my-tenant",
|
|
1374
|
+
})
|
|
1375
|
+
ctx.Next()
|
|
1376
|
+
})
|
|
1377
|
+
engine.Use(Tenant())
|
|
1378
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1379
|
+
ctx.Status(http.StatusOK)
|
|
1380
|
+
})
|
|
1381
|
+
|
|
1382
|
+
w := httptest.NewRecorder()
|
|
1383
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1384
|
+
req.Header.Set(types.HeaderTenantID, "another-tenant")
|
|
1385
|
+
engine.ServeHTTP(w, req)
|
|
1386
|
+
|
|
1387
|
+
assert.Equal(t, http.StatusForbidden, w.Code)
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
// TestGetTenantID_WhenSet 验证 GetTenantID 从 context 正确取值
|
|
1391
|
+
func TestGetTenantID_WhenSet(t *testing.T) {
|
|
1392
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1393
|
+
c.Set(string(types.ContextKeyTenantID), "my-tenant-123")
|
|
1394
|
+
assert.Equal(t, "my-tenant-123", GetTenantID(c))
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
// TestGetTenantID_WhenNotSet 验证 GetTenantID 无值时返回空字符串
|
|
1398
|
+
func TestGetTenantID_WhenNotSet(t *testing.T) {
|
|
1399
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1400
|
+
assert.Equal(t, "", GetTenantID(c))
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
// TestGetTenantInfo_WhenSet 验证 GetTenantInfo 从 context 正确取值
|
|
1404
|
+
func TestGetTenantInfo_WhenSet(t *testing.T) {
|
|
1405
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1406
|
+
info := &types.TenantInfo{ID: "abc", Status: types.TenantStatusActive}
|
|
1407
|
+
c.Set(string(types.ContextKeyTenant), info)
|
|
1408
|
+
|
|
1409
|
+
got := GetTenantInfo(c)
|
|
1410
|
+
require.NotNil(t, got)
|
|
1411
|
+
assert.Equal(t, "abc", got.ID)
|
|
1412
|
+
assert.Equal(t, types.TenantStatusActive, got.Status)
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
// TestGetTenantInfo_WhenNotSet 验证 GetTenantInfo 无值时返回 nil
|
|
1416
|
+
func TestGetTenantInfo_WhenNotSet(t *testing.T) {
|
|
1417
|
+
c, _ := newTestContext(http.MethodGet, "/")
|
|
1418
|
+
assert.Nil(t, GetTenantInfo(c))
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1422
|
+
// OptionalTenant 测试
|
|
1423
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1424
|
+
|
|
1425
|
+
// TestOptionalTenant_WithValidID 验证 OptionalTenant 有合法租户 ID 时注入 context
|
|
1426
|
+
func TestOptionalTenant_WithValidID(t *testing.T) {
|
|
1427
|
+
engine := gin.New()
|
|
1428
|
+
engine.Use(OptionalTenant())
|
|
1429
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1430
|
+
tenantID := GetTenantID(ctx)
|
|
1431
|
+
assert.Equal(t, "opt-tenant", tenantID)
|
|
1432
|
+
ctx.Status(http.StatusOK)
|
|
1433
|
+
})
|
|
1434
|
+
|
|
1435
|
+
w := httptest.NewRecorder()
|
|
1436
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1437
|
+
req.Header.Set(types.HeaderTenantID, "opt-tenant")
|
|
1438
|
+
engine.ServeHTTP(w, req)
|
|
1439
|
+
|
|
1440
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
// TestOptionalTenant_WithMissingID 验证 OptionalTenant 无租户 ID 时不阻断,context 中无租户 ID
|
|
1444
|
+
func TestOptionalTenant_WithMissingID(t *testing.T) {
|
|
1445
|
+
engine := gin.New()
|
|
1446
|
+
engine.Use(OptionalTenant())
|
|
1447
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1448
|
+
tenantID := GetTenantID(ctx)
|
|
1449
|
+
assert.Equal(t, "", tenantID)
|
|
1450
|
+
ctx.Status(http.StatusOK)
|
|
1451
|
+
})
|
|
1452
|
+
|
|
1453
|
+
w := httptest.NewRecorder()
|
|
1454
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1455
|
+
engine.ServeHTTP(w, req)
|
|
1456
|
+
|
|
1457
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1461
|
+
// 中间件链集成测试
|
|
1462
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1463
|
+
|
|
1464
|
+
// TestMiddlewareChain_GatewayIdentityAndProductContext 验证 GatewayIdentity 和 ProductContext 组合使用
|
|
1465
|
+
func TestMiddlewareChain_GatewayIdentityAndProductContext(t *testing.T) {
|
|
1466
|
+
engine := gin.New()
|
|
1467
|
+
engine.Use(
|
|
1468
|
+
RequestID(),
|
|
1469
|
+
newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{}),
|
|
1470
|
+
ProductContext(productContextReader(string(types.ProductPlatform)), coreclock.NewSystemClock()),
|
|
1471
|
+
ClientContext(),
|
|
1472
|
+
)
|
|
1473
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1474
|
+
// 验证所有 context 值都已注入
|
|
1475
|
+
assert.NotEmpty(t, GetRequestID(ctx))
|
|
1476
|
+
identity := GetUserIdentity(ctx)
|
|
1477
|
+
require.NotNil(t, identity)
|
|
1478
|
+
assert.Equal(t, types.UserID(123), identity.UserID)
|
|
1479
|
+
|
|
1480
|
+
product := GetProductCode(ctx)
|
|
1481
|
+
assert.Equal(t, types.ProductPlatform, product)
|
|
1482
|
+
|
|
1483
|
+
cc := GetClientContext(ctx)
|
|
1484
|
+
require.NotNil(t, cc)
|
|
1485
|
+
assert.Equal(t, "web", cc.Type)
|
|
1486
|
+
|
|
1487
|
+
ctx.Status(http.StatusOK)
|
|
1488
|
+
})
|
|
1489
|
+
|
|
1490
|
+
w := httptest.NewRecorder()
|
|
1491
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1492
|
+
applyGatewayAuthHeaders(req, 123, 7001, "jti-chain", "tenant-chain")
|
|
1493
|
+
req.Header.Set(types.HeaderProductCode, string(types.ProductPlatform))
|
|
1494
|
+
req.Header.Set(types.HeaderClientType, "web")
|
|
1495
|
+
engine.ServeHTTP(w, req)
|
|
1496
|
+
|
|
1497
|
+
assert.Equal(t, http.StatusOK, w.Code)
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
// TestMiddlewareChain_RecoveryWithGatewayIdentity 验证 Recovery 在复杂中间件链中可捕获 panic
|
|
1501
|
+
func TestMiddlewareChain_RecoveryWithGatewayIdentity(t *testing.T) {
|
|
1502
|
+
logger := zap.NewNop()
|
|
1503
|
+
|
|
1504
|
+
engine := gin.New()
|
|
1505
|
+
engine.Use(Recovery(logger), newGatewayIdentityMiddleware(&fakeGatewayBlacklistChecker{}))
|
|
1506
|
+
engine.GET("/", func(ctx *gin.Context) {
|
|
1507
|
+
panic("handler panic after auth")
|
|
1508
|
+
})
|
|
1509
|
+
|
|
1510
|
+
w := httptest.NewRecorder()
|
|
1511
|
+
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
1512
|
+
applyGatewayAuthHeaders(req, 1, 8001, "jti-recovery", "tenant-recovery")
|
|
1513
|
+
engine.ServeHTTP(w, req)
|
|
1514
|
+
|
|
1515
|
+
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
|
1516
|
+
}
|