Skip to content

fix: auth login 時に tenantId を service として config に保存する#89

Merged
miya0001 merged 4 commits into
mainfrom
fix/login-save-tenant
Mar 23, 2026
Merged

fix: auth login 時に tenantId を service として config に保存する#89
miya0001 merged 4 commits into
mainfrom
fix/login-save-tenant

Conversation

@miya0001

@miya0001 miya0001 commented Mar 23, 2026

Copy link
Copy Markdown
Member

Summary

  • auth login 後にリクエストへ NGSILD-Tenant ヘッダーが付与されない問題を修正
  • ログインレスポンスの tenantIdconfig.service として保存するようにした
  • re-login(テナント選択)時はユーザーが選んだ selectedTenantId を保存
  • tenantId がないレスポンス(スーパー管理者等)は従来通り service 未設定のまま

Root Cause

GdbClient.buildHeaders()this.service が設定されている場合のみ NGSILD-Tenant ヘッダーを付与する。しかし auth login はトークンと refreshToken しか config に保存しておらず、tenantIdservice の変換が行われていなかった。

Test plan

  • auth login 後に geonic entities list --dry-runNGSILD-Tenant ヘッダーが含まれることを確認
  • npm run lint && npm run typecheck && npm test がすべて通ること(CI)
  • スーパー管理者アカウントでログインした場合、service が設定されないことを確認

Summary by CodeRabbit

  • バグ修正

    • マルチテナント環境でのログイン後、選択したテナントIDが設定に正しく保存されるようになり、設定の永続化が改善されました。
  • テスト

    • ログイン応答のテナント情報保存動作を検証するテストケースを追加・拡充しました。
  • ドキュメント

    • 未リリース欄に本修正を記載した更新を追加しました。

ログイン後に NGSILD-Tenant ヘッダーが付与されない問題を修正。
レスポンスの tenantId を config.service に保存するようにした。
@coderabbitai

coderabbitai Bot commented Mar 23, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6ff2a7bb-fdea-4aae-9a93-3b169373f6ed

📥 Commits

Reviewing files that changed from the base of the PR and between cab42b6 and 3d7fa6e.

📒 Files selected for processing (2)
  • src/commands/auth.ts
  • tests/auth.test.ts
✅ Files skipped from review due to trivial changes (1)
  • src/commands/auth.ts

📝 Walkthrough

Walkthrough

マルチテナントの対話型 email/password ログインで、選択された最終テナントIDを追跡し、ログイン結果を設定に保存する際に config.service フィールドへ反映するように変更しました。CHANGELOG にも該当修正を追記し、関連テストを更新しています。

Changes

Cohort / File(s) Summary
CHANGELOG更新
CHANGELOG.md
「Unreleased」に2026-03-24の項目を追加し、auth loginNGSILD-Tenant ヘッダーを設定していなかった問題の修正と、ログインレスポンスの tenantId を設定に service として保存する旨を記載。
認証ロジック
src/commands/auth.ts
対話型マルチテナントログインで不変だった currentTenantIdfinalTenantId に置換し、選択や再ログイン時に最終的に使用されたテナントIDを追跡。保存時に finalTenantId があれば config.service に設定、なければ config.service を削除するように変更。
テスト更新
tests/auth.test.ts
auth login (email/password) テストを拡張し、レスポンスに tenantId がある場合は saveConfigservice が保存されること、tenantId がない場合は service が存在しない(既存の service を削除する)ことを検証するアサーションを追加・更新。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇✨ テナントを追ってぴょんぴょんと、
最後の一歩はちゃんと記録するよ。
設定に刻まれた小さな印、
テストが笑って合格の合図、
ニンジンで乾杯、また進もう!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed タイトルはプルリクエストの主要な変更である「auth login 時に tenantId を service として config に保存する」を正確に反映しており、具体的かつ明確です。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/login-save-tenant

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/auth.test.ts (1)

338-349: 既存 service がある場合の消去ケースもテストしてください。

このケースは「最初から service が無い設定」しか検証していないため、既存 service が残留する不具合を取りこぼします。loadConfig{ service: "old-tenant" } にしたテストを追加するのが安全です。

🧪 追加テスト案
+    it("removes existing service when tenantId is absent from login response", async () => {
+      vi.mocked(isInteractive).mockReturnValue(true);
+      vi.mocked(promptEmail).mockResolvedValue("user@example.com");
+      vi.mocked(promptPassword).mockResolvedValue("pass123");
+      vi.mocked(loadConfig).mockReturnValue({ service: "old-tenant" } as never);
+      client.rawRequest.mockResolvedValue(
+        mockResponse({ accessToken: "tok" }),
+      );
+      const program = makeProgram();
+      await runCommand(program, ["auth", "login"]);
+      const savedConfig = vi.mocked(saveConfig).mock.calls[0][0] as Record<string, unknown>;
+      expect(savedConfig).not.toHaveProperty("service");
+    });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/auth.test.ts` around lines 338 - 349, Add a second test that covers the
case where an existing service value should be removed: mock loadConfig to
return { service: "old-tenant" } before calling runCommand in the "does not set
service when tenantId is absent from login response" scenario (use the same
setup as the existing test: vi.mocked(isInteractive).., promptEmail,
promptPassword, client.rawRequest returning mockResponse({ accessToken: "tok"
})), then call runCommand(program, ["auth", "login"]) and assert that the saved
config passed to saveConfig (vi.mocked(saveConfig).mock.calls[0][0]) does not
have the "service" property; reference functions: loadConfig, saveConfig,
runCommand, makeProgram, client.rawRequest.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/commands/auth.ts`:
- Around line 147-149: The current branch only sets config.service when
finalTenantId is present, leaving an old tenant value when finalTenantId is
absent; update the logic around finalTenantId (the if (finalTenantId) {
config.service = finalTenantId; } block) to explicitly remove or clear
config.service when finalTenantId is falsy (e.g., delete config.service or set
it to undefined/null) so stale NGSILD-Tenant headers are not sent by subsequent
API calls.

---

Nitpick comments:
In `@tests/auth.test.ts`:
- Around line 338-349: Add a second test that covers the case where an existing
service value should be removed: mock loadConfig to return { service:
"old-tenant" } before calling runCommand in the "does not set service when
tenantId is absent from login response" scenario (use the same setup as the
existing test: vi.mocked(isInteractive).., promptEmail, promptPassword,
client.rawRequest returning mockResponse({ accessToken: "tok" })), then call
runCommand(program, ["auth", "login"]) and assert that the saved config passed
to saveConfig (vi.mocked(saveConfig).mock.calls[0][0]) does not have the
"service" property; reference functions: loadConfig, saveConfig, runCommand,
makeProgram, client.rawRequest.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 65541604-e640-4179-b457-1f4fe2f2e1ff

📥 Commits

Reviewing files that changed from the base of the PR and between 6e2c2df and cab42b6.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • src/commands/auth.ts
  • tests/auth.test.ts

Comment thread src/commands/auth.ts
CodeRabbit レビュー対応。finalTenantId が falsy の場合に
config.service を明示的に delete してステール値を防ぐ。
テストケースも追加。
@miya0001

Copy link
Copy Markdown
Member Author

ご指摘ありがとうございます。2点対応しました。

1. src/commands/auth.ts — stale service の消去

finalTenantId が falsy の場合(スーパー管理者等)、既存の config.servicedelete するように修正しました。これでテナントなしのアカウントでログインし直した際に古い NGSILD-Tenant ヘッダーが残留しなくなります。

2. tests/auth.test.ts — 既存 service 消去ケースのテスト追加

loadConfig{ service: "old-tenant" } に差し替えたテストを追加し、tenantId 不在時に service が削除されることを検証しています。

@miya0001
miya0001 merged commit 8da24cc into main Mar 23, 2026
7 checks passed
@miya0001
miya0001 deleted the fix/login-save-tenant branch March 23, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant