-
Notifications
You must be signed in to change notification settings - Fork 266
Fix login URL handling, add Ctrl+C support, add logout command #740
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
Open
matthiaswenz
wants to merge
6
commits into
main
Choose a base branch
from
chore/cli-login-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4249e5f
Fix login: prefer complete URL for browser, handle Ctrl+C during prompt
matthiaswenz f1f9963
Add logout command to remove stored credentials
matthiaswenz 7f77e31
Print complete auth URL on browser-open failure
matthiaswenz 8131a03
Use mock keyring in logout tests
matthiaswenz 86778eb
Only use verification_uri, never verification_uri_complete
matthiaswenz d5b4daf
Merge branch 'main' into chore/cli-login-fixes
matthiaswenz 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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
|
|
||
| apiurl "github.com/entireio/cli/cmd/entire/cli/api" | ||
| "github.com/entireio/cli/cmd/entire/cli/auth" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // tokenDeleter abstracts token removal so runLogout can be unit-tested | ||
| // without hitting the real OS keyring. | ||
| type tokenDeleter interface { | ||
| DeleteToken(baseURL string) error | ||
| } | ||
|
|
||
| func newLogoutCmd() *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "logout", | ||
| Short: "Log out of Entire", | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
| return runLogout(cmd.OutOrStdout(), auth.NewStore(), apiurl.BaseURL()) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func runLogout(outW io.Writer, store tokenDeleter, baseURL string) error { | ||
| if err := store.DeleteToken(baseURL); err != nil { | ||
| return fmt.Errorf("remove auth token: %w", err) | ||
| } | ||
|
|
||
| fmt.Fprintln(outW, "Logged out.") | ||
| return nil | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| type mockTokenDeleter struct { | ||
| deleted map[string]bool | ||
| failWith error | ||
| } | ||
|
|
||
| func newMockTokenDeleter() *mockTokenDeleter { | ||
| return &mockTokenDeleter{deleted: make(map[string]bool)} | ||
| } | ||
|
|
||
| func (m *mockTokenDeleter) DeleteToken(baseURL string) error { | ||
| if m.failWith != nil { | ||
| return m.failWith | ||
| } | ||
| m.deleted[baseURL] = true | ||
| return nil | ||
| } | ||
|
|
||
| func TestRunLogout_DeletesTokenAndPrintsMessage(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| store := newMockTokenDeleter() | ||
| var out bytes.Buffer | ||
|
|
||
| err := runLogout(&out, store, "https://entire.io") | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| if !store.deleted["https://entire.io"] { | ||
| t.Fatal("expected token to be deleted for https://entire.io") | ||
| } | ||
|
|
||
| if !strings.Contains(out.String(), "Logged out.") { | ||
| t.Fatalf("output = %q, want to contain %q", out.String(), "Logged out.") | ||
| } | ||
| } | ||
|
|
||
| func TestRunLogout_ReturnsErrorOnDeleteFailure(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| store := newMockTokenDeleter() | ||
| store.failWith = errors.New("keyring locked") | ||
| var out bytes.Buffer | ||
|
|
||
| err := runLogout(&out, store, "https://entire.io") | ||
| if err == nil { | ||
| t.Fatal("expected error, got nil") | ||
| } | ||
|
|
||
| if !strings.Contains(err.Error(), "keyring locked") { | ||
| t.Fatalf("error = %q, want to contain %q", err.Error(), "keyring locked") | ||
| } | ||
|
|
||
| if strings.Contains(out.String(), "Logged out.") { | ||
| t.Fatal("should not print success message on error") | ||
| } | ||
| } | ||
|
|
||
| func TestLogoutCmd_IsRegistered(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| root := NewRootCmd() | ||
| found := false | ||
| for _, c := range root.Commands() { | ||
| if c.Use == "logout" { | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| t.Fatal("logout command not registered on root") | ||
| } | ||
| } |
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
Oops, something went wrong.
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.