diff --git a/README.md b/README.md index 336e49a..5f571f0 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,9 @@ docker compose exec app php artisan migrate docker compose exec app php artisan hado:source:create docs # токен печатается один раз ``` -- http://localhost:8080/ — инбокс (нужен заголовок `X-Remote-User`; локально: расширение - браузера для заголовков или `curl -H 'X-Remote-User: nikita' localhost:8080/me`) +- http://localhost:8080/ — инбокс. В проде пользователь приходит в заголовке `X-Remote-User` + от хаба; локально поставь `HADO_DEV_USER=nikita` в `core/.env` (работает только при + `APP_ENV=local`) и открывай в браузере без заголовков. - `PUT /api/events` с `Authorization: Bearer <токен>` — см. спеку §5 Тесты локально: `cd core && php artisan test` (SQLite in-memory, нужны расширения diff --git a/core/.env.example b/core/.env.example index 5e97b68..9f0c5a9 100644 --- a/core/.env.example +++ b/core/.env.example @@ -41,6 +41,8 @@ CACHE_STORE=database # CACHE_PREFIX= HADO_DEFAULT_TZ=America/Montevideo +# Только dev (APP_ENV=local): логин вместо X-Remote-User, чтобы открыть инбокс без хаба +HADO_DEV_USER= HADO_PRESENCE_TIMEOUT=2 HADO_FALLBACK_DAY=30 HADO_FALLBACK_HOUR=10 diff --git a/core/app/Http/Middleware/RemoteUser.php b/core/app/Http/Middleware/RemoteUser.php index 98e3f82..e2d9e0e 100644 --- a/core/app/Http/Middleware/RemoteUser.php +++ b/core/app/Http/Middleware/RemoteUser.php @@ -18,6 +18,10 @@ class RemoteUser public function handle(Request $request, Closure $next): Response { $login = trim((string) $request->header('X-Remote-User', '')); + if ($login === '' && app()->environment('local')) { + // Dev без хаба: HADO_DEV_USER подставляет пользователя. В проде не работает. + $login = trim((string) config('hado.dev_user')); + } if ($login === '') { return response()->json(['message' => 'Нет X-Remote-User: запрос должен идти через хаб'], 401); } diff --git a/core/config/hado.php b/core/config/hado.php index c0f9a54..069f25c 100644 --- a/core/config/hado.php +++ b/core/config/hado.php @@ -2,6 +2,8 @@ return [ 'default_tz' => env('HADO_DEFAULT_TZ', 'UTC'), + // Только для APP_ENV=local: логин, который подставляется вместо X-Remote-User, если заголовка нет. + 'dev_user' => env('HADO_DEV_USER', ''), 'presence_timeout' => (int) env('HADO_PRESENCE_TIMEOUT', 2), 'presence_ttl' => 60, 'fallback_day_minutes' => (int) env('HADO_FALLBACK_DAY', 30), diff --git a/core/tests/Feature/MeApiTest.php b/core/tests/Feature/MeApiTest.php index e1aa592..6e2d902 100644 --- a/core/tests/Feature/MeApiTest.php +++ b/core/tests/Feature/MeApiTest.php @@ -52,6 +52,21 @@ class MeApiTest extends TestCase public function test_requires_remote_user_header(): void { $this->getJson('/me')->assertStatus(401); + + // dev_user срабатывает только в local + config(['hado.dev_user' => 'dev']); + $this->getJson('/me')->assertStatus(401); + } + + public function test_dev_user_substitutes_header_in_local_env_only(): void + { + config(['hado.dev_user' => 'dev']); + $this->app->detectEnvironment(fn () => 'local'); + + $this->getJson('/me')->assertOk()->assertJsonPath('login', 'dev'); + + config(['hado.dev_user' => '']); + $this->getJson('/me')->assertStatus(401); } public function test_first_visit_creates_user_with_web_channel(): void