-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_auth_test.go
More file actions
204 lines (187 loc) · 5.72 KB
/
Copy pathapp_auth_test.go
File metadata and controls
204 lines (187 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"futrixdata/platform/internal/agentaudit"
"futrixdata/platform/internal/auth"
"futrixdata/platform/internal/bootstrap"
"futrixdata/platform/internal/securefile"
)
func TestHandleOpenURLCompletesPendingLoginFromCallback(t *testing.T) {
var requestBody map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/client/exchange" {
w.WriteHeader(http.StatusNotFound)
return
}
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
t.Fatalf("decode request: %v", err)
}
_ = json.NewEncoder(w).Encode(map[string]any{
"access_token": "access_2",
"refresh_token": "refresh_2",
"expires_in": 900,
"user": map[string]any{
"id": "user_123",
"email": "user@example.com",
"display_name": "Auth User",
},
"license": map[string]any{
"plan": "pro",
"status": "active",
},
})
}))
defer srv.Close()
store := auth.NewStore(filepath.Join(t.TempDir(), "auth-session.json"))
if err := store.Load(); err != nil {
t.Fatalf("load store: %v", err)
}
state := store.Current()
state.PendingLogin = &auth.PendingLogin{
SessionID: "session_123",
CodeVerifier: "verifier_123",
LoginURL: srv.URL + "/app?session_id=session_123",
}
if err := store.Save(state); err != nil {
t.Fatalf("save state: %v", err)
}
var emittedName string
var emittedState auth.State
app := &App{
authStore: store,
authService: auth.NewService(auth.ServiceConfig{
BaseURL: srv.URL,
Store: store,
HTTPClient: srv.Client(),
}),
emitEvent: func(ctx context.Context, eventName string, data ...interface{}) {
emittedName = eventName
if len(data) == 1 {
if next, ok := data[0].(auth.State); ok {
emittedState = next
}
}
},
}
app.handleOpenURL("futrix://callback?code=one-time-authorization-code")
var current auth.State
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
current = store.Current()
if current.Session != nil {
break
}
time.Sleep(10 * time.Millisecond)
}
if current.Session == nil {
t.Fatalf("expected callback to complete login")
}
if current.PendingLogin != nil {
t.Fatalf("expected pending login to be cleared")
}
if requestBody["code"] != "one-time-authorization-code" {
t.Fatalf("expected callback authorization code to be exchanged as-is, got %#v", requestBody["code"])
}
if emittedName != "auth:state" {
t.Fatalf("expected auth:state event, got %q", emittedName)
}
if emittedState.Session == nil || emittedState.Session.User.Email != "user@example.com" {
t.Fatalf("expected emitted state to include session, got %#v", emittedState)
}
}
func TestHandleOpenURLEmitsCodexConnectRequestBeforeAuthorization(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
key := bytes.Repeat([]byte{7}, 32)
securefile.SetKey(key)
t.Cleanup(securefile.ResetForTest)
dataPath := filepath.Join(t.TempDir(), "datasources.json")
var emittedName string
var emittedPayload map[string]any
app := &App{
cfg: Config{DataPath: dataPath},
ctx: context.Background(),
emitEvent: func(ctx context.Context, eventName string, data ...interface{}) {
emittedName = eventName
if len(data) == 1 {
if payload, ok := data[0].(map[string]any); ok {
emittedPayload = payload
}
}
},
}
app.handleOpenURL("futrixdata://codex/connect?source=codex-plugin")
bridgePath := filepath.Join(home, ".futrixdata", "codex-plugin.json")
if _, err := os.Stat(bridgePath); !os.IsNotExist(err) {
t.Fatalf("expected deep link to wait for user confirmation before writing bridge, stat err=%v", err)
}
if emittedName != "codex:connect-request" {
t.Fatalf("expected codex:connect-request event, got %q", emittedName)
}
if emittedPayload["source"] != "codex-plugin" {
t.Fatalf("expected source to round-trip, got %#v", emittedPayload["source"])
}
result := app.AuthorizeCodexPlugin()
if len(result.Installed) != 1 || !result.Installed[0].Success {
t.Fatalf("expected confirmed authorization to succeed, got %#v", result.Installed)
}
bridge, err := os.ReadFile(bridgePath)
if err != nil {
t.Fatalf("read codex plugin bridge: %v", err)
}
content := string(bridge)
for _, token := range []string{
"futrixdata-cli",
"accessKey",
"agent_",
} {
if !strings.Contains(content, token) {
t.Fatalf("expected bridge to contain %q, got %s", token, content)
}
}
identities, err := agentaudit.NewIdentityStore(bootstrap.AgentIdentityPath(dataPath)).ListAll()
if err != nil {
t.Fatalf("ListAll: %v", err)
}
if len(identities) != 1 {
t.Fatalf("expected one codex identity, got %#v", identities)
}
if identities[0].AgentType != "codex" {
t.Fatalf("identity agent type = %q, want codex", identities[0].AgentType)
}
}
func TestHandleLaunchArgsForwardsFutrixDataCodexDeepLink(t *testing.T) {
var emittedName string
var emittedPayload map[string]any
app := &App{
cfg: Config{DataPath: filepath.Join(t.TempDir(), "datasources.json")},
ctx: context.Background(),
emitEvent: func(ctx context.Context, eventName string, data ...interface{}) {
emittedName = eventName
if len(data) == 1 {
if payload, ok := data[0].(map[string]any); ok {
emittedPayload = payload
}
}
},
}
app.handleLaunchArgs([]string{
"--already-running",
"futrixdata://codex/connect?source=codex-plugin",
})
if emittedName != "codex:connect-request" {
t.Fatalf("expected second-instance futrixdata deep link to emit codex:connect-request, got %q", emittedName)
}
if emittedPayload["source"] != "codex-plugin" {
t.Fatalf("expected source to round-trip, got %#v", emittedPayload["source"])
}
}