-
Notifications
You must be signed in to change notification settings - Fork 930
Improve CLI logout flow #1692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Improve CLI logout flow #1692
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
abff3c8
Improve CLI logout flow
AbhineetJain a7775b0
Fix lint error
AbhineetJain 0584479
Make notLoggedInMessage a const
AbhineetJain 33adc09
successful logout with a msg when cfg files are absent
AbhineetJain 06142b0
use require, os.remove, show only one message, add prompt
AbhineetJain 5f01dfd
Merge branch 'main' into abhineetjain/improve-logout
AbhineetJain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,127 @@ | ||
package cli_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/assert" | ||
AbhineetJain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/cli/config" | ||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/coder/coder/pty/ptytest" | ||
) | ||
|
||
func TestLogout(t *testing.T) { | ||
t.Parallel() | ||
t.Run("Logout", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
pty := ptytest.New(t) | ||
config := login(t, pty) | ||
|
||
// ensure session files exist | ||
assert.FileExists(t, string(config.URL())) | ||
assert.FileExists(t, string(config.Session())) | ||
AbhineetJain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
logoutChan := make(chan struct{}) | ||
logout, _ := clitest.New(t, "logout", "--global-config", string(config)) | ||
logout.SetIn(pty.Input()) | ||
logout.SetOut(pty.Output()) | ||
|
||
go func() { | ||
defer close(logoutChan) | ||
err := logout.Execute() | ||
assert.NoError(t, err) | ||
assert.NoFileExists(t, string(config.URL())) | ||
assert.NoFileExists(t, string(config.Session())) | ||
AbhineetJain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}() | ||
|
||
pty.ExpectMatch("Successfully logged out") | ||
<-logoutChan | ||
}) | ||
t.Run("NoURLFile", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
pty := ptytest.New(t) | ||
config := login(t, pty) | ||
|
||
// ensure session files exist | ||
assert.FileExists(t, string(config.URL())) | ||
assert.FileExists(t, string(config.Session())) | ||
|
||
os.RemoveAll(string(config.URL())) | ||
AbhineetJain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
logoutChan := make(chan struct{}) | ||
logout, _ := clitest.New(t, "logout", "--global-config", string(config)) | ||
|
||
logout.SetIn(pty.Input()) | ||
logout.SetOut(pty.Output()) | ||
|
||
go func() { | ||
defer close(logoutChan) | ||
err := logout.Execute() | ||
assert.NoError(t, err) | ||
assert.NoFileExists(t, string(config.URL())) | ||
assert.NoFileExists(t, string(config.Session())) | ||
}() | ||
|
||
pty.ExpectMatch("You are not logged in. Try logging in using 'coder login <url>'.") | ||
pty.ExpectMatch("Successfully logged out") | ||
<-logoutChan | ||
}) | ||
t.Run("NoSessionFile", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
pty := ptytest.New(t) | ||
config := login(t, pty) | ||
|
||
// ensure session files exist | ||
assert.FileExists(t, string(config.URL())) | ||
assert.FileExists(t, string(config.Session())) | ||
|
||
os.RemoveAll(string(config.Session())) | ||
|
||
logoutChan := make(chan struct{}) | ||
logout, _ := clitest.New(t, "logout", "--global-config", string(config)) | ||
|
||
logout.SetIn(pty.Input()) | ||
logout.SetOut(pty.Output()) | ||
|
||
go func() { | ||
defer close(logoutChan) | ||
err := logout.Execute() | ||
assert.NoError(t, err) | ||
assert.NoFileExists(t, string(config.URL())) | ||
assert.NoFileExists(t, string(config.Session())) | ||
}() | ||
|
||
pty.ExpectMatch("You are not logged in. Try logging in using 'coder login <url>'.") | ||
pty.ExpectMatch("Successfully logged out") | ||
<-logoutChan | ||
}) | ||
} | ||
|
||
func login(t *testing.T, pty *ptytest.PTY) config.Root { | ||
t.Helper() | ||
|
||
// login | ||
client := coderdtest.New(t, nil) | ||
coderdtest.CreateFirstUser(t, client) | ||
|
||
doneChan := make(chan struct{}) | ||
root, config := clitest.New(t, "login", "--force-tty", client.URL.String(), "--no-open") | ||
pty := ptytest.New(t) | ||
root, cfg := clitest.New(t, "login", "--force-tty", client.URL.String(), "--no-open") | ||
root.SetIn(pty.Input()) | ||
root.SetOut(pty.Output()) | ||
go func() { | ||
defer close(doneChan) | ||
err := root.Execute() | ||
require.NoError(t, err) | ||
assert.NoError(t, err) | ||
}() | ||
|
||
pty.ExpectMatch("Paste your token here:") | ||
pty.WriteLine(client.SessionToken) | ||
pty.ExpectMatch("Welcome to Coder") | ||
<-doneChan | ||
|
||
// ensure session files exist | ||
require.FileExists(t, string(config.URL())) | ||
require.FileExists(t, string(config.Session())) | ||
|
||
logout, _ := clitest.New(t, "logout", "--global-config", string(config)) | ||
err := logout.Execute() | ||
require.NoError(t, err) | ||
require.NoFileExists(t, string(config.URL())) | ||
require.NoFileExists(t, string(config.Session())) | ||
return cfg | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.