@@ -2,14 +2,14 @@ package auth
22
33import (
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
1515func 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
4040func 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
202207func 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