From aca4c4eea9c9020bdaa08ee7a4a07088974e45be Mon Sep 17 00:00:00 2001 From: Vladislav Agapov Date: Thu, 29 Oct 2026 16:27:20 +0300 Subject: [PATCH] feat: add private S3 attachments and safe reconciliation --- .dockerignore | 28 +- .github/workflows/go.yml | 20 ++ .golangci.yml | 21 ++ ARCHITECTURE.md | 5 + Dockerfile | 16 + Makefile | 29 ++ REPORT.md | 87 +++-- charts/messenger/Chart.yaml | 6 + charts/messenger/templates/_helpers.tpl | 7 + charts/messenger/templates/api.yaml | 135 +++++++ charts/messenger/templates/guard.yaml | 7 + charts/messenger/templates/hpa.yaml | 33 ++ charts/messenger/templates/ingress.yaml | 23 ++ charts/messenger/templates/load.yaml | 39 ++ charts/messenger/templates/migrate.yaml | 32 ++ charts/messenger/templates/monitoring.yaml | 19 + .../messenger/templates/network-policy.yaml | 54 +++ charts/messenger/templates/postgres.yaml | 66 ++++ charts/messenger/templates/s3.yaml | 97 +++++ charts/messenger/templates/valkey.yaml | 57 +++ charts/messenger/values.schema.json | 9 + charts/messenger/values.yaml | 64 ++++ cmd/server/app.go | 105 ++++++ cmd/server/app_test.go | 108 ++++++ cmd/server/commands.go | 50 +++ cmd/server/healthcheck.go | 46 +++ cmd/server/healthcheck_test.go | 111 ++++++ cmd/server/logger.go | 29 ++ cmd/server/main.go | 15 + cmd/server/media.go | 39 ++ cmd/server/media_test.go | 88 +++++ cmd/server/persistence.go | 85 +++++ cmd/server/persistence_test.go | 66 ++++ cmd/server/reconcile.go | 37 ++ cmd/server/s3_bootstrap.go | 24 ++ docs/PLAN.md | 8 + docs/SPEC.md | 14 + evidence/current/attachment-tests.txt | 4 + evidence/current/coverage-per-file.json | 91 +++++ evidence/current/coverage.txt | 112 ++++++ evidence/current/environment.txt | 3 + evidence/current/image-build-retry.txt | 65 ++++ evidence/current/image-build.txt | 20 ++ evidence/current/image.json | 20 ++ evidence/current/lint.txt | 1 + evidence/current/reconciliation.txt | 16 + evidence/current/smoke.txt | 2 + evidence/current/source.sha256 | 183 ++++++++++ evidence/current/static.txt | 8 + evidence/current/tests.txt | 21 ++ evidence/current/upload-memory.txt | 1 + evidence/current/upload-rss-kib.txt | 24 ++ evidence/current/upload-summary.json | 218 ++++++++++++ evidence/current/upload.txt | 64 ++++ evidence/local/actions-errors.txt | 0 evidence/local/actions.txt | 4 + evidence/local/binary.sha256 | 1 + evidence/local/failover.txt | 4 + evidence/local/go-version.txt | 1 + evidence/local/local-a.txt | 0 evidence/local/local-b.txt | 0 evidence/local/persistence.txt | 3 + evidence/local/smoke-after.txt | 2 + evidence/local/smoke.txt | 2 + go.mod | 55 +++ go.sum | 128 +++++++ internal/admission/distributed.go | 18 + internal/admission/limiter.go | 73 ++++ internal/admission/limiter_test.go | 148 ++++++++ internal/config/config.go | 53 +++ internal/config/config_test.go | 241 +++++++++++++ internal/config/controller.go | 160 +++++++++ internal/config/environment.go | 41 +++ internal/config/logger.go | 32 ++ internal/config/memory.go | 54 +++ internal/config/objectstore.go | 35 ++ internal/config/objectstore_test.go | 24 ++ internal/config/persistence.go | 55 +++ internal/config/persistence_test.go | 34 ++ internal/controller/fx.go | 8 + internal/controller/http/fiber/attachments.go | 112 ++++++ .../controller/http/fiber/attachments_test.go | 102 ++++++ internal/controller/http/fiber/auth.go | 76 ++++ internal/controller/http/fiber/auth_test.go | 161 +++++++++ .../controller/http/fiber/body_limit_test.go | 69 ++++ internal/controller/http/fiber/chats_test.go | 312 ++++++++++++++++ internal/controller/http/fiber/configure.go | 37 ++ internal/controller/http/fiber/controller.go | 66 ++++ .../controller/http/fiber/controller_test.go | 321 +++++++++++++++++ internal/controller/http/fiber/decode.go | 34 ++ .../controller/http/fiber/dependencies.go | 9 + internal/controller/http/fiber/error_codes.go | 22 ++ .../controller/http/fiber/error_registry.go | 33 ++ internal/controller/http/fiber/errors.go | 73 ++++ internal/controller/http/fiber/errors_test.go | 107 ++++++ .../http/fiber/fixtures_auth_test.go | 61 ++++ internal/controller/http/fiber/handlers.go | 98 +++++ internal/controller/http/fiber/headers.go | 7 + internal/controller/http/fiber/health.go | 28 ++ internal/controller/http/fiber/lifecycle.go | 70 ++++ .../controller/http/fiber/lifecycle_test.go | 132 +++++++ internal/controller/http/fiber/mapping.go | 79 ++++ .../controller/http/fiber/messages_test.go | 270 ++++++++++++++ internal/controller/http/fiber/metrics.go | 61 ++++ .../http/fiber/metrics_labels_test.go | 38 ++ .../controller/http/fiber/metrics_test.go | 73 ++++ internal/controller/http/fiber/middleware.go | 90 +++++ .../controller/http/fiber/middleware_test.go | 211 +++++++++++ .../controller/http/fiber/public_errors.go | 55 +++ .../http/fiber/public_errors_test.go | 43 +++ internal/controller/http/fiber/routes.go | 26 ++ internal/controller/http/fiber/stream.go | 40 +++ internal/controller/http/fiber/users_test.go | 130 +++++++ internal/controller/interface.go | 12 + internal/cursor/codec.go | 119 +++++++ internal/cursor/codec_test.go | 212 +++++++++++ internal/hashring/ring.go | 62 ++++ internal/hashring/ring_test.go | 73 ++++ internal/media/orphans.go | 76 ++++ internal/media/reconcile.go | 102 ++++++ internal/media/reconcile_test.go | 159 +++++++++ internal/media/run_reconcile.go | 55 +++ internal/media/run_reconcile_test.go | 92 +++++ internal/media/service.go | 145 ++++++++ internal/media/service_test.go | 225 ++++++++++++ internal/media/validation.go | 48 +++ internal/media/validation_test.go | 65 ++++ internal/model/dto/attachment.go | 33 ++ internal/model/dto/models.go | 118 ++++++ internal/model/dto/session.go | 13 + internal/model/http/models.go | 102 ++++++ internal/objectstore/client.go | 110 ++++++ internal/objectstore/client_test.go | 193 ++++++++++ internal/objectstore/list.go | 26 ++ internal/password/password.go | 54 +++ internal/password/password_test.go | 37 ++ internal/service/auth.go | 44 +++ internal/service/authentication/service.go | 123 +++++++ .../service/authentication/service_test.go | 232 ++++++++++++ internal/service/cache.go | 17 + internal/service/cursor.go | 12 + internal/service/errors.go | 27 ++ internal/service/interface.go | 36 ++ internal/service/production/access.go | 62 ++++ internal/service/production/cache_test.go | 84 +++++ internal/service/production/chats.go | 47 +++ .../service/production/chats_page_test.go | 207 +++++++++++ internal/service/production/chats_test.go | 261 ++++++++++++++ internal/service/production/creation.go | 75 ++++ internal/service/production/errors.go | 23 ++ internal/service/production/fixture_test.go | 147 ++++++++ internal/service/production/members.go | 42 +++ internal/service/production/messages.go | 38 ++ .../service/production/messages_page_test.go | 231 ++++++++++++ internal/service/production/messages_test.go | 170 +++++++++ internal/service/production/pages.go | 49 +++ internal/service/production/pages_test.go | 53 +++ internal/service/production/pagination.go | 78 ++++ .../service/production/pagination_test.go | 74 ++++ internal/service/production/service.go | 39 ++ internal/service/production/service_test.go | 39 ++ internal/service/production/users.go | 47 +++ internal/service/production/users_test.go | 289 +++++++++++++++ internal/service/production/validation.go | 51 +++ internal/store/interface.go | 54 +++ internal/store/memory/capacity.go | 38 ++ internal/store/memory/chats.go | 66 ++++ internal/store/memory/chats_test.go | 148 ++++++++ internal/store/memory/messages.go | 41 +++ internal/store/memory/messages_test.go | 72 ++++ internal/store/memory/ordering.go | 25 ++ internal/store/memory/store.go | 47 +++ internal/store/memory/store_test.go | 64 ++++ internal/store/memory/users.go | 50 +++ internal/store/memory/users_test.go | 116 ++++++ internal/store/postgres/attachments.go | 69 ++++ internal/store/postgres/attachments_test.go | 167 +++++++++ internal/store/postgres/chats.go | 58 +++ internal/store/postgres/errors_test.go | 52 +++ internal/store/postgres/idempotency.go | 52 +++ internal/store/postgres/idempotency_test.go | 139 ++++++++ internal/store/postgres/messages.go | 64 ++++ .../store/postgres/migrations/001_initial.sql | 34 ++ .../postgres/migrations/002_idempotency.sql | 9 + .../postgres/migrations/003_attachments.sql | 15 + internal/store/postgres/reconcile.go | 64 ++++ .../store/postgres/reconcile_fixture_test.go | 18 + internal/store/postgres/research_test.go | 52 +++ internal/store/postgres/sessions.go | 36 ++ internal/store/postgres/store.go | 152 ++++++++ internal/store/postgres/store_test.go | 336 ++++++++++++++++++ internal/store/postgres/users.go | 58 +++ internal/strictjson/decode.go | 101 ++++++ internal/strictjson/decode_test.go | 52 +++ internal/strictjson/optional_test.go | 33 ++ internal/valkey/admission.go | 132 +++++++ internal/valkey/cache.go | 42 +++ internal/valkey/client.go | 113 ++++++ internal/valkey/client_test.go | 247 +++++++++++++ scripts/coverage.awk | 35 ++ scripts/coverage.sh | 83 +++++ scripts/init-secrets.sh | 37 ++ scripts/local-action.sh | 50 +++ scripts/measure-upload.sh | 19 + scripts/port-forward.sh | 21 ++ scripts/restart.sh | 21 ++ scripts/stop-one.sh | 10 + scripts/test-local.sh | 101 ++++++ student-tests/README.md | 11 + student-tests/load/attachments.js | 43 +++ student-tests/load/failover.js | 61 ++++ student-tests/load/hpa.js | 18 + student-tests/load/idempotency.js | 34 ++ student-tests/load/scaling.js | 77 ++++ student-tests/plans.sql | 16 + student-tests/quality_test.go | 125 +++++++ student-tests/research.sql | 26 ++ 217 files changed, 15042 insertions(+), 44 deletions(-) create mode 100644 .github/workflows/go.yml create mode 100644 .golangci.yml create mode 100644 ARCHITECTURE.md create mode 100644 Dockerfile create mode 100644 charts/messenger/Chart.yaml create mode 100644 charts/messenger/templates/_helpers.tpl create mode 100644 charts/messenger/templates/api.yaml create mode 100644 charts/messenger/templates/guard.yaml create mode 100644 charts/messenger/templates/hpa.yaml create mode 100644 charts/messenger/templates/ingress.yaml create mode 100644 charts/messenger/templates/load.yaml create mode 100644 charts/messenger/templates/migrate.yaml create mode 100644 charts/messenger/templates/monitoring.yaml create mode 100644 charts/messenger/templates/network-policy.yaml create mode 100644 charts/messenger/templates/postgres.yaml create mode 100644 charts/messenger/templates/s3.yaml create mode 100644 charts/messenger/templates/valkey.yaml create mode 100644 charts/messenger/values.schema.json create mode 100644 charts/messenger/values.yaml create mode 100644 cmd/server/app.go create mode 100644 cmd/server/app_test.go create mode 100644 cmd/server/commands.go create mode 100644 cmd/server/healthcheck.go create mode 100644 cmd/server/healthcheck_test.go create mode 100644 cmd/server/logger.go create mode 100644 cmd/server/main.go create mode 100644 cmd/server/media.go create mode 100644 cmd/server/media_test.go create mode 100644 cmd/server/persistence.go create mode 100644 cmd/server/persistence_test.go create mode 100644 cmd/server/reconcile.go create mode 100644 cmd/server/s3_bootstrap.go create mode 100644 docs/PLAN.md create mode 100644 docs/SPEC.md create mode 100644 evidence/current/attachment-tests.txt create mode 100644 evidence/current/coverage-per-file.json create mode 100644 evidence/current/coverage.txt create mode 100644 evidence/current/environment.txt create mode 100644 evidence/current/image-build-retry.txt create mode 100644 evidence/current/image-build.txt create mode 100644 evidence/current/image.json create mode 100644 evidence/current/lint.txt create mode 100644 evidence/current/reconciliation.txt create mode 100644 evidence/current/smoke.txt create mode 100644 evidence/current/source.sha256 create mode 100644 evidence/current/static.txt create mode 100644 evidence/current/tests.txt create mode 100644 evidence/current/upload-memory.txt create mode 100644 evidence/current/upload-rss-kib.txt create mode 100644 evidence/current/upload-summary.json create mode 100644 evidence/current/upload.txt create mode 100644 evidence/local/actions-errors.txt create mode 100644 evidence/local/actions.txt create mode 100644 evidence/local/binary.sha256 create mode 100644 evidence/local/failover.txt create mode 100644 evidence/local/go-version.txt create mode 100644 evidence/local/local-a.txt create mode 100644 evidence/local/local-b.txt create mode 100644 evidence/local/persistence.txt create mode 100644 evidence/local/smoke-after.txt create mode 100644 evidence/local/smoke.txt create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/admission/distributed.go create mode 100644 internal/admission/limiter.go create mode 100644 internal/admission/limiter_test.go create mode 100644 internal/config/config.go create mode 100644 internal/config/config_test.go create mode 100644 internal/config/controller.go create mode 100644 internal/config/environment.go create mode 100644 internal/config/logger.go create mode 100644 internal/config/memory.go create mode 100644 internal/config/objectstore.go create mode 100644 internal/config/objectstore_test.go create mode 100644 internal/config/persistence.go create mode 100644 internal/config/persistence_test.go create mode 100644 internal/controller/fx.go create mode 100644 internal/controller/http/fiber/attachments.go create mode 100644 internal/controller/http/fiber/attachments_test.go create mode 100644 internal/controller/http/fiber/auth.go create mode 100644 internal/controller/http/fiber/auth_test.go create mode 100644 internal/controller/http/fiber/body_limit_test.go create mode 100644 internal/controller/http/fiber/chats_test.go create mode 100644 internal/controller/http/fiber/configure.go create mode 100644 internal/controller/http/fiber/controller.go create mode 100644 internal/controller/http/fiber/controller_test.go create mode 100644 internal/controller/http/fiber/decode.go create mode 100644 internal/controller/http/fiber/dependencies.go create mode 100644 internal/controller/http/fiber/error_codes.go create mode 100644 internal/controller/http/fiber/error_registry.go create mode 100644 internal/controller/http/fiber/errors.go create mode 100644 internal/controller/http/fiber/errors_test.go create mode 100644 internal/controller/http/fiber/fixtures_auth_test.go create mode 100644 internal/controller/http/fiber/handlers.go create mode 100644 internal/controller/http/fiber/headers.go create mode 100644 internal/controller/http/fiber/health.go create mode 100644 internal/controller/http/fiber/lifecycle.go create mode 100644 internal/controller/http/fiber/lifecycle_test.go create mode 100644 internal/controller/http/fiber/mapping.go create mode 100644 internal/controller/http/fiber/messages_test.go create mode 100644 internal/controller/http/fiber/metrics.go create mode 100644 internal/controller/http/fiber/metrics_labels_test.go create mode 100644 internal/controller/http/fiber/metrics_test.go create mode 100644 internal/controller/http/fiber/middleware.go create mode 100644 internal/controller/http/fiber/middleware_test.go create mode 100644 internal/controller/http/fiber/public_errors.go create mode 100644 internal/controller/http/fiber/public_errors_test.go create mode 100644 internal/controller/http/fiber/routes.go create mode 100644 internal/controller/http/fiber/stream.go create mode 100644 internal/controller/http/fiber/users_test.go create mode 100644 internal/controller/interface.go create mode 100644 internal/cursor/codec.go create mode 100644 internal/cursor/codec_test.go create mode 100644 internal/hashring/ring.go create mode 100644 internal/hashring/ring_test.go create mode 100644 internal/media/orphans.go create mode 100644 internal/media/reconcile.go create mode 100644 internal/media/reconcile_test.go create mode 100644 internal/media/run_reconcile.go create mode 100644 internal/media/run_reconcile_test.go create mode 100644 internal/media/service.go create mode 100644 internal/media/service_test.go create mode 100644 internal/media/validation.go create mode 100644 internal/media/validation_test.go create mode 100644 internal/model/dto/attachment.go create mode 100644 internal/model/dto/models.go create mode 100644 internal/model/dto/session.go create mode 100644 internal/model/http/models.go create mode 100644 internal/objectstore/client.go create mode 100644 internal/objectstore/client_test.go create mode 100644 internal/objectstore/list.go create mode 100644 internal/password/password.go create mode 100644 internal/password/password_test.go create mode 100644 internal/service/auth.go create mode 100644 internal/service/authentication/service.go create mode 100644 internal/service/authentication/service_test.go create mode 100644 internal/service/cache.go create mode 100644 internal/service/cursor.go create mode 100644 internal/service/errors.go create mode 100644 internal/service/interface.go create mode 100644 internal/service/production/access.go create mode 100644 internal/service/production/cache_test.go create mode 100644 internal/service/production/chats.go create mode 100644 internal/service/production/chats_page_test.go create mode 100644 internal/service/production/chats_test.go create mode 100644 internal/service/production/creation.go create mode 100644 internal/service/production/errors.go create mode 100644 internal/service/production/fixture_test.go create mode 100644 internal/service/production/members.go create mode 100644 internal/service/production/messages.go create mode 100644 internal/service/production/messages_page_test.go create mode 100644 internal/service/production/messages_test.go create mode 100644 internal/service/production/pages.go create mode 100644 internal/service/production/pages_test.go create mode 100644 internal/service/production/pagination.go create mode 100644 internal/service/production/pagination_test.go create mode 100644 internal/service/production/service.go create mode 100644 internal/service/production/service_test.go create mode 100644 internal/service/production/users.go create mode 100644 internal/service/production/users_test.go create mode 100644 internal/service/production/validation.go create mode 100644 internal/store/interface.go create mode 100644 internal/store/memory/capacity.go create mode 100644 internal/store/memory/chats.go create mode 100644 internal/store/memory/chats_test.go create mode 100644 internal/store/memory/messages.go create mode 100644 internal/store/memory/messages_test.go create mode 100644 internal/store/memory/ordering.go create mode 100644 internal/store/memory/store.go create mode 100644 internal/store/memory/store_test.go create mode 100644 internal/store/memory/users.go create mode 100644 internal/store/memory/users_test.go create mode 100644 internal/store/postgres/attachments.go create mode 100644 internal/store/postgres/attachments_test.go create mode 100644 internal/store/postgres/chats.go create mode 100644 internal/store/postgres/errors_test.go create mode 100644 internal/store/postgres/idempotency.go create mode 100644 internal/store/postgres/idempotency_test.go create mode 100644 internal/store/postgres/messages.go create mode 100644 internal/store/postgres/migrations/001_initial.sql create mode 100644 internal/store/postgres/migrations/002_idempotency.sql create mode 100644 internal/store/postgres/migrations/003_attachments.sql create mode 100644 internal/store/postgres/reconcile.go create mode 100644 internal/store/postgres/reconcile_fixture_test.go create mode 100644 internal/store/postgres/research_test.go create mode 100644 internal/store/postgres/sessions.go create mode 100644 internal/store/postgres/store.go create mode 100644 internal/store/postgres/store_test.go create mode 100644 internal/store/postgres/users.go create mode 100644 internal/strictjson/decode.go create mode 100644 internal/strictjson/decode_test.go create mode 100644 internal/strictjson/optional_test.go create mode 100644 internal/valkey/admission.go create mode 100644 internal/valkey/cache.go create mode 100644 internal/valkey/client.go create mode 100644 internal/valkey/client_test.go create mode 100644 scripts/coverage.awk create mode 100755 scripts/coverage.sh create mode 100755 scripts/init-secrets.sh create mode 100755 scripts/local-action.sh create mode 100755 scripts/measure-upload.sh create mode 100755 scripts/port-forward.sh create mode 100755 scripts/restart.sh create mode 100755 scripts/stop-one.sh create mode 100755 scripts/test-local.sh create mode 100644 student-tests/README.md create mode 100644 student-tests/load/attachments.js create mode 100644 student-tests/load/failover.js create mode 100644 student-tests/load/hpa.js create mode 100644 student-tests/load/idempotency.js create mode 100644 student-tests/load/scaling.js create mode 100644 student-tests/plans.sql create mode 100644 student-tests/quality_test.go create mode 100644 student-tests/research.sql diff --git a/.dockerignore b/.dockerignore index bc16e55..a4397ed 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,20 +1,8 @@ -.git -.github -.env -.env.* -!.env.example -secrets -certs -volumes -data -backups -*.key -*.pem -*.p12 -*.pfx -*.jks -.venv -__pycache__ -node_modules -evidence -.DS_Store +* +!go.mod +!go.sum +!cmd/ +!cmd/** +!internal/ +!internal/** +!Dockerfile diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..ea1773c --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,20 @@ +name: Go correctness and per-file coverage +on: [push, pull_request] +jobs: + verify: + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: '1.26.1' + - run: sudo apt-get update && sudo apt-get install -y jq ruby + - run: go vet ./... + - run: test -z "$(gofmt -l cmd internal)" + - run: LOCAL_COVERAGE=true scripts/test-local.sh + - uses: actions/upload-artifact@v4 + if: always() + with: + name: coverage + path: coverage/summary.json diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..1a5f7df --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,21 @@ +version: "2" +run: + timeout: 3m +linters: + default: none + enable: + - errcheck + - govet + - ineffassign + - staticcheck + - unused + - revive + - gocritic +formatters: + enable: + - gofmt + - goimports + settings: + goimports: + local-prefixes: + - gitea.privcloud.tech/vlad_marlo/mtusi2026lab4 diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md new file mode 100644 index 0000000..51573d0 --- /dev/null +++ b/ARCHITECTURE.md @@ -0,0 +1,5 @@ +# Архитектура lab4 + +Fiber controller зависит от интерфейса приложения; application service проверяет пользователя, чат и файл. PostgreSQL store обеспечивает атомарность метаданных/сообщений. S3 adapter хранит приватные байты по server-generated UUID key. Fx связывает реализации. Метаданные DTO не содержат транспортных типов, ключ S3 не попадает в HTTP. + +Upload ограничен по размеру и числу одновременно декодируемых изображений. Сначала DecodeConfig проверяет пиксели, затем полный decode выявляет повреждение. После проверки временный decoded buffer освобождается; исходные байты передаются S3 без повторного полного буфера. Между PostgreSQL и S3 нет распределённой транзакции: резервирование, ready commit и периодическая сверка явно закрывают окна отказа. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5a47956 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM golang:1.26.1-alpine3.23 AS build +WORKDIR /src +COPY go.mod go.sum ./ +RUN go mod download +COPY cmd/ ./cmd/ +COPY internal/ ./internal/ +RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server + +FROM scratch AS runtime +COPY --from=build /out/server /server +USER 65532:65532 +ENV APP_HOST=0.0.0.0 APP_PORT=8080 GOMEMLIMIT=160MiB +EXPOSE 8080 +STOPSIGNAL SIGTERM +HEALTHCHECK --interval=5s --timeout=3s --start-period=3s --retries=3 CMD ["/server", "healthcheck"] +ENTRYPOINT ["/server"] diff --git a/Makefile b/Makefile index 5d86d17..58d64d6 100644 --- a/Makefile +++ b/Makefile @@ -28,3 +28,32 @@ test-persistence: test-failover: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +.PHONY: test-student vet lint coverage chart-check +test-student: + go test -race ./... +vet: + go vet ./... +lint: + golangci-lint run ./... +coverage: + scripts/coverage.sh +chart-check: + helm lint charts/messenger + helm template lab2 charts/messenger >/dev/null + +KUBE_CONTEXT ?= dev +NAMESPACE ?= highload-applications +RELEASE ?= lab2 +IMAGE ?= asisti.registry.twcstorage.ru/mtusi2026lab4 +TAG ?= local +.PHONY: init-secrets deploy build-push test-local +init-secrets: + KUBE_CONTEXT=$(KUBE_CONTEXT) NAMESPACE=$(NAMESPACE) scripts/init-secrets.sh +build-push: + docker buildx build --platform linux/amd64 --target runtime --tag $(IMAGE):$(TAG) --metadata-file image-build.json --push . + @echo 'Copy containerimage.digest from image-build.json into charts/messenger/values.yaml before deploy.' +deploy: chart-check + helm upgrade --install $(RELEASE) charts/messenger --kube-context $(KUBE_CONTEXT) --namespace $(NAMESPACE) --wait --wait-for-jobs --timeout 8m +test-local: + scripts/test-local.sh diff --git a/REPORT.md b/REPORT.md index 9ad2b84..127c0b1 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,76 @@ -# Отчёт — лабораторная 4 +# Лабораторная 4: приватные вложения -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +Автор: Агапов, БПИ2303. Целевой уровень: 5, трек 2 — согласованность PostgreSQL и S3. Оценка не выставлена. -## Запуск из чистого clone +## Запуск -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Нужны Go 1.26.1, Docker, Helm, kubectl, jq, openssl; для публичных тестов Python 3.9+, для нагрузки k6. Образы и ресурсы закреплены в `charts/messenger/values.yaml`. Используется SeaweedFS 3.85 с закреплённым digest, PostgreSQL 17.6 и Valkey 8.1.3. SDK — minio-go v7.0.95. -## Архитектура и решения +```sh +export GOTOOLCHAIN=go1.26.1 +make check +make test-local +make chart-check +# Токен реестра находится только в локальном файле ~/.secrets/twc-registry-token. +make init-secrets +make deploy +make test BASE_URL=https://hl.agapov.space +``` -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Helm добавляет S3 в существующий release `lab2`, namespace `highload-applications`, context `dev`. Для другого стенда переопределить Make-параметры и `ingress.host`. Bootstrap создаёт приватный bucket идемпотентно; миграции выполняет Job. Secret генерация сохраняет уже существующие SQL/S3 credentials. Registry secret создаётся из файла токена; секреты не попадают в values. -## Доказательства критериев +Для своего образа: `make build-push TAG=`, перенести `containerimage.digest` из `image-build.json` в values, затем `make deploy`. Опубликованный образ указан в [image.json](evidence/current/image.json). Публикация образа не означает применение в кластере. -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +Безопасная остановка — масштабирование API до нуля после выключения HPA через values. PVC сохраняются. Для намеренного удаления данных отдельного учебного стенда: сначала `helm uninstall lab2 -n highload-applications --kube-context dev`, затем вручную удалить только принадлежащие этому стенду PVC, сверив имена. Общий namespace не удалять. Для проверки перезапуска с реальными PVC предусмотрен `scripts/restart.sh`; HPA должен быть заранее отключён, затем восстановлен. -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +## Устройство -## Эксперименты и отказы +[Архитектура](ARCHITECTURE.md), [требования](docs/SPEC.md). Сохранены текстовые сообщения, сессии, распределённый admission control, идемпотентность, курсорная пагинация и идентификатор API-реплики. -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +PostgreSQL хранит метаданные; приватное S3 — исходные байты. Сначала создаётся `uploading` reservation, затем выполняется PUT, затем metadata становится `ready`. Только после этого HTTP возвращает 201. Неопределённый результат COMMIT не вызывает опасного компенсирующего удаления. Download авторизуется через членство в чате и передаётся потоком. Его context и admission lease живут до закрытия потока. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +MIME определяется по данным, полный decode обнаруживает повреждение; до него проверяются 10 MiB и 40 млн пикселей. Одновременно декодируются не более двух изображений на процесс. Transport ограничен 16 соединениями; в chart memory limit 768 MiB и GOMEMLIMIT 640 MiB. Последний не является жёстким пределом RSS. API имеет права только Read/Write/List для bucket attachments; административные credentials доступны bootstrap Job. S3 Service не открывает консоль; NetworkPolicy разрешает порт 9000 только учебным клиентам. -## Ограничения и следующие шаги +## Проверки -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Измерения выполнены 29.10.2026 на локальном Apple Silicon, API — native Go, зависимости — Docker Desktop. Локальные процессы не имели Kubernetes CPU/RAM limits; эти результаты не измеряют dev-кластер. + +| Критерий | Проверка | Доказательство | +| --- | --- | --- | +| Upload/message/download/checksum/ACL | Публичный smoke | [smoke](evidence/local/smoke.txt) | +| Другая API-реплика после остановки | Публичный failover, остановка наблюдаемого INSTANCE_ID | [failover](evidence/local/failover.txt), [actions](evidence/local/actions.txt) | +| Сохранность после рестарта PG/Valkey/S3/API | Публичный persistence с настоящим hook, затем smoke | [persistence](evidence/local/persistence.txt), [smoke-after](evidence/local/smoke-after.txt) | +| MIME, повреждение, размер, пиксели, filename, чужой чат | Go regression tests media/controller/store | [race+coverage](evidence/current/coverage.txt) | +| Приватность и отдельные credentials | Реальные S3 тесты: anonymous GET запрещён, чужой bucket запрещён, повтор bootstrap | Тот же coverage log | +| Ограничение отказов S3 | Недоступный endpoint с timeout, безопасное HTTP-маппирование | Тот же coverage log | +| Уровень 90% для каждого файла | Общий профиль unit/integration/process | [JSON](evidence/current/coverage-per-file.json) | +| 10 одновременных загрузок | k6 + RSS sampler | [summary](evidence/current/upload-summary.json), [RSS](evidence/current/upload-rss-kib.txt) | +| Сверка и защита используемых/свежих объектов | Fault tests + реальные SQL CAS/leases | coverage log; [измерение](evidence/current/reconciliation.txt) | + +Первый вариант restart hook обращался к прежним портам после Docker restart и не прошёл. Исправленный hook заново получает назначенные порты. Итоговые доказательства относятся к исправленному сценарию. Он сохраняет файловые системы контейнеров; пересоздание StatefulSet с PVC этим локальным тестом не проверено. + +## Эксперимент с памятью + +Гипотеза: ограниченная конкурентность decode и потоковый download удерживают память при десяти одновременных upload. `student-tests/load/attachments.js`: 10 VU, 100 операций, fixture PNG 320×200, RSS опрашивается каждые 100 ms скриптом `scripts/measure-upload.sh`. Успешны 67 upload, 33 запроса отклонены ограничителями; p95 upload — 19.586 ms. Пиковый RSS — 80032 KiB (78.16 MiB). Raw-данные сохранены в таблице выше. + +Это один короткий прогон с небольшим файлом, без прогрева и повторов. Он подтверждает измеренный пик и обработку перегрузки, но не доказывает такой же RSS для 40-мегапиксельных изображений или выполнение SLO. Отклонения не включены в число успешных операций. + +## Трек 2: сверка двух хранилищ + +Гипотеза: durable reservation и ограниченный повторяемый обход позволяют безопасно исправлять разрывы без распределённой транзакции. + +```sh +# Команды запускаются с теми же конфигурацией и credentials, что API. +./server reconcile +./server reconcile --apply +``` + +Dry-run — по умолчанию. Сканирование ограничено страницами по 1000 записей/объектов, grace period не менее часа, PUT ограничен 15 секундами. Старый uploading атомарно переводится в deleting только при отсутствии ссылок из сообщений; поздний publish после этого запрещён. Неопределённое удаление повторяется. Существующий ready без объекта становится missing, метаданные и ссылки сохраняются. Найденный снова объект позволяет восстановить ready. Наличие любого reservation защищает объект от orphan-cleanup. При ошибке зависимости обход прекращается, а не принимает недоступность за отсутствие данных. + +Проверены dry-run, повтор apply, свежие/используемые объекты, отказ PUT/публикации, повтор удаления, гонка publish/reclaim. Это управляемые fault tests и реальные SQL/S3 операции, а не убийство процесса на каждой инструкции. Для инвентаря ready без объектов стоимость dry-run: 100 записей — 45.622 ms, 1000 — 457.760 ms. По одному прогону каждого размера; в данном профиле рост приблизительно линейный. Бounded pagination ограничивает память, но число HEAD/LIST и полное время остаются пропорциональны объёму. Восстановление missing проверяет наличие объекта, не повторный checksum. + +## Ограничения и источники + +Kubernetes — реализованный путь развёртывания; замена требуемого заданием Compose/Swarm требует подтверждения преподавателя. Нет HA PostgreSQL/S3, репликации между площадками и восстановления после потери диска. Отдельного доказательства полного развёртывания этой версии в dev пока нет. Локальные проверки и публикация образа не заменяют его. + +Контракт и публичные тесты сохранены. Использованы официальные [S3 API minio-go](https://github.com/minio/minio-go/blob/master/docs/API.md) и [конфигурация SeaweedFS S3](https://github.com/seaweedfs/seaweedfs/blob/master/weed/command/s3.go). При разработке использовалась помощь ИИ; приведённые результаты относятся к выполненным проверкам, ограничения указаны отдельно. diff --git a/charts/messenger/Chart.yaml b/charts/messenger/Chart.yaml new file mode 100644 index 0000000..b6f6d1f --- /dev/null +++ b/charts/messenger/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: messenger +description: Учебный мессенджер с PostgreSQL и шардированным Valkey +version: 0.1.0 +appVersion: lab3 +type: application diff --git a/charts/messenger/templates/_helpers.tpl b/charts/messenger/templates/_helpers.tpl new file mode 100644 index 0000000..2fcf371 --- /dev/null +++ b/charts/messenger/templates/_helpers.tpl @@ -0,0 +1,7 @@ +{{- define "messenger.shards" -}} +{{- $nodes := list -}} +{{- range $i := until (int .Values.valkey.shards) -}} +{{- $nodes = append $nodes (printf "shard%d=valkey-%d.valkey:6379" $i $i) -}} +{{- end -}} +{{- join "," $nodes -}} +{{- end -}} diff --git a/charts/messenger/templates/api.yaml b/charts/messenger/templates/api.yaml new file mode 100644 index 0000000..c53922a --- /dev/null +++ b/charts/messenger/templates/api.yaml @@ -0,0 +1,135 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: messenger-config + namespace: {{ .Release.Namespace }} +data: + APP_PORT: '8080' + VALKEY_COORDINATOR: shard0 + VALKEY_SHARDS: {{ include "messenger.shards" . | quote }} +{{- range $key, $value := .Values.api.env }} + {{ $key }}: {{ $value | quote }} +{{- end }} +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: api + name: api + namespace: {{ .Release.Namespace }} +spec: + selector: + app: api + ports: + - name: http + port: 8080 + targetPort: 8080 +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: api + namespace: {{ .Release.Namespace }} +spec: +{{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.api.replicas }} +{{- end }} + strategy: + rollingUpdate: + maxUnavailable: 0 + maxSurge: 1 + selector: + matchLabels: + app: api + template: + metadata: + annotations: + checksum/config: {{ printf "%s%s" (include "messenger.shards" .) (toJson .Values.api.env) | sha256sum }} + labels: + app: api + spec: + {{- with .Values.api.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + imagePullSecrets: + - name: {{ .Values.imagePullSecret }} + terminationGracePeriodSeconds: 20 + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app: api + securityContext: + runAsNonRoot: true + runAsUser: 65532 + seccompProfile: + type: RuntimeDefault + containers: + - name: api + image: "{{ .Values.image.repository }}@{{ .Values.image.digest }}" + imagePullPolicy: IfNotPresent + ports: + - containerPort: 8080 + envFrom: + - configMapRef: + name: messenger-config + env: + - name: S3_ENDPOINT + value: s3:9000 + - name: S3_BUCKET + value: {{ .Values.s3.bucket }} + - name: S3_TLS + value: "false" + - name: S3_ACCESS_KEY + valueFrom: + secretKeyRef: {name: {{ .Values.s3.secret }}, key: access-key} + - name: S3_SECRET_KEY + valueFrom: + secretKeyRef: {name: {{ .Values.s3.secret }}, key: secret-key} + - name: INSTANCE_ID + valueFrom: + fieldRef: + fieldPath: metadata.name + - name: CURSOR_SIGNING_KEY + valueFrom: + secretKeyRef: + name: {{ .Values.databaseSecret }} + key: cursor-key + - name: DATABASE_URL + valueFrom: + secretKeyRef: + name: {{ .Values.databaseSecret }} + key: database-url + resources: + {{- toYaml .Values.api.resources | nindent 12 }} + lifecycle: + preStop: + sleep: + seconds: 3 + startupProbe: + httpGet: + path: /health/ready + port: 8080 + periodSeconds: 2 + failureThreshold: 30 + readinessProbe: + timeoutSeconds: {{ .Values.api.readiness.timeoutSeconds }} + failureThreshold: {{ .Values.api.readiness.failureThreshold }} + httpGet: + path: /health/ready + port: 8080 + periodSeconds: 2 + livenessProbe: + httpGet: + path: /health/live + port: 8080 + periodSeconds: 5 + securityContext: + readOnlyRootFilesystem: true + allowPrivilegeEscalation: false + capabilities: + drop: [ALL] diff --git a/charts/messenger/templates/guard.yaml b/charts/messenger/templates/guard.yaml new file mode 100644 index 0000000..313b843 --- /dev/null +++ b/charts/messenger/templates/guard.yaml @@ -0,0 +1,7 @@ +{{- $live := lookup "v1" "ConfigMap" .Release.Namespace "messenger-config" -}} +{{- if and $live (ne (index $live.data "VALKEY_SHARDS") (include "messenger.shards" .)) -}} +{{- $api := lookup "apps/v1" "Deployment" .Release.Namespace "api" -}} +{{- if or (gt (int .Values.api.replicas) 0) (and $api (gt (int $api.spec.replicas) 0)) -}} +{{- fail "Changing shards requires drained API: use make reshard SHARDS=2 or SHARDS=3" -}} +{{- end -}} +{{- end -}} diff --git a/charts/messenger/templates/hpa.yaml b/charts/messenger/templates/hpa.yaml new file mode 100644 index 0000000..a6b8d72 --- /dev/null +++ b/charts/messenger/templates/hpa.yaml @@ -0,0 +1,33 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: api +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: api + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: {{ .Values.autoscaling.cpuPercent }} + behavior: + scaleUp: + stabilizationWindowSeconds: 15 + policies: + - type: Pods + value: 1 + periodSeconds: 30 + scaleDown: + stabilizationWindowSeconds: 120 + policies: + - type: Pods + value: 1 + periodSeconds: 60 +{{- end }} diff --git a/charts/messenger/templates/ingress.yaml b/charts/messenger/templates/ingress.yaml new file mode 100644 index 0000000..3bc3d8a --- /dev/null +++ b/charts/messenger/templates/ingress.yaml @@ -0,0 +1,23 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: messenger + annotations: + cert-manager.io/cluster-issuer: {{ .Values.ingress.clusterIssuer | quote }} +spec: + ingressClassName: {{ .Values.ingress.className }} + tls: + - hosts: + - {{ .Values.ingress.host | quote }} + secretName: messenger-tls + rules: + - host: {{ .Values.ingress.host | quote }} + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: api + port: + number: 8080 diff --git a/charts/messenger/templates/load.yaml b/charts/messenger/templates/load.yaml new file mode 100644 index 0000000..b9a5f13 --- /dev/null +++ b/charts/messenger/templates/load.yaml @@ -0,0 +1,39 @@ +{{- if .Values.load.enabled }} +apiVersion: batch/v1 +kind: Job +metadata: + name: load-{{ .Values.load.run }} +spec: + backoffLimit: 0 + activeDeadlineSeconds: 300 + ttlSecondsAfterFinished: 86400 + template: + metadata: + labels: + app: load + spec: + restartPolicy: Never + containers: + - name: k6 + image: grafana/k6:1.3.0 + args: [run, --quiet, /scripts/{{ .Values.load.script }}] + env: + - name: BASE_URL + value: "https://{{ .Values.ingress.host }}" + - name: TARGET_INSTANCE_ID + value: {{ .Values.load.target | default "" | quote }} + - name: DURATION + value: {{ .Values.load.duration | quote }} + - name: VUS + value: {{ .Values.load.vus | quote }} + resources: + requests: {cpu: 250m, memory: 128Mi} + limits: {cpu: '2', memory: 256Mi} + volumeMounts: + - name: scripts + mountPath: /scripts + volumes: + - name: scripts + configMap: + name: load-scripts +{{- end }} diff --git a/charts/messenger/templates/migrate.yaml b/charts/messenger/templates/migrate.yaml new file mode 100644 index 0000000..db5f372 --- /dev/null +++ b/charts/messenger/templates/migrate.yaml @@ -0,0 +1,32 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: migrate-{{ .Release.Revision }} +spec: + activeDeadlineSeconds: 180 + backoffLimit: 3 + ttlSecondsAfterFinished: 86400 + template: + metadata: + labels: + app: migrate + spec: + restartPolicy: OnFailure + imagePullSecrets: + - name: {{ .Values.imagePullSecret }} + containers: + - name: migrate + image: "{{ .Values.image.repository }}@{{ .Values.image.digest }}" + args: [migrate] + envFrom: + - configMapRef: + name: messenger-config + env: + - name: DATABASE_URL + valueFrom: + secretKeyRef: + name: {{ .Values.databaseSecret }} + key: database-url + resources: + requests: {cpu: 50m, memory: 32Mi} + limits: {cpu: 500m, memory: 128Mi} diff --git a/charts/messenger/templates/monitoring.yaml b/charts/messenger/templates/monitoring.yaml new file mode 100644 index 0000000..57db5d5 --- /dev/null +++ b/charts/messenger/templates/monitoring.yaml @@ -0,0 +1,19 @@ +{{- if .Values.monitoring.serviceMonitor }} +apiVersion: monitoring.coreos.com/v1 +kind: ServiceMonitor +metadata: + name: api + labels: + app: api +spec: + namespaceSelector: + matchNames: [{{ .Release.Namespace }}] + selector: + matchLabels: + app: api + endpoints: + - port: http + path: /metrics + interval: 15s + scrapeTimeout: 3s +{{- end }} diff --git a/charts/messenger/templates/network-policy.yaml b/charts/messenger/templates/network-policy.yaml new file mode 100644 index 0000000..3ad21b2 --- /dev/null +++ b/charts/messenger/templates/network-policy.yaml @@ -0,0 +1,54 @@ +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: private-databases + namespace: {{ .Release.Namespace }} +spec: + podSelector: + matchExpressions: + - key: app + operator: In + values: [postgres, valkey] + policyTypes: [Ingress] + ingress: + - from: + - podSelector: + matchLabels: + app: api + ports: + - protocol: TCP + port: 5432 + - protocol: TCP + port: 6379 + - from: + - podSelector: + matchLabels: + app: migrate + ports: + - protocol: TCP + port: 5432 +--- +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: private-api +spec: + podSelector: + matchLabels: + app: api + policyTypes: [Ingress] + ingress: + - from: + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: traefik + ports: + - protocol: TCP + port: 8080 + - from: + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: monitoring + ports: + - protocol: TCP + port: 8080 diff --git a/charts/messenger/templates/postgres.yaml b/charts/messenger/templates/postgres.yaml new file mode 100644 index 0000000..8d8704a --- /dev/null +++ b/charts/messenger/templates/postgres.yaml @@ -0,0 +1,66 @@ +apiVersion: v1 +kind: Service +metadata: + name: postgres + namespace: {{ .Release.Namespace }} +spec: + selector: + app: postgres + ports: + - port: 5432 + targetPort: 5432 +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: postgres + namespace: {{ .Release.Namespace }} +spec: + serviceName: postgres + replicas: 1 + selector: + matchLabels: + app: postgres + template: + metadata: + labels: + app: postgres + spec: + terminationGracePeriodSeconds: 30 + containers: + - name: postgres + image: {{ .Values.postgres.image | quote }} + ports: + - containerPort: 5432 + env: + - name: POSTGRES_DB + value: messenger + - name: POSTGRES_USER + value: messenger + - name: POSTGRES_PASSWORD + valueFrom: + secretKeyRef: + name: {{ .Values.databaseSecret }} + key: postgres-password + - name: PGDATA + value: /var/lib/postgresql/data/pgdata + resources: + {{- toYaml .Values.postgres.resources | nindent 12 }} + readinessProbe: + exec: + command: [pg_isready, -U, messenger, -d, messenger] + initialDelaySeconds: 3 + volumeMounts: + - name: data + mountPath: /var/lib/postgresql/data + volumeClaimTemplates: + - metadata: + name: data + spec: +{{- if .Values.storageClass }} + storageClassName: {{ .Values.storageClass | quote }} +{{- end }} + accessModes: [ReadWriteOnce] + resources: + requests: + storage: {{ .Values.postgres.storage }} diff --git a/charts/messenger/templates/s3.yaml b/charts/messenger/templates/s3.yaml new file mode 100644 index 0000000..531fb87 --- /dev/null +++ b/charts/messenger/templates/s3.yaml @@ -0,0 +1,97 @@ +apiVersion: v1 +kind: Service +metadata: + name: s3 +spec: + selector: {app: s3} + ports: + - {name: s3, port: 9000, targetPort: 9000} +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: s3 +spec: + serviceName: s3 + replicas: 1 + selector: + matchLabels: {app: s3} + template: + metadata: + labels: {app: s3} + spec: + securityContext: + runAsUser: 1000 + runAsNonRoot: true + fsGroup: 1000 + containers: + - name: s3 + image: {{ .Values.s3.image }} + args: [server, -dir=/data, -s3, -s3.port=9000, -s3.config=/identity/identities.json, -master.volumeSizeLimitMB=100, -volume.max=5] + ports: [{containerPort: 9000}] + readinessProbe: + tcpSocket: {port: 9000} + resources: + requests: {cpu: 100m, memory: 256Mi} + limits: {cpu: '1', memory: 512Mi} + securityContext: + allowPrivilegeEscalation: false + capabilities: {drop: [ALL]} + volumeMounts: + - {name: objects, mountPath: /data} + - {name: identity, mountPath: /identity, readOnly: true} + volumes: + - name: identity + secret: {secretName: {{ .Values.s3.secret }}, items: [{key: identities.json, path: identities.json}]} + volumeClaimTemplates: + - metadata: {name: objects} + spec: + accessModes: [ReadWriteOnce] + resources: + requests: {storage: {{ .Values.s3.storage }}} +--- +apiVersion: batch/v1 +kind: Job +metadata: + name: s3-bootstrap-{{ .Release.Revision }} +spec: + ttlSecondsAfterFinished: 3600 + backoffLimit: 4 + template: + metadata: + labels: {app: s3-bootstrap} + spec: + restartPolicy: OnFailure + imagePullSecrets: [{name: {{ .Values.imagePullSecret }}}] + containers: + - name: bootstrap + image: "{{ .Values.image.repository }}@{{ .Values.image.digest }}" + args: [s3-bootstrap] + env: + - {name: S3_ENDPOINT, value: 's3:9000'} + - {name: S3_TLS, value: 'false'} + - {name: S3_BUCKET, value: {{ .Values.s3.bucket }}} + - name: S3_ACCESS_KEY + valueFrom: + secretKeyRef: {name: {{ .Values.s3.secret }}, key: root-user} + - name: S3_SECRET_KEY + valueFrom: + secretKeyRef: {name: {{ .Values.s3.secret }}, key: root-password} + resources: + requests: {cpu: 50m, memory: 64Mi} + limits: {cpu: 200m, memory: 128Mi} +--- +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: private-s3 +spec: + podSelector: + matchLabels: {app: s3} + policyTypes: [Ingress] + ingress: + - from: + - podSelector: + matchExpressions: + - {key: app, operator: In, values: [api, s3-bootstrap, reconcile]} + ports: [{protocol: TCP, port: 9000}] diff --git a/charts/messenger/templates/valkey.yaml b/charts/messenger/templates/valkey.yaml new file mode 100644 index 0000000..5c996cc --- /dev/null +++ b/charts/messenger/templates/valkey.yaml @@ -0,0 +1,57 @@ +apiVersion: v1 +kind: Service +metadata: + name: valkey + namespace: {{ .Release.Namespace }} +spec: + clusterIP: None + selector: + app: valkey + ports: + - port: 6379 + targetPort: 6379 +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: valkey + namespace: {{ .Release.Namespace }} +spec: + serviceName: valkey + replicas: {{ .Values.valkey.shards }} + podManagementPolicy: Parallel + selector: + matchLabels: + app: valkey + template: + metadata: + labels: + app: valkey + spec: + terminationGracePeriodSeconds: 30 + containers: + - name: valkey + image: {{ .Values.valkey.image | quote }} + args: [valkey-server, --appendonly, 'yes', --appendfsync, always, --maxmemory, {{ .Values.valkey.maxmemory | quote }}, --maxmemory-policy, noeviction] + ports: + - containerPort: 6379 + resources: + {{- toYaml .Values.valkey.resources | nindent 12 }} + readinessProbe: + exec: + command: [valkey-cli, ping] + initialDelaySeconds: 2 + volumeMounts: + - name: data + mountPath: /data + volumeClaimTemplates: + - metadata: + name: data + spec: +{{- if .Values.storageClass }} + storageClassName: {{ .Values.storageClass | quote }} +{{- end }} + accessModes: [ReadWriteOnce] + resources: + requests: + storage: {{ .Values.valkey.storage }} diff --git a/charts/messenger/values.schema.json b/charts/messenger/values.schema.json new file mode 100644 index 0000000..3640ffe --- /dev/null +++ b/charts/messenger/values.schema.json @@ -0,0 +1,9 @@ +{ + "$schema": "https://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "valkey": {"type": "object", "properties": {"shards": {"type": "integer", "minimum": 2, "maximum": 3}}}, + "api": {"type": "object", "properties": {"replicas": {"type": "integer", "minimum": 0}}}, + "image": {"type": "object", "properties": {"digest": {"type": "string", "pattern": "^sha256:[a-f0-9]{64}$"}}} + } +} diff --git a/charts/messenger/values.yaml b/charts/messenger/values.yaml new file mode 100644 index 0000000..a91002c --- /dev/null +++ b/charts/messenger/values.yaml @@ -0,0 +1,64 @@ +# Existing lab2 release; shared persistent stores and credentials. +image: + repository: asisti.registry.twcstorage.ru/mtusi2026lab4 + digest: sha256:36d4ccb6d3c44e7a2540187a628d148e8c1601cdc3215eb81fc37edf48f5c2ce +imagePullSecret: registry-pull +databaseSecret: messenger-secrets +api: + readiness: + timeoutSeconds: 2 + failureThreshold: 2 + replicas: 2 + resources: + requests: {cpu: 250m, memory: 128Mi} + limits: {cpu: '1', memory: 768Mi} + env: + SESSION_TTL_SECONDS: '3600' + DEPENDENCY_TIMEOUT_MS: '2000' + DEPENDENCY_POOL_SIZE: '8' + REQUEST_TIMEOUT_SECONDS: '4' + SHUTDOWN_TIMEOUT_SECONDS: '5' + GOMEMLIMIT: 640MiB + MAX_CONNECTIONS: '16' + MAX_BODY_BYTES: '10485760' +postgres: + image: postgres:17.6-alpine + storage: 2Gi + resources: + requests: {cpu: 250m, memory: 256Mi} + limits: {cpu: '1', memory: 512Mi} +valkey: + image: valkey/valkey:8.1.3-alpine + shards: 3 + storage: 1Gi + maxmemory: 64mb + resources: + requests: {cpu: 100m, memory: 96Mi} + limits: {cpu: 500m, memory: 128Mi} +# Пустое значение использует default StorageClass. Задать до первой установки. +storageClass: '' + +autoscaling: + enabled: true + minReplicas: 2 + maxReplicas: 4 + cpuPercent: 60 +monitoring: + serviceMonitor: false +load: + enabled: false + run: manual + script: scaling.js + duration: 30 + vus: 32 + +ingress: + host: hl.agapov.space + className: traefik + clusterIssuer: letsencrypt-prod + +s3: + image: chrislusf/seaweedfs:3.85@sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a + secret: messenger-s3 + bucket: attachments + storage: 5Gi diff --git a/cmd/server/app.go b/cmd/server/app.go new file mode 100644 index 0000000..7bd030b --- /dev/null +++ b/cmd/server/app.go @@ -0,0 +1,105 @@ +package main + +import ( + "time" + + "go.uber.org/fx" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/admission" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/controller" + fiberctrl "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/controller/http/fiber" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/password" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service/production" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/postgres" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/valkey" +) + +const applicationStopTimeout = 9 * time.Second + +// CreateApp defines the dependency graph and Fx lifecycle for the lab2 server. +// Constructors do not open sockets; controller.RunControllerFx registers startup +// and shutdown hooks. The outer stop deadline bounds all component cleanup. +func CreateApp() fx.Option { + return fx.Options( + fx.StopTimeout(applicationStopTimeout), + fx.Supply(fx.Annotate(password.Hasher{}, fx.As(new(service.Passwords)))), + fx.Provide( + config.New, + config.NewPersistence, + config.NewObjectStore, + newObjects, + newMedia, + newDatabase, + newValkey, + newAuthentication, + fx.Annotate(newMediaReadiness, fx.As(new(fiberctrl.Readiness))), + httpConfig, + + loggerConfig, + newLogger, + fx.Annotate(newAdmissionLimiter, fx.As(new(admission.Distributed))), + fiberctrl.NewErrorRegistry, + fx.Annotate(databaseStore, fx.As(new(store.Interface))), + fx.Annotate(databaseAccounts, fx.As(new(service.Accounts))), + fx.Annotate(profileCache, fx.As(new(service.ProfileCache))), + fx.Annotate(newCursor, fx.As(new(service.CursorCodec))), + fx.Annotate(production.NewCached, fx.As(new(service.Service))), + fx.Annotate(newHTTPController, fx.As(new(controller.Controller))), + ), + fx.WithLogger(newFxLogger), + fx.Invoke(controller.RunControllerFx), + ) +} + +// httpConfig provides the HTTP configuration section to Fx. +func httpConfig(cfg *config.Config) *config.Controller { + return cfg.HTTP +} + +// loggerConfig provides the logging configuration section to Fx. +func loggerConfig(cfg *config.Config) *config.Logger { + return cfg.Logger +} + +// newAdmissionLimiter translates configured bounds into separate shared policy budgets. +func newAdmissionLimiter(cfg *config.Controller, client *valkey.Client) (*valkey.Limiter, error) { + global := valkey.Budget{Rate: cfg.Admission.RequestsPerSecond, Slots: cfg.Admission.MaxInFlight} + return valkey.NewLimiter(client, valkey.Limits{ + Global: global, + Groups: map[string]valkey.Budget{ + "auth": {Rate: cfg.Admission.AuthRate, Slots: cfg.Admission.AuthSlots}, + "read": global, + "write": global, + }, + User: valkey.Budget{Rate: cfg.Admission.UserRate, Slots: cfg.Admission.UserSlots}, + UserGroups: map[string]valkey.Budget{ + "auth": {Rate: cfg.Admission.UserAuthRate, Slots: cfg.Admission.UserAuthSlots}, + "read": {Rate: cfg.Admission.UserRate, Slots: cfg.Admission.UserSlots}, + "write": {Rate: cfg.Admission.UserWriteRate, Slots: cfg.Admission.UserWriteSlots}, + }, + }) +} + +// databaseStore exposes business persistence through its consumer interface. +func databaseStore(database *postgres.Store) store.Interface { + return database +} + +// databaseAccounts exposes account and session persistence through its consumer interface. +func databaseAccounts(database *postgres.Store) service.Accounts { + return database +} + +// profileCache exposes immutable profiles without leaking the Valkey client. +func profileCache(client *valkey.Client) service.ProfileCache { + return client +} + +// newCursor binds the shared signing secret at the composition root. +func newCursor(cfg *config.Persistence) (*cursor.Codec, error) { + return cursor.NewShared(cfg.CursorKey) +} diff --git a/cmd/server/app_test.go b/cmd/server/app_test.go new file mode 100644 index 0000000..f4a692e --- /dev/null +++ b/cmd/server/app_test.go @@ -0,0 +1,108 @@ +package main + +import ( + "context" + "net" + "os" + "testing" + "time" + + "go.uber.org/fx" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" +) + +// TestDependencyGraph ensures constructor signatures and interface bindings agree. +func TestDependencyGraph(t *testing.T) { + if err := fx.ValidateApp(CreateApp(), fx.NopLogger); err != nil { + t.Fatal(err) + } +} + +// TestInvalidConfigurationStopsStartup checks failure before a listener is acquired. +func TestInvalidConfigurationStopsStartup(t *testing.T) { + t.Setenv("APP_PORT", "0") + app := fx.New(CreateApp(), fx.NopLogger) + if err := app.Start(context.Background()); err == nil { + t.Fatal("invalid listener configuration accepted") + } +} + +// TestCommandValidation ensures unsupported arguments cannot accidentally start a server. +func TestCommandValidation(t *testing.T) { + if err := run([]string{"unknown"}); err == nil { + t.Fatal("unknown command accepted") + } +} + +// TestLoggerConfiguration checks that process logging uses its own validated section. +func TestLoggerConfiguration(t *testing.T) { + if _, err := newLogger(&config.Logger{Level: "invalid"}); err == nil { + t.Fatal("invalid logger configuration accepted") + } +} + +// TestApplicationLifecycle starts the complete Fx graph and drains its real HTTP listener. +func TestApplicationLifecycle(t *testing.T) { + requirePersistence(t) + listener, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + cfg := config.Default() + cfg.HTTP.Host = "127.0.0.1" + cfg.HTTP.Port = listener.Addr().(*net.TCPAddr).Port + cfg.Logger.Level = "error" + if err := listener.Close(); err != nil { + t.Fatal(err) + } + app := fx.New(CreateApp(), fx.Replace(cfg)) + ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) + defer cancel() + if err := app.Start(ctx); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + stopCtx, stopCancel := context.WithTimeout(context.Background(), 5*time.Second) + defer stopCancel() + if err := app.Stop(stopCtx); err != nil { + t.Error(err) + } + }) + if err := checkReadiness(cfg.HTTP); err != nil { + t.Fatal(err) + } +} + +// TestOccupiedListenerFailsStartup ensures bind failures propagate through the Fx lifecycle. +func TestOccupiedListenerFailsStartup(t *testing.T) { + requirePersistence(t) + listener, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer func() { + _ = listener.Close() + }() + cfg := config.Default() + cfg.HTTP.Host = "127.0.0.1" + cfg.HTTP.Port = listener.Addr().(*net.TCPAddr).Port + cfg.Logger.Level = "error" + app := fx.New(CreateApp(), fx.Replace(cfg), fx.NopLogger) + if err := app.Start(t.Context()); err == nil { + _ = app.Stop(t.Context()) + t.Fatal("startup succeeded on an occupied listener") + } +} + +// requirePersistence prevents infrastructure scenarios from silently passing on configuration failure. +func requirePersistence(t *testing.T) { + t.Helper() + if os.Getenv("DATABASE_URL") == "" || os.Getenv("VALKEY_SHARDS") == "" { + t.Skip("DATABASE_URL and VALKEY_SHARDS are required for real application lifecycle tests") + } + t.Setenv("CURSOR_SIGNING_KEY", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=") + if err := migrate(); err != nil { + t.Fatal(err) + } +} diff --git a/cmd/server/commands.go b/cmd/server/commands.go new file mode 100644 index 0000000..e70ac05 --- /dev/null +++ b/cmd/server/commands.go @@ -0,0 +1,50 @@ +package main + +import ( + "context" + "fmt" + "time" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/postgres" + + "go.uber.org/fx" +) + +// run dispatches the server command or the container healthcheck. +func run(args []string) error { + if len(args) == 1 && args[0] == "s3-bootstrap" { + return bootstrapS3() + } + if len(args) == 0 { + fx.New(CreateApp()).Run() + return nil + } + if len(args) == 1 && args[0] == "reconcile" { + return reconcile(false) + } + if len(args) == 2 && args[0] == "reconcile" && args[1] == "--apply" { + return reconcile(true) + } + if len(args) == 1 && args[0] == "migrate" { + return migrate() + } + if len(args) == 1 && args[0] == "healthcheck" { + return healthcheck() + } + return fmt.Errorf("usage: server [healthcheck|migrate]") +} + +// migrate applies versioned DDL once from an operator job, independently of the API. +func migrate() error { + cfg, err := config.NewPersistence() + if err != nil { + return err + } + database, err := postgres.New(postgres.Options{URL: cfg.DatabaseURL, MaxConnections: 1, Timeout: 30 * time.Second}) + if err != nil { + return err + } + defer database.Close() + return database.Start(context.Background()) +} diff --git a/cmd/server/healthcheck.go b/cmd/server/healthcheck.go new file mode 100644 index 0000000..4919fa5 --- /dev/null +++ b/cmd/server/healthcheck.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "net" + "net/http" + "strconv" + "time" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" +) + +const readinessProbeTimeout = 2 * time.Second + +// healthcheck loads configuration and runs the container readiness probe. +func healthcheck() error { + cfg, err := config.New() + if err != nil { + return err + } + return checkReadiness(cfg.HTTP) +} + +// checkReadiness checks the local readiness endpoint within the probe timeout. +func checkReadiness(cfg *config.Controller) error { + host := cfg.Host + if host == "0.0.0.0" { + host = "127.0.0.1" + } + if host == "::" { + host = "::1" + } + address := "http://" + net.JoinHostPort(host, strconv.Itoa(cfg.Port)) + "/health/ready" + client := http.Client{Timeout: readinessProbeTimeout} + response, err := client.Get(address) + if err != nil { + return fmt.Errorf("readiness probe: %w", err) + } + defer func() { + _ = response.Body.Close() + }() + if response.StatusCode != http.StatusOK { + return fmt.Errorf("not ready: %d", response.StatusCode) + } + return nil +} diff --git a/cmd/server/healthcheck_test.go b/cmd/server/healthcheck_test.go new file mode 100644 index 0000000..eb24696 --- /dev/null +++ b/cmd/server/healthcheck_test.go @@ -0,0 +1,111 @@ +package main + +import ( + "errors" + "net" + "net/http" + "net/http/httptest" + "net/url" + "strconv" + "testing" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" +) + +// TestRun_Healthcheck verifies command routing to the readiness endpoint. +func TestRun_Healthcheck(t *testing.T) { + // Arrange. + requests := make(chan string, 1) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests <- r.Method + " " + r.URL.Path + w.WriteHeader(http.StatusOK) + })) + t.Cleanup(server.Close) + address := server.Listener.Addr().(*net.TCPAddr) + t.Setenv("APP_HOST", "0.0.0.0") + t.Setenv("APP_PORT", strconv.Itoa(address.Port)) + + // Act. + err := run([]string{"healthcheck"}) + + // Assert. + if err != nil { + t.Fatal(err) + } + select { + case got := <-requests: + if got != "GET /health/ready" { + t.Fatalf("probe=%q", got) + } + default: + t.Fatal("command returned success without probing readiness") + } +} + +// TestHealthcheck_InvalidConfig rejects invalid startup settings before attempting HTTP. +func TestHealthcheck_InvalidConfig(t *testing.T) { + // Arrange. + t.Setenv("APP_PORT", "invalid") + + // Act. + err := healthcheck() + + // Assert. + if err == nil { + t.Fatal("invalid port accepted") + } +} + +// TestCheckReadiness distinguishes unhealthy responses from connection failures. +func TestCheckReadiness(t *testing.T) { + t.Run("Negative: unhealthy response", func(t *testing.T) { + // Arrange. + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusServiceUnavailable) + })) + t.Cleanup(server.Close) + address := server.Listener.Addr().(*net.TCPAddr) + cfg := config.Default().HTTP + cfg.Host = "127.0.0.1" + cfg.Port = address.Port + + // Act. + err := checkReadiness(cfg) + + // Assert. + if err == nil { + t.Fatalf("probe accepted %s", "Negative: unhealthy response") + } + if err.Error() != "not ready: 503" { + t.Fatalf("unexpected health failure: %v", err) + } + + }) + + t.Run("Negative: server unavailable", func(t *testing.T) { + // Arrange. + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusServiceUnavailable) + })) + t.Cleanup(server.Close) + address := server.Listener.Addr().(*net.TCPAddr) + cfg := config.Default().HTTP + cfg.Host = "127.0.0.1" + cfg.Port = address.Port + + server.Close() + + // Act. + err := checkReadiness(cfg) + + // Assert. + if err == nil { + t.Fatalf("probe accepted %s", "Negative: server unavailable") + } + + if _, ok := errors.AsType[*url.Error](err); !ok { + t.Fatalf("connection failure was not preserved: %v", err) + } + + }) +} diff --git a/cmd/server/logger.go b/cmd/server/logger.go new file mode 100644 index 0000000..b24dda2 --- /dev/null +++ b/cmd/server/logger.go @@ -0,0 +1,29 @@ +package main + +import ( + "go.uber.org/fx/fxevent" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" +) + +// newLogger creates a structured logger with the configured severity threshold. +func newLogger(cfg *config.Logger) (*zap.Logger, error) { + if err := cfg.Validate(); err != nil { + return nil, err + } + level, err := zapcore.ParseLevel(cfg.Level) + if err != nil { + return nil, err + } + settings := zap.NewProductionConfig() + settings.Level = zap.NewAtomicLevelAt(level) + settings.DisableStacktrace = true + return settings.Build() +} + +// newFxLogger adapts the application logger for Fx lifecycle events. +func newFxLogger(log *zap.Logger) fxevent.Logger { + return &fxevent.ZapLogger{Logger: log.Named("fx")} +} diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..1d1fbd5 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,15 @@ +// Command server runs the messenger or its container readiness probe. +package main + +import ( + "fmt" + "os" +) + +// main runs the selected command and reports failures to stderr. +func main() { + if err := run(os.Args[1:]); err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} diff --git a/cmd/server/media.go b/cmd/server/media.go new file mode 100644 index 0000000..d9f7f31 --- /dev/null +++ b/cmd/server/media.go @@ -0,0 +1,39 @@ +package main + +import ( + "go.lab260.ru/fielderr" + "go.uber.org/zap" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/admission" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + fiberctrl "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/controller/http/fiber" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/media" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/objectstore" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/postgres" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/valkey" +) + +// newObjects binds the S3 adapter at the composition root. +func newObjects(cfg *config.ObjectStore) (*objectstore.Client, error) { + return objectstore.New(cfg) +} + +// newMedia injects infrastructure through the media service's consumer interfaces. +func newMedia(database *postgres.Store, objects *objectstore.Client, chats service.Service) (*media.Service, error) { + return media.New(database, objects, chats) +} + +// newHTTPController connects the independently tested transport and attachment service. +func newHTTPController(log *zap.Logger, cfg *config.Controller, svc service.Service, registry *fielderr.Registry, limiter admission.Distributed, auth service.Authentication, readiness fiberctrl.Readiness, attachments *media.Service) (*fiberctrl.Controller, error) { + ctrl, err := fiberctrl.New(log, cfg, svc, registry, limiter, auth, readiness) + if err != nil { + return nil, err + } + return ctrl.WithAttachments(attachments), nil +} + +// newMediaReadiness includes private originals in the mandatory synchronous dependency path. +func newMediaReadiness(database *postgres.Store, cache *valkey.Client, objects *objectstore.Client) *dependencyReadiness { + return &dependencyReadiness{database: database, cache: cache, objects: objects} +} diff --git a/cmd/server/media_test.go b/cmd/server/media_test.go new file mode 100644 index 0000000..7bd3a4c --- /dev/null +++ b/cmd/server/media_test.go @@ -0,0 +1,88 @@ +package main + +import ( + "os" + "testing" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" +) + +// TestMediaHTTPConfigurationFailure rejects invalid listener settings before serving files. +func TestMediaHTTPConfigurationFailure(t *testing.T) { + if _, err := newHTTPController(nil, &config.Controller{}, nil, nil, nil, nil, nil, nil); err == nil { + t.Fatal("invalid HTTP composition accepted") + } +} + +// TestReconcileConfigurationFailures checks operator commands fail closed before side effects. +func TestReconcileConfigurationFailures(t *testing.T) { + cases := []struct { + name string + key string + value string + }{ + {"database missing", "DATABASE_URL", ""}, + {"s3 missing", "S3_ENDPOINT", ""}, + {"database malformed", "DATABASE_URL", "://invalid"}, + {"s3 malformed", "S3_ENDPOINT", "http://invalid/path"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Setenv("DATABASE_URL", "postgres://localhost/test") + t.Setenv("VALKEY_SHARDS", "shard0=localhost:6379") + t.Setenv("S3_ENDPOINT", "localhost:9000") + t.Setenv("S3_BUCKET", "attachments") + t.Setenv("S3_ACCESS_KEY", "test") + t.Setenv("S3_SECRET_KEY", "synthetic") + t.Setenv(tc.key, tc.value) + if err := run([]string{"reconcile"}); err == nil { + t.Fatal("invalid reconciliation configuration accepted") + } + }) + } +} + +// TestReconcileRealInventory verifies the operator command can scan both real dependencies. +func TestReconcileRealInventory(t *testing.T) { + requirePersistence(t) + if os.Getenv("S3_ENDPOINT") == "" { + t.Skip("isolated S3 required") + } + if err := run([]string{"reconcile"}); err != nil { + t.Fatal(err) + } + if err := run([]string{"reconcile", "--apply"}); err != nil { + t.Fatal(err) + } +} + +// TestBootstrapConfigurationFailures rejects missing credentials and malformed endpoints. +func TestBootstrapConfigurationFailures(t *testing.T) { + t.Run("missing", func(t *testing.T) { + t.Setenv("S3_ENDPOINT", "") + if err := run([]string{"s3-bootstrap"}); err == nil { + t.Fatal("missing config accepted") + } + }) + t.Run("malformed", func(t *testing.T) { + t.Setenv("S3_ENDPOINT", "http://invalid/path") + t.Setenv("S3_BUCKET", "attachments") + t.Setenv("S3_ACCESS_KEY", "test") + t.Setenv("S3_SECRET_KEY", "synthetic") + if err := run([]string{"s3-bootstrap"}); err == nil { + t.Fatal("malformed endpoint accepted") + } + }) +} + +// TestBootstrapExistingBucket verifies repeatable operator execution against the isolated bucket. +func TestBootstrapExistingBucket(t *testing.T) { + if os.Getenv("S3_ENDPOINT") == "" || os.Getenv("S3_ADMIN_SECRET") == "" { + t.Skip("isolated S3 operator credentials required") + } + t.Setenv("S3_ACCESS_KEY", "operator") + t.Setenv("S3_SECRET_KEY", os.Getenv("S3_ADMIN_SECRET")) + if err := run([]string{"s3-bootstrap"}); err != nil { + t.Fatal(err) + } +} diff --git a/cmd/server/persistence.go b/cmd/server/persistence.go new file mode 100644 index 0000000..b746a70 --- /dev/null +++ b/cmd/server/persistence.go @@ -0,0 +1,85 @@ +package main + +import ( + "context" + "time" + + "go.uber.org/fx" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + fiberctrl "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/controller/http/fiber" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service/authentication" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/postgres" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/valkey" +) + +type dependencyReadiness struct { + database fiberctrl.Readiness + cache fiberctrl.Readiness + objects fiberctrl.Readiness +} + +// Ping checks persistence and all limiter shards under a shared deadline. +func (d *dependencyReadiness) Ping(ctx context.Context) error { + if err := d.database.Ping(ctx); err != nil { + return err + } + if err := d.cache.Ping(ctx); err != nil { + return err + } + if d.objects != nil { + return d.objects.Ping(ctx) + } + return nil +} + +// newDatabase creates the pool and registers its migration/start and shutdown hooks. +func newDatabase(lifecycle fx.Lifecycle, cfg *config.Persistence) (*postgres.Store, error) { + database, err := postgres.New(postgres.Options{ + URL: cfg.DatabaseURL, + MaxConnections: int32(cfg.PoolSize), + Timeout: time.Duration(cfg.TimeoutMilliseconds) * time.Millisecond, + }) + if err != nil { + return nil, err + } + lifecycle.Append(fx.Hook{ + OnStart: database.CheckSchema, + OnStop: func(context.Context) error { + database.Close() + return nil + }, + }) + return database, nil +} + +// newValkey creates bounded clients and registers connectivity and cleanup hooks. +func newValkey(lifecycle fx.Lifecycle, cfg *config.Persistence) (*valkey.Client, error) { + client, err := valkey.New(valkey.Options{ + Nodes: cfg.Shards, + Coordinator: cfg.Coordinator, + PoolSize: cfg.PoolSize, + Timeout: time.Duration(cfg.TimeoutMilliseconds) * time.Millisecond, + }) + if err != nil { + return nil, err + } + lifecycle.Append(fx.Hook{ + OnStart: client.Ping, + OnStop: func(context.Context) error { + return client.Close() + }, + }) + return client, nil +} + +// newAuthentication binds credentials and KDF implementations to authentication policy. +func newAuthentication(accounts service.Accounts, passwords service.Passwords, cfg *config.Persistence) (service.Authentication, error) { + return authentication.New(accounts, passwords, time.Duration(cfg.SessionTTLSeconds)*time.Second) +} + +// newReadiness assembles infrastructure checks at the composition boundary. +func newReadiness(database *postgres.Store, cache *valkey.Client) *dependencyReadiness { + return &dependencyReadiness{database: database, cache: cache} +} diff --git a/cmd/server/persistence_test.go b/cmd/server/persistence_test.go new file mode 100644 index 0000000..b02d749 --- /dev/null +++ b/cmd/server/persistence_test.go @@ -0,0 +1,66 @@ +package main + +import ( + "testing" + "time" + + "go.uber.org/fx" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/postgres" +) + +// TestDatabaseProviderRejectsInvalidConfiguration verifies dependency errors propagate before startup. +func TestDatabaseProviderRejectsInvalidConfiguration(t *testing.T) { + var lifecycle fx.Lifecycle + app := fx.New(fx.NopLogger, fx.Populate(&lifecycle)) + if err := app.Err(); err != nil { + t.Fatal(err) + } + if _, err := newDatabase(lifecycle, &config.Persistence{}); err == nil { + t.Fatal("invalid database settings accepted") + } +} + +// TestValkeyProviderRejectsInvalidConfiguration verifies the cache constructor is a real boundary. +func TestValkeyProviderRejectsInvalidConfiguration(t *testing.T) { + var lifecycle fx.Lifecycle + app := fx.New(fx.NopLogger, fx.Populate(&lifecycle)) + if err := app.Err(); err != nil { + t.Fatal(err) + } + if _, err := newValkey(lifecycle, &config.Persistence{}); err == nil { + t.Fatal("invalid cache settings accepted") + } +} + +// TestReadinessStopsAtUnavailableDatabase checks that mandatory persistence failure is visible. +func TestReadinessStopsAtUnavailableDatabase(t *testing.T) { + database, err := postgres.New(postgres.Options{URL: "postgres://localhost/test", MaxConnections: 1, Timeout: time.Second}) + if err != nil { + t.Fatal(err) + } + database.Close() + readiness := newReadiness(database, nil) + if err := readiness.Ping(t.Context()); err == nil { + t.Fatal("closed database reported ready") + } +} + +// TestMigrationCommandRejectsMissingConfiguration fails before opening a database. +func TestMigrationCommandRejectsMissingConfiguration(t *testing.T) { + t.Setenv("DATABASE_URL", "") + t.Setenv("VALKEY_SHARDS", "shard0=localhost:6379") + if err := run([]string{"migrate"}); err == nil { + t.Fatal("missing database configuration accepted") + } +} + +// TestMigrationCommandRejectsMalformedDatabaseURL fails without logging credentials. +func TestMigrationCommandRejectsMalformedDatabaseURL(t *testing.T) { + t.Setenv("DATABASE_URL", "://invalid") + t.Setenv("VALKEY_SHARDS", "shard0=localhost:6379") + if err := run([]string{"migrate"}); err == nil { + t.Fatal("malformed database configuration accepted") + } +} diff --git a/cmd/server/reconcile.go b/cmd/server/reconcile.go new file mode 100644 index 0000000..db529ec --- /dev/null +++ b/cmd/server/reconcile.go @@ -0,0 +1,37 @@ +package main + +import ( + "context" + + "os" + "time" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/media" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/objectstore" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/postgres" +) + +// reconcile runs a bounded dry-run scan unless the operator explicitly supplies apply. +func reconcile(apply bool) error { + cfg, err := config.NewPersistence() + if err != nil { + return err + } + s3, err := config.NewObjectStore() + if err != nil { + return err + } + database, err := postgres.New(postgres.Options{URL: cfg.DatabaseURL, MaxConnections: 1, Timeout: 5 * time.Second}) + if err != nil { + return err + } + defer database.Close() + objects, err := objectstore.New(s3) + if err != nil { + return err + } + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) + defer cancel() + return media.RunReconciliation(ctx, database, objects, os.Stdout, apply) +} diff --git a/cmd/server/s3_bootstrap.go b/cmd/server/s3_bootstrap.go new file mode 100644 index 0000000..21b6f7a --- /dev/null +++ b/cmd/server/s3_bootstrap.go @@ -0,0 +1,24 @@ +package main + +import ( + "context" + "time" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/objectstore" +) + +// bootstrapS3 creates the private bucket using separate operator credentials. +func bootstrapS3() error { + cfg, err := config.NewObjectStore() + if err != nil { + return err + } + objects, err := objectstore.New(cfg) + if err != nil { + return err + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + return objects.Bootstrap(ctx) +} diff --git a/docs/PLAN.md b/docs/PLAN.md new file mode 100644 index 0000000..f022f94 --- /dev/null +++ b/docs/PLAN.md @@ -0,0 +1,8 @@ +# План lab4 + +- L4-01–04: перенос основы, S3, метаданные и HTTP — выполнено; публичный smoke и regression tests прошли. +- L4-05: reconciler, защита ссылок/свежих данных, fault injection — выполнено; измерен обход 100/1000 записей. Ограничения в REPORT. +- L4-06: race, per-file coverage ≥90%, lint, память 10 VU, реальные persistence/failover — выполнено локально. +- Образ опубликован, digest закреплён. Применение этой версии в dev не подтверждено. +- CI автоматически проверяет порог каждого файла; выполнение удалённого workflow пока не подтверждено. +- Репозиторий готов к отдельному коммиту и публикации. Описание PR — в родительском каталоге рабочего пространства. diff --git a/docs/SPEC.md b/docs/SPEC.md new file mode 100644 index 0000000..f562d08 --- /dev/null +++ b/docs/SPEC.md @@ -0,0 +1,14 @@ +# Спецификация lab4 + +Цель: весь уровень 3/4, трек 5 «Согласованность двух хранилищ». Основание: README, COURSE, contracts/openapi.json и contracts/README.md. Kubernetes/Helm сохраняется основным стендом; принятие замены Compose преподавателем отдельно не подтверждено. + +| ID | Требование | Приёмка | +| --- | --- | --- | +| L4-01 | Приватный S3, отдельные ограниченные credentials, повторяемый bootstrap | Повтор bootstrap, anonymous GET denied | +| L4-02 | Авторизованный PNG/JPEG upload 201 и byte-exact download | Публичный smoke, SHA256, failover/persistence | +| L4-03 | 10MiB/40M pixels, filename/MIME/полная декодируемость | Негативные тесты; ограниченная concurrency/RAM | +| L4-04 | До 10 уникальных вложений чата в сообщении, idempotency учитывает IDs | Чужое/другой чат 404, повтор/конфликт | +| L4-05 | Безопасная сверка orphan/missing, grace, dry-run, повтор | Fault injection S3/DB, fresh/referenced сохранены, стоимость обхода | +| L4-06 | Изолированные интеграционные тесты и ≥10 concurrent upload | Raw результаты с ревизией; coverage ≥90% каждого Go файла | + +Решение: резервирование метаданных uploading до PUT; публикация ready после PUT. Неопределённый результат записи БД не вызывает слепой DELETE объекта. Reconciler проверяет возраст и состояние под блокировкой; используемые записи/объекты не удаляет. Объекты имеют UUID key, не filename. Не переносить старые измерения как результаты lab4. diff --git a/evidence/current/attachment-tests.txt b/evidence/current/attachment-tests.txt new file mode 100644 index 0000000..f159be2 --- /dev/null +++ b/evidence/current/attachment-tests.txt @@ -0,0 +1,4 @@ +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/media 0.399s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/objectstore 0.359s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/postgres 1.626s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/controller/http/fiber 0.298s diff --git a/evidence/current/coverage-per-file.json b/evidence/current/coverage-per-file.json new file mode 100644 index 0000000..0208fdb --- /dev/null +++ b/evidence/current/coverage-per-file.json @@ -0,0 +1,91 @@ +{"threshold_per_file":90,"files":[ +{"file":"internal/store/memory/store.go","statements":3,"covered":3,"percent":100.00000000,"sha256":"23fa65457a7b5a3d456fc4b518b76eeed8d4063b3e5090f62ae8c57d276f018d"}, +{"file":"internal/service/production/members.go","statements":19,"covered":19,"percent":100.00000000,"sha256":"99a25f75b1289d812c125a2a68640869a2b879abd205c81155fae7bd96174935"}, +{"file":"internal/config/controller.go","statements":23,"covered":22,"percent":95.65217391,"sha256":"1c853c0f771e2c75958db8158797f2287e06c8d9efe8856365b7b9c71b1747b0"}, +{"file":"internal/controller/interface.go","statements":0,"covered":0,"percent":null,"sha256":"24b6b06d6fab919cd2130697b44fd0d358265184dca0ca8362e0b4fcc2ae5a68"}, +{"file":"internal/config/logger.go","statements":7,"covered":7,"percent":100.00000000,"sha256":"3a69e20a00fa2ed816f344a990e9e0ef0077e1607d4207858b4b00e4ff5d1988"}, +{"file":"internal/valkey/admission.go","statements":42,"covered":42,"percent":100.00000000,"sha256":"f729a0c695ad01f80e7450d0e802c07cfb3f35340b6ea22a20e00348a9b64371"}, +{"file":"internal/service/production/service.go","statements":10,"covered":10,"percent":100.00000000,"sha256":"6f0cf8ab6c0e07bbb82e6b289e3d734c84ead5b82932db2007ef0c54f159f4cb"}, +{"file":"internal/password/password.go","statements":18,"covered":17,"percent":94.44444444,"sha256":"fe2df6990331573bbf0202ebc81c58263a7c16a29b713eefa0f0e852e4ee4a76"}, +{"file":"internal/strictjson/decode.go","statements":47,"covered":47,"percent":100.00000000,"sha256":"d99595e1f74406c5aa617c0ec062afb7f12f116bd86776e87cc9c9a436ff7eea"}, +{"file":"internal/store/postgres/messages.go","statements":31,"covered":29,"percent":93.54838710,"sha256":"3b9f6cc9906f3007cf416bbfc01046794c652a8589c3ea43e57a03b1c8c26bc2"}, +{"file":"internal/service/cursor.go","statements":0,"covered":0,"percent":null,"sha256":"430996f921d5dab91b47a6f796e19c2b7cb31d386572b326adec581b8be9f6b7"}, +{"file":"internal/media/reconcile.go","statements":38,"covered":38,"percent":100.00000000,"sha256":"278dc4c8720348df752801adf04a30d1b7508a730665122693ce1dd7b90c376a"}, +{"file":"cmd/server/reconcile.go","statements":16,"covered":16,"percent":100.00000000,"sha256":"a88c8f29f9482564248bc43d7a6386baca64710932877e5fa7008a31a2ae4c42"}, +{"file":"internal/store/memory/users.go","statements":22,"covered":21,"percent":95.45454545,"sha256":"946a1a7a72badf6a80b9ff9a5c850914f46cf5ebde13a5f9f945514f0e188f88"}, +{"file":"internal/store/memory/capacity.go","statements":7,"covered":7,"percent":100.00000000,"sha256":"da46630a8a5a3625e40677cd4d5e268e2c320a8f1ed272c2f6c05f45cb0f458b"}, +{"file":"internal/service/production/access.go","statements":27,"covered":27,"percent":100.00000000,"sha256":"550ab142c922a201f72595ce0774a1b07062a06e5b728119ccb6d31cac5c59e6"}, +{"file":"internal/controller/http/fiber/handlers.go","statements":37,"covered":36,"percent":97.29729730,"sha256":"cfa54cff49e70a0f7c2354e60ff84d50b5a19f0d3c3ddd931cdf7872ed1c0e19"}, +{"file":"internal/controller/http/fiber/routes.go","statements":18,"covered":18,"percent":100.00000000,"sha256":"e248223517102e1eecdeba9a5e2d844c0afc14116a56e9a1d76c6afb755e63d1"}, +{"file":"internal/controller/http/fiber/mapping.go","statements":16,"covered":16,"percent":100.00000000,"sha256":"2db9a0329f722d2ec1cefc529df17875dbc7eb3e50d58319934891c1da3bc7ed"}, +{"file":"internal/controller/http/fiber/health.go","statements":7,"covered":7,"percent":100.00000000,"sha256":"1b7628b0f313089891e9bfa44fd6e0cdb753375ffd2cf22caf5abdab61f9c102"}, +{"file":"internal/media/validation.go","statements":17,"covered":17,"percent":100.00000000,"sha256":"16a99b15901009e4861cdd30a24a433d8d36782745af1212e3dffa8b0d50906a"}, +{"file":"internal/cursor/codec.go","statements":36,"covered":34,"percent":94.44444444,"sha256":"678e197744fb2fc4b51715f51593b98761125bbaddbe4cb1b7032bd6b173ef04"}, +{"file":"internal/controller/fx.go","statements":1,"covered":1,"percent":100.00000000,"sha256":"af72da25fea9811c13a50c8133e9e8b35e070c5295069c40c0ae89cdc0aff19a"}, +{"file":"internal/service/production/pages.go","statements":17,"covered":16,"percent":94.11764706,"sha256":"0eb30c852f178768e11457a29aa978127d22c4c6f0019aea004123265da5e789"}, +{"file":"internal/service/errors.go","statements":0,"covered":0,"percent":null,"sha256":"1c36cfef90643276e1c83aede658af6b9e2eb0152b7ae68c2c8abd0e7bf655cd"}, +{"file":"internal/config/objectstore.go","statements":4,"covered":4,"percent":100.00000000,"sha256":"c6283fe310747e8297850b48c0a119c1d62db4903dd2c779c6296678563df188"}, +{"file":"internal/media/service.go","statements":46,"covered":46,"percent":100.00000000,"sha256":"77f45226996a1d83f3f4ca2ac6980967d4f7cc1f17f3fd2d324ef267237523c0"}, +{"file":"internal/service/production/errors.go","statements":5,"covered":5,"percent":100.00000000,"sha256":"27abf6ee6231c9e89643b512bd5694f32872e357831ec0d6bd1302d754a5d4dd"}, +{"file":"internal/controller/http/fiber/headers.go","statements":0,"covered":0,"percent":null,"sha256":"d32a9a01a8b391f4d986e14947a25a0504f4363335ea1524df05e0dd8e9e61bd"}, +{"file":"cmd/server/commands.go","statements":22,"covered":22,"percent":100.00000000,"sha256":"1d16d1df1e0b0d59e7084e78d2e6caa0d30c3240e0bc040a7963f7f6b862c311"}, +{"file":"internal/store/memory/messages.go","statements":19,"covered":18,"percent":94.73684211,"sha256":"ca4526c55af46dc8f4205aa8402d6819e05a46840d704d8caea0a477014f70ab"}, +{"file":"internal/objectstore/client.go","statements":39,"covered":36,"percent":92.30769231,"sha256":"0e42d39cc3ca70d440fc49dddf614af8a33801ac23a954ac613d6397631a9c3c"}, +{"file":"internal/model/dto/models.go","statements":0,"covered":0,"percent":null,"sha256":"5e2536b8eba6bb92b751a7b817b6338755008857e3b53920d8da6fb67adeebdb"}, +{"file":"internal/store/postgres/idempotency.go","statements":21,"covered":19,"percent":90.47619048,"sha256":"448da4c0755f16184d9bbe6cdef35b9ea4ffcb04b7da832ff82f5c1c34ad0966"}, +{"file":"internal/store/postgres/chats.go","statements":26,"covered":24,"percent":92.30769231,"sha256":"cbd4e074fa631b4e2e6a746f01e2d634391cddfd1d4e60d58ca0182524b97eaa"}, +{"file":"internal/service/production/chats.go","statements":22,"covered":22,"percent":100.00000000,"sha256":"46f4d77cc32838c28abb57e49ebdc0d30cf36ffe49079b2f84d03c405969a346"}, +{"file":"internal/media/run_reconcile.go","statements":21,"covered":21,"percent":100.00000000,"sha256":"3255403afcb9486d33c5bb45b5afa9c3c9b85596ce97f86ac2f224011cd8a860"}, +{"file":"internal/controller/http/fiber/error_registry.go","statements":2,"covered":2,"percent":100.00000000,"sha256":"9d5da8d98ece2706e324cb2deca915e0811b6ad98f7aaac5805ca0f3cc0a1f9a"}, +{"file":"internal/controller/http/fiber/public_errors.go","statements":15,"covered":15,"percent":100.00000000,"sha256":"1fbe98b2de99a794a0f2726ad350f258004bd2890660d28b2982c72e5579f3f5"}, +{"file":"internal/controller/http/fiber/metrics.go","statements":17,"covered":17,"percent":100.00000000,"sha256":"54db82201697bdfcc65712afe61b650f8f0885fc65222dc4cc6d1c3bb07d82dd"}, +{"file":"internal/controller/http/fiber/dependencies.go","statements":0,"covered":0,"percent":null,"sha256":"6ee07f110bdb29d8d2191ab8889ba60474b33a5b225739875c8a57d8434be15d"}, +{"file":"internal/hashring/ring.go","statements":21,"covered":20,"percent":95.23809524,"sha256":"e205901cec2e66011ec764b70879b0eb0d28a78c4dea8913a9fb30c2d778fa8c"}, +{"file":"cmd/server/logger.go","statements":10,"covered":9,"percent":90.00000000,"sha256":"934f5f3cac0307dbb26b38ec232270a1100b83fe54990e16f98a1277126feb14"}, +{"file":"internal/store/memory/ordering.go","statements":6,"covered":6,"percent":100.00000000,"sha256":"4ed267c6ecd734687e5af3645d18a1a6b2608ca9bda19de3e2e0676f4fa8c416"}, +{"file":"internal/media/orphans.go","statements":23,"covered":23,"percent":100.00000000,"sha256":"327ee894c8c9afa5bcf8236ed4c91befd66699c0ca33178e9690705764faf002"}, +{"file":"internal/controller/http/fiber/decode.go","statements":12,"covered":11,"percent":91.66666667,"sha256":"0f8ac98d5ec9b606f661985515d9a13047ebd262b2c4e99f1a9d61bf8a591dfa"}, +{"file":"internal/controller/http/fiber/auth.go","statements":29,"covered":29,"percent":100.00000000,"sha256":"330913d86e6b3f1bedf14f770df7012426f86f4ae2a1cb1fddb2b4f6294a98f8"}, +{"file":"internal/service/production/messages.go","statements":18,"covered":18,"percent":100.00000000,"sha256":"76b7c6ee33d98debda99df1ab8272deed64b80aadfec3e079d9d550e0facb4a4"}, +{"file":"internal/config/environment.go","statements":14,"covered":14,"percent":100.00000000,"sha256":"027761b059b76898a84318ec1a8b43158d8bafd6e0b15b52efbe902b4d8c5ea3"}, +{"file":"cmd/server/persistence.go","statements":22,"covered":20,"percent":90.90909091,"sha256":"3302faabb63a43c83a646e3c2217af28f55707198206c80aae5f18787cb8e627"}, +{"file":"internal/service/production/validation.go","statements":13,"covered":13,"percent":100.00000000,"sha256":"423d4be24ae5ea70d47d2da5b781296e8bb3cebdea90abf31c2117056bd17bbd"}, +{"file":"internal/service/production/creation.go","statements":24,"covered":23,"percent":95.83333333,"sha256":"6de190a75ed12c6b07bcf2889a085fd8fe1025bc13981f771e0a84627c377d0d"}, +{"file":"internal/objectstore/list.go","statements":10,"covered":9,"percent":90.00000000,"sha256":"e347b48cde4d4292505dc3f2834ada4d6c8c2dd133600e281741a13f9f13b104"}, +{"file":"internal/model/http/models.go","statements":0,"covered":0,"percent":null,"sha256":"d68827b0bd3e6df4037621b6941298d3923dc348bda5bf314a34861e7c34f04e"}, +{"file":"internal/model/dto/attachment.go","statements":0,"covered":0,"percent":null,"sha256":"4a7790ef7a3cfc2d9c6f35d42873e04ea48e72a95af2dd173a6f4d2e29c51b46"}, +{"file":"cmd/server/app.go","statements":9,"covered":9,"percent":100.00000000,"sha256":"b5a69912970b440c09e1d3db9a8dcbbe21077ecf848acd389d83cc98ad5189dd"}, +{"file":"internal/valkey/cache.go","statements":17,"covered":16,"percent":94.11764706,"sha256":"99e7ffe68dd6405fd9495d1bce7ec8c495859023335aa92c0520fcab48ad913a"}, +{"file":"internal/store/postgres/reconcile.go","statements":32,"covered":31,"percent":96.87500000,"sha256":"ab6d8b2018f910a0ef4fafc3d203dd4efcf63a64f7bef613739f656f89b69670"}, +{"file":"internal/service/production/pagination.go","statements":21,"covered":21,"percent":100.00000000,"sha256":"a0935444ad82e2b92fd975826e91e25cb53bbafbebe352be887571359264d62e"}, +{"file":"internal/service/auth.go","statements":0,"covered":0,"percent":null,"sha256":"3e6f99e5239d8fa5de5d406acb6f95485bd6d4fc23f1080b8eb6fd8834fbd722"}, +{"file":"internal/model/dto/session.go","statements":0,"covered":0,"percent":null,"sha256":"896a694b0c217240343077d4287f4a6e447338c38571abbebcb3d16a519137fb"}, +{"file":"internal/config/memory.go","statements":9,"covered":9,"percent":100.00000000,"sha256":"86cdf9e90b576f4484946bfd9605333fc52077f38d3a39c5d71ddca4cdeecda0"}, +{"file":"internal/controller/http/fiber/middleware.go","statements":41,"covered":40,"percent":97.56097561,"sha256":"3905cbc45b0637da007d5e556900d74e3a9d5575de358f8f4584c2327214324c"}, +{"file":"internal/controller/http/fiber/controller.go","statements":10,"covered":10,"percent":100.00000000,"sha256":"ba7fa956feeeb7426ab183438b019588569a5a1212ad9b1a212423211f136d30"}, +{"file":"internal/controller/http/fiber/configure.go","statements":7,"covered":7,"percent":100.00000000,"sha256":"be356cf26c228958d51f5bafa4305d7325e81ab01065575acbc9b2d7a6ddd4bd"}, +{"file":"internal/admission/distributed.go","statements":0,"covered":0,"percent":null,"sha256":"41ef40897886c0878cbfe52f6716bdcb8433fb7eaddcfff298e1d54c55db423d"}, +{"file":"internal/controller/http/fiber/errors.go","statements":29,"covered":29,"percent":100.00000000,"sha256":"a4a40a11689541409fb804f4a285cbe5c0dc9e620073a8257d410e3db3c882d6"}, +{"file":"cmd/server/s3_bootstrap.go","statements":9,"covered":9,"percent":100.00000000,"sha256":"b45f7b28965346df23dcbeeeaf4cd446f9dd5f9c0446558298f96246f1b725d3"}, +{"file":"internal/store/memory/chats.go","statements":31,"covered":31,"percent":100.00000000,"sha256":"69652b5c7b3834503efe29ae3847db6b0000b9fb0a15774cd7cb20bc8a003e8e"}, +{"file":"internal/store/interface.go","statements":0,"covered":0,"percent":null,"sha256":"a91695ddbf0162fad728643e5d33002bbd382d03d950c433ca9c1f44cdbdc2ba"}, +{"file":"internal/controller/http/fiber/lifecycle.go","statements":24,"covered":22,"percent":91.66666667,"sha256":"f39cf7a0049a2ed39d6cc1fcc66b44656d9d6120914d0e973b032e12f3d0de08"}, +{"file":"internal/valkey/client.go","statements":35,"covered":34,"percent":97.14285714,"sha256":"2db9603f21ceef6a2d245385fbf0977666f4bdb842adbe216c0e294be8d3c02f"}, +{"file":"internal/store/postgres/store.go","statements":57,"covered":53,"percent":92.98245614,"sha256":"c82ce119e5bf06a701df762ded6398886781962581148b9204ab37f108f07360"}, +{"file":"cmd/server/main.go","statements":3,"covered":3,"percent":100.00000000,"sha256":"c22d8cdf4ce31fde29945e967d3f025e280a7dc4d2022329d51be972d2a0f495"}, +{"file":"internal/controller/http/fiber/stream.go","statements":14,"covered":14,"percent":100.00000000,"sha256":"6bc72fdc23332e08e4dee8a5ed3f9110952208af86b529d5d4847ebcf00dd4c3"}, +{"file":"internal/config/config.go","statements":17,"covered":17,"percent":100.00000000,"sha256":"8cb675a0bef8b1645071e4cb85b79a467620e99b375db5d8d3e16a54b4c5b5fa"}, +{"file":"internal/service/interface.go","statements":0,"covered":0,"percent":null,"sha256":"2e5597cdc59a0e6b8f587ece7c42d9c790617c8f462baf02c16ca851790a1ded"}, +{"file":"internal/controller/http/fiber/attachments.go","statements":23,"covered":23,"percent":100.00000000,"sha256":"179cc4b26b87991f4f5876c726c775ec0b8ce22a1ac28c3a5e5a13413301a224"}, +{"file":"internal/admission/limiter.go","statements":16,"covered":16,"percent":100.00000000,"sha256":"a373c9539167a4b7c35d45334d35ffd4e9d124c1740c88c12094fededd9643b4"}, +{"file":"internal/store/postgres/sessions.go","statements":13,"covered":13,"percent":100.00000000,"sha256":"889a63b51b29012f4ae5499d1c0577fe29f2f2959b05629f8e123715cfc1f75f"}, +{"file":"internal/store/postgres/users.go","statements":21,"covered":20,"percent":95.23809524,"sha256":"d6adb976909a211aa195ef3ac752cc02f5422944bbc6aff5ea25227214415b1c"}, +{"file":"internal/service/production/users.go","statements":19,"covered":19,"percent":100.00000000,"sha256":"30d3299b63ab49163c3654ad22e0b9f74094181ee13e3931775eab4df6fc38ce"}, +{"file":"internal/controller/http/fiber/error_codes.go","statements":0,"covered":0,"percent":null,"sha256":"3351a019c43d0360ee0fd68afabb5200422d065ae7bd908b1c0a5c02a0c1ad41"}, +{"file":"cmd/server/media.go","statements":7,"covered":7,"percent":100.00000000,"sha256":"6b4274e5eb7530f2ea80dc663d80312e6d2ba93f1fe1b68204ebc37ca66c54a5"}, +{"file":"cmd/server/healthcheck.go","statements":19,"covered":18,"percent":94.73684211,"sha256":"5ba854fae2321ed9e57f8776fe1503d567ffe2c88a2d4c4abb9bf74d84cc9fce"}, +{"file":"internal/service/authentication/service.go","statements":48,"covered":47,"percent":97.91666667,"sha256":"295ac01d910f0b6eb94098c3808e7af5d878dc5e401c85094db86598b786cc93"}, +{"file":"internal/service/cache.go","statements":0,"covered":0,"percent":null,"sha256":"033986bd1fb166564c0cd5fafea74699d22c4fe4a6143e69fbd72c89f5eafdb0"}, +{"file":"internal/store/postgres/attachments.go","statements":21,"covered":20,"percent":95.23809524,"sha256":"d2af1aa998f902640c17e913dd9b591fc6612c8b4410786080b6023ee13a6a9c"}, +{"file":"internal/config/persistence.go","statements":13,"covered":13,"percent":100.00000000,"sha256":"28c61d23db4ca2ef7ed952451f465318865076d2393eaa94ff5d7bb339c4c06b"} +],"passed":true} diff --git a/evidence/current/coverage.txt b/evidence/current/coverage.txt new file mode 100644 index 0000000..11b8623 --- /dev/null +++ b/evidence/current/coverage.txt @@ -0,0 +1,112 @@ +warning: GOCOVERDIR not set, no coverage data emitted +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/cmd/server 3.503s coverage: 31.1% of statements in ./... +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/admission 2.633s coverage: 1.1% of statements in ./... +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config 2.641s coverage: 5.5% of statements in ./... + gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/controller coverage: 0.0% of statements +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/controller/http/fiber 2.794s coverage: 43.5% of statements in ./... +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor 2.647s coverage: 2.3% of statements in ./... +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/hashring 3.107s coverage: 1.3% of statements in ./... +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/media 2.687s coverage: 9.8% of statements in ./... +? gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto [no test files] +? gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/http [no test files] +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/objectstore 2.769s coverage: 3.2% of statements in ./... +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/password 3.586s coverage: 1.1% of statements in ./... +? gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service [no test files] +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service/authentication 2.638s coverage: 3.6% of statements in ./... +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service/production 2.542s coverage: 14.5% of statements in ./... +? gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store [no test files] +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/memory 2.171s coverage: 6.1% of statements in ./... +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/postgres 4.483s coverage: 16.1% of statements in ./... +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/strictjson 2.163s coverage: 3.2% of statements in ./... +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/valkey 2.229s coverage: 7.5% of statements in ./... +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/student-tests 2.186s coverage: 0.0% of statements in ./... +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. + 100.00% internal/store/memory/store.go + 100.00% internal/service/production/members.go + 95.65% internal/config/controller.go + N/A internal/controller/interface.go + 100.00% internal/config/logger.go + 100.00% internal/valkey/admission.go + 100.00% internal/service/production/service.go + 94.44% internal/password/password.go + 100.00% internal/strictjson/decode.go + 93.55% internal/store/postgres/messages.go + N/A internal/service/cursor.go + 100.00% internal/media/reconcile.go + 100.00% cmd/server/reconcile.go + 95.45% internal/store/memory/users.go + 100.00% internal/store/memory/capacity.go + 100.00% internal/service/production/access.go + 97.30% internal/controller/http/fiber/handlers.go + 100.00% internal/controller/http/fiber/routes.go + 100.00% internal/controller/http/fiber/mapping.go + 100.00% internal/controller/http/fiber/health.go + 100.00% internal/media/validation.go + 94.44% internal/cursor/codec.go + 100.00% internal/controller/fx.go + 94.12% internal/service/production/pages.go + N/A internal/service/errors.go + 100.00% internal/config/objectstore.go + 100.00% internal/media/service.go + 100.00% internal/service/production/errors.go + N/A internal/controller/http/fiber/headers.go + 100.00% cmd/server/commands.go + 94.74% internal/store/memory/messages.go + 92.31% internal/objectstore/client.go + N/A internal/model/dto/models.go + 90.48% internal/store/postgres/idempotency.go + 92.31% internal/store/postgres/chats.go + 100.00% internal/service/production/chats.go + 100.00% internal/media/run_reconcile.go + 100.00% internal/controller/http/fiber/error_registry.go + 100.00% internal/controller/http/fiber/public_errors.go + 100.00% internal/controller/http/fiber/metrics.go + N/A internal/controller/http/fiber/dependencies.go + 95.24% internal/hashring/ring.go + 90.00% cmd/server/logger.go + 100.00% internal/store/memory/ordering.go + 100.00% internal/media/orphans.go + 91.67% internal/controller/http/fiber/decode.go + 100.00% internal/controller/http/fiber/auth.go + 100.00% internal/service/production/messages.go + 100.00% internal/config/environment.go + 90.91% cmd/server/persistence.go + 100.00% internal/service/production/validation.go + 95.83% internal/service/production/creation.go + 90.00% internal/objectstore/list.go + N/A internal/model/http/models.go + N/A internal/model/dto/attachment.go + 100.00% cmd/server/app.go + 94.12% internal/valkey/cache.go + 96.88% internal/store/postgres/reconcile.go + 100.00% internal/service/production/pagination.go + N/A internal/service/auth.go + N/A internal/model/dto/session.go + 100.00% internal/config/memory.go + 97.56% internal/controller/http/fiber/middleware.go + 100.00% internal/controller/http/fiber/controller.go + 100.00% internal/controller/http/fiber/configure.go + N/A internal/admission/distributed.go + 100.00% internal/controller/http/fiber/errors.go + 100.00% cmd/server/s3_bootstrap.go + 100.00% internal/store/memory/chats.go + N/A internal/store/interface.go + 91.67% internal/controller/http/fiber/lifecycle.go + 97.14% internal/valkey/client.go + 92.98% internal/store/postgres/store.go + 100.00% cmd/server/main.go + 100.00% internal/controller/http/fiber/stream.go + 100.00% internal/config/config.go + N/A internal/service/interface.go + 100.00% internal/controller/http/fiber/attachments.go + 100.00% internal/admission/limiter.go + 100.00% internal/store/postgres/sessions.go + 95.24% internal/store/postgres/users.go + 100.00% internal/service/production/users.go + N/A internal/controller/http/fiber/error_codes.go + 100.00% cmd/server/media.go + 94.74% cmd/server/healthcheck.go + 97.92% internal/service/authentication/service.go + N/A internal/service/cache.go + 95.24% internal/store/postgres/attachments.go + 100.00% internal/config/persistence.go diff --git a/evidence/current/environment.txt b/evidence/current/environment.txt new file mode 100644 index 0000000..21d7240 --- /dev/null +++ b/evidence/current/environment.txt @@ -0,0 +1,3 @@ +go version go1.26.1 darwin/arm64 +Darwin arm64 +2026-10-29T13:27:20Z diff --git a/evidence/current/image-build-retry.txt b/evidence/current/image-build-retry.txt new file mode 100644 index 0000000..e94a575 --- /dev/null +++ b/evidence/current/image-build-retry.txt @@ -0,0 +1,65 @@ +#0 building with "asisti-builder" instance using docker-container driver + +#1 [internal] load build definition from Dockerfile +#1 transferring dockerfile: 583B done +#1 DONE 0.0s + +#2 [internal] load metadata for docker.io/library/golang:1.26.1-alpine3.23 +#2 DONE 2.0s + +#3 [internal] load .dockerignore +#3 transferring context: 145B done +#3 DONE 0.0s + +#4 [build 1/7] FROM docker.io/library/golang:1.26.1-alpine3.23@sha256:2389ebfa5b7f43eeafbd6be0c3700cc46690ef842ad962f6c5bd6be49ed82039 +#4 resolve docker.io/library/golang:1.26.1-alpine3.23@sha256:2389ebfa5b7f43eeafbd6be0c3700cc46690ef842ad962f6c5bd6be49ed82039 done +#4 DONE 0.0s + +#5 [internal] load build context +#5 transferring context: 413.07kB 0.0s done +#5 DONE 0.0s + +#6 [build 2/7] WORKDIR /src +#6 CACHED + +#7 [build 3/7] COPY go.mod go.sum ./ +#7 DONE 0.0s + +#8 [build 4/7] RUN go mod download +#8 DONE 66.3s + +#9 [build 5/7] COPY cmd/ ./cmd/ +#9 DONE 0.0s + +#10 [build 6/7] COPY internal/ ./internal/ +#10 DONE 0.0s + +#11 [build 7/7] RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server +#11 DONE 62.9s + +#12 [runtime 1/1] COPY --from=build /out/server /server +#12 DONE 0.0s + +#13 exporting to image +#13 exporting layers +#13 exporting layers 0.6s done +#13 exporting manifest sha256:3a10e818a9f128428d08a8c5546e259943077ef339a90d5f74952597a2185453 done +#13 exporting config sha256:c0c16b388693366ca11526b37426a3f3256591b18df33276647a5e96fd674bdd done +#13 exporting attestation manifest sha256:5457631740a3ffa079de9c58509922fd53b1b0f6abcde0459a1c121f8e3ee031 done +#13 exporting manifest list sha256:36d4ccb6d3c44e7a2540187a628d148e8c1601cdc3215eb81fc37edf48f5c2ce done +#13 pushing layers +#13 ... + +#14 [auth] mtusi2026lab4:pull,push token for asisti.registry.twcstorage.ru +#14 DONE 0.0s + +#13 exporting to image +#13 pushing layers 28.2s done +#13 pushing manifest for asisti.registry.twcstorage.ru/mtusi2026lab4:solution-20261029@sha256:36d4ccb6d3c44e7a2540187a628d148e8c1601cdc3215eb81fc37edf48f5c2ce +#13 pushing manifest for asisti.registry.twcstorage.ru/mtusi2026lab4:solution-20261029@sha256:36d4ccb6d3c44e7a2540187a628d148e8c1601cdc3215eb81fc37edf48f5c2ce 3.8s done +#13 DONE 32.6s + +#15 resolving provenance for metadata file +#15 DONE 0.0s + +View build details: docker-desktop://dashboard/build/asisti-builder/asisti-builder0/3sb0okjofqie2jze3vr1rhanl diff --git a/evidence/current/image-build.txt b/evidence/current/image-build.txt new file mode 100644 index 0000000..8d59e9e --- /dev/null +++ b/evidence/current/image-build.txt @@ -0,0 +1,20 @@ +#0 building with "asisti-builder" instance using docker-container driver + +#1 [internal] load build definition from Dockerfile +#1 transferring dockerfile: 583B done +#1 DONE 0.0s + +#2 [internal] load metadata for docker.io/library/golang:1.26.1-alpine3.23 +#2 ERROR: failed to authorize: failed to fetch anonymous token: Get "https://auth.docker.io/token?scope=repository%3Alibrary%2Fgolang%3Apull&service=registry.docker.io": EOF +------ + > [internal] load metadata for docker.io/library/golang:1.26.1-alpine3.23: +------ +Dockerfile:1 +-------------------- + 1 | >>> FROM golang:1.26.1-alpine3.23 AS build + 2 | WORKDIR /src + 3 | COPY go.mod go.sum ./ +-------------------- +ERROR: failed to solve: failed to fetch anonymous token: Get "https://auth.docker.io/token?scope=repository%3Alibrary%2Fgolang%3Apull&service=registry.docker.io": EOF + +View build details: docker-desktop://dashboard/build/asisti-builder/asisti-builder0/5ql060od551yb87e96hnuvzf8 diff --git a/evidence/current/image.json b/evidence/current/image.json new file mode 100644 index 0000000..ead2c7d --- /dev/null +++ b/evidence/current/image.json @@ -0,0 +1,20 @@ +{ + "buildx.build.provenance": { + "buildType": "", + "invocation": { + "configSource": {}, + "parameters": {}, + "environment": { + "platform": "" + } + } + }, + "buildx.build.ref": "asisti-builder/asisti-builder0/3sb0okjofqie2jze3vr1rhanl", + "containerimage.descriptor": { + "mediaType": "application/vnd.oci.image.index.v1+json", + "digest": "sha256:36d4ccb6d3c44e7a2540187a628d148e8c1601cdc3215eb81fc37edf48f5c2ce", + "size": 855 + }, + "containerimage.digest": "sha256:36d4ccb6d3c44e7a2540187a628d148e8c1601cdc3215eb81fc37edf48f5c2ce", + "image.name": "asisti.registry.twcstorage.ru/mtusi2026lab4:solution-20261029" +} \ No newline at end of file diff --git a/evidence/current/lint.txt b/evidence/current/lint.txt new file mode 100644 index 0000000..6a3ebaa --- /dev/null +++ b/evidence/current/lint.txt @@ -0,0 +1 @@ +0 issues. diff --git a/evidence/current/reconciliation.txt b/evidence/current/reconciliation.txt new file mode 100644 index 0000000..1382f01 --- /dev/null +++ b/evidence/current/reconciliation.txt @@ -0,0 +1,16 @@ +=== RUN TestReclaimExcludesPublication +--- PASS: TestReclaimExcludesPublication (0.05s) +=== RUN TestReclaimProtectsFreshReservation +--- PASS: TestReclaimProtectsFreshReservation (0.03s) +=== RUN TestMetadataReconciliation +--- PASS: TestMetadataReconciliation (0.03s) +=== RUN TestMeasuredMissingInventory +=== RUN TestMeasuredMissingInventory/100_records + research_test.go:49: records=100 missing=100 elapsed_ms=45.622 apply=false +=== RUN TestMeasuredMissingInventory/1000_records + research_test.go:49: records=1000 missing=1000 elapsed_ms=457.760 apply=false +--- PASS: TestMeasuredMissingInventory (0.58s) + --- PASS: TestMeasuredMissingInventory/100_records (0.08s) + --- PASS: TestMeasuredMissingInventory/1000_records (0.50s) +PASS +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/postgres 1.117s diff --git a/evidence/current/smoke.txt b/evidence/current/smoke.txt new file mode 100644 index 0000000..467be14 --- /dev/null +++ b/evidence/current/smoke.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://127.0.0.1:18084" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/current/source.sha256 b/evidence/current/source.sha256 new file mode 100644 index 0000000..699fee5 --- /dev/null +++ b/evidence/current/source.sha256 @@ -0,0 +1,183 @@ +b063cc345976028b590d616f56581254269503e6ab190ca0380cb75ecb7e5111 cmd/server/app_test.go +3302faabb63a43c83a646e3c2217af28f55707198206c80aae5f18787cb8e627 cmd/server/persistence.go +6b4274e5eb7530f2ea80dc663d80312e6d2ba93f1fe1b68204ebc37ca66c54a5 cmd/server/media.go +5ba854fae2321ed9e57f8776fe1503d567ffe2c88a2d4c4abb9bf74d84cc9fce cmd/server/healthcheck.go +aaa809bfdef5c19eec725d754c6934d45dfb491c2d97b5a135c0bc351d61beaf cmd/server/persistence_test.go +934f5f3cac0307dbb26b38ec232270a1100b83fe54990e16f98a1277126feb14 cmd/server/logger.go +b45f7b28965346df23dcbeeeaf4cd446f9dd5f9c0446558298f96246f1b725d3 cmd/server/s3_bootstrap.go +b5a69912970b440c09e1d3db9a8dcbbe21077ecf848acd389d83cc98ad5189dd cmd/server/app.go +deb6af7778dd9882215785e951784756832df64dafca8d52cb47fa632d07596c cmd/server/healthcheck_test.go +a88c8f29f9482564248bc43d7a6386baca64710932877e5fa7008a31a2ae4c42 cmd/server/reconcile.go +1d16d1df1e0b0d59e7084e78d2e6caa0d30c3240e0bc040a7963f7f6b862c311 cmd/server/commands.go +ed550fa2b8e74b2d8a77264f141516a5a949a01dc681d43e38cf43fc1af8c9c9 cmd/server/media_test.go +c22d8cdf4ce31fde29945e967d3f025e280a7dc4d2022329d51be972d2a0f495 cmd/server/main.go +1ad8dcb7d055fcb8da56d10e1089b47fe1b9cbf848381ded1a4048f8fd41278a internal/password/password_test.go +fe2df6990331573bbf0202ebc81c58263a7c16a29b713eefa0f0e852e4ee4a76 internal/password/password.go +6041f376a0f69a66ebf0ecacbd20d03d5d57590ae67585613256b0713e106fdf internal/cursor/codec_test.go +678e197744fb2fc4b51715f51593b98761125bbaddbe4cb1b7032bd6b173ef04 internal/cursor/codec.go +86cdf9e90b576f4484946bfd9605333fc52077f38d3a39c5d71ddca4cdeecda0 internal/config/memory.go +8cb675a0bef8b1645071e4cb85b79a467620e99b375db5d8d3e16a54b4c5b5fa internal/config/config.go +28c61d23db4ca2ef7ed952451f465318865076d2393eaa94ff5d7bb339c4c06b internal/config/persistence.go +1c853c0f771e2c75958db8158797f2287e06c8d9efe8856365b7b9c71b1747b0 internal/config/controller.go +7377c7fc286e04292b7c82e4e286cadf409ce833079f794a501456024e73434a internal/config/persistence_test.go +3a69e20a00fa2ed816f344a990e9e0ef0077e1607d4207858b4b00e4ff5d1988 internal/config/logger.go +c6283fe310747e8297850b48c0a119c1d62db4903dd2c779c6296678563df188 internal/config/objectstore.go +027761b059b76898a84318ec1a8b43158d8bafd6e0b15b52efbe902b4d8c5ea3 internal/config/environment.go +4e57110e53843506d1dd7336feaa603538b425a6127988590fdfc9fe3e878323 internal/config/objectstore_test.go +0181673dcc4db2557783941aa8c5b3f61bc0ca6b5196d87e69df000346c40391 internal/config/config_test.go +99e7ffe68dd6405fd9495d1bce7ec8c495859023335aa92c0520fcab48ad913a internal/valkey/cache.go +2db9603f21ceef6a2d245385fbf0977666f4bdb842adbe216c0e294be8d3c02f internal/valkey/client.go +e4931f0a6c5ead7d4a7c14aee1862b9a7d5a6dff9e938ac3a3819034435af28e internal/valkey/client_test.go +f729a0c695ad01f80e7450d0e802c07cfb3f35340b6ea22a20e00348a9b64371 internal/valkey/admission.go +d52fb501e76ddb23c645c1aaa599e32da9d01e50064c9ca97ac0e320ffc7f0c8 internal/admission/limiter_test.go +41ef40897886c0878cbfe52f6716bdcb8433fb7eaddcfff298e1d54c55db423d internal/admission/distributed.go +a373c9539167a4b7c35d45334d35ffd4e9d124c1740c88c12094fededd9643b4 internal/admission/limiter.go +ea6273526572b4bfcf681ef2659ea2c1ec74bfedeb5e9bdbd83ebfabe0647bf9 internal/strictjson/decode_test.go +3830b93225ff8056993643fd404ab990ddb752fd80fb8ece6410e834b6023f5f internal/strictjson/optional_test.go +d99595e1f74406c5aa617c0ec062afb7f12f116bd86776e87cc9c9a436ff7eea internal/strictjson/decode.go +24b6b06d6fab919cd2130697b44fd0d358265184dca0ca8362e0b4fcc2ae5a68 internal/controller/interface.go +0b3fad8fc87f561573bf4ee906a48a8fb317385ecc7600bdca3cc61992fa9253 internal/controller/http/fiber/users_test.go +435c87ffd05adbd06cbfe9bd2cfbf1f4ca6a01d185e29c165d46b0da20f8abb3 internal/controller/http/fiber/body_limit_test.go +52dc454454e455f1df3b63a7f7958986fefb850dbdacb2d96dbc9ead153fdb74 internal/controller/http/fiber/public_errors_test.go +be356cf26c228958d51f5bafa4305d7325e81ab01065575acbc9b2d7a6ddd4bd internal/controller/http/fiber/configure.go +c810dbed23d3587c50f80aec92843d01158d153e4e3f8bbe03fe9674b18447ee internal/controller/http/fiber/attachments_test.go +ba7fa956feeeb7426ab183438b019588569a5a1212ad9b1a212423211f136d30 internal/controller/http/fiber/controller.go +468f8d5b964d07a63c7900613bc6b84b2bbdaf925e9f549c1970e392de324caf internal/controller/http/fiber/errors_test.go +400914b0bed80b41eb890fb8439c4169dd855fa6f98879fcdc570eceba495be6 internal/controller/http/fiber/auth_test.go +a46a8429feb8c8761fde4d20d431ebd875b9c0e2a3deb71750ff6a666cb913b6 internal/controller/http/fiber/messages_test.go +54db82201697bdfcc65712afe61b650f8f0885fc65222dc4cc6d1c3bb07d82dd internal/controller/http/fiber/metrics.go +330913d86e6b3f1bedf14f770df7012426f86f4ae2a1cb1fddb2b4f6294a98f8 internal/controller/http/fiber/auth.go +6bc72fdc23332e08e4dee8a5ed3f9110952208af86b529d5d4847ebcf00dd4c3 internal/controller/http/fiber/stream.go +ba1e6f4d2aedb003e9818d33625f74fd1e8ae03e987f0c20d7e4c1d8c9e727ce internal/controller/http/fiber/middleware_test.go +4b2316357f3bdcb74281fd63a11b88aaebeac89a048a152fed6ddd9069793fa9 internal/controller/http/fiber/metrics_test.go +cfa54cff49e70a0f7c2354e60ff84d50b5a19f0d3c3ddd931cdf7872ed1c0e19 internal/controller/http/fiber/handlers.go +41dfd12d3d2a6f518376e2125f4f818c92eff786f061bea89e924cdfcd0aa8db internal/controller/http/fiber/controller_test.go +1b7628b0f313089891e9bfa44fd6e0cdb753375ffd2cf22caf5abdab61f9c102 internal/controller/http/fiber/health.go +f39cf7a0049a2ed39d6cc1fcc66b44656d9d6120914d0e973b032e12f3d0de08 internal/controller/http/fiber/lifecycle.go +fe59de68ad07ae56c061b01a7946e848b3ee41a3005a3ae9fee1843d31aba5ab internal/controller/http/fiber/metrics_labels_test.go +1fbe98b2de99a794a0f2726ad350f258004bd2890660d28b2982c72e5579f3f5 internal/controller/http/fiber/public_errors.go +179cc4b26b87991f4f5876c726c775ec0b8ce22a1ac28c3a5e5a13413301a224 internal/controller/http/fiber/attachments.go +e248223517102e1eecdeba9a5e2d844c0afc14116a56e9a1d76c6afb755e63d1 internal/controller/http/fiber/routes.go +6ee07f110bdb29d8d2191ab8889ba60474b33a5b225739875c8a57d8434be15d internal/controller/http/fiber/dependencies.go +3905cbc45b0637da007d5e556900d74e3a9d5575de358f8f4584c2327214324c internal/controller/http/fiber/middleware.go +3351a019c43d0360ee0fd68afabb5200422d065ae7bd908b1c0a5c02a0c1ad41 internal/controller/http/fiber/error_codes.go +a33db3d6e1b01d62c65a28935e7f8890f66c8e60413a079f6ee3d042f814592d internal/controller/http/fiber/fixtures_auth_test.go +9d5da8d98ece2706e324cb2deca915e0811b6ad98f7aaac5805ca0f3cc0a1f9a internal/controller/http/fiber/error_registry.go +496f50e5693456574614b8a47086a550018884adb37919ce82abf8c4e7e5254d internal/controller/http/fiber/lifecycle_test.go +2db9a0329f722d2ec1cefc529df17875dbc7eb3e50d58319934891c1da3bc7ed internal/controller/http/fiber/mapping.go +9c092ba7bca191f25c3ba9c9a5d1ca844126bee740a4c0262b776e947263ac68 internal/controller/http/fiber/chats_test.go +0f8ac98d5ec9b606f661985515d9a13047ebd262b2c4e99f1a9d61bf8a591dfa internal/controller/http/fiber/decode.go +d32a9a01a8b391f4d986e14947a25a0504f4363335ea1524df05e0dd8e9e61bd internal/controller/http/fiber/headers.go +a4a40a11689541409fb804f4a285cbe5c0dc9e620073a8257d410e3db3c882d6 internal/controller/http/fiber/errors.go +af72da25fea9811c13a50c8133e9e8b35e070c5295069c40c0ae89cdc0aff19a internal/controller/fx.go +e347b48cde4d4292505dc3f2834ada4d6c8c2dd133600e281741a13f9f13b104 internal/objectstore/list.go +0e42d39cc3ca70d440fc49dddf614af8a33801ac23a954ac613d6397631a9c3c internal/objectstore/client.go +94732ce0cfd579c4e1bc7a7911aca1d6851b86fbc7665f0101b328d9333e66cc internal/objectstore/client_test.go +e205901cec2e66011ec764b70879b0eb0d28a78c4dea8913a9fb30c2d778fa8c internal/hashring/ring.go +606cdf84342b97a63eedfb9cd37aa7c99a489eee54641d8645be930a9ce6be0d internal/hashring/ring_test.go +5e2536b8eba6bb92b751a7b817b6338755008857e3b53920d8da6fb67adeebdb internal/model/dto/models.go +896a694b0c217240343077d4287f4a6e447338c38571abbebcb3d16a519137fb internal/model/dto/session.go +4a7790ef7a3cfc2d9c6f35d42873e04ea48e72a95af2dd173a6f4d2e29c51b46 internal/model/dto/attachment.go +d68827b0bd3e6df4037621b6941298d3923dc348bda5bf314a34861e7c34f04e internal/model/http/models.go +3e6f99e5239d8fa5de5d406acb6f95485bd6d4fc23f1080b8eb6fd8834fbd722 internal/service/auth.go +033986bd1fb166564c0cd5fafea74699d22c4fe4a6143e69fbd72c89f5eafdb0 internal/service/cache.go +2e5597cdc59a0e6b8f587ece7c42d9c790617c8f462baf02c16ca851790a1ded internal/service/interface.go +99a25f75b1289d812c125a2a68640869a2b879abd205c81155fae7bd96174935 internal/service/production/members.go +30d3299b63ab49163c3654ad22e0b9f74094181ee13e3931775eab4df6fc38ce internal/service/production/users.go +5d66b4430605634a484a1ecd0113f7bce45f0a470dcbf04383a0b124f4717901 internal/service/production/messages_page_test.go +3a3141621df8380873cbd1d4ec7e46c42179862f2e0cfb075e430ce428c8587f internal/service/production/users_test.go +6de190a75ed12c6b07bcf2889a085fd8fe1025bc13981f771e0a84627c377d0d internal/service/production/creation.go +2bba77cbf35f7da04d31c6e6c729b06e077f184478dcdd75529d9359ff8081b8 internal/service/production/chats_page_test.go +6f0cf8ab6c0e07bbb82e6b289e3d734c84ead5b82932db2007ef0c54f159f4cb internal/service/production/service.go +cab4f160ebdb0fc833be84452beda326cf42de4e838914850bd5beb3ad0840c1 internal/service/production/messages_test.go +c14ab6f325015a46dea8ed845f271df0a6ddf610bbcf1312eaf14d557a8c2752 internal/service/production/cache_test.go +cc635314dc6d21ad328a6c6ec60fc4cf199ba95d2c3aa7f229fa312050fe0b4a internal/service/production/service_test.go +550ab142c922a201f72595ce0774a1b07062a06e5b728119ccb6d31cac5c59e6 internal/service/production/access.go +879b0a877f8a4a80ca29aaa18ededc5febbccc6dd8dbcb170be0ac4022bdaeec internal/service/production/pagination_test.go +2b7603e5a4b9796eb722281c4dd32b762facadbb97e0006f407bba0267875c67 internal/service/production/fixture_test.go +46f4d77cc32838c28abb57e49ebdc0d30cf36ffe49079b2f84d03c405969a346 internal/service/production/chats.go +76b7c6ee33d98debda99df1ab8272deed64b80aadfec3e079d9d550e0facb4a4 internal/service/production/messages.go +064847cd4f6b9d4cd50913823e20d3538b78aadf056660dafd720d79c0ecf5ca internal/service/production/pages_test.go +0eb30c852f178768e11457a29aa978127d22c4c6f0019aea004123265da5e789 internal/service/production/pages.go +423d4be24ae5ea70d47d2da5b781296e8bb3cebdea90abf31c2117056bd17bbd internal/service/production/validation.go +45e9c94fa662b855608def8830e7eafd05707f5a96b33fdddf640ac87085bcd1 internal/service/production/chats_test.go +a0935444ad82e2b92fd975826e91e25cb53bbafbebe352be887571359264d62e internal/service/production/pagination.go +27abf6ee6231c9e89643b512bd5694f32872e357831ec0d6bd1302d754a5d4dd internal/service/production/errors.go +430996f921d5dab91b47a6f796e19c2b7cb31d386572b326adec581b8be9f6b7 internal/service/cursor.go +295ac01d910f0b6eb94098c3808e7af5d878dc5e401c85094db86598b786cc93 internal/service/authentication/service.go +484d1f64a400a58b27bc9542486626fac2c70e26bd7a48236560118b2c9a8a7d internal/service/authentication/service_test.go +1c36cfef90643276e1c83aede658af6b9e2eb0152b7ae68c2c8abd0e7bf655cd internal/service/errors.go +946a1a7a72badf6a80b9ff9a5c850914f46cf5ebde13a5f9f945514f0e188f88 internal/store/memory/users.go +b797c0b13989ec3de92402414d8d82c2a6570abb130edb5ec3b9318368995a53 internal/store/memory/users_test.go +7b836b76e7681ab5ab78a2a30017f5e9a8cb38cbe5c8c94ec1ad65a606c9f2e7 internal/store/memory/store_test.go +da46630a8a5a3625e40677cd4d5e268e2c320a8f1ed272c2f6c05f45cb0f458b internal/store/memory/capacity.go +da8ff00199151be0462c5a27951fa7b7061a42a1ec9cabce9ed8f46d99820480 internal/store/memory/messages_test.go +23fa65457a7b5a3d456fc4b518b76eeed8d4063b3e5090f62ae8c57d276f018d internal/store/memory/store.go +69652b5c7b3834503efe29ae3847db6b0000b9fb0a15774cd7cb20bc8a003e8e internal/store/memory/chats.go +ca4526c55af46dc8f4205aa8402d6819e05a46840d704d8caea0a477014f70ab internal/store/memory/messages.go +4ed267c6ecd734687e5af3645d18a1a6b2608ca9bda19de3e2e0676f4fa8c416 internal/store/memory/ordering.go +38187635fd996dba52ddf3da216ba6143cb552d1e6375c4b19ba98eb8b8bf26a internal/store/memory/chats_test.go +a91695ddbf0162fad728643e5d33002bbd382d03d950c433ca9c1f44cdbdc2ba internal/store/interface.go +d6adb976909a211aa195ef3ac752cc02f5422944bbc6aff5ea25227214415b1c internal/store/postgres/users.go +448f0287ddd1e68914edcda4b39cc90daaac952a8e19a9e938ad78d27601b590 internal/store/postgres/migrations/003_attachments.sql +3678bb935ed8006d4bdc3e928231cfc5e8ba29e397d6b70122675933acd2a6f4 internal/store/postgres/migrations/002_idempotency.sql +8b8bf6a255cc48cee4b3e4adc94c23cf6955ecf5dfba689bba973dfccbd1b1cb internal/store/postgres/migrations/001_initial.sql +3f40c0225530edb325550cd380c643e2da530bce3b18c93c10093d9a7b4559ee internal/store/postgres/attachments_test.go +be0d432f8c9db7f7d8c44f7c230bddeb1d94509fe22669efd8ca7ac990787b27 internal/store/postgres/store_test.go +faedbc4f7bd15fa6aa14479c9285beac14063529bd2d1a200b1128b894b22363 internal/store/postgres/errors_test.go +889a63b51b29012f4ae5499d1c0577fe29f2f2959b05629f8e123715cfc1f75f internal/store/postgres/sessions.go +c82ce119e5bf06a701df762ded6398886781962581148b9204ab37f108f07360 internal/store/postgres/store.go +b22da29bf82729f76307247476b828f294aaf8ea4fe9285cb1cd7a8d0636dc01 internal/store/postgres/idempotency_test.go +733c121222dc75448c363874aa131e1b9ce89b957a5d45b667c08e33ebb3867a internal/store/postgres/research_test.go +448da4c0755f16184d9bbe6cdef35b9ea4ffcb04b7da832ff82f5c1c34ad0966 internal/store/postgres/idempotency.go +cbd4e074fa631b4e2e6a746f01e2d634391cddfd1d4e60d58ca0182524b97eaa internal/store/postgres/chats.go +3b9f6cc9906f3007cf416bbfc01046794c652a8589c3ea43e57a03b1c8c26bc2 internal/store/postgres/messages.go +375914d765b4ae8e1ba25767bb6e96d6de8ce29a8787055f4242971cb0b5900d internal/store/postgres/reconcile_fixture_test.go +d2af1aa998f902640c17e913dd9b591fc6612c8b4410786080b6023ee13a6a9c internal/store/postgres/attachments.go +ab6d8b2018f910a0ef4fafc3d203dd4efcf63a64f7bef613739f656f89b69670 internal/store/postgres/reconcile.go +77f45226996a1d83f3f4ca2ac6980967d4f7cc1f17f3fd2d324ef267237523c0 internal/media/service.go +2de8c90b6fbc1f680ff34f17a3ad2dead27b27d0ba99e18ba7f1f44175d2e07a internal/media/run_reconcile_test.go +f61e243e07e9260f3d497a8d807e670368575a30842c2c9324e3af9f45c2894f internal/media/service_test.go +327ee894c8c9afa5bcf8236ed4c91befd66699c0ca33178e9690705764faf002 internal/media/orphans.go +16a99b15901009e4861cdd30a24a433d8d36782745af1212e3dffa8b0d50906a internal/media/validation.go +ca279564fe8b3ed9ecba34f071577ef22726f34ca41981f83b985b3bf3e620e3 internal/media/reconcile_test.go +278dc4c8720348df752801adf04a30d1b7508a730665122693ce1dd7b90c376a internal/media/reconcile.go +3255403afcb9486d33c5bb45b5afa9c3c9b85596ce97f86ac2f224011cd8a860 internal/media/run_reconcile.go +fc6e5dfde6eba735601d7087f94b3dbf8905fab7ecbaff0d5c09e11769a19944 internal/media/validation_test.go +df6202fba7566de0489aabc5e7f3d31ad1515673eea106b0332355a8bbc6b1fe charts/messenger/Chart.yaml +d0e0b2382082f0c52dc3c55b53334743680224d0f8cbc4c5ee9b623ff1299e92 charts/messenger/values.schema.json +2c65bf5e0025ff487370acf5039d33ad8308dabee2dccebbc14743c74aeacdb7 charts/messenger/templates/api.yaml +30fa7bc17d161ba63e3713b5b986d90197c0fc54d6eab25bbfad683190162982 charts/messenger/templates/guard.yaml +a5aba49edfb383bd892865349bbc9749769a61b0cc703374948b923a85fc650c charts/messenger/templates/ingress.yaml +7937ef25fb5d23a231cceedfbf79ed36244e9c27ce409db938745dec43e5272e charts/messenger/templates/migrate.yaml +68cd59c6f4d7a24e384509fc920b996031bb949c76c60fe051e4be19882d5616 charts/messenger/templates/network-policy.yaml +a4fa258f575e01fba40dd0616d37e3defb43b84b5c83949420543621ac70af74 charts/messenger/templates/hpa.yaml +0fc92783f1a210eb5af8cf55ee531a8a474429e65fb37033d17b16f45f515658 charts/messenger/templates/postgres.yaml +e42a3a31f9b0d9443038d8e48e512793a8d7454328da322aae45e1138568f1c4 charts/messenger/templates/s3.yaml +3ae360e746fba7cc0f17d8c64972cc5ba1caf4b747cc0805515c0ea660bdfabf charts/messenger/templates/_helpers.tpl +9e64fabb33291e5bfc55cc4f40e4e3ee3651e7d3151ca69b7c05e93165b63545 charts/messenger/templates/valkey.yaml +3b917fc87b39bab6d15a5e9d7877bb0771e2c052cb2b90409ed38010f2739814 charts/messenger/templates/load.yaml +9d95b4a1b729cb04f3ba0cdcb1fc79c662c1dcd74acc334e6deac74a0dfd3e8a charts/messenger/templates/monitoring.yaml +6e83a4e41e4b765cb9c651df4b45779ede0c70313f5d6a3c6ca1b7c8e82a8a73 charts/messenger/values.yaml +aada77ad42debd425cec516e33668a640fc0257f30d1a9aa2f15534ed042bf01 scripts/restart.sh +7c4402c2a6ab80d3b4676e1866d6ecc87b6daea14c080bcb9be9a7e8b959d671 scripts/check_contract.py +08f7633fd1e133ce2c83d15d6921b1f9a9a2d3458df711592a169fb262855a98 scripts/local-action.sh +1bcf34394520b89d95b7605ec56675be799eea920e83cfbba306d5dc0a6d9811 scripts/stop-one.example.sh +d1b09abb1447c9cbfbd17edc12491f0985dcd5652b16cb71a93651d289b46ce8 scripts/stop-one.sh +f7a70569c1fe573ad4fedc5680b77d6d6215c78a4b8bbcd666fefd07f1a8818e scripts/measure-upload.sh +b231016d1e6be9735a6a8925031c6b0e7bf2be9f48be8e7b7aaa2ed5db8b2fbc scripts/port-forward.sh +32a5510e87db858abced249b7ebc23024b41c327170e0f0ff6549bbd444a1fd2 scripts/test-local.sh +e70fd589b93cf610e7f18ecd741be9cebeea4320bb152d58438259cead557a84 scripts/init-secrets.sh +ad9562009b57fa9438740988a6bc9e77758f7e242f781129a8375ed674b99dbf scripts/coverage.sh +4e981acbfc10dac58817bf922d5896e996798551fedc1b2385130e1cddb13d54 scripts/validate_openapi.py +0328246ba75defbced992104e986fde973f63e3830d83251eced5953263e944b scripts/restart.example.sh +0587e6b6d9387fb0179b23d283694121300732c898aad95eabe0376f744fe94c scripts/coverage.awk +a7f93eedc694e099e2b609d04bee5e0920e0c4b06b0692a5688d4ba5913d7259 student-tests/quality_test.go +280f6eaf50aaea25fda4ce6f5829c49403c396d9104f972b5157b954276fc355 student-tests/README.md +817e6702852fc7d168e22cef15daafd3bd13c1ad52036f4533eecc3b2324b3ed student-tests/load/idempotency.js +2ca01d154f2ba1e6f72f036e02061967a8ba149f5b18131eb5760e2692bd76c2 student-tests/load/failover.js +0a58dff9ad7acf8f228e0fd1c2a670bbe7e18a11360715b47659104832e34892 student-tests/load/scaling.js +b370b7793f5c8fc66f250b9e4776b4edbaaa5646eb4425bb5b6e4fe2d242c6b3 student-tests/load/attachments.js +e83331ee1c444bc35898d295a23b767f9c7eca77ae81d2a8a218fa44b976eb27 student-tests/load/hpa.js +f47019141be4ac06d912cbd4281d94d181625a08637801627e897633bb20a124 student-tests/plans.sql +f7ca02dfce0db42f72594d794eda5821a9125e7a2b6966df731f32869a5f0b43 student-tests/research.sql diff --git a/evidence/current/static.txt b/evidence/current/static.txt new file mode 100644 index 0000000..e61e4f6 --- /dev/null +++ b/evidence/current/static.txt @@ -0,0 +1,8 @@ +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +helm lint charts/messenger +==> Linting charts/messenger +[INFO] Chart.yaml: icon is recommended + +1 chart(s) linted, 0 chart(s) failed +helm template lab2 charts/messenger >/dev/null diff --git a/evidence/current/tests.txt b/evidence/current/tests.txt new file mode 100644 index 0000000..cefba00 --- /dev/null +++ b/evidence/current/tests.txt @@ -0,0 +1,21 @@ +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/cmd/server 2.214s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/admission 1.483s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config 1.306s +? gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/controller [no test files] +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/controller/http/fiber 1.671s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor 1.512s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/hashring 1.627s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/media 1.312s +? gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto [no test files] +? gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/http [no test files] +? gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/objectstore [no test files] +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/password 2.280s +? gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service [no test files] +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service/authentication 1.520s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service/production 1.492s +? gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store [no test files] +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/memory 1.517s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/postgres 2.514s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/strictjson 1.234s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/valkey 1.328s +ok gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/student-tests 1.329s diff --git a/evidence/current/upload-memory.txt b/evidence/current/upload-memory.txt new file mode 100644 index 0000000..eee9c72 --- /dev/null +++ b/evidence/current/upload-memory.txt @@ -0,0 +1 @@ +peak_rss_kib=80032 diff --git a/evidence/current/upload-rss-kib.txt b/evidence/current/upload-rss-kib.txt new file mode 100644 index 0000000..c056d9a --- /dev/null +++ b/evidence/current/upload-rss-kib.txt @@ -0,0 +1,24 @@ + 8288 + 8288 + 8288 + 8288 + 8288 + 8288 + 34672 + 75600 + 75856 + 75952 + 75952 + 75984 + 79472 + 79712 + 79776 + 79824 + 79968 + 80032 + 80032 + 80032 + 80032 + 80032 + 80032 + 80048 diff --git a/evidence/current/upload-summary.json b/evidence/current/upload-summary.json new file mode 100644 index 0000000..5cfa0b2 --- /dev/null +++ b/evidence/current/upload-summary.json @@ -0,0 +1,218 @@ +{ + "metrics": { + "vus": { + "value": 10, + "min": 10, + "max": 10 + }, + "http_req_waiting": { + "avg": 9.315203045685283, + "min": 1.592, + "med": 7.114, + "max": 74.707, + "p(90)": 21.329800000000002, + "p(95)": 23.296 + }, + "attachment_upload_ms": { + "p(90)": 18.738200000000003, + "p(95)": 19.585549999999998, + "avg": 9.961129999999999, + "min": 2.953, + "med": 8.788, + "max": 26.254 + }, + "iterations": { + "count": 100, + "rate": 79.42408011030416 + }, + "http_req_sending": { + "avg": 0.028802030456852767, + "min": 0.002, + "med": 0.033, + "max": 0.184, + "p(90)": 0.056, + "p(95)": 0.06779999999999997 + }, + "data_received": { + "count": 7575266, + "rate": 6016585.336408634 + }, + "iteration_duration": { + "min": 53.103833, + "med": 63.8951045, + "max": 82.034333, + "p(90)": 72.3499792, + "p(95)": 75.46148929999998, + "avg": 63.88111499000001 + }, + "http_req_duration": { + "avg": 9.824152284263961, + "min": 2.345, + "med": 7.622, + "max": 74.903, + "p(90)": 21.3578, + "p(95)": 23.354999999999997 + }, + "http_req_receiving": { + "min": 0.007, + "med": 0.027, + "max": 3.207, + "p(90)": 1.4660000000000002, + "p(95)": 1.9305999999999999, + "avg": 0.4801472081218276 + }, + "attachments_rejected": { + "count": 33, + "rate": 26.209946436400376 + }, + "attachments_ready": { + "count": 67, + "rate": 53.21413367390379, + "thresholds": { + "count>0": false + } + }, + "http_req_blocked": { + "avg": 0.021106598984771616, + "min": 0, + "med": 0.002, + "max": 0.538, + "p(90)": 0.002, + "p(95)": 0.2417999999999998 + }, + "http_req_connecting": { + "avg": 0.01655329949238579, + "min": 0, + "med": 0, + "max": 0.345, + "p(90)": 0, + "p(95)": 0.223 + }, + "http_req_failed": { + "passes": 33, + "fails": 164, + "value": 0.16751269035532995 + }, + "data_sent": { + "count": 11229337, + "rate": 8918797.614736026 + }, + "http_req_duration{expected_response:true}": { + "avg": 10.176689024390244, + "min": 2.345, + "med": 7.8205, + "max": 74.903, + "p(90)": 22.5853, + "p(95)": 23.63235 + }, + "http_req_tls_handshaking": { + "avg": 0, + "min": 0, + "med": 0, + "max": 0, + "p(90)": 0, + "p(95)": 0 + }, + "vus_max": { + "value": 10, + "min": 10, + "max": 10 + }, + "checks": { + "passes": 100, + "fails": 0, + "thresholds": { + "rate==1": false + }, + "value": 1 + }, + "http_reqs": { + "count": 197, + "rate": 156.4654378172992 + } + }, + "setup_data": [ + { + "chat": "92b45da6-9c02-443b-82a9-3634b9639297", + "headers": { + "Authorization": "Bearer gG5LxLp2-l_3BaVgaY7cvALh3UrC-DmIuUKpWzs8k2k" + } + }, + { + "chat": "378534fc-c899-4654-ab95-c7a3cad98974", + "headers": { + "Authorization": "Bearer XntfANbWk2OXHzE79bLzKapkhBQoFTwX3IeHMdzMtEo" + } + }, + { + "chat": "b72c4972-4d62-47fd-adda-1f6e75748141", + "headers": { + "Authorization": "Bearer ah5FxhFVT3wzblpoVdLUJZRGQO4HLuu_PT3tWBTFI1k" + } + }, + { + "chat": "77ad720e-ea48-4efc-a114-902d55c7d751", + "headers": { + "Authorization": "Bearer zbl3HvoYSTJNfJ7oTpKjaEzlwZw-9qao9jWk6GalRd8" + } + }, + { + "chat": "ecd393ce-d8ce-48a2-85b0-673182b1a7d4", + "headers": { + "Authorization": "Bearer OHL_jNEfTJFARw6Cyxae5KhVhXK-P17ZzLQZ5uL9MIM" + } + }, + { + "chat": "28134e88-c4ae-4c07-b080-ee722e4c8bbd", + "headers": { + "Authorization": "Bearer dcvmA_MGREYsI65JKDeMF06xPVTdCt0kwIftQoHSoXk" + } + }, + { + "chat": "3da11cb6-af5f-4c82-b0ca-5e0bb2117582", + "headers": { + "Authorization": "Bearer SLkmTQsYV1LRptZmVHuc5bK_FMHSXjLF-gA6Q8pByuk" + } + }, + { + "chat": "81b08113-80c0-4b48-905a-327a76cd9ab8", + "headers": { + "Authorization": "Bearer ByzqqUPpkAPpUS3JY1o-joitFg0t5aSjHLB4X1U3iv0" + } + }, + { + "chat": "1d08c8a5-348e-4754-90ba-ec473306fc6a", + "headers": { + "Authorization": "Bearer jGhMP_A92jtjrTygdmWE-7vMlArSdAc5ZPmCJtJAz9g" + } + }, + { + "chat": "ba310d60-ac80-40a3-bda4-fcc947c121d1", + "headers": { + "Authorization": "Bearer e3Aaez_P-595tPb7BnFGMq0oMVsCT4fpkTaWRR0ru4g" + } + } + ], + "root_group": { + "name": "", + "path": "", + "id": "d41d8cd98f00b204e9800998ecf8427e", + "groups": {}, + "checks": { + "bounded overload": { + "id": "c635697b57e40e018ab59d4990b7dfec", + "passes": 33, + "fails": 0, + "name": "bounded overload", + "path": "::bounded overload" + }, + "download complete": { + "name": "download complete", + "path": "::download complete", + "id": "b059a8458a190cb232f32a0831d24c02", + "passes": 67, + "fails": 0 + } + } + } +} \ No newline at end of file diff --git a/evidence/current/upload.txt b/evidence/current/upload.txt new file mode 100644 index 0000000..2ef43b3 --- /dev/null +++ b/evidence/current/upload.txt @@ -0,0 +1,64 @@ + + /\ Grafana /‾‾/ + /\ / \ |\ __ / / + / \/ \ | |/ / / ‾‾\ + / \ | ( | (‾) | + / __________ \ |_|\_\ \_____/ + + + execution: local + script: student-tests/load/attachments.js + output: - + + scenarios: (100.00%) 1 scenario, 10 max VUs, 10m30s max duration (incl. graceful stop): + * default: 100 iterations shared among 10 VUs (maxDuration: 10m0s, gracefulStop: 30s) + + +running (00m01.0s), 10/10 VUs, 59 complete and 0 interrupted iterations +default [ 59% ] 10 VUs 00m00.4s/10m0s 059/100 shared iters + + + █ THRESHOLDS + + attachments_ready + ✓ 'count>0' count=67 + + checks + ✓ 'rate==1' rate=100.00% + + + █ TOTAL RESULTS + + checks_total.......: 100 79.42408/s + checks_succeeded...: 100.00% 100 out of 100 + checks_failed......: 0.00% 0 out of 100 + + ✓ bounded overload + ✓ download complete + + CUSTOM + attachment_upload_ms...........: avg=9.96ms min=2.95ms med=8.78ms max=26.25ms p(90)=18.73ms p(95)=19.58ms + attachments_ready..............: 67 53.214134/s + attachments_rejected...........: 33 26.209946/s + + HTTP + http_req_duration..............: avg=9.82ms min=2.34ms med=7.62ms max=74.9ms p(90)=21.35ms p(95)=23.35ms + { expected_response:true }...: avg=10.17ms min=2.34ms med=7.82ms max=74.9ms p(90)=22.58ms p(95)=23.63ms + http_req_failed................: 16.75% 33 out of 197 + http_reqs......................: 197 156.465438/s + + EXECUTION + iteration_duration.............: avg=63.88ms min=53.1ms med=63.89ms max=82.03ms p(90)=72.34ms p(95)=75.46ms + iterations.....................: 100 79.42408/s + vus............................: 10 min=10 max=10 + vus_max........................: 10 min=10 max=10 + + NETWORK + data_received..................: 7.6 MB 6.0 MB/s + data_sent......................: 11 MB 8.9 MB/s + + + + +running (00m01.3s), 00/10 VUs, 100 complete and 0 interrupted iterations +default ✓ [ 100% ] 10 VUs 00m00.7s/10m0s 100/100 shared iters diff --git a/evidence/local/actions-errors.txt b/evidence/local/actions-errors.txt new file mode 100644 index 0000000..e69de29 diff --git a/evidence/local/actions.txt b/evidence/local/actions.txt new file mode 100644 index 0000000..b30796c --- /dev/null +++ b/evidence/local/actions.txt @@ -0,0 +1,4 @@ +2026-10-29T13:13:36Z target=local-b action=stop +2026-10-29T13:13:51Z action=restart-api-postgres-valkey-s3 retained-container-filesystems +2026-10-29T13:23:13Z target=local-b action=stop +2026-10-29T13:23:28Z action=restart-api-postgres-valkey-s3 retained-container-filesystems diff --git a/evidence/local/binary.sha256 b/evidence/local/binary.sha256 new file mode 100644 index 0000000..c271c1c --- /dev/null +++ b/evidence/local/binary.sha256 @@ -0,0 +1 @@ +1ec59037d8730a8a00e7fce27716d1925a29dc291e6c504e02cb427de2aebf51 /var/folders/j2/q072k5hn69lbhbzd5sbzrzbm0000gp/T/tmp.ZnJzHrjm5f/server diff --git a/evidence/local/failover.txt b/evidence/local/failover.txt new file mode 100644 index 0000000..7631c82 --- /dev/null +++ b/evidence/local/failover.txt @@ -0,0 +1,4 @@ +python3 tests/resilience.py --mode failover --action-script "/Users/marlo/go/src/gitflick.ru/vlad_marlo/gitea/mtusi2026lab4/scripts/local-action.sh" --base-url "http://127.0.0.1:62881" +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 115, "failed_attempts": 0, "first_recovery_seconds": 0.148} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/local/go-version.txt b/evidence/local/go-version.txt new file mode 100644 index 0000000..c787ace --- /dev/null +++ b/evidence/local/go-version.txt @@ -0,0 +1 @@ +go version go1.26.1 darwin/arm64 diff --git a/evidence/local/local-a.txt b/evidence/local/local-a.txt new file mode 100644 index 0000000..e69de29 diff --git a/evidence/local/local-b.txt b/evidence/local/local-b.txt new file mode 100644 index 0000000..e69de29 diff --git a/evidence/local/persistence.txt b/evidence/local/persistence.txt new file mode 100644 index 0000000..6d38baa --- /dev/null +++ b/evidence/local/persistence.txt @@ -0,0 +1,3 @@ +python3 tests/resilience.py --mode persistence --action-script "/Users/marlo/go/src/gitflick.ru/vlad_marlo/gitea/mtusi2026lab4/scripts/local-action.sh" --base-url "http://127.0.0.1:62881" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/local/smoke-after.txt b/evidence/local/smoke-after.txt new file mode 100644 index 0000000..f8de521 --- /dev/null +++ b/evidence/local/smoke-after.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://127.0.0.1:62881" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/local/smoke.txt b/evidence/local/smoke.txt new file mode 100644 index 0000000..f8de521 --- /dev/null +++ b/evidence/local/smoke.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://127.0.0.1:62881" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e43420e --- /dev/null +++ b/go.mod @@ -0,0 +1,55 @@ +module gitea.privcloud.tech/vlad_marlo/mtusi2026lab4 + +go 1.26.1 + +require ( + github.com/gofiber/fiber/v2 v2.52.12 + github.com/google/uuid v1.6.0 + github.com/jackc/pgx/v5 v5.9.1 + github.com/prometheus/client_golang v1.23.2 + github.com/redis/go-redis/v9 v9.17.2 + github.com/valyala/fasthttp v1.51.0 + go.lab260.ru/fielderr v0.0.0-20260609150058-2f9540b99e41 + go.uber.org/fx v1.24.0 + go.uber.org/zap v1.27.1 + golang.org/x/crypto v0.49.0 +) + +require ( + github.com/andybalholm/brotli v1.1.0 // indirect + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/go-ini/ini v1.67.0 // indirect + github.com/goccy/go-json v0.10.5 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/klauspost/compress v1.18.0 // indirect + github.com/klauspost/cpuid/v2 v2.2.11 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/minio/crc64nvme v1.0.2 // indirect + github.com/minio/md5-simd v1.1.2 // indirect + github.com/minio/minio-go/v7 v7.0.95 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/philhofer/fwd v1.2.0 // indirect + github.com/prometheus/client_model v0.6.2 // indirect + github.com/prometheus/common v0.66.1 // indirect + github.com/prometheus/procfs v0.16.1 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + github.com/rs/xid v1.6.0 // indirect + github.com/tinylib/msgp v1.3.0 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + go.uber.org/dig v1.19.0 // indirect + go.uber.org/multierr v1.10.0 // indirect + go.yaml.in/yaml/v2 v2.4.2 // indirect + golang.org/x/net v0.51.0 // indirect + golang.org/x/sync v0.20.0 // indirect + golang.org/x/sys v0.42.0 // indirect + golang.org/x/text v0.35.0 // indirect + google.golang.org/protobuf v1.36.8 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e74f2fc --- /dev/null +++ b/go.sum @@ -0,0 +1,128 @@ +github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= +github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= +github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/gofiber/fiber/v2 v2.52.12 h1:0LdToKclcPOj8PktUdIKo9BUohjjwfnQl42Dhw8/WUw= +github.com/gofiber/fiber/v2 v2.52.12/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.9.1 h1:uwrxJXBnx76nyISkhr33kQLlUqjv7et7b9FjCen/tdc= +github.com/jackc/pgx/v5 v5.9.1/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= +github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.11 h1:0OwqZRYI2rFrjS4kvkDnqJkKHdHaRnCm68/DY4OxRzU= +github.com/klauspost/cpuid/v2 v2.2.11/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/minio/crc64nvme v1.0.2 h1:6uO1UxGAD+kwqWWp7mBFsi5gAse66C4NXO8cmcVculg= +github.com/minio/crc64nvme v1.0.2/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg= +github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= +github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= +github.com/minio/minio-go/v7 v7.0.95 h1:ywOUPg+PebTMTzn9VDsoFJy32ZuARN9zhB+K3IYEvYU= +github.com/minio/minio-go/v7 v7.0.95/go.mod h1:wOOX3uxS334vImCNRVyIDdXX9OsXDm89ToynKgqUKlo= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM= +github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o= +github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg= +github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk= +github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE= +github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs= +github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA= +github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg= +github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is= +github.com/redis/go-redis/v9 v9.17.2 h1:P2EGsA4qVIM3Pp+aPocCJ7DguDHhqrXNhVcEp4ViluI= +github.com/redis/go-redis/v9 v9.17.2/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/tinylib/msgp v1.3.0 h1:ULuf7GPooDaIlbyvgAxBV/FI7ynli6LZ1/nVUNu+0ww= +github.com/tinylib/msgp v1.3.0/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= +github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +go.lab260.ru/fielderr v0.0.0-20260609150058-2f9540b99e41 h1:kHFUC/1OnQ05ShJW1wjCAbrSa/XxylAgEnZ75BholE4= +go.lab260.ru/fielderr v0.0.0-20260609150058-2f9540b99e41/go.mod h1:OQKs2tIkkTxUjzewIMsqChKuHPWDjIgUz+SYqdhR9rM= +go.uber.org/dig v1.19.0 h1:BACLhebsYdpQ7IROQ1AGPjrXcP5dF80U3gKoFzbaq/4= +go.uber.org/dig v1.19.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE= +go.uber.org/fx v1.24.0 h1:wE8mruvpg2kiiL1Vqd0CC+tr0/24XIB10Iwp2lLWzkg= +go.uber.org/fx v1.24.0/go.mod h1:AmDeGyS+ZARGKM4tlH4FY2Jr63VjbEDJHtqXTGP5hbo= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc= +go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI= +go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU= +golang.org/x/crypto v0.49.0 h1:+Ng2ULVvLHnJ/ZFEq4KdcDd/cfjrrjjNSXNzxg0Y4U4= +golang.org/x/crypto v0.49.0/go.mod h1:ErX4dUh2UM+CFYiXZRTcMpEcN8b/1gxEuv3nODoYtCA= +golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo= +golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y= +golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4= +golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo= +golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8= +golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA= +google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc= +google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/admission/distributed.go b/internal/admission/distributed.go new file mode 100644 index 0000000..c268f97 --- /dev/null +++ b/internal/admission/distributed.go @@ -0,0 +1,18 @@ +package admission + +import ( + "context" + "errors" +) + +// ErrRate means the request exhausted a shared rate budget. +var ErrRate = errors.New("shared request rate exceeded") + +// ErrSlots means the request exhausted a shared concurrency budget. +var ErrSlots = errors.New("shared execution slots exhausted") + +// Distributed defines request admission independently of the backing database. +type Distributed interface { + // Enter reserves global/group or user/group budgets and returns a release operation. + Enter(ctx context.Context, group, user string) (func(), error) +} diff --git a/internal/admission/limiter.go b/internal/admission/limiter.go new file mode 100644 index 0000000..97e7f2a --- /dev/null +++ b/internal/admission/limiter.go @@ -0,0 +1,73 @@ +// Package admission bounds request rate and concurrent execution independently of transport. +package admission + +import ( + "fmt" + "sync" + "time" +) + +// Options defines the rate and execution bounds for one limiter. +type Options struct { + // MaxInFlight limits concurrent executions without an admission queue. + MaxInFlight int + // RequestsPerSecond is the token refill rate. + RequestsPerSecond int + // Burst is the maximum number of immediately available tokens. + Burst int +} + +// Limiter owns synchronization for the token bucket and execution slots. +// It knows neither HTTP response codes nor application shutdown state. +type Limiter struct { + mu sync.Mutex + rate float64 + burst float64 + tokens float64 + lastRefill time.Time + slots chan struct{} +} + +// New validates limits and creates a full token bucket with bounded execution slots. +func New(cfg Options) (*Limiter, error) { + if cfg.MaxInFlight <= 0 || cfg.RequestsPerSecond <= 0 || cfg.Burst <= 0 { + return nil, fmt.Errorf("admission limits must be positive") + } + return &Limiter{ + rate: float64(cfg.RequestsPerSecond), + burst: float64(cfg.Burst), + tokens: float64(cfg.Burst), + lastRefill: time.Now(), + slots: make(chan struct{}, cfg.MaxInFlight), + }, nil +} + +// Allow refills the token bucket and consumes a token when one is available. +func (limiter *Limiter) Allow(now time.Time) bool { + limiter.mu.Lock() + defer limiter.mu.Unlock() + elapsed := max(0, now.Sub(limiter.lastRefill).Seconds()) + limiter.tokens = min(limiter.burst, limiter.tokens+elapsed*limiter.rate) + limiter.lastRefill = now + if limiter.tokens < 1 { + return false + } + limiter.tokens-- + return true +} + +// Acquire reserves an execution slot without waiting. +func (limiter *Limiter) Acquire() bool { + select { + case limiter.slots <- struct{}{}: + return true + default: + return false + } +} + +// Release returns an execution slot previously reserved by Acquire. +// Each successful Acquire must be paired with exactly one Release. +func (limiter *Limiter) Release() { + <-limiter.slots +} diff --git a/internal/admission/limiter_test.go b/internal/admission/limiter_test.go new file mode 100644 index 0000000..9fcb7e4 --- /dev/null +++ b/internal/admission/limiter_test.go @@ -0,0 +1,148 @@ +package admission + +import ( + "testing" + "testing/synctest" + "time" +) + +// TestLimiter_Allow checks token exhaustion and refill with deterministic time. +func TestLimiter_Allow(t *testing.T) { + cases := []struct { + name string + elapsed time.Duration + want bool + }{ + {"Negative: exhausted bucket", 0, false}, + {"Negative: partial token", time.Millisecond * 500, false}, + {"Positive: refilled token", time.Second, true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + // Arrange. + limiter := newTestLimiter(t, Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 1}) + if !limiter.Allow(time.Now()) { + t.Fatal("fixture token missing") + } + time.Sleep(tc.elapsed) + + // Act. + got := limiter.Allow(time.Now()) + + // Assert. + if got != tc.want { + t.Fatalf("Allow after %s=%v, want=%v", tc.elapsed, got, tc.want) + } + }) + }) + } +} + +// TestLimiter_Acquire checks available, occupied and released execution slots independently. +func TestLimiter_Acquire(t *testing.T) { + t.Run("Positive: free slot", func(t *testing.T) { + // Arrange. + limiter := newTestLimiter(t, Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 1}) + + want := true + + // Act. + got := limiter.Acquire() + if got { + t.Cleanup(limiter.Release) + } + + // Assert. + if got != want { + t.Fatalf("Acquire=%v, want=%v", got, want) + } + }) + + t.Run("Negative: occupied slot", func(t *testing.T) { + // Arrange. + limiter := newTestLimiter(t, Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 1}) + + if !limiter.Acquire() { + t.Fatal("fixture slot unavailable") + } + + t.Cleanup(limiter.Release) + + want := false + + // Act. + got := limiter.Acquire() + if got { + t.Cleanup(limiter.Release) + } + + // Assert. + if got != want { + t.Fatalf("Acquire=%v, want=%v", got, want) + } + }) + + t.Run("Positive: released slot", func(t *testing.T) { + // Arrange. + limiter := newTestLimiter(t, Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 1}) + + if !limiter.Acquire() { + t.Fatal("fixture slot unavailable") + } + + limiter.Release() + + want := true + + // Act. + got := limiter.Acquire() + if got { + t.Cleanup(limiter.Release) + } + + // Assert. + if got != want { + t.Fatalf("Acquire=%v, want=%v", got, want) + } + }) +} + +// newTestLimiter creates a limiter or fails the test on invalid settings. +func newTestLimiter(t *testing.T, options Options) *Limiter { + t.Helper() + limiter, err := New(options) + if err != nil { + t.Fatal(err) + } + return limiter +} + +// TestLimiter_New rejects each nonpositive limit independently. +func TestLimiter_New(t *testing.T) { + cases := []struct { + name string + options Options + }{ + {"Negative: zero concurrency", Options{MaxInFlight: 0, RequestsPerSecond: 1, Burst: 1}}, + {"Negative: negative concurrency", Options{MaxInFlight: -1, RequestsPerSecond: 1, Burst: 1}}, + {"Negative: zero refill rate", Options{MaxInFlight: 1, RequestsPerSecond: 0, Burst: 1}}, + {"Negative: negative refill rate", Options{MaxInFlight: 1, RequestsPerSecond: -1, Burst: 1}}, + {"Negative: zero burst", Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 0}}, + {"Negative: negative burst", Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: -1}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + options := tc.options + + // Act. + limiter, err := New(options) + + // Assert. + if err == nil || limiter != nil { + t.Fatalf("invalid options accepted: %v, %v", limiter, err) + } + }) + } +} diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..339faac --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,53 @@ +// Package config loads and validates the environment configuration of the application. +package config + +import "fmt" + +// Config groups settings by the component that consumes them. +type Config struct { + // HTTP configures the listener, request limits and shutdown deadline. + HTTP *Controller + // Memory configures the capacity of the process-local store. + Memory *Memory + // Logger configures structured process logging. + Logger *Logger +} + +// Default returns independent configuration sections with the documented defaults. +func Default() *Config { + return &Config{ + HTTP: defaultController(), + Memory: defaultMemory(), + Logger: &Logger{Level: "info"}, + } +} + +// New loads environment overrides and rejects invalid settings before startup. +func New() (*Config, error) { + cfg := Default() + if err := cfg.HTTP.loadEnvironment(); err != nil { + return nil, err + } + if err := cfg.Memory.loadEnvironment(); err != nil { + return nil, err + } + cfg.Logger.loadEnvironment() + if err := cfg.Validate(); err != nil { + return nil, err + } + return cfg, nil +} + +// Validate verifies the presence and invariants of each configuration section. +func (cfg *Config) Validate() error { + if cfg == nil || cfg.HTTP == nil || cfg.Memory == nil || cfg.Logger == nil { + return fmt.Errorf("HTTP, Memory and Logger configuration sections are required") + } + if err := cfg.HTTP.Validate(); err != nil { + return err + } + if err := cfg.Memory.Validate(); err != nil { + return err + } + return cfg.Logger.Validate() +} diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000..8396991 --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,241 @@ +package config + +import "testing" + +// TestConfig_New checks individual environment overrides and invalid settings. +func TestConfig_New(t *testing.T) { + t.Run("Positive: APP_PORT override", func(t *testing.T) { + // Arrange. + t.Setenv("APP_PORT", "18080") + + // Act. + cfg, err := New() + + // Assert. + if err != nil { + t.Fatal(err) + } + if cfg.HTTP.Port != 18080 { + t.Fatalf("APP_PORT=%v, want=%v", cfg.HTTP.Port, 18080) + } + }) + t.Run("Positive: REQUEST_TIMEOUT_SECONDS override", func(t *testing.T) { + // Arrange. + t.Setenv("REQUEST_TIMEOUT_SECONDS", "4") + + // Act. + cfg, err := New() + + // Assert. + if err != nil { + t.Fatal(err) + } + if cfg.HTTP.TimeoutSeconds != 4 { + t.Fatalf("REQUEST_TIMEOUT_SECONDS=%v, want=%v", cfg.HTTP.TimeoutSeconds, 4) + } + }) + t.Run("Positive: MAX_MESSAGES override", func(t *testing.T) { + // Arrange. + t.Setenv("MAX_MESSAGES", "25") + + // Act. + cfg, err := New() + + // Assert. + if err != nil { + t.Fatal(err) + } + if cfg.Memory.MaxMessages != 25 { + t.Fatalf("MAX_MESSAGES=%v, want=%v", cfg.Memory.MaxMessages, 25) + } + }) + t.Run("Positive: REQUESTS_PER_SECOND override", func(t *testing.T) { + // Arrange. + t.Setenv("REQUESTS_PER_SECOND", "12") + + // Act. + cfg, err := New() + + // Assert. + if err != nil { + t.Fatal(err) + } + if cfg.HTTP.Admission.RequestsPerSecond != 12 { + t.Fatalf("REQUESTS_PER_SECOND=%v, want=%v", cfg.HTTP.Admission.RequestsPerSecond, 12) + } + }) + t.Run("Positive: LOG_LEVEL override", func(t *testing.T) { + // Arrange. + t.Setenv("LOG_LEVEL", "debug") + + // Act. + cfg, err := New() + + // Assert. + if err != nil { + t.Fatal(err) + } + if cfg.Logger.Level != "debug" { + t.Fatalf("LOG_LEVEL=%v, want=%v", cfg.Logger.Level, "debug") + } + }) + cases := []struct { + name string + key string + value string + }{ + {"port exceeds TCP range", "APP_PORT", "65536"}, + {"zero message capacity", "MAX_MESSAGES", "0"}, + {"nonnumeric body limit", "MAX_BODY_BYTES", "bad"}, + {"shutdown deadline exceeds bound", "SHUTDOWN_TIMEOUT_SECONDS", "10"}, + {"hostname instead of bind IP", "APP_HOST", "hostname"}, + } + for _, tc := range cases { + t.Run("Negative: "+tc.name, func(t *testing.T) { + // Arrange. + t.Setenv(tc.key, tc.value) + + // Act. + cfg, err := New() + + // Assert. + if err == nil || cfg != nil { + t.Fatalf("invalid %s=%q returned config=%+v, error=%v", tc.key, tc.value, cfg, err) + } + }) + } +} + +// TestConfig_Validate rejects one missing section or invalid component per scenario. +func TestConfig_Validate(t *testing.T) { + t.Run("Negative: nil config", func(t *testing.T) { + // Arrange. + var cfg *Config + + // Act. + err := cfg.Validate() + + // Assert. + if err == nil { + t.Fatalf("accepted %s", "nil config") + } + }) + + t.Run("Negative: missing HTTP", func(t *testing.T) { + // Arrange. + cfg := Default() + + cfg.HTTP = nil + + // Act. + err := cfg.Validate() + + // Assert. + if err == nil { + t.Fatalf("accepted %s", "missing HTTP") + } + }) + + t.Run("Negative: missing Memory", func(t *testing.T) { + // Arrange. + cfg := Default() + + cfg.Memory = nil + + // Act. + err := cfg.Validate() + + // Assert. + if err == nil { + t.Fatalf("accepted %s", "missing Memory") + } + }) + + t.Run("Negative: missing Logger", func(t *testing.T) { + // Arrange. + cfg := Default() + + cfg.Logger = nil + + // Act. + err := cfg.Validate() + + // Assert. + if err == nil { + t.Fatalf("accepted %s", "missing Logger") + } + }) + + t.Run("Negative: unknown log level", func(t *testing.T) { + // Arrange. + cfg := Default() + + cfg.Logger.Level = "unknown" + + // Act. + err := cfg.Validate() + + // Assert. + if err == nil { + t.Fatalf("accepted %s", "unknown log level") + } + }) + + t.Run("Negative: zero storage capacity", func(t *testing.T) { + // Arrange. + cfg := Default() + + cfg.Memory.MaxUsers = 0 + + // Act. + err := cfg.Validate() + + // Assert. + if err == nil { + t.Fatalf("accepted %s", "zero storage capacity") + } + }) + + t.Run("Negative: zero concurrency", func(t *testing.T) { + // Arrange. + cfg := Default() + + cfg.HTTP.Admission.MaxInFlight = 0 + + // Act. + err := cfg.Validate() + + // Assert. + if err == nil { + t.Fatalf("accepted %s", "zero concurrency") + } + }) +} + +// TestLogger_Validate rejects an absent logging section without panicking. +func TestLogger_Validate(t *testing.T) { + // Arrange. + var cfg *Logger + + // Act. + err := cfg.Validate() + + // Assert. + if err == nil { + t.Fatal("nil logger accepted") + } +} + +// TestMemory_Validate rejects absent storage settings without panicking. +func TestMemory_Validate(t *testing.T) { + // Arrange. + var cfg *Memory + + // Act. + err := cfg.Validate() + + // Assert. + if err == nil { + t.Fatal("nil memory configuration accepted") + } +} diff --git a/internal/config/controller.go b/internal/config/controller.go new file mode 100644 index 0000000..d1538dd --- /dev/null +++ b/internal/config/controller.go @@ -0,0 +1,160 @@ +package config + +import ( + "fmt" + "net" + "os" + "strconv" + "time" +) + +const ( + maxTCPPort = 65535 + maxShutdownTimeoutSeconds = 9 + maxRequestTimeoutSeconds = 15 +) + +// Controller contains settings owned exclusively by the HTTP transport. +type Controller struct { + // InstanceID identifies this API process to the load balancer diagnostics. + InstanceID string + // Host is the IP address on which the listener binds. + Host string + // Port is the TCP port, in the range 1..65535. + Port int + // TimeoutSeconds limits request reads, response writes and idle connections. + TimeoutSeconds int + // ShutdownTimeoutSeconds bounds draining of active requests; it must not exceed nine seconds. + ShutdownTimeoutSeconds int + // MaxBodyBytes is the maximum accepted request body size before decoding. + MaxBodyBytes int + // MaxConnections bounds simultaneous connections handled by fasthttp. + MaxConnections int + // Admission controls business request throughput and execution concurrency. + Admission Admission +} + +// Admission defines shared request policies; health endpoints bypass them. +type Admission struct { + // MaxInFlight is the maximum number of executing requests, with no waiting queue. + MaxInFlight int + // RequestsPerSecond is the token refill rate for the global admission bucket. + RequestsPerSecond int + // Burst is the bucket capacity and maximum immediate request burst. + Burst int + // AuthRate limits public registration/login attempts per second. + AuthRate int + // AuthSlots bounds concurrent password KDF work globally. + AuthSlots int + // UserRate limits each authenticated account across all groups. + UserRate int + // UserSlots bounds concurrency across one account's groups. + UserSlots int + // UserWriteRate limits one account's write attempts per second. + UserWriteRate int + // UserWriteSlots bounds one account's concurrent writes. + UserWriteSlots int + // UserAuthRate limits session-management operations per account. + UserAuthRate int + // UserAuthSlots bounds one account's session-management concurrency. + UserAuthSlots int +} + +// defaultController returns the default listener, timeout and admission settings. +func defaultController() *Controller { + return &Controller{ + Host: "0.0.0.0", + Port: 8080, + TimeoutSeconds: 3, + ShutdownTimeoutSeconds: 5, + MaxBodyBytes: 10 << 20, + MaxConnections: 16, + Admission: Admission{ + MaxInFlight: 32, + RequestsPerSecond: 1000, + Burst: 100, + AuthRate: 100, + AuthSlots: 4, + UserRate: 100, + UserSlots: 8, + UserWriteRate: 50, + UserWriteSlots: 4, + UserAuthRate: 20, + UserAuthSlots: 2, + }, + } +} + +// loadEnvironment applies environment overrides to this configuration section. +func (cfg *Controller) loadEnvironment() error { + cfg.InstanceID = os.Getenv("INSTANCE_ID") + if host, exists := os.LookupEnv("APP_HOST"); exists { + cfg.Host = host + } + return loadIntegers( + integerSetting{"APP_PORT", &cfg.Port}, + integerSetting{"REQUEST_TIMEOUT_SECONDS", &cfg.TimeoutSeconds}, + integerSetting{"SHUTDOWN_TIMEOUT_SECONDS", &cfg.ShutdownTimeoutSeconds}, + integerSetting{"MAX_BODY_BYTES", &cfg.MaxBodyBytes}, + integerSetting{"MAX_CONNECTIONS", &cfg.MaxConnections}, + integerSetting{"MAX_IN_FLIGHT", &cfg.Admission.MaxInFlight}, + integerSetting{"REQUESTS_PER_SECOND", &cfg.Admission.RequestsPerSecond}, + integerSetting{"REQUEST_BURST", &cfg.Admission.Burst}, + integerSetting{"AUTH_RATE", &cfg.Admission.AuthRate}, + integerSetting{"AUTH_SLOTS", &cfg.Admission.AuthSlots}, + integerSetting{"USER_RATE", &cfg.Admission.UserRate}, + integerSetting{"USER_SLOTS", &cfg.Admission.UserSlots}, + integerSetting{"USER_WRITE_RATE", &cfg.Admission.UserWriteRate}, + integerSetting{"USER_WRITE_SLOTS", &cfg.Admission.UserWriteSlots}, + integerSetting{"USER_AUTH_RATE", &cfg.Admission.UserAuthRate}, + integerSetting{"USER_AUTH_SLOTS", &cfg.Admission.UserAuthSlots}, + ) +} + +// Validate checks listener addresses, timeouts and admission bounds. +func (cfg *Controller) Validate() error { + if cfg == nil { + return fmt.Errorf("HTTP configuration is required") + } + if cfg.TimeoutSeconds > maxRequestTimeoutSeconds { + return fmt.Errorf("REQUEST_TIMEOUT_SECONDS must not exceed %d", maxRequestTimeoutSeconds) + } + if net.ParseIP(cfg.Host) == nil { + return fmt.Errorf("APP_HOST must be an IP address") + } + if cfg.Port < 1 || cfg.Port > maxTCPPort { + return fmt.Errorf("APP_PORT must be 1..%d", maxTCPPort) + } + if cfg.ShutdownTimeoutSeconds < 1 || cfg.ShutdownTimeoutSeconds > maxShutdownTimeoutSeconds { + return fmt.Errorf("SHUTDOWN_TIMEOUT_SECONDS must be 1..%d", maxShutdownTimeoutSeconds) + } + settings := []integerSetting{ + {"REQUEST_TIMEOUT_SECONDS", &cfg.TimeoutSeconds}, + {"MAX_BODY_BYTES", &cfg.MaxBodyBytes}, + {"MAX_CONNECTIONS", &cfg.MaxConnections}, + {"MAX_IN_FLIGHT", &cfg.Admission.MaxInFlight}, + {"REQUESTS_PER_SECOND", &cfg.Admission.RequestsPerSecond}, + {"REQUEST_BURST", &cfg.Admission.Burst}, + } + for _, setting := range settings { + if err := validatePositive(setting.name, *setting.target); err != nil { + return err + } + } + return nil +} + +// Bind returns the listener address, including brackets for IPv6 hosts. +func (cfg *Controller) Bind() string { + return net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port)) +} + +// Timeout returns the configured read, write and idle timeout. +func (cfg *Controller) Timeout() time.Duration { + return time.Duration(cfg.TimeoutSeconds) * time.Second +} + +// ShutdownTimeout returns the maximum time allowed to drain active requests. +func (cfg *Controller) ShutdownTimeout() time.Duration { + return time.Duration(cfg.ShutdownTimeoutSeconds) * time.Second +} diff --git a/internal/config/environment.go b/internal/config/environment.go new file mode 100644 index 0000000..db5dfd8 --- /dev/null +++ b/internal/config/environment.go @@ -0,0 +1,41 @@ +package config + +import ( + "fmt" + "os" + "strconv" +) + +const maxConfiguredInteger = 1 << 30 + +type integerSetting struct { + name string + target *int +} + +// loadIntegers parses configured integer overrides and reports invalid values. +func loadIntegers(settings ...integerSetting) error { + for _, setting := range settings { + raw, exists := os.LookupEnv(setting.name) + if !exists { + continue + } + value, err := strconv.Atoi(raw) + if err != nil { + return fmt.Errorf("%s must be an integer", setting.name) + } + if err := validatePositive(setting.name, value); err != nil { + return err + } + *setting.target = value + } + return nil +} + +// validatePositive checks that a setting is positive and within the supported range. +func validatePositive(name string, value int) error { + if value < 1 || value > maxConfiguredInteger { + return fmt.Errorf("%s must be between 1 and %d", name, maxConfiguredInteger) + } + return nil +} diff --git a/internal/config/logger.go b/internal/config/logger.go new file mode 100644 index 0000000..58f2450 --- /dev/null +++ b/internal/config/logger.go @@ -0,0 +1,32 @@ +package config + +import ( + "fmt" + "os" +) + +// Logger configures the minimum severity written as structured JSON to stderr. +type Logger struct { + // Level is one of debug, info, warn or error; the default is info. + Level string +} + +// loadEnvironment applies environment overrides to this configuration section. +func (cfg *Logger) loadEnvironment() { + if level, exists := os.LookupEnv("LOG_LEVEL"); exists { + cfg.Level = level + } +} + +// Validate rejects unsupported levels rather than silently changing verbosity. +func (cfg *Logger) Validate() error { + if cfg == nil { + return fmt.Errorf("Logger configuration is required") + } + switch cfg.Level { + case "debug", "info", "warn", "error": + return nil + default: + return fmt.Errorf("LOG_LEVEL must be debug, info, warn or error") + } +} diff --git a/internal/config/memory.go b/internal/config/memory.go new file mode 100644 index 0000000..401e67d --- /dev/null +++ b/internal/config/memory.go @@ -0,0 +1,54 @@ +package config + +import "fmt" + +// Memory defines logical capacity limits; MaxStateBytes is not a measurement of RSS. +type Memory struct { + // MaxUsers limits the number of registered users. + MaxUsers int + // MaxChats limits the number of immutable chats. + MaxChats int + // MaxMessages limits the total number of messages across all chats. + MaxMessages int + // MaxStateBytes limits the logical storage budget, including estimated record overhead. + MaxStateBytes int +} + +// defaultMemory returns the default storage capacity limits. +func defaultMemory() *Memory { + return &Memory{ + MaxUsers: 10000, + MaxChats: 10000, + MaxMessages: 100000, + MaxStateBytes: 32 << 20, + } +} + +// loadEnvironment applies environment overrides to this configuration section. +func (cfg *Memory) loadEnvironment() error { + return loadIntegers( + integerSetting{"MAX_USERS", &cfg.MaxUsers}, + integerSetting{"MAX_CHATS", &cfg.MaxChats}, + integerSetting{"MAX_MESSAGES", &cfg.MaxMessages}, + integerSetting{"MAX_STATE_BYTES", &cfg.MaxStateBytes}, + ) +} + +// Validate rejects disabled, negative and excessively large storage limits. +func (cfg *Memory) Validate() error { + if cfg == nil { + return fmt.Errorf("Memory configuration is required") + } + settings := []integerSetting{ + {"MAX_USERS", &cfg.MaxUsers}, + {"MAX_CHATS", &cfg.MaxChats}, + {"MAX_MESSAGES", &cfg.MaxMessages}, + {"MAX_STATE_BYTES", &cfg.MaxStateBytes}, + } + for _, setting := range settings { + if err := validatePositive(setting.name, *setting.target); err != nil { + return err + } + } + return nil +} diff --git a/internal/config/objectstore.go b/internal/config/objectstore.go new file mode 100644 index 0000000..169d9cd --- /dev/null +++ b/internal/config/objectstore.go @@ -0,0 +1,35 @@ +package config + +import ( + "fmt" + "os" +) + +// ObjectStore contains only the S3 adapter's connection settings. +type ObjectStore struct { + // Endpoint is host:port, without a URL scheme. + Endpoint string + // Bucket is the private application bucket. + Bucket string + // AccessKey is the restricted service identity. + AccessKey string + // SecretKey is supplied by the runtime secret provider. + SecretKey string + // Secure enables certificate-verified TLS. + Secure bool +} + +// NewObjectStore validates S3 settings independently of their use. +func NewObjectStore() (*ObjectStore, error) { + cfg := &ObjectStore{ + Endpoint: os.Getenv("S3_ENDPOINT"), + Bucket: os.Getenv("S3_BUCKET"), + AccessKey: os.Getenv("S3_ACCESS_KEY"), + SecretKey: os.Getenv("S3_SECRET_KEY"), + Secure: os.Getenv("S3_TLS") != "false", + } + if cfg.Endpoint == "" || cfg.Bucket == "" || cfg.AccessKey == "" || cfg.SecretKey == "" { + return nil, fmt.Errorf("S3 endpoint, bucket and scoped credentials required") + } + return cfg, nil +} diff --git a/internal/config/objectstore_test.go b/internal/config/objectstore_test.go new file mode 100644 index 0000000..8836738 --- /dev/null +++ b/internal/config/objectstore_test.go @@ -0,0 +1,24 @@ +package config + +import "testing" + +// TestObjectStoreConfiguration verifies mandatory credentials and opt-in local plaintext. +func TestObjectStoreConfiguration(t *testing.T) { + t.Run("missing credentials", func(t *testing.T) { + t.Setenv("S3_ENDPOINT", "") + if _, err := NewObjectStore(); err == nil { + t.Fatal("missing endpoint accepted") + } + }) + t.Run("local configuration", func(t *testing.T) { + t.Setenv("S3_ENDPOINT", "localhost:9000") + t.Setenv("S3_BUCKET", "attachments") + t.Setenv("S3_ACCESS_KEY", "test") + t.Setenv("S3_SECRET_KEY", "synthetic-only") + t.Setenv("S3_TLS", "false") + cfg, err := NewObjectStore() + if err != nil || cfg.Secure || cfg.Bucket != "attachments" { + t.Fatalf("invalid result: %v", err) + } + }) +} diff --git a/internal/config/persistence.go b/internal/config/persistence.go new file mode 100644 index 0000000..6950d4d --- /dev/null +++ b/internal/config/persistence.go @@ -0,0 +1,55 @@ +package config + +import ( + "fmt" + "os" + "strings" +) + +// Persistence contains database, cache and session settings. +type Persistence struct { + // CursorKey is the shared base64 signing key supplied through a secret. + CursorKey string + // DatabaseURL identifies the PostgreSQL database. + DatabaseURL string + // Shards maps stable identities to Valkey addresses. + Shards map[string]string + // Coordinator names the stable global admission shard. + Coordinator string + // PoolSize bounds connections per dependency. + PoolSize int + // TimeoutMilliseconds bounds each dependency operation. + TimeoutMilliseconds int + // SessionTTLSeconds sets the absolute session lifetime. + SessionTTLSeconds int +} + +// NewPersistence loads dependency settings separately from HTTP configuration. +func NewPersistence() (*Persistence, error) { + cfg := &Persistence{ + CursorKey: os.Getenv("CURSOR_SIGNING_KEY"), + DatabaseURL: os.Getenv("DATABASE_URL"), + Coordinator: os.Getenv("VALKEY_COORDINATOR"), + Shards: make(map[string]string), + PoolSize: 8, + TimeoutMilliseconds: 1000, + SessionTTLSeconds: 3600, + } + if cfg.Coordinator == "" { + cfg.Coordinator = "shard0" + } + for _, entry := range strings.Split(os.Getenv("VALKEY_SHARDS"), ",") { + id, address, ok := strings.Cut(entry, "=") + if !ok || id == "" || address == "" || cfg.Shards[id] != "" { + return nil, fmt.Errorf("VALKEY_SHARDS requires unique id=address entries") + } + cfg.Shards[id] = address + } + if cfg.DatabaseURL == "" || cfg.Shards[cfg.Coordinator] == "" { + return nil, fmt.Errorf("DATABASE_URL and a configured VALKEY_COORDINATOR are required") + } + if err := loadIntegers(integerSetting{"DEPENDENCY_POOL_SIZE", &cfg.PoolSize}, integerSetting{"DEPENDENCY_TIMEOUT_MS", &cfg.TimeoutMilliseconds}, integerSetting{"SESSION_TTL_SECONDS", &cfg.SessionTTLSeconds}); err != nil { + return nil, err + } + return cfg, nil +} diff --git a/internal/config/persistence_test.go b/internal/config/persistence_test.go new file mode 100644 index 0000000..6f9b63f --- /dev/null +++ b/internal/config/persistence_test.go @@ -0,0 +1,34 @@ +package config + +import "testing" + +// TestPersistenceConfiguration validates independent environment configurations. +func TestPersistenceConfiguration(t *testing.T) { + cases := []struct { + name string + database string + shards string + coordinator string + ttl string + valid bool + }{ + {"defaults", "postgres://localhost/test", "shard0=localhost:6379", "", "3600", true}, + {"missing database", "", "shard0=localhost:6379", "", "3600", false}, + {"missing coordinator", "postgres://localhost/test", "shard1=localhost:6379", "shard0", "3600", false}, + {"duplicate shard", "postgres://localhost/test", "shard0=a,shard0=b", "", "3600", false}, + {"invalid topology", "postgres://localhost/test", "wrong", "", "3600", false}, + {"invalid ttl", "postgres://localhost/test", "shard0=a", "", "no", false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Setenv("DATABASE_URL", tc.database) + t.Setenv("VALKEY_SHARDS", tc.shards) + t.Setenv("VALKEY_COORDINATOR", tc.coordinator) + t.Setenv("SESSION_TTL_SECONDS", tc.ttl) + _, err := NewPersistence() + if (err == nil) != tc.valid { + t.Fatalf("unexpected validation result: %v", err) + } + }) + } +} diff --git a/internal/controller/fx.go b/internal/controller/fx.go new file mode 100644 index 0000000..e99b226 --- /dev/null +++ b/internal/controller/fx.go @@ -0,0 +1,8 @@ +package controller + +import "go.uber.org/fx" + +// RunControllerFx lets Fx own startup, signal handling and graceful shutdown. +func RunControllerFx(lc fx.Lifecycle, ctrl Controller) { + lc.Append(fx.Hook{OnStart: ctrl.Start, OnStop: ctrl.Stop}) +} diff --git a/internal/controller/http/fiber/attachments.go b/internal/controller/http/fiber/attachments.go new file mode 100644 index 0000000..e665473 --- /dev/null +++ b/internal/controller/http/fiber/attachments.go @@ -0,0 +1,112 @@ +package fiber + +import ( + "context" + "io" + "net/http" + "time" + + "github.com/gofiber/fiber/v2" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// Attachments defines file operations required by the HTTP boundary. +type Attachments interface { + // Upload validates and publishes an original. + Upload(context.Context, string, string, string, string, []byte) (dto.Attachment, error) + // Get authorizes access to attachment metadata. + Get(context.Context, string, string) (dto.Attachment, error) + // Download returns an authorized original stream. + Download(context.Context, string, string, string) (dto.Attachment, io.ReadCloser, error) +} + +// WithAttachments binds the file service during composition before server startup. +func (ctrl *Controller) WithAttachments(attachments Attachments) *Controller { + ctrl.attachments = attachments + return ctrl +} + +type attachmentResponse struct { + // ID identifies the uploaded file. + ID string `json:"id"` + // ChatID defines the chat authorization scope. + ChatID string `json:"chat_id"` + // OwnerID identifies the original uploader. + OwnerID string `json:"owner_id"` + // Filename is the validated display name. + Filename string `json:"filename"` + // ContentType describes the verified original format. + ContentType string `json:"content_type"` + // SizeBytes is the encoded original length. + SizeBytes int64 `json:"size_bytes"` + // SHA256 is the original lowercase checksum. + SHA256 string `json:"sha256"` + // Status is the durable publication state. + Status string `json:"status"` + // Variants lists generated variants, empty in lab4. + Variants []string `json:"variants"` + // ErrorCode is null for a ready original. + ErrorCode *string `json:"error_code"` + // CreatedAt is the reservation time. + CreatedAt time.Time `json:"created_at"` + // UpdatedAt is the publication time. + UpdatedAt time.Time `json:"updated_at"` +} + +// attachmentResponseOf deliberately omits private S3 coordinates. +func attachmentResponseOf(a dto.Attachment) attachmentResponse { + return attachmentResponse{ + ID: a.ID.String(), + ChatID: a.ChatID.String(), + OwnerID: a.OwnerID.String(), + Filename: a.Filename, + ContentType: a.ContentType, + SizeBytes: a.SizeBytes, + SHA256: a.SHA256, + Status: a.Status, + Variants: []string{}, + CreatedAt: a.CreatedAt, + UpdatedAt: a.UpdatedAt, + } +} + +// handleUpload passes raw bounded bytes to application validation. +func (ctrl *Controller) handleUpload(ctx *fiber.Ctx) error { + if ctrl.attachments == nil { + return service.ErrUnavailable + } + a, err := ctrl.attachments.Upload(ctx.UserContext(), identity(ctx), ctx.Params("chat_id"), ctx.Query("filename"), ctx.Get("Content-Type"), ctx.Body()) + if err != nil { + return err + } + return ctx.Status(http.StatusCreated).JSON(attachmentResponseOf(a)) +} + +// handleAttachment returns only the public metadata of an accessible file. +func (ctrl *Controller) handleAttachment(ctx *fiber.Ctx) error { + if ctrl.attachments == nil { + return service.ErrUnavailable + } + a, err := ctrl.attachments.Get(ctx.UserContext(), identity(ctx), ctx.Params("id")) + if err != nil { + return err + } + return ctx.JSON(attachmentResponseOf(a)) +} + +// handleContent streams original bytes without redirecting around authorization. +func (ctrl *Controller) handleContent(ctx *fiber.Ctx) error { + if ctrl.attachments == nil { + return service.ErrUnavailable + } + a, reader, err := ctrl.attachments.Download(ctx.UserContext(), identity(ctx), ctx.Params("id"), ctx.Query("variant")) + if err != nil { + return err + } + ctx.Set("Content-Type", a.ContentType) + ctx.Set("X-Content-Type-Options", "nosniff") + return ctx.SendStream(&responseStream{Reader: reader, cleanup: func() { + }}, int(a.SizeBytes)) +} diff --git a/internal/controller/http/fiber/attachments_test.go b/internal/controller/http/fiber/attachments_test.go new file mode 100644 index 0000000..c87f6bb --- /dev/null +++ b/internal/controller/http/fiber/attachments_test.go @@ -0,0 +1,102 @@ +package fiber + +import ( + "bytes" + "context" + "io" + "net/http" + "net/http/httptest" + "testing" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +type attachmentFixture struct { + attachment dto.Attachment + body []byte + err error +} + +// Upload returns the configured application outcome for an HTTP mapping scenario. +func (f attachmentFixture) Upload(context.Context, string, string, string, string, []byte) (dto.Attachment, error) { + return f.attachment, f.err +} + +// Get returns the configured metadata outcome for an HTTP mapping scenario. +func (f attachmentFixture) Get(context.Context, string, string) (dto.Attachment, error) { + return f.attachment, f.err +} + +// Download returns a stream that fails if the request context is canceled before transmission. +func (f attachmentFixture) Download(ctx context.Context, _, _, _ string) (dto.Attachment, io.ReadCloser, error) { + return f.attachment, &contextReader{Reader: bytes.NewReader(f.body), ctx: ctx}, f.err +} + +type contextReader struct { + *bytes.Reader + ctx context.Context +} + +// Read verifies the admission context survives until the response is actually read. +func (r *contextReader) Read(p []byte) (int, error) { + if err := r.ctx.Err(); err != nil { + return 0, err + } + return r.Reader.Read(p) +} + +// Close makes the synthetic response stream an owned closer. +func (r *contextReader) Close() error { + return nil +} + +// TestAttachmentHTTPMapping exercises early errors, successful metadata and byte-exact streaming. +func TestAttachmentHTTPMapping(t *testing.T) { + cases := []struct { + name string + method string + path string + fixture *attachmentFixture + status int + }{ + {"upload unavailable", "POST", "/api/v1/chats/test/attachments", nil, http.StatusServiceUnavailable}, + {"metadata unavailable", "GET", "/api/v1/attachments/test", nil, http.StatusServiceUnavailable}, + {"download unavailable", "GET", "/api/v1/attachments/test/content", nil, http.StatusServiceUnavailable}, + {"upload too large", "POST", "/api/v1/chats/test/attachments", &attachmentFixture{err: service.ErrTooLarge}, http.StatusRequestEntityTooLarge}, + {"upload unsupported", "POST", "/api/v1/chats/test/attachments", &attachmentFixture{err: service.ErrMedia}, http.StatusUnsupportedMediaType}, + {"metadata missing", "GET", "/api/v1/attachments/test", &attachmentFixture{err: service.ErrNotFound}, http.StatusNotFound}, + {"download missing", "GET", "/api/v1/attachments/test/content", &attachmentFixture{err: service.ErrNotFound}, http.StatusNotFound}, + {"upload ready", "POST", "/api/v1/chats/test/attachments", &attachmentFixture{attachment: dto.Attachment{ID: uuid.New(), Status: "ready"}}, http.StatusCreated}, + {"metadata ready", "GET", "/api/v1/attachments/test", &attachmentFixture{attachment: dto.Attachment{ID: uuid.New(), Status: "ready"}}, http.StatusOK}, + {"download stream", "GET", "/api/v1/attachments/test/content", &attachmentFixture{attachment: dto.Attachment{SizeBytes: 4, ContentType: "image/png"}, body: []byte{0, 1, 2, 3}}, http.StatusOK}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + ctrl := testController(t, nil) + user := call(t, ctrl, http.MethodPost, "/api/v1/users", "", map[string]any{"username": "test_user", "display_name": "Test", "password": "synthetic-password"}, http.StatusCreated) + if tc.fixture != nil { + ctrl.WithAttachments(*tc.fixture) + } + req := httptest.NewRequest(tc.method, tc.path, bytes.NewReader([]byte("fixture"))) + req.Header.Set("Authorization", "Bearer "+user["id"].(string)) + req.Header.Set("Content-Type", "image/png") + response, err := ctrl.server.Test(req, 10000) + if err != nil { + t.Fatal(err) + } + body, err := io.ReadAll(response.Body) + if closeErr := response.Body.Close(); closeErr != nil { + t.Fatal(closeErr) + } + if err != nil || response.StatusCode != tc.status { + t.Fatalf("response status=%d err=%v", response.StatusCode, err) + } + if tc.fixture != nil && tc.fixture.body != nil && !bytes.Equal(body, tc.fixture.body) { + t.Fatalf("stream changed: %v", body) + } + }) + } +} diff --git a/internal/controller/http/fiber/auth.go b/internal/controller/http/fiber/auth.go new file mode 100644 index 0000000..df43d20 --- /dev/null +++ b/internal/controller/http/fiber/auth.go @@ -0,0 +1,76 @@ +package fiber + +import ( + "encoding/base64" + "net/http" + "strings" + + "github.com/gofiber/fiber/v2" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +const identityKey = "authenticated-user" + +// HandleLogin structurally decodes Basic credentials and delegates authentication. +func (ctrl *Controller) HandleLogin(ctx *fiber.Ctx) error { + scheme, encoded, ok := strings.Cut(ctx.Get(fiber.HeaderAuthorization), " ") + if !ok || !strings.EqualFold(scheme, "Basic") { + return service.ErrUnauthorized + } + decoded, err := base64.StdEncoding.DecodeString(encoded) + if err != nil { + return service.ErrUnauthorized + } + username, password, ok := strings.Cut(string(decoded), ":") + if !ok { + return service.ErrUnauthorized + } + if len(ctx.Body()) != 0 { + return service.ErrInvalid + } + session, err := ctrl.auth.Login(ctx.UserContext(), username, password) + if err != nil { + return err + } + return ctx.Status(http.StatusCreated).JSON(fiber.Map{ + "token": session.Token, + "token_type": "Bearer", + "expires_at": session.ExpiresAt, + "user": userResponse(session.User), + }) +} + +// HandleLogout revokes the current authenticated token before returning no content. +func (ctrl *Controller) HandleLogout(ctx *fiber.Ctx) error { + if err := ctrl.auth.Logout(ctx.UserContext(), bearer(ctx)); err != nil { + return err + } + return ctx.SendStatus(http.StatusNoContent) +} + +// bearer extracts a Bearer token without interpreting its identity or validity. +func bearer(ctx *fiber.Ctx) string { + scheme, token, ok := strings.Cut(ctx.Get(fiber.HeaderAuthorization), " ") + if !ok || !strings.EqualFold(scheme, "Bearer") { + return "" + } + return token +} + +// identity reads only the identity produced by successful session authentication. +func identity(ctx *fiber.Ctx) string { + value, _ := ctx.Locals(identityKey).(string) + return value +} + +// requestGroup selects a bounded policy group from the HTTP operation. +func requestGroup(ctx *fiber.Ctx) string { + if ctx.Path() == "/api/v1/users" || strings.HasPrefix(ctx.Path(), "/api/v1/auth/") { + return "auth" + } + if ctx.Method() == http.MethodGet { + return "read" + } + return "write" +} diff --git a/internal/controller/http/fiber/auth_test.go b/internal/controller/http/fiber/auth_test.go new file mode 100644 index 0000000..371a9ee --- /dev/null +++ b/internal/controller/http/fiber/auth_test.go @@ -0,0 +1,161 @@ +package fiber + +import ( + "context" + "encoding/base64" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +type sessionFixture struct { + fixtureAuthentication + login func(string, string) (dto.SessionDTO, error) + logoutError error +} + +// Login delegates the credentials decoded by the controller to the arranged scenario. +func (f sessionFixture) Login(_ context.Context, username, password string) (dto.SessionDTO, error) { + return f.login(username, password) +} + +// Logout returns the prescribed revocation outcome. +func (f sessionFixture) Logout(context.Context, string) error { + return f.logoutError +} + +// TestLoginHTTPChecksBasicEncoding verifies independent wire-level login scenarios. +func TestLoginHTTPChecksBasicEncoding(t *testing.T) { + cases := []struct { + name string + header string + body string + status int + }{ + {"missing header", "", "", http.StatusUnauthorized}, + {"wrong scheme", "Bearer abc", "", http.StatusUnauthorized}, + {"bad base64", "Basic %%%", "", http.StatusUnauthorized}, + {"no delimiter", "Basic " + base64.StdEncoding.EncodeToString([]byte("alice")), "", http.StatusUnauthorized}, + {"unexpected body", "Basic " + base64.StdEncoding.EncodeToString([]byte("alice:long-password:ok")), "{}", http.StatusBadRequest}, + {"valid colon in password", "Basic " + base64.StdEncoding.EncodeToString([]byte("alice:long-password:ok")), "", http.StatusCreated}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + ctrl := testController(t, nil) + ctrl.auth = sessionFixture{login: func(username, password string) (dto.SessionDTO, error) { + if username != "alice" || password != "long-password:ok" { + t.Fatal("credentials altered") + } + return dto.SessionDTO{Token: "opaque", ExpiresAt: time.Now().UTC().Add(time.Hour), User: dto.UserDTO{ID: uuid.New(), Username: "alice"}}, nil + }} + req := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", strings.NewReader(tc.body)) + req.Header.Set("Authorization", tc.header) + response, err := ctrl.server.Test(req) + if err != nil { + t.Fatal(err) + } + defer func() { + _ = response.Body.Close() + }() + if response.StatusCode != tc.status { + t.Fatalf("got %d want %d", response.StatusCode, tc.status) + } + if tc.status == http.StatusUnauthorized && response.Header.Get("WWW-Authenticate") != `Basic realm="messenger", charset="UTF-8"` { + t.Fatal("missing Basic challenge") + } + }) + } +} + +// TestLoginInvalidCredentialsUsesBasicChallenge checks the service rejection mapping. +func TestLoginInvalidCredentialsUsesBasicChallenge(t *testing.T) { + ctrl := testController(t, nil) + ctrl.auth = sessionFixture{login: func(string, string) (dto.SessionDTO, error) { + return dto.SessionDTO{}, service.ErrUnauthorized + }} + req := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", nil) + req.SetBasicAuth("alice", "invalid-password") + response, err := ctrl.server.Test(req) + if err != nil { + t.Fatal(err) + } + defer func() { + _ = response.Body.Close() + }() + if response.StatusCode != http.StatusUnauthorized || response.Header.Get("WWW-Authenticate") != `Basic realm="messenger", charset="UTF-8"` { + t.Fatal("incorrect login rejection") + } +} + +// TestLogoutHTTPReturnsNoContent checks the authenticated revocation response. +func TestLogoutHTTPReturnsNoContent(t *testing.T) { + ctrl := testController(t, nil) + actor := user(t, ctrl) + req := httptest.NewRequest(http.MethodDelete, "/api/v1/auth/sessions/current", nil) + req.Header.Set("Authorization", "Bearer "+actor) + response, err := ctrl.server.Test(req) + if err != nil { + t.Fatal(err) + } + defer func() { + _ = response.Body.Close() + }() + body, err := io.ReadAll(response.Body) + if err != nil || response.StatusCode != http.StatusNoContent || len(body) != 0 { + t.Fatalf("invalid logout response: %v", err) + } +} + +// TestLogoutHTTPPreservesFailure ensures an unsuccessful revoke does not claim success. +func TestLogoutHTTPPreservesFailure(t *testing.T) { + ctrl := testController(t, nil) + actor := user(t, ctrl) + ctrl.auth = sessionFixture{fixtureAuthentication: fixtureAuthentication{ctrl.service}, logoutError: context.DeadlineExceeded} + status, _, err := request(ctrl, http.MethodDelete, "/api/v1/auth/sessions/current", actor, nil) + if err != nil || status != http.StatusInternalServerError { + t.Fatalf("revocation failure hidden: %d %v", status, err) + } +} + +// TestLegacyIdentityDoesNotAuthenticate verifies the lab1 header cannot grant access. +func TestLegacyIdentityDoesNotAuthenticate(t *testing.T) { + ctrl := testController(t, nil) + actor := user(t, ctrl) + req := httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil) + req.Header.Set("X-User-Id", actor) + response, err := ctrl.server.Test(req) + if err != nil { + t.Fatal(err) + } + defer func() { + _ = response.Body.Close() + }() + if response.StatusCode != http.StatusUnauthorized || response.Header.Get("WWW-Authenticate") != `Bearer realm="messenger"` { + t.Fatal("legacy identity granted access or missing challenge") + } +} + +type unavailableReadiness struct{} + +// Ping simulates an unavailable mandatory dependency for readiness mapping. +func (unavailableReadiness) Ping(context.Context) error { + return context.DeadlineExceeded +} + +// TestReadinessRejectsDependencyFailure verifies readiness reflects mandatory infrastructure. +func TestReadinessRejectsDependencyFailure(t *testing.T) { + ctrl := testController(t, nil) + ctrl.readiness = unavailableReadiness{} + status, _, err := request(ctrl, http.MethodGet, "/health/ready", "", nil) + if err != nil || status != http.StatusServiceUnavailable { + t.Fatalf("incorrect readiness: %d %v", status, err) + } +} diff --git a/internal/controller/http/fiber/body_limit_test.go b/internal/controller/http/fiber/body_limit_test.go new file mode 100644 index 0000000..ae74a47 --- /dev/null +++ b/internal/controller/http/fiber/body_limit_test.go @@ -0,0 +1,69 @@ +package fiber + +import ( + "context" + "encoding/json" + "net" + "net/http" + "strings" + "testing" + "time" + + "github.com/gofiber/fiber/v2" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + httpmodel "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/http" +) + +// TestBodyLimitInstanceHeader verifies parser-generated 413 responses over a real TCP listener. +func TestBodyLimitInstanceHeader(t *testing.T) { + // Arrange. + ctrl := testController(t, func(cfg *config.Config) { + cfg.HTTP.MaxBodyBytes = 64 << 10 + cfg.HTTP.InstanceID = "body-limit-test-instance" + }) + ctrl.cfg.Host = "127.0.0.1" + ctrl.cfg.Port = 0 + address := make(chan string, 1) + ctrl.server.Hooks().OnListen(func(data fiber.ListenData) error { + address <- net.JoinHostPort(data.Host, data.Port) + return nil + }) + if err := ctrl.Start(t.Context()); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + if err := ctrl.Stop(ctx); err != nil { + t.Error(err) + } + }) + client := &http.Client{Timeout: 3 * time.Second} + + // Act. + response, err := client.Post("http://"+<-address+"/api/v1/users", "application/json", strings.NewReader(strings.Repeat("x", ctrl.cfg.MaxBodyBytes+1))) + if err != nil { + t.Fatal(err) + } + defer func() { + if err := response.Body.Close(); err != nil { + t.Error(err) + } + }() + + // Assert. + if response.StatusCode != http.StatusRequestEntityTooLarge { + t.Fatalf("status=%d", response.StatusCode) + } + if instance := response.Header.Get("X-Instance-Id"); instance != ctrl.instanceID { + t.Fatalf("instance=%q, want %q", instance, ctrl.instanceID) + } + var body httpmodel.ErrorResponse + if err := json.NewDecoder(response.Body).Decode(&body); err != nil { + t.Fatal(err) + } + if body.Error.Code != "payload_too_large" || body.Error.RequestID == "" || body.Error.RequestID != response.Header.Get(headerRequestID) { + t.Fatalf("invalid error envelope: %+v", body) + } +} diff --git a/internal/controller/http/fiber/chats_test.go b/internal/controller/http/fiber/chats_test.go new file mode 100644 index 0000000..d1660ba --- /dev/null +++ b/internal/controller/http/fiber/chats_test.go @@ -0,0 +1,312 @@ +package fiber + +import ( + "net/http" + "net/url" + "reflect" + "testing" + + "github.com/google/uuid" +) + +// TestController_ChatsCreate checks membership and request validation in isolated chats. +func TestController_ChatsCreate(t *testing.T) { + t.Run("Positive: caller included", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + payload := mustJSON(t, map[string]any{"title": "Team", "member_ids": []string{actor}}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusCreated, body) + if body["title"] != "Team" || !reflect.DeepEqual(body["member_ids"], []any{actor}) { + t.Fatalf("chat fields changed: %v", body) + } + }) + + t.Run("Negative: empty members", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + payload := mustJSON(t, map[string]any{"title": "Team", "member_ids": []string{}}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") + }) + + t.Run("Negative: duplicate members", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + payload := mustJSON(t, map[string]any{"title": "Team", "member_ids": []string{actor, actor}}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") + }) + + t.Run("Negative: caller omitted", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + outsider := user(t, ctrl) + payload := mustJSON(t, map[string]any{"title": "Team", "member_ids": []string{outsider}}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") + }) + + t.Run("Negative: unknown member", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + payload := mustJSON(t, map[string]any{"title": "Team", "member_ids": []string{actor, uuid.NewString()}}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") + }) + + t.Run("Negative: malformed member ID", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + payload := mustJSON(t, map[string]any{"title": "Team", "member_ids": []string{actor, "invalid"}}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") + }) + + t.Run("Negative: blank title", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + payload := mustJSON(t, map[string]any{"title": " ", "member_ids": []string{actor}}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") + }) + + t.Run("Negative: malformed JSON", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + payload := []byte(`{`) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") + }) +} + +// TestController_ChatsGetByID checks access and the returned chat identity. +func TestController_ChatsGetByID(t *testing.T) { + t.Run("Positive: member reads chat", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + id := chat(t, ctrl, owner) + actor, target := owner, id + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+target, actor, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusOK, body) + if body["id"] != id || body["title"] != "Чат" { + t.Fatalf("wrong chat: %v", body) + } + }) + + t.Run("Negative: outsider", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + id := chat(t, ctrl, owner) + actor, target := user(t, ctrl), id + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+target, actor, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusNotFound, body) + assertErrorCode(t, body, "not_found") + }) + + t.Run("Negative: unknown chat", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + actor, target := owner, uuid.NewString() + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+target, actor, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusNotFound, body) + assertErrorCode(t, body, "not_found") + }) + + t.Run("Negative: malformed chat ID", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + actor, target := owner, "invalid" + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+target, actor, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") + }) + + t.Run("Negative: authentication precedes ID parsing", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor, target := "", "invalid" + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+target, actor, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusUnauthorized, body) + assertErrorCode(t, body, "unauthorized") + }) +} + +// TestController_ChatsGetPage checks that foreign memberships remain absent from lists. +func TestController_ChatsGetPage(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + chat(t, ctrl, owner) + outsider := user(t, ctrl) + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats", outsider, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusOK, body) + if len(body["items"].([]any)) != 0 || body["next_cursor"] != nil { + t.Fatalf("foreign chat leaked: %v", body) + } +} + +// TestController_ChatPagination recovers every chat exactly once across page boundaries. +func TestController_ChatPagination(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + expected := make(map[string]bool) + for range 15 { + expected[chat(t, ctrl, actor)] = true + } + + // Act. + items := history(t, ctrl, actor, "/api/v1/chats") + + // Assert. + if len(items) != len(expected) { + t.Fatalf("got %d chats, want %d", len(items), len(expected)) + } + for _, item := range items { + id := item.(map[string]any)["id"].(string) + if !expected[id] { + t.Fatalf("duplicate or unexpected chat %s", id) + } + delete(expected, id) + } +} + +// TestController_ChatCursorScope rejects a continuation issued for another user's list. +func TestController_ChatCursorScope(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + outsider := user(t, ctrl) + chat(t, ctrl, owner) + chat(t, ctrl, owner) + page := call(t, ctrl, http.MethodGet, "/api/v1/chats?limit=1", owner, nil, http.StatusOK) + token := page["next_cursor"].(string) + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats?cursor="+url.QueryEscape(token), outsider, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") +} diff --git a/internal/controller/http/fiber/configure.go b/internal/controller/http/fiber/configure.go new file mode 100644 index 0000000..1f3da39 --- /dev/null +++ b/internal/controller/http/fiber/configure.go @@ -0,0 +1,37 @@ +package fiber + +import ( + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/recover" +) + +// configure initializes the HTTP server, middleware and routes. +func (ctrl *Controller) configure() { + ctrl.createServer() + ctrl.initMiddlewares() + ctrl.initRoutes() +} + +// createServer creates Fiber with the configured transport limits and error handler. +func (ctrl *Controller) createServer() { + timeout := ctrl.cfg.Timeout() + ctrl.server = fiber.New(fiber.Config{ + AppName: "messenger-lab1", + DisableStartupMessage: true, + Immutable: true, + CaseSensitive: true, + StrictRouting: true, + BodyLimit: ctrl.cfg.MaxBodyBytes, + Concurrency: ctrl.cfg.MaxConnections, + ReadTimeout: timeout, + WriteTimeout: timeout, + IdleTimeout: timeout, + ErrorHandler: ctrl.handleErr, + }) +} + +// initMiddlewares registers request logging and panic recovery middleware. +func (ctrl *Controller) initMiddlewares() { + ctrl.server.Use(ctrl.request) + ctrl.server.Use(recover.New()) +} diff --git a/internal/controller/http/fiber/controller.go b/internal/controller/http/fiber/controller.go new file mode 100644 index 0000000..7bae75a --- /dev/null +++ b/internal/controller/http/fiber/controller.go @@ -0,0 +1,66 @@ +// Package fiber implements the messenger HTTP transport using Fiber. +package fiber + +import ( + "fmt" + "sync/atomic" + + "github.com/gofiber/fiber/v2" + "github.com/google/uuid" + "go.lab260.ru/fielderr" + "go.uber.org/zap" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/admission" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/controller" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// Controller owns the HTTP server and delegates business operations to Service. +// Its configuration is copied at construction and must not change during serving. +// Fx manages its listener through Start and Stop. +type Controller struct { + attachments Attachments + metrics *httpMetrics + log *zap.Logger + cfg config.Controller + service service.Service + server *fiber.App + errorRegistry *fielderr.Registry + admission admission.Distributed + auth service.Authentication + readiness Readiness + stopping atomic.Bool + instanceID string + serveDone chan error +} + +var _ controller.Controller = (*Controller)(nil) + +// New validates dependencies and prepares the server without opening a listener. +// Routing, middleware and Fiber configuration are installed by configure. +func New(log *zap.Logger, cfg *config.Controller, svc service.Service, registry *fielderr.Registry, limiter admission.Distributed, auth service.Authentication, readiness Readiness) (*Controller, error) { + if err := cfg.Validate(); err != nil { + return nil, err + } + if log == nil || svc == nil || registry == nil || limiter == nil || auth == nil || readiness == nil { + return nil, fmt.Errorf("HTTP logger, service, error registry and admission limiter are required") + } + ctrl := &Controller{ + metrics: newHTTPMetrics(), + log: log.Named("controller.http.fiber"), + cfg: *cfg, + service: svc, + errorRegistry: registry, + admission: limiter, + auth: auth, + readiness: readiness, + serveDone: make(chan error, 1), + } + ctrl.instanceID = cfg.InstanceID + if ctrl.instanceID == "" { + ctrl.instanceID = uuid.NewString() + } + ctrl.configure() + return ctrl, nil +} diff --git a/internal/controller/http/fiber/controller_test.go b/internal/controller/http/fiber/controller_test.go new file mode 100644 index 0000000..0586cfd --- /dev/null +++ b/internal/controller/http/fiber/controller_test.go @@ -0,0 +1,321 @@ +package fiber + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "net/url" + "reflect" + "strings" + "sync" + "testing" + + "github.com/google/uuid" + "go.uber.org/zap" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/admission" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service/production" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store/memory" +) + +// testController creates an integration fixture with the real controller, service and store. +func testController(t *testing.T, change func(*config.Config)) *Controller { + t.Helper() + cfg := config.Default() + cfg.HTTP.MaxConnections = 256 + cfg.HTTP.Admission.RequestsPerSecond = 100000 + cfg.HTTP.Admission.Burst = 100000 + if change != nil { + change(cfg) + } + repo, err := memory.New(cfg.Memory) + if err != nil { + t.Fatal(err) + } + codec, err := cursor.New() + if err != nil { + t.Fatal(err) + } + svc, err := production.New(repo, codec) + if err != nil { + t.Fatal(err) + } + registry, err := NewErrorRegistry() + if err != nil { + t.Fatal(err) + } + limiter, err := admission.New(admission.Options{ + MaxInFlight: cfg.HTTP.Admission.MaxInFlight, + RequestsPerSecond: cfg.HTTP.Admission.RequestsPerSecond, + Burst: cfg.HTTP.Admission.Burst, + }) + if err != nil { + t.Fatal(err) + } + ctrl, err := New(zap.NewNop(), cfg.HTTP, svc, registry, &fixtureAdmission{limiter}, fixtureAuthentication{svc}, fixtureReadiness{}) + if err != nil { + t.Fatal(err) + } + return ctrl +} + +// request sends a raw test request and decodes its response. +func request(ctrl *Controller, method, path, actor string, body []byte) (int, map[string]any, error) { + req := httptest.NewRequest(method, path, bytes.NewReader(body)) + req.Header.Set("Idempotency-Key", uuid.NewString()) + if body != nil { + req.Header.Set("Content-Type", "application/json") + } + if actor != "" { + req.Header.Set("Authorization", "Bearer "+actor) + } + resp, err := ctrl.server.Test(req, 10000) + if err != nil { + return 0, nil, err + } + defer func() { + _ = resp.Body.Close() + }() + b, err := io.ReadAll(resp.Body) + if err != nil { + return 0, nil, err + } + var data map[string]any + if err = json.Unmarshal(b, &data); err != nil { + return resp.StatusCode, nil, fmt.Errorf("invalid JSON response: %w", err) + } + if resp.Header.Get("X-Request-Id") == "" { + return resp.StatusCode, data, fmt.Errorf("missing request ID") + } + if resp.StatusCode >= http.StatusBadRequest { + envelope, ok := data["error"].(map[string]any) + if !ok || envelope["request_id"] != resp.Header.Get("X-Request-Id") { + return resp.StatusCode, data, fmt.Errorf("invalid error envelope") + } + } + return resp.StatusCode, data, nil +} + +// call sends a JSON test request and checks its expected HTTP status. +func call(t *testing.T, ctrl *Controller, method, path, actor string, body any, want int) map[string]any { + t.Helper() + var b []byte + var err error + if raw, ok := body.([]byte); ok { + b = raw + } else if body != nil { + b, err = json.Marshal(body) + if err != nil { + t.Fatal(err) + } + } + status, data, err := request(ctrl, method, path, actor, b) + if err != nil { + t.Fatal(err) + } + if status != want { + t.Fatalf("%s %s: got %d want %d: %v", method, path, status, want, data) + } + return data +} + +// user creates a test user and returns its identifier. +func user(t *testing.T, c *Controller) string { + t.Helper() + r := call(t, c, http.MethodPost, "/api/v1/users", "", map[string]any{"username": "u_" + uuid.New().String()[:8], "display_name": "Алиса 👋", "password": "fixture-password-123"}, http.StatusCreated) + return r["id"].(string) +} + +// chat creates a test chat containing the supplied user. +func chat(t *testing.T, c *Controller, u string) string { + t.Helper() + r := call(t, c, http.MethodPost, "/api/v1/chats", u, map[string]any{"title": "Чат", "member_ids": []string{u}}, http.StatusCreated) + return r["id"].(string) +} + +// TestDuplicateUsernameRace checks username uniqueness under concurrent requests. +func TestDuplicateUsernameRace(t *testing.T) { + // Arrange. + c := testController(t, nil) + results := make(chan int, 10) + failures := make(chan error, 10) + var wg sync.WaitGroup + // Act. + for range 10 { + wg.Go(func() { + status, _, err := request(c, http.MethodPost, "/api/v1/users", "", []byte(`{"username":"same","display_name":"User","password":"fixture-password-123"}`)) + if err != nil { + failures <- err + } + results <- status + }) + } + wg.Wait() + close(results) + close(failures) + // Assert. + for err := range failures { + t.Error(err) + } + created, conflicts := 0, 0 + for s := range results { + switch s { + case http.StatusCreated: + created++ + case http.StatusConflict: + conflicts++ + default: + t.Errorf("unexpected %d", s) + } + } + if created != 1 || conflicts != 9 { + t.Fatalf("created=%d conflicts=%d", created, conflicts) + } +} + +// history collects all test messages by following pagination cursors. +func history(t *testing.T, c *Controller, u, path string) []any { + t.Helper() + var items []any + next := path + "?limit=7" + for range 100 { + r := call(t, c, http.MethodGet, next, u, nil, http.StatusOK) + items = append(items, r["items"].([]any)...) + if r["next_cursor"] == nil { + return items + } + next = path + "?limit=7&cursor=" + url.QueryEscape(r["next_cursor"].(string)) + } + t.Fatal("pagination never finished") + return nil +} + +// TestConcurrent100Messages10Clients recovers the exact acknowledged records after concurrent writes. +func TestConcurrent100Messages10Clients(t *testing.T) { + // Arrange. + const clients = 10 + const messagesPerClient = 10 + const total = clients * messagesPerClient + ctrl := testController(t, nil) + actor := user(t, ctrl) + chatID := chat(t, ctrl, actor) + path := "/api/v1/chats/" + chatID + "/messages" + acknowledgements := make(chan map[string]any, total) + failures := make(chan error, total) + var writers sync.WaitGroup + + // Act. + for client := range clients { + writers.Go(func() { + for index := range messagesPerClient { + text := fmt.Sprintf("client %d message %d", client, index) + payload, err := json.Marshal(map[string]string{"text": text}) + if err != nil { + failures <- err + continue + } + status, body, err := request(ctrl, http.MethodPost, path, actor, payload) + if err != nil { + failures <- err + continue + } + if status != http.StatusCreated || body["text"] != text || body["sender_id"] != actor || body["chat_id"] != chatID { + failures <- fmt.Errorf("incorrect acknowledgement: status=%d, body=%v", status, body) + continue + } + acknowledgements <- body + } + }) + } + writers.Wait() + close(failures) + close(acknowledgements) + + // Assert. + for err := range failures { + t.Error(err) + } + if t.Failed() { + t.FailNow() + } + expected := make(map[string]map[string]any, total) + for message := range acknowledgements { + id, ok := message["id"].(string) + if !ok || id == "" { + t.Fatalf("invalid message ID: %v", message) + } + if _, duplicate := expected[id]; duplicate { + t.Fatalf("duplicate acknowledgement ID %s", id) + } + expected[id] = message + } + if len(expected) != total { + t.Fatalf("acknowledged=%d, want=%d", len(expected), total) + } + items := history(t, ctrl, actor, path) + if len(items) != total { + t.Fatalf("recovered=%d, want=%d", len(items), total) + } + for _, item := range items { + message := item.(map[string]any) + id := message["id"].(string) + if !reflect.DeepEqual(message, expected[id]) { + t.Fatalf("stored record differs from acknowledgement: got=%v, want=%v", message, expected[id]) + } + delete(expected, id) + } +} + +// TestUnsupportedMediaType checks rejection of unsupported request content types. +func TestUnsupportedMediaType(t *testing.T) { + // Arrange. + c := testController(t, nil) + req := httptest.NewRequest(http.MethodPost, "/api/v1/users", strings.NewReader(`{}`)) + req.Header.Set("Content-Type", "text/plain") + // Act. + r, e := c.server.Test(req) + + // Assert. + if e != nil { + t.Fatal(e) + } + defer func() { + _ = r.Body.Close() + }() + if r.StatusCode != http.StatusUnsupportedMediaType { + t.Fatalf("status %d", r.StatusCode) + } +} + +// TestConfigIndependentState prevents one application instance from accepting another's identity. +func TestConfigIndependentState(t *testing.T) { + // Arrange. + first := testController(t, nil) + actor := user(t, first) + second := testController(t, nil) + + // Act. + status, body, err := request(second, http.MethodGet, "/api/v1/users/me", actor, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusUnauthorized, body) + assertErrorCode(t, body, "unauthorized") +} + +// mustJSON encodes fixture payloads and reports preparation failures at the caller. +func mustJSON(t *testing.T, value any) []byte { + t.Helper() + payload, err := json.Marshal(value) + if err != nil { + t.Fatal(err) + } + return payload +} diff --git a/internal/controller/http/fiber/decode.go b/internal/controller/http/fiber/decode.go new file mode 100644 index 0000000..ea2d16c --- /dev/null +++ b/internal/controller/http/fiber/decode.go @@ -0,0 +1,34 @@ +package fiber + +import ( + "mime" + + "github.com/gofiber/fiber/v2" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/strictjson" +) + +// decode checks HTTP media type and maps strict JSON decoding errors to API input errors. +func decode(ctx *fiber.Ctx, target any, requiredFields ...string) error { + mediaType, _, err := mime.ParseMediaType(ctx.Get(fiber.HeaderContentType)) + if err != nil || mediaType != fiber.MIMEApplicationJSON { + return errMedia + } + if err := strictjson.DecodeObject(ctx.Body(), target, requiredFields...); err != nil { + return service.ErrInvalid + } + return nil +} + +// decodeMessage accepts the optional attachment list without weakening strict JSON validation. +func decodeMessage(ctx *fiber.Ctx, target any) error { + mediaType, _, err := mime.ParseMediaType(ctx.Get(fiber.HeaderContentType)) + if err != nil || mediaType != fiber.MIMEApplicationJSON { + return errMedia + } + if err := strictjson.DecodeOptional(ctx.Body(), target, []string{"text"}, []string{"attachment_ids"}); err != nil { + return service.ErrInvalid + } + return nil +} diff --git a/internal/controller/http/fiber/dependencies.go b/internal/controller/http/fiber/dependencies.go new file mode 100644 index 0000000..58a3a74 --- /dev/null +++ b/internal/controller/http/fiber/dependencies.go @@ -0,0 +1,9 @@ +package fiber + +import "context" + +// Readiness checks the dependencies required by the synchronous request path. +type Readiness interface { + // Ping reports whether requests can be safely served. + Ping(context.Context) error +} diff --git a/internal/controller/http/fiber/error_codes.go b/internal/controller/http/fiber/error_codes.go new file mode 100644 index 0000000..64decfa --- /dev/null +++ b/internal/controller/http/fiber/error_codes.go @@ -0,0 +1,22 @@ +package fiber + +const ( + // codeInvalid identifies malformed or invalid request input. + codeInvalid = "invalid_request" + // codeUnauthorized identifies an unresolved user identity. + codeUnauthorized = "unauthorized" + // codeNotFound deliberately covers both absent and inaccessible resources. + codeNotFound = "not_found" + // codeConflict identifies a violated uniqueness constraint. + codeConflict = "conflict" + // codeUnavailable identifies temporary admission or persistent capacity exhaustion. + codeUnavailable = "unavailable" + // codeRateLimited identifies exhaustion of the request token bucket. + codeRateLimited = "rate_limited" + // codeTooLarge identifies a request exceeding the transport byte budget. + codeTooLarge = "payload_too_large" + // codeMedia identifies an unsupported request media type. + codeMedia = "unsupported_media_type" + // codeInternal identifies a hidden unexpected failure. + codeInternal = "internal_error" +) diff --git a/internal/controller/http/fiber/error_registry.go b/internal/controller/http/fiber/error_registry.go new file mode 100644 index 0000000..d5e1007 --- /dev/null +++ b/internal/controller/http/fiber/error_registry.go @@ -0,0 +1,33 @@ +package fiber + +import ( + "net/http" + + "go.lab260.ru/fielderr" +) + +// NewErrorRegistry returns an immutable mapping of application statuses to HTTP codes. +// The registry is injected rather than installed in process-global state, allowing +// independent application instances in integration tests. +func NewErrorRegistry() (*fielderr.Registry, error) { + return fielderr.NewRegistry( + statusDefinition(codeInvalid, http.StatusBadRequest), + statusDefinition(codeUnauthorized, http.StatusUnauthorized), + statusDefinition(codeNotFound, http.StatusNotFound), + statusDefinition(codeConflict, http.StatusConflict), + statusDefinition(codeTooLarge, http.StatusRequestEntityTooLarge), + statusDefinition(codeMedia, http.StatusUnsupportedMediaType), + statusDefinition(codeRateLimited, http.StatusTooManyRequests), + statusDefinition(codeUnavailable, http.StatusServiceUnavailable), + statusDefinition(codeInternal, http.StatusInternalServerError), + ) +} + +// statusDefinition associates an application error code with its HTTP status. +func statusDefinition(name string, httpStatus int) fielderr.StatusDefinition { + return fielderr.StatusDefinition{ + Code: fielderr.NewStatusCode(name), + Name: name, + HTTPStatus: httpStatus, + } +} diff --git a/internal/controller/http/fiber/errors.go b/internal/controller/http/fiber/errors.go new file mode 100644 index 0000000..1b757d0 --- /dev/null +++ b/internal/controller/http/fiber/errors.go @@ -0,0 +1,73 @@ +package fiber + +import ( + "errors" + "net/http" + + "github.com/gofiber/fiber/v2" + "github.com/google/uuid" + "go.lab260.ru/fielderr" + "go.uber.org/zap" + + httpmodel "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/http" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// handleErr normalizes a failure and writes the public error envelope. +func (ctrl *Controller) handleErr(ctx *fiber.Ctx, err error) error { + // Parser failures such as BodyLimit rejection bypass request middleware. + ctx.Set("X-Instance-Id", ctrl.instanceID) + requestID := ctx.GetRespHeader(headerRequestID) + if requestID == "" { + requestID = uuid.NewString() + ctx.Set(headerRequestID, requestID) + } + err = publicError(normalizeTransportError(err)) + status, ok := fielderr.HTTPStatusFrom(ctrl.errorRegistry, err) + if !ok { + err = errInternal + status = http.StatusInternalServerError + } + if status == http.StatusUnauthorized { + challenge := `Bearer realm="messenger"` + if ctx.Path() == "/api/v1/auth/sessions" && ctx.Method() == http.MethodPost { + challenge = `Basic realm="messenger", charset="UTF-8"` + } + ctx.Set(fiber.HeaderWWWAuthenticate, challenge) + } + if status == http.StatusInternalServerError { + ctrl.log.Error("unexpected request failure", zap.String("request_id", requestID)) + } + return ctx.Status(status).JSON(errorResponse(err, requestID)) +} + +// normalizeTransportError maps Fiber failures to transport-independent application errors. +func normalizeTransportError(err error) error { + transportError, ok := errors.AsType[*fiber.Error](err) + if !ok { + return err + } + switch transportError.Code { + case http.StatusRequestEntityTooLarge: + return errTooLarge + case http.StatusNotFound, http.StatusMethodNotAllowed: + return service.ErrNotFound + case http.StatusBadRequest, http.StatusRequestTimeout, http.StatusRequestHeaderFieldsTooLarge: + return service.ErrInvalid + default: + return errInternal + } +} + +// errorResponse builds the public error envelope with its request identifier. +func errorResponse(err error, requestID string) httpmodel.ErrorResponse { + status, _ := fielderr.Status(err) + message, _ := fielderr.PublicMessage(err) + return httpmodel.ErrorResponse{ + Error: httpmodel.ErrorDetail{ + Code: status.String(), + Message: message, + RequestID: requestID, + }, + } +} diff --git a/internal/controller/http/fiber/errors_test.go b/internal/controller/http/fiber/errors_test.go new file mode 100644 index 0000000..24bc36d --- /dev/null +++ b/internal/controller/http/fiber/errors_test.go @@ -0,0 +1,107 @@ +package fiber + +import ( + "encoding/json" + "errors" + "net/http" + "testing" + + "github.com/gofiber/fiber/v2" + "github.com/valyala/fasthttp" + "go.lab260.ru/fielderr" + + httpmodel "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/http" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// TestTransportErrorMapping checks safe API errors for framework-generated failures. +func TestTransportErrorMapping(t *testing.T) { + t.Run("large body", func(t *testing.T) { + if normalizeTransportError(fiber.ErrRequestEntityTooLarge) != errTooLarge { + t.Fatal("incorrect body-limit error") + } + }) + t.Run("method not allowed", func(t *testing.T) { + if normalizeTransportError(fiber.ErrMethodNotAllowed) != service.ErrNotFound { + t.Fatal("unexpected method error") + } + }) + t.Run("invalid request", func(t *testing.T) { + if normalizeTransportError(fiber.ErrBadRequest) != service.ErrInvalid { + t.Fatal("unexpected input error") + } + }) + t.Run("unexpected framework failure", func(t *testing.T) { + if normalizeTransportError(fiber.ErrInternalServerError) != errInternal { + t.Fatal("unexpected failure escaped normalization") + } + }) +} + +// TestController_handleErr writes a safe, correlated fallback response. +func TestController_handleErr(t *testing.T) { + t.Run("Negative: unknown internal error", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + raw := &fasthttp.RequestCtx{} + ctx := ctrl.server.AcquireCtx(raw) + defer ctrl.server.ReleaseCtx(ctx) + + // Act. + err := ctrl.handleErr(ctx, errors.New("private storage secret")) + + // Assert. + if err != nil { + t.Fatal(err) + } + if raw.Response.StatusCode() != http.StatusInternalServerError { + t.Fatalf("status=%d", raw.Response.StatusCode()) + } + var body httpmodel.ErrorResponse + if err := json.Unmarshal(raw.Response.Body(), &body); err != nil { + t.Fatal(err) + } + requestID := string(raw.Response.Header.Peek(headerRequestID)) + if requestID == "" || body.Error.RequestID != requestID { + t.Fatalf("uncorrelated error: %+v, header=%q", body, requestID) + } + if body.Error.Code != "internal_error" || body.Error.Message != "internal server error" { + t.Fatalf("unsafe fallback: %+v", body) + } + }) + + t.Run("Negative: missing status mapping", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + registry, err := fielderr.NewRegistry() + if err != nil { + t.Fatal(err) + } + ctrl.errorRegistry = registry + raw := &fasthttp.RequestCtx{} + ctx := ctrl.server.AcquireCtx(raw) + defer ctrl.server.ReleaseCtx(ctx) + + // Act. + err = ctrl.handleErr(ctx, service.ErrInvalid) + + // Assert. + if err != nil { + t.Fatal(err) + } + if raw.Response.StatusCode() != http.StatusInternalServerError { + t.Fatalf("status=%d", raw.Response.StatusCode()) + } + var body httpmodel.ErrorResponse + if err := json.Unmarshal(raw.Response.Body(), &body); err != nil { + t.Fatal(err) + } + requestID := string(raw.Response.Header.Peek(headerRequestID)) + if requestID == "" || body.Error.RequestID != requestID { + t.Fatalf("uncorrelated error: %+v, header=%q", body, requestID) + } + if body.Error.Code != "internal_error" || body.Error.Message != "internal server error" { + t.Fatalf("unsafe fallback: %+v", body) + } + }) +} diff --git a/internal/controller/http/fiber/fixtures_auth_test.go b/internal/controller/http/fiber/fixtures_auth_test.go new file mode 100644 index 0000000..f43a9e7 --- /dev/null +++ b/internal/controller/http/fiber/fixtures_auth_test.go @@ -0,0 +1,61 @@ +package fiber + +import ( + "context" + "time" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/admission" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// fixtureAuthentication isolates HTTP decoding and business authorization from session persistence. +type fixtureAuthentication struct { + service service.Service +} + +// Register delegates profile validation to the real business service. +func (f fixtureAuthentication) Register(ctx context.Context, request dto.UsersCreateRequestDTO, _ string) (dto.UserDTO, error) { + return f.service.UsersCreate(ctx, request) +} + +// Authenticate uses fixture tokens equal to user IDs exclusively in controller tests. +func (f fixtureAuthentication) Authenticate(ctx context.Context, token string) (string, error) { + return token, f.service.UsersCheckIdentity(ctx, token) +} + +// Login is deliberately unsupported by this fixture; real login has separate integration coverage. +func (f fixtureAuthentication) Login(context.Context, string, string) (dto.SessionDTO, error) { + return dto.SessionDTO{}, service.ErrUnauthorized +} + +// Logout accepts the fixture session without persistent side effects. +func (f fixtureAuthentication) Logout(context.Context, string) error { + return nil +} + +type fixtureReadiness struct{} + +// Ping marks isolated controller fixtures ready without external resources. +func (fixtureReadiness) Ping(context.Context) error { + return nil +} + +type fixtureAdmission struct { + *admission.Limiter +} + +// Enter adapts the deterministic local policy fixture to the distributed boundary. +func (f *fixtureAdmission) Enter(_ context.Context, _, user string) (func(), error) { + if user != "" { + return func() { + }, nil + } + if !f.Allow(time.Now()) { + return nil, admission.ErrRate + } + if !f.Acquire() { + return nil, admission.ErrSlots + } + return f.Release, nil +} diff --git a/internal/controller/http/fiber/handlers.go b/internal/controller/http/fiber/handlers.go new file mode 100644 index 0000000..ebe01ac --- /dev/null +++ b/internal/controller/http/fiber/handlers.go @@ -0,0 +1,98 @@ +package fiber + +import ( + "net/http" + + "github.com/gofiber/fiber/v2" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + httpmodel "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/http" +) + +// HandleUsersCreate decodes registration input and returns the created user. +func (ctrl *Controller) HandleUsersCreate(ctx *fiber.Ctx) error { + var request httpmodel.UsersCreateRequest + if err := decode(ctx, &request, "username", "display_name", "password"); err != nil { + return err + } + user, err := ctrl.auth.Register(ctx.UserContext(), dto.UsersCreateRequestDTO{ + Username: request.Username, + DisplayName: request.DisplayName, + }, request.Password) + if err != nil { + return err + } + return ctx.Status(http.StatusCreated).JSON(userResponse(user)) +} + +// HandleUsersGetMe returns the account selected by the validated session. +func (ctrl *Controller) HandleUsersGetMe(ctx *fiber.Ctx) error { + user, err := ctrl.service.UsersGetMe(ctx.UserContext(), identity(ctx)) + if err != nil { + return err + } + return ctx.Status(http.StatusOK).JSON(userResponse(user)) +} + +// HandleChatsCreate decodes the chat payload and returns the new chat. +func (ctrl *Controller) HandleChatsCreate(ctx *fiber.Ctx) error { + var request httpmodel.ChatsCreateRequest + if err := decode(ctx, &request, "title", "member_ids"); err != nil { + return err + } + chat, err := ctrl.service.ChatsCreate(ctx.UserContext(), dto.ChatsCreateRequestDTO{ + UserID: identity(ctx), + Title: request.Title, + MemberIDs: request.MemberIDs, + }) + if err != nil { + return err + } + return ctx.Status(http.StatusCreated).JSON(chatResponse(chat)) +} + +// HandleChatsGetByID delegates chat authorization and maps the accessible chat. +func (ctrl *Controller) HandleChatsGetByID(ctx *fiber.Ctx) error { + chat, err := ctrl.service.ChatsGetByID(ctx.UserContext(), identity(ctx), ctx.Params("chat_id")) + if err != nil { + return err + } + return ctx.Status(http.StatusOK).JSON(chatResponse(chat)) +} + +// HandleChatsGetPage returns the service's page of visible chats. +func (ctrl *Controller) HandleChatsGetPage(ctx *fiber.Ctx) error { + page, err := ctrl.service.ChatsGetPage(ctx.UserContext(), pageRequest(ctx)) + if err != nil { + return err + } + return ctx.Status(http.StatusOK).JSON(chatPageResponse(page)) +} + +// HandleMessagesCreate decodes text; the service derives and authorizes the sender. +func (ctrl *Controller) HandleMessagesCreate(ctx *fiber.Ctx) error { + var request httpmodel.MessagesCreateRequest + if err := decodeMessage(ctx, &request); err != nil { + return err + } + message, err := ctrl.service.MessagesCreate(ctx.UserContext(), dto.MessagesCreateRequestDTO{ + AttachmentIDs: request.AttachmentIDs, + IdempotencyKey: ctx.Get("Idempotency-Key"), + UserID: identity(ctx), + ChatID: ctx.Params("chat_id"), + Text: request.Text, + }) + if err != nil { + return err + } + return ctx.Status(http.StatusCreated).JSON(messageResponse(message)) +} + +// HandleMessagesGetPage returns the service's ordered history page. +func (ctrl *Controller) HandleMessagesGetPage(ctx *fiber.Ctx) error { + page, err := ctrl.service.MessagesGetPage(ctx.UserContext(), pageRequest(ctx)) + if err != nil { + return err + } + return ctx.Status(http.StatusOK).JSON(messagePageResponse(page)) +} diff --git a/internal/controller/http/fiber/headers.go b/internal/controller/http/fiber/headers.go new file mode 100644 index 0000000..d699168 --- /dev/null +++ b/internal/controller/http/fiber/headers.go @@ -0,0 +1,7 @@ +package fiber + +const ( + headerUserID = "X-User-Id" + headerRequestID = "X-Request-Id" + retryAfterSeconds = 1 +) diff --git a/internal/controller/http/fiber/health.go b/internal/controller/http/fiber/health.go new file mode 100644 index 0000000..64ac39a --- /dev/null +++ b/internal/controller/http/fiber/health.go @@ -0,0 +1,28 @@ +package fiber + +import ( + "github.com/gofiber/fiber/v2" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// handleLive reports that the server process is running. +func (ctrl *Controller) handleLive(ctx *fiber.Ctx) error { + return ctx.JSON(fiber.Map{"status": "ok"}) +} + +// handleReady reports readiness unless shutdown has started. +func (ctrl *Controller) handleReady(ctx *fiber.Ctx) error { + if ctrl.stopping.Load() { + return errBusy + } + if err := ctrl.readiness.Ping(ctx.UserContext()); err != nil { + return errBusy + } + return ctrl.handleLive(ctx) +} + +// handleNotFound returns the application error for an unmatched route. +func (ctrl *Controller) handleNotFound(_ *fiber.Ctx) error { + return service.ErrNotFound +} diff --git a/internal/controller/http/fiber/lifecycle.go b/internal/controller/http/fiber/lifecycle.go new file mode 100644 index 0000000..455e213 --- /dev/null +++ b/internal/controller/http/fiber/lifecycle.go @@ -0,0 +1,70 @@ +package fiber + +import ( + "context" + "net" + "sync" + + "go.uber.org/zap" +) + +// Start opens the listener synchronously so Fx observes bind errors immediately. +// It must be called once per Controller; Stop terminates the serving goroutine. +func (ctrl *Controller) Start(ctx context.Context) error { + listener, err := net.Listen("tcp", ctrl.cfg.Bind()) + if err != nil { + return err + } + ready := make(chan struct{}) + go ctrl.serve(&acceptingListener{Listener: listener, ready: ready}) + select { + case <-ready: + ctrl.log.Info("http listener started", zap.String("address", listener.Addr().String())) + return nil + case err := <-ctrl.serveDone: + return err + case <-ctx.Done(): + _ = listener.Close() + return ctx.Err() + } +} + +// acceptingListener signals after fasthttp registers the listener and enters its +// accept loop. An OnListen hook runs earlier and cannot guarantee safe immediate Stop. +type acceptingListener struct { + // Listener is the bound socket owned by the HTTP lifecycle. + net.Listener + ready chan struct{} + once sync.Once +} + +// Accept marks the server as started before waiting for the next connection. +func (listener *acceptingListener) Accept() (net.Conn, error) { + listener.once.Do(func() { + close(listener.ready) + }) + return listener.Listener.Accept() +} + +// serve serves the listener and publishes its terminal result to the lifecycle. +func (ctrl *Controller) serve(listener net.Listener) { + ctrl.serveDone <- ctrl.server.Listener(listener) +} + +// Stop disables admission, closes the listener and drains active requests. +// The configured timeout is bounded by the caller's Fx shutdown context. +func (ctrl *Controller) Stop(ctx context.Context) error { + ctrl.stopping.Store(true) + ctx, cancel := context.WithTimeout(ctx, ctrl.cfg.ShutdownTimeout()) + defer cancel() + if err := ctrl.server.ShutdownWithContext(ctx); err != nil { + return err + } + select { + case err := <-ctrl.serveDone: + ctrl.log.Info("http listener stopped") + return err + case <-ctx.Done(): + return ctx.Err() + } +} diff --git a/internal/controller/http/fiber/lifecycle_test.go b/internal/controller/http/fiber/lifecycle_test.go new file mode 100644 index 0000000..ed9e1b4 --- /dev/null +++ b/internal/controller/http/fiber/lifecycle_test.go @@ -0,0 +1,132 @@ +package fiber + +import ( + "context" + "errors" + "net" + "net/http" + "testing" + "time" + + "github.com/gofiber/fiber/v2" +) + +// TestImmediateShutdown checks the startup/stop race without waiting for a first request. +func TestImmediateShutdown(t *testing.T) { + const repetitions = 10 + const stopDeadline = time.Second + for range repetitions { + // Arrange. + ctrl := testController(t, nil) + // Select an ephemeral test port after validating the normal application config. + ctrl.cfg.Host = "127.0.0.1" + ctrl.cfg.Port = 0 + if err := ctrl.Start(t.Context()); err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(t.Context(), stopDeadline) + // Act. + err := ctrl.Stop(ctx) + cancel() + // Assert. + if err != nil { + t.Fatalf("immediate shutdown leaked the listener or serve goroutine: %v", err) + } + } +} + +// TestController_Stop_ActiveRequestDeadline bounds draining while a real handler is still running. +func TestController_Stop_ActiveRequestDeadline(t *testing.T) { + // Arrange. + const deadline = 3 * time.Second + ctrl := testController(t, nil) + ctrl.cfg.Host = "127.0.0.1" + ctrl.cfg.Port = 0 + // Install one controlled handler to test lifecycle independently of business routes. + ctrl.createServer() + entered := make(chan struct{}) + release := make(chan struct{}) + address := make(chan string, 1) + ctrl.server.Hooks().OnListen(func(data fiber.ListenData) error { + address <- net.JoinHostPort(data.Host, data.Port) + return nil + }) + ctrl.server.Get("/blocked", func(ctx *fiber.Ctx) error { + close(entered) + <-release + return ctx.SendStatus(http.StatusOK) + }) + if err := ctrl.Start(t.Context()); err != nil { + t.Fatal(err) + } + client := &http.Client{Timeout: deadline} + completed := make(chan error, 1) + go func() { + response, err := client.Get("http://" + <-address + "/blocked") + if err == nil { + err = response.Body.Close() + } + completed <- err + }() + t.Cleanup(func() { + close(release) + select { + case err := <-completed: + if err != nil { + t.Error(err) + } + case <-time.After(deadline): + t.Error("blocked request did not terminate after release") + } + stopCtx, cancel := context.WithTimeout(context.Background(), deadline) + defer cancel() + if err := ctrl.Stop(stopCtx); err != nil { + t.Error(err) + } + }) + select { + case <-entered: + case <-time.After(deadline): + t.Fatal("request did not reach handler") + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + + // Act. + err := ctrl.Stop(ctx) + + // Assert. + if !errors.Is(err, context.Canceled) { + t.Fatalf("expected canceled drain, got %v", err) + } + if !ctrl.stopping.Load() { + t.Fatal("shutdown did not disable admission") + } +} + +// TestCanceledStartup closes its socket while server initialization is still blocked. +func TestCanceledStartup(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + ctrl.cfg.Host = "127.0.0.1" + ctrl.cfg.Port = 0 + release := make(chan struct{}) + ctrl.server.Hooks().OnListen(func(_ fiber.ListenData) error { + <-release + return nil + }) + ctx, cancel := context.WithCancel(t.Context()) + cancel() + // Act. + err := ctrl.Start(ctx) + close(release) + // Assert. + if !errors.Is(err, context.Canceled) { + t.Fatalf("expected canceled startup, got %v", err) + } + select { + case <-ctrl.serveDone: + case <-time.After(time.Second): + t.Fatal("serving goroutine leaked after canceled startup") + } +} diff --git a/internal/controller/http/fiber/mapping.go b/internal/controller/http/fiber/mapping.go new file mode 100644 index 0000000..170569a --- /dev/null +++ b/internal/controller/http/fiber/mapping.go @@ -0,0 +1,79 @@ +package fiber + +import ( + "github.com/gofiber/fiber/v2" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + httpmodel "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/http" +) + +// userResponse converts a user DTO into its HTTP representation. +func userResponse(user dto.UserDTO) httpmodel.UserResponse { + return httpmodel.UserResponse{ + ID: user.ID.String(), + Username: user.Username, + DisplayName: user.DisplayName, + CreatedAt: user.CreatedAt, + } +} + +// chatResponse converts a chat DTO and its member identifiers into an HTTP response. +func chatResponse(chat dto.ChatDTO) httpmodel.ChatResponse { + members := make([]string, len(chat.MemberIDs)) + for index, id := range chat.MemberIDs { + members[index] = id.String() + } + return httpmodel.ChatResponse{ + ID: chat.ID.String(), + Title: chat.Title, + MemberIDs: members, + CreatedAt: chat.CreatedAt, + } +} + +// messageResponse converts a message DTO into its HTTP representation. +func messageResponse(message dto.MessageDTO) httpmodel.MessageResponse { + ids := append([]string{}, message.AttachmentIDs...) + return httpmodel.MessageResponse{ + AttachmentIDs: ids, + ID: message.ID.String(), + ChatID: message.ChatID.String(), + SenderID: message.SenderID.String(), + Text: message.Text, + CreatedAt: message.CreatedAt, + } +} + +// pageRequest extracts identity, resource and pagination parameters from the request. +func pageRequest(ctx *fiber.Ctx) dto.PageRequestDTO { + return dto.PageRequestDTO{ + UserID: identity(ctx), + ChatID: ctx.Params("chat_id"), + Limit: ctx.Query("limit"), + Cursor: ctx.Query("cursor"), + } +} + +// chatPageResponse converts a page of chat DTOs into an HTTP response. +func chatPageResponse(page dto.ChatPageDTO) httpmodel.ChatPageResponse { + response := httpmodel.ChatPageResponse{ + Items: make([]httpmodel.ChatResponse, len(page.Items)), + NextCursor: page.NextCursor, + } + for index, chat := range page.Items { + response.Items[index] = chatResponse(chat) + } + return response +} + +// messagePageResponse converts a page of message DTOs into an HTTP response. +func messagePageResponse(page dto.MessagePageDTO) httpmodel.MessagePageResponse { + response := httpmodel.MessagePageResponse{ + Items: make([]httpmodel.MessageResponse, len(page.Items)), + NextCursor: page.NextCursor, + } + for index, message := range page.Items { + response.Items[index] = messageResponse(message) + } + return response +} diff --git a/internal/controller/http/fiber/messages_test.go b/internal/controller/http/fiber/messages_test.go new file mode 100644 index 0000000..7f2365e --- /dev/null +++ b/internal/controller/http/fiber/messages_test.go @@ -0,0 +1,270 @@ +package fiber + +import ( + "net/http" + "net/url" + "strings" + "testing" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" +) + +// TestController_MessagesCreate checks message validation and identity-derived sender fields. +func TestController_MessagesCreate(t *testing.T) { + t.Run("Positive: Unicode at rune limit", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + id := chat(t, ctrl, owner) + actor := owner + text := strings.Repeat("界", 4000) + payload := mustJSON(t, map[string]string{"text": text}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusCreated, body) + if body["sender_id"] != owner || body["chat_id"] != id || body["text"] != text { + t.Fatalf("message fields changed: %v", body) + } + }) + + t.Run("Negative: empty text", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + id := chat(t, ctrl, owner) + actor := owner + payload := mustJSON(t, map[string]string{"text": ""}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") + }) + + t.Run("Negative: whitespace text", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + id := chat(t, ctrl, owner) + actor := owner + payload := mustJSON(t, map[string]string{"text": " \n\t"}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") + }) + + t.Run("Negative: text exceeds rune limit", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + id := chat(t, ctrl, owner) + actor := owner + payload := mustJSON(t, map[string]string{"text": strings.Repeat("界", 4001)}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") + }) + + t.Run("Negative: sender spoof", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + id := chat(t, ctrl, owner) + actor := owner + outsider := user(t, ctrl) + payload := mustJSON(t, map[string]string{"text": "hello", "sender_id": outsider}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") + }) + + t.Run("Negative: outsider", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + id := chat(t, ctrl, owner) + actor := user(t, ctrl) + payload := mustJSON(t, map[string]string{"text": "hello"}) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusNotFound, body) + assertErrorCode(t, body, "not_found") + }) + + t.Run("Negative: authentication precedes JSON decoding", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + owner := user(t, ctrl) + id := chat(t, ctrl, owner) + actor := "" + payload := []byte(`{`) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/chats/"+id+"/messages", actor, payload) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusUnauthorized, body) + assertErrorCode(t, body, "unauthorized") + }) +} + +// TestController_MessagesGetPage rejects invalid pagination separately from access failures. +func TestController_MessagesGetPage(t *testing.T) { + cases := []struct { + name string + query string + outsider bool + status int + code string + }{ + {"Negative: zero limit", "limit=0", false, http.StatusBadRequest, "invalid_request"}, + {"Negative: limit above maximum", "limit=101", false, http.StatusBadRequest, "invalid_request"}, + {"Negative: negative limit", "limit=-1", false, http.StatusBadRequest, "invalid_request"}, + {"Negative: nonnumeric limit", "limit=x", false, http.StatusBadRequest, "invalid_request"}, + {"Negative: fractional limit", "limit=1.0", false, http.StatusBadRequest, "invalid_request"}, + {"Negative: malformed cursor", "cursor=invalid", false, http.StatusBadRequest, "invalid_request"}, + {"Negative: outsider", "", true, http.StatusNotFound, "not_found"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + id := chat(t, ctrl, actor) + if tc.outsider { + actor = user(t, ctrl) + } + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+id+"/messages?"+tc.query, actor, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, tc.status, body) + assertErrorCode(t, body, tc.code) + }) + } +} + +// TestController_MessageCursorScope rejects a continuation from another chat's history. +func TestController_MessageCursorScope(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + first := chat(t, ctrl, actor) + second := chat(t, ctrl, actor) + path := "/api/v1/chats/" + first + "/messages" + for _, text := range []string{"first", "second"} { + call(t, ctrl, http.MethodPost, path, actor, map[string]string{"text": text}, http.StatusCreated) + } + page := call(t, ctrl, http.MethodGet, path+"?limit=1", actor, nil, http.StatusOK) + token := page["next_cursor"].(string) + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/chats/"+second+"/messages?cursor="+url.QueryEscape(token), actor, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusBadRequest, body) + assertErrorCode(t, body, "invalid_request") +} + +// TestController_MessagePageLimitChange preserves continuation when the client changes page size. +func TestController_MessagePageLimitChange(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + path := "/api/v1/chats/" + chat(t, ctrl, actor) + "/messages" + for _, text := range []string{"first", "second", "third"} { + call(t, ctrl, http.MethodPost, path, actor, map[string]string{"text": text}, http.StatusCreated) + } + first := call(t, ctrl, http.MethodGet, path+"?limit=1", actor, nil, http.StatusOK) + token := first["next_cursor"].(string) + + // Act. + status, body, err := request(ctrl, http.MethodGet, path+"?limit=100&cursor="+url.QueryEscape(token), actor, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusOK, body) + items := body["items"].([]any) + if len(items) != 2 || body["next_cursor"] != nil { + t.Fatalf("unexpected final page: %v", body) + } + if items[0].(map[string]any)["text"] != "second" || items[1].(map[string]any)["text"] != "third" { + t.Fatalf("lost order or skipped messages: %v", items) + } +} + +// TestController_MessageCapacity preserves acknowledged data after rejecting an overflow write. +func TestController_MessageCapacity(t *testing.T) { + // Arrange. + ctrl := testController(t, func(cfg *config.Config) { + cfg.Memory.MaxMessages = 2 + }) + actor := user(t, ctrl) + path := "/api/v1/chats/" + chat(t, ctrl, actor) + "/messages" + first := call(t, ctrl, http.MethodPost, path, actor, map[string]string{"text": "first"}, http.StatusCreated) + second := call(t, ctrl, http.MethodPost, path, actor, map[string]string{"text": "second"}, http.StatusCreated) + + // Act. + status, body, err := request(ctrl, http.MethodPost, path, actor, []byte(`{"text":"rejected"}`)) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusServiceUnavailable, body) + assertErrorCode(t, body, "unavailable") + items := history(t, ctrl, actor, path) + if len(items) != 2 || items[0].(map[string]any)["id"] != first["id"] || items[1].(map[string]any)["id"] != second["id"] { + t.Fatalf("overflow changed acknowledged messages: %v", items) + } +} diff --git a/internal/controller/http/fiber/metrics.go b/internal/controller/http/fiber/metrics.go new file mode 100644 index 0000000..448b7f7 --- /dev/null +++ b/internal/controller/http/fiber/metrics.go @@ -0,0 +1,61 @@ +package fiber + +import ( + "net/http" + "time" + + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/adaptor" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +// httpMetrics owns a per-server registry with bounded labels and no user identifiers. +type httpMetrics struct { + registry *prometheus.Registry + requests *prometheus.CounterVec + duration *prometheus.HistogramVec + active prometheus.Gauge +} + +// newHTTPMetrics creates independent collectors so multiple test servers do not share state. +func newHTTPMetrics() *httpMetrics { + metrics := &httpMetrics{ + registry: prometheus.NewRegistry(), + requests: prometheus.NewCounterVec(prometheus.CounterOpts{Name: "messenger_http_requests_total", Help: "Completed requests by route, method and status class."}, []string{"route", "method", "status_class"}), + duration: prometheus.NewHistogramVec(prometheus.HistogramOpts{Name: "messenger_http_duration_seconds", Help: "Completed request latency.", Buckets: prometheus.DefBuckets}, []string{"route", "method"}), + active: prometheus.NewGauge(prometheus.GaugeOpts{Name: "messenger_http_in_flight", Help: "Currently executing HTTP requests."}), + } + metrics.registry.MustRegister(metrics.requests, metrics.duration, metrics.active) + return metrics +} + +// observe records bounded route patterns, methods and status classes after error handling. +func (m *httpMetrics) observe(ctx *fiber.Ctx, elapsed time.Duration) { + route := "unmatched" + if ctx.Route() != nil && ctx.Route().Path != "/" { + route = ctx.Route().Path + } + method := ctx.Method() + switch method { + case http.MethodGet, http.MethodPost, http.MethodDelete, http.MethodHead, http.MethodOptions: + default: + method = "other" + } + status := "5xx" + switch ctx.Response().StatusCode() / 100 { + case 2: + status = "2xx" + case 3: + status = "3xx" + case 4: + status = "4xx" + } + m.requests.WithLabelValues(route, method, status).Inc() + m.duration.WithLabelValues(route, method).Observe(elapsed.Seconds()) +} + +// handleMetrics exposes this process's Prometheus registry without external backend calls. +func (ctrl *Controller) handleMetrics(ctx *fiber.Ctx) error { + return adaptor.HTTPHandler(promhttp.HandlerFor(ctrl.metrics.registry, promhttp.HandlerOpts{}))(ctx) +} diff --git a/internal/controller/http/fiber/metrics_labels_test.go b/internal/controller/http/fiber/metrics_labels_test.go new file mode 100644 index 0000000..73cc983 --- /dev/null +++ b/internal/controller/http/fiber/metrics_labels_test.go @@ -0,0 +1,38 @@ +package fiber + +import ( + "net/http" + "testing" + "time" + + "github.com/valyala/fasthttp" +) + +// TestMetricsBoundUnknownMethods keeps arbitrary client methods out of metric labels. +func TestMetricsBoundUnknownMethods(t *testing.T) { + ctrl := testController(t, nil) + raw := &fasthttp.RequestCtx{} + raw.Request.Header.SetMethod("CUSTOM_RANDOM_METHOD") + raw.Response.SetStatusCode(http.StatusTemporaryRedirect) + ctx := ctrl.server.AcquireCtx(raw) + defer ctrl.server.ReleaseCtx(ctx) + ctrl.metrics.observe(ctx, time.Millisecond) + families, err := ctrl.metrics.registry.Gather() + if err != nil { + t.Fatal(err) + } + found := false + for _, family := range families { + for _, metric := range family.Metric { + for _, label := range metric.Label { + if label.GetValue() == "CUSTOM_RANDOM_METHOD" { + t.Fatal("unbounded method label") + } + found = found || label.GetValue() == "other" + } + } + } + if !found { + t.Fatal("unknown method was not grouped") + } +} diff --git a/internal/controller/http/fiber/metrics_test.go b/internal/controller/http/fiber/metrics_test.go new file mode 100644 index 0000000..9f2d4e9 --- /dev/null +++ b/internal/controller/http/fiber/metrics_test.go @@ -0,0 +1,73 @@ +package fiber + +import ( + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// TestMetricsEndpoint exposes completed requests without token or user labels. +func TestMetricsEndpoint(t *testing.T) { + ctrl := testController(t, nil) + call(t, ctrl, http.MethodGet, "/health/live", "", nil, http.StatusOK) + resp, err := ctrl.server.Test(httptest.NewRequest(http.MethodGet, "/metrics", nil)) + if err != nil { + t.Fatal(err) + } + defer func() { + _ = resp.Body.Close() + }() + body, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatal(err) + } + if resp.StatusCode != http.StatusOK || !strings.Contains(string(body), "messenger_http_requests_total") || !strings.Contains(string(body), "messenger_http_duration_seconds_bucket") { + t.Fatalf("invalid metrics response: status %d", resp.StatusCode) + } + if resp.Header.Get("X-Instance-Id") == "" { + t.Fatal("metrics response lacks instance ID") + } +} + +// TestInstanceIdentityRemainsStable verifies errors and successes identify the same process. +func TestInstanceIdentityRemainsStable(t *testing.T) { + ctrl := testController(t, nil) + var instance string + for _, path := range []string{"/health/live", "/unknown"} { + resp, err := ctrl.server.Test(httptest.NewRequest(http.MethodGet, path, nil)) + if err != nil { + t.Fatal(err) + } + if instance == "" { + instance = resp.Header.Get("X-Instance-Id") + } + if instance == "" || resp.Header.Get("X-Instance-Id") != instance { + t.Fatal("unstable instance identity") + } + if err := resp.Body.Close(); err != nil { + t.Fatal(err) + } + } +} + +// TestMissingIdempotencyHeader rejects an otherwise valid authenticated write. +func TestMissingIdempotencyHeader(t *testing.T) { + ctrl := testController(t, nil) + caller := user(t, ctrl) + room := chat(t, ctrl, caller) + req := httptest.NewRequest(http.MethodPost, "/api/v1/chats/"+room+"/messages", strings.NewReader(`{"text":"valid"}`)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Bearer "+caller) + resp, err := ctrl.server.Test(req) + if err != nil { + t.Fatal(err) + } + defer func() { + _ = resp.Body.Close() + }() + if resp.StatusCode != http.StatusBadRequest { + t.Fatalf("missing key accepted: %d", resp.StatusCode) + } +} diff --git a/internal/controller/http/fiber/middleware.go b/internal/controller/http/fiber/middleware.go new file mode 100644 index 0000000..a2ba32d --- /dev/null +++ b/internal/controller/http/fiber/middleware.go @@ -0,0 +1,90 @@ +package fiber + +import ( + "context" + "errors" + "strconv" + "time" + + "github.com/gofiber/fiber/v2" + "github.com/google/uuid" + "go.uber.org/zap" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/admission" +) + +// request assigns a request identifier, handles errors and logs the completed request. +func (ctrl *Controller) request(ctx *fiber.Ctx) error { + ctx.Set(headerRequestID, uuid.NewString()) + ctx.Set("X-Instance-Id", ctrl.instanceID) + started := time.Now() + ctrl.metrics.active.Inc() + defer ctrl.metrics.active.Dec() + err := ctx.Next() + if err != nil { + err = ctrl.handleErr(ctx, err) + } + ctrl.logRequest(ctx, time.Since(started)) + if ctx.Path() != "/metrics" { + ctrl.metrics.observe(ctx, time.Since(started)) + } + return err +} + +// logRequest logs request metadata without including message contents. +func (ctrl *Controller) logRequest(ctx *fiber.Ctx, elapsed time.Duration) { + route := "unmatched" + if ctx.Route() != nil { + route = ctx.Route().Path + } + ctrl.log.Info("http request", + zap.String("method", ctx.Method()), + zap.String("route", route), + zap.Int("status", ctx.Response().StatusCode()), + zap.String("request_id", ctx.GetRespHeader(headerRequestID)), + zap.Duration("duration", elapsed), + ) +} + +// admit reserves global budgets and bounds downstream request work. +func (ctrl *Controller) admit(ctx *fiber.Ctx) error { + if ctrl.stopping.Load() { + return errBusy + } + requestCtx, cancel := context.WithTimeout(ctx.UserContext(), ctrl.cfg.Timeout()) + ctx.SetUserContext(requestCtx) + release, err := ctrl.admission.Enter(requestCtx, requestGroup(ctx), "") + if err != nil { + cancel() + return admissionError(ctx, err) + } + defer finishRequest(ctx, func() { + release() + cancel() + }) + return ctx.Next() +} + +// requireIdentity resolves the session before reserving per-user budgets. +func (ctrl *Controller) requireIdentity(ctx *fiber.Ctx) error { + user, err := ctrl.auth.Authenticate(ctx.UserContext(), bearer(ctx)) + if err != nil { + return err + } + ctx.Locals(identityKey, user) + release, err := ctrl.admission.Enter(ctx.UserContext(), requestGroup(ctx), user) + if err != nil { + return admissionError(ctx, err) + } + defer finishRequest(ctx, release) + return ctx.Next() +} + +// admissionError maps policy rejection and backend failure into HTTP-owned errors. +func admissionError(ctx *fiber.Ctx, err error) error { + ctx.Set(fiber.HeaderRetryAfter, strconv.Itoa(retryAfterSeconds)) + if errors.Is(err, admission.ErrRate) { + return errRateLimited + } + return errBusy +} diff --git a/internal/controller/http/fiber/middleware_test.go b/internal/controller/http/fiber/middleware_test.go new file mode 100644 index 0000000..af521e5 --- /dev/null +++ b/internal/controller/http/fiber/middleware_test.go @@ -0,0 +1,211 @@ +package fiber + +import ( + "net/http" + "testing" + "testing/synctest" + "time" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" +) + +// TestController_InFlightAdmission checks rejection, recovery and health exemption independently. +func TestController_InFlightAdmission(t *testing.T) { + cases := []struct { + name string + release bool + path string + status int + }{ + {"Negative: occupied execution slot", false, "/api/v1/users/me", http.StatusServiceUnavailable}, + {"Positive: released execution slot", true, "/api/v1/users/me", http.StatusUnauthorized}, + {"Positive: health bypasses occupied slot", false, "/health/live", http.StatusOK}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + ctrl := testController(t, func(cfg *config.Config) { + cfg.HTTP.Admission.MaxInFlight = 1 + }) + if !ctrl.admission.(*fixtureAdmission).Acquire() { + t.Fatal("fixture could not reserve slot") + } + if tc.release { + ctrl.admission.(*fixtureAdmission).Release() + } else { + t.Cleanup(ctrl.admission.(*fixtureAdmission).Release) + } + + // Act. + status, body, err := request(ctrl, http.MethodGet, tc.path, "", nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, tc.status, body) + }) + } +} + +// TestController_RateAdmission checks exhausted tokens without timing-dependent HTTP setup. +func TestController_RateAdmission(t *testing.T) { + t.Run("/api/v1/users/me", func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + // Arrange. + ctrl := testController(t, func(cfg *config.Config) { + cfg.HTTP.Admission.RequestsPerSecond = 1 + cfg.HTTP.Admission.Burst = 1 + }) + if !ctrl.admission.(*fixtureAdmission).Allow(time.Now()) { + t.Fatal("fixture token missing") + } + expected := http.StatusTooManyRequests + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/users/me", "", nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, expected, body) + }) + }) + + t.Run("/health/ready", func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + // Arrange. + ctrl := testController(t, func(cfg *config.Config) { + cfg.HTTP.Admission.RequestsPerSecond = 1 + cfg.HTTP.Admission.Burst = 1 + }) + if !ctrl.admission.(*fixtureAdmission).Allow(time.Now()) { + t.Fatal("fixture token missing") + } + expected := http.StatusOK + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/health/ready", "", nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, expected, body) + }) + }) +} + +// TestController_Draining checks each endpoint's admission policy during shutdown. +func TestController_Draining(t *testing.T) { + cases := []struct { + name string + path string + status int + }{ + {"Negative: readiness disabled", "/health/ready", http.StatusServiceUnavailable}, + {"Positive: liveness remains available", "/health/live", http.StatusOK}, + {"Negative: business requests rejected", "/api/v1/users/me", http.StatusServiceUnavailable}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + ctrl.stopping.Store(true) + + // Act. + status, body, err := request(ctrl, http.MethodGet, tc.path, "", nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, tc.status, body) + }) + } +} + +// TestController_UnknownRoute checks the public fallback for an unmatched path. +func TestController_UnknownRoute(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/missing", "", nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusNotFound, body) + assertErrorCode(t, body, "not_found") +} + +// TestController_New rejects each missing dependency independently. +func TestController_New(t *testing.T) { + t.Run("Negative: missing configuration", func(t *testing.T) { + // Arrange. + fixture := testController(t, nil) + + // Act. + ctrl, err := New(fixture.log, nil, fixture.service, fixture.errorRegistry, fixture.admission, fixture.auth, fixture.readiness) + + // Assert. + if err == nil || ctrl != nil { + t.Fatalf("incomplete controller accepted: %v, %v", ctrl, err) + } + }) + + t.Run("Negative: missing logger", func(t *testing.T) { + // Arrange. + fixture := testController(t, nil) + + // Act. + ctrl, err := New(nil, &fixture.cfg, fixture.service, fixture.errorRegistry, fixture.admission, fixture.auth, fixture.readiness) + + // Assert. + if err == nil || ctrl != nil { + t.Fatalf("incomplete controller accepted: %v, %v", ctrl, err) + } + }) + + t.Run("Negative: missing service", func(t *testing.T) { + // Arrange. + fixture := testController(t, nil) + + // Act. + ctrl, err := New(fixture.log, &fixture.cfg, nil, fixture.errorRegistry, fixture.admission, fixture.auth, fixture.readiness) + + // Assert. + if err == nil || ctrl != nil { + t.Fatalf("incomplete controller accepted: %v, %v", ctrl, err) + } + }) + + t.Run("Negative: missing registry", func(t *testing.T) { + // Arrange. + fixture := testController(t, nil) + + // Act. + ctrl, err := New(fixture.log, &fixture.cfg, fixture.service, nil, fixture.admission, fixture.auth, fixture.readiness) + + // Assert. + if err == nil || ctrl != nil { + t.Fatalf("incomplete controller accepted: %v, %v", ctrl, err) + } + }) + + t.Run("Negative: missing limiter", func(t *testing.T) { + // Arrange. + fixture := testController(t, nil) + + // Act. + ctrl, err := New(fixture.log, &fixture.cfg, fixture.service, fixture.errorRegistry, nil, fixture.auth, fixture.readiness) + + // Assert. + if err == nil || ctrl != nil { + t.Fatalf("incomplete controller accepted: %v, %v", ctrl, err) + } + }) +} diff --git a/internal/controller/http/fiber/public_errors.go b/internal/controller/http/fiber/public_errors.go new file mode 100644 index 0000000..7743989 --- /dev/null +++ b/internal/controller/http/fiber/public_errors.go @@ -0,0 +1,55 @@ +package fiber + +import ( + "errors" + + "go.lab260.ru/fielderr" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +var ( + errBusy = publicFailure(codeUnavailable, "server is busy") + errRateLimited = publicFailure(codeRateLimited, "request rate exceeded") + errTooLarge = publicFailure(codeTooLarge, "request body is too large") + errMedia = publicFailure(codeMedia, "expected application/json") + errInternal = publicFailure(codeInternal, "internal server error") +) + +// publicFailure creates a transport-owned error with a safe API message. +func publicFailure(code, message string) error { + return fielderr.WithStatus(errors.New(message), fielderr.NewStatusCode(code), message) +} + +// publicError maps application failures into the public API envelope. +// Unknown errors receive a fixed response without exposing their details. +func publicError(err error) error { + switch { + case errors.Is(err, service.ErrTooLarge): + return errTooLarge + case errors.Is(err, service.ErrMedia): + return errMedia + case errors.Is(err, service.ErrUnavailable): + return publicFailure(codeUnavailable, "dependency unavailable") + case errors.Is(err, service.ErrInvalid): + return fielderr.WithStatus(err, fielderr.NewStatusCode(codeInvalid), "invalid request") + case errors.Is(err, service.ErrUnauthorized): + return fielderr.WithStatus(err, fielderr.NewStatusCode(codeUnauthorized), "unknown user identity") + case errors.Is(err, service.ErrNotFound): + return fielderr.WithStatus(err, fielderr.NewStatusCode(codeNotFound), "resource not found") + case errors.Is(err, service.ErrConflict): + return fielderr.WithStatus(err, fielderr.NewStatusCode(codeConflict), "username already exists") + case errors.Is(err, service.ErrCapacity): + return fielderr.WithStatus(err, fielderr.NewStatusCode(codeUnavailable), "in-memory storage capacity reached") + case errors.Is(err, errBusy): + return errBusy + case errors.Is(err, errRateLimited): + return errRateLimited + case errors.Is(err, errTooLarge): + return errTooLarge + case errors.Is(err, errMedia): + return errMedia + default: + return errInternal + } +} diff --git a/internal/controller/http/fiber/public_errors_test.go b/internal/controller/http/fiber/public_errors_test.go new file mode 100644 index 0000000..39a04d5 --- /dev/null +++ b/internal/controller/http/fiber/public_errors_test.go @@ -0,0 +1,43 @@ +package fiber + +import ( + "errors" + "fmt" + "testing" + + "go.lab260.ru/fielderr" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// TestPublicErrorSafety maps wrapped application failures without exposing internal details. +func TestPublicErrorSafety(t *testing.T) { + t.Run("wrapped application error", func(t *testing.T) { + err := publicError(fmt.Errorf("private database detail: %w", service.ErrConflict)) + response := errorResponse(err, "request") + if response.Error.Code != codeConflict || response.Error.Message != "username already exists" { + t.Fatalf("unexpected response: %+v", response) + } + }) + t.Run("unknown error", func(t *testing.T) { + err := publicError(errors.New("private database detail")) + if err != errInternal { + t.Fatalf("unexpected public error: %v", err) + } + }) + t.Run("foreign public status", func(t *testing.T) { + err := fielderr.WithStatus(errors.New("private detail"), fielderr.NewStatusCode(codeInvalid), "private detail") + if publicError(err) != errInternal { + t.Fatal("untrusted error metadata reached the public response") + } + }) +} + +// TestPublicErrorUnavailable keeps dependency details out of the public envelope. +func TestPublicErrorUnavailable(t *testing.T) { + err := publicError(fmt.Errorf("private database detail: %w", service.ErrUnavailable)) + response := errorResponse(err, "request") + if response.Error.Code != codeUnavailable || response.Error.Message != "dependency unavailable" { + t.Fatalf("unexpected response: %+v", response) + } +} diff --git a/internal/controller/http/fiber/routes.go b/internal/controller/http/fiber/routes.go new file mode 100644 index 0000000..31f17de --- /dev/null +++ b/internal/controller/http/fiber/routes.go @@ -0,0 +1,26 @@ +package fiber + +// initRoutes registers health endpoints and authenticated application routes. +func (ctrl *Controller) initRoutes() { + ctrl.server.Get("/metrics", ctrl.handleMetrics) + ctrl.server.Get("/health/live", ctrl.handleLive) + ctrl.server.Get("/health/ready", ctrl.handleReady) + ctrl.server.Use(ctrl.admit) + ctrl.server.Post("/api/v1/users", ctrl.HandleUsersCreate) + + ctrl.server.Post("/api/v1/auth/sessions", ctrl.HandleLogin) + + api := ctrl.server.Group("/api/v1", ctrl.requireIdentity) + api.Delete("/auth/sessions/current", ctrl.HandleLogout) + api.Get("/users/me", ctrl.HandleUsersGetMe) + api.Post("/chats", ctrl.HandleChatsCreate) + api.Get("/chats", ctrl.HandleChatsGetPage) + api.Get("/chats/:chat_id", ctrl.HandleChatsGetByID) + api.Post("/chats/:chat_id/attachments", ctrl.handleUpload) + api.Get("/attachments/:id", ctrl.handleAttachment) + api.Get("/attachments/:id/content", ctrl.handleContent) + api.Post("/chats/:chat_id/messages", ctrl.HandleMessagesCreate) + api.Get("/chats/:chat_id/messages", ctrl.HandleMessagesGetPage) + + ctrl.server.Use(ctrl.handleNotFound) +} diff --git a/internal/controller/http/fiber/stream.go b/internal/controller/http/fiber/stream.go new file mode 100644 index 0000000..4fef6d8 --- /dev/null +++ b/internal/controller/http/fiber/stream.go @@ -0,0 +1,40 @@ +package fiber + +import ( + "io" + "sync" + + "github.com/gofiber/fiber/v2" +) + +type responseStream struct { + io.Reader + cleanup func() + once sync.Once +} + +// Close releases request budgets after fasthttp finishes transmitting the stream. +func (s *responseStream) Close() error { + var err error + s.once.Do(func() { + if closer, ok := s.Reader.(io.Closer); ok { + err = closer.Close() + } + s.cleanup() + }) + return err +} + +// finishRequest keeps deadlines and admission leases alive until streamed responses close. +func finishRequest(ctx *fiber.Ctx, cleanup func()) { + stream, ok := ctx.Response().BodyStream().(*responseStream) + if !ok { + cleanup() + return + } + previous := stream.cleanup + stream.cleanup = func() { + previous() + cleanup() + } +} diff --git a/internal/controller/http/fiber/users_test.go b/internal/controller/http/fiber/users_test.go new file mode 100644 index 0000000..61d7e35 --- /dev/null +++ b/internal/controller/http/fiber/users_test.go @@ -0,0 +1,130 @@ +package fiber + +import ( + "net/http" + "strings" + "testing" + + "github.com/google/uuid" +) + +// TestController_UsersCreate checks one registration contract scenario per subtest. +func TestController_UsersCreate(t *testing.T) { + cases := []struct { + name string + body string + status int + }{ + {"Positive: valid Unicode display name", `{"password":"fixture-password-123","username":"alice","display_name":"Алиса 👋"}`, http.StatusCreated}, + {"Positive: display name at rune limit", `{"password":"fixture-password-123","username":"alice","display_name":"` + strings.Repeat("👋", 100) + `"}`, http.StatusCreated}, + {"Negative: display name exceeds rune limit", `{"password":"fixture-password-123","username":"alice","display_name":"` + strings.Repeat("👋", 101) + `"}`, http.StatusBadRequest}, + {"Negative: uppercase username", `{"password":"fixture-password-123","username":"Alice","display_name":"Alice"}`, http.StatusBadRequest}, + {"Negative: malformed JSON", `{`, http.StatusBadRequest}, + {"Negative: null root", `null`, http.StatusBadRequest}, + {"Negative: array root", `[]`, http.StatusBadRequest}, + {"Negative: missing fields", `{}`, http.StatusBadRequest}, + {"Negative: null field", `{"password":"fixture-password-123","username":"alice","display_name":null}`, http.StatusBadRequest}, + {"Negative: wrong field type", `{"password":"fixture-password-123","username":5,"display_name":"Alice"}`, http.StatusBadRequest}, + {"Negative: unknown field", `{"password":"fixture-password-123","username":"alice","display_name":"Alice","unknown":"secret"}`, http.StatusBadRequest}, + {"Negative: field name case", `{"password":"fixture-password-123","Username":"alice","display_name":"Alice"}`, http.StatusBadRequest}, + {"Negative: duplicate field", `{"password":"fixture-password-123","username":"alice","username":"bob","display_name":"Alice"}`, http.StatusBadRequest}, + {"Negative: trailing object", `{"password":"fixture-password-123","username":"alice","display_name":"Alice"} {}`, http.StatusBadRequest}, + {"Negative: invalid UTF-8", "{\"username\":\"alice\",\"display_name\":\"\xff\"}", http.StatusBadRequest}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + + // Act. + status, body, err := request(ctrl, http.MethodPost, "/api/v1/users", "", []byte(tc.body)) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, tc.status, body) + if tc.status == http.StatusBadRequest { + assertErrorCode(t, body, "invalid_request") + return + } + if body["username"] != "alice" || body["display_name"] == "" { + t.Fatalf("registration fields changed: %v", body) + } + if _, err := uuid.Parse(body["id"].(string)); err != nil { + t.Fatalf("invalid assigned ID: %v", err) + } + }) + } +} + +// TestController_UsersGetMe verifies profile selection and authentication failures. +func TestController_UsersGetMe(t *testing.T) { + t.Run("Positive: own profile", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + actor := user(t, ctrl) + identity := actor + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/users/me", identity, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusOK, body) + if body["id"] != actor || body["display_name"] != "Алиса 👋" { + t.Fatalf("wrong profile: %v", body) + } + }) + + t.Run("Negative: missing identity", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + identity := "" + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/users/me", identity, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusUnauthorized, body) + assertErrorCode(t, body, "unauthorized") + }) + + t.Run("Negative: unknown identity", func(t *testing.T) { + // Arrange. + ctrl := testController(t, nil) + identity := uuid.NewString() + + // Act. + status, body, err := request(ctrl, http.MethodGet, "/api/v1/users/me", identity, nil) + + // Assert. + if err != nil { + t.Fatal(err) + } + assertHTTPStatus(t, status, http.StatusUnauthorized, body) + assertErrorCode(t, body, "unauthorized") + }) +} + +// assertHTTPStatus reports the response body alongside a mismatched status. +func assertHTTPStatus(t *testing.T, got, want int, body map[string]any) { + t.Helper() + if got != want { + t.Fatalf("status=%d, want=%d, body=%v", got, want, body) + } +} + +// assertErrorCode checks the documented error classification independently of status. +func assertErrorCode(t *testing.T, body map[string]any, want string) { + t.Helper() + envelope, ok := body["error"].(map[string]any) + if !ok || envelope["code"] != want { + t.Fatalf("error=%v, want code %q", body, want) + } +} diff --git a/internal/controller/interface.go b/internal/controller/interface.go new file mode 100644 index 0000000..46e9af5 --- /dev/null +++ b/internal/controller/interface.go @@ -0,0 +1,12 @@ +// Package controller connects transport lifecycles to Fx. +package controller + +import "context" + +// Controller is a process-owned transport server. +type Controller interface { + // Start acquires the listener and reports startup errors to the lifecycle owner. + Start(context.Context) error + // Stop stops accepting work and drains active requests within the supplied deadline. + Stop(context.Context) error +} diff --git a/internal/cursor/codec.go b/internal/cursor/codec.go new file mode 100644 index 0000000..6f4cd94 --- /dev/null +++ b/internal/cursor/codec.go @@ -0,0 +1,119 @@ +// Package cursor signs and verifies resource-bound keyset positions. +package cursor + +import ( + "crypto/hmac" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "time" + + "github.com/google/uuid" +) + +const ( + cursorSigningKeyBytes = 32 + maxEncodedCursorBytes = 1024 +) + +// ErrInvalid means a cursor is malformed, tampered with or bound to another resource. +var ErrInvalid = errors.New("invalid cursor") + +// Position identifies the last record already returned by keyset pagination. +type Position struct { + // CreatedAt is the creation timestamp of the last returned record. + CreatedAt time.Time + // ID breaks ties between records sharing a timestamp. + ID uuid.UUID +} + +// Codec authenticates pagination positions with an immutable process-local key. +type Codec struct { + key [cursorSigningKeyBytes]byte +} + +type cursorPayload struct { + // Resource binds the position to a chat history or one user's chat list. + Resource string `json:"r"` + // Time is the creation timestamp of the last record in the previous page. + Time time.Time `json:"t"` + // ID breaks ties between records with the same creation timestamp. + ID uuid.UUID `json:"i"` +} + +// New creates a cursor codec with a random process-local signing key. +func New() (*Codec, error) { + codec := &Codec{} + if _, err := rand.Read(codec.key[:]); err != nil { + return nil, err + } + return codec, nil +} + +// Encode serializes and signs a pagination position bound to a resource. +func (codec *Codec) Encode(resource string, position Position) (string, error) { + payload, err := json.Marshal(cursorPayload{ + Resource: resource, + Time: position.CreatedAt, + ID: position.ID, + }) + if err != nil { + return "", fmt.Errorf("encode cursor: %w", err) + } + payload = append(payload, codec.signature(payload)...) + return base64.RawURLEncoding.EncodeToString(payload), nil +} + +// Decode verifies the cursor signature and extracts its resource-bound position. +func (codec *Codec) Decode(encoded, resource string) (Position, error) { + if encoded == "" { + return Position{}, nil + } + if len(encoded) > maxEncodedCursorBytes { + return Position{}, ErrInvalid + } + signed, err := base64.RawURLEncoding.DecodeString(encoded) + if err != nil || len(signed) <= sha256.Size { + return Position{}, ErrInvalid + } + boundary := len(signed) - sha256.Size + payload, signature := signed[:boundary], signed[boundary:] + if !hmac.Equal(signature, codec.signature(payload)) { + return Position{}, ErrInvalid + } + return decodeCursorPosition(payload, resource) +} + +// signature computes the HMAC-SHA256 authentication tag for a cursor payload. +func (codec *Codec) signature(payload []byte) []byte { + mac := hmac.New(sha256.New, codec.key[:]) + // hash.Hash.Write is documented to always return a nil error. + _, _ = mac.Write(payload) + return mac.Sum(nil) +} + +// decodeCursorPosition validates the decoded cursor resource and timestamp. +func decodeCursorPosition(payload []byte, resource string) (Position, error) { + var cursor cursorPayload + if err := json.Unmarshal(payload, &cursor); err != nil { + return Position{}, ErrInvalid + } + if cursor.Resource != resource || cursor.Time.IsZero() { + return Position{}, ErrInvalid + } + return Position{CreatedAt: cursor.Time, ID: cursor.ID}, nil +} + +// NewShared creates a codec from the common 256-bit key provisioned to every replica. +func NewShared(encoded string) (*Codec, error) { + key, err := base64.StdEncoding.DecodeString(encoded) + if err != nil || len(key) != cursorSigningKeyBytes { + return nil, fmt.Errorf("CURSOR_SIGNING_KEY must encode exactly 32 bytes as base64") + } + codec := &Codec{} + copy(codec.key[:], key) + return codec, nil +} diff --git a/internal/cursor/codec_test.go b/internal/cursor/codec_test.go new file mode 100644 index 0000000..441e530 --- /dev/null +++ b/internal/cursor/codec_test.go @@ -0,0 +1,212 @@ +package cursor + +import ( + "encoding/base64" + "errors" + "strings" + "testing" + "time" + + "github.com/google/uuid" +) + +// TestCodec_EncodeDecode preserves the complete position through an authenticated round trip. +func TestCodec_EncodeDecode(t *testing.T) { + // Arrange. + codec := newTestCodec(t) + position := Position{CreatedAt: time.Unix(1700000000, 123456789).UTC(), ID: uuid.New()} + + // Act. + encoded, err := codec.Encode("chats:alice", position) + + // Assert. + if err != nil { + t.Fatal(err) + } + if encoded == "" { + t.Fatal("encoded position is empty") + } + decoded, err := codec.Decode(encoded, "chats:alice") + if err != nil || decoded != position { + t.Fatalf("decoded=%v, want=%v, error=%v", decoded, position, err) + } +} + +// TestCodec_Decode checks first-page behavior and independent rejection conditions. +func TestCodec_Decode(t *testing.T) { + t.Run("Positive: empty token starts first page", func(t *testing.T) { + // Arrange. + codec := newTestCodec(t) + + // Act. + got, err := codec.Decode("", "chats:alice") + + // Assert. + if err != nil || got != (Position{}) { + t.Fatalf("first-page position=%v, error=%v", got, err) + } + }) + + t.Run("Negative: another resource", func(t *testing.T) { + // Arrange. + codec := newTestCodec(t) + encoded := encodeFixture(t, codec, "chats:alice") + + // Act. + got, err := codec.Decode(encoded, "chats:bob") + + // Assert. + if !errors.Is(err, ErrInvalid) || got != (Position{}) { + t.Fatalf("position=%v, error=%v", got, err) + } + }) + + t.Run("Negative: another process key", func(t *testing.T) { + // Arrange. + codec := newTestCodec(t) + encoded := encodeFixture(t, codec, "chats:alice") + codec = newTestCodec(t) + + // Act. + got, err := codec.Decode(encoded, "chats:alice") + + // Assert. + if !errors.Is(err, ErrInvalid) || got != (Position{}) { + t.Fatalf("position=%v, error=%v", got, err) + } + }) + + t.Run("Negative: tampered payload", func(t *testing.T) { + // Arrange. + codec := newTestCodec(t) + encoded := encodeFixture(t, codec, "chats:alice") + signed, err := base64.RawURLEncoding.DecodeString(encoded) + if err != nil { + t.Fatal(err) + } + signed[0] ^= 1 + encoded = base64.RawURLEncoding.EncodeToString(signed) + + // Act. + got, err := codec.Decode(encoded, "chats:alice") + + // Assert. + if !errors.Is(err, ErrInvalid) || got != (Position{}) { + t.Fatalf("position=%v, error=%v", got, err) + } + }) + + t.Run("Negative: oversized token", func(t *testing.T) { + // Arrange. + codec := newTestCodec(t) + encoded := strings.Repeat("a", 1025) + + // Act. + got, err := codec.Decode(encoded, "chats:alice") + + // Assert. + if !errors.Is(err, ErrInvalid) || got != (Position{}) { + t.Fatalf("position=%v, error=%v", got, err) + } + }) + + t.Run("Negative: malformed base64", func(t *testing.T) { + // Arrange. + codec := newTestCodec(t) + encoded := "!invalid!" + + // Act. + got, err := codec.Decode(encoded, "chats:alice") + + // Assert. + if !errors.Is(err, ErrInvalid) || got != (Position{}) { + t.Fatalf("position=%v, error=%v", got, err) + } + }) + + t.Run("Negative: signed malformed JSON", func(t *testing.T) { + // Arrange. + codec := newTestCodec(t) + encoded := signedPayload(codec, []byte("not JSON")) + + // Act. + got, err := codec.Decode(encoded, "chats:alice") + + // Assert. + if !errors.Is(err, ErrInvalid) || got != (Position{}) { + t.Fatalf("position=%v, error=%v", got, err) + } + }) + + t.Run("Negative: signed zero timestamp", func(t *testing.T) { + // Arrange. + codec := newTestCodec(t) + encoded := signedPayload(codec, []byte(`{"r":"chats:alice","t":"0001-01-01T00:00:00Z","i":"11111111-1111-1111-1111-111111111111"}`)) + + // Act. + got, err := codec.Decode(encoded, "chats:alice") + + // Assert. + if !errors.Is(err, ErrInvalid) || got != (Position{}) { + t.Fatalf("position=%v, error=%v", got, err) + } + }) +} + +// signedPayload creates authenticated malformed fixtures to isolate payload validation. +func signedPayload(codec *Codec, payload []byte) string { + return base64.RawURLEncoding.EncodeToString(append(payload, codec.signature(payload)...)) +} + +// newTestCodec creates an independent signing key for one scenario. +func newTestCodec(t *testing.T) *Codec { + t.Helper() + codec, err := New() + if err != nil { + t.Fatal(err) + } + return codec +} + +// encodeFixture prepares a valid token before testing a separate decoding condition. +func encodeFixture(t *testing.T, codec *Codec, resource string) string { + t.Helper() + token, err := codec.Encode(resource, Position{ID: uuid.New(), CreatedAt: time.Unix(1700000000, 0).UTC()}) + if err != nil { + t.Fatal(err) + } + return token +} + +// TestSharedCodec verifies a second process can verify the first process's cursor. +func TestSharedCodec(t *testing.T) { + key := base64.StdEncoding.EncodeToString(make([]byte, 32)) + first, err := NewShared(key) + if err != nil { + t.Fatal(err) + } + second, err := NewShared(key) + if err != nil { + t.Fatal(err) + } + position := Position{CreatedAt: time.Now().UTC(), ID: uuid.New()} + encoded, err := first.Encode("chat", position) + if err != nil { + t.Fatal(err) + } + result, err := second.Decode(encoded, "chat") + if err != nil || result != position { + t.Fatalf("cross-replica cursor: %v", err) + } +} + +// TestSharedCodecRejectsInvalidKey requires a full 256-bit decoded secret. +func TestSharedCodecRejectsInvalidKey(t *testing.T) { + for _, key := range []string{"", "broken!", base64.StdEncoding.EncodeToString([]byte("short"))} { + t.Run(key, func(t *testing.T) { + if _, err := NewShared(key); err == nil { + t.Fatal("invalid key accepted") + } + }) + } +} diff --git a/internal/hashring/ring.go b/internal/hashring/ring.go new file mode 100644 index 0000000..f8c0362 --- /dev/null +++ b/internal/hashring/ring.go @@ -0,0 +1,62 @@ +// Package hashring implements immutable consistent hashing with virtual nodes. +package hashring + +import ( + "cmp" + "crypto/sha256" + "encoding/binary" + "fmt" + "slices" + "sort" + "strconv" +) + +type point struct { + hash uint64 + node string +} + +// Ring maps arbitrary keys to stable node identifiers. +type Ring struct { + points []point +} + +// New creates an order-independent ring and rejects ambiguous node identities. +func New(nodes []string, replicas int) (*Ring, error) { + if len(nodes) == 0 || replicas < 1 { + return nil, fmt.Errorf("nodes and positive virtual node count are required") + } + ring := &Ring{} + seen := make(map[string]bool) + for _, node := range nodes { + if node == "" || seen[node] { + return nil, fmt.Errorf("node identifiers must be unique and nonempty") + } + seen[node] = true + for index := range replicas { + ring.points = append(ring.points, point{hash: hash(node + ":" + strconv.Itoa(index)), node: node}) + } + } + slices.SortFunc(ring.points, func(a, b point) int { + if order := cmp.Compare(a.hash, b.hash); order != 0 { + return order + } + return cmp.Compare(a.node, b.node) + }) + return ring, nil +} + +// Owner selects the first virtual node clockwise from the key hash. +func (r *Ring) Owner(key string) string { + value := hash(key) + index := sort.Search(len(r.points), func(index int) bool { + return r.points[index].hash >= value + }) + return r.points[index%len(r.points)].node +} + +// hash maps a stable string into the ring's 64-bit coordinate space. +func hash(value string) uint64 { + sum := sha256.Sum256([]byte(value)) + return binary.BigEndian.Uint64(sum[:8]) +} diff --git a/internal/hashring/ring_test.go b/internal/hashring/ring_test.go new file mode 100644 index 0000000..56bd762 --- /dev/null +++ b/internal/hashring/ring_test.go @@ -0,0 +1,73 @@ +package hashring + +import ( + "fmt" + "testing" +) + +// TestExpansionMovesOnlyKeysOwnedByNewNode verifies the consistent hashing invariant. +func TestExpansionMovesOnlyKeysOwnedByNewNode(t *testing.T) { + before, err := New([]string{"a", "b"}, 256) + if err != nil { + t.Fatal(err) + } + after, err := New([]string{"c", "b", "a"}, 256) + if err != nil { + t.Fatal(err) + } + moved := 0 + for index := range 100000 { + key := fmt.Sprint(index) + oldOwner, newOwner := before.Owner(key), after.Owner(key) + if oldOwner != newOwner { + moved++ + if newOwner != "c" { + t.Fatalf("existing nodes exchanged key %s", key) + } + } + } + fraction := float64(moved) / 100000 + if fraction < 0.2 || fraction > 0.45 { + t.Fatalf("unexpected moved fraction: %f", fraction) + } + t.Logf("100000 keys: moved=%d fraction=%.5f", moved, fraction) +} + +// TestRingRejectsAmbiguousConfiguration checks independent malformed rings. +func TestRingRejectsAmbiguousConfiguration(t *testing.T) { + cases := []struct { + name string + nodes []string + replicas int + }{ + {"empty", nil, 1}, + {"zero replicas", []string{"a"}, 0}, + {"empty node", []string{""}, 1}, + {"duplicate", []string{"a", "a"}, 1}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if _, err := New(tc.nodes, tc.replicas); err == nil { + t.Fatal("invalid ring accepted") + } + }) + } +} + +// TestRingIgnoresInputOrder verifies that replicas agree despite configuration ordering. +func TestRingIgnoresInputOrder(t *testing.T) { + a, err := New([]string{"a", "b"}, 64) + if err != nil { + t.Fatal(err) + } + b, err := New([]string{"b", "a"}, 64) + if err != nil { + t.Fatal(err) + } + for index := range 1000 { + key := fmt.Sprint(index) + if a.Owner(key) != b.Owner(key) { + t.Fatal("node order changes ownership") + } + } +} diff --git a/internal/media/orphans.go b/internal/media/orphans.go new file mode 100644 index 0000000..e199786 --- /dev/null +++ b/internal/media/orphans.go @@ -0,0 +1,76 @@ +package media + +import ( + "context" + "fmt" + "time" +) + +// ObjectInfo describes an immutable object without its contents. +type ObjectInfo struct { + // Key is the server-assigned private coordinate. + Key string + // ModifiedAt determines eligibility for a grace-protected scan. + ModifiedAt time.Time +} + +// OrphanMetadata checks all reservations, not only published files. +type OrphanMetadata interface { + // HasObject reports whether any state references the key. + HasObject(context.Context, string) (bool, error) +} + +// OrphanObjects supports resumable listing and single-object cleanup. +type OrphanObjects interface { + // List returns a bounded lexicographic page after the given key. + List(context.Context, string) ([]ObjectInfo, error) + // Remove deletes an explicitly identified unreferenced key. + Remove(context.Context, string) error +} + +// OrphanResult reports one page of a safe S3-to-database reconciliation. +type OrphanResult struct { + // Scanned counts objects examined. + Scanned int `json:"scanned"` + // Orphans counts old objects with no metadata reservation. + Orphans int `json:"orphans"` + // Removed counts applied removals. + Removed int `json:"removed"` + // Next resumes the lexicographic scan. + Next string `json:"next"` +} + +// ReconcileOrphans protects fresh and referenced objects, and defaults to dry-run at the CLI. +func ReconcileOrphans(ctx context.Context, metadata OrphanMetadata, objects OrphanObjects, after string, grace time.Duration, apply bool) (OrphanResult, error) { + var result OrphanResult + if grace < time.Hour { + return result, fmt.Errorf("reconciliation grace must be at least one hour") + } + entries, err := objects.List(ctx, after) + if err != nil { + return result, err + } + cutoff := time.Now().UTC().Add(-grace) + for _, entry := range entries { + result.Scanned++ + result.Next = entry.Key + if entry.ModifiedAt.IsZero() || entry.ModifiedAt.After(cutoff) { + continue + } + referenced, err := metadata.HasObject(ctx, entry.Key) + if err != nil { + return result, err + } + if referenced { + continue + } + result.Orphans++ + if apply { + if err := objects.Remove(ctx, entry.Key); err != nil { + return result, err + } + result.Removed++ + } + } + return result, nil +} diff --git a/internal/media/reconcile.go b/internal/media/reconcile.go new file mode 100644 index 0000000..1d8882b --- /dev/null +++ b/internal/media/reconcile.go @@ -0,0 +1,102 @@ +package media + +import ( + "context" + "fmt" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// Inventory supports bounded, resumable scans and guarded state transitions. +type Inventory interface { + // Scan returns the next at most limit metadata records by ID. + Scan(context.Context, uuid.UUID, int) ([]dto.Attachment, error) + // Reclaim deletes only an old unfinished unreferenced reservation after claiming it. + Reclaim(context.Context, uuid.UUID, time.Time, bool) (bool, error) + // MarkMissing preserves metadata and references when an original disappears. + MarkMissing(context.Context, uuid.UUID, bool) error +} + +// ObjectInventory exposes bounded inspection and explicit removal. +type ObjectInventory interface { + // Exists distinguishes absence from infrastructure failure. + Exists(context.Context, string) (bool, error) + // Remove deletes a single immutable object key. + Remove(context.Context, string) error +} + +// ReconcileResult records observations and the continuation cursor without file contents. +type ReconcileResult struct { + // Scanned counts inspected metadata rows. + Scanned int `json:"scanned"` + // Missing counts published originals absent from S3. + Missing int `json:"missing"` + // Abandoned counts old incomplete reservations eligible for cleanup. + Abandoned int `json:"abandoned"` + // Changed counts successful applied repairs, zero for dry run. + Changed int `json:"changed"` + // Next continues scanning without retaining the full database in RAM. + Next uuid.UUID `json:"next"` +} + +// Reconcile inspects at most 1000 records and protects active uploads with a one-hour minimum grace. +func Reconcile(ctx context.Context, metadata Inventory, objects ObjectInventory, after uuid.UUID, grace time.Duration, apply bool) (ReconcileResult, error) { + var result ReconcileResult + if grace < time.Hour { + return result, fmt.Errorf("reconciliation grace must be at least one hour") + } + records, err := metadata.Scan(ctx, after, 1000) + if err != nil { + return result, err + } + cutoff := time.Now().UTC().Add(-grace) + for _, a := range records { + result.Scanned++ + result.Next = a.ID + if a.CreatedAt.After(cutoff) { + continue + } + exists, err := objects.Exists(ctx, a.ObjectKey) + if err != nil { + return result, err + } + switch { + case a.Status == "uploading" || a.Status == "deleting": + result.Abandoned++ + if !apply { + continue + } + claimed, err := metadata.Reclaim(ctx, a.ID, cutoff, false) + if err != nil { + return result, err + } + if !claimed { + continue + } + if err := objects.Remove(ctx, a.ObjectKey); err != nil { + return result, err + } + if _, err := metadata.Reclaim(ctx, a.ID, cutoff, true); err != nil { + return result, err + } + result.Changed++ + case !exists: + result.Missing++ + if apply && a.Status != "missing" { + if err := metadata.MarkMissing(ctx, a.ID, true); err != nil { + return result, err + } + result.Changed++ + } + case a.Status == "missing" && apply: + if err := metadata.MarkMissing(ctx, a.ID, false); err != nil { + return result, err + } + result.Changed++ + } + } + return result, nil +} diff --git a/internal/media/reconcile_test.go b/internal/media/reconcile_test.go new file mode 100644 index 0000000..078e994 --- /dev/null +++ b/internal/media/reconcile_test.go @@ -0,0 +1,159 @@ +package media + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +type reconciliationFixture struct { + records []dto.Attachment + entries []ObjectInfo + exists bool + referenced bool + claim bool + scanErr error + existsErr error + claimErr error + finishErr error + removeErr error + markErr error + listErr error + referenceErr error + removed int +} + +// Scan returns an independent bounded fixture page. +func (f *reconciliationFixture) Scan(context.Context, uuid.UUID, int) ([]dto.Attachment, error) { + return f.records, f.scanErr +} + +// Reclaim injects failures at the claim or finish boundary independently. +func (f *reconciliationFixture) Reclaim(_ context.Context, _ uuid.UUID, _ time.Time, finish bool) (bool, error) { + if finish { + return f.claim, f.finishErr + } + return f.claim, f.claimErr +} + +// MarkMissing models availability-state persistence without deleting metadata. +func (f *reconciliationFixture) MarkMissing(context.Context, uuid.UUID, bool) error { + return f.markErr +} + +// Exists returns an explicit existence or infrastructure-failure outcome. +func (f *reconciliationFixture) Exists(context.Context, string) (bool, error) { + return f.exists, f.existsErr +} + +// Remove counts applied side effects for dry-run and referenced-object assertions. +func (f *reconciliationFixture) Remove(context.Context, string) error { + f.removed++ + return f.removeErr +} + +// List supplies object timestamps independently of database state. +func (f *reconciliationFixture) List(context.Context, string) ([]ObjectInfo, error) { + return f.entries, f.listErr +} + +// HasObject includes reservations so active uploads remain protected. +func (f *reconciliationFixture) HasObject(context.Context, string) (bool, error) { + return f.referenced, f.referenceErr +} + +// TestReconciliationSafety exercises interrupted cleanup, age protection and missing-object repair. +func TestReconciliationSafety(t *testing.T) { + fault := errors.New("dependency failure") + old := time.Now().Add(-2 * time.Hour) + cases := []struct { + name string + fixture reconciliationFixture + status string + created time.Time + apply bool + wantErr bool + changed int + }{ + {name: "fresh", created: time.Now(), apply: true}, + {name: "scan failure", fixture: reconciliationFixture{scanErr: fault}, wantErr: true}, + {name: "head failure", fixture: reconciliationFixture{existsErr: fault}, wantErr: true}, + {name: "dry abandoned", status: "uploading"}, + {name: "abandoned applied", status: "uploading", apply: true, fixture: reconciliationFixture{claim: true}, changed: 1}, + {name: "claimed by another", status: "uploading", apply: true}, + {name: "claim failure", status: "uploading", apply: true, fixture: reconciliationFixture{claimErr: fault}, wantErr: true}, + {name: "delete failure", status: "deleting", apply: true, fixture: reconciliationFixture{claim: true, removeErr: fault}, wantErr: true}, + {name: "finish failure", status: "deleting", apply: true, fixture: reconciliationFixture{claim: true, finishErr: fault}, wantErr: true}, + {name: "missing dry", status: "ready"}, + {name: "missing applied", status: "ready", apply: true, changed: 1}, + {name: "mark failure", status: "ready", apply: true, fixture: reconciliationFixture{markErr: fault}, wantErr: true}, + {name: "missing repeat", status: "missing", apply: true}, + {name: "restored", status: "missing", apply: true, fixture: reconciliationFixture{exists: true}, changed: 1}, + {name: "restore failure", status: "missing", apply: true, fixture: reconciliationFixture{exists: true, markErr: fault}, wantErr: true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + f := tc.fixture + created := tc.created + if created.IsZero() { + created = old + } + f.records = []dto.Attachment{{ID: uuid.New(), Status: tc.status, CreatedAt: created, ObjectKey: "original/test"}} + result, err := Reconcile(t.Context(), &f, &f, uuid.Nil, time.Hour, tc.apply) + if (err != nil) != tc.wantErr || result.Changed != tc.changed { + t.Fatalf("result=%+v error=%v", result, err) + } + }) + } +} + +// TestOrphanSafety rejects deleting fresh, reserved and unknown-age objects. +func TestOrphanSafety(t *testing.T) { + fault := errors.New("dependency failure") + cases := []struct { + name string + fixture reconciliationFixture + fresh bool + apply bool + wantErr bool + removed int + }{ + {name: "fresh", fresh: true, apply: true}, + {name: "referenced", fixture: reconciliationFixture{referenced: true}, apply: true}, + {name: "dry run"}, + {name: "remove", apply: true, removed: 1}, + {name: "list failure", fixture: reconciliationFixture{listErr: fault}, wantErr: true}, + {name: "metadata failure", fixture: reconciliationFixture{referenceErr: fault}, wantErr: true}, + {name: "remove failure", fixture: reconciliationFixture{removeErr: fault}, apply: true, wantErr: true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + f := tc.fixture + modified := time.Now().Add(-2 * time.Hour) + if tc.fresh { + modified = time.Now() + } + f.entries = []ObjectInfo{{Key: "original/test", ModifiedAt: modified}} + result, err := ReconcileOrphans(t.Context(), &f, &f, "", time.Hour, tc.apply) + if (err != nil) != tc.wantErr || result.Removed != tc.removed { + t.Fatalf("result=%+v error=%v", result, err) + } + }) + } +} + +// TestGraceCannotBeDisabled rejects unsafe cleanup windows at both scan entrypoints. +func TestGraceCannotBeDisabled(t *testing.T) { + f := &reconciliationFixture{} + if _, err := Reconcile(t.Context(), f, f, uuid.Nil, time.Second, true); err == nil { + t.Fatal("unsafe metadata grace accepted") + } + if _, err := ReconcileOrphans(t.Context(), f, f, "", time.Second, true); err == nil { + t.Fatal("unsafe object grace accepted") + } +} diff --git a/internal/media/run_reconcile.go b/internal/media/run_reconcile.go new file mode 100644 index 0000000..ae840b4 --- /dev/null +++ b/internal/media/run_reconcile.go @@ -0,0 +1,55 @@ +package media + +import ( + "context" + "encoding/json" + "io" + "time" + + "github.com/google/uuid" +) + +// ReconcileMetadata combines bounded database inventory operations for the operator workflow. +type ReconcileMetadata interface { + Inventory + OrphanMetadata +} + +// ReconcileObjects combines object inspection and listing for the operator workflow. +type ReconcileObjects interface { + ObjectInventory + OrphanObjects +} + +// RunReconciliation emits one bounded result per page and stops on any uncertain dependency outcome. +func RunReconciliation(ctx context.Context, metadata ReconcileMetadata, objects ReconcileObjects, output io.Writer, apply bool) error { + encoder := json.NewEncoder(output) + after := uuid.Nil + for { + result, err := Reconcile(ctx, metadata, objects, after, time.Hour, apply) + if err != nil { + return err + } + if err := encoder.Encode(result); err != nil { + return err + } + if result.Scanned < 1000 { + break + } + after = result.Next + } + key := "" + for { + result, err := ReconcileOrphans(ctx, metadata, objects, key, time.Hour, apply) + if err != nil { + return err + } + if err := encoder.Encode(result); err != nil { + return err + } + if result.Scanned < 1000 { + return nil + } + key = result.Next + } +} diff --git a/internal/media/run_reconcile_test.go b/internal/media/run_reconcile_test.go new file mode 100644 index 0000000..1864db7 --- /dev/null +++ b/internal/media/run_reconcile_test.go @@ -0,0 +1,92 @@ +package media + +import ( + "context" + "errors" + "io" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +type pagedFixture struct { + reconciliationFixture + metadataPage int + objectPage int +} + +// Scan returns one full fresh page followed by an empty continuation. +func (f *pagedFixture) Scan(context.Context, uuid.UUID, int) ([]dto.Attachment, error) { + f.metadataPage++ + if f.metadataPage > 1 { + return nil, nil + } + records := make([]dto.Attachment, 1000) + for i := range records { + records[i] = dto.Attachment{ID: uuid.New(), CreatedAt: time.Now()} + } + return records, nil +} + +// List returns one full fresh page followed by an empty continuation. +func (f *pagedFixture) List(context.Context, string) ([]ObjectInfo, error) { + f.objectPage++ + if f.objectPage > 1 { + return nil, nil + } + records := make([]ObjectInfo, 1000) + for i := range records { + records[i] = ObjectInfo{Key: uuid.NewString(), ModifiedAt: time.Now()} + } + return records, nil +} + +type failingWriter struct { + successful int +} + +// Write injects a broken report sink after a fixed number of complete pages. +func (f *failingWriter) Write(data []byte) (int, error) { + if f.successful == 0 { + return 0, io.ErrClosedPipe + } + f.successful-- + return len(data), nil +} + +// TestReconciliationPagination proves both inventories advance beyond a full page. +func TestReconciliationPagination(t *testing.T) { + f := &pagedFixture{} + if err := RunReconciliation(t.Context(), f, f, io.Discard, false); err != nil { + t.Fatal(err) + } + if f.metadataPage != 2 || f.objectPage != 2 { + t.Fatalf("pages: metadata=%d objects=%d", f.metadataPage, f.objectPage) + } +} + +// TestReconciliationStopsOnFailures prevents misleading successful reports after lost evidence or dependencies. +func TestReconciliationStopsOnFailures(t *testing.T) { + fault := errors.New("dependency failure") + cases := []struct { + name string + fixture reconciliationFixture + writer io.Writer + }{ + {"metadata", reconciliationFixture{scanErr: fault}, io.Discard}, + {"objects", reconciliationFixture{listErr: fault}, io.Discard}, + {"first report", reconciliationFixture{}, &failingWriter{}}, + {"second report", reconciliationFixture{}, &failingWriter{successful: 1}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + f := tc.fixture + if err := RunReconciliation(t.Context(), &f, &f, tc.writer, false); err == nil { + t.Fatal("failed run reported success") + } + }) + } +} diff --git a/internal/media/service.go b/internal/media/service.go new file mode 100644 index 0000000..b2bc9ff --- /dev/null +++ b/internal/media/service.go @@ -0,0 +1,145 @@ +// Package media implements attachment authorization and the S3 publication protocol. +package media + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "io" + "time" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// Metadata persists reservations before object creation. +type Metadata interface { + // Reserve inserts an uploading reservation. + Reserve(context.Context, dto.Attachment) error + // Publish atomically transitions the reservation to ready. + Publish(context.Context, uuid.UUID) (dto.Attachment, error) + // Attachment retrieves metadata by a validated ID. + Attachment(context.Context, uuid.UUID) (dto.Attachment, error) +} + +// Objects exposes private object operations without a concrete S3 client. +type Objects interface { + // Put uploads a known bounded length without a multipart buffer. + Put(context.Context, string, string, io.Reader, int64) error + // Get opens an original stream; the caller closes it. + Get(context.Context, string) (io.ReadCloser, error) +} + +// Chats authorizes access using the existing business service. +type Chats interface { + // ChatsGetByID checks the caller and membership. + ChatsGetByID(context.Context, string, string) (dto.ChatDTO, error) +} + +// Service coordinates bounded validation and durable publication. +type Service struct { + metadata Metadata + objects Objects + chats Chats + slots chan struct{} +} + +// New validates required dependencies and caps simultaneous image decoding. +func New(metadata Metadata, objects Objects, chats Chats) (*Service, error) { + if metadata == nil || objects == nil || chats == nil { + return nil, fmt.Errorf("attachment dependencies are required") + } + return &Service{metadata: metadata, objects: objects, chats: chats, slots: make(chan struct{}, 2)}, nil +} + +// Upload validates access and original bytes before reserving and publishing. +func (s *Service) Upload(ctx context.Context, user, chat, filename, contentType string, body []byte) (dto.Attachment, error) { + scope, err := s.chats.ChatsGetByID(ctx, user, chat) + if err != nil { + return dto.Attachment{}, err + } + select { + case s.slots <- struct{}{}: + defer func() { + <-s.slots + }() + default: + return dto.Attachment{}, service.ErrUnavailable + } + if err := validateImage(filename, contentType, body); err != nil { + return dto.Attachment{}, err + } + owner, err := uuid.Parse(user) + if err != nil { + return dto.Attachment{}, service.ErrUnauthorized + } + sum := sha256.Sum256(body) + id := uuid.New() + now := time.Now().UTC() + attachment := dto.Attachment{ + ID: id, + ChatID: scope.ID, + OwnerID: owner, + Filename: filename, + ContentType: contentType, + SizeBytes: int64(len(body)), + SHA256: hex.EncodeToString(sum[:]), + Status: "uploading", + ObjectKey: "original/" + id.String(), + CreatedAt: now, + UpdatedAt: now, + } + if err := s.metadata.Reserve(ctx, attachment); err != nil { + return dto.Attachment{}, err + } + if err := s.objects.Put(ctx, attachment.ObjectKey, contentType, bytes.NewReader(body), attachment.SizeBytes); err != nil { + return dto.Attachment{}, service.ErrUnavailable + } + // A lost COMMIT acknowledgement must never trigger deletion of a possibly published object. + return s.metadata.Publish(ctx, id) +} + +// Get authorizes metadata access without exposing private object coordinates. +func (s *Service) Get(ctx context.Context, user, rawID string) (dto.Attachment, error) { + id, err := uuid.Parse(rawID) + if err != nil { + return dto.Attachment{}, service.ErrInvalid + } + attachment, err := s.metadata.Attachment(ctx, id) + if errors.Is(err, store.ErrNotFound) { + return dto.Attachment{}, service.ErrNotFound + } + if err != nil { + return dto.Attachment{}, err + } + if _, err := s.chats.ChatsGetByID(ctx, user, attachment.ChatID.String()); err != nil { + return dto.Attachment{}, err + } + if attachment.Status != "ready" { + return dto.Attachment{}, service.ErrConflict + } + return attachment, nil +} + +// Download validates access and variant before opening the original stream. +func (s *Service) Download(ctx context.Context, user, id, variant string) (dto.Attachment, io.ReadCloser, error) { + attachment, err := s.Get(ctx, user, id) + if err != nil { + return dto.Attachment{}, nil, err + } + if variant != "" && variant != "original" { + return dto.Attachment{}, nil, service.ErrNotFound + } + reader, err := s.objects.Get(ctx, attachment.ObjectKey) + if err != nil { + return dto.Attachment{}, nil, service.ErrUnavailable + } + return attachment, reader, nil +} diff --git a/internal/media/service_test.go b/internal/media/service_test.go new file mode 100644 index 0000000..d2fdcb2 --- /dev/null +++ b/internal/media/service_test.go @@ -0,0 +1,225 @@ +package media + +import ( + "bytes" + "context" + "errors" + "io" + "testing" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +type mediaFixture struct { + attachment dto.Attachment + body []byte + authorizeErr error + reserveErr error + publishErr error + getErr error + objectErr error +} + +// ChatsGetByID models a single authorization outcome for one scenario. +func (f *mediaFixture) ChatsGetByID(_ context.Context, _, chat string) (dto.ChatDTO, error) { + id, _ := uuid.Parse(chat) + return dto.ChatDTO{ID: id}, f.authorizeErr +} + +// Reserve records durable intent before a test object can be written. +func (f *mediaFixture) Reserve(_ context.Context, a dto.Attachment) error { + f.attachment = a + return f.reserveErr +} + +// Publish models an acknowledged or uncertain database commit. +func (f *mediaFixture) Publish(_ context.Context, _ uuid.UUID) (dto.Attachment, error) { + f.attachment.Status = "ready" + return f.attachment, f.publishErr +} + +// Attachment returns fixture metadata without bypassing service authorization. +func (f *mediaFixture) Attachment(_ context.Context, _ uuid.UUID) (dto.Attachment, error) { + return f.attachment, f.getErr +} + +// Put captures exact original bytes or injects an object failure. +func (f *mediaFixture) Put(_ context.Context, _, _ string, r io.Reader, _ int64) error { + if f.objectErr != nil { + return f.objectErr + } + var err error + f.body, err = io.ReadAll(r) + return err +} + +// Get returns a separate read cursor over the captured original. +func (f *mediaFixture) Get(_ context.Context, _ string) (io.ReadCloser, error) { + return io.NopCloser(bytes.NewReader(f.body)), f.objectErr +} + +// mediaService constructs an independent service with explicit fixture dependencies. +func mediaService(t *testing.T, f *mediaFixture) *Service { + t.Helper() + s, err := New(f, f, f) + if err != nil { + t.Fatal(err) + } + return s +} + +// TestUploadPublication verifies exact bytes and durable publication ordering. +func TestUploadPublication(t *testing.T) { + f := &mediaFixture{} + s := mediaService(t, f) + original := imageBytes(t) + a, err := s.Upload(t.Context(), uuid.NewString(), uuid.NewString(), "a.png", "image/png", original) + if err != nil || a.Status != "ready" || a.SizeBytes != int64(len(original)) || len(a.SHA256) != 64 || !bytes.Equal(f.body, original) { + t.Fatalf("upload result: %+v %v", a, err) + } +} + +// TestUploadRejectsUnauthorized verifies authorization precedes object creation. +func TestUploadRejectsUnauthorized(t *testing.T) { + f := &mediaFixture{authorizeErr: service.ErrNotFound} + s := mediaService(t, f) + _, err := s.Upload(t.Context(), uuid.NewString(), uuid.NewString(), "a.png", "image/png", imageBytes(t)) + if !errors.Is(err, service.ErrNotFound) || f.body != nil { + t.Fatalf("unauthorized upload: %v", err) + } +} + +// TestUploadDependencyFailures verifies failed operations are not acknowledged as ready. +func TestUploadDependencyFailures(t *testing.T) { + cases := []struct { + name string + fixture mediaFixture + }{ + {name: "reserve", fixture: mediaFixture{reserveErr: service.ErrUnavailable}}, + {name: "put", fixture: mediaFixture{objectErr: service.ErrUnavailable}}, + {name: "commit acknowledgement", fixture: mediaFixture{publishErr: service.ErrUnavailable}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + f := tc.fixture + s := mediaService(t, &f) + _, err := s.Upload(t.Context(), uuid.NewString(), uuid.NewString(), "a.png", "image/png", imageBytes(t)) + if !errors.Is(err, service.ErrUnavailable) { + t.Fatalf("error=%v", err) + } + }) + } +} + +// TestUploadConcurrencyBound verifies decoding cannot create an unbounded queue. +func TestUploadConcurrencyBound(t *testing.T) { + s := mediaService(t, &mediaFixture{}) + s.slots <- struct{}{} + s.slots <- struct{}{} + _, err := s.Upload(t.Context(), uuid.NewString(), uuid.NewString(), "a.png", "image/png", imageBytes(t)) + if !errors.Is(err, service.ErrUnavailable) { + t.Fatal(err) + } +} + +// TestGetAttachment checks metadata authorization and intermediate state handling. +func TestGetAttachment(t *testing.T) { + cases := []struct { + name string + fixture mediaFixture + id string + want error + }{ + {name: "invalid id", id: "bad", want: service.ErrInvalid}, + {name: "missing", id: uuid.NewString(), fixture: mediaFixture{getErr: store.ErrNotFound}, want: service.ErrNotFound}, + {name: "database unavailable", id: uuid.NewString(), fixture: mediaFixture{getErr: service.ErrUnavailable}, want: service.ErrUnavailable}, + {name: "foreign", id: uuid.NewString(), fixture: mediaFixture{authorizeErr: service.ErrNotFound}, want: service.ErrNotFound}, + {name: "unfinished", id: uuid.NewString(), fixture: mediaFixture{attachment: dto.Attachment{Status: "uploading"}}, want: service.ErrConflict}, + {name: "ready", id: uuid.NewString(), fixture: mediaFixture{attachment: dto.Attachment{Status: "ready"}}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + f := tc.fixture + s := mediaService(t, &f) + _, err := s.Get(t.Context(), uuid.NewString(), tc.id) + if !errors.Is(err, tc.want) { + t.Fatalf("got %v want %v", err, tc.want) + } + }) + } +} + +// TestDownloadOriginal checks byte identity through an independently authorized read. +func TestDownloadOriginal(t *testing.T) { + f := &mediaFixture{attachment: dto.Attachment{Status: "ready"}, body: imageBytes(t)} + s := mediaService(t, f) + _, reader, err := s.Download(t.Context(), uuid.NewString(), uuid.NewString(), "original") + if err != nil { + t.Fatal(err) + } + got, err := io.ReadAll(reader) + if closeErr := reader.Close(); closeErr != nil { + t.Fatal(closeErr) + } + if err != nil || !bytes.Equal(got, f.body) { + t.Fatalf("download: %v", err) + } +} + +// TestMediaRequiresDependencies rejects incomplete service composition. +func TestMediaRequiresDependencies(t *testing.T) { + if _, err := New(nil, nil, nil); err == nil { + t.Fatal("missing dependencies accepted") + } +} + +// TestUploadRejectsInvalidOriginal never reserves invalid file metadata. +func TestUploadRejectsInvalidOriginal(t *testing.T) { + f := &mediaFixture{} + s := mediaService(t, f) + _, err := s.Upload(t.Context(), uuid.NewString(), uuid.NewString(), "a.png", "image/png", []byte("invalid")) + if !errors.Is(err, service.ErrMedia) || f.attachment.ID != uuid.Nil { + t.Fatalf("invalid bytes persisted: %v", err) + } +} + +// TestUploadRejectsMalformedOwner does not persist an invalid authenticated identity. +func TestUploadRejectsMalformedOwner(t *testing.T) { + f := &mediaFixture{} + s := mediaService(t, f) + _, err := s.Upload(t.Context(), "bad", uuid.NewString(), "a.png", "image/png", imageBytes(t)) + if !errors.Is(err, service.ErrUnauthorized) { + t.Fatal(err) + } +} + +// TestDownloadFailures keeps authorization, variant and dependency failures distinct. +func TestDownloadFailures(t *testing.T) { + cases := []struct { + name string + fixture mediaFixture + variant string + want error + }{ + {name: "foreign", fixture: mediaFixture{authorizeErr: service.ErrNotFound}, want: service.ErrNotFound}, + {name: "variant missing", fixture: mediaFixture{attachment: dto.Attachment{Status: "ready"}}, variant: "thumbnail", want: service.ErrNotFound}, + {name: "S3 unavailable", fixture: mediaFixture{attachment: dto.Attachment{Status: "ready"}, objectErr: service.ErrUnavailable}, want: service.ErrUnavailable}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + f := tc.fixture + s := mediaService(t, &f) + _, reader, err := s.Download(t.Context(), uuid.NewString(), uuid.NewString(), tc.variant) + if reader != nil { + _ = reader.Close() + } + if !errors.Is(err, tc.want) { + t.Fatalf("got %v want %v", err, tc.want) + } + }) + } +} diff --git a/internal/media/validation.go b/internal/media/validation.go new file mode 100644 index 0000000..2ea551d --- /dev/null +++ b/internal/media/validation.go @@ -0,0 +1,48 @@ +package media + +import ( + "bytes" + "image" + + // Register the only accepted original image decoders. + _ "image/jpeg" + // Register PNG for content validation before storage. + _ "image/png" + "strings" + "unicode" + "unicode/utf8" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +const maxImageBytes = 10 << 20 +const maxImagePixels = 40_000_000 + +// validateImage checks encoded size, display name, actual format and complete decoding. +func validateImage(filename, mime string, body []byte) error { + if len(body) > maxImageBytes { + return service.ErrTooLarge + } + if !utf8.ValidString(filename) || strings.TrimSpace(filename) == "" || utf8.RuneCountInString(filename) > 255 { + return service.ErrInvalid + } + for _, r := range filename { + if r == '/' || r == '\\' || unicode.IsControl(r) { + return service.ErrInvalid + } + } + if mime != "image/png" && mime != "image/jpeg" { + return service.ErrMedia + } + dimensions, format, err := image.DecodeConfig(bytes.NewReader(body)) + if err != nil || "image/"+format != mime { + return service.ErrMedia + } + if dimensions.Width < 1 || dimensions.Height < 1 || int64(dimensions.Width)*int64(dimensions.Height) > maxImagePixels { + return service.ErrTooLarge + } + if _, _, err := image.Decode(bytes.NewReader(body)); err != nil { + return service.ErrMedia + } + return nil +} diff --git a/internal/media/validation_test.go b/internal/media/validation_test.go new file mode 100644 index 0000000..ac9b859 --- /dev/null +++ b/internal/media/validation_test.go @@ -0,0 +1,65 @@ +package media + +import ( + "bytes" + "encoding/binary" + "errors" + "hash/crc32" + "image" + "image/png" + "testing" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// imageBytes creates a valid independent synthetic PNG fixture. +func imageBytes(t *testing.T) []byte { + t.Helper() + var output bytes.Buffer + if err := png.Encode(&output, image.NewRGBA(image.Rect(0, 0, 3, 2))); err != nil { + t.Fatal(err) + } + return output.Bytes() +} + +// TestValidateImage checks file-level rejection independently of persistence. +func TestValidateImage(t *testing.T) { + valid := imageBytes(t) + cases := []struct { + name string + filename string + mime string + body []byte + want error + }{ + {"valid", "test.png", "image/png", valid, nil}, + {"traversal", "../test.png", "image/png", valid, service.ErrInvalid}, + {"backslash", "a\\b", "image/png", valid, service.ErrInvalid}, + {"control", "a\n", "image/png", valid, service.ErrInvalid}, + {"empty name", "", "image/png", valid, service.ErrInvalid}, + {"mime mismatch", "a.jpg", "image/jpeg", valid, service.ErrMedia}, + {"unsupported", "a.png", "text/plain", valid, service.ErrMedia}, + {"corrupt header", "a.png", "image/png", []byte("bad"), service.ErrMedia}, + {"truncated payload", "a.png", "image/png", valid[:len(valid)-12], service.ErrMedia}, + {"encoded limit", "a.png", "image/png", make([]byte, maxImageBytes+1), service.ErrTooLarge}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + err := validateImage(tc.filename, tc.mime, tc.body) + if !errors.Is(err, tc.want) { + t.Fatalf("got %v want %v", err, tc.want) + } + }) + } +} + +// TestPixelLimitPrecedesDecode rejects a valid PNG header before allocating a huge pixel buffer. +func TestPixelLimitPrecedesDecode(t *testing.T) { + body := append([]byte{}, imageBytes(t)...) + binary.BigEndian.PutUint32(body[16:20], 10000) + binary.BigEndian.PutUint32(body[20:24], 10000) + binary.BigEndian.PutUint32(body[29:33], crc32.ChecksumIEEE(body[12:29])) + if err := validateImage("huge.png", "image/png", body); !errors.Is(err, service.ErrTooLarge) { + t.Fatalf("pixel limit: %v", err) + } +} diff --git a/internal/model/dto/attachment.go b/internal/model/dto/attachment.go new file mode 100644 index 0000000..f9e400f --- /dev/null +++ b/internal/model/dto/attachment.go @@ -0,0 +1,33 @@ +package dto + +import ( + "time" + + "github.com/google/uuid" +) + +// Attachment contains private persistence metadata and the public file description. +type Attachment struct { + // ID identifies the immutable upload. + ID uuid.UUID + // ChatID defines the authorization scope. + ChatID uuid.UUID + // OwnerID identifies the uploader. + OwnerID uuid.UUID + // Filename is display-only and never an object path. + Filename string + // ContentType is validated against decoded bytes. + ContentType string + // SizeBytes is the original encoded length. + SizeBytes int64 + // SHA256 is the lowercase original checksum. + SHA256 string + // Status is the durable upload state. + Status string + // ObjectKey is private and must not reach HTTP responses. + ObjectKey string + // CreatedAt is the reservation timestamp. + CreatedAt time.Time + // UpdatedAt is the latest durable transition. + UpdatedAt time.Time +} diff --git a/internal/model/dto/models.go b/internal/model/dto/models.go new file mode 100644 index 0000000..61227e1 --- /dev/null +++ b/internal/model/dto/models.go @@ -0,0 +1,118 @@ +// Package dto defines the transport-independent data shared by service and store. +package dto + +import ( + "time" + + "github.com/google/uuid" +) + +// UserDTO is the public account profile without credentials. +type UserDTO struct { + // ID is the server-generated user identifier. + ID uuid.UUID + // Username is the unique lowercase ASCII account name. + Username string + // DisplayName is the display label, limited to 100 Unicode code points. + DisplayName string + // CreatedAt is the server-assigned UTC creation time. + CreatedAt time.Time +} + +// ChatDTO is a chat with immutable membership. +type ChatDTO struct { + // ID is the server-generated chat identifier. + ID uuid.UUID + // Title is the nonblank title, limited to 100 Unicode code points. + Title string + // MemberIDs contains the fixed set of users allowed to read and write the chat. + MemberIDs []uuid.UUID + // CreatedAt is the server-assigned UTC creation time. + CreatedAt time.Time +} + +// MessageDTO is an immutable message stored in a chat. +type MessageDTO struct { + // AttachmentIDs references ordered immutable files in the same chat. + AttachmentIDs []string + // IdempotencyKey identifies a validated retry within the sender and chat scope. + IdempotencyKey string + // ID is the server-generated message identifier. + ID uuid.UUID + // ChatID identifies the owning chat. + ChatID uuid.UUID + // SenderID identifies the user resolved from the request identity. + SenderID uuid.UUID + // Text contains the original text, limited to 4000 Unicode code points. + Text string + // CreatedAt is the server-assigned UTC ordering timestamp. + CreatedAt time.Time +} + +// UsersCreateRequestDTO carries untrusted registration values into the service. +type UsersCreateRequestDTO struct { + // Username is the raw requested account name. + Username string + // DisplayName is the raw requested display label. + DisplayName string +} + +// ChatsCreateRequestDTO carries untrusted chat creation input. +type ChatsCreateRequestDTO struct { + // UserID is the caller identifier resolved from a validated session. + UserID string + // Title is the raw requested chat title. + Title string + // MemberIDs contains raw user UUIDs; the service checks uniqueness and membership. + MemberIDs []string +} + +// MessagesCreateRequestDTO carries untrusted message creation input. +type MessagesCreateRequestDTO struct { + // AttachmentIDs references ordered immutable files in the same chat. + AttachmentIDs []string + // IdempotencyKey identifies a validated retry within the sender and chat scope. + IdempotencyKey string + // UserID is the authenticated caller identifier, never a supplied sender field. + UserID string + // ChatID is the raw chat UUID from the route. + ChatID string + // Text is the unmodified message text to validate. + Text string +} + +// PageRequestDTO carries raw pagination values for service-level validation. +type PageRequestDTO struct { + // UserID is the caller identifier resolved from a validated session. + UserID string + // ChatID is the requested chat UUID; it is unused when listing chats. + ChatID string + // Limit is the raw page size; empty selects the contract default. + Limit string + // Cursor is the opaque continuation token; empty starts at the beginning. + Cursor string +} + +// PositionDTO is a validated keyset boundary; its zero value starts a history. +type PositionDTO struct { + // CreatedAt is the timestamp of the last record already returned. + CreatedAt time.Time + // ID is the tie-breaking identifier of the last record already returned. + ID uuid.UUID +} + +// ChatPageDTO contains an ordered page of accessible chats. +type ChatPageDTO struct { + // Items contains at most the requested number of chats. + Items []ChatDTO + // NextCursor continues the same user-specific list; nil marks its current end. + NextCursor *string +} + +// MessagePageDTO contains an ordered page of chat history. +type MessagePageDTO struct { + // Items contains at most the requested number of messages. + Items []MessageDTO + // NextCursor continues the same chat history; nil marks its current end. + NextCursor *string +} diff --git a/internal/model/dto/session.go b/internal/model/dto/session.go new file mode 100644 index 0000000..9d1193c --- /dev/null +++ b/internal/model/dto/session.go @@ -0,0 +1,13 @@ +package dto + +import "time" + +// SessionDTO is an issued session, exposing the raw token only at creation. +type SessionDTO struct { + // Token contains 256 random bits encoded for a Bearer header. + Token string + // ExpiresAt is an absolute UTC deadline which is not extended by reads. + ExpiresAt time.Time + // User is the authenticated account without credentials. + User UserDTO +} diff --git a/internal/model/http/models.go b/internal/model/http/models.go new file mode 100644 index 0000000..e9db290 --- /dev/null +++ b/internal/model/http/models.go @@ -0,0 +1,102 @@ +// Package http defines the public JSON boundary, separate from DTOs. +package http + +import "time" + +// UsersCreateRequest is the registration payload. +type UsersCreateRequest struct { + // Password is the unmodified Unicode registration secret. + Password string `json:"password"` + // Username is the unique lowercase ASCII username. + Username string `json:"username"` + // DisplayName is the user-visible name, limited to 100 Unicode code points. + DisplayName string `json:"display_name"` +} + +// ChatsCreateRequest is the chat payload. +type ChatsCreateRequest struct { + // Title is the nonblank chat title, limited to 100 Unicode code points. + Title string `json:"title"` + // MemberIDs contains unique user UUIDs, including the caller when creating a chat. + MemberIDs []string `json:"member_ids"` +} + +// MessagesCreateRequest is the message payload. +type MessagesCreateRequest struct { + // AttachmentIDs references up to ten files of this chat. + AttachmentIDs []string `json:"attachment_ids"` + // Text contains nonblank text, limited to 4000 Unicode code points. + Text string `json:"text"` +} + +// UserResponse is the public user model. +type UserResponse struct { + // ID is the canonical server-generated UUID. + ID string `json:"id"` + // Username is the unique lowercase ASCII username. + Username string `json:"username"` + // DisplayName is the user-visible name, limited to 100 Unicode code points. + DisplayName string `json:"display_name"` + // CreatedAt is the UTC creation time serialized as RFC 3339. + CreatedAt time.Time `json:"created_at"` +} + +// ChatResponse is the public chat model. +type ChatResponse struct { + // ID is the canonical server-generated UUID. + ID string `json:"id"` + // Title is the nonblank chat title, limited to 100 Unicode code points. + Title string `json:"title"` + // MemberIDs contains unique user UUIDs, including the caller when creating a chat. + MemberIDs []string `json:"member_ids"` + // CreatedAt is the UTC creation time serialized as RFC 3339. + CreatedAt time.Time `json:"created_at"` +} + +// MessageResponse is the public message model. +type MessageResponse struct { + // AttachmentIDs references up to ten files of this chat. + AttachmentIDs []string `json:"attachment_ids"` + // ID is the canonical server-generated UUID. + ID string `json:"id"` + // ChatID is the canonical UUID of the chat containing the message. + ChatID string `json:"chat_id"` + // SenderID is derived from the request identity, never from a client-supplied sender. + SenderID string `json:"sender_id"` + // Text contains nonblank text, limited to 4000 Unicode code points. + Text string `json:"text"` + // CreatedAt is the UTC creation time serialized as RFC 3339. + CreatedAt time.Time `json:"created_at"` +} + +// ChatPageResponse contains visible chats. +type ChatPageResponse struct { + // Items contains the ordered page records; an empty page is serialized as an empty array. + Items []ChatResponse `json:"items"` + // NextCursor is the opaque continuation token or null when the current page is last. + NextCursor *string `json:"next_cursor"` +} + +// MessagePageResponse contains the requested history page. +type MessagePageResponse struct { + // Items contains the ordered page records; an empty page is serialized as an empty array. + Items []MessageResponse `json:"items"` + // NextCursor is the opaque continuation token or null when the current page is last. + NextCursor *string `json:"next_cursor"` +} + +// ErrorResponse is the mandatory error envelope. +type ErrorResponse struct { + // Error contains the safe public error details. + Error ErrorDetail `json:"error"` +} + +// ErrorDetail never exposes internal errors or input contents. +type ErrorDetail struct { + // Code is one of the machine-readable error codes declared by the API contract. + Code string `json:"code"` + // Message is a safe public explanation without internal errors or input contents. + Message string `json:"message"` + // RequestID matches the X-Request-Id response header for log correlation. + RequestID string `json:"request_id"` +} diff --git a/internal/objectstore/client.go b/internal/objectstore/client.go new file mode 100644 index 0000000..1d5d302 --- /dev/null +++ b/internal/objectstore/client.go @@ -0,0 +1,110 @@ +// Package objectstore adapts the S3 protocol to bounded application operations. +package objectstore + +import ( + "context" + "fmt" + "io" + "time" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" +) + +const operationTimeout = 15 * time.Second + +// Client owns a reusable S3 client and its private bucket scope. +type Client struct { + client *minio.Client + bucket string +} + +// New configures a lazy client with explicit service credentials. +func New(cfg *config.ObjectStore) (*Client, error) { + client, err := minio.New(cfg.Endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""), + Secure: cfg.Secure, + }) + if err != nil { + return nil, err + } + return &Client{client: client, bucket: cfg.Bucket}, nil +} + +// Put uses one bounded PUT for an already validated original. +func (c *Client) Put(ctx context.Context, key, mime string, reader io.Reader, size int64) error { + ctx, cancel := context.WithTimeout(ctx, operationTimeout) + defer cancel() + _, err := c.client.PutObject(ctx, c.bucket, key, reader, size, minio.PutObjectOptions{ + ContentType: mime, + DisableMultipart: true, + }) + return err +} + +// Get verifies object availability before handing its bounded stream to the caller. +func (c *Client) Get(ctx context.Context, key string) (io.ReadCloser, error) { + ctx, cancel := context.WithTimeout(ctx, operationTimeout) + object, err := c.client.GetObject(ctx, c.bucket, key, minio.GetObjectOptions{}) + if err != nil { + cancel() + return nil, err + } + if _, err := object.Stat(); err != nil { + _ = object.Close() + cancel() + return nil, err + } + return &stream{ReadCloser: object, cancel: cancel}, nil +} + +type stream struct { + io.ReadCloser + cancel context.CancelFunc +} + +// Close releases the connection and its operation deadline. +func (s *stream) Close() error { + defer s.cancel() + return s.ReadCloser.Close() +} + +// Ping verifies the bucket is accessible with the configured scoped identity. +func (c *Client) Ping(ctx context.Context) error { + ctx, cancel := context.WithTimeout(ctx, operationTimeout) + defer cancel() + exists, err := c.client.BucketExists(ctx, c.bucket) + if err == nil && !exists { + return fmt.Errorf("private bucket missing") + } + return err +} + +// Exists distinguishes a missing object from a failed dependency. +func (c *Client) Exists(ctx context.Context, key string) (bool, error) { + ctx, cancel := context.WithTimeout(ctx, operationTimeout) + defer cancel() + _, err := c.client.StatObject(ctx, c.bucket, key, minio.StatObjectOptions{}) + if minio.ToErrorResponse(err).Code == "NoSuchKey" { + return false, nil + } + return err == nil, err +} + +// Remove deletes one explicitly claimed immutable object. +func (c *Client) Remove(ctx context.Context, key string) error { + ctx, cancel := context.WithTimeout(ctx, operationTimeout) + defer cancel() + return c.client.RemoveObject(ctx, c.bucket, key, minio.RemoveObjectOptions{}) +} + +// Bootstrap creates a bucket idempotently; no anonymous policy is installed. +func (c *Client) Bootstrap(ctx context.Context) error { + exists, err := c.client.BucketExists(ctx, c.bucket) + if err != nil || exists { + return err + } + return c.client.MakeBucket(ctx, c.bucket, minio.MakeBucketOptions{}) +} diff --git a/internal/objectstore/client_test.go b/internal/objectstore/client_test.go new file mode 100644 index 0000000..715b82a --- /dev/null +++ b/internal/objectstore/client_test.go @@ -0,0 +1,193 @@ +package objectstore + +import ( + "bytes" + "context" + "io" + "net/http" + "os" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" +) + +// testObjects creates a scoped client against an explicitly isolated S3 server. +func testObjects(t *testing.T) *Client { + t.Helper() + if os.Getenv("S3_ENDPOINT") == "" { + t.Skip("isolated S3_ENDPOINT required") + } + cfg, err := config.NewObjectStore() + if err != nil { + t.Fatal(err) + } + client, err := New(cfg) + if err != nil { + t.Fatal(err) + } + return client +} + +// TestOriginalRoundTrip checks real S3 publication, streaming, listing and explicit deletion. +func TestOriginalRoundTrip(t *testing.T) { + client := testObjects(t) + key := "original/" + uuid.NewString() + original := []byte("synthetic original bytes") + if err := client.Put(t.Context(), key, "application/octet-stream", bytes.NewReader(original), int64(len(original))); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := client.Remove(context.Background(), key); err != nil { + t.Error(err) + } + }) + reader, err := client.Get(t.Context(), key) + if err != nil { + t.Fatal(err) + } + data, err := io.ReadAll(reader) + if closeErr := reader.Close(); closeErr != nil { + t.Fatal(closeErr) + } + if err != nil || !bytes.Equal(data, original) { + t.Fatalf("original changed: %v", err) + } + found, err := client.Exists(t.Context(), key) + if err != nil || !found { + t.Fatalf("existence: %v", err) + } + entries, err := client.List(t.Context(), "") + if err != nil { + t.Fatal(err) + } + found = false + for _, entry := range entries { + found = found || entry.Key == key + } + if !found { + t.Fatal("original absent from inventory") + } +} + +// TestMissingOriginal checks absence is distinct from unavailable S3. +func TestMissingOriginal(t *testing.T) { + client := testObjects(t) + key := "original/" + uuid.NewString() + exists, err := client.Exists(t.Context(), key) + if err != nil || exists { + t.Fatalf("missing: %v", err) + } + if reader, err := client.Get(t.Context(), key); err == nil { + _ = reader.Close() + t.Fatal("missing stream accepted") + } +} + +// TestUnavailableS3 returns bounded failures instead of converting them into missing objects. +func TestUnavailableS3(t *testing.T) { + client, err := New(&config.ObjectStore{Endpoint: "127.0.0.1:1", Bucket: "attachments", AccessKey: "test", SecretKey: "test"}) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(t.Context(), 50*time.Millisecond) + defer cancel() + if err := client.Put(ctx, "original/test", "image/png", bytes.NewReader(nil), 0); err == nil { + t.Fatal("unavailable PUT succeeded") + } + if _, err := client.Get(ctx, "original/test"); err == nil { + t.Fatal("unavailable GET succeeded") + } + if _, err := client.Exists(ctx, "original/test"); err == nil { + t.Fatal("unavailable HEAD classified as missing") + } + if _, err := client.List(ctx, ""); err == nil { + t.Fatal("unavailable LIST succeeded") + } + if err := client.Ping(ctx); err == nil { + t.Fatal("unavailable bucket ready") + } + if err := client.Bootstrap(ctx); err == nil { + t.Fatal("unavailable bootstrap succeeded") + } +} + +// TestInvalidEndpoint rejects malformed S3 configuration before a network request. +func TestInvalidEndpoint(t *testing.T) { + if _, err := New(&config.ObjectStore{Endpoint: "http://invalid/path"}); err == nil { + t.Fatal("invalid endpoint accepted") + } +} + +// TestBucketBootstrap creates an isolated private bucket twice and verifies scoped credentials cannot access it. +func TestBucketBootstrap(t *testing.T) { + scoped := testObjects(t) + if err := scoped.Ping(t.Context()); err != nil { + t.Fatal(err) + } + cfg, err := config.NewObjectStore() + if err != nil { + t.Fatal(err) + } + secret := os.Getenv("S3_ADMIN_SECRET") + if secret == "" { + t.Skip("isolated S3_ADMIN_SECRET required for bootstrap test") + } + cfg.AccessKey = "operator" + cfg.SecretKey = secret + cfg.Bucket = "test-" + uuid.NewString() + admin, err := New(cfg) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := admin.client.RemoveBucket(context.Background(), cfg.Bucket); err != nil { + t.Error(err) + } + }) + if err := admin.Bootstrap(t.Context()); err != nil { + t.Fatal(err) + } + if err := admin.Bootstrap(t.Context()); err != nil { + t.Fatal(err) + } + scoped.bucket = cfg.Bucket + if _, err := scoped.List(t.Context(), ""); err == nil { + t.Fatal("service identity accessed another bucket") + } +} + +// TestPrivateBucketRejectsAnonymous verifies object ACL independently of application authorization. +func TestPrivateBucketRejectsAnonymous(t *testing.T) { + client := testObjects(t) + key := "original/" + uuid.NewString() + if err := client.Put(t.Context(), key, "application/octet-stream", bytes.NewReader([]byte("private")), 7); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := client.Remove(context.Background(), key); err != nil { + t.Error(err) + } + }) + cfg, err := config.NewObjectStore() + if err != nil { + t.Fatal(err) + } + scheme := "https://" + if !cfg.Secure { + scheme = "http://" + } + httpClient := &http.Client{Timeout: time.Second} + response, err := httpClient.Get(scheme + cfg.Endpoint + "/" + cfg.Bucket + "/" + key) + if err != nil { + t.Fatal(err) + } + if err := response.Body.Close(); err != nil { + t.Fatal(err) + } + if response.StatusCode != http.StatusForbidden { + t.Fatalf("anonymous access status=%d", response.StatusCode) + } +} diff --git a/internal/objectstore/list.go b/internal/objectstore/list.go new file mode 100644 index 0000000..10543a1 --- /dev/null +++ b/internal/objectstore/list.go @@ -0,0 +1,26 @@ +package objectstore + +import ( + "context" + + "github.com/minio/minio-go/v7" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/media" +) + +// List bounds each reconciliation page and cancels the SDK producer before returning. +func (c *Client) List(ctx context.Context, after string) ([]media.ObjectInfo, error) { + ctx, cancel := context.WithTimeout(ctx, operationTimeout) + defer cancel() + result := make([]media.ObjectInfo, 0, 1000) + for object := range c.client.ListObjects(ctx, c.bucket, minio.ListObjectsOptions{Prefix: "original/", StartAfter: after, Recursive: true, MaxKeys: 1000}) { + if object.Err != nil { + return nil, object.Err + } + result = append(result, media.ObjectInfo{Key: object.Key, ModifiedAt: object.LastModified}) + if len(result) == 1000 { + break + } + } + return result, nil +} diff --git a/internal/password/password.go b/internal/password/password.go new file mode 100644 index 0000000..630e160 --- /dev/null +++ b/internal/password/password.go @@ -0,0 +1,54 @@ +// Package password implements a bounded Argon2id password KDF. +package password + +import ( + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "strings" + + "golang.org/x/crypto/argon2" +) + +const ( + memoryKiB = 19 * 1024 + iterations = 2 + parallelism = 1 + saltBytes = 16 + hashBytes = 32 + prefix = "$argon2id$v=19$m=19456,t=2,p=1$" +) + +// Hasher implements password hashing without transport or storage dependencies. +type Hasher struct{} + +// Hash generates a salted Argon2id encoding using fixed resource bounds. +func (Hasher) Hash(password string) (string, error) { + salt := make([]byte, saltBytes) + if _, err := rand.Read(salt); err != nil { + return "", err + } + hash := argon2.IDKey([]byte(password), salt, iterations, memoryKiB, parallelism, hashBytes) + return prefix + base64.RawStdEncoding.EncodeToString(salt) + "$" + base64.RawStdEncoding.EncodeToString(hash), nil +} + +// Verify accepts only this application's bounded KDF encoding and compares in constant time. +func (Hasher) Verify(encoded, password string) bool { + if !strings.HasPrefix(encoded, prefix) { + return false + } + parts := strings.Split(strings.TrimPrefix(encoded, prefix), "$") + if len(parts) != 2 { + return false + } + salt, err := base64.RawStdEncoding.DecodeString(parts[0]) + if err != nil || len(salt) != saltBytes { + return false + } + expected, err := base64.RawStdEncoding.DecodeString(parts[1]) + if err != nil || len(expected) != hashBytes { + return false + } + actual := argon2.IDKey([]byte(password), salt, iterations, memoryKiB, parallelism, hashBytes) + return subtle.ConstantTimeCompare(actual, expected) == 1 +} diff --git a/internal/password/password_test.go b/internal/password/password_test.go new file mode 100644 index 0000000..565df62 --- /dev/null +++ b/internal/password/password_test.go @@ -0,0 +1,37 @@ +package password + +import ( + "strings" + "testing" +) + +// TestPasswordRoundTrip verifies long Unicode input without bcrypt-style truncation. +func TestPasswordRoundTrip(t *testing.T) { + password := strings.Repeat("🔐", 128) + hasher := Hasher{} + encoded, err := hasher.Hash(password) + if err != nil { + t.Fatal(err) + } + if !hasher.Verify(encoded, password) || hasher.Verify(encoded, password+"x") { + t.Fatal("password verification mismatch") + } + second, err := hasher.Hash(password) + if err != nil { + t.Fatal(err) + } + if encoded == second { + t.Fatal("salt reused") + } +} + +// TestPasswordRejectsInvalidEncoding checks corrupt or unbounded KDF descriptions. +func TestPasswordRejectsInvalidEncoding(t *testing.T) { + for _, encoded := range []string{"", "$argon2id$v=19$m=999999999,t=2,p=1$a$b", prefix + "x", prefix + "%%%$abc", prefix + "YWJj$abc", prefix + "AAAAAAAAAAAAAAAAAAAAAA$%%%", prefix + "AAAAAAAAAAAAAAAAAAAAAA$YWJj"} { + t.Run(encoded, func(t *testing.T) { + if (Hasher{}).Verify(encoded, "password") { + t.Fatal("invalid encoding accepted") + } + }) + } +} diff --git a/internal/service/auth.go b/internal/service/auth.go new file mode 100644 index 0000000..2d7a529 --- /dev/null +++ b/internal/service/auth.go @@ -0,0 +1,44 @@ +package service + +import ( + "context" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// Passwords is the password KDF boundary used by authentication. +type Passwords interface { + // Hash creates a salted password encoding. + Hash(password string) (string, error) + // Verify checks the supplied password against its encoding. + Verify(encoded, password string) bool +} + +// Accounts persists credentials and session validity independently of HTTP. +type Accounts interface { + // Register atomically stores a profile with credentials. + Register(context.Context, dto.UserDTO, string) (dto.UserDTO, error) + // Credentials looks up the password encoding and account profile. + Credentials(context.Context, string) (dto.UserDTO, string, error) + // SaveSession persists an opaque token digest and absolute expiry. + SaveSession(context.Context, string, uuid.UUID, time.Time) error + // SessionUser resolves an unexpired token digest. + SessionUser(context.Context, string) (uuid.UUID, error) + // RevokeSession invalidates a token digest. + RevokeSession(context.Context, string) error +} + +// Authentication defines registration and opaque session operations. +type Authentication interface { + // Register validates registration and persists a password-protected account. + Register(context.Context, dto.UsersCreateRequestDTO, string) (dto.UserDTO, error) + // Login verifies credentials and issues an opaque session. + Login(context.Context, string, string) (dto.SessionDTO, error) + // Authenticate resolves a valid token to its immutable owner. + Authenticate(context.Context, string) (string, error) + // Logout revokes the current token. + Logout(context.Context, string) error +} diff --git a/internal/service/authentication/service.go b/internal/service/authentication/service.go new file mode 100644 index 0000000..7737ebf --- /dev/null +++ b/internal/service/authentication/service.go @@ -0,0 +1,123 @@ +// Package authentication validates credentials and owns the session lifecycle. +package authentication + +import ( + "context" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "errors" + "fmt" + "time" + "unicode/utf8" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service/production" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +const ( + minimumPasswordRunes = 12 + maximumPasswordRunes = 128 + tokenBytes = 32 +) + +// Service performs password and session operations through explicit boundaries. +type Service struct { + accounts service.Accounts + passwords service.Passwords + ttl time.Duration + dummyHash string +} + +// New validates dependencies and prepares the unknown-account verification hash. +func New(accounts service.Accounts, passwords service.Passwords, ttl time.Duration) (*Service, error) { + if accounts == nil || passwords == nil || ttl <= 0 { + return nil, fmt.Errorf("accounts, passwords and positive session TTL are required") + } + dummyHash, err := passwords.Hash("unknown-account-dummy-password") + if err != nil { + return nil, err + } + return &Service{accounts: accounts, passwords: passwords, ttl: ttl, dummyHash: dummyHash}, nil +} + +// Register checks profile and Unicode password bounds before hashing and persistence. +func (s *Service) Register(ctx context.Context, request dto.UsersCreateRequestDTO, password string) (dto.UserDTO, error) { + user, err := production.PrepareUser(request) + if err != nil || !validPassword(password) { + return dto.UserDTO{}, service.ErrInvalid + } + hash, err := s.passwords.Hash(password) + if err != nil { + return dto.UserDTO{}, err + } + user, err = s.accounts.Register(ctx, user, hash) + if errors.Is(err, store.ErrConflict) { + return dto.UserDTO{}, service.ErrConflict + } + return user, err +} + +// Login verifies credentials uniformly and persists a new session before returning its token. +func (s *Service) Login(ctx context.Context, username, password string) (dto.SessionDTO, error) { + if !validPassword(password) { + return dto.SessionDTO{}, service.ErrUnauthorized + } + user, hash, err := s.accounts.Credentials(ctx, username) + if err != nil && !errors.Is(err, store.ErrNotFound) { + return dto.SessionDTO{}, err + } + missing := errors.Is(err, store.ErrNotFound) + if missing { + hash = s.dummyHash + } + if !s.passwords.Verify(hash, password) || missing { + return dto.SessionDTO{}, service.ErrUnauthorized + } + random := make([]byte, tokenBytes) + if _, err := rand.Read(random); err != nil { + return dto.SessionDTO{}, err + } + token := base64.RawURLEncoding.EncodeToString(random) + expiry := time.Now().UTC().Add(s.ttl) + if err := s.accounts.SaveSession(ctx, digest(token), user.ID, expiry); err != nil { + return dto.SessionDTO{}, err + } + return dto.SessionDTO{Token: token, ExpiresAt: expiry, User: user}, nil +} + +// Authenticate checks absolute expiry in the source of truth on every request. +func (s *Service) Authenticate(ctx context.Context, token string) (string, error) { + decoded, err := base64.RawURLEncoding.DecodeString(token) + if err != nil || len(decoded) != tokenBytes { + return "", service.ErrUnauthorized + } + id, err := s.accounts.SessionUser(ctx, digest(token)) + if errors.Is(err, store.ErrNotFound) { + return "", service.ErrUnauthorized + } + if err != nil { + return "", err + } + return id.String(), nil +} + +// Logout removes the current session from the authoritative store. +func (s *Service) Logout(ctx context.Context, token string) error { + return s.accounts.RevokeSession(ctx, digest(token)) +} + +// digest produces the storage key without persisting the bearer secret. +func digest(token string) string { + hash := sha256.Sum256([]byte(token)) + return hex.EncodeToString(hash[:]) +} + +// validPassword enforces the contract in Unicode code points without truncation. +func validPassword(password string) bool { + count := utf8.RuneCountInString(password) + return utf8.ValidString(password) && count >= minimumPasswordRunes && count <= maximumPasswordRunes +} diff --git a/internal/service/authentication/service_test.go b/internal/service/authentication/service_test.go new file mode 100644 index 0000000..70209f8 --- /dev/null +++ b/internal/service/authentication/service_test.go @@ -0,0 +1,232 @@ +package authentication + +import ( + "context" + "errors" + "strings" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +type accountFixture struct { + user dto.UserDTO + hash string + tokenHash string + expiry time.Time + failure error + registerFailure error + saveFailure error +} + +// Register captures one synthetic account or a prescribed persistence failure. +func (f *accountFixture) Register(_ context.Context, user dto.UserDTO, hash string) (dto.UserDTO, error) { + f.user = user + f.hash = hash + return user, f.registerFailure +} + +// Credentials returns the fixture account and its password encoding. +func (f *accountFixture) Credentials(context.Context, string) (dto.UserDTO, string, error) { + return f.user, f.hash, f.failure +} + +// SaveSession captures the digest and absolute expiry without storing the raw token. +func (f *accountFixture) SaveSession(_ context.Context, hash string, _ uuid.UUID, expiry time.Time) error { + f.tokenHash = hash + f.expiry = expiry + return f.saveFailure +} + +// SessionUser implements expiry and revocation for the service unit fixture. +func (f *accountFixture) SessionUser(_ context.Context, hash string) (uuid.UUID, error) { + if f.failure != nil { + return uuid.Nil, f.failure + } + if hash != f.tokenHash || !f.expiry.After(time.Now()) { + return uuid.Nil, store.ErrNotFound + } + return f.user.ID, nil +} + +// RevokeSession invalidates the fixture digest. +func (f *accountFixture) RevokeSession(context.Context, string) error { + f.tokenHash = "" + return f.failure +} + +type passwordFixture struct { + failure error +} + +// Hash creates an intentionally noncryptographic encoding for service-only unit tests. +func (f passwordFixture) Hash(password string) (string, error) { + return "fixture:" + password, f.failure +} + +// Verify compares only test fixture encodings; production uses Argon2id. +func (passwordFixture) Verify(encoded, password string) bool { + return encoded == "fixture:"+password +} + +// authFixture assembles deterministic service boundaries. +func authFixture(t *testing.T) (*Service, *accountFixture) { + t.Helper() + accounts := &accountFixture{user: dto.UserDTO{ID: uuid.New(), Username: "alice"}, hash: "fixture:synthetic-password"} + s, err := New(accounts, passwordFixture{}, time.Hour) + if err != nil { + t.Fatal(err) + } + return s, accounts +} + +// TestRegisterValidatesPassword checks contract boundaries without truncation. +func TestRegisterValidatesPassword(t *testing.T) { + cases := []struct { + name string + password string + valid bool + }{ + {"short", "short", false}, + {"minimum", strings.Repeat("a", 12), true}, + {"unicode maximum", strings.Repeat("🔐", 128), true}, + {"too long", strings.Repeat("a", 129), false}, + {"invalid UTF8", strings.Repeat("a", 12) + "\xff", false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + s, _ := authFixture(t) + _, err := s.Register(t.Context(), dto.UsersCreateRequestDTO{Username: "alice", DisplayName: "Alice"}, tc.password) + if (err == nil) != tc.valid { + t.Fatalf("unexpected registration result: %v", err) + } + }) + } +} + +// TestRegisterConflictMapsToApplicationError verifies uniqueness error translation. +func TestRegisterConflictMapsToApplicationError(t *testing.T) { + s, accounts := authFixture(t) + accounts.registerFailure = store.ErrConflict + _, err := s.Register(t.Context(), dto.UsersCreateRequestDTO{Username: "alice", DisplayName: "Alice"}, "synthetic-password") + if !errors.Is(err, service.ErrConflict) { + t.Fatal(err) + } +} + +// TestRegisterHashFailurePreservesCause checks KDF failures before persistence. +func TestRegisterHashFailurePreservesCause(t *testing.T) { + s, _ := authFixture(t) + failure := errors.New("KDF unavailable") + s.passwords = passwordFixture{failure: failure} + _, err := s.Register(t.Context(), dto.UsersCreateRequestDTO{Username: "alice", DisplayName: "Alice"}, "synthetic-password") + if !errors.Is(err, failure) { + t.Fatal(err) + } +} + +// TestLoginPersistsOnlyTokenDigest checks issued entropy, owner and deadline together. +func TestLoginPersistsOnlyTokenDigest(t *testing.T) { + s, accounts := authFixture(t) + session, err := s.Login(t.Context(), "alice", "synthetic-password") + if err != nil { + t.Fatal(err) + } + if session.Token == accounts.tokenHash || len(accounts.tokenHash) != 64 || session.User.ID != accounts.user.ID || !session.ExpiresAt.Equal(accounts.expiry) { + t.Fatal("invalid issued session") + } + id, err := s.Authenticate(t.Context(), session.Token) + if err != nil || id != accounts.user.ID.String() { + t.Fatalf("session does not authenticate its owner: %v", err) + } +} + +// TestLoginRejectsCredentials checks independent authentication rejection scenarios. +func TestLoginRejectsCredentials(t *testing.T) { + cases := []struct { + name string + password string + failure error + expected error + }{ + {"short password", "short", nil, service.ErrUnauthorized}, + {"wrong password", "incorrect-password", nil, service.ErrUnauthorized}, + {"missing account", "synthetic-password", store.ErrNotFound, service.ErrUnauthorized}, + {"database unavailable", "synthetic-password", context.DeadlineExceeded, context.DeadlineExceeded}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + s, accounts := authFixture(t) + accounts.failure = tc.failure + _, err := s.Login(t.Context(), "alice", tc.password) + if !errors.Is(err, tc.expected) { + t.Fatalf("expected %v, got %v", tc.expected, err) + } + }) + } +} + +// TestLoginDoesNotReturnUnpersistedSession checks failure after successful password verification. +func TestLoginDoesNotReturnUnpersistedSession(t *testing.T) { + s, accounts := authFixture(t) + accounts.saveFailure = context.DeadlineExceeded + session, err := s.Login(t.Context(), "alice", "synthetic-password") + if !errors.Is(err, context.DeadlineExceeded) || session.Token != "" { + t.Fatal("unpersisted session escaped") + } +} + +// TestAuthenticateRejectsUnknownToken checks malformed and correctly encoded unknown tokens. +func TestAuthenticateRejectsUnknownToken(t *testing.T) { + for _, token := range []string{"", "%%%", strings.Repeat("A", 43)} { + t.Run(token, func(t *testing.T) { + s, _ := authFixture(t) + if _, err := s.Authenticate(t.Context(), token); !errors.Is(err, service.ErrUnauthorized) { + t.Fatal(err) + } + }) + } +} + +// TestAuthenticatePropagatesDatabaseFailure prevents trusting a token during an outage. +func TestAuthenticatePropagatesDatabaseFailure(t *testing.T) { + s, accounts := authFixture(t) + accounts.failure = context.DeadlineExceeded + if _, err := s.Authenticate(t.Context(), strings.Repeat("A", 43)); !errors.Is(err, context.DeadlineExceeded) { + t.Fatal(err) + } +} + +// TestLogoutRevokesIssuedToken checks that the same token cannot be reused. +func TestLogoutRevokesIssuedToken(t *testing.T) { + s, _ := authFixture(t) + session, err := s.Login(t.Context(), "alice", "synthetic-password") + if err != nil { + t.Fatal(err) + } + if err := s.Logout(t.Context(), session.Token); err != nil { + t.Fatal(err) + } + if _, err := s.Authenticate(t.Context(), session.Token); !errors.Is(err, service.ErrUnauthorized) { + t.Fatal("revoked token authenticated") + } +} + +// TestNewRejectsIncompleteDependencies checks constructor validation and KDF startup failure. +func TestNewRejectsIncompleteDependencies(t *testing.T) { + t.Run("missing accounts", func(t *testing.T) { + if _, err := New(nil, passwordFixture{}, time.Hour); err == nil { + t.Fatal("missing accounts accepted") + } + }) + t.Run("hash failure", func(t *testing.T) { + if _, err := New(&accountFixture{}, passwordFixture{failure: context.Canceled}, time.Hour); !errors.Is(err, context.Canceled) { + t.Fatal(err) + } + }) +} diff --git a/internal/service/cache.go b/internal/service/cache.go new file mode 100644 index 0000000..586aa61 --- /dev/null +++ b/internal/service/cache.go @@ -0,0 +1,17 @@ +package service + +import ( + "context" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// ProfileCache stores immutable public account profiles; misses fall back to PostgreSQL. +type ProfileCache interface { + // GetProfile returns a cached profile or a miss, including on backend failure. + GetProfile(context.Context, uuid.UUID) (dto.UserDTO, bool) + // PutProfile stores a bounded-lifetime profile on a best-effort basis. + PutProfile(context.Context, dto.UserDTO) +} diff --git a/internal/service/cursor.go b/internal/service/cursor.go new file mode 100644 index 0000000..233d960 --- /dev/null +++ b/internal/service/cursor.go @@ -0,0 +1,12 @@ +package service + +import "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor" + +// CursorCodec defines the pagination operations required by application services. +type CursorCodec interface { + // Encode signs a position for the specified resource. + Encode(resource string, position cursor.Position) (string, error) + + // Decode verifies a token and returns its position; empty tokens start a page sequence. + Decode(encoded, resource string) (cursor.Position, error) +} diff --git a/internal/service/errors.go b/internal/service/errors.go new file mode 100644 index 0000000..57a90be --- /dev/null +++ b/internal/service/errors.go @@ -0,0 +1,27 @@ +package service + +import ( + "errors" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +var ( + // ErrTooLarge rejects encoded or decoded images beyond policy. + ErrTooLarge = errors.New("image exceeds limit") + // ErrMedia rejects unsupported or corrupted image bytes. + ErrMedia = errors.New("invalid image") + // ErrUnavailable preserves the dependency failure category across service boundaries. + ErrUnavailable = store.ErrUnavailable + + // ErrInvalid indicates invalid operation input. + ErrInvalid = errors.New("invalid request") + // ErrUnauthorized indicates an unresolved current user. + ErrUnauthorized = errors.New("unknown user identity") + // ErrNotFound hides both absent and inaccessible resources. + ErrNotFound = errors.New("resource not found") + // ErrConflict indicates a duplicate username. + ErrConflict = errors.New("username already exists") + // ErrCapacity indicates exhausted storage capacity without eviction. + ErrCapacity = errors.New("in-memory storage capacity reached") +) diff --git a/internal/service/interface.go b/internal/service/interface.go new file mode 100644 index 0000000..2d42497 --- /dev/null +++ b/internal/service/interface.go @@ -0,0 +1,36 @@ +// Package service defines the application operations consumed by HTTP controllers. +package service + +import ( + "context" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// Service validates raw input and enforces identity and membership requirements. +// Implementations must not expose transport-specific types or storage internals. +type Service interface { + // UsersCreate registers a user or reports invalid input, conflict or capacity exhaustion. + UsersCreate(ctx context.Context, request dto.UsersCreateRequestDTO) (dto.UserDTO, error) + + // UsersCheckIdentity verifies that an identity belongs to an existing user. + UsersCheckIdentity(ctx context.Context, userID string) error + + // UsersGetMe resolves the authenticated caller, rejecting malformed or unknown identifiers. + UsersGetMe(ctx context.Context, userID string) (dto.UserDTO, error) + + // ChatsCreate validates all members and creates one chat atomically. + ChatsCreate(ctx context.Context, request dto.ChatsCreateRequestDTO) (dto.ChatDTO, error) + + // ChatsGetByID hides missing and inaccessible chats behind the same not-found result. + ChatsGetByID(ctx context.Context, userID, chatID string) (dto.ChatDTO, error) + + // ChatsGetPage lists only chats visible to the caller, using resource-bound cursors. + ChatsGetPage(ctx context.Context, request dto.PageRequestDTO) (dto.ChatPageDTO, error) + + // MessagesCreate derives the sender from the identity and appends validated text. + MessagesCreate(ctx context.Context, request dto.MessagesCreateRequestDTO) (dto.MessageDTO, error) + + // MessagesGetPage authorizes the caller and returns history after a verified cursor. + MessagesGetPage(ctx context.Context, request dto.PageRequestDTO) (dto.MessagePageDTO, error) +} diff --git a/internal/service/production/access.go b/internal/service/production/access.go new file mode 100644 index 0000000..fa5073b --- /dev/null +++ b/internal/service/production/access.go @@ -0,0 +1,62 @@ +package production + +import ( + "context" + "slices" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// parseIdentity maps malformed identity values to the authentication error. +func parseIdentity(rawID string) (uuid.UUID, error) { + id, err := parseID(rawID) + if err != nil { + return uuid.Nil, service.ErrUnauthorized + } + return id, nil +} + +// requireUserID resolves an existing identity without loading a user profile. +func (s *Service) requireUserID(ctx context.Context, rawID string) (uuid.UUID, error) { + id, err := parseIdentity(rawID) + if err != nil { + return uuid.Nil, err + } + exists, err := s.repo.UsersExist(ctx, []uuid.UUID{id}) + if err != nil { + return uuid.Nil, normalizeStoreError(err) + } + if !exists { + return uuid.Nil, service.ErrUnauthorized + } + return id, nil +} + +// requireChatMember resolves an existing caller and a chat available to that caller. +func (s *Service) requireChatMember(ctx context.Context, userID, chatID string) (uuid.UUID, dto.ChatDTO, error) { + caller, err := s.requireUserID(ctx, userID) + if err != nil { + return uuid.Nil, dto.ChatDTO{}, err + } + chat, err := s.findChat(ctx, chatID) + if err != nil { + return uuid.Nil, dto.ChatDTO{}, err + } + if !slices.Contains(chat.MemberIDs, caller) { + return uuid.Nil, dto.ChatDTO{}, service.ErrNotFound + } + return caller, chat, nil +} + +// findChat parses a chat identifier and translates storage lookup errors. +func (s *Service) findChat(ctx context.Context, rawID string) (dto.ChatDTO, error) { + id, err := parseID(rawID) + if err != nil { + return dto.ChatDTO{}, err + } + chat, err := s.repo.Chat(ctx, id) + return chat, normalizeStoreError(err) +} diff --git a/internal/service/production/cache_test.go b/internal/service/production/cache_test.go new file mode 100644 index 0000000..3d57393 --- /dev/null +++ b/internal/service/production/cache_test.go @@ -0,0 +1,84 @@ +package production + +import ( + "context" + "testing" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +type cacheFixture struct { + profile dto.UserDTO + hit bool + writes int +} + +// GetProfile returns the arranged optional-cache lookup outcome. +func (f *cacheFixture) GetProfile(context.Context, uuid.UUID) (dto.UserDTO, bool) { + return f.profile, f.hit +} + +// PutProfile records successful cache population from authoritative data. +func (f *cacheFixture) PutProfile(_ context.Context, user dto.UserDTO) { + f.profile = user + f.writes++ +} + +// TestProfileCacheHitAvoidsProfileQuery verifies that the cache has a concrete role. +func TestProfileCacheHitAvoidsProfileQuery(t *testing.T) { + codec, err := cursor.New() + if err != nil { + t.Fatal(err) + } + cache := &cacheFixture{profile: dto.UserDTO{ID: uuid.New()}, hit: true} + svc, err := NewCached(&storeStub{t: t}, codec, cache) + if err != nil { + t.Fatal(err) + } + got, err := svc.UsersGetMe(t.Context(), cache.profile.ID.String()) + if err != nil || got.ID != cache.profile.ID { + t.Fatalf("cache hit ignored: %v", err) + } +} + +// TestProfileCacheMissPopulatesAfterDatabaseRead checks the authoritative fallback. +func TestProfileCacheMissPopulatesAfterDatabaseRead(t *testing.T) { + codec, err := cursor.New() + if err != nil { + t.Fatal(err) + } + id := uuid.New() + repo := &storeStub{t: t, user: func(context.Context, uuid.UUID) (dto.UserDTO, error) { + return dto.UserDTO{ID: id}, nil + }} + cache := &cacheFixture{} + svc, err := NewCached(repo, codec, cache) + if err != nil { + t.Fatal(err) + } + got, err := svc.UsersGetMe(t.Context(), id.String()) + if err != nil || got.ID != id || cache.writes != 1 { + t.Fatalf("fallback failed: %v", err) + } +} + +// TestCachedServiceRejectsMissingDependencies checks explicit service boundaries. +func TestCachedServiceRejectsMissingDependencies(t *testing.T) { + t.Run("store", func(t *testing.T) { + if _, err := NewCached(nil, nil, &cacheFixture{}); err == nil { + t.Fatal("missing store accepted") + } + }) + t.Run("cache", func(t *testing.T) { + codec, err := cursor.New() + if err != nil { + t.Fatal(err) + } + if _, err := NewCached(&storeStub{t: t}, codec, nil); err == nil { + t.Fatal("missing cache accepted") + } + }) +} diff --git a/internal/service/production/chats.go b/internal/service/production/chats.go new file mode 100644 index 0000000..ecba6ac --- /dev/null +++ b/internal/service/production/chats.go @@ -0,0 +1,47 @@ +package production + +import ( + "context" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// ChatsCreate resolves the caller, validates the proposed chat and persists it atomically. +func (s *Service) ChatsCreate(ctx context.Context, request dto.ChatsCreateRequestDTO) (dto.ChatDTO, error) { + caller, err := s.requireUserID(ctx, request.UserID) + if err != nil { + return dto.ChatDTO{}, err + } + chat, err := prepareChat(request, caller) + if err != nil { + return dto.ChatDTO{}, err + } + if err := s.requireExistingMembers(ctx, chat.MemberIDs); err != nil { + return dto.ChatDTO{}, err + } + created, err := s.repo.CreateChat(ctx, chat) + return created, normalizeStoreError(err) +} + +// ChatsGetByID returns a chat only to a current member, hiding its existence otherwise. +func (s *Service) ChatsGetByID(ctx context.Context, userID, chatID string) (dto.ChatDTO, error) { + _, chat, err := s.requireChatMember(ctx, userID, chatID) + return chat, err +} + +// ChatsGetPage authorizes the caller, loads a page and prepares its continuation. +func (s *Service) ChatsGetPage(ctx context.Context, request dto.PageRequestDTO) (dto.ChatPageDTO, error) { + caller, err := s.requireUserID(ctx, request.UserID) + if err != nil { + return dto.ChatPageDTO{}, err + } + page, err := s.parsePage(request, chatListResource(caller)) + if err != nil { + return dto.ChatPageDTO{}, err + } + items, err := s.repo.Chats(ctx, caller, page.position, page.fetchLimit()) + if err != nil { + return dto.ChatPageDTO{}, normalizeStoreError(err) + } + return s.chatPage(items, page) +} diff --git a/internal/service/production/chats_page_test.go b/internal/service/production/chats_page_test.go new file mode 100644 index 0000000..10d2d61 --- /dev/null +++ b/internal/service/production/chats_page_test.go @@ -0,0 +1,207 @@ +package production + +import ( + "context" + "errors" + "reflect" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// TestService_ChatsGetPage validates access and pagination before issuing a typed store query. +func TestService_ChatsGetPage(t *testing.T) { + t.Run("Positive: continuation uses scope, position and lookahead", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + allowIdentity(repo, actor) + + previous := cursor.Position{ID: uuid.New(), CreatedAt: time.Unix(1700000000, 0).UTC()} + records := []dto.ChatDTO{ + {ID: uuid.New(), CreatedAt: time.Unix(1700000001, 0).UTC()}, + {ID: uuid.New(), CreatedAt: time.Unix(1700000002, 0).UTC()}, + {ID: uuid.New(), CreatedAt: time.Unix(1700000003, 0).UTC()}, + } + calls := 0 + repo.chats = func(_ context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.ChatDTO, error) { + calls++ + expected := dto.PositionDTO{ID: previous.ID, CreatedAt: previous.CreatedAt} + if id != actor || position != expected || limit != 3 { + t.Fatalf("wrong page query: id=%s, position=%v, limit=%d", id, position, limit) + } + return records, nil + } + svc := newStubService(t, repo) + resource := "chats:" + actor.String() + token, err := svc.cursors.Encode(resource, previous) + if err != nil { + t.Fatal(err) + } + input := dto.PageRequestDTO{UserID: actor.String(), Limit: "2", Cursor: token} + + // Act. + got, err := svc.ChatsGetPage(t.Context(), input) + + // Assert. + + if err != nil { + t.Fatal(err) + } + if calls != 1 || !reflect.DeepEqual(got.Items, records[:2]) || got.NextCursor == nil { + t.Fatalf("calls=%d, page=%+v", calls, got) + } + position, err := svc.cursors.Decode(*got.NextCursor, resource) + expected := cursor.Position{ID: records[1].ID, CreatedAt: records[1].CreatedAt} + if err != nil || position != expected { + t.Fatalf("next position=%v, want=%v, error=%v", position, expected, err) + } + + }) + + t.Run("Positive: empty history uses default limit and no continuation", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + allowIdentity(repo, actor) + + calls := 0 + repo.chats = func(_ context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.ChatDTO, error) { + calls++ + if id != actor || position != (dto.PositionDTO{}) || limit != 51 { + t.Fatalf("wrong initial page query: id=%s, position=%v, limit=%d", id, position, limit) + } + return []dto.ChatDTO{}, nil + } + svc := newStubService(t, repo) + input := dto.PageRequestDTO{UserID: actor.String()} + + // Act. + got, err := svc.ChatsGetPage(t.Context(), input) + + // Assert. + if err != nil { + t.Fatal(err) + } + if calls != 1 || got.Items == nil || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("calls=%d, page=%+v", calls, got) + } + }) + + t.Run("Negative: cursor for another resource skips page lookup", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + allowIdentity(repo, actor) + + svc := newStubService(t, repo) + token, err := svc.cursors.Encode("another-resource", cursor.Position{ID: uuid.New(), CreatedAt: time.Unix(1700000000, 0).UTC()}) + if err != nil { + t.Fatal(err) + } + input := dto.PageRequestDTO{UserID: actor.String(), Cursor: token} + + // Act. + got, err := svc.ChatsGetPage(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrInvalid) || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("page=%+v, error=%v", got, err) + } + }) + + t.Run("Negative: codec failure skips page lookup", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + allowIdentity(repo, actor) + + failure := errors.New("cursor unavailable") + svc := newStubService(t, repo) + svc.cursors = failingCursor{failure: failure} + input := dto.PageRequestDTO{UserID: actor.String(), Cursor: "token"} + + // Act. + got, err := svc.ChatsGetPage(t.Context(), input) + + // Assert. + if !errors.Is(err, failure) || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("page=%+v, error=%v", got, err) + } + }) + + t.Run("Negative: page storage fails", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + allowIdentity(repo, actor) + + failure := errors.New("page unavailable") + repo.chats = func(_ context.Context, _ uuid.UUID, _ dto.PositionDTO, _ int) ([]dto.ChatDTO, error) { + return nil, failure + } + svc := newStubService(t, repo) + input := dto.PageRequestDTO{UserID: actor.String()} + + // Act. + got, err := svc.ChatsGetPage(t.Context(), input) + + // Assert. + if !errors.Is(err, failure) || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("page=%+v, error=%v", got, err) + } + }) + + t.Run("Negative: malformed identity skips all storage", func(t *testing.T) { + // Arrange. + svc := newStubService(t, newStoreStub(t)) + input := dto.PageRequestDTO{UserID: "invalid", ChatID: uuid.NewString()} + + // Act. + got, err := svc.ChatsGetPage(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrUnauthorized) || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("page=%+v, error=%v", got, err) + } + }) +} + +// TestService_ChatsGetPage_InvalidLimit rejects invalid limits before reading stored pages. +func TestService_ChatsGetPage_InvalidLimit(t *testing.T) { + cases := []struct { + name string + limit string + }{ + {"Negative: zero limit", "0"}, + {"Negative: negative limit", "-1"}, + {"Negative: limit above maximum", "101"}, + {"Negative: nonnumeric limit", "text"}, + {"Negative: fractional limit", "1.5"}, + {"Negative: integer overflow", "99999999999999999999999999"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + allowIdentity(repo, actor) + + svc := newStubService(t, repo) + input := dto.PageRequestDTO{UserID: actor.String(), Limit: tc.limit} + + // Act. + got, err := svc.ChatsGetPage(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrInvalid) || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("page=%+v, error=%v", got, err) + } + }) + } +} diff --git a/internal/service/production/chats_test.go b/internal/service/production/chats_test.go new file mode 100644 index 0000000..34623ad --- /dev/null +++ b/internal/service/production/chats_test.go @@ -0,0 +1,261 @@ +package production + +import ( + "context" + "errors" + "reflect" + "slices" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// TestService_ChatsCreate checks membership in one batch and persists only validated chats. +func TestService_ChatsCreate(t *testing.T) { + t.Run("Positive: one membership query and original chat fields", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor, other := uuid.New(), uuid.New() + var queries [][]uuid.UUID + repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) { + queries = append(queries, slices.Clone(ids)) + return true, nil + } + var persisted dto.ChatDTO + repo.createChat = func(_ context.Context, input dto.ChatDTO) (dto.ChatDTO, error) { + persisted = input + input.CreatedAt = time.Unix(1700000000, 0).UTC() + return input, nil + } + svc := newStubService(t, repo) + input := dto.ChatsCreateRequestDTO{UserID: actor.String(), Title: " Team 👋 ", MemberIDs: []string{actor.String(), other.String()}} + + // Act. + got, err := svc.ChatsCreate(t.Context(), input) + + // Assert. + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(queries, [][]uuid.UUID{{actor}, {actor, other}}) { + t.Fatalf("existence queries=%v", queries) + } + if persisted.ID == uuid.Nil || persisted.Title != input.Title || !slices.Equal(persisted.MemberIDs, []uuid.UUID{actor, other}) { + t.Fatalf("wrong persistence input: %+v", persisted) + } + if got.ID != persisted.ID || got.CreatedAt.IsZero() || got.Title != input.Title || !slices.Equal(got.MemberIDs, persisted.MemberIDs) { + t.Fatalf("wrong returned chat: %+v", got) + } + }) + + t.Run("Negative: unknown member prevents persistence", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor, missing := uuid.New(), uuid.New() + repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) { + return !slices.Contains(ids, missing), nil + } + svc := newStubService(t, repo) + input := dto.ChatsCreateRequestDTO{UserID: actor.String(), Title: "Team", MemberIDs: []string{actor.String(), missing.String()}} + + // Act. + got, err := svc.ChatsCreate(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrInvalid) || !reflect.DeepEqual(got, dto.ChatDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + }) + + t.Run("Negative: membership lookup failure prevents persistence", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor, other := uuid.New(), uuid.New() + failure := errors.New("membership unavailable") + repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) { + if len(ids) == 1 { + return true, nil + } + return false, failure + } + svc := newStubService(t, repo) + input := dto.ChatsCreateRequestDTO{UserID: actor.String(), Title: "Team", MemberIDs: []string{actor.String(), other.String()}} + + // Act. + got, err := svc.ChatsCreate(t.Context(), input) + + // Assert. + if !errors.Is(err, failure) || !reflect.DeepEqual(got, dto.ChatDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + }) + + t.Run("Negative: unknown creator prevents membership and persistence calls", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + repo.usersExist = func(_ context.Context, _ []uuid.UUID) (bool, error) { + return false, nil + } + svc := newStubService(t, repo) + actor := uuid.New() + input := dto.ChatsCreateRequestDTO{UserID: actor.String(), Title: "Team", MemberIDs: []string{actor.String()}} + + // Act. + got, err := svc.ChatsCreate(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrUnauthorized) || !reflect.DeepEqual(got, dto.ChatDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + }) +} + +// TestService_ChatsCreate_InvalidInput rejects malformed chat fields before checking member existence. +func TestService_ChatsCreate_InvalidInput(t *testing.T) { + actor := uuid.New() + cases := []struct { + name string + title string + members []string + }{ + {"Negative: empty member set", "Team", nil}, + {"Negative: duplicate member", "Team", []string{actor.String(), actor.String()}}, + {"Negative: caller omitted", "Team", []string{uuid.NewString()}}, + {"Negative: malformed member ID", "Team", []string{actor.String(), "invalid"}}, + {"Negative: blank title", " ", []string{actor.String()}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + calls := 0 + repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) { + calls++ + if calls != 1 || !slices.Equal(ids, []uuid.UUID{actor}) { + t.Fatalf("unexpected membership check: %v", ids) + } + return true, nil + } + svc := newStubService(t, repo) + input := dto.ChatsCreateRequestDTO{UserID: actor.String(), Title: tc.title, MemberIDs: tc.members} + + // Act. + got, err := svc.ChatsCreate(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrInvalid) || !reflect.DeepEqual(got, dto.ChatDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + if calls != 1 { + t.Fatalf("identity checks=%d, want=1", calls) + } + }) + } +} + +// TestService_ChatsGetByID authorizes the requested chat and conceals inaccessible records. +func TestService_ChatsGetByID(t *testing.T) { + t.Run("Positive: member receives the complete chat", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + expected := dto.ChatDTO{ID: uuid.New(), Title: "Team", MemberIDs: []uuid.UUID{actor}, CreatedAt: time.Unix(1700000000, 0).UTC()} + allowChatAccess(repo, actor, expected) + svc := newStubService(t, repo) + + // Act. + got, err := svc.ChatsGetByID(t.Context(), actor.String(), expected.ID.String()) + + // Assert. + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(got, expected) { + t.Fatalf("chat=%+v, want=%+v", got, expected) + } + }) + + t.Run("Negative: outsider receives no chat details", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + chat := dto.ChatDTO{ID: uuid.New(), Title: "Private", MemberIDs: []uuid.UUID{uuid.New()}} + allowChatAccess(repo, actor, chat) + svc := newStubService(t, repo) + + // Act. + got, err := svc.ChatsGetByID(t.Context(), actor.String(), chat.ID.String()) + + // Assert. + if !errors.Is(err, service.ErrNotFound) || !reflect.DeepEqual(got, dto.ChatDTO{}) { + t.Fatalf("private chat exposed: %+v, error=%v", got, err) + } + }) + + t.Run("Negative: malformed chat ID skips chat lookup", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + allowIdentity(repo, actor) + svc := newStubService(t, repo) + + // Act. + got, err := svc.ChatsGetByID(t.Context(), actor.String(), "invalid") + + // Assert. + if !errors.Is(err, service.ErrInvalid) || !reflect.DeepEqual(got, dto.ChatDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + }) + + t.Run("Negative: malformed identity skips storage", func(t *testing.T) { + // Arrange. + svc := newStubService(t, newStoreStub(t)) + + // Act. + got, err := svc.ChatsGetByID(t.Context(), "invalid", uuid.NewString()) + + // Assert. + if !errors.Is(err, service.ErrUnauthorized) || !reflect.DeepEqual(got, dto.ChatDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + }) +} + +// TestService_ChatsGetByID_StoreErrors translates missing records and retains unexpected failures. +func TestService_ChatsGetByID_StoreErrors(t *testing.T) { + failure := errors.New("chat lookup unavailable") + cases := []struct { + name string + cause error + want error + }{ + {"Negative: absent chat", store.ErrNotFound, service.ErrNotFound}, + {"Negative: storage unavailable", failure, failure}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + allowIdentity(repo, actor) + repo.chat = func(_ context.Context, _ uuid.UUID) (dto.ChatDTO, error) { + return dto.ChatDTO{}, tc.cause + } + svc := newStubService(t, repo) + + // Act. + got, err := svc.ChatsGetByID(t.Context(), actor.String(), uuid.NewString()) + + // Assert. + if !errors.Is(err, tc.want) || !reflect.DeepEqual(got, dto.ChatDTO{}) { + t.Fatalf("result=%+v, error=%v, want=%v", got, err, tc.want) + } + }) + } +} diff --git a/internal/service/production/creation.go b/internal/service/production/creation.go new file mode 100644 index 0000000..216ad96 --- /dev/null +++ b/internal/service/production/creation.go @@ -0,0 +1,75 @@ +package production + +import ( + "strings" + "unicode/utf8" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// PrepareUser validates registration fields and assigns the new user identifier. +func PrepareUser(request dto.UsersCreateRequestDTO) (dto.UserDTO, error) { + if !validUsername(request.Username) || !validText(request.DisplayName, maxDisplayNameRunes) { + return dto.UserDTO{}, service.ErrInvalid + } + return dto.UserDTO{ + ID: uuid.New(), + Username: request.Username, + DisplayName: request.DisplayName, + }, nil +} + +// prepareChat validates the title and member identifiers before storage access. +func prepareChat(request dto.ChatsCreateRequestDTO, caller uuid.UUID) (dto.ChatDTO, error) { + if !validText(request.Title, maxChatTitleRunes) { + return dto.ChatDTO{}, service.ErrInvalid + } + members, err := parseMembers(request.MemberIDs, caller) + if err != nil { + return dto.ChatDTO{}, err + } + return dto.ChatDTO{ + ID: uuid.New(), + Title: request.Title, + MemberIDs: members, + }, nil +} + +// prepareMessage validates text and creates a record for the authorized sender and chat. +func prepareMessage(request dto.MessagesCreateRequestDTO, sender, chatID uuid.UUID) (dto.MessageDTO, error) { + if (strings.TrimSpace(request.Text) == "" && len(request.AttachmentIDs) == 0) || !utf8.ValidString(request.Text) || utf8.RuneCountInString(request.Text) > maxMessageRunes || len(request.AttachmentIDs) > 10 || !validIdempotencyKey(request.IdempotencyKey) { + return dto.MessageDTO{}, service.ErrInvalid + } + seen := make(map[string]bool) + for _, id := range request.AttachmentIDs { + if _, err := parseID(id); err != nil || seen[id] { + return dto.MessageDTO{}, service.ErrInvalid + } + seen[id] = true + } + return dto.MessageDTO{ + ID: uuid.New(), + ChatID: chatID, + SenderID: sender, + Text: request.Text, + AttachmentIDs: append([]string{}, request.AttachmentIDs...), + IdempotencyKey: request.IdempotencyKey, + }, nil +} + +// validIdempotencyKey accepts only the bounded ASCII alphabet from the contract. +func validIdempotencyKey(key string) bool { + if len(key) < 1 || len(key) > 128 { + return false + } + for _, c := range key { + if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '.' || c == '_' || c == ':' || c == '-' { + continue + } + return false + } + return true +} diff --git a/internal/service/production/errors.go b/internal/service/production/errors.go new file mode 100644 index 0000000..996a8cb --- /dev/null +++ b/internal/service/production/errors.go @@ -0,0 +1,23 @@ +package production + +import ( + "errors" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// normalizeStoreError maps expected storage failures to application errors. +// Unexpected failures retain their cause for the transport's safe fallback. +func normalizeStoreError(err error) error { + switch { + case errors.Is(err, store.ErrNotFound): + return service.ErrNotFound + case errors.Is(err, store.ErrConflict): + return service.ErrConflict + case errors.Is(err, store.ErrCapacity): + return service.ErrCapacity + default: + return err + } +} diff --git a/internal/service/production/fixture_test.go b/internal/service/production/fixture_test.go new file mode 100644 index 0000000..9aa1a12 --- /dev/null +++ b/internal/service/production/fixture_test.go @@ -0,0 +1,147 @@ +package production + +import ( + "context" + "testing" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// storeStub permits only the interactions explicitly arranged by each service scenario. +type storeStub struct { + t *testing.T + createUser func(ctx context.Context, user dto.UserDTO) (dto.UserDTO, error) + user func(ctx context.Context, id uuid.UUID) (dto.UserDTO, error) + usersExist func(ctx context.Context, ids []uuid.UUID) (bool, error) + createChat func(ctx context.Context, chat dto.ChatDTO) (dto.ChatDTO, error) + chat func(ctx context.Context, id uuid.UUID) (dto.ChatDTO, error) + chats func(ctx context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.ChatDTO, error) + createMessage func(ctx context.Context, message dto.MessageDTO) (dto.MessageDTO, error) + messages func(ctx context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.MessageDTO, error) +} + +// CreateUser delegates to the arranged behavior and rejects unexpected storage access. +func (repo *storeStub) CreateUser(ctx context.Context, user dto.UserDTO) (dto.UserDTO, error) { + repo.t.Helper() + if repo.createUser == nil { + repo.t.Fatalf("unexpected store.CreateUser call") + return dto.UserDTO{}, nil + } + return repo.createUser(ctx, user) +} + +// User delegates to the arranged behavior and rejects unexpected storage access. +func (repo *storeStub) User(ctx context.Context, id uuid.UUID) (dto.UserDTO, error) { + repo.t.Helper() + if repo.user == nil { + repo.t.Fatalf("unexpected store.User call") + return dto.UserDTO{}, nil + } + return repo.user(ctx, id) +} + +// UsersExist delegates to the arranged behavior and rejects unexpected storage access. +func (repo *storeStub) UsersExist(ctx context.Context, ids []uuid.UUID) (bool, error) { + repo.t.Helper() + if repo.usersExist == nil { + repo.t.Fatalf("unexpected store.UsersExist call") + return false, nil + } + return repo.usersExist(ctx, ids) +} + +// CreateChat delegates to the arranged behavior and rejects unexpected storage access. +func (repo *storeStub) CreateChat(ctx context.Context, chat dto.ChatDTO) (dto.ChatDTO, error) { + repo.t.Helper() + if repo.createChat == nil { + repo.t.Fatalf("unexpected store.CreateChat call") + return dto.ChatDTO{}, nil + } + return repo.createChat(ctx, chat) +} + +// Chat delegates to the arranged behavior and rejects unexpected storage access. +func (repo *storeStub) Chat(ctx context.Context, id uuid.UUID) (dto.ChatDTO, error) { + repo.t.Helper() + if repo.chat == nil { + repo.t.Fatalf("unexpected store.Chat call") + return dto.ChatDTO{}, nil + } + return repo.chat(ctx, id) +} + +// Chats delegates to the arranged behavior and rejects unexpected storage access. +func (repo *storeStub) Chats(ctx context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.ChatDTO, error) { + repo.t.Helper() + if repo.chats == nil { + repo.t.Fatalf("unexpected store.Chats call") + return nil, nil + } + return repo.chats(ctx, id, position, limit) +} + +// CreateMessage delegates to the arranged behavior and rejects unexpected storage access. +func (repo *storeStub) CreateMessage(ctx context.Context, message dto.MessageDTO) (dto.MessageDTO, error) { + repo.t.Helper() + if repo.createMessage == nil { + repo.t.Fatalf("unexpected store.CreateMessage call") + return dto.MessageDTO{}, nil + } + return repo.createMessage(ctx, message) +} + +// Messages delegates to the arranged behavior and rejects unexpected storage access. +func (repo *storeStub) Messages(ctx context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.MessageDTO, error) { + repo.t.Helper() + if repo.messages == nil { + repo.t.Fatalf("unexpected store.Messages call") + return nil, nil + } + return repo.messages(ctx, id, position, limit) +} + +// newStoreStub creates a repository that fails on every unarranged interaction. +func newStoreStub(t *testing.T) *storeStub { + t.Helper() + return &storeStub{t: t} +} + +// newStubService composes the service with controlled storage and a real cursor codec. +func newStubService(t *testing.T, repo *storeStub) *Service { + t.Helper() + codec, err := cursor.New() + if err != nil { + t.Fatal(err) + } + svc, err := New(repo, codec) + if err != nil { + t.Fatal(err) + } + return svc +} + +// allowIdentity permits exactly the arranged actor's existence lookup, never a profile read. +func allowIdentity(repo *storeStub, actor uuid.UUID) { + repo.t.Helper() + repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) { + if len(ids) != 1 || ids[0] != actor { + repo.t.Fatalf("unexpected identity query: %v", ids) + } + return true, nil + } +} + +// allowChatAccess supplies a typed chat after checking the lookup key at the service boundary. +func allowChatAccess(repo *storeStub, actor uuid.UUID, chat dto.ChatDTO) { + repo.t.Helper() + allowIdentity(repo, actor) + repo.chat = func(_ context.Context, id uuid.UUID) (dto.ChatDTO, error) { + if id != chat.ID { + repo.t.Fatalf("chat queried for %s, want %s", id, chat.ID) + } + return chat, nil + } +} diff --git a/internal/service/production/members.go b/internal/service/production/members.go new file mode 100644 index 0000000..ec273d0 --- /dev/null +++ b/internal/service/production/members.go @@ -0,0 +1,42 @@ +package production + +import ( + "context" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// parseMembers validates unique member identifiers and requires the caller to be included. +func parseMembers(rawIDs []string, caller uuid.UUID) ([]uuid.UUID, error) { + if len(rawIDs) == 0 || len(rawIDs) > maxChatMembers { + return nil, service.ErrInvalid + } + members := make([]uuid.UUID, 0, len(rawIDs)) + seen := make(map[uuid.UUID]bool, len(rawIDs)) + for _, rawID := range rawIDs { + id, err := parseID(rawID) + if err != nil || seen[id] { + return nil, service.ErrInvalid + } + seen[id] = true + members = append(members, id) + } + if !seen[caller] { + return nil, service.ErrInvalid + } + return members, nil +} + +// requireExistingMembers validates the complete membership set with one storage operation. +func (s *Service) requireExistingMembers(ctx context.Context, members []uuid.UUID) error { + exists, err := s.repo.UsersExist(ctx, members) + if err != nil { + return normalizeStoreError(err) + } + if !exists { + return service.ErrInvalid + } + return nil +} diff --git a/internal/service/production/messages.go b/internal/service/production/messages.go new file mode 100644 index 0000000..5f74e3c --- /dev/null +++ b/internal/service/production/messages.go @@ -0,0 +1,38 @@ +package production + +import ( + "context" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// MessagesCreate authorizes the sender, validates the message and stores it atomically. +func (s *Service) MessagesCreate(ctx context.Context, request dto.MessagesCreateRequestDTO) (dto.MessageDTO, error) { + caller, chat, err := s.requireChatMember(ctx, request.UserID, request.ChatID) + if err != nil { + return dto.MessageDTO{}, err + } + message, err := prepareMessage(request, caller, chat.ID) + if err != nil { + return dto.MessageDTO{}, err + } + created, err := s.repo.CreateMessage(ctx, message) + return created, normalizeStoreError(err) +} + +// MessagesGetPage authorizes access, loads history and prepares its continuation. +func (s *Service) MessagesGetPage(ctx context.Context, request dto.PageRequestDTO) (dto.MessagePageDTO, error) { + _, chat, err := s.requireChatMember(ctx, request.UserID, request.ChatID) + if err != nil { + return dto.MessagePageDTO{}, err + } + page, err := s.parsePage(request, messageHistoryResource(chat.ID)) + if err != nil { + return dto.MessagePageDTO{}, err + } + items, err := s.repo.Messages(ctx, chat.ID, page.position, page.fetchLimit()) + if err != nil { + return dto.MessagePageDTO{}, normalizeStoreError(err) + } + return s.messagePage(items, page) +} diff --git a/internal/service/production/messages_page_test.go b/internal/service/production/messages_page_test.go new file mode 100644 index 0000000..2e7303d --- /dev/null +++ b/internal/service/production/messages_page_test.go @@ -0,0 +1,231 @@ +package production + +import ( + "context" + "errors" + "reflect" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// TestService_MessagesGetPage validates access and pagination before issuing a typed store query. +func TestService_MessagesGetPage(t *testing.T) { + t.Run("Positive: continuation uses scope, position and lookahead", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}} + allowChatAccess(repo, actor, chat) + + previous := cursor.Position{ID: uuid.New(), CreatedAt: time.Unix(1700000000, 0).UTC()} + records := []dto.MessageDTO{ + {ID: uuid.New(), CreatedAt: time.Unix(1700000001, 0).UTC()}, + {ID: uuid.New(), CreatedAt: time.Unix(1700000002, 0).UTC()}, + {ID: uuid.New(), CreatedAt: time.Unix(1700000003, 0).UTC()}, + } + calls := 0 + repo.messages = func(_ context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.MessageDTO, error) { + calls++ + expected := dto.PositionDTO{ID: previous.ID, CreatedAt: previous.CreatedAt} + if id != chat.ID || position != expected || limit != 3 { + t.Fatalf("wrong page query: id=%s, position=%v, limit=%d", id, position, limit) + } + return records, nil + } + svc := newStubService(t, repo) + resource := "messages:" + chat.ID.String() + token, err := svc.cursors.Encode(resource, previous) + if err != nil { + t.Fatal(err) + } + input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String(), Limit: "2", Cursor: token} + + // Act. + got, err := svc.MessagesGetPage(t.Context(), input) + + // Assert. + + if err != nil { + t.Fatal(err) + } + if calls != 1 || !reflect.DeepEqual(got.Items, records[:2]) || got.NextCursor == nil { + t.Fatalf("calls=%d, page=%+v", calls, got) + } + position, err := svc.cursors.Decode(*got.NextCursor, resource) + expected := cursor.Position{ID: records[1].ID, CreatedAt: records[1].CreatedAt} + if err != nil || position != expected { + t.Fatalf("next position=%v, want=%v, error=%v", position, expected, err) + } + + }) + + t.Run("Positive: empty history uses default limit and no continuation", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}} + allowChatAccess(repo, actor, chat) + + calls := 0 + repo.messages = func(_ context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.MessageDTO, error) { + calls++ + if id != chat.ID || position != (dto.PositionDTO{}) || limit != 51 { + t.Fatalf("wrong initial page query: id=%s, position=%v, limit=%d", id, position, limit) + } + return []dto.MessageDTO{}, nil + } + svc := newStubService(t, repo) + input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String()} + + // Act. + got, err := svc.MessagesGetPage(t.Context(), input) + + // Assert. + if err != nil { + t.Fatal(err) + } + if calls != 1 || got.Items == nil || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("calls=%d, page=%+v", calls, got) + } + }) + + t.Run("Negative: cursor for another resource skips page lookup", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}} + allowChatAccess(repo, actor, chat) + + svc := newStubService(t, repo) + token, err := svc.cursors.Encode("another-resource", cursor.Position{ID: uuid.New(), CreatedAt: time.Unix(1700000000, 0).UTC()}) + if err != nil { + t.Fatal(err) + } + input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String(), Cursor: token} + + // Act. + got, err := svc.MessagesGetPage(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrInvalid) || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("page=%+v, error=%v", got, err) + } + }) + + t.Run("Negative: codec failure skips page lookup", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}} + allowChatAccess(repo, actor, chat) + + failure := errors.New("cursor unavailable") + svc := newStubService(t, repo) + svc.cursors = failingCursor{failure: failure} + input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String(), Cursor: "token"} + + // Act. + got, err := svc.MessagesGetPage(t.Context(), input) + + // Assert. + if !errors.Is(err, failure) || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("page=%+v, error=%v", got, err) + } + }) + + t.Run("Negative: page storage fails", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}} + allowChatAccess(repo, actor, chat) + + failure := errors.New("page unavailable") + repo.messages = func(_ context.Context, _ uuid.UUID, _ dto.PositionDTO, _ int) ([]dto.MessageDTO, error) { + return nil, failure + } + svc := newStubService(t, repo) + input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String()} + + // Act. + got, err := svc.MessagesGetPage(t.Context(), input) + + // Assert. + if !errors.Is(err, failure) || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("page=%+v, error=%v", got, err) + } + }) + + t.Run("Negative: malformed identity skips all storage", func(t *testing.T) { + // Arrange. + svc := newStubService(t, newStoreStub(t)) + input := dto.PageRequestDTO{UserID: "invalid", ChatID: uuid.NewString()} + + // Act. + got, err := svc.MessagesGetPage(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrUnauthorized) || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("page=%+v, error=%v", got, err) + } + }) + + t.Run("Negative: outsider cannot read message history", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{uuid.New()}} + allowChatAccess(repo, actor, chat) + svc := newStubService(t, repo) + input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String()} + + // Act. + got, err := svc.MessagesGetPage(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrNotFound) || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("page=%+v, error=%v", got, err) + } + }) +} + +// TestService_MessagesGetPage_InvalidLimit rejects invalid limits before reading stored pages. +func TestService_MessagesGetPage_InvalidLimit(t *testing.T) { + cases := []struct { + name string + limit string + }{ + {"Negative: zero limit", "0"}, + {"Negative: negative limit", "-1"}, + {"Negative: limit above maximum", "101"}, + {"Negative: nonnumeric limit", "text"}, + {"Negative: fractional limit", "1.5"}, + {"Negative: integer overflow", "99999999999999999999999999"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}} + allowChatAccess(repo, actor, chat) + + svc := newStubService(t, repo) + input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String(), Limit: tc.limit} + + // Act. + got, err := svc.MessagesGetPage(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrInvalid) || len(got.Items) != 0 || got.NextCursor != nil { + t.Fatalf("page=%+v, error=%v", got, err) + } + }) + } +} diff --git a/internal/service/production/messages_test.go b/internal/service/production/messages_test.go new file mode 100644 index 0000000..cb053e1 --- /dev/null +++ b/internal/service/production/messages_test.go @@ -0,0 +1,170 @@ +package production + +import ( + "context" + "errors" + "reflect" + "strings" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// TestService_MessagesCreate authorizes the sender before validating and persisting the message. +func TestService_MessagesCreate(t *testing.T) { + t.Run("Positive: authenticated sender and original text persisted", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}} + allowChatAccess(repo, actor, chat) + var persisted dto.MessageDTO + repo.createMessage = func(_ context.Context, input dto.MessageDTO) (dto.MessageDTO, error) { + persisted = input + input.CreatedAt = time.Unix(1700000000, 0).UTC() + return input, nil + } + svc := newStubService(t, repo) + input := dto.MessagesCreateRequestDTO{IdempotencyKey: "fixture-key", UserID: actor.String(), ChatID: chat.ID.String(), Text: " Original 👋 "} + + // Act. + got, err := svc.MessagesCreate(t.Context(), input) + + // Assert. + if err != nil { + t.Fatal(err) + } + if persisted.ID == uuid.Nil || persisted.SenderID != actor || persisted.ChatID != chat.ID || persisted.Text != input.Text { + t.Fatalf("wrong persistence input: %+v", persisted) + } + if got.ID != persisted.ID || got.SenderID != actor || got.ChatID != chat.ID || got.Text != input.Text || got.CreatedAt.IsZero() { + t.Fatalf("wrong acknowledgement: %+v", got) + } + }) + + t.Run("Negative: outsider cannot persist messages", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{uuid.New()}} + allowChatAccess(repo, actor, chat) + svc := newStubService(t, repo) + input := dto.MessagesCreateRequestDTO{IdempotencyKey: "fixture-key", UserID: actor.String(), ChatID: chat.ID.String(), Text: "Valid text"} + + // Act. + got, err := svc.MessagesCreate(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrNotFound) || !reflect.DeepEqual(got, dto.MessageDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + }) + + t.Run("Negative: malformed identity skips storage", func(t *testing.T) { + // Arrange. + svc := newStubService(t, newStoreStub(t)) + input := dto.MessagesCreateRequestDTO{IdempotencyKey: "fixture-key", UserID: "invalid", ChatID: uuid.NewString(), Text: "text"} + + // Act. + got, err := svc.MessagesCreate(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrUnauthorized) || !reflect.DeepEqual(got, dto.MessageDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + }) +} + +// TestService_MessagesCreate_InvalidText refuses invalid text without attempting persistence. +func TestService_MessagesCreate_InvalidText(t *testing.T) { + cases := []struct { + name string + text string + }{ + {"Negative: empty text", ""}, + {"Negative: whitespace only", " \t"}, + {"Negative: rune limit exceeded", strings.Repeat("界", 4001)}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}} + allowChatAccess(repo, actor, chat) + svc := newStubService(t, repo) + input := dto.MessagesCreateRequestDTO{IdempotencyKey: "fixture-key", UserID: actor.String(), ChatID: chat.ID.String(), Text: tc.text} + + // Act. + got, err := svc.MessagesCreate(t.Context(), input) + + // Assert. + if !errors.Is(err, service.ErrInvalid) || !reflect.DeepEqual(got, dto.MessageDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + }) + } +} + +// TestService_MessagesCreate_StoreErrors propagates persistence failures without acknowledging a record. +func TestService_MessagesCreate_StoreErrors(t *testing.T) { + failure := errors.New("write unavailable") + cases := []struct { + name string + cause error + want error + }{ + {"Negative: capacity exhausted", store.ErrCapacity, service.ErrCapacity}, + {"Negative: chat disappeared", store.ErrNotFound, service.ErrNotFound}, + {"Negative: unexpected write failure", failure, failure}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + actor := uuid.New() + chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}} + allowChatAccess(repo, actor, chat) + repo.createMessage = func(_ context.Context, _ dto.MessageDTO) (dto.MessageDTO, error) { + return dto.MessageDTO{}, tc.cause + } + svc := newStubService(t, repo) + input := dto.MessagesCreateRequestDTO{IdempotencyKey: "fixture-key", UserID: actor.String(), ChatID: chat.ID.String(), Text: "Valid text"} + + // Act. + got, err := svc.MessagesCreate(t.Context(), input) + + // Assert. + if !errors.Is(err, tc.want) || !reflect.DeepEqual(got, dto.MessageDTO{}) { + t.Fatalf("result=%+v, error=%v, want=%v", got, err, tc.want) + } + }) + } +} + +// TestIdempotencyKeyValidation checks the exact contract alphabet and length independently. +func TestIdempotencyKeyValidation(t *testing.T) { + for _, test := range []struct { + name string + key string + want bool + }{ + {name: "missing", key: "", want: false}, + {name: "allowed alphabet", key: "aZ09._:-", want: true}, + {name: "space", key: "a b", want: false}, + {name: "unicode", key: "ключ", want: false}, + {name: "maximum", key: strings.Repeat("a", 128), want: true}, + {name: "too long", key: strings.Repeat("a", 129), want: false}, + } { + t.Run(test.name, func(t *testing.T) { + if got := validIdempotencyKey(test.key); got != test.want { + t.Fatalf("valid=%v want=%v", got, test.want) + } + }) + } +} diff --git a/internal/service/production/pages.go b/internal/service/production/pages.go new file mode 100644 index 0000000..a550b3b --- /dev/null +++ b/internal/service/production/pages.go @@ -0,0 +1,49 @@ +package production + +import ( + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// chatPage adapts the shared page result to the chat response DTO. +func (s *Service) chatPage(items []dto.ChatDTO, page pagination) (dto.ChatPageDTO, error) { + items, next, err := buildPage(items, page, s.cursors, chatPosition) + if err != nil { + return dto.ChatPageDTO{}, err + } + return dto.ChatPageDTO{Items: items, NextCursor: next}, nil +} + +// messagePage adapts the shared page result to the message response DTO. +func (s *Service) messagePage(items []dto.MessageDTO, page pagination) (dto.MessagePageDTO, error) { + items, next, err := buildPage(items, page, s.cursors, messagePosition) + if err != nil { + return dto.MessagePageDTO{}, err + } + return dto.MessagePageDTO{Items: items, NextCursor: next}, nil +} + +// buildPage trims lookahead and signs the last returned record when more data exists. +// The page limit must be validated before calling this helper. +func buildPage[T any](items []T, page pagination, codec service.CursorCodec, position func(T) cursor.Position) ([]T, *string, error) { + if len(items) <= page.limit { + return items, nil, nil + } + items = items[:page.limit] + next, err := codec.Encode(page.resource, position(items[len(items)-1])) + if err != nil { + return nil, nil, err + } + return items, new(next), nil +} + +// chatPosition extracts the stable pagination boundary from a stored chat. +func chatPosition(chat dto.ChatDTO) cursor.Position { + return cursor.Position{CreatedAt: chat.CreatedAt, ID: chat.ID} +} + +// messagePosition extracts the stable pagination boundary from a stored message. +func messagePosition(message dto.MessageDTO) cursor.Position { + return cursor.Position{CreatedAt: message.CreatedAt, ID: message.ID} +} diff --git a/internal/service/production/pages_test.go b/internal/service/production/pages_test.go new file mode 100644 index 0000000..6e1d35a --- /dev/null +++ b/internal/service/production/pages_test.go @@ -0,0 +1,53 @@ +package production + +import ( + "reflect" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// TestService_messagePage checks boundaries and signs the last returned item, not lookahead. +func TestService_messagePage(t *testing.T) { + for _, count := range []int{0, 2, 3} { + names := map[int]string{0: "Positive: empty page", 2: "Positive: exact limit is final", 3: "Positive: lookahead creates continuation"} + t.Run(names[count], func(t *testing.T) { + // Arrange. + svc := newStubService(t, newStoreStub(t)) + items := make([]dto.MessageDTO, count) + for i := range items { + items[i] = dto.MessageDTO{ID: uuid.New(), Text: "message", CreatedAt: time.Unix(int64(i+1), 0).UTC()} + } + expected := items[:min(count, 2)] + + // Act. + page, err := svc.messagePage(items, pagination{limit: 2, resource: "messages:chat"}) + + // Assert. + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(page.Items, expected) { + t.Fatalf("page=%v, want=%v", page.Items, expected) + } + if count <= 2 { + if page.NextCursor != nil { + t.Fatal("final page has a continuation") + } + return + } + if page.NextCursor == nil { + t.Fatal("lookahead did not produce continuation") + } + decoded, err := svc.cursors.Decode(*page.NextCursor, "messages:chat") + want := cursor.Position{ID: items[1].ID, CreatedAt: items[1].CreatedAt} + if err != nil || decoded != want { + t.Fatalf("cursor=%v, want=%v, error=%v", decoded, want, err) + } + }) + } +} diff --git a/internal/service/production/pagination.go b/internal/service/production/pagination.go new file mode 100644 index 0000000..f72eb3b --- /dev/null +++ b/internal/service/production/pagination.go @@ -0,0 +1,78 @@ +package production + +import ( + "errors" + "strconv" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +const ( + defaultPageLimit = 50 + maxPageLimit = 100 + // Fetching one extra record determines whether to issue a next cursor. + pageLookahead = 1 +) + +type pagination struct { + position dto.PositionDTO + limit int + resource string +} + +// parsePage validates the page size and resource-bound cursor. +func (s *Service) parsePage(request dto.PageRequestDTO, resource string) (pagination, error) { + limit, err := parseLimit(request.Limit) + if err != nil { + return pagination{}, err + } + position, err := s.cursors.Decode(request.Cursor, resource) + if errors.Is(err, cursor.ErrInvalid) { + return pagination{}, service.ErrInvalid + } + if err != nil { + return pagination{}, err + } + return pagination{ + position: dto.PositionDTO{CreatedAt: position.CreatedAt, ID: position.ID}, + limit: limit, + resource: resource, + }, nil +} + +// parseLimit parses a bounded page size or supplies the default. +func parseLimit(raw string) (int, error) { + if raw == "" { + return defaultPageLimit, nil + } + for _, char := range raw { + if char < '0' || char > '9' { + return 0, service.ErrInvalid + } + } + limit, err := strconv.Atoi(raw) + if err != nil || limit < 1 || limit > maxPageLimit { + return 0, service.ErrInvalid + } + return limit, nil +} + +// fetchLimit includes one lookahead record to detect a following page. +func (page pagination) fetchLimit() int { + return page.limit + pageLookahead +} + +// chatListResource scopes a cursor to one user's visible chats. +func chatListResource(userID uuid.UUID) string { + return "chats:" + userID.String() +} + +// messageHistoryResource scopes a cursor to one chat's message history. +func messageHistoryResource(chatID uuid.UUID) string { + return "messages:" + chatID.String() +} diff --git a/internal/service/production/pagination_test.go b/internal/service/production/pagination_test.go new file mode 100644 index 0000000..2f81a5e --- /dev/null +++ b/internal/service/production/pagination_test.go @@ -0,0 +1,74 @@ +package production + +import ( + "errors" + "testing" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +type failingCursor struct { + failure error +} + +// Encode injects a signing failure at the service dependency boundary. +func (codec failingCursor) Encode(_ string, _ cursor.Position) (string, error) { + return "", codec.failure +} + +// Decode injects a verification failure at the service dependency boundary. +func (codec failingCursor) Decode(_, _ string) (cursor.Position, error) { + return cursor.Position{}, codec.failure +} + +// TestService_parsePage translates invalid tokens while retaining unexpected dependency failures. +func TestService_parsePage(t *testing.T) { + failure := errors.New("codec unavailable") + cases := []struct { + name string + cause error + want error + }{ + {"Negative: invalid token", cursor.ErrInvalid, service.ErrInvalid}, + {"Negative: unavailable codec", failure, failure}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + svc := &Service{cursors: failingCursor{failure: tc.cause}} + input := dto.PageRequestDTO{Cursor: "supplied-token"} + + // Act. + page, err := svc.parsePage(input, "messages:chat") + + // Assert. + if !errors.Is(err, tc.want) { + t.Fatalf("error=%v, want=%v", err, tc.want) + } + if page != (pagination{}) { + t.Fatalf("failed cursor produced a page: %+v", page) + } + }) + } +} + +// TestService_messagePage_EncodeFailure returns no partial page when signing fails. +func TestService_messagePage_EncodeFailure(t *testing.T) { + // Arrange. + failure := errors.New("signing unavailable") + svc := &Service{cursors: failingCursor{failure: failure}} + items := []dto.MessageDTO{{Text: "first"}, {Text: "lookahead"}} + + // Act. + page, err := svc.messagePage(items, pagination{limit: 1, resource: "messages:chat"}) + + // Assert. + if !errors.Is(err, failure) { + t.Fatalf("lost signing failure: %v", err) + } + if page.Items != nil || page.NextCursor != nil { + t.Fatalf("partial page escaped: %+v", page) + } +} diff --git a/internal/service/production/service.go b/internal/service/production/service.go new file mode 100644 index 0000000..359ef06 --- /dev/null +++ b/internal/service/production/service.go @@ -0,0 +1,39 @@ +// Package production implements the messenger application operations. +package production + +import ( + "fmt" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// Service orchestrates operations through storage and cursor interfaces. +type Service struct { + repo store.Interface + cursors service.CursorCodec + cache service.ProfileCache +} + +var _ service.Service = (*Service)(nil) + +// New creates an application service using explicitly supplied dependencies. +func New(repo store.Interface, cursors service.CursorCodec) (*Service, error) { + if repo == nil || cursors == nil { + return nil, fmt.Errorf("store and cursor codec are required") + } + return &Service{repo: repo, cursors: cursors}, nil +} + +// NewCached builds the business service with optional immutable-profile caching. +func NewCached(repo store.Interface, cursors service.CursorCodec, cache service.ProfileCache) (*Service, error) { + instance, err := New(repo, cursors) + if err != nil { + return nil, err + } + if cache == nil { + return nil, fmt.Errorf("profile cache is required") + } + instance.cache = cache + return instance, nil +} diff --git a/internal/service/production/service_test.go b/internal/service/production/service_test.go new file mode 100644 index 0000000..159d0d9 --- /dev/null +++ b/internal/service/production/service_test.go @@ -0,0 +1,39 @@ +package production + +import ( + "testing" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/cursor" +) + +// TestService_New rejects each missing dependency separately. +func TestService_New(t *testing.T) { + t.Run("Negative: missing store", func(t *testing.T) { + // Arrange. + codec, err := cursor.New() + if err != nil { + t.Fatal(err) + } + + // Act. + svc, err := New(nil, codec) + + // Assert. + if err == nil || svc != nil { + t.Fatalf("service=%v, error=%v", svc, err) + } + }) + + t.Run("Negative: missing codec", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + + // Act. + svc, err := New(repo, nil) + + // Assert. + if err == nil || svc != nil { + t.Fatalf("service=%v, error=%v", svc, err) + } + }) +} diff --git a/internal/service/production/users.go b/internal/service/production/users.go new file mode 100644 index 0000000..714eba4 --- /dev/null +++ b/internal/service/production/users.go @@ -0,0 +1,47 @@ +package production + +import ( + "context" + "errors" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// UsersCreate validates registration and atomically reserves the unique username. +func (s *Service) UsersCreate(ctx context.Context, request dto.UsersCreateRequestDTO) (dto.UserDTO, error) { + user, err := PrepareUser(request) + if err != nil { + return dto.UserDTO{}, err + } + created, err := s.repo.CreateUser(ctx, user) + return created, normalizeStoreError(err) +} + +// UsersGetMe loads the current user's profile; malformed and missing identities are unauthorized. +func (s *Service) UsersGetMe(ctx context.Context, rawID string) (dto.UserDTO, error) { + id, err := parseIdentity(rawID) + if err != nil { + return dto.UserDTO{}, err + } + if s.cache != nil { + if cached, ok := s.cache.GetProfile(ctx, id); ok { + return cached, nil + } + } + user, err := s.repo.User(ctx, id) + if errors.Is(err, store.ErrNotFound) { + return dto.UserDTO{}, service.ErrUnauthorized + } + if err == nil && s.cache != nil { + s.cache.PutProfile(ctx, user) + } + return user, normalizeStoreError(err) +} + +// UsersCheckIdentity validates the current identity without loading its profile. +func (s *Service) UsersCheckIdentity(ctx context.Context, rawID string) error { + _, err := s.requireUserID(ctx, rawID) + return err +} diff --git a/internal/service/production/users_test.go b/internal/service/production/users_test.go new file mode 100644 index 0000000..e39dc45 --- /dev/null +++ b/internal/service/production/users_test.go @@ -0,0 +1,289 @@ +package production + +import ( + "context" + "errors" + "slices" + "strings" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// TestService_UsersCreate validates registration before persistence and preserves storage failures. +func TestService_UsersCreate(t *testing.T) { + t.Run("Positive: validates and persists the original profile", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + expected := dto.UserDTO{Username: "alice", DisplayName: "Алиса 👋", CreatedAt: time.Unix(1700000000, 0).UTC()} + calls := 0 + repo.createUser = func(_ context.Context, input dto.UserDTO) (dto.UserDTO, error) { + calls++ + if input.ID == uuid.Nil || input.Username != expected.Username || input.DisplayName != expected.DisplayName { + t.Fatalf("wrong persistence input: %+v", input) + } + expected.ID = input.ID + return expected, nil + } + svc := newStubService(t, repo) + input := dto.UsersCreateRequestDTO{Username: expected.Username, DisplayName: expected.DisplayName} + + // Act. + got, err := svc.UsersCreate(t.Context(), input) + + // Assert. + if err != nil { + t.Fatal(err) + } + if calls != 1 || got != expected { + t.Fatalf("calls=%d, got=%+v, want=%+v", calls, got, expected) + } + }) +} + +// TestService_UsersCreate_InvalidInput refuses malformed registrations without calling storage. +func TestService_UsersCreate_InvalidInput(t *testing.T) { + cases := []struct { + name string + input dto.UsersCreateRequestDTO + }{ + {"Negative: short username", dto.UsersCreateRequestDTO{Username: "a", DisplayName: "Alice"}}, + {"Negative: uppercase username", dto.UsersCreateRequestDTO{Username: "Alice", DisplayName: "Alice"}}, + {"Negative: blank display name", dto.UsersCreateRequestDTO{Username: "alice", DisplayName: " "}}, + {"Negative: display name exceeds rune limit", dto.UsersCreateRequestDTO{Username: "alice", DisplayName: strings.Repeat("界", 101)}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange: every store method fails immediately if called. + svc := newStubService(t, newStoreStub(t)) + + // Act. + got, err := svc.UsersCreate(t.Context(), tc.input) + + // Assert. + if !errors.Is(err, service.ErrInvalid) || got != (dto.UserDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + }) + } +} + +// TestService_UsersCreate_StoreErrors preserves the application meaning of persistence failures. +func TestService_UsersCreate_StoreErrors(t *testing.T) { + unexpected := errors.New("storage unavailable") + cases := []struct { + name string + cause error + want error + }{ + {"Negative: duplicate username", store.ErrConflict, service.ErrConflict}, + {"Negative: capacity exhausted", store.ErrCapacity, service.ErrCapacity}, + {"Negative: unexpected failure", unexpected, unexpected}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + repo.createUser = func(_ context.Context, _ dto.UserDTO) (dto.UserDTO, error) { + return dto.UserDTO{}, tc.cause + } + svc := newStubService(t, repo) + + // Act. + got, err := svc.UsersCreate(t.Context(), dto.UsersCreateRequestDTO{Username: "alice", DisplayName: "Alice"}) + + // Assert. + if !errors.Is(err, tc.want) || got != (dto.UserDTO{}) { + t.Fatalf("result=%+v, error=%v, want=%v", got, err, tc.want) + } + }) + } +} + +// TestService_UsersGetMe loads only the requested profile and hides unknown identities. +func TestService_UsersGetMe(t *testing.T) { + t.Run("Positive: returns stored profile", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + expected := dto.UserDTO{ID: uuid.New(), Username: "alice", DisplayName: "Alice", CreatedAt: time.Unix(1700000000, 0).UTC()} + calls := 0 + repo.user = func(_ context.Context, id uuid.UUID) (dto.UserDTO, error) { + calls++ + if id != expected.ID { + t.Fatalf("profile queried for %s instead of %s", id, expected.ID) + } + return expected, nil + } + svc := newStubService(t, repo) + + // Act. + got, err := svc.UsersGetMe(t.Context(), expected.ID.String()) + + // Assert. + if err != nil { + t.Fatal(err) + } + if calls != 1 || got != expected { + t.Fatalf("calls=%d, got=%+v, want=%+v", calls, got, expected) + } + }) + + t.Run("Negative: unknown identity", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + repo.user = func(_ context.Context, _ uuid.UUID) (dto.UserDTO, error) { + return dto.UserDTO{}, store.ErrNotFound + } + svc := newStubService(t, repo) + wantErr := service.ErrUnauthorized + + // Act. + got, err := svc.UsersGetMe(t.Context(), uuid.NewString()) + + // Assert. + if !errors.Is(err, wantErr) { + t.Fatalf("error=%v, want=%v", err, wantErr) + } + if got != (dto.UserDTO{}) { + t.Fatalf("failed operation returned a profile: %+v", got) + } + }) + + t.Run("Negative: storage failure preserved", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + wantErr := errors.New("profile unavailable") + repo.user = func(_ context.Context, _ uuid.UUID) (dto.UserDTO, error) { + return dto.UserDTO{}, wantErr + } + svc := newStubService(t, repo) + + // Act. + got, err := svc.UsersGetMe(t.Context(), uuid.NewString()) + + // Assert. + if !errors.Is(err, wantErr) { + t.Fatalf("error=%v, want=%v", err, wantErr) + } + if got != (dto.UserDTO{}) { + t.Fatalf("failed operation returned a profile: %+v", got) + } + }) +} + +// TestService_UsersGetMe_InvalidIdentity rejects malformed identifiers before any store call. +func TestService_UsersGetMe_InvalidIdentity(t *testing.T) { + cases := []struct { + name string + raw string + }{ + {"Negative: missing header", ""}, + {"Negative: short ID", "invalid"}, + {"Negative: non-UUID characters", strings.Repeat("z", 36)}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + svc := newStubService(t, newStoreStub(t)) + + // Act. + got, err := svc.UsersGetMe(t.Context(), tc.raw) + + // Assert. + if !errors.Is(err, service.ErrUnauthorized) || got != (dto.UserDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + }) + } +} + +// TestService_UsersCheckIdentity uses the existence query and never loads a profile. +func TestService_UsersCheckIdentity(t *testing.T) { + t.Run("Positive: existence check without profile loading", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + id := uuid.New() + calls := 0 + repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) { + calls++ + if !slices.Equal(ids, []uuid.UUID{id}) { + t.Fatalf("queried IDs=%v", ids) + } + return true, nil + } + svc := newStubService(t, repo) + + // Act. + err := svc.UsersCheckIdentity(t.Context(), id.String()) + + // Assert. + if !errors.Is(err, nil) || calls != 1 { + t.Fatalf("calls=%d, error=%v", calls, err) + } + }) + + t.Run("Negative: identity does not exist", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + id := uuid.New() + calls := 0 + repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) { + calls++ + if !slices.Equal(ids, []uuid.UUID{id}) { + t.Fatalf("queried IDs=%v", ids) + } + return false, nil + } + svc := newStubService(t, repo) + + // Act. + err := svc.UsersCheckIdentity(t.Context(), id.String()) + + // Assert. + if !errors.Is(err, service.ErrUnauthorized) || calls != 1 { + t.Fatalf("calls=%d, error=%v", calls, err) + } + }) + + t.Run("Negative: existence lookup fails", func(t *testing.T) { + // Arrange. + repo := newStoreStub(t) + id := uuid.New() + calls := 0 + failure := errors.New("identity unavailable") + repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) { + calls++ + if !slices.Equal(ids, []uuid.UUID{id}) { + t.Fatalf("queried IDs=%v", ids) + } + return false, failure + } + svc := newStubService(t, repo) + + // Act. + err := svc.UsersCheckIdentity(t.Context(), id.String()) + + // Assert. + if !errors.Is(err, failure) || calls != 1 { + t.Fatalf("calls=%d, error=%v", calls, err) + } + }) + + t.Run("Negative: malformed identity skips storage", func(t *testing.T) { + // Arrange. + svc := newStubService(t, newStoreStub(t)) + + // Act. + err := svc.UsersCheckIdentity(t.Context(), "invalid") + + // Assert. + if !errors.Is(err, service.ErrUnauthorized) { + t.Fatalf("error=%v", err) + } + }) +} diff --git a/internal/service/production/validation.go b/internal/service/production/validation.go new file mode 100644 index 0000000..1ba500c --- /dev/null +++ b/internal/service/production/validation.go @@ -0,0 +1,51 @@ +package production + +import ( + "strings" + "unicode/utf8" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/service" +) + +// These bounds come from the mandatory lab1 OpenAPI schema. +const ( + minUsernameLength = 3 + maxUsernameLength = 32 + maxDisplayNameRunes = 100 + maxChatTitleRunes = 100 + maxChatMembers = 100 + maxMessageRunes = 4000 + canonicalUUIDLength = len("00000000-0000-0000-0000-000000000000") +) + +// validText checks that text is nonblank and within its Unicode length limit. +func validText(value string, maxRunes int) bool { + return utf8.ValidString(value) && strings.TrimSpace(value) != "" && utf8.RuneCountInString(value) <= maxRunes +} + +// validUsername checks the username length and permitted ASCII characters. +func validUsername(value string) bool { + if len(value) < minUsernameLength || len(value) > maxUsernameLength { + return false + } + for _, char := range value { + if (char < 'a' || char > 'z') && (char < '0' || char > '9') && char != '_' { + return false + } + } + return true +} + +// parseID parses a canonical UUID or returns a validation error. +func parseID(value string) (uuid.UUID, error) { + if len(value) != canonicalUUIDLength { + return uuid.Nil, service.ErrInvalid + } + id, err := uuid.Parse(value) + if err != nil { + return uuid.Nil, service.ErrInvalid + } + return id, nil +} diff --git a/internal/store/interface.go b/internal/store/interface.go new file mode 100644 index 0000000..9e2b880 --- /dev/null +++ b/internal/store/interface.go @@ -0,0 +1,54 @@ +// Package store defines typed persistence operations and storage errors. +package store + +import ( + "context" + "errors" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +var ( + // ErrUnavailable means a required storage dependency cannot serve the operation. + ErrUnavailable = errors.New("storage unavailable") + + // ErrNotFound means a record does not exist. + ErrNotFound = errors.New("record not found") + + // ErrConflict means a unique constraint failed. + ErrConflict = errors.New("record already exists") + + // ErrCapacity means the bounded store cannot accept more data. + ErrCapacity = errors.New("storage capacity reached") +) + +// Interface defines persistence operations on validated typed records. +// Implementations own atomicity, unique constraints and storage capacity, but not +// authorization. Callers must validate membership before accessing chat records. +type Interface interface { + // CreateUser atomically enforces username uniqueness and assigns CreatedAt. + CreateUser(ctx context.Context, user dto.UserDTO) (dto.UserDTO, error) + + // User returns a user by ID or ErrNotFound. + User(ctx context.Context, id uuid.UUID) (dto.UserDTO, error) + + // UsersExist reports whether every supplied ID exists; an empty set returns true. + UsersExist(ctx context.Context, ids []uuid.UUID) (bool, error) + + // CreateChat atomically stores a chat and its member indexes, assigning CreatedAt. + CreateChat(ctx context.Context, chat dto.ChatDTO) (dto.ChatDTO, error) + + // Chat returns a defensive copy of the stored chat or ErrNotFound. + Chat(ctx context.Context, id uuid.UUID) (dto.ChatDTO, error) + + // Chats reads up to limit records after position from the user's chat index. + Chats(ctx context.Context, userID uuid.UUID, position dto.PositionDTO, limit int) ([]dto.ChatDTO, error) + + // CreateMessage appends one message atomically and assigns its ordering timestamp. + CreateMessage(ctx context.Context, message dto.MessageDTO) (dto.MessageDTO, error) + + // Messages reads up to limit records strictly after position in one chat. + Messages(ctx context.Context, chatID uuid.UUID, position dto.PositionDTO, limit int) ([]dto.MessageDTO, error) +} diff --git a/internal/store/memory/capacity.go b/internal/store/memory/capacity.go new file mode 100644 index 0000000..46bd05b --- /dev/null +++ b/internal/store/memory/capacity.go @@ -0,0 +1,38 @@ +package memory + +import "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + +// Conservative logical charges, not measurements of Go allocations or RSS. +// The budget includes record metadata and index entries; HTTP buffers are separate. +const ( + userRecordOverheadBytes = 256 + chatRecordOverheadBytes = 256 + messageRecordOverheadBytes = 256 + chatMemberIndexBytes = 128 + usernameIndexCopies = 2 +) + +// userCost calculates the logical storage charge for a user and its index entries. +func userCost(user dto.UserDTO) int { + return userRecordOverheadBytes + len(user.Username)*usernameIndexCopies + len(user.DisplayName) +} + +// chatCost calculates the logical storage charge for a chat and its membership index. +func chatCost(chat dto.ChatDTO) int { + return chatRecordOverheadBytes + len(chat.Title) + chatMemberIndexBytes*len(chat.MemberIDs) +} + +// messageCost calculates the logical storage charge for a message. +func messageCost(message dto.MessageDTO) int { + return messageRecordOverheadBytes + len(message.Text) +} + +// reserve checks and charges capacity while the caller holds the write lock. +// Callers must not perform a fallible operation after reserving this budget. +func (s *Store) reserve(bytes int) bool { + if bytes > s.limits.MaxStateBytes-s.usedBytes { + return false + } + s.usedBytes += bytes + return true +} diff --git a/internal/store/memory/chats.go b/internal/store/memory/chats.go new file mode 100644 index 0000000..9e4fae5 --- /dev/null +++ b/internal/store/memory/chats.go @@ -0,0 +1,66 @@ +package memory + +import ( + "context" + "slices" + "sort" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// copyChat copies the membership slice to keep stored chat state private. +func copyChat(c dto.ChatDTO) dto.ChatDTO { + c.MemberIDs = slices.Clone(c.MemberIDs) + return c +} + +// CreateChat stores the chat and its member index atomically. +func (s *Store) CreateChat(_ context.Context, c dto.ChatDTO) (dto.ChatDTO, error) { + s.mu.Lock() + defer s.mu.Unlock() + for _, id := range c.MemberIDs { + if _, ok := s.users[id]; !ok { + return dto.ChatDTO{}, store.ErrNotFound + } + } + if len(s.chats) >= s.limits.MaxChats || !s.reserve(chatCost(c)) { + return dto.ChatDTO{}, store.ErrCapacity + } + c.CreatedAt = s.timestamp() + c = copyChat(c) + s.chats[c.ID] = c + for _, id := range c.MemberIDs { + s.userChats[id] = append(s.userChats[id], c) + } + return copyChat(c), nil +} + +// Chat retrieves a defensive copy. Authorization belongs to the service. +func (s *Store) Chat(_ context.Context, id uuid.UUID) (dto.ChatDTO, error) { + s.mu.RLock() + defer s.mu.RUnlock() + c, ok := s.chats[id] + if !ok { + return c, store.ErrNotFound + } + return copyChat(c), nil +} + +// Chats uses the per-member index and binary search, without scanning other chats. +func (s *Store) Chats(_ context.Context, user uuid.UUID, p dto.PositionDTO, limit int) ([]dto.ChatDTO, error) { + s.mu.RLock() + defer s.mu.RUnlock() + all := s.userChats[user] + i := sort.Search(len(all), func(i int) bool { + return after(all[i].CreatedAt, all[i].ID, p) + }) + end := min(len(all), i+limit) + out := make([]dto.ChatDTO, 0, end-i) + for _, c := range all[i:end] { + out = append(out, copyChat(c)) + } + return out, nil +} diff --git a/internal/store/memory/chats_test.go b/internal/store/memory/chats_test.go new file mode 100644 index 0000000..b495c37 --- /dev/null +++ b/internal/store/memory/chats_test.go @@ -0,0 +1,148 @@ +package memory + +import ( + "errors" + "reflect" + "testing" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// TestStore_CreateChat_RejectsWithoutMutation checks atomic rejection of invalid or excess chats. +func TestStore_CreateChat_RejectsWithoutMutation(t *testing.T) { + t.Run("Negative: chat capacity reached", func(t *testing.T) { + // Arrange. + cfg := config.Default().Memory + cfg.MaxChats = 1 + repo := newTestStore(t, cfg) + member := seedUser(t, repo, "alice") + original := seedChat(t, repo, member.ID) + input := dto.ChatDTO{ID: uuid.New(), Title: "Rejected", MemberIDs: []uuid.UUID{member.ID}} + wantErr := store.ErrCapacity + + previousBytes := repo.usedBytes + + // Act. + got, err := repo.CreateChat(t.Context(), input) + + // Assert. + if !errors.Is(err, wantErr) || got.ID != uuid.Nil { + t.Fatalf("result=%+v, error=%v, want=%v", got, err, wantErr) + } + items, readErr := repo.Chats(t.Context(), member.ID, dto.PositionDTO{}, 10) + if readErr != nil { + t.Fatal(readErr) + } + if !reflect.DeepEqual(items, []dto.ChatDTO{original}) || len(repo.chats) != 1 || repo.usedBytes != previousBytes { + t.Fatalf("rejected chat changed stored state: %v", items) + } + }) + + t.Run("Negative: member does not exist", func(t *testing.T) { + // Arrange. + cfg := config.Default().Memory + cfg.MaxChats = 1 + repo := newTestStore(t, cfg) + member := seedUser(t, repo, "alice") + original := seedChat(t, repo, member.ID) + input := dto.ChatDTO{ID: uuid.New(), Title: "Rejected", MemberIDs: []uuid.UUID{member.ID}} + wantErr := store.ErrNotFound + input.MemberIDs = append(input.MemberIDs, uuid.New()) + + previousBytes := repo.usedBytes + + // Act. + got, err := repo.CreateChat(t.Context(), input) + + // Assert. + if !errors.Is(err, wantErr) || got.ID != uuid.Nil { + t.Fatalf("result=%+v, error=%v, want=%v", got, err, wantErr) + } + items, readErr := repo.Chats(t.Context(), member.ID, dto.PositionDTO{}, 10) + if readErr != nil { + t.Fatal(readErr) + } + if !reflect.DeepEqual(items, []dto.ChatDTO{original}) || len(repo.chats) != 1 || repo.usedBytes != previousBytes { + t.Fatalf("rejected chat changed stored state: %v", items) + } + }) +} + +// TestStore_CreateChat_DefensiveCopy ensures callers cannot mutate membership through write boundaries. +func TestStore_CreateChat_DefensiveCopy(t *testing.T) { + t.Run("Positive: isolated input membership", func(t *testing.T) { + // Arrange. + repo := newTestStore(t, config.Default().Memory) + member := seedUser(t, repo, "alice") + input := dto.ChatDTO{ID: uuid.New(), Title: "Team", MemberIDs: []uuid.UUID{member.ID}} + _, err := repo.CreateChat(t.Context(), input) + if err != nil { + t.Fatal(err) + } + + // Act: a caller modifies the slice it owns after CreateChat returns. + + input.MemberIDs[0] = uuid.New() + + // Assert. + stored, err := repo.Chat(t.Context(), input.ID) + if err != nil { + t.Fatal(err) + } + if len(stored.MemberIDs) != 1 || stored.MemberIDs[0] != member.ID { + t.Fatalf("caller mutated stored membership: %+v", stored) + } + }) + + t.Run("Positive: isolated result membership", func(t *testing.T) { + // Arrange. + repo := newTestStore(t, config.Default().Memory) + member := seedUser(t, repo, "alice") + input := dto.ChatDTO{ID: uuid.New(), Title: "Team", MemberIDs: []uuid.UUID{member.ID}} + created, err := repo.CreateChat(t.Context(), input) + if err != nil { + t.Fatal(err) + } + + // Act: a caller modifies the slice it owns after CreateChat returns. + + created.MemberIDs[0] = uuid.New() + + // Assert. + stored, err := repo.Chat(t.Context(), input.ID) + if err != nil { + t.Fatal(err) + } + if len(stored.MemberIDs) != 1 || stored.MemberIDs[0] != member.ID { + t.Fatalf("caller mutated stored membership: %+v", stored) + } + }) +} + +// TestStore_Chat_DefensiveCopy ensures a fetched detail cannot alter future reads. +func TestStore_Chat_DefensiveCopy(t *testing.T) { + // Arrange. + repo := newTestStore(t, config.Default().Memory) + member := seedUser(t, repo, "alice") + original := seedChat(t, repo, member.ID) + fetched, err := repo.Chat(t.Context(), original.ID) + if err != nil { + t.Fatal(err) + } + + // Act. + fetched.MemberIDs[0] = uuid.New() + + // Assert. + stored, err := repo.Chat(t.Context(), original.ID) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(stored, original) { + t.Fatalf("read changed stored chat: %+v", stored) + } +} diff --git a/internal/store/memory/messages.go b/internal/store/memory/messages.go new file mode 100644 index 0000000..15fff0d --- /dev/null +++ b/internal/store/memory/messages.go @@ -0,0 +1,41 @@ +package memory + +import ( + "context" + "sort" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// CreateMessage assigns ordering and publishes one complete message atomically. +func (s *Store) CreateMessage(_ context.Context, m dto.MessageDTO) (dto.MessageDTO, error) { + s.mu.Lock() + defer s.mu.Unlock() + if _, ok := s.chats[m.ChatID]; !ok { + return dto.MessageDTO{}, store.ErrNotFound + } + if s.messageCount >= s.limits.MaxMessages || !s.reserve(messageCost(m)) { + return dto.MessageDTO{}, store.ErrCapacity + } + m.CreatedAt = s.timestamp() + s.messages[m.ChatID] = append(s.messages[m.ChatID], m) + s.messageCount++ + return m, nil +} + +// Messages performs keyset pagination over an append-only ordered history. +func (s *Store) Messages(_ context.Context, chat uuid.UUID, p dto.PositionDTO, limit int) ([]dto.MessageDTO, error) { + s.mu.RLock() + defer s.mu.RUnlock() + all := s.messages[chat] + i := sort.Search(len(all), func(i int) bool { + return after(all[i].CreatedAt, all[i].ID, p) + }) + end := min(len(all), i+limit) + out := make([]dto.MessageDTO, end-i) + copy(out, all[i:end]) + return out, nil +} diff --git a/internal/store/memory/messages_test.go b/internal/store/memory/messages_test.go new file mode 100644 index 0000000..a973725 --- /dev/null +++ b/internal/store/memory/messages_test.go @@ -0,0 +1,72 @@ +package memory + +import ( + "errors" + "reflect" + "strings" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// TestStore_CreateMessage_ByteCapacity preserves acknowledged history and accounting on rejection. +func TestStore_CreateMessage_ByteCapacity(t *testing.T) { + // Arrange. + cfg := config.Default().Memory + cfg.MaxStateBytes = 2048 + repo := newTestStore(t, cfg) + member := seedUser(t, repo, "alice") + chat := seedChat(t, repo, member.ID) + original := seedMessage(t, repo, chat, "kept") + input := dto.MessageDTO{ID: uuid.New(), ChatID: chat.ID, SenderID: member.ID, Text: strings.Repeat("x", 2000)} + previousBytes := repo.usedBytes + + // Act. + got, err := repo.CreateMessage(t.Context(), input) + + // Assert. + if !errors.Is(err, store.ErrCapacity) || !reflect.DeepEqual(got, dto.MessageDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + items, readErr := repo.Messages(t.Context(), chat.ID, dto.PositionDTO{}, 10) + if readErr != nil { + t.Fatal(readErr) + } + if !reflect.DeepEqual(items, []dto.MessageDTO{original}) || repo.messageCount != 1 || repo.usedBytes != previousBytes { + t.Fatalf("overflow changed history or accounting: %v", items) + } +} + +// TestStore_CreateMessage_ClockRollback preserves chronological append order after a clock rollback. +func TestStore_CreateMessage_ClockRollback(t *testing.T) { + // Arrange. + repo := newTestStore(t, config.Default().Memory) + member := seedUser(t, repo, "alice") + chat := seedChat(t, repo, member.ID) + repo.lastTime = time.Now().UTC().Add(time.Hour) + first := seedMessage(t, repo, chat, "first") + input := dto.MessageDTO{ID: uuid.New(), ChatID: chat.ID, SenderID: member.ID, Text: "second"} + + // Act. + second, err := repo.CreateMessage(t.Context(), input) + + // Assert. + if err != nil { + t.Fatal(err) + } + if !second.CreatedAt.After(first.CreatedAt) { + t.Fatalf("second=%s must follow first=%s", second.CreatedAt, first.CreatedAt) + } + page, readErr := repo.Messages(t.Context(), chat.ID, dto.PositionDTO{CreatedAt: first.CreatedAt, ID: first.ID}, 10) + if readErr != nil { + t.Fatal(readErr) + } + if !reflect.DeepEqual(page, []dto.MessageDTO{second}) { + t.Fatalf("continuation lost the later message: %v", page) + } +} diff --git a/internal/store/memory/ordering.go b/internal/store/memory/ordering.go new file mode 100644 index 0000000..0cfb2fd --- /dev/null +++ b/internal/store/memory/ordering.go @@ -0,0 +1,25 @@ +package memory + +import ( + "bytes" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// timestamp is called under the write lock, keeping append order during clock rollback. +func (s *Store) timestamp() time.Time { + now := time.Now().UTC() + if !now.After(s.lastTime) { + now = s.lastTime.Add(time.Nanosecond) + } + s.lastTime = now + return now +} + +// after compares a record with a keyset pagination position. +func after(t time.Time, id uuid.UUID, p dto.PositionDTO) bool { + return t.After(p.CreatedAt) || (t.Equal(p.CreatedAt) && bytes.Compare(id[:], p.ID[:]) > 0) +} diff --git a/internal/store/memory/store.go b/internal/store/memory/store.go new file mode 100644 index 0000000..b7b2152 --- /dev/null +++ b/internal/store/memory/store.go @@ -0,0 +1,47 @@ +// Package memory implements synchronized storage within one bounded process. +package memory + +import ( + "sync" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// Store owns all mutable state and its indexes under one lock. +// Records are immutable once published; membership slices are copied on both +// write and read boundaries. Its zero value is not usable; construct it with New. +type Store struct { + mu sync.RWMutex + limits config.Memory + users map[uuid.UUID]dto.UserDTO + usernames map[string]uuid.UUID + chats map[uuid.UUID]dto.ChatDTO + userChats map[uuid.UUID][]dto.ChatDTO + messages map[uuid.UUID][]dto.MessageDTO + messageCount int + usedBytes int + lastTime time.Time +} + +var _ store.Interface = (*Store)(nil) + +// New validates and copies capacity settings, then initializes an empty store. +// It creates no files, goroutines or external connections. +func New(cfg *config.Memory) (*Store, error) { + if err := cfg.Validate(); err != nil { + return nil, err + } + return &Store{ + limits: *cfg, + users: make(map[uuid.UUID]dto.UserDTO), + usernames: make(map[string]uuid.UUID), + chats: make(map[uuid.UUID]dto.ChatDTO), + userChats: make(map[uuid.UUID][]dto.ChatDTO), + messages: make(map[uuid.UUID][]dto.MessageDTO), + }, nil +} diff --git a/internal/store/memory/store_test.go b/internal/store/memory/store_test.go new file mode 100644 index 0000000..b5707bd --- /dev/null +++ b/internal/store/memory/store_test.go @@ -0,0 +1,64 @@ +package memory + +import ( + "testing" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// TestStore_New rejects missing capacity settings. +func TestStore_New(t *testing.T) { + // Arrange. + var cfg *config.Memory + + // Act. + repo, err := New(cfg) + + // Assert. + if err == nil || repo != nil { + t.Fatalf("missing configuration accepted: %v, %v", repo, err) + } +} + +// newTestStore creates independent storage for one scenario. +func newTestStore(t *testing.T, cfg *config.Memory) *Store { + t.Helper() + repo, err := New(cfg) + if err != nil { + t.Fatal(err) + } + return repo +} + +// seedUser creates a fixture user and checks preparation errors. +func seedUser(t *testing.T, repo *Store, username string) dto.UserDTO { + t.Helper() + user, err := repo.CreateUser(t.Context(), dto.UserDTO{ID: uuid.New(), Username: username}) + if err != nil { + t.Fatal(err) + } + return user +} + +// seedChat creates a fixture chat for an existing member. +func seedChat(t *testing.T, repo *Store, member uuid.UUID) dto.ChatDTO { + t.Helper() + chat, err := repo.CreateChat(t.Context(), dto.ChatDTO{ID: uuid.New(), Title: "Fixture", MemberIDs: []uuid.UUID{member}}) + if err != nil { + t.Fatal(err) + } + return chat +} + +// seedMessage creates an acknowledged fixture message. +func seedMessage(t *testing.T, repo *Store, chat dto.ChatDTO, text string) dto.MessageDTO { + t.Helper() + message, err := repo.CreateMessage(t.Context(), dto.MessageDTO{ID: uuid.New(), ChatID: chat.ID, SenderID: chat.MemberIDs[0], Text: text}) + if err != nil { + t.Fatal(err) + } + return message +} diff --git a/internal/store/memory/users.go b/internal/store/memory/users.go new file mode 100644 index 0000000..e09ae7b --- /dev/null +++ b/internal/store/memory/users.go @@ -0,0 +1,50 @@ +package memory + +import ( + "context" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// CreateUser atomically enforces uniqueness and resource limits. +func (s *Store) CreateUser(_ context.Context, u dto.UserDTO) (dto.UserDTO, error) { + s.mu.Lock() + defer s.mu.Unlock() + if _, ok := s.usernames[u.Username]; ok { + return dto.UserDTO{}, store.ErrConflict + } + if len(s.users) >= s.limits.MaxUsers || !s.reserve(userCost(u)) { + return dto.UserDTO{}, store.ErrCapacity + } + u.CreatedAt = s.timestamp() + s.users[u.ID] = u + s.usernames[u.Username] = u.ID + return u, nil +} + +// User retrieves an immutable user. +func (s *Store) User(_ context.Context, id uuid.UUID) (dto.UserDTO, error) { + s.mu.RLock() + defer s.mu.RUnlock() + u, ok := s.users[id] + if !ok { + return u, store.ErrNotFound + } + return u, nil +} + +// UsersExist checks the complete set under one read lock without loading user records. +// Empty sets return true, and duplicate IDs do not affect the result. +func (s *Store) UsersExist(_ context.Context, ids []uuid.UUID) (bool, error) { + s.mu.RLock() + defer s.mu.RUnlock() + for _, id := range ids { + if _, exists := s.users[id]; !exists { + return false, nil + } + } + return true, nil +} diff --git a/internal/store/memory/users_test.go b/internal/store/memory/users_test.go new file mode 100644 index 0000000..da34e47 --- /dev/null +++ b/internal/store/memory/users_test.go @@ -0,0 +1,116 @@ +package memory + +import ( + "errors" + "testing" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// TestStore_UsersExist checks all-members semantics on an independent store per case. +func TestStore_UsersExist(t *testing.T) { + t.Run("Positive: existing user", func(t *testing.T) { + // Arrange. + repo := newTestStore(t, config.Default().Memory) + user := seedUser(t, repo, "alice") + ids := []uuid.UUID{user.ID} + want := true + + // Act. + got, err := repo.UsersExist(t.Context(), ids) + + // Assert. + if err != nil { + t.Fatal(err) + } + if got != want { + t.Fatalf("UsersExist(%v)=%v, want=%v", ids, got, want) + } + }) + + t.Run("Positive: duplicate IDs", func(t *testing.T) { + // Arrange. + repo := newTestStore(t, config.Default().Memory) + user := seedUser(t, repo, "alice") + ids := []uuid.UUID{user.ID} + want := true + + ids = append(ids, user.ID) + + // Act. + got, err := repo.UsersExist(t.Context(), ids) + + // Assert. + if err != nil { + t.Fatal(err) + } + if got != want { + t.Fatalf("UsersExist(%v)=%v, want=%v", ids, got, want) + } + }) + + t.Run("Positive: empty set", func(t *testing.T) { + // Arrange. + repo := newTestStore(t, config.Default().Memory) + var ids []uuid.UUID + want := true + + // Act. + got, err := repo.UsersExist(t.Context(), ids) + + // Assert. + if err != nil { + t.Fatal(err) + } + if got != want { + t.Fatalf("UsersExist(%v)=%v, want=%v", ids, got, want) + } + }) + + t.Run("Negative: missing member", func(t *testing.T) { + // Arrange. + repo := newTestStore(t, config.Default().Memory) + user := seedUser(t, repo, "alice") + ids := []uuid.UUID{user.ID} + want := false + + ids = append(ids, uuid.New()) + + // Act. + got, err := repo.UsersExist(t.Context(), ids) + + // Assert. + if err != nil { + t.Fatal(err) + } + if got != want { + t.Fatalf("UsersExist(%v)=%v, want=%v", ids, got, want) + } + }) +} + +// TestStore_CreateUser_Capacity checks that a refused user consumes no storage or username index. +func TestStore_CreateUser_Capacity(t *testing.T) { + // Arrange. + cfg := config.Default().Memory + cfg.MaxUsers = 1 + repo := newTestStore(t, cfg) + original := seedUser(t, repo, "alice") + rejected := dto.UserDTO{ID: uuid.New(), Username: "bob"} + previousBytes := repo.usedBytes + + // Act. + got, err := repo.CreateUser(t.Context(), rejected) + + // Assert. + if !errors.Is(err, store.ErrCapacity) || got != (dto.UserDTO{}) { + t.Fatalf("result=%+v, error=%v", got, err) + } + if len(repo.users) != 1 || repo.users[original.ID] != original || len(repo.usernames) != 1 || repo.usernames[original.Username] != original.ID || repo.usedBytes != previousBytes { + t.Fatal("refused registration changed storage or indexes") + } +} diff --git a/internal/store/postgres/attachments.go b/internal/store/postgres/attachments.go new file mode 100644 index 0000000..576e5c9 --- /dev/null +++ b/internal/store/postgres/attachments.go @@ -0,0 +1,69 @@ +package postgres + +import ( + "context" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +const attachmentColumns = `id,chat_id,owner_id,filename,content_type,size_bytes,sha256,status,object_key,created_at,updated_at` + +// Reserve creates durable intent before any S3 PUT. +func (s *Store) Reserve(ctx context.Context, a dto.Attachment) error { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + _, err := s.pool.Exec(ctx, `INSERT INTO attachments (`+attachmentColumns+`) VALUES(@id,@chat,@owner,@filename,@mime,@size,@sha,@status,@key,@created,@updated)`, pgx.StrictNamedArgs{ + "id": a.ID, + "chat": a.ChatID, + "owner": a.OwnerID, + "filename": a.Filename, + "mime": a.ContentType, + "size": a.SizeBytes, + "sha": a.SHA256, + "status": a.Status, + "key": a.ObjectKey, + "created": a.CreatedAt, + "updated": a.UpdatedAt, + }) + return normalize(err) +} + +// Publish atomically exposes a completed object without changing its immutable key. +func (s *Store) Publish(ctx context.Context, id uuid.UUID) (dto.Attachment, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + return scanAttachment(s.pool.QueryRow(ctx, `UPDATE attachments SET status='ready',updated_at=clock_timestamp() WHERE id=@id AND status='uploading' RETURNING `+attachmentColumns, pgx.StrictNamedArgs{"id": id})) +} + +// Attachment reads one immutable metadata record. +func (s *Store) Attachment(ctx context.Context, id uuid.UUID) (dto.Attachment, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + return scanAttachment(s.pool.QueryRow(ctx, `SELECT `+attachmentColumns+` FROM attachments WHERE id=@id`, pgx.StrictNamedArgs{"id": id})) +} + +// scanAttachment decodes explicit columns shared by read and publication. +func scanAttachment(row pgx.Row) (dto.Attachment, error) { + var a dto.Attachment + err := row.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, &a.SizeBytes, &a.SHA256, &a.Status, &a.ObjectKey, &a.CreatedAt, &a.UpdatedAt) + return a, normalize(err) +} + +// validateAttachments locks referenced metadata before inserting the message. +func validateAttachments(ctx context.Context, tx pgx.Tx, message dto.MessageDTO) error { + for _, id := range message.AttachmentIDs { + var status string + err := tx.QueryRow(ctx, `SELECT status FROM attachments WHERE id=@id AND chat_id=@chat FOR SHARE`, pgx.StrictNamedArgs{"id": id, "chat": message.ChatID}).Scan(&status) + if err != nil { + return err + } + if status != "ready" { + return store.ErrConflict + } + } + return nil +} diff --git a/internal/store/postgres/attachments_test.go b/internal/store/postgres/attachments_test.go new file mode 100644 index 0000000..0e7159c --- /dev/null +++ b/internal/store/postgres/attachments_test.go @@ -0,0 +1,167 @@ +package postgres + +import ( + "errors" + "testing" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/media" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// fixtureAttachment reserves an independent attachment for a real SQL scenario. +func fixtureAttachment(t *testing.T, s *Store) dto.Attachment { + t.Helper() + user := fixtureUser(t, s) + chat := fixtureChat(t, s, user) + id := uuid.New() + a := dto.Attachment{ + ID: id, + ChatID: chat.ID, + OwnerID: user.ID, + Filename: "test.png", + ContentType: "image/png", + SizeBytes: 32, + SHA256: "synthetic-checksum", + Status: "uploading", + ObjectKey: "original/" + id.String(), + CreatedAt: time.Now().UTC().Add(-2 * time.Hour), + UpdatedAt: time.Now().UTC(), + } + if err := s.Reserve(t.Context(), a); err != nil { + t.Fatal(err) + } + return a +} + +// TestAttachmentPublication verifies stored metadata survives a fresh read after commit. +func TestAttachmentPublication(t *testing.T) { + s := testStore(t) + a := fixtureAttachment(t, s) + published, err := s.Publish(t.Context(), a.ID) + if err != nil || published.Status != "ready" { + t.Fatalf("publish: %v", err) + } + got, err := s.Attachment(t.Context(), a.ID) + if err != nil || got.ObjectKey != a.ObjectKey || got.Status != "ready" { + t.Fatalf("stored metadata: %v", err) + } +} + +// TestAttachmentMessageScope checks a foreign chat cannot reference another chat's file. +func TestAttachmentMessageScope(t *testing.T) { + s := testStore(t) + a := fixtureAttachment(t, s) + if _, err := s.Publish(t.Context(), a.ID); err != nil { + t.Fatal(err) + } + foreignUser := fixtureUser(t, s) + foreignChat := fixtureChat(t, s, foreignUser) + _, err := s.CreateMessage(t.Context(), dto.MessageDTO{ID: uuid.New(), ChatID: foreignChat.ID, SenderID: foreignUser.ID, AttachmentIDs: []string{a.ID.String()}, IdempotencyKey: "foreign"}) + if !errors.Is(err, store.ErrNotFound) { + t.Fatalf("foreign attachment: %v", err) + } +} + +// TestAttachmentRetryConflict verifies attachment identity participates in the immutable request. +func TestAttachmentRetryConflict(t *testing.T) { + s := testStore(t) + a := fixtureAttachment(t, s) + if _, err := s.Publish(t.Context(), a.ID); err != nil { + t.Fatal(err) + } + m := dto.MessageDTO{ID: uuid.New(), ChatID: a.ChatID, SenderID: a.OwnerID, Text: "same", AttachmentIDs: []string{a.ID.String()}, IdempotencyKey: "retry"} + if _, err := s.CreateMessage(t.Context(), m); err != nil { + t.Fatal(err) + } + m.AttachmentIDs = nil + _, err := s.CreateMessage(t.Context(), m) + if !errors.Is(err, store.ErrConflict) { + t.Fatalf("changed attachment list: %v", err) + } +} + +// TestReclaimExcludesPublication demonstrates the durable cleanup state closes the publish race. +func TestReclaimExcludesPublication(t *testing.T) { + s := testStore(t) + a := fixtureAttachment(t, s) + claimed, err := s.Reclaim(t.Context(), a.ID, time.Now().Add(-time.Hour), false) + if err != nil || !claimed { + t.Fatalf("claim: %v", err) + } + if _, err := s.Publish(t.Context(), a.ID); !errors.Is(err, store.ErrNotFound) { + t.Fatalf("late publication: %v", err) + } + removed, err := s.Reclaim(t.Context(), a.ID, time.Now().Add(-time.Hour), true) + if err != nil || !removed { + t.Fatalf("cleanup: %v", err) + } +} + +// TestReclaimProtectsFreshReservation ensures a concurrent valid upload stays intact. +func TestReclaimProtectsFreshReservation(t *testing.T) { + s := testStore(t) + a := fixtureAttachment(t, s) + _, err := s.pool.Exec(t.Context(), `UPDATE attachments SET created_at=clock_timestamp() WHERE id=@id`, pgx.StrictNamedArgs{"id": a.ID}) + if err != nil { + t.Fatal(err) + } + claimed, err := s.Reclaim(t.Context(), a.ID, time.Now().Add(-time.Hour), false) + if err != nil || claimed { + t.Fatalf("fresh reservation removed: %v", err) + } +} + +// TestMetadataReconciliation detects missing originals without deleting referenced metadata. +func TestMetadataReconciliation(t *testing.T) { + s := testStore(t) + a := fixtureAttachment(t, s) + if _, err := s.Publish(t.Context(), a.ID); err != nil { + t.Fatal(err) + } + objects := &reconcileObjects{exists: false} + dry, err := media.Reconcile(t.Context(), s, objects, uuid.Nil, time.Hour, false) + if err != nil || dry.Missing != 1 || dry.Changed != 0 { + t.Fatalf("dry run: %+v %v", dry, err) + } + applied, err := media.Reconcile(t.Context(), s, objects, uuid.Nil, time.Hour, true) + if err != nil || applied.Changed != 1 { + t.Fatalf("repair: %+v %v", applied, err) + } + again, err := media.Reconcile(t.Context(), s, objects, uuid.Nil, time.Hour, true) + if err != nil || again.Changed != 0 { + t.Fatalf("repeat changed state: %+v %v", again, err) + } + objects.exists = true + restored, err := media.Reconcile(t.Context(), s, objects, uuid.Nil, time.Hour, true) + if err != nil || restored.Changed != 1 { + t.Fatalf("restore: %+v %v", restored, err) + } +} + +// TestObjectReservationLookup protects unfinished uploads from orphan deletion. +func TestObjectReservationLookup(t *testing.T) { + s := testStore(t) + a := fixtureAttachment(t, s) + exists, err := s.HasObject(t.Context(), a.ObjectKey) + if err != nil || !exists { + t.Fatalf("reservation not protected: %v", err) + } + exists, err = s.HasObject(t.Context(), "original/"+uuid.NewString()) + if err != nil || exists { + t.Fatalf("unknown object considered reserved: %v", err) + } +} + +// TestInventoryStopsAtUnavailableDatabase rejects a scan rather than reporting an empty inventory. +func TestInventoryStopsAtUnavailableDatabase(t *testing.T) { + s := testStore(t) + s.Close() + if _, err := s.Scan(t.Context(), uuid.Nil, 1000); err == nil { + t.Fatal("closed database reported empty inventory") + } +} diff --git a/internal/store/postgres/chats.go b/internal/store/postgres/chats.go new file mode 100644 index 0000000..2dc2d96 --- /dev/null +++ b/internal/store/postgres/chats.go @@ -0,0 +1,58 @@ +package postgres + +import ( + "context" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// CreateChat commits the chat and every member together, rolling back on any failure. +func (s *Store) CreateChat(ctx context.Context, chat dto.ChatDTO) (dto.ChatDTO, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { + if err := tx.QueryRow(ctx, `INSERT INTO chats(id,title) VALUES(@id,@title) RETURNING created_at`, pgx.StrictNamedArgs{"id": chat.ID, "title": chat.Title}).Scan(&chat.CreatedAt); err != nil { + return err + } + _, err := tx.Exec(ctx, `INSERT INTO chat_members(chat_id,user_id) SELECT @chat,id FROM unnest(@members::uuid[]) AS member(id)`, pgx.StrictNamedArgs{"chat": chat.ID, "members": chat.MemberIDs}) + return err + }) + return chat, normalize(err) +} + +// Chat reads a chat and its immutable membership in one snapshot. +func (s *Store) Chat(ctx context.Context, id uuid.UUID) (dto.ChatDTO, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + var chat dto.ChatDTO + err := s.pool.QueryRow(ctx, `SELECT c.id,c.title,c.created_at,ARRAY(SELECT user_id FROM chat_members WHERE chat_id=c.id ORDER BY user_id) + FROM chats c WHERE c.id=@id`, pgx.StrictNamedArgs{"id": id}).Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs) + return chat, normalize(err) +} + +// Chats returns the requested keyset page of chats visible to a user. +func (s *Store) Chats(ctx context.Context, userID uuid.UUID, position dto.PositionDTO, limit int) ([]dto.ChatDTO, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + rows, err := s.pool.Query(ctx, `SELECT c.id,c.title,c.created_at,ARRAY(SELECT user_id FROM chat_members WHERE chat_id=c.id ORDER BY user_id) + FROM chats c JOIN chat_members m ON m.chat_id=c.id + WHERE m.user_id=@user AND (c.created_at,c.id)>(@time,@id) ORDER BY c.created_at,c.id LIMIT @limit`, pgx.StrictNamedArgs{ + "user": userID, "time": position.CreatedAt, "id": position.ID, "limit": limit, + }) + if err != nil { + return nil, normalize(err) + } + defer rows.Close() + chats := make([]dto.ChatDTO, 0) + for rows.Next() { + var chat dto.ChatDTO + if err := rows.Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs); err != nil { + return nil, normalize(err) + } + chats = append(chats, chat) + } + return chats, normalize(rows.Err()) +} diff --git a/internal/store/postgres/errors_test.go b/internal/store/postgres/errors_test.go new file mode 100644 index 0000000..9603772 --- /dev/null +++ b/internal/store/postgres/errors_test.go @@ -0,0 +1,52 @@ +package postgres + +import ( + "context" + "errors" + "fmt" + "io" + "net" + "testing" + + "github.com/jackc/pgx/v5/pgconn" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// TestNormalizeUnavailable classifies dependency failures while retaining their causes. +func TestNormalizeUnavailable(t *testing.T) { + cases := []struct { + name string + err error + }{ + {"deadline", context.DeadlineExceeded}, + {"closed stream", io.EOF}, + {"truncated stream", io.ErrUnexpectedEOF}, + {"network", &net.OpError{Op: "dial", Net: "tcp", Err: errors.New("refused")}}, + {"connection exception", &pgconn.PgError{Code: "08006"}}, + {"connection limit", &pgconn.PgError{Code: "53300"}}, + {"shutdown", &pgconn.PgError{Code: "57P01"}}, + {"crash", &pgconn.PgError{Code: "57P02"}}, + {"starting", &pgconn.PgError{Code: "57P03"}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + err := normalize(fmt.Errorf("operation failed: %w", tc.err)) + if !errors.Is(err, store.ErrUnavailable) || !errors.Is(err, tc.err) { + t.Fatalf("failure category or cause lost: %T", tc.err) + } + }) + } +} + +// TestNormalizeUnexpected preserves programming errors instead of misclassifying them as outages. +func TestNormalizeUnexpected(t *testing.T) { + for _, code := range []string{"42601", "23503", "42P01"} { + t.Run(code, func(t *testing.T) { + err := &pgconn.PgError{Code: code} + if normalize(err) != err { + t.Fatal("unexpected SQL error was reclassified") + } + }) + } +} diff --git a/internal/store/postgres/idempotency.go b/internal/store/postgres/idempotency.go new file mode 100644 index 0000000..fe685ab --- /dev/null +++ b/internal/store/postgres/idempotency.go @@ -0,0 +1,52 @@ +package postgres + +import ( + "context" + "errors" + "slices" + + "github.com/jackc/pgx/v5" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// replayMessage serializes one chat and returns an unexpired original response. +func replayMessage(ctx context.Context, tx pgx.Tx, message dto.MessageDTO) (dto.MessageDTO, bool, error) { + var locked string + if err := tx.QueryRow(ctx, "SELECT id FROM chats WHERE id=@chat FOR UPDATE", pgx.StrictNamedArgs{"chat": message.ChatID}).Scan(&locked); err != nil { + return dto.MessageDTO{}, false, err + } + if message.IdempotencyKey == "" { + return dto.MessageDTO{}, false, nil + } + args := pgx.StrictNamedArgs{"user": message.SenderID, "chat": message.ChatID, "key": message.IdempotencyKey} + if _, err := tx.Exec(ctx, `DELETE FROM message_idempotency WHERE user_id=@user AND chat_id=@chat AND key=@key AND expires_at<=clock_timestamp()`, args); err != nil { + return dto.MessageDTO{}, false, err + } + var original dto.MessageDTO + err := tx.QueryRow(ctx, `SELECT m.id,m.chat_id,m.sender_id,m.text,m.created_at,m.attachment_ids FROM message_idempotency i JOIN messages m ON m.id=i.message_id + WHERE i.user_id=@user AND i.chat_id=@chat AND i.key=@key`, args).Scan(&original.ID, &original.ChatID, &original.SenderID, &original.Text, &original.CreatedAt, &original.AttachmentIDs) + if errors.Is(err, pgx.ErrNoRows) { + return dto.MessageDTO{}, false, nil + } + if err != nil { + return dto.MessageDTO{}, false, err + } + if original.Text != message.Text || !slices.Equal(original.AttachmentIDs, message.AttachmentIDs) { + return dto.MessageDTO{}, false, store.ErrConflict + } + return original, true, nil +} + +// rememberMessage stores the result in the same transaction as the message insert. +func rememberMessage(ctx context.Context, tx pgx.Tx, message dto.MessageDTO) error { + if message.IdempotencyKey == "" { + return nil + } + _, err := tx.Exec(ctx, `INSERT INTO message_idempotency(user_id,chat_id,key,message_id,expires_at) + VALUES(@user,@chat,@key,@id,clock_timestamp()+interval '24 hours')`, pgx.StrictNamedArgs{ + "user": message.SenderID, "chat": message.ChatID, "key": message.IdempotencyKey, "id": message.ID, + }) + return err +} diff --git a/internal/store/postgres/idempotency_test.go b/internal/store/postgres/idempotency_test.go new file mode 100644 index 0000000..2a5902c --- /dev/null +++ b/internal/store/postgres/idempotency_test.go @@ -0,0 +1,139 @@ +package postgres + +import ( + "errors" + "sync" + "testing" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// TestMessageRetryRace verifies concurrent attempts commit exactly one message and replay its response. +func TestMessageRetryRace(t *testing.T) { + s := testStore(t) + user := fixtureUser(t, s) + chat := fixtureChat(t, s, user) + var wg sync.WaitGroup + outcomes := make(chan dto.MessageDTO, 12) + failures := make(chan error, 12) + for range 12 { + wg.Go(func() { + message, err := s.CreateMessage(t.Context(), dto.MessageDTO{ID: uuid.New(), ChatID: chat.ID, SenderID: user.ID, Text: "same", IdempotencyKey: "retry"}) + if err != nil { + failures <- err + return + } + outcomes <- message + }) + } + wg.Wait() + close(failures) + close(outcomes) + for err := range failures { + t.Error(err) + } + var original dto.MessageDTO + for m := range outcomes { + if original.ID == uuid.Nil { + original = m + } + if m.ID != original.ID || m.Text != original.Text || !m.CreatedAt.Equal(original.CreatedAt) { + t.Fatal("retry returned a different result") + } + } + messages, err := s.Messages(t.Context(), chat.ID, dto.PositionDTO{}, 100) + if err != nil || len(messages) != 1 { + t.Fatalf("messages=%d error=%v", len(messages), err) + } +} + +// TestMessageRetryConflict rejects a different body without publishing a second message. +func TestMessageRetryConflict(t *testing.T) { + s := testStore(t) + user := fixtureUser(t, s) + chat := fixtureChat(t, s, user) + message := dto.MessageDTO{ID: uuid.New(), ChatID: chat.ID, SenderID: user.ID, Text: "first", IdempotencyKey: "retry"} + if _, err := s.CreateMessage(t.Context(), message); err != nil { + t.Fatal(err) + } + message.ID = uuid.New() + message.Text = "different" + if _, err := s.CreateMessage(t.Context(), message); !errors.Is(err, store.ErrConflict) { + t.Fatalf("expected conflict: %v", err) + } +} + +// TestMessageRetryExpiry permits reuse after the original retention window ends. +func TestMessageRetryExpiry(t *testing.T) { + s := testStore(t) + user := fixtureUser(t, s) + chat := fixtureChat(t, s, user) + message := dto.MessageDTO{ID: uuid.New(), ChatID: chat.ID, SenderID: user.ID, Text: "first", IdempotencyKey: "retry"} + if _, err := s.CreateMessage(t.Context(), message); err != nil { + t.Fatal(err) + } + if _, err := s.pool.Exec(t.Context(), "UPDATE message_idempotency SET expires_at=clock_timestamp()-interval '1 second'"); err != nil { + t.Fatal(err) + } + message.ID = uuid.New() + message.Text = "new" + if result, err := s.CreateMessage(t.Context(), message); err != nil || result.ID != message.ID { + t.Fatalf("expired result reused: %v", err) + } +} + +// TestMessageRetryRollback proves failed inserts leave no poisoned idempotency result. +func TestMessageRetryRollback(t *testing.T) { + s := testStore(t) + user := fixtureUser(t, s) + chat := fixtureChat(t, s, user) + message := dto.MessageDTO{ID: uuid.New(), ChatID: chat.ID, SenderID: uuid.New(), Text: "first", IdempotencyKey: "retry"} + if _, err := s.CreateMessage(t.Context(), message); err == nil { + t.Fatal("missing sender accepted") + } + var count int + if err := s.pool.QueryRow(t.Context(), "SELECT count(*) FROM message_idempotency").Scan(&count); err != nil || count != 0 { + t.Fatalf("failed write stored a result: %d %v", count, err) + } + message.SenderID = user.ID + if _, err := s.CreateMessage(t.Context(), message); err != nil { + t.Fatal(err) + } +} + +// TestSchemaCheck verifies API schema checks neither create nor upgrade tables. +func TestSchemaCheck(t *testing.T) { + s := testStore(t) + if err := s.CheckSchema(t.Context()); err != nil { + t.Fatal(err) + } + if _, err := s.pool.Exec(t.Context(), "DELETE FROM schema_migrations WHERE version=3"); err != nil { + t.Fatal(err) + } + if err := s.CheckSchema(t.Context()); err == nil { + t.Fatal("outdated schema accepted") + } + s.Close() + if err := s.CheckSchema(t.Context()); err == nil { + t.Fatal("closed connection accepted") + } +} + +// TestIdempotencyMigrationFailureRollsBackVersion keeps an unsuccessful DDL change out of migration history. +func TestIdempotencyMigrationFailureRollsBackVersion(t *testing.T) { + s := testStore(t) + if _, err := s.pool.Exec(t.Context(), "DELETE FROM schema_migrations WHERE version=2"); err != nil { + t.Fatal(err) + } + // The existing table deliberately conflicts with the pending migration. + if err := s.Start(t.Context()); err == nil { + t.Fatal("conflicting DDL reported success") + } + var applied bool + if err := s.pool.QueryRow(t.Context(), "SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version=2)").Scan(&applied); err != nil || applied { + t.Fatalf("failed migration marked applied: %v", err) + } +} diff --git a/internal/store/postgres/messages.go b/internal/store/postgres/messages.go new file mode 100644 index 0000000..5b94e3a --- /dev/null +++ b/internal/store/postgres/messages.go @@ -0,0 +1,64 @@ +package postgres + +import ( + "context" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// CreateMessage serializes timestamps on the owning chat and atomically appends a message. +func (s *Store) CreateMessage(ctx context.Context, message dto.MessageDTO) (dto.MessageDTO, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { + original, found, err := replayMessage(ctx, tx, message) + if err != nil { + return err + } + if found { + message = original + return nil + } + if err := validateAttachments(ctx, tx, message); err != nil { + return err + } + if err := tx.QueryRow(ctx, `UPDATE chats SET last_message_at=GREATEST(clock_timestamp(),last_message_at+interval '1 microsecond') + WHERE id=@chat RETURNING last_message_at`, pgx.StrictNamedArgs{"chat": message.ChatID}).Scan(&message.CreatedAt); err != nil { + return err + } + _, err = tx.Exec(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,created_at,attachment_ids) VALUES(@id,@chat,@sender,@text,@time,@attachments)`, pgx.StrictNamedArgs{ + "attachments": append([]string{}, message.AttachmentIDs...), "id": message.ID, "chat": message.ChatID, "sender": message.SenderID, "text": message.Text, "time": message.CreatedAt, + }) + if err != nil { + return err + } + return rememberMessage(ctx, tx, message) + }) + return message, normalize(err) +} + +// Messages reads an indexed keyset page without scanning earlier history. +func (s *Store) Messages(ctx context.Context, chatID uuid.UUID, position dto.PositionDTO, limit int) ([]dto.MessageDTO, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + rows, err := s.pool.Query(ctx, `SELECT id,chat_id,sender_id,text,created_at,attachment_ids FROM messages + WHERE chat_id=@chat AND (created_at,id)>(@time,@id) ORDER BY created_at,id LIMIT @limit`, pgx.StrictNamedArgs{ + "chat": chatID, "time": position.CreatedAt, "id": position.ID, "limit": limit, + }) + if err != nil { + return nil, normalize(err) + } + defer rows.Close() + messages := make([]dto.MessageDTO, 0) + for rows.Next() { + var message dto.MessageDTO + if err := rows.Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, &message.CreatedAt, &message.AttachmentIDs); err != nil { + return nil, normalize(err) + } + messages = append(messages, message) + } + return messages, normalize(rows.Err()) +} diff --git a/internal/store/postgres/migrations/001_initial.sql b/internal/store/postgres/migrations/001_initial.sql new file mode 100644 index 0000000..0a19d68 --- /dev/null +++ b/internal/store/postgres/migrations/001_initial.sql @@ -0,0 +1,34 @@ +CREATE TABLE users ( + id uuid PRIMARY KEY, + username text NOT NULL UNIQUE, + display_name text NOT NULL, + password_hash text NOT NULL DEFAULT '', + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); +CREATE TABLE chats ( + id uuid PRIMARY KEY, + title text NOT NULL, + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + last_message_at timestamptz NOT NULL DEFAULT '-infinity' +); +CREATE TABLE chat_members ( + chat_id uuid NOT NULL REFERENCES chats(id), + user_id uuid NOT NULL REFERENCES users(id), + PRIMARY KEY(chat_id,user_id) +); +CREATE INDEX chat_members_user ON chat_members(user_id,chat_id); +CREATE TABLE messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id), + sender_id uuid NOT NULL REFERENCES users(id), + text text NOT NULL, + created_at timestamptz NOT NULL, + FOREIGN KEY(chat_id,sender_id) REFERENCES chat_members(chat_id,user_id) +); +CREATE INDEX messages_history ON messages(chat_id,created_at,id); +CREATE TABLE sessions ( + token_hash text PRIMARY KEY, + user_id uuid NOT NULL REFERENCES users(id), + expires_at timestamptz NOT NULL +); +CREATE INDEX sessions_expiry ON sessions(expires_at); diff --git a/internal/store/postgres/migrations/002_idempotency.sql b/internal/store/postgres/migrations/002_idempotency.sql new file mode 100644 index 0000000..d34a519 --- /dev/null +++ b/internal/store/postgres/migrations/002_idempotency.sql @@ -0,0 +1,9 @@ +CREATE TABLE message_idempotency ( + user_id uuid NOT NULL REFERENCES users(id), + chat_id uuid NOT NULL REFERENCES chats(id), + key text NOT NULL, + message_id uuid NOT NULL REFERENCES messages(id), + expires_at timestamptz NOT NULL, + PRIMARY KEY (user_id,chat_id,key) +); +CREATE INDEX message_idempotency_expiry ON message_idempotency(expires_at); diff --git a/internal/store/postgres/migrations/003_attachments.sql b/internal/store/postgres/migrations/003_attachments.sql new file mode 100644 index 0000000..3208e69 --- /dev/null +++ b/internal/store/postgres/migrations/003_attachments.sql @@ -0,0 +1,15 @@ +CREATE TABLE attachments ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id), + owner_id uuid NOT NULL REFERENCES users(id), + filename text NOT NULL, + content_type text NOT NULL, + size_bytes bigint NOT NULL CHECK(size_bytes BETWEEN 1 AND 10485760), + sha256 text NOT NULL, + status text NOT NULL CHECK(status IN ('uploading','ready','missing','deleting')), + object_key text NOT NULL UNIQUE, + created_at timestamptz NOT NULL, + updated_at timestamptz NOT NULL +); +CREATE INDEX attachments_scan ON attachments(created_at,id); +ALTER TABLE messages ADD COLUMN attachment_ids text[] NOT NULL DEFAULT '{}'; diff --git a/internal/store/postgres/reconcile.go b/internal/store/postgres/reconcile.go new file mode 100644 index 0000000..e3163fa --- /dev/null +++ b/internal/store/postgres/reconcile.go @@ -0,0 +1,64 @@ +package postgres + +import ( + "context" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// Scan returns a bounded metadata page ordered by the stable primary key. +func (s *Store) Scan(ctx context.Context, after uuid.UUID, limit int) ([]dto.Attachment, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + rows, err := s.pool.Query(ctx, `SELECT `+attachmentColumns+` FROM attachments WHERE id>@after ORDER BY id LIMIT @limit`, pgx.StrictNamedArgs{"after": after, "limit": limit}) + if err != nil { + return nil, normalize(err) + } + defer rows.Close() + result := make([]dto.Attachment, 0) + for rows.Next() { + a, err := scanAttachment(rows) + if err != nil { + return nil, err + } + result = append(result, a) + } + return result, normalize(rows.Err()) +} + +// Reclaim uses a durable deleting state to exclude publication during object cleanup. +func (s *Store) Reclaim(ctx context.Context, id uuid.UUID, cutoff time.Time, finish bool) (bool, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + query := `UPDATE attachments SET status='deleting' WHERE id=@id AND created_at<@cutoff AND status IN ('uploading','deleting') AND NOT EXISTS(SELECT 1 FROM messages WHERE @textid=ANY(attachment_ids))` + if finish { + query = `DELETE FROM attachments WHERE id=@id AND created_at<@cutoff AND status='deleting' AND NOT EXISTS(SELECT 1 FROM messages WHERE @textid=ANY(attachment_ids))` + } + tag, err := s.pool.Exec(ctx, query, pgx.StrictNamedArgs{"id": id, "textid": id.String(), "cutoff": cutoff}) + return tag.RowsAffected() == 1, normalize(err) +} + +// MarkMissing preserves referenced metadata while recording or clearing an availability incident. +func (s *Store) MarkMissing(ctx context.Context, id uuid.UUID, missing bool) error { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + status := "ready" + if missing { + status = "missing" + } + _, err := s.pool.Exec(ctx, `UPDATE attachments SET status=@status,updated_at=clock_timestamp() WHERE id=@id AND status IN ('ready','missing')`, pgx.StrictNamedArgs{"id": id, "status": status}) + return normalize(err) +} + +// HasObject protects even unfinished durable reservations from orphan cleanup. +func (s *Store) HasObject(ctx context.Context, key string) (bool, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + var exists bool + err := s.pool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM attachments WHERE object_key=@key)`, pgx.StrictNamedArgs{"key": key}).Scan(&exists) + return exists, normalize(err) +} diff --git a/internal/store/postgres/reconcile_fixture_test.go b/internal/store/postgres/reconcile_fixture_test.go new file mode 100644 index 0000000..2d7342c --- /dev/null +++ b/internal/store/postgres/reconcile_fixture_test.go @@ -0,0 +1,18 @@ +package postgres + +import "context" + +type reconcileObjects struct { + exists bool +} + +// Exists supplies a deterministic object-side fault while exercising real SQL transitions. +func (r *reconcileObjects) Exists(context.Context, string) (bool, error) { + return r.exists, nil +} + +// Remove models an idempotent object removal for SQL cleanup tests. +func (r *reconcileObjects) Remove(context.Context, string) error { + r.exists = false + return nil +} diff --git a/internal/store/postgres/research_test.go b/internal/store/postgres/research_test.go new file mode 100644 index 0000000..02eb711 --- /dev/null +++ b/internal/store/postgres/research_test.go @@ -0,0 +1,52 @@ +package postgres + +import ( + "os" + "testing" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/config" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/media" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/objectstore" +) + +// TestMeasuredMissingInventory measures bounded real PostgreSQL/S3 scans with injected missing originals. +func TestMeasuredMissingInventory(t *testing.T) { + if os.Getenv("S3_ENDPOINT") == "" { + t.Skip("isolated S3 required for measured reconciliation") + } + for _, size := range []struct { + name string + count int + }{{"100 records", 100}, {"1000 records", 1000}} { + t.Run(size.name, func(t *testing.T) { + s := testStore(t) + user := fixtureUser(t, s) + chat := fixtureChat(t, s, user) + _, err := s.pool.Exec(t.Context(), `WITH ids AS (SELECT gen_random_uuid() AS id FROM generate_series(1,@count::int)) + INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,status,object_key,created_at,updated_at) + SELECT id,@chat,@owner,'synthetic.png','image/png',32,'synthetic','ready','original/'||id::text,clock_timestamp()-interval '2 hours',clock_timestamp() FROM ids`, pgx.StrictNamedArgs{"count": size.count, "chat": chat.ID, "owner": user.ID}) + if err != nil { + t.Fatal(err) + } + cfg, err := config.NewObjectStore() + if err != nil { + t.Fatal(err) + } + objects, err := objectstore.New(cfg) + if err != nil { + t.Fatal(err) + } + started := time.Now() + result, err := media.Reconcile(t.Context(), s, objects, uuid.Nil, time.Hour, false) + elapsed := time.Since(started) + if err != nil || result.Scanned != size.count || result.Missing != size.count || result.Changed != 0 { + t.Fatalf("inventory=%+v err=%v", result, err) + } + t.Logf("records=%d missing=%d elapsed_ms=%.3f apply=false", result.Scanned, result.Missing, float64(elapsed.Microseconds())/1000) + }) + } +} diff --git a/internal/store/postgres/sessions.go b/internal/store/postgres/sessions.go new file mode 100644 index 0000000..3e64d27 --- /dev/null +++ b/internal/store/postgres/sessions.go @@ -0,0 +1,36 @@ +package postgres + +import ( + "context" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" +) + +// SaveSession persists an absolute expiry and a token digest in PostgreSQL. +func (s *Store) SaveSession(ctx context.Context, hash string, userID uuid.UUID, expiry time.Time) error { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + _, err := s.pool.Exec(ctx, `INSERT INTO sessions(token_hash,user_id,expires_at) VALUES(@hash,@user,@expiry)`, pgx.StrictNamedArgs{ + "hash": hash, "user": userID, "expiry": expiry, + }) + return normalize(err) +} + +// SessionUser rejects absent, revoked and expired sessions using database time. +func (s *Store) SessionUser(ctx context.Context, hash string) (uuid.UUID, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + var id uuid.UUID + err := s.pool.QueryRow(ctx, `SELECT user_id FROM sessions WHERE token_hash=@hash AND expires_at>clock_timestamp()`, pgx.StrictNamedArgs{"hash": hash}).Scan(&id) + return id, normalize(err) +} + +// RevokeSession deletes the authoritative session before logout succeeds. +func (s *Store) RevokeSession(ctx context.Context, hash string) error { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + _, err := s.pool.Exec(ctx, `DELETE FROM sessions WHERE token_hash=@hash`, pgx.StrictNamedArgs{"hash": hash}) + return normalize(err) +} diff --git a/internal/store/postgres/store.go b/internal/store/postgres/store.go new file mode 100644 index 0000000..bf7725e --- /dev/null +++ b/internal/store/postgres/store.go @@ -0,0 +1,152 @@ +// Package postgres persists messenger records in PostgreSQL. +package postgres + +import ( + "context" + _ "embed" + "errors" + "fmt" + "io" + "net" + "strings" + "time" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" + "github.com/jackc/pgx/v5/pgxpool" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +//go:embed migrations/001_initial.sql +var initialMigration string + +//go:embed migrations/002_idempotency.sql +var idempotencyMigration string + +//go:embed migrations/003_attachments.sql +var attachmentsMigration string + +const migrationLockID = 202602 + +// Options bounds database resource use. +type Options struct { + // URL identifies the isolated application database. + URL string + // MaxConnections bounds concurrent database connections. + MaxConnections int32 + // Timeout bounds each persistence operation. + Timeout time.Duration +} + +// Store owns the PostgreSQL pool; Start and Close belong to application lifecycle. +type Store struct { + pool *pgxpool.Pool + timeout time.Duration +} + +var _ store.Interface = (*Store)(nil) + +// New validates options and builds a lazy pool without opening connections. +func New(options Options) (*Store, error) { + if options.URL == "" || options.MaxConnections < 1 || options.Timeout <= 0 { + return nil, fmt.Errorf("database URL, pool bound and timeout are required") + } + cfg, err := pgxpool.ParseConfig(options.URL) + if err != nil { + return nil, fmt.Errorf("invalid database configuration") + } + cfg.AfterConnect = func(_ context.Context, conn *pgx.Conn) error { + conn.TypeMap().RegisterType(&pgtype.Type{Name: "timestamptz", OID: pgtype.TimestamptzOID, Codec: &pgtype.TimestamptzCodec{ScanLocation: time.UTC}}) + return nil + } + cfg.MaxConns = options.MaxConnections + cfg.ConnConfig.ConnectTimeout = options.Timeout + pool, err := pgxpool.NewWithConfig(context.Background(), cfg) + if err != nil { + return nil, err + } + return &Store{pool: pool, timeout: options.Timeout}, nil +} + +// Start verifies connectivity and applies versioned migrations under a transaction lock. +func (s *Store) Start(ctx context.Context) error { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { + if _, err := tx.Exec(ctx, "SELECT pg_advisory_xact_lock(@id)", pgx.StrictNamedArgs{"id": migrationLockID}); err != nil { + return err + } + if _, err := tx.Exec(ctx, "CREATE TABLE IF NOT EXISTS schema_migrations (version integer PRIMARY KEY)"); err != nil { + return err + } + for index, source := range []string{initialMigration, idempotencyMigration, attachmentsMigration} { + version := index + 1 + var exists bool + if err := tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version=@version)", pgx.StrictNamedArgs{"version": version}).Scan(&exists); err != nil { + return err + } + if exists { + continue + } + if _, err := tx.Exec(ctx, source); err != nil { + return err + } + if _, err := tx.Exec(ctx, "INSERT INTO schema_migrations(version) VALUES(@version)", pgx.StrictNamedArgs{"version": version}); err != nil { + return err + } + } + return nil + + }) +} + +// Close releases owned pool resources after serving has stopped. +func (s *Store) Close() { + s.pool.Close() +} + +// Ping checks whether mandatory persistence can serve requests. +func (s *Store) Ping(ctx context.Context) error { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + return s.pool.Ping(ctx) +} + +// normalize translates expected database errors without exposing SQL to higher layers. +func normalize(err error) error { + if errors.Is(err, pgx.ErrNoRows) { + return store.ErrNotFound + } + if databaseError, ok := errors.AsType[*pgconn.PgError](err); ok { + if databaseError.Code == "23505" { + return store.ErrConflict + } + if strings.HasPrefix(databaseError.Code, "08") || strings.HasPrefix(databaseError.Code, "53") || + databaseError.Code == "57P01" || databaseError.Code == "57P02" || databaseError.Code == "57P03" { + return fmt.Errorf("%w: %w", store.ErrUnavailable, err) + } + } + var networkError net.Error + var connectError *pgconn.ConnectError + if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) || + errors.As(err, &networkError) || errors.As(err, &connectError) || pgconn.SafeToRetry(err) { + return fmt.Errorf("%w: %w", store.ErrUnavailable, err) + } + return err +} + +// CheckSchema verifies readiness without allowing API replicas to run DDL. +func (s *Store) CheckSchema(ctx context.Context) error { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + var version int + if err := s.pool.QueryRow(ctx, "SELECT COALESCE(max(version),0) FROM schema_migrations").Scan(&version); err != nil { + return err + } + if version != 3 { + return fmt.Errorf("database schema version %d; run server migrate", version) + } + return nil +} diff --git a/internal/store/postgres/store_test.go b/internal/store/postgres/store_test.go new file mode 100644 index 0000000..f357e6e --- /dev/null +++ b/internal/store/postgres/store_test.go @@ -0,0 +1,336 @@ +package postgres + +import ( + "context" + "errors" + "os" + "sync" + "testing" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" +) + +// testStore creates an isolated schema inside the explicitly configured test database. +func testStore(t *testing.T) *Store { + t.Helper() + url := os.Getenv("TEST_DATABASE_URL") + if url == "" { + t.Skip("TEST_DATABASE_URL is required for PostgreSQL integration tests") + } + admin, err := pgx.Connect(t.Context(), url) + if err != nil { + t.Fatal(err) + } + schema := "test_" + uuid.New().String()[:8] + identifier := pgx.Identifier{schema}.Sanitize() + if _, err := admin.Exec(t.Context(), "CREATE SCHEMA "+identifier); err != nil { + t.Fatal(err) + } + separator := "?" + if containsQuery(url) { + separator = "&" + } + s, err := New(Options{URL: url + separator + "search_path=" + schema, MaxConnections: 4, Timeout: 3 * time.Second}) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + s.Close() + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + if _, err := admin.Exec(ctx, "DROP SCHEMA "+identifier+" CASCADE"); err != nil { + t.Error(err) + } + if err := admin.Close(ctx); err != nil { + t.Error(err) + } + }) + if err := s.Start(t.Context()); err != nil { + t.Fatal(err) + } + return s +} + +// containsQuery identifies URL parameters without exposing credentials in test logs. +func containsQuery(value string) bool { + for _, char := range value { + if char == '?' { + return true + } + } + return false +} + +// fixtureUser creates an independent profile for a persistence scenario. +func fixtureUser(t *testing.T, s *Store) dto.UserDTO { + t.Helper() + user, err := s.Register(t.Context(), dto.UserDTO{ID: uuid.New(), Username: "u_" + uuid.NewString()[:8], DisplayName: "Test"}, "test-hash") + if err != nil { + t.Fatal(err) + } + return user +} + +// fixtureChat creates a chat containing an existing user. +func fixtureChat(t *testing.T, s *Store, user dto.UserDTO) dto.ChatDTO { + t.Helper() + chat, err := s.CreateChat(t.Context(), dto.ChatDTO{ID: uuid.New(), Title: "Test", MemberIDs: []uuid.UUID{user.ID}}) + if err != nil { + t.Fatal(err) + } + return chat +} + +// TestMigrationsRepeat verifies idempotent schema initialization. +func TestMigrationsRepeat(t *testing.T) { + s := testStore(t) + user := fixtureUser(t, s) + if err := s.Start(t.Context()); err != nil { + t.Fatal(err) + } + read, err := s.User(t.Context(), user.ID) + if err != nil || read.ID != user.ID || read.CreatedAt.Location() != time.UTC { + t.Fatalf("migration changed data or UTC: %v", err) + } + if err := s.Ping(t.Context()); err != nil { + t.Fatal(err) + } +} + +// TestRegistrationRace verifies database uniqueness under concurrent registration. +func TestRegistrationRace(t *testing.T) { + s := testStore(t) + outcomes := make(chan error, 12) + var wg sync.WaitGroup + for range 12 { + wg.Go(func() { + _, err := s.Register(t.Context(), dto.UserDTO{ID: uuid.New(), Username: "same", DisplayName: "Test"}, "hash") + outcomes <- err + }) + } + wg.Wait() + close(outcomes) + successes := 0 + for err := range outcomes { + if err == nil { + successes++ + } else if !errors.Is(err, store.ErrConflict) { + t.Fatal(err) + } + } + if successes != 1 { + t.Fatalf("got %d successful registrations", successes) + } +} + +// TestCreateChatRollsBack verifies failure of membership insertion leaves no chat. +func TestCreateChatRollsBack(t *testing.T) { + s := testStore(t) + id := uuid.New() + _, err := s.CreateChat(t.Context(), dto.ChatDTO{ID: id, Title: "Rollback", MemberIDs: []uuid.UUID{uuid.New()}}) + if err == nil { + t.Fatal("foreign key failure absent") + } + if _, err := s.Chat(t.Context(), id); !errors.Is(err, store.ErrNotFound) { + t.Fatalf("chat survived rollback: %v", err) + } +} + +// TestChatPageUsesMembershipAndCursor verifies one compound visibility/page invariant. +func TestChatPageUsesMembershipAndCursor(t *testing.T) { + s := testStore(t) + owner := fixtureUser(t, s) + outsider := fixtureUser(t, s) + first := fixtureChat(t, s, owner) + second := fixtureChat(t, s, owner) + fixtureChat(t, s, outsider) + page, err := s.Chats(t.Context(), owner.ID, dto.PositionDTO{CreatedAt: first.CreatedAt, ID: first.ID}, 10) + if err != nil || len(page) != 1 || page[0].ID != second.ID || len(page[0].MemberIDs) != 1 { + t.Fatalf("invalid membership page: %+v %v", page, err) + } +} + +// TestMessagePageOrdersConcurrentWrites verifies monotonic durable ordering and continuation. +func TestMessagePageOrdersConcurrentWrites(t *testing.T) { + s := testStore(t) + user := fixtureUser(t, s) + chat := fixtureChat(t, s, user) + var wg sync.WaitGroup + failures := make(chan error, 20) + for range 20 { + wg.Go(func() { + _, err := s.CreateMessage(t.Context(), dto.MessageDTO{ID: uuid.New(), ChatID: chat.ID, SenderID: user.ID, Text: "synthetic"}) + failures <- err + }) + } + wg.Wait() + close(failures) + for err := range failures { + if err != nil { + t.Fatal(err) + } + } + page, err := s.Messages(t.Context(), chat.ID, dto.PositionDTO{}, 10) + if err != nil || len(page) != 10 { + t.Fatalf("first page: %d %v", len(page), err) + } + for index := 1; index < len(page); index++ { + if !page[index].CreatedAt.After(page[index-1].CreatedAt) { + t.Fatal("timestamps are not monotonic") + } + } + last := page[len(page)-1] + next, err := s.Messages(t.Context(), chat.ID, dto.PositionDTO{CreatedAt: last.CreatedAt, ID: last.ID}, 20) + if err != nil || len(next) != 10 || !next[0].CreatedAt.After(last.CreatedAt) { + t.Fatalf("continuation: %d %v", len(next), err) + } +} + +// TestCredentialsReadsStoredHash checks credential lookup independently of public profiles. +func TestCredentialsReadsStoredHash(t *testing.T) { + s := testStore(t) + user := fixtureUser(t, s) + got, hash, err := s.Credentials(t.Context(), user.Username) + if err != nil || got.ID != user.ID || hash != "test-hash" { + t.Fatalf("credentials mismatch: %v", err) + } +} + +// TestUsersExistChecksAllIDs verifies the set membership contract. +func TestUsersExistChecksAllIDs(t *testing.T) { + for _, missing := range []bool{false, true} { + t.Run(map[bool]string{false: "all exist", true: "missing member"}[missing], func(t *testing.T) { + s := testStore(t) + ids := []uuid.UUID{fixtureUser(t, s).ID} + if missing { + ids = append(ids, uuid.New()) + } + exists, err := s.UsersExist(t.Context(), ids) + if err != nil || exists == missing { + t.Fatalf("exists=%v error=%v", exists, err) + } + }) + } +} + +// TestSessionExpiry rejects persisted sessions whose absolute deadline has passed. +func TestSessionExpiry(t *testing.T) { + s := testStore(t) + user := fixtureUser(t, s) + if err := s.SaveSession(t.Context(), "expired", user.ID, time.Now().Add(-time.Second)); err != nil { + t.Fatal(err) + } + if _, err := s.SessionUser(t.Context(), "expired"); !errors.Is(err, store.ErrNotFound) { + t.Fatalf("expired session accepted: %v", err) + } +} + +// TestSessionRevocation removes a still-valid session from the source of truth. +func TestSessionRevocation(t *testing.T) { + s := testStore(t) + user := fixtureUser(t, s) + if err := s.SaveSession(t.Context(), "live", user.ID, time.Now().Add(time.Hour)); err != nil { + t.Fatal(err) + } + if err := s.RevokeSession(t.Context(), "live"); err != nil { + t.Fatal(err) + } + if _, err := s.SessionUser(t.Context(), "live"); !errors.Is(err, store.ErrNotFound) { + t.Fatalf("revoked session accepted: %v", err) + } +} + +// TestSessionResolvesOwner verifies a live session's immutable identity. +func TestSessionResolvesOwner(t *testing.T) { + s := testStore(t) + user := fixtureUser(t, s) + if err := s.SaveSession(t.Context(), "live", user.ID, time.Now().Add(time.Hour)); err != nil { + t.Fatal(err) + } + id, err := s.SessionUser(t.Context(), "live") + if err != nil || id != user.ID { + t.Fatalf("invalid owner: %v", err) + } +} + +// TestNewRejectsInvalidOptions verifies pool construction errors without connecting. +func TestNewRejectsInvalidOptions(t *testing.T) { + for _, options := range []Options{{}, {URL: "://invalid", MaxConnections: 1, Timeout: time.Second}} { + if _, err := New(options); err == nil { + t.Fatal("invalid options accepted") + } + } +} + +// TestCreateMessageMissingChat reports a typed missing-record error. +func TestCreateMessageMissingChat(t *testing.T) { + s := testStore(t) + _, err := s.CreateMessage(t.Context(), dto.MessageDTO{ID: uuid.New(), ChatID: uuid.New(), SenderID: uuid.New(), Text: "test"}) + if !errors.Is(err, store.ErrNotFound) { + t.Fatal(err) + } +} + +// TestMessagesCanceledReturnsError verifies bounded cancellation before query execution. +func TestMessagesCanceledReturnsError(t *testing.T) { + s := testStore(t) + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if _, err := s.Messages(ctx, uuid.New(), dto.PositionDTO{}, 10); err == nil { + t.Fatal("canceled query succeeded") + } +} + +// TestChatsCanceledReturnsError verifies bounded cancellation of membership pages. +func TestChatsCanceledReturnsError(t *testing.T) { + s := testStore(t) + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if _, err := s.Chats(ctx, uuid.New(), dto.PositionDTO{}, 10); err == nil { + t.Fatal("canceled query succeeded") + } +} + +// TestMigrationCancellationReturnsError checks startup cancellation before migration work. +func TestMigrationCancellationReturnsError(t *testing.T) { + s := testStore(t) + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if err := s.Start(ctx); err == nil { + t.Fatal("canceled migration succeeded") + } +} + +// TestMigrationFailureRollsBackVersion verifies a failed migration is never marked applied. +func TestMigrationFailureRollsBackVersion(t *testing.T) { + s := testStore(t) + if _, err := s.pool.Exec(t.Context(), "DELETE FROM schema_migrations"); err != nil { + t.Fatal(err) + } + if err := s.Start(t.Context()); err == nil { + t.Fatal("conflicting unversioned schema accepted") + } + var count int + if err := s.pool.QueryRow(t.Context(), "SELECT count(*) FROM schema_migrations").Scan(&count); err != nil { + t.Fatal(err) + } + if count != 0 { + t.Fatal("failed migration version committed") + } +} + +// TestMigrationRejectsBrokenVersionLedger verifies corrupt migration metadata stops startup safely. +func TestMigrationRejectsBrokenVersionLedger(t *testing.T) { + s := testStore(t) + if _, err := s.pool.Exec(t.Context(), "ALTER TABLE schema_migrations RENAME COLUMN version TO broken_version"); err != nil { + t.Fatal(err) + } + if err := s.Start(t.Context()); err == nil { + t.Fatal("broken version ledger accepted") + } +} diff --git a/internal/store/postgres/users.go b/internal/store/postgres/users.go new file mode 100644 index 0000000..ecff7d7 --- /dev/null +++ b/internal/store/postgres/users.go @@ -0,0 +1,58 @@ +package postgres + +import ( + "context" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// CreateUser inserts a profile and enforces uniqueness inside PostgreSQL. +func (s *Store) CreateUser(ctx context.Context, user dto.UserDTO) (dto.UserDTO, error) { + return s.Register(ctx, user, "") +} + +// Register atomically stores a profile and its password hash. +func (s *Store) Register(ctx context.Context, user dto.UserDTO, passwordHash string) (dto.UserDTO, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + err := s.pool.QueryRow(ctx, `INSERT INTO users(id,username,display_name,password_hash) + VALUES(@id,@username,@display_name,@password_hash) RETURNING created_at`, pgx.StrictNamedArgs{ + "id": user.ID, + "username": user.Username, + "display_name": user.DisplayName, + "password_hash": passwordHash, + }).Scan(&user.CreatedAt) + return user, normalize(err) +} + +// User reads a profile without credentials. +func (s *Store) User(ctx context.Context, id uuid.UUID) (dto.UserDTO, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + var user dto.UserDTO + err := s.pool.QueryRow(ctx, `SELECT id,username,display_name,created_at FROM users WHERE id=@id`, pgx.StrictNamedArgs{"id": id}).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt) + return user, normalize(err) +} + +// Credentials reads the account needed for password verification. +func (s *Store) Credentials(ctx context.Context, username string) (dto.UserDTO, string, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + var user dto.UserDTO + var hash string + err := s.pool.QueryRow(ctx, `SELECT id,username,display_name,created_at,password_hash FROM users WHERE username=@username`, pgx.StrictNamedArgs{"username": username}).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &hash) + return user, hash, normalize(err) +} + +// UsersExist checks all distinct requested identifiers in one bounded query. +func (s *Store) UsersExist(ctx context.Context, ids []uuid.UUID) (bool, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + var exists bool + err := s.pool.QueryRow(ctx, `SELECT NOT EXISTS (SELECT id FROM unnest(@ids::uuid[]) AS requested(id) + WHERE NOT EXISTS (SELECT 1 FROM users WHERE users.id=requested.id))`, pgx.StrictNamedArgs{"ids": ids}).Scan(&exists) + return exists, normalize(err) +} diff --git a/internal/strictjson/decode.go b/internal/strictjson/decode.go new file mode 100644 index 0000000..ceef25b --- /dev/null +++ b/internal/strictjson/decode.go @@ -0,0 +1,101 @@ +// Package strictjson decodes objects with an exact set of required, non-null fields. +package strictjson + +import ( + "bytes" + "encoding/json" + "errors" + "io" + "slices" + "unicode/utf8" +) + +// ErrInvalid indicates invalid JSON, encoding, fields or destination field types. +var ErrInvalid = errors.New("invalid JSON object") + +// DecodeObject decodes one UTF-8 object with exactly the listed required fields. +// Duplicate, unknown, missing and null fields are rejected before unmarshaling. +// The destination must describe the supplied fields; this is not a general schema validator. +func DecodeObject(body []byte, target any, requiredFields ...string) error { + if !utf8.Valid(body) { + return ErrInvalid + } + if err := validateObject(body, requiredFields); err != nil { + return err + } + if err := json.Unmarshal(body, target); err != nil { + return ErrInvalid + } + return nil +} + +// validateObject enforces exact, unique, non-null required keys and one JSON value. +// All lab1 request properties are required; this is not a generic schema validator. +func validateObject(body []byte, requiredFields []string) error { + return validateSchema(body, requiredFields, requiredFields) +} + +// validateSchema distinguishes required keys from optional allowed keys. +func validateSchema(body []byte, requiredFields, allowedFields []string) error { + decoder := json.NewDecoder(bytes.NewReader(body)) + token, err := decoder.Token() + if err != nil || token != json.Delim('{') { + return ErrInvalid + } + seen := make(map[string]bool, len(requiredFields)) + for decoder.More() { + key, err := readObjectField(decoder, allowedFields, seen) + if err != nil { + return err + } + seen[key] = true + } + if token, err = decoder.Token(); err != nil || token != json.Delim('}') { + return ErrInvalid + } + for _, key := range requiredFields { + if !seen[key] { + return ErrInvalid + } + } + var trailing any + if err := decoder.Decode(&trailing); err != io.EOF { + return ErrInvalid + } + return nil +} + +// readObjectField reads a known, unique, non-null JSON object field. +func readObjectField(decoder *json.Decoder, requiredFields []string, seen map[string]bool) (string, error) { + token, err := decoder.Token() + if err != nil { + return "", ErrInvalid + } + key, ok := token.(string) + if !ok || seen[key] || !slices.Contains(requiredFields, key) { + return "", ErrInvalid + } + var value json.RawMessage + if err := decoder.Decode(&value); err != nil { + return "", ErrInvalid + } + if bytes.Equal(bytes.TrimSpace(value), []byte("null")) { + return "", ErrInvalid + } + return key, nil +} + +// DecodeOptional rejects unknown, duplicate and null fields while allowing omitted optional fields. +func DecodeOptional(body []byte, target any, required, optional []string) error { + if !utf8.Valid(body) { + return ErrInvalid + } + allowed := append(append([]string{}, required...), optional...) + if err := validateSchema(body, required, allowed); err != nil { + return err + } + if err := json.Unmarshal(body, target); err != nil { + return ErrInvalid + } + return nil +} diff --git a/internal/strictjson/decode_test.go b/internal/strictjson/decode_test.go new file mode 100644 index 0000000..5c3b428 --- /dev/null +++ b/internal/strictjson/decode_test.go @@ -0,0 +1,52 @@ +package strictjson + +import ( + "errors" + "testing" +) + +type input struct { + // Name contains the only accepted field in these fixed-schema tests. + Name string `json:"name"` +} + +// TestDecodeObject checks each fixed-schema rule with a fresh destination. +func TestDecodeObject(t *testing.T) { + cases := []struct { + name string + body string + want string + wantErr error + }{ + {"Positive: Unicode preserved", `{"name":"Алиса 👋"}`, "Алиса 👋", nil}, + {"Negative: missing field", `{}`, "", ErrInvalid}, + {"Negative: unknown field", `{"name":"alice","extra":true}`, "", ErrInvalid}, + {"Negative: duplicate field", `{"name":"alice","name":"bob"}`, "", ErrInvalid}, + {"Negative: case mismatch", `{"Name":"alice"}`, "", ErrInvalid}, + {"Negative: null field", `{"name":null}`, "", ErrInvalid}, + {"Negative: wrong value type", `{"name":12}`, "", ErrInvalid}, + {"Negative: invalid UTF-8", "{\"name\":\"\xff\"}", "", ErrInvalid}, + {"Negative: array root", `[]`, "", ErrInvalid}, + {"Negative: unclosed object", `{"name":"alice"`, "", ErrInvalid}, + {"Negative: invalid value", `{"name":}`, "", ErrInvalid}, + {"Negative: invalid key", `{name:"alice"}`, "", ErrInvalid}, + {"Negative: trailing value", `{"name":"alice"} {}`, "", ErrInvalid}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Arrange. + var destination input + + // Act. + err := DecodeObject([]byte(tc.body), &destination, "name") + + // Assert. + if !errors.Is(err, tc.wantErr) { + t.Fatalf("error=%v, want=%v", err, tc.wantErr) + } + if destination.Name != tc.want { + t.Fatalf("decoded name=%q, want=%q", destination.Name, tc.want) + } + }) + } +} diff --git a/internal/strictjson/optional_test.go b/internal/strictjson/optional_test.go new file mode 100644 index 0000000..af0ad5d --- /dev/null +++ b/internal/strictjson/optional_test.go @@ -0,0 +1,33 @@ +package strictjson + +import "testing" + +// TestDecodeOptional distinguishes omission from explicit null and duplicate fields. +func TestDecodeOptional(t *testing.T) { + cases := []struct { + name string + body string + valid bool + }{ + {"omitted", `{"text":"ok"}`, true}, + {"provided", `{"text":"ok","attachment_ids":["a"]}`, true}, + {"null", `{"text":"ok","attachment_ids":null}`, false}, + {"unknown", `{"text":"ok","sender_id":"a"}`, false}, + {"duplicate", `{"text":"ok","text":"other"}`, false}, + {"missing required", `{"attachment_ids":[]}`, false}, + {"wrong type", `{"text":"ok","attachment_ids":12}`, false}, + {"invalid utf8", string([]byte{255}), false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var target struct { + Text string `json:"text"` + IDs []string `json:"attachment_ids"` + } + err := DecodeOptional([]byte(tc.body), &target, []string{"text"}, []string{"attachment_ids"}) + if (err == nil) != tc.valid { + t.Fatalf("validation: %v", err) + } + }) + } +} diff --git a/internal/valkey/admission.go b/internal/valkey/admission.go new file mode 100644 index 0000000..506bbc9 --- /dev/null +++ b/internal/valkey/admission.go @@ -0,0 +1,132 @@ +package valkey + +import ( + "context" + "fmt" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/admission" +) + +const ( + admissionWindow = time.Second + admissionLease = 30 * time.Second + releaseTimeout = time.Second +) + +// Budget defines a rate window and concurrent execution capacity. +type Budget struct { + // Rate is the maximum attempts per one-second window. + Rate int + // Slots is the maximum requests executing concurrently. + Slots int +} + +// Limits separates global, route-group and user budgets. +type Limits struct { + // Global covers every business request before authentication. + Global Budget + // Groups covers auth, read and write independently. + Groups map[string]Budget + // User covers all authenticated requests made by one account. + User Budget + // UserGroups limits each authenticated account within a route group. + UserGroups map[string]Budget +} + +// Limiter applies atomic local-shard decisions; global reservations precede user reservations. +type Limiter struct { + client *Client + limits Limits +} + +// NewLimiter validates independent rate and concurrency budgets. +func NewLimiter(client *Client, limits Limits) (*Limiter, error) { + if client == nil || limits.Global.Rate < 1 || limits.Global.Slots < 1 || limits.User.Rate < 1 || limits.User.Slots < 1 { + return nil, fmt.Errorf("client and positive budgets are required") + } + for _, group := range []string{"auth", "read", "write"} { + global := limits.Groups[group] + user := limits.UserGroups[group] + if global.Rate < 1 || global.Slots < 1 || user.Rate < 1 || user.Slots < 1 { + return nil, fmt.Errorf("positive budgets for every group are required") + } + } + groups := make(map[string]Budget, len(limits.Groups)) + userGroups := make(map[string]Budget, len(limits.UserGroups)) + for key, value := range limits.Groups { + groups[key] = value + } + for key, value := range limits.UserGroups { + userGroups[key] = value + } + limits.Groups = groups + limits.UserGroups = userGroups + return &Limiter{client: client, limits: limits}, nil +} + +// Enter atomically reserves two rate and slot budgets on one shard. +// Global admission precedes authentication; user admission follows verified identity. +func (l *Limiter) Enter(ctx context.Context, group, user string) (func(), error) { + groupBudget, ok := l.limits.Groups[group] + if !ok { + return nil, fmt.Errorf("unknown admission group") + } + totalBudget := l.limits.Global + node := l.client.coordinator + prefix := "admission:global" + if user != "" { + prefix = "admission:user:" + user + node = l.client.nodes[l.client.ring.Owner(prefix)] + totalBudget = l.limits.User + groupBudget = l.limits.UserGroups[group] + } + keys := []string{prefix + ":rate", prefix + ":slots", prefix + ":" + group + ":rate", prefix + ":" + group + ":slots"} + token := uuid.NewString() + ctx, cancel := context.WithTimeout(ctx, l.client.timeout) + defer cancel() + result, err := node.Eval(ctx, reserveScript, keys, token, admissionWindow.Milliseconds(), admissionLease.Milliseconds(), totalBudget.Rate, totalBudget.Slots, groupBudget.Rate, groupBudget.Slots).Int() + if err != nil { + return nil, err + } + if result == 1 { + return nil, admission.ErrRate + } + if result == 2 { + return nil, admission.ErrSlots + } + return func() { + releaseCtx, releaseCancel := context.WithTimeout(context.Background(), releaseTimeout) + defer releaseCancel() + // Failed release is bounded by the lease; tokens are never reused. + _ = node.Eval(releaseCtx, releaseScript, []string{keys[1], keys[3]}, token).Err() + }, nil +} + +// Redis TIME and one Lua operation make check-and-reserve indivisible within a shard. +const reserveScript = ` +local clock = redis.call('TIME') +local now = clock[1]*1000 + math.floor(clock[2]/1000) +for i=1,3,2 do + redis.call('ZREMRANGEBYSCORE',KEYS[i+1],'-inf',now) + local rate = tonumber(redis.call('GET',KEYS[i]) or '0') + local limitIndex = i==1 and 4 or 6 + if rate >= tonumber(ARGV[limitIndex]) then return 1 end + if redis.call('ZCARD',KEYS[i+1]) >= tonumber(ARGV[limitIndex+1]) then return 2 end +end +for i=1,3,2 do + local count = redis.call('INCR',KEYS[i]) + if count==1 then redis.call('PEXPIRE',KEYS[i],ARGV[2]) end + redis.call('ZADD',KEYS[i+1],now+tonumber(ARGV[3]),ARGV[1]) + redis.call('PEXPIRE',KEYS[i+1],ARGV[3]) +end +return 0 +` + +const releaseScript = ` +redis.call('ZREM',KEYS[1],ARGV[1]) +redis.call('ZREM',KEYS[2],ARGV[1]) +return 0 +` diff --git a/internal/valkey/cache.go b/internal/valkey/cache.go new file mode 100644 index 0000000..84f3ad2 --- /dev/null +++ b/internal/valkey/cache.go @@ -0,0 +1,42 @@ +package valkey + +import ( + "context" + "encoding/json" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +const profileTTL = 5 * time.Minute + +// GetProfile reads an immutable profile; corrupt values and outages are cache misses. +func (c *Client) GetProfile(ctx context.Context, id uuid.UUID) (dto.UserDTO, bool) { + ctx, cancel := context.WithTimeout(ctx, c.timeout) + defer cancel() + key := "profile:" + id.String() + value, err := c.nodes[c.ring.Owner(key)].Get(ctx, key).Bytes() + if err != nil { + return dto.UserDTO{}, false + } + var user dto.UserDTO + if json.Unmarshal(value, &user) != nil || user.ID != id { + return dto.UserDTO{}, false + } + return user, true +} + +// PutProfile populates the immutable profile cache with a bounded TTL. +func (c *Client) PutProfile(ctx context.Context, user dto.UserDTO) { + ctx, cancel := context.WithTimeout(ctx, c.timeout) + defer cancel() + value, err := json.Marshal(user) + if err != nil { + return + } + key := "profile:" + user.ID.String() + // Cache writes are best effort; PostgreSQL remains the source of truth. + _ = c.nodes[c.ring.Owner(key)].Set(ctx, key, value, profileTTL).Err() +} diff --git a/internal/valkey/client.go b/internal/valkey/client.go new file mode 100644 index 0000000..6885969 --- /dev/null +++ b/internal/valkey/client.go @@ -0,0 +1,113 @@ +// Package valkey implements sharded cache and admission infrastructure. +package valkey + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "sort" + "strings" + "time" + + "github.com/redis/go-redis/v9" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/hashring" +) + +const virtualNodes = 256 + +// Options bounds each independent Valkey connection pool. +type Options struct { + // Nodes maps stable shard identities to network addresses. + Nodes map[string]string + // Coordinator identifies the stable global admission shard. + Coordinator string + // Timeout bounds operations and pool acquisition. + Timeout time.Duration + // PoolSize bounds connections to each shard. + PoolSize int +} + +// Client owns a fixed topology; callers must drain before changing it. +type Client struct { + nodes map[string]*redis.Client + ring *hashring.Ring + coordinator *redis.Client + timeout time.Duration + topology string +} + +// New prepares lazy clients without opening network connections. +func New(options Options) (*Client, error) { + if len(options.Nodes) < 1 || options.PoolSize < 1 || options.Timeout <= 0 { + return nil, fmt.Errorf("valkey nodes, pool bound and timeout are required") + } + ids := make([]string, 0, len(options.Nodes)) + for id, address := range options.Nodes { + if address == "" { + return nil, fmt.Errorf("valkey address is required") + } + ids = append(ids, id) + } + ring, err := hashring.New(ids, virtualNodes) + if err != nil { + return nil, err + } + if _, ok := options.Nodes[options.Coordinator]; !ok { + return nil, fmt.Errorf("coordinator must identify a configured node") + } + sort.Strings(ids) + topologyHash := sha256.Sum256([]byte(strings.Join(ids, ","))) + client := &Client{nodes: make(map[string]*redis.Client), ring: ring, timeout: options.Timeout} + for id, address := range options.Nodes { + client.nodes[id] = redis.NewClient(&redis.Options{ + Addr: address, + PoolSize: options.PoolSize, + MaxActiveConns: options.PoolSize, + PoolTimeout: options.Timeout, + DialTimeout: options.Timeout, + ReadTimeout: options.Timeout, + WriteTimeout: options.Timeout, + ContextTimeoutEnabled: true, + MaxRetries: -1, + }) + } + client.topology = hex.EncodeToString(topologyHash[:]) + client.coordinator = client.nodes[options.Coordinator] + return client, nil +} + +// Ping checks every shard required for safe admission. +func (c *Client) Ping(ctx context.Context) error { + ctx, cancel := context.WithTimeout(ctx, c.timeout) + defer cancel() + for _, node := range c.nodes { + if err := node.Ping(ctx).Err(); err != nil { + return err + } + } + stored, err := c.coordinator.Eval(ctx, topologyScript, []string{"admission:topology"}, c.topology).Text() + if err != nil { + return err + } + if stored != c.topology { + return fmt.Errorf("valkey topology differs: stop admission and drain before changing the ring") + } + return nil +} + +const topologyScript = ` +redis.call('SET',KEYS[1],ARGV[1],'NX') +return redis.call('GET',KEYS[1]) +` + +// Close releases every connection pool owned by the client. +func (c *Client) Close() error { + var result error + for _, node := range c.nodes { + result = errors.Join(result, node.Close()) + } + return result +} diff --git a/internal/valkey/client_test.go b/internal/valkey/client_test.go new file mode 100644 index 0000000..4094e71 --- /dev/null +++ b/internal/valkey/client_test.go @@ -0,0 +1,247 @@ +package valkey + +import ( + "context" + "errors" + "os" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/google/uuid" + + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/admission" + "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto" +) + +// testClient connects only to explicitly configured disposable Valkey shards. +func testClient(t *testing.T) *Client { + t.Helper() + address := os.Getenv("TEST_VALKEY_ADDRESS") + if address == "" { + t.Skip("TEST_VALKEY_ADDRESS is required for Valkey integration tests") + } + client, err := New(Options{Nodes: map[string]string{"test": address}, Coordinator: "test", PoolSize: 16, Timeout: time.Second}) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := client.Close(); err != nil { + t.Error(err) + } + }) + if err := client.Ping(t.Context()); err != nil { + t.Fatal(err) + } + return client +} + +// testLimits builds independent small budgets for admission scenarios. +func testLimits() Limits { + return Limits{ + Global: Budget{Rate: 10000, Slots: 100}, + User: Budget{Rate: 1000, Slots: 3}, + Groups: map[string]Budget{ + "auth": {Rate: 100, Slots: 4}, + "read": {Rate: 1000, Slots: 50}, + "write": {Rate: 1000, Slots: 50}, + }, + UserGroups: map[string]Budget{ + "auth": {Rate: 10, Slots: 1}, + "read": {Rate: 100, Slots: 2}, + "write": {Rate: 100, Slots: 2}, + }, + } +} + +// TestPerUserSlotsAreAtomic verifies concurrent reservations cannot exceed the group budget. +func TestPerUserSlotsAreAtomic(t *testing.T) { + client := testClient(t) + limiter, err := NewLimiter(client, testLimits()) + if err != nil { + t.Fatal(err) + } + user := uuid.NewString() + var accepted atomic.Int32 + releases := make(chan func(), 30) + failures := make(chan error, 30) + var wg sync.WaitGroup + for range 30 { + wg.Go(func() { + release, err := limiter.Enter(t.Context(), "read", user) + if err == nil { + accepted.Add(1) + releases <- release + } else if !errors.Is(err, admission.ErrSlots) { + failures <- err + } + }) + } + wg.Wait() + close(releases) + close(failures) + for release := range releases { + release() + } + for err := range failures { + t.Error(err) + } + if accepted.Load() != 2 { + t.Fatalf("accepted %d, want exactly two", accepted.Load()) + } +} + +// TestUserGroupsHaveSeparateSlots verifies read saturation does not consume write-group capacity. +func TestUserGroupsHaveSeparateSlots(t *testing.T) { + client := testClient(t) + limiter, err := NewLimiter(client, testLimits()) + if err != nil { + t.Fatal(err) + } + user := uuid.NewString() + for range 2 { + release, err := limiter.Enter(t.Context(), "read", user) + if err != nil { + t.Fatal(err) + } + t.Cleanup(release) + } + release, err := limiter.Enter(t.Context(), "write", user) + if err != nil { + t.Fatal(err) + } + release() +} + +// TestUserRateSurvivesSlotRelease checks that release returns concurrency but not rate tokens. +func TestUserRateSurvivesSlotRelease(t *testing.T) { + client := testClient(t) + limits := testLimits() + limits.User.Rate = 1 + limiter, err := NewLimiter(client, limits) + if err != nil { + t.Fatal(err) + } + user := uuid.NewString() + release, err := limiter.Enter(t.Context(), "read", user) + if err != nil { + t.Fatal(err) + } + release() + if _, err := limiter.Enter(t.Context(), "read", user); !errors.Is(err, admission.ErrRate) { + t.Fatalf("rate budget reset after release: %v", err) + } +} + +// TestAdmissionRejectsUnavailableBackend verifies fail-closed behavior without network fallback. +func TestAdmissionRejectsUnavailableBackend(t *testing.T) { + client := testClient(t) + limiter, err := NewLimiter(client, testLimits()) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if _, err := limiter.Enter(ctx, "read", ""); err == nil { + t.Fatal("canceled backend operation admitted request") + } +} + +// TestGlobalAdmissionReleasesSlots verifies the coordinator path and release semantics. +func TestGlobalAdmissionReleasesSlots(t *testing.T) { + client := testClient(t) + limiter, err := NewLimiter(client, testLimits()) + if err != nil { + t.Fatal(err) + } + release, err := limiter.Enter(t.Context(), "read", "") + if err != nil { + t.Fatal(err) + } + release() +} + +// TestProfileRoundTrip checks storage and ownership of immutable cached profiles. +func TestProfileRoundTrip(t *testing.T) { + client := testClient(t) + user := dto.UserDTO{ID: uuid.New(), Username: "synthetic", DisplayName: "Test", CreatedAt: time.Now().UTC()} + client.PutProfile(t.Context(), user) + got, ok := client.GetProfile(t.Context(), user.ID) + if !ok || got.ID != user.ID || got.Username != user.Username { + t.Fatal("profile cache did not preserve account") + } +} + +// TestProfileMissHandlesCorruption verifies corrupt cache entries cannot impersonate another account. +func TestProfileMissHandlesCorruption(t *testing.T) { + client := testClient(t) + id := uuid.New() + key := "profile:" + id.String() + if err := client.nodes[client.ring.Owner(key)].Set(t.Context(), key, `{"ID":"00000000-0000-0000-0000-000000000000"}`, time.Minute).Err(); err != nil { + t.Fatal(err) + } + if _, ok := client.GetProfile(t.Context(), id); ok { + t.Fatal("corrupt profile accepted") + } +} + +// TestProfileBackendFailureIsMiss checks optional-cache fallback semantics. +func TestProfileBackendFailureIsMiss(t *testing.T) { + client := testClient(t) + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if _, ok := client.GetProfile(ctx, uuid.New()); ok { + t.Fatal("failed cache read treated as hit") + } + client.PutProfile(ctx, dto.UserDTO{ID: uuid.New()}) + if err := client.Ping(ctx); err == nil { + t.Fatal("canceled readiness succeeded") + } +} + +// TestClientRejectsInvalidConfiguration covers configuration boundaries without opening sockets. +func TestClientRejectsInvalidConfiguration(t *testing.T) { + cases := []Options{ + {}, + {Nodes: map[string]string{"a": ""}, Coordinator: "a", PoolSize: 1, Timeout: time.Second}, + {Nodes: map[string]string{"": "localhost:6379"}, Coordinator: "", PoolSize: 1, Timeout: time.Second}, + {Nodes: map[string]string{"a": "localhost:6379"}, Coordinator: "b", PoolSize: 1, Timeout: time.Second}, + } + for index, options := range cases { + t.Run(string(rune('a'+index)), func(t *testing.T) { + if _, err := New(options); err == nil { + t.Fatal("invalid client options accepted") + } + }) + } +} + +// TestLimiterRejectsInvalidConfiguration verifies policy completeness. +func TestLimiterRejectsInvalidConfiguration(t *testing.T) { + if _, err := NewLimiter(nil, testLimits()); err == nil { + t.Fatal("nil client accepted") + } + client := testClient(t) + limits := testLimits() + delete(limits.Groups, "auth") + if _, err := NewLimiter(client, limits); err == nil { + t.Fatal("missing group accepted") + } + limiter, err := NewLimiter(client, testLimits()) + if err != nil { + t.Fatal(err) + } + if _, err := limiter.Enter(t.Context(), "unknown", ""); err == nil { + t.Fatal("unknown group accepted") + } +} + +// TestTopologyMismatchFailsClosed prevents replicas from silently splitting user budgets. +func TestTopologyMismatchFailsClosed(t *testing.T) { + client := testClient(t) + client.topology = "different-ring" + if err := client.Ping(t.Context()); err == nil { + t.Fatal("incompatible topology accepted") + } +} diff --git a/scripts/coverage.awk b/scripts/coverage.awk new file mode 100644 index 0000000..982f844 --- /dev/null +++ b/scripts/coverage.awk @@ -0,0 +1,35 @@ +# Aggregate covered statements once per merged block; keep files with no statements as N/A. +FNR == NR { + split($0, fields, "\t") + paths[fields[1]] = fields[2] + hashes[fields[1]] = fields[3] + executable[fields[1]] = fields[4] + next +} +/^mode:/ { next } +{ + file = $1 + sub(/:[^:]*$/, "", file) + if (!(file in paths)) { + print "unknown coverage source: " file > "/dev/stderr" + invalid = 1 + } + total[file] += $2 + if ($3 > 0) covered[file] += $2 +} +END { + print "{\"threshold_per_file\":90,\"files\":[" > summary + separator = "" + for (file in paths) { + percent = total[file] ? sprintf("%.8f", covered[file] * 100 / total[file]) : "null" + printf "%s{\"file\":\"%s\",\"statements\":%d,\"covered\":%d,\"percent\":%s,\"sha256\":\"%s\"}", separator, paths[file], total[file], covered[file], percent, hashes[file] > summary + separator = ",\n" + printf "%8s %s\n", (total[file] ? sprintf("%.2f%%", covered[file] * 100 / total[file]) : "N/A"), paths[file] + if (covered[file] * 100 < total[file] * 90 || (executable[file] && !total[file])) invalid = 1 + } + printf "\n],\"passed\":%s}\n", (invalid ? "false" : "true") > summary + if (invalid) { + print "FAIL: per-file coverage must reach 90%" > "/dev/stderr" + exit 1 + } +} diff --git a/scripts/coverage.sh b/scripts/coverage.sh new file mode 100755 index 0000000..1eac1a4 --- /dev/null +++ b/scripts/coverage.sh @@ -0,0 +1,83 @@ +#!/bin/sh +# Measure unit and real-process coverage, enforcing 90% separately for each Go source file. +set -eu +cd "$(dirname "$0")/.." +GO=${GO:-go} +: "${DATABASE_URL:?Use an isolated test database}" "${VALKEY_SHARDS:?Use test shards}" +: "${TEST_DATABASE_URL:?Use an isolated test database}" "${TEST_VALKEY_ADDRESS:?Use a test shard}" +export APP_PORT=${COVERAGE_PORT:-18080} +export LOG_LEVEL=error +export CURSOR_SIGNING_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= +server_pid= +watchdog_pid= +mkdir -p coverage +run_dir=$(mktemp -d "$PWD/coverage/run.XXXXXX") +cleanup() { + if [ -n "$server_pid" ]; then kill "$server_pid" 2>/dev/null || true; wait "$server_pid" 2>/dev/null || true; fi + if [ -n "$watchdog_pid" ]; then kill "$watchdog_pid" 2>/dev/null || true; wait "$watchdog_pid" 2>/dev/null || true; fi + rm -rf "$run_dir" +} +trap cleanup EXIT +trap 'exit 129' HUP +trap 'exit 130' INT +trap 'exit 143' TERM +mkdir "$run_dir/process" +"$GO" build -race -cover -covermode=atomic -coverpkg=./... -o "$run_dir/server" ./cmd/server +"$run_dir/server" migrate +"$GO" test -count=1 -race -coverpkg=./... -coverprofile="$run_dir/unit.out" ./... +export GOCOVERDIR="$run_dir/process" +if "$run_dir/server" unknown >"$run_dir/server-invalid.log" 2>&1; then + echo 'server accepted an unknown command' >&2 + exit 1 +else + test "$?" -eq 1 +fi +"$run_dir/server" >"$run_dir/server.log" 2>&1 & +server_pid=$! +ready=false +for attempt in $(seq 1 60); do + kill -0 "$server_pid" 2>/dev/null || { cat "$run_dir/server.log"; exit 1; } + if curl --fail --silent --max-time 1 "http://localhost:$APP_PORT/health/ready" >/dev/null; then ready=true; break; fi + sleep .25 +done +$ready || { cat "$run_dir/server.log"; exit 1; } +"$run_dir/server" healthcheck +"$run_dir/server" migrate +"${PYTHON:-python3}" tests/smoke.py --base-url "http://localhost:$APP_PORT" +kill -TERM "$server_pid" +# Bound the real-process shutdown check; a forced kill makes wait fail. +(sleep 12; kill -KILL "$server_pid" 2>/dev/null || true) & +watchdog_pid=$! +wait "$server_pid" +server_pid= +kill "$watchdog_pid" 2>/dev/null || true +wait "$watchdog_pid" 2>/dev/null || true +watchdog_pid= +unset GOCOVERDIR +"$GO" tool covdata textfmt -i="$run_dir/process" -o="$run_dir/process.out" +awk ' +/^mode:/ { if ($0 != "mode: atomic") exit 1; next } +{ + if (($1 in statements) && statements[$1] != $2) exit 1; + statements[$1] = $2; hits[$1] += $3 +} +END { + print "mode: atomic"; + for (block in statements) print block, statements[block], hits[block] +}' "$run_dir/unit.out" "$run_dir/process.out" > coverage/coverage.out +"$GO" list -f '{{.ImportPath}} {{.Dir}} {{join .GoFiles ","}}' ./... > "$run_dir/packages.txt" +: > "$run_dir/inventory.tsv" +while read -r import_path directory names; do + old_ifs=$IFS + IFS=, + for name in $names; do + checksum=$(shasum -a 256 "$directory/$name") + checksum=${checksum%% *} + relative=${directory#"$PWD/"}/$name + executable=$(awk '/^func[ (]/ {found=1} END {print found+0}' "$directory/$name") + printf '%s\t%s\t%s\t%s\n' "$import_path/$name" "$relative" "$checksum" "$executable" >> "$run_dir/inventory.tsv" + done + IFS=$old_ifs +done < "$run_dir/packages.txt" +awk -v summary=coverage/summary.json -f scripts/coverage.awk "$run_dir/inventory.tsv" coverage/coverage.out +"$GO" version > coverage/go-version.txt diff --git a/scripts/init-secrets.sh b/scripts/init-secrets.sh new file mode 100755 index 0000000..c5f8231 --- /dev/null +++ b/scripts/init-secrets.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# Create lab credentials once, outside Helm values and release history. +set -eu +: "${KUBE_CONTEXT:=dev}" "${NAMESPACE:=highload-applications}" +: "${REGISTRY:=asisti.registry.twcstorage.ru}" "${REGISTRY_USER:=token}" +: "${REGISTRY_TOKEN_FILE:=$HOME/.secrets/twc-registry-token}" +test -s "$REGISTRY_TOKEN_FILE" || { echo 'Registry token file is missing or empty' >&2; exit 1; } +umask 077 +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +kubectl --context "$KUBE_CONTEXT" create namespace "$NAMESPACE" --dry-run=client -o yaml | kubectl --context "$KUBE_CONTEXT" apply -f - +if ! kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get secret messenger-secrets >/dev/null 2>&1; then + openssl rand -hex 32 | tr -d '\n' > "$work/postgres-password" + printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable' "$(cat "$work/postgres-password")" > "$work/database-url" + openssl rand -base64 32 | tr -d '\n' > "$work/cursor-key" + kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" create secret generic messenger-secrets --from-file="$work/postgres-password" --from-file="$work/database-url" --from-file="$work/cursor-key" +fi +# Add the shared cursor key to an existing lab2 secret without replacing database credentials. +if ! kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get secret messenger-secrets -o json | jq -e '.data["cursor-key"] | length > 0' >/dev/null; then + openssl rand -base64 32 | tr -d '\n' > "$work/cursor-key" + jq -n --rawfile key "$work/cursor-key" '{stringData:{"cursor-key":$key}}' > "$work/patch.json" + kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" patch secret messenger-secrets --type merge --patch-file "$work/patch.json" +fi +# Build a portable Docker config; macOS credential-helper references cannot be used by kubelet. +jq -n --arg registry "$REGISTRY" --arg user "$REGISTRY_USER" --rawfile token "$REGISTRY_TOKEN_FILE" \ + '{auths:{($registry):{auth:(($user+":"+($token | sub("[\\r\\n]+$"; "")))|@base64)}}}' > "$work/config.json" +kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" create secret generic registry-pull \ + --type=kubernetes.io/dockerconfigjson --from-file=".dockerconfigjson=$work/config.json" \ + --dry-run=client -o yaml | kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" apply -f - +if ! kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get secret messenger-s3 >/dev/null 2>&1; then + printf 'operator' > "$work/root-user" + printf 'messenger' > "$work/access-key" + openssl rand -hex 32 | tr -d '\n' > "$work/root-password" + openssl rand -hex 32 | tr -d '\n' > "$work/secret-key" +jq -n --rawfile admin "$work/root-user" --rawfile adminSecret "$work/root-password" --rawfile app "$work/access-key" --rawfile appSecret "$work/secret-key" '{identities:[{name:"operator",credentials:[{accessKey:$admin,secretKey:$adminSecret}],actions:["Admin","Read","Write","List"]},{name:"messenger",credentials:[{accessKey:$app,secretKey:$appSecret}],actions:["Read:attachments","Write:attachments","List:attachments"]}]}' > "$work/identities.json" + kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" create secret generic messenger-s3 --from-file="$work/root-user" --from-file="$work/root-password" --from-file="$work/access-key" --from-file="$work/secret-key" --from-file="$work/identities.json" +fi diff --git a/scripts/local-action.sh b/scripts/local-action.sh new file mode 100755 index 0000000..f73e07b --- /dev/null +++ b/scripts/local-action.sh @@ -0,0 +1,50 @@ +#!/bin/sh +# Operate only on the isolated processes and containers recorded by test-local.sh. +set -eu +exec 2>> evidence/local/actions-errors.txt +: "${LOCAL_TEST_WORK:?Run through scripts/test-local.sh}" +. "$LOCAL_TEST_WORK/environment" +stop_api() { + label=$1 + pid=$(cat "$LOCAL_TEST_WORK/$label.pid") + if kill -0 "$pid" 2>/dev/null; then + state=$(ps -p "$pid" -o stat=) + case "$state" in Z*) return ;; esac + command=$(ps -p "$pid" -o command=) + test "$command" = "$LOCAL_TEST_WORK/server" || { echo 'API PID ownership mismatch' >&2; exit 1; } + kill -TERM "$pid" + for attempt in $(seq 1 60); do kill -0 "$pid" 2>/dev/null || break; sleep .1; done + fi +} +start_api() { + label=$1 + port=$2 + INSTANCE_ID="$label" APP_PORT="$port" nohup "$LOCAL_TEST_WORK/server" > "$LOCAL_TEST_WORK/$label.log" 2>&1 < /dev/null & + echo "$!" > "$LOCAL_TEST_WORK/$label.pid" +} +if [ "${LOCAL_ACTION:-restart}" = failover ]; then + case "${TARGET_INSTANCE_ID:?Missing observed replica}" in local-a|local-b) ;; *) echo 'Unknown isolated replica' >&2; exit 1 ;; esac + printf "%s target=%s action=stop\n" "$(date -u +%FT%TZ)" "$TARGET_INSTANCE_ID" >> evidence/local/actions.txt + stop_api "$TARGET_INSTANCE_ID" + exit 0 +fi +stop_api local-a +stop_api local-b +printf "%s action=restart-api-postgres-valkey-s3 retained-container-filesystems\n" "$(date -u +%FT%TZ)" >> evidence/local/actions.txt +docker restart "$LOCAL_TEST_PG" "$LOCAL_TEST_VK" "$LOCAL_TEST_S3" >/dev/null +# Docker may assign new host ports when restarting containers with ephemeral bindings. +export DATABASE_URL="postgres://postgres@$(docker port "$LOCAL_TEST_PG" 5432/tcp)/messenger?sslmode=disable" +export VALKEY_SHARDS="shard0=$(docker port "$LOCAL_TEST_VK" 6379/tcp)" +export S3_ENDPOINT="$(docker port "$LOCAL_TEST_S3" 9000/tcp)" +for attempt in $(seq 1 60); do + if docker exec "$LOCAL_TEST_PG" pg_isready -U postgres >/dev/null 2>&1 && "$LOCAL_TEST_WORK/server" s3-bootstrap >/dev/null 2>&1; then break; fi + sleep 1 +done +start_api local-a "$LOCAL_TEST_PORT_A" +start_api local-b "$LOCAL_TEST_PORT_B" +for attempt in $(seq 1 60); do + if curl --fail --silent --max-time 1 "http://127.0.0.1:$LOCAL_TEST_PORT_A/health/ready" >/dev/null && curl --fail --silent --max-time 1 "http://127.0.0.1:$LOCAL_TEST_PORT_B/health/ready" >/dev/null; then exit 0; fi + sleep 1 +done +echo 'Isolated restart readiness timed out' >&2 +exit 1 diff --git a/scripts/measure-upload.sh b/scripts/measure-upload.sh new file mode 100755 index 0000000..02a03eb --- /dev/null +++ b/scripts/measure-upload.sh @@ -0,0 +1,19 @@ +#!/bin/sh +# Measure the owned local API process RSS while ten clients upload concurrently. +set -eu +: "${API_PID:?Pass the PID of this lab API}" "${BASE_URL:=http://127.0.0.1:18084}" +mkdir -p evidence/current +sampler= +cleanup() { + if [ -n "$sampler" ]; then kill "$sampler" 2>/dev/null || true; wait "$sampler" 2>/dev/null || true; fi +} +trap cleanup EXIT INT TERM +( + while kill -0 "$API_PID" 2>/dev/null; do + ps -o rss= -p "$API_PID" + sleep 0.1 + done +) > evidence/current/upload-rss-kib.txt & +sampler=$! +BASE_URL="$BASE_URL" k6 run --summary-export evidence/current/upload-summary.json student-tests/load/attachments.js > evidence/current/upload.txt 2>&1 +awk 'BEGIN {max=0} $1>max {max=$1} END {print "peak_rss_kib=" max}' evidence/current/upload-rss-kib.txt diff --git a/scripts/port-forward.sh b/scripts/port-forward.sh new file mode 100755 index 0000000..e8d7d13 --- /dev/null +++ b/scripts/port-forward.sh @@ -0,0 +1,21 @@ +#!/bin/sh +# Reconnect the loopback tunnel after the API pod is replaced during restart tests. +set -eu +: "${KUBE_CONTEXT:=dev}" "${NAMESPACE:=highload-applications}" +LOCAL_PORT=${LOCAL_PORT:-8083} +child= +cleanup() { + if [ -n "$child" ]; then + kill "$child" 2>/dev/null || true + wait "$child" 2>/dev/null || true + fi + exit 0 +} +trap cleanup INT TERM +while :; do + kubectl --context "$KUBE_CONTEXT" -n traefik port-forward service/traefik "$LOCAL_PORT:80" --address 127.0.0.1 & + child=$! + wait "$child" || true + child= + sleep 1 +done diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 0000000..e6abc6c --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,21 @@ +#!/bin/sh +# Restart only this lab's API and persistent stores, retaining every PVC. +set -eu +: "${KUBE_CONTEXT:=dev}" "${NAMESPACE:=highload-applications}" +# HPA must be disabled explicitly for this destructive test window. +if kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get hpa api >/dev/null 2>&1; then + echo 'Disable autoscaling in the lab release before the restart test, then restore it.' >&2 + exit 1 +fi +replicas=$(kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get deployment api -o jsonpath='{.spec.replicas}') +mkdir -p evidence +kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get pods -l 'app in (api,postgres,valkey,s3)' -o custom-columns=NAME:.metadata.name,UID:.metadata.uid > evidence/restart-before.txt +kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" scale deployment/api --replicas=0 +kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" wait --for=delete pod -l app=api --timeout=60s +kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" rollout restart statefulset/postgres statefulset/valkey statefulset/s3 +kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" rollout status statefulset/postgres --timeout=120s +kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" rollout status statefulset/valkey --timeout=120s +kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" rollout status statefulset/s3 --timeout=120s +kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" scale deployment/api --replicas="$replicas" +kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" rollout status deployment/api --timeout=120s +kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get pods -l 'app in (api,postgres,valkey,s3)' -o custom-columns=NAME:.metadata.name,UID:.metadata.uid > evidence/restart-after.txt diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 0000000..b06f823 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,10 @@ +#!/bin/sh +# Terminate the exact observed API pod; its replacement receives a different instance ID. +set -eu +: "${KUBE_CONTEXT:=dev}" "${NAMESPACE:=highload-applications}" "${TARGET_INSTANCE_ID:?Expected API pod name}" +case "$TARGET_INSTANCE_ID" in api-*) ;; *) echo 'Target is not a lab3 API pod' >&2; exit 2 ;; esac +label=$(kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get pod "$TARGET_INSTANCE_ID" -o jsonpath='{.metadata.labels.app}') +test "$label" = api +mkdir -p evidence +printf '%s target=%s\n' "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" "$TARGET_INSTANCE_ID" >> evidence/failover-targets.txt +kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" delete pod "$TARGET_INSTANCE_ID" --wait=false diff --git a/scripts/test-local.sh b/scripts/test-local.sh new file mode 100755 index 0000000..a90a2fa --- /dev/null +++ b/scripts/test-local.sh @@ -0,0 +1,101 @@ +#!/bin/sh +# Disposable integration harness; Helm remains the deployment source of truth. +set -eu +cd "$(dirname "$0")/.." +umask 077 +export LOCAL_TEST_WORK=$(mktemp -d) +containers= +cleanup() { + for label in local-a local-b; do + if [ -f "$LOCAL_TEST_WORK/$label.pid" ]; then kill -TERM "$(cat "$LOCAL_TEST_WORK/$label.pid")" 2>/dev/null || true; fi + done + for container in $containers; do docker rm -f "$container" >/dev/null; done + for label in local-a local-b; do if [ -f "$LOCAL_TEST_WORK/$label.log" ]; then cp "$LOCAL_TEST_WORK/$label.log" "evidence/local/$label.txt"; fi; done + rm -rf "$LOCAL_TEST_WORK" +} +trap cleanup EXIT +trap 'exit 130' INT TERM +mkdir -p evidence/local +pg=$(docker run -d -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=messenger -p 127.0.0.1::5432 postgres:17.6-alpine) +containers="$pg" +vk=$(docker run -d -p 127.0.0.1::6379 valkey/valkey:8.1.3-alpine) +containers="$containers $vk" +admin=$(openssl rand -hex 24) +app=$(openssl rand -hex 24) +jq -n --arg admin "$admin" --arg app "$app" '{identities:[{name:"operator",credentials:[{accessKey:"operator",secretKey:$admin}],actions:["Admin","Read","Write","List"]},{name:"messenger",credentials:[{accessKey:"messenger",secretKey:$app}],actions:["Read:attachments","Write:attachments","List:attachments"]}]}' > "$LOCAL_TEST_WORK/identities.json" +s3=$(docker run -d -p 127.0.0.1::9000 -v "$LOCAL_TEST_WORK/identities.json:/identity.json:ro" chrislusf/seaweedfs:3.85 server -dir=/data -s3 -s3.port=9000 -s3.config=/identity.json -master.volumeSizeLimitMB=100 -volume.max=5) +containers="$containers $s3" +port_a=$(ruby -rsocket -e 's=TCPServer.new("127.0.0.1",0);puts s.addr[1];s.close') +port_b=$(ruby -rsocket -e 's=TCPServer.new("127.0.0.1",0);puts s.addr[1];s.close') +cat > "$LOCAL_TEST_WORK/environment" </dev/null 2>&1; then break; fi + sleep 1 +done +"$LOCAL_TEST_WORK/server" migrate +if [ "${LOCAL_COVERAGE:-false}" = true ]; then + test_vk=$(docker run -d -p 127.0.0.1::6379 valkey/valkey:8.1.3-alpine) + containers="$containers $test_vk" + export TEST_DATABASE_URL="$DATABASE_URL" + export TEST_VALKEY_ADDRESS=$(docker port "$test_vk" 6379/tcp) + export S3_ADMIN_SECRET="$admin" + GOTOOLCHAIN=go1.26.1 scripts/coverage.sh + exit 0 +fi +for label in local-a local-b; do + port=$port_a + if [ "$label" = local-b ]; then port=$port_b; fi + INSTANCE_ID="$label" APP_PORT="$port" "$LOCAL_TEST_WORK/server" > "$LOCAL_TEST_WORK/$label.log" 2>&1 & + echo "$!" > "$LOCAL_TEST_WORK/$label.pid" +done +cat > "$LOCAL_TEST_WORK/nginx.conf" </dev/null; then break; fi + sleep 1 +done +make test > evidence/local/smoke.txt 2>&1 +LOCAL_ACTION=failover make test-failover ACTION_SCRIPT="$PWD/scripts/local-action.sh" > evidence/local/failover.txt 2>&1 +make test-persistence ACTION_SCRIPT="$PWD/scripts/local-action.sh" > evidence/local/persistence.txt 2>&1 +make test > evidence/local/smoke-after.txt 2>&1 +GOTOOLCHAIN=go1.26.1 go version > evidence/local/go-version.txt +shasum -a 256 "$LOCAL_TEST_WORK/server" > evidence/local/binary.sha256 +echo 'PASS: isolated smoke, attachment failover and persistence' diff --git a/student-tests/README.md b/student-tests/README.md new file mode 100644 index 0000000..b121260 --- /dev/null +++ b/student-tests/README.md @@ -0,0 +1,11 @@ +# Дополнительные проверки + +Публичные тесты остаются в `tests/`. Go-тесты расположены рядом с исходниками. + +- `make test-local`: отдельные PostgreSQL, Valkey, SeaweedFS и Nginx, два настоящих процесса API. Проверяет smoke, остановку обслужившей реплики и перезапуск всех учебных хранилищ с сохранением файловых систем контейнеров. После проверки удаляет только созданные им ресурсы. +- `make test-student`: race detector; интеграционные тесты требуют отдельные `TEST_DATABASE_URL`, `TEST_VALKEY_ADDRESS` и S3-настройки. Без них соответствующие проверки пропускаются. +- `make coverage`: объединяет Go-тесты и покрытие настоящего процесса, проверяет минимум 90% statements отдельно для каждого файла. Требует изолированные зависимости и `S3_ADMIN_SECRET` для теста политики bucket. +- `scripts/measure-upload.sh`: k6, десять пользователей и десять одновременных загрузок, измерение RSS указанного процесса API. Параметры и результаты — в `REPORT.md`. +- `go test -run TestMeasuredMissingInventory -v ./internal/store/postgres`: измерение dry-run сверки 100/1000 отсутствующих объектов в отдельной схеме. + +Результаты и ограничения: [REPORT.md](../REPORT.md). Отказы между S3 и SQL проверяются управляемыми ошибками зависимостей; публичный failover действительно останавливает процесс API. Это разные виды доказательств. diff --git a/student-tests/load/attachments.js b/student-tests/load/attachments.js new file mode 100644 index 0000000..5b2c784 --- /dev/null +++ b/student-tests/load/attachments.js @@ -0,0 +1,43 @@ +import http from 'k6/http'; +import encoding from 'k6/encoding'; +import {check, sleep} from 'k6'; +import {Counter, Trend} from 'k6/metrics'; + +const base = __ENV.BASE_URL || 'http://127.0.0.1:18084'; +const original = open('../../tests/fixtures/sample.png', 'b'); +const uploaded = new Counter('attachments_ready'); +const rejected = new Counter('attachments_rejected'); +const latency = new Trend('attachment_upload_ms', true); +export const options = {vus: 10, iterations: 100, setupTimeout: '60s', thresholds: {checks: ['rate==1'], attachments_ready: ['count>0']}}; + +export function setup() { + const actors = []; + for (let i = 0; i < 10; i++) { + const username = `a_${Date.now()}_${i}`; + const password = 'synthetic-attachment-password'; + const user = http.post(`${base}/api/v1/users`, JSON.stringify({username, display_name: 'Synthetic', password}), {headers: {'Content-Type': 'application/json'}}); + if (user.status !== 201) throw new Error(`registration: ${user.status}`); + const session = http.post(`${base}/api/v1/auth/sessions`, null, {headers: {Authorization: `Basic ${encoding.b64encode(username + ':' + password)}`}}); + if (session.status !== 201) throw new Error(`login: ${session.status}`); + const headers = {Authorization: `Bearer ${session.json('token')}`}; + const chat = http.post(`${base}/api/v1/chats`, JSON.stringify({title: 'Attachment experiment', member_ids: [user.json('id')]}), {headers: {...headers, 'Content-Type': 'application/json'}}); + if (chat.status !== 201) throw new Error(`chat: ${chat.status}`); + actors.push({headers, chat: chat.json('id')}); + } + return actors; +} + +export default function (actors) { + const actor = actors[(__VU - 1) % actors.length]; + const response = http.post(`${base}/api/v1/chats/${actor.chat}/attachments?filename=sample.png`, original, {headers: {...actor.headers, 'Content-Type': 'image/png'}}); + latency.add(response.timings.duration); + if (response.status === 201) { + uploaded.add(1); + const read = http.get(`${base}/api/v1/attachments/${response.json('id')}/content`, {headers: actor.headers, responseType: 'binary'}); + check(read, {'download complete': r => r.status === 200 && r.body.byteLength === original.byteLength}); + } else { + rejected.add(1); + check(response, {'bounded overload': r => r.status === 429 || r.status === 503}); + } + sleep(0.05); +} diff --git a/student-tests/load/failover.js b/student-tests/load/failover.js new file mode 100644 index 0000000..518ae1d --- /dev/null +++ b/student-tests/load/failover.js @@ -0,0 +1,61 @@ +import http from 'k6/http'; +import encoding from 'k6/encoding'; +import { sleep } from 'k6'; +export const options = { vus: 1, iterations: 1, setupTimeout: '120s' }; +const base = __ENV.BASE_URL; +const target = __ENV.TARGET_INSTANCE_ID; +export function setup() { + const username = 'fail_' + Date.now(); + const password = 'synthetic-failover-password'; + const user = http.post(base + '/api/v1/users', JSON.stringify({username, password, display_name:'Failover'}), {headers:{'Content-Type':'application/json'}}); + if (user.status !== 201) throw new Error('registration ' + user.status); + const session = http.post(base + '/api/v1/auth/sessions', null, {headers:{Authorization:'Basic ' + encoding.b64encode(username + ':' + password)}}); + if (session.status !== 201) throw new Error('login ' + session.status); + const headers = {Authorization:'Bearer ' + session.json().token, 'Content-Type':'application/json'}; + const chat = http.post(base + '/api/v1/chats', JSON.stringify({title:'Failover',member_ids:[user.json().id]}), {headers}); + if (chat.status !== 201) throw new Error('chat ' + chat.status); + const seen = new Set(); + for (let i=0; i<20; i++) { + const response = http.get(base + '/api/v1/users/me', {headers, timeout:'2s'}); + if (response.status !== 200) throw new Error('initial read ' + response.status); + seen.add(response.headers['X-Instance-Id']); + } + if (seen.size < 2 || !seen.has(target)) throw new Error('target not observed through LB'); + const path = base + '/api/v1/chats/' + chat.json().id + '/messages'; + const original = http.post(path, JSON.stringify({text:'before failure'}), {headers:{...headers,'Idempotency-Key':'before'}}); + if (original.status !== 201) throw new Error('initial message ' + original.status); + return {headers,path,original:original.json()}; +} +export default function(data) { + console.log('READY_TO_STOP ' + target); + const started = Date.now(); + const confirmed = new Set([data.original.id]); + let errors = 0, reads = 0, sequence = 0; + const events = []; + const lastInstances = []; + while (Date.now() - started < 25000) { + const response = http.post(data.path, JSON.stringify({text:'during-' + sequence}), {headers:{...data.headers,'Idempotency-Key':'during-' + sequence},timeout:'2s'}); + if (response.status === 201) { + confirmed.add(response.json().id); sequence++; + events.push({seconds:(Date.now()-started)/1000,instance:response.headers['X-Instance-Id']}); + lastInstances.push(response.headers['X-Instance-Id']); + } else errors++; + const read = http.get(data.path + '?limit=100', {headers:data.headers,timeout:'2s'}); + if (read.status === 200) reads++; else errors++; + sleep(0.1); + } + const items = []; + let path = data.path + '?limit=100'; + while (path) { + const response = http.get(path, {headers:data.headers,timeout:'5s'}); + if (response.status !== 200) throw new Error('final history ' + response.status); + items.push(...response.json().items); + const cursor = response.json().next_cursor; + path = cursor ? data.path + '?limit=100&cursor=' + encodeURIComponent(cursor) : null; + } + const ids = new Set(items.map(item=>item.id)); + if ([...confirmed].some(id=>!ids.has(id))) throw new Error('confirmed message lost'); + if (new Set(items.map(item=>item.text)).size !== items.length) throw new Error('uncertain retry duplicated'); + if (lastInstances.length < 5 || lastInstances.slice(-5).includes(target)) throw new Error('target still serves or insufficient writes'); + console.log(JSON.stringify({target,started_epoch_ms:started,confirmed:confirmed.size,reads,errors,events,pass:true})); +} diff --git a/student-tests/load/hpa.js b/student-tests/load/hpa.js new file mode 100644 index 0000000..97c915c --- /dev/null +++ b/student-tests/load/hpa.js @@ -0,0 +1,18 @@ +import http from 'k6/http'; +import encoding from 'k6/encoding'; +import { check, sleep } from 'k6'; +export const options = { vus: 2, duration: '90s', thresholds: { checks: ['rate>0.95'] } }; +const base = __ENV.BASE_URL || 'https://hl.agapov.space'; +export function setup() { + const username = `hpa_${Date.now()}`; + const password = 'synthetic-autoscaling-password'; + const response = http.post(`${base}/api/v1/users`, JSON.stringify({ username, display_name: 'HPA CPU test', password }), { headers: { 'Content-Type': 'application/json' } }); + if (response.status !== 201) throw new Error(`registration: ${response.status}`); + return { authorization: `Basic ${encoding.b64encode(`${username}:${password}`)}` }; +} +export default function (data) { + const response = http.post(`${base}/api/v1/auth/sessions`, null, { headers: { Authorization: data.authorization } }); + check(response, { 'authenticated login': r => r.status === 201 }); + // Keep one account below its 20 requests/s auth budget. + sleep(0.1); +} diff --git a/student-tests/load/idempotency.js b/student-tests/load/idempotency.js new file mode 100644 index 0000000..6097e4c --- /dev/null +++ b/student-tests/load/idempotency.js @@ -0,0 +1,34 @@ +import http from 'k6/http'; +import encoding from 'k6/encoding'; +import { check, sleep } from 'k6'; +export const options = { vus: 8, iterations: 32, thresholds: { checks: ['rate==1'] } }; +const base = __ENV.BASE_URL || 'https://hl.agapov.space'; +export function setup() { + const username = `idem_${Date.now()}`; + const password = 'synthetic-idempotency-password'; + const registered = http.post(`${base}/api/v1/users`, JSON.stringify({ username, display_name: 'Idempotency', password }), { headers: { 'Content-Type': 'application/json' } }); + if (registered.status !== 201) throw new Error(`registration ${registered.status}`); + const session = http.post(`${base}/api/v1/auth/sessions`, null, { headers: { Authorization: `Basic ${encoding.b64encode(`${username}:${password}`)}` } }); + if (session.status !== 201) throw new Error(`login ${session.status}`); + const headers = { Authorization: `Bearer ${session.json().token}`, 'Content-Type': 'application/json', 'Idempotency-Key': 'shared-key' }; + const chat = http.post(`${base}/api/v1/chats`, JSON.stringify({ title: 'Retry race', member_ids: [registered.json().id] }), { headers }); + if (chat.status !== 201) throw new Error(`chat ${chat.status}`); + return { headers, path: `${base}/api/v1/chats/${chat.json().id}/messages` }; +} +export default function (data) { + // Eight writers share four user slots. Retry admission failures with the same key/body. + let response; + for (let attempt = 0; attempt < 20; attempt++) { + response = http.post(data.path, JSON.stringify({ text: 'one logical message' }), { headers: data.headers }); + if (response.status !== 503 && response.status !== 429) break; + sleep(0.1); + } + check(response, { 'concurrent retry succeeds': r => r.status === 201 }); + console.log(`instance=${response.headers['X-Instance-Id']}, status=${response.status}`); +} +export function teardown(data) { + const history = http.get(`${data.path}?limit=100`, { headers: data.headers }); + check(history, { 'exactly one stored message': r => r.status === 200 && r.json().items.length === 1 }); + const conflict = http.post(data.path, JSON.stringify({ text: 'different message' }), { headers: data.headers }); + check(conflict, { 'different body conflicts': r => r.status === 409 }); +} diff --git a/student-tests/load/scaling.js b/student-tests/load/scaling.js new file mode 100644 index 0000000..09ae867 --- /dev/null +++ b/student-tests/load/scaling.js @@ -0,0 +1,77 @@ +import http from 'k6/http'; +import encoding from 'k6/encoding'; +import execution from 'k6/execution'; +import { check, sleep } from 'k6'; +import { Counter, Trend } from 'k6/metrics'; + +const base = __ENV.BASE_URL || 'https://hl.agapov.space'; +const seconds = Number(__ENV.DURATION || 30); +const users = Number(__ENV.USERS || 16); +const writes = new Counter('confirmed_messages'); +const attempts = new Counter('attempted_messages'); +const failures = new Counter('application_errors'); +const operations = new Counter('measured_operations'); +const failedWrites = new Counter('failed_writes'); +const latency = new Trend('message_latency_ms', true); +export const options = { + scenarios: { + warmup: { executor: 'constant-vus', vus: 4, duration: '5s', gracefulStop: '0s' }, + measured: { executor: 'constant-vus', vus: Number(__ENV.VUS || 32), duration: `${seconds}s`, startTime: '10s', gracefulStop: '0s' }, + }, + setupTimeout: '180s', + thresholds: { 'application_errors{status:429}': ['count>=0'], 'application_errors{status:503}': ['count>=0'], 'application_errors{status:0}': ['count>=0'] }, + summaryTrendStats: ['avg', 'p(50)', 'p(95)', 'p(99)', 'max'], +}; + +export function setup() { + const actors = []; + const prefix = `bench_${Date.now()}`; + for (let i = 0; i < users; i++) { + let prepared = false; + for (let attempt = 0; attempt < 5; attempt++) { + for (let ready = 0; ready < 30; ready++) { + if (http.get(base + '/health/ready', {timeout:'3s', tags:{phase:'setup'}}).status === 200) break; + sleep(1); + } + const username = `${prefix}_${i}_${attempt}`; + const password = 'synthetic-benchmark-password'; + let response = http.post(`${base}/api/v1/users`, JSON.stringify({ username, display_name: 'Synthetic', password }), { headers: { 'Content-Type': 'application/json' }, tags: { phase: 'setup' } }); + if (response.status !== 201) { console.warn(`setup registration: ${response.status}; abandoning synthetic actor`); sleep(1); continue; } + const user = response.json(); + response = http.post(`${base}/api/v1/auth/sessions`, null, { headers: { Authorization: `Basic ${encoding.b64encode(`${username}:${password}`)}` }, tags: { phase: 'setup' } }); + if (response.status !== 201) { console.warn(`setup login: ${response.status}; abandoning synthetic actor`); sleep(1); continue; } + const token = response.json().token; + response = http.post(`${base}/api/v1/chats`, JSON.stringify({ title: 'Synthetic benchmark', member_ids: [user.id] }), { headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' }, tags: { phase: 'setup' } }); + if (response.status !== 201) { console.warn(`setup chat: ${response.status}; abandoning synthetic actor`); sleep(1); continue; } + actors.push({ token, chat: response.json().id }); + prepared = true; + break; + } + if (!prepared) throw new Error("synthetic actor setup exhausted retries"); + } + return actors; +} + +export default function (actors) { + const measured = execution.scenario.name === 'measured'; + const actor = actors[(__VU - 1) % actors.length]; + const headers = { Authorization: `Bearer ${actor.token}`, 'Content-Type': 'application/json', 'Idempotency-Key': `${execution.scenario.name}:${__VU}:${__ITER}` }; + const path = `${base}/api/v1/chats/${actor.chat}/messages`; + const payload = JSON.stringify({ text: `synthetic message ${__VU}:${__ITER}` }); + const response = http.post(path, payload, { headers, tags: { phase: 'load' } }); + if (measured) { latency.add(response.timings.duration); attempts.add(1); operations.add(1); } + if (check(response, { 'message confirmed': r => r.status === 201 })) { if (measured) writes.add(1); } + else if (measured) { failures.add(1, { status: String(response.status) }); failedWrites.add(1); } + if (response.status === 201 && __ITER % 10 === 0) { + if (measured) operations.add(1); + const repeated = http.post(path, payload, { headers, tags: { phase: 'load' } }); + if (!check(repeated, { 'retry returns original': r => r.status === 201 && r.json().id === response.json().id })) { if (measured) failures.add(1, { status: String(repeated.status) }); } + } + if (measured) operations.add(1); + const read = http.get(`${path}?limit=10`, { headers, tags: { phase: 'load' } }); + if (!check(read, { 'history readable': r => r.status === 200 })) { if (measured) failures.add(1, { status: String(read.status) }); } +} + +export function handleSummary(data) { + return { stdout: JSON.stringify({ profile: { vus: Number(__ENV.VUS || 32), seconds, warmup_seconds: 5, drain_seconds: 5, users }, summary: data }) + '\n' }; +} diff --git a/student-tests/plans.sql b/student-tests/plans.sql new file mode 100644 index 0000000..9e1ea28 --- /dev/null +++ b/student-tests/plans.sql @@ -0,0 +1,16 @@ +-- Repeat the same read and rolled-back write three times for each index. +SELECT 'EXPLAIN (ANALYZE,BUFFERS,FORMAT JSON) +SELECT id,chat_id,sender_id,text,created_at FROM messages +WHERE chat_id=md5(''1'')::uuid +AND (created_at,id)>(''2026-01-01Z''::timestamptz+85000*interval ''1 microsecond'',md5(''85000'')::uuid) +ORDER BY created_at,id LIMIT 51; +BEGIN; +EXPLAIN (ANALYZE,BUFFERS,FORMAT JSON) +INSERT INTO messages +SELECT md5(n::text)::uuid,md5(''1'')::uuid, +''11111111-1111-4111-8111-111111111111'',''new-synthetic-''||n, +''2026-01-01Z''::timestamptz+n*interval ''1 microsecond'' +FROM generate_series(100001,101000) n; +ROLLBACK;' +FROM generate_series(1,3) +\gexec diff --git a/student-tests/quality_test.go b/student-tests/quality_test.go new file mode 100644 index 0000000..dcbfecc --- /dev/null +++ b/student-tests/quality_test.go @@ -0,0 +1,125 @@ +// Package quality checks source-level conventions required by the lab implementation. +package quality + +import ( + "go/ast" + "go/parser" + "go/token" + "io/fs" + "path/filepath" + "strconv" + "strings" + "testing" +) + +// TestExportedDocumentation checks functions, types, constants, fields and interface methods. +func TestExportedDocumentation(t *testing.T) { + visitProductionFiles(t, func(path string, file *ast.File) { + ast.Inspect(file, func(node ast.Node) bool { + switch declaration := node.(type) { + case *ast.FuncDecl: + if declaration.Doc == nil || !strings.HasPrefix(strings.TrimSpace(declaration.Doc.Text()), declaration.Name.Name+" ") { + t.Errorf("%s: function %s needs a prefixed doc comment", path, declaration.Name.Name) + } + case *ast.GenDecl: + for _, spec := range declaration.Specs { + switch spec := spec.(type) { + case *ast.TypeSpec: + doc := spec.Doc + if doc == nil { + doc = declaration.Doc + } + requireDoc(t, path, spec.Name, doc) + case *ast.ValueSpec: + doc := spec.Doc + if doc == nil { + doc = declaration.Doc + } + for _, name := range spec.Names { + requireDoc(t, path, name, doc) + } + } + } + case *ast.StructType: + for _, field := range declaration.Fields.List { + for _, name := range field.Names { + requireDoc(t, path, name, field.Doc) + } + } + case *ast.InterfaceType: + for _, method := range declaration.Methods.List { + for _, name := range method.Names { + requireDoc(t, path, name, method.Doc) + } + } + } + return true + }) + }) +} + +// requireDoc checks that an exported declaration has a correctly prefixed doc comment. +func requireDoc(t *testing.T, path string, name *ast.Ident, doc *ast.CommentGroup) { + t.Helper() + if !name.IsExported() { + return + } + if doc == nil || !strings.HasPrefix(strings.TrimSpace(doc.Text()), name.Name+" ") { + t.Errorf("%s: %s needs a Go doc comment starting with its name", path, name.Name) + } +} + +// TestDependencyDirection prevents concrete infrastructure leaking into higher layers. +func TestDependencyDirection(t *testing.T) { + visitProductionFiles(t, func(path string, file *ast.File) { + normalized := filepath.ToSlash(path) + for _, dependency := range file.Imports { + imported, err := strconv.Unquote(dependency.Path.Value) + if err != nil { + t.Fatal(err) + } + var forbidden []string + switch { + case strings.Contains(normalized, "internal/service/"): + forbidden = []string{"net/http", "github.com/gofiber/", "/internal/controller", "/internal/store/memory", "/internal/store/postgres", "/internal/valkey", "/internal/config", "go.lab260.ru/fielderr"} + case strings.Contains(normalized, "internal/admission/"), strings.Contains(normalized, "internal/strictjson/"): + forbidden = []string{"net/http", "github.com/gofiber/", "/internal/"} + case strings.Contains(normalized, "internal/cursor/"): + forbidden = []string{"net/http", "github.com/gofiber/", "/internal/service", "/internal/store", "/internal/controller", "/internal/model"} + case strings.Contains(normalized, "internal/store/"): + forbidden = []string{"net/http", "github.com/gofiber/", "/internal/controller", "/internal/service"} + case strings.Contains(normalized, "internal/controller/"): + forbidden = []string{"/internal/service/production", "/internal/store"} + } + for _, fragment := range forbidden { + if strings.Contains(imported, fragment) { + t.Errorf("%s: forbidden dependency %s", path, imported) + } + } + } + }) +} + +// visitProductionFiles parses application Go files and visits their syntax trees. +func visitProductionFiles(t *testing.T, visit func(string, *ast.File)) { + t.Helper() + for _, root := range []string{"../cmd", "../internal"} { + err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error { + if err != nil { + return err + } + if entry.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { + return nil + } + file, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ParseComments) + if err != nil { + return err + } + visit(path, file) + return nil + }) + if err != nil { + t.Fatal(err) + } + } +} diff --git a/student-tests/research.sql b/student-tests/research.sql new file mode 100644 index 0000000..c43c67b --- /dev/null +++ b/student-tests/research.sql @@ -0,0 +1,26 @@ +-- Isolated database: actual application schema and a deliberately skewed history. +\set ON_ERROR_STOP on +\timing on +SELECT version(); +INSERT INTO users(id,username,display_name,password_hash) +VALUES('11111111-1111-4111-8111-111111111111','research','Synthetic','not-a-login-hash'); +INSERT INTO chats(id,title) +SELECT md5(n::text)::uuid,'Synthetic '||n FROM generate_series(1,3) n; +INSERT INTO chat_members(chat_id,user_id) +SELECT id,'11111111-1111-4111-8111-111111111111' FROM chats; +INSERT INTO messages(id,chat_id,sender_id,text,created_at) +SELECT md5(n::text)::uuid, + md5((CASE WHEN n<=90000 THEN 1 WHEN n<=99000 THEN 2 ELSE 3 END)::text)::uuid, + '11111111-1111-4111-8111-111111111111','synthetic-'||n, + '2026-01-01Z'::timestamptz+n*interval '1 microsecond' +FROM generate_series(1,100000) AS n; +DROP INDEX messages_history; +CREATE INDEX candidate ON messages(chat_id); +\echo 'VARIANT: chat_only' +ANALYZE messages; +\i /tmp/plans.sql +DROP INDEX candidate; +CREATE INDEX candidate ON messages(chat_id,created_at,id); +\echo 'VARIANT: history_keyset' +ANALYZE messages; +\i /tmp/plans.sql