(t *testing.T)
| 9 | ) |
| 10 | |
| 11 | func TestPerSessionAuthE2E(t *testing.T) { |
| 12 | ctx := testharness.NewTestContext(t) |
| 13 | |
| 14 | // Create client with COPILOT_DEBUG_GITHUB_API_URL redirected to the proxy |
| 15 | // so per-session auth token resolution (fetchCopilotUser) is intercepted. |
| 16 | client := ctx.NewClient(func(opts *copilot.ClientOptions) { |
| 17 | opts.Env = append(opts.Env, "COPILOT_DEBUG_GITHUB_API_URL="+ctx.ProxyURL) |
| 18 | }) |
| 19 | t.Cleanup(func() { client.ForceStop() }) |
| 20 | // Register per-token user configs on the proxy |
| 21 | if err := ctx.SetCopilotUserByToken("token-alice", map[string]interface{}{ |
| 22 | "login": "alice", |
| 23 | "copilot_plan": "individual_pro", |
| 24 | "endpoints": map[string]interface{}{"api": ctx.ProxyURL, "telemetry": "https://localhost:1/telemetry"}, |
| 25 | "analytics_tracking_id": "alice-tracking-id", |
| 26 | }); err != nil { |
| 27 | t.Fatalf("Failed to set copilot user for alice: %v", err) |
| 28 | } |
| 29 | |
| 30 | if err := ctx.SetCopilotUserByToken("token-bob", map[string]interface{}{ |
| 31 | "login": "bob", |
| 32 | "copilot_plan": "business", |
| 33 | "endpoints": map[string]interface{}{"api": ctx.ProxyURL, "telemetry": "https://localhost:1/telemetry"}, |
| 34 | "analytics_tracking_id": "bob-tracking-id", |
| 35 | }); err != nil { |
| 36 | t.Fatalf("Failed to set copilot user for bob: %v", err) |
| 37 | } |
| 38 | |
| 39 | t.Run("should authenticate with per-session token", func(t *testing.T) { |
| 40 | ctx.ConfigureForTest(t) |
| 41 | |
| 42 | session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{ |
| 43 | OnPermissionRequest: copilot.PermissionHandler.ApproveAll, |
| 44 | GitHubToken: "token-alice", |
| 45 | }) |
| 46 | if err != nil { |
| 47 | t.Fatalf("Failed to create session: %v", err) |
| 48 | } |
| 49 | |
| 50 | authStatus, err := session.RPC.GitHubAuth.GetStatus(t.Context()) |
| 51 | if err != nil { |
| 52 | t.Fatalf("Failed to get auth status: %v", err) |
| 53 | } |
| 54 | |
| 55 | if !authStatus.IsAuthenticated { |
| 56 | t.Errorf("Expected session to be authenticated") |
| 57 | } |
| 58 | if authStatus.Login == nil || *authStatus.Login != "alice" { |
| 59 | t.Errorf("Expected login to be 'alice', got %v", authStatus.Login) |
| 60 | } |
| 61 | }) |
| 62 | |
| 63 | t.Run("should isolate auth between sessions", func(t *testing.T) { |
| 64 | ctx.ConfigureForTest(t) |
| 65 | |
| 66 | sessionA, err := client.CreateSession(t.Context(), &copilot.SessionConfig{ |
| 67 | OnPermissionRequest: copilot.PermissionHandler.ApproveAll, |
| 68 | GitHubToken: "token-alice", |
nothing calls this directly
no test coverage detected
searching dependent graphs…