Skip to content

Commit 33adc09

Browse files
committed
successful logout with a msg when cfg files are absent
1 parent 0584479 commit 33adc09

File tree

3 files changed

+78
-22
lines changed

3 files changed

+78
-22
lines changed

cli/logout.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,41 @@ func logout() *cobra.Command {
1313
Use: "logout",
1414
Short: "Remove the local authenticated session",
1515
RunE: func(cmd *cobra.Command, args []string) error {
16+
var isLoggedOut bool
17+
1618
config := createConfig(cmd)
1719

1820
err := config.URL().Delete()
1921
if err != nil {
20-
// If the URL configuration file is absent, the user is logged out
21-
if os.IsNotExist(err) {
22-
return xerrors.New(notLoggedInMessage)
22+
// Only throw error if the URL configuration file is present,
23+
// otherwise the user is already logged out, and we proceed
24+
if !os.IsNotExist(err) {
25+
return xerrors.Errorf("remove URL file: %w", err)
2326
}
24-
return xerrors.Errorf("remove URL file: %w", err)
27+
isLoggedOut = true
2528
}
2629

2730
err = config.Session().Delete()
2831
if err != nil {
29-
// If the session configuration file is absent, the user is logged out
30-
if os.IsNotExist(err) {
31-
return xerrors.New(notLoggedInMessage)
32+
// Only throw error if the session configuration file is present,
33+
// otherwise the user is already logged out, and we proceed
34+
if !os.IsNotExist(err) {
35+
return xerrors.Errorf("remove session file: %w", err)
3236
}
33-
return xerrors.Errorf("remove session file: %w", err)
37+
isLoggedOut = true
3438
}
3539

3640
err = config.Organization().Delete()
37-
// If the organization configuration file is absent, we should still log out
41+
// If the organization configuration file is absent, we still proceed
3842
if err != nil && !os.IsNotExist(err) {
3943
return xerrors.Errorf("remove organization file: %w", err)
4044
}
4145

46+
// If the user was already logged out, we show them a message
47+
if isLoggedOut {
48+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), notLoggedInMessage+"\n")
49+
}
50+
4251
_, _ = fmt.Fprintf(cmd.OutOrStdout(), caret+"Successfully logged out.\n")
4352
return nil
4453
},

cli/logout_test.go

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,99 @@ func TestLogout(t *testing.T) {
1717
t.Run("Logout", func(t *testing.T) {
1818
t.Parallel()
1919

20-
config := login(t)
20+
pty := ptytest.New(t)
21+
config := login(t, pty)
2122

2223
// ensure session files exist
2324
assert.FileExists(t, string(config.URL()))
2425
assert.FileExists(t, string(config.Session()))
2526

27+
logoutChan := make(chan struct{})
2628
logout, _ := clitest.New(t, "logout", "--global-config", string(config))
27-
err := logout.Execute()
28-
assert.NoError(t, err)
29-
assert.NoFileExists(t, string(config.URL()))
30-
assert.NoFileExists(t, string(config.Session()))
29+
logout.SetIn(pty.Input())
30+
logout.SetOut(pty.Output())
31+
32+
go func() {
33+
defer close(logoutChan)
34+
err := logout.Execute()
35+
assert.NoError(t, err)
36+
assert.NoFileExists(t, string(config.URL()))
37+
assert.NoFileExists(t, string(config.Session()))
38+
}()
39+
40+
pty.ExpectMatch("Successfully logged out")
41+
<-logoutChan
3142
})
3243
t.Run("NoURLFile", func(t *testing.T) {
3344
t.Parallel()
3445

35-
logout, _ := clitest.New(t, "logout")
46+
pty := ptytest.New(t)
47+
config := login(t, pty)
48+
49+
// ensure session files exist
50+
assert.FileExists(t, string(config.URL()))
51+
assert.FileExists(t, string(config.Session()))
52+
53+
os.RemoveAll(string(config.URL()))
3654

37-
err := logout.Execute()
38-
assert.EqualError(t, err, "You are not logged in. Try logging in using 'coder login <url>'.")
55+
logoutChan := make(chan struct{})
56+
logout, _ := clitest.New(t, "logout", "--global-config", string(config))
57+
58+
logout.SetIn(pty.Input())
59+
logout.SetOut(pty.Output())
60+
61+
go func() {
62+
defer close(logoutChan)
63+
err := logout.Execute()
64+
assert.NoError(t, err)
65+
assert.NoFileExists(t, string(config.URL()))
66+
assert.NoFileExists(t, string(config.Session()))
67+
}()
68+
69+
pty.ExpectMatch("You are not logged in. Try logging in using 'coder login <url>'.")
70+
pty.ExpectMatch("Successfully logged out")
71+
<-logoutChan
3972
})
4073
t.Run("NoSessionFile", func(t *testing.T) {
4174
t.Parallel()
4275

43-
config := login(t)
76+
pty := ptytest.New(t)
77+
config := login(t, pty)
4478

4579
// ensure session files exist
4680
assert.FileExists(t, string(config.URL()))
4781
assert.FileExists(t, string(config.Session()))
4882

4983
os.RemoveAll(string(config.Session()))
5084

85+
logoutChan := make(chan struct{})
5186
logout, _ := clitest.New(t, "logout", "--global-config", string(config))
5287

53-
err := logout.Execute()
54-
assert.EqualError(t, err, "You are not logged in. Try logging in using 'coder login <url>'.")
88+
logout.SetIn(pty.Input())
89+
logout.SetOut(pty.Output())
90+
91+
go func() {
92+
defer close(logoutChan)
93+
err := logout.Execute()
94+
assert.NoError(t, err)
95+
assert.NoFileExists(t, string(config.URL()))
96+
assert.NoFileExists(t, string(config.Session()))
97+
}()
98+
99+
pty.ExpectMatch("You are not logged in. Try logging in using 'coder login <url>'.")
100+
pty.ExpectMatch("Successfully logged out")
101+
<-logoutChan
55102
})
56103
}
57104

58-
func login(t *testing.T) config.Root {
105+
func login(t *testing.T, pty *ptytest.PTY) config.Root {
59106
t.Helper()
60107

61108
client := coderdtest.New(t, nil)
62109
coderdtest.CreateFirstUser(t, client)
63110

64111
doneChan := make(chan struct{})
65112
root, cfg := clitest.New(t, "login", "--force-tty", client.URL.String(), "--no-open")
66-
pty := ptytest.New(t)
67113
root.SetIn(pty.Input())
68114
root.SetOut(pty.Output())
69115
go func() {

cli/userlist_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
func TestUserList(t *testing.T) {
15+
t.Parallel()
1516
t.Run("List", func(t *testing.T) {
1617
t.Parallel()
1718
client := coderdtest.New(t, nil)

0 commit comments

Comments
 (0)