Skip to content

Commit 104c997

Browse files
leggetterclaudecursoragent
committed
test(login): unit tests for RefreshGuestSigninLink
Cover profile update on success, fallback to saved URL on API error, and no-op when guest profile fields are missing. Co-Authored-By: Claude <noreply@anthropic.com> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 2fa28bc commit 104c997

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

pkg/login/guest_link_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package login
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
"testing"
11+
12+
configpkg "github.com/hookdeck/hookdeck-cli/pkg/config"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestRefreshGuestSigninLink_updatesProfileOnSuccess(t *testing.T) {
17+
configpkg.ResetAPIClientForTesting()
18+
t.Cleanup(configpkg.ResetAPIClientForTesting)
19+
20+
signin_link_hits := 0
21+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22+
if r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/cli/guest/signin-link") {
23+
signin_link_hits++
24+
user, pass, ok := r.BasicAuth()
25+
require.True(t, ok)
26+
require.Equal(t, "hk_test_guest_refresh_key12", user)
27+
require.Empty(t, pass)
28+
body, err := json.Marshal(map[string]string{
29+
"id": "usr_guest_refresh",
30+
"link": "https://api.example.test/signin/guest?token=fresh_token",
31+
"expires_at": "2099-01-01T00:00:00.000Z",
32+
})
33+
require.NoError(t, err)
34+
_, _ = w.Write(body)
35+
return
36+
}
37+
t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)
38+
}))
39+
t.Cleanup(ts.Close)
40+
41+
configPath := filepath.Join(t.TempDir(), "config.toml")
42+
require.NoError(t, os.WriteFile(configPath, []byte(`profile = "default"
43+
44+
[default]
45+
api_key = "hk_test_guest_refresh_key12"
46+
guest_url = "https://api.example.test/signin/guest?token=stale_token"
47+
guest_user_id = "usr_guest_old"
48+
`), 0o600))
49+
50+
cfg, err := configpkg.LoadConfigFromFile(configPath)
51+
require.NoError(t, err)
52+
cfg.APIBaseURL = ts.URL
53+
cfg.LogLevel = "error"
54+
cfg.TelemetryDisabled = true
55+
56+
got := RefreshGuestSigninLink(cfg)
57+
require.Equal(t, 1, signin_link_hits)
58+
require.Equal(t, "https://api.example.test/signin/guest?token=fresh_token", got)
59+
require.Equal(t, got, cfg.Profile.GuestURL)
60+
require.Equal(t, "usr_guest_refresh", cfg.Profile.GuestUserID)
61+
62+
reloaded, err := os.ReadFile(configPath)
63+
require.NoError(t, err)
64+
require.Contains(t, string(reloaded), "fresh_token")
65+
require.Contains(t, string(reloaded), "usr_guest_refresh")
66+
}
67+
68+
func TestRefreshGuestSigninLink_fallsBackToSavedURLOnAPIError(t *testing.T) {
69+
configpkg.ResetAPIClientForTesting()
70+
t.Cleanup(configpkg.ResetAPIClientForTesting)
71+
72+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
73+
if r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/cli/guest/signin-link") {
74+
w.WriteHeader(http.StatusInternalServerError)
75+
_, _ = w.Write([]byte(`{"message":"server boom"}`))
76+
return
77+
}
78+
t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)
79+
}))
80+
t.Cleanup(ts.Close)
81+
82+
saved_url := "https://api.example.test/signin/guest?token=stale_token"
83+
configPath := filepath.Join(t.TempDir(), "config.toml")
84+
require.NoError(t, os.WriteFile(configPath, []byte(`profile = "default"
85+
86+
[default]
87+
api_key = "hk_test_guest_refresh_key12"
88+
guest_url = "`+saved_url+`"
89+
guest_user_id = "usr_guest_old"
90+
`), 0o600))
91+
92+
cfg, err := configpkg.LoadConfigFromFile(configPath)
93+
require.NoError(t, err)
94+
cfg.APIBaseURL = ts.URL
95+
cfg.LogLevel = "error"
96+
cfg.TelemetryDisabled = true
97+
98+
got := RefreshGuestSigninLink(cfg)
99+
require.Equal(t, saved_url, got)
100+
require.Equal(t, saved_url, cfg.Profile.GuestURL)
101+
require.Equal(t, "usr_guest_old", cfg.Profile.GuestUserID)
102+
}
103+
104+
func TestRefreshGuestSigninLink_returnsEmptyWithoutGuestProfile(t *testing.T) {
105+
cfg := &configpkg.Config{
106+
LogLevel: "error",
107+
TelemetryDisabled: true,
108+
}
109+
cfg.Profile = configpkg.Profile{
110+
Name: "default",
111+
APIKey: "hk_test_guest_refresh_key12",
112+
Config: cfg,
113+
}
114+
115+
require.Empty(t, RefreshGuestSigninLink(cfg))
116+
117+
cfg.Profile.GuestURL = "https://example.test/signin/guest?token=x"
118+
cfg.Profile.APIKey = ""
119+
require.Empty(t, RefreshGuestSigninLink(cfg))
120+
}

0 commit comments

Comments
 (0)