Skip to content

Commit 4c74276

Browse files
Fix auth tests to use mock keyring
Tests were hitting the real keychain, causing them to delete the user's stored token when running `go test ./...`. Now use keyring.MockInit() to isolate tests from the real keychain.
1 parent 3377081 commit 4c74276

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

internal/auth/token_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/stretchr/testify/assert"
88
"github.com/stretchr/testify/require"
9+
"github.com/zalando/go-keyring"
910
)
1011

1112
func TestNewTokenSource(t *testing.T) {
@@ -50,13 +51,13 @@ func TestGetToken(t *testing.T) {
5051
})
5152

5253
t.Run("returns error when no token available", func(t *testing.T) {
54+
// Mock keyring to ensure no token is found
55+
keyring.MockInit()
5356
t.Setenv("FASTMAIL_TOKEN", "")
5457

5558
ts := NewTokenSource()
5659
_, err := ts.GetToken()
5760

58-
// In CI/test environment, keyring likely fails, so we expect an error
59-
// The error message should guide the user
6061
require.Error(t, err)
6162
assert.Contains(t, err.Error(), "not authenticated")
6263
assert.Contains(t, err.Error(), "fm auth login")
@@ -73,11 +74,12 @@ func TestIsAuthenticated(t *testing.T) {
7374
})
7475

7576
t.Run("false when no token", func(t *testing.T) {
77+
// Mock keyring to ensure no token is found
78+
keyring.MockInit()
7679
t.Setenv("FASTMAIL_TOKEN", "")
7780

7881
ts := NewTokenSource()
7982

80-
// In test environment without keyring, should be false
8183
assert.False(t, ts.IsAuthenticated())
8284
})
8385
}

internal/cmd/auth/auth_test.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package auth
22

33
import (
44
"bytes"
5-
"strings"
65
"testing"
76

87
"github.com/jarcoal/httpmock"
98
"github.com/marckohlbrugge/fastmail-cli/internal/cmdutil"
109
"github.com/marckohlbrugge/fastmail-cli/internal/iostreams"
1110
"github.com/stretchr/testify/assert"
1211
"github.com/stretchr/testify/require"
12+
"github.com/zalando/go-keyring"
1313
)
1414

1515
func setupTest(t *testing.T) (*cmdutil.Factory, *bytes.Buffer, *bytes.Buffer, *bytes.Buffer) {
@@ -38,7 +38,10 @@ func setupTest(t *testing.T) (*cmdutil.Factory, *bytes.Buffer, *bytes.Buffer, *b
3838
// Login command tests
3939

4040
func TestLoginCommand(t *testing.T) {
41-
t.Run("validates token with API", func(t *testing.T) {
41+
t.Run("validates token with API and stores in keyring", func(t *testing.T) {
42+
// Mock the keyring to avoid touching real keychain
43+
keyring.MockInit()
44+
4245
f, in, out, _ := setupTest(t)
4346

4447
httpmock.RegisterResponder("GET", "https://api.fastmail.com/jmap/session",
@@ -59,11 +62,13 @@ func TestLoginCommand(t *testing.T) {
5962

6063
err := cmd.Execute()
6164

62-
// Will fail on keyring storage in test environment, but validates the token
63-
// The error should be about keyring, not about auth
64-
if err != nil {
65-
assert.Contains(t, err.Error(), "keychain")
66-
}
65+
require.NoError(t, err)
66+
assert.Contains(t, out.String(), "Logged in")
67+
68+
// Verify token was stored in mock keyring
69+
token, err := keyring.Get("fm-cli", "fastmail-token")
70+
require.NoError(t, err)
71+
assert.Equal(t, "fmu1-test-token-12345678", token)
6772
})
6873

6974
t.Run("rejects empty token", func(t *testing.T) {
@@ -201,6 +206,9 @@ func TestStatusCommand(t *testing.T) {
201206

202207
func TestLogoutCommand(t *testing.T) {
203208
t.Run("handles not logged in gracefully", func(t *testing.T) {
209+
// Mock the keyring to avoid touching real keychain
210+
keyring.MockInit()
211+
204212
f, _, out, _ := setupTest(t)
205213

206214
cmd := NewCmdLogout(f)
@@ -213,8 +221,26 @@ func TestLogoutCommand(t *testing.T) {
213221
// Should not error, just say not logged in
214222
require.NoError(t, err)
215223
output := out.String()
216-
// Either "Not logged in" or "Logged out" depending on keyring state
217-
assert.True(t, strings.Contains(output, "Not logged in") || strings.Contains(output, "Logged out"))
224+
assert.Contains(t, output, "Not logged in")
225+
})
226+
227+
t.Run("logs out when token exists", func(t *testing.T) {
228+
// Mock the keyring and store a token
229+
keyring.MockInit()
230+
_ = keyring.Set("fm-cli", "fastmail-token", "test-token")
231+
232+
f, _, out, _ := setupTest(t)
233+
234+
cmd := NewCmdLogout(f)
235+
cmd.SetArgs([]string{})
236+
cmd.SetOut(out)
237+
cmd.SetErr(&bytes.Buffer{})
238+
239+
err := cmd.Execute()
240+
241+
require.NoError(t, err)
242+
output := out.String()
243+
assert.Contains(t, output, "Logged out")
218244
})
219245

220246
t.Run("accepts no arguments", func(t *testing.T) {

0 commit comments

Comments
 (0)