-
Notifications
You must be signed in to change notification settings - Fork 12
Handle OAuth denial and deduplicate login output #260
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
273ecb2
Handle OAuth consent denial without missing-code errors
rgarcia ace1e30
Print one final login result
rgarcia d45fff2
Clear login spinner before final status
rgarcia df635cd
Merge remote-tracking branch 'origin/main' into hypeship/oauth-denial…
rgarcia d99e4da
Tighten login cancellation and spinner coverage
rgarcia 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,197 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/charmbracelet/fang" | ||
| "github.com/charmbracelet/lipgloss/v2" | ||
| "github.com/kernel/cli/pkg/auth" | ||
| "github.com/pterm/pterm" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type testSpinner struct { | ||
| stops int | ||
| } | ||
|
|
||
| func (s *testSpinner) Stop() error { | ||
| s.stops++ | ||
| return nil | ||
| } | ||
|
|
||
| func TestCompleteLoginOutput(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| authErr error | ||
| saveErr error | ||
| cancel bool | ||
| wantErr bool | ||
| wantSave bool | ||
| wantSuccess int | ||
| wantErrors int | ||
| wantOutput []string | ||
| absentOutput []string | ||
| }{ | ||
| { | ||
| name: "success", | ||
| wantSave: true, | ||
| wantSuccess: 1, | ||
| wantOutput: []string{ | ||
| "Successfully authenticated with Kernel!", | ||
| "You can now use other Kernel CLI commands without setting KERNEL_API_KEY", | ||
| }, | ||
| absentOutput: []string{"Authentication successful!", "ERROR"}, | ||
| }, | ||
| { | ||
| name: "consent denial", | ||
| authErr: auth.ErrAuthorizationDenied, | ||
| wantErr: true, | ||
| wantErrors: 1, | ||
| wantOutput: []string{"authorization denied; no new credentials were saved"}, | ||
| absentOutput: []string{ | ||
| "Authentication failed", | ||
| "Successfully authenticated", | ||
| }, | ||
| }, | ||
| { | ||
| name: "generic authentication failure", | ||
| authErr: errors.New("callback server failed"), | ||
| wantErr: true, | ||
| wantErrors: 1, | ||
| wantOutput: []string{"authentication failed: callback server failed"}, | ||
| absentOutput: []string{ | ||
| "Successfully authenticated", | ||
| }, | ||
| }, | ||
| { | ||
| name: "credential save failure", | ||
| saveErr: errors.New("credential store unavailable"), | ||
| wantErr: true, | ||
| wantSave: true, | ||
| wantErrors: 1, | ||
| wantOutput: []string{ | ||
| "OAuth authorization completed, but credentials could not be saved: credential store unavailable", | ||
| "fix credential storage and run 'kernel login' again", | ||
| }, | ||
| absentOutput: []string{"SUCCESS", "Successfully authenticated"}, | ||
| }, | ||
| { | ||
| name: "cancellation", | ||
| authErr: context.Canceled, | ||
| cancel: true, | ||
| wantErr: true, | ||
| wantErrors: 1, | ||
| wantOutput: []string{"authentication cancelled by user"}, | ||
| absentOutput: []string{ | ||
| "authentication failed", | ||
| "Successfully authenticated", | ||
| }, | ||
| }, | ||
| { | ||
| name: "ambient cancellation does not mask authentication failure", | ||
| authErr: errors.New("callback server failed"), | ||
| cancel: true, | ||
| wantErr: true, | ||
| wantErrors: 1, | ||
| wantOutput: []string{"authentication failed: callback server failed"}, | ||
| absentOutput: []string{ | ||
| "authentication cancelled", | ||
| "Successfully authenticated", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| output := capturePtermOutput(t) | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| if tt.cancel { | ||
| cancel() | ||
| } else { | ||
| defer cancel() | ||
| } | ||
|
|
||
| spinner := &testSpinner{} | ||
| saveCalled := false | ||
| err := completeLogin( | ||
| ctx, | ||
| spinner, | ||
| func(context.Context) (*auth.TokenStorage, error) { | ||
| return &auth.TokenStorage{}, tt.authErr | ||
| }, | ||
| func(*auth.TokenStorage) error { | ||
| saveCalled = true | ||
| return tt.saveErr | ||
| }, | ||
| ) | ||
|
|
||
| if tt.wantErr { | ||
| require.Error(t, err) | ||
| renderCommandError(output, fang.Styles{ | ||
| ErrorText: lipgloss.NewStyle(), | ||
| Program: fang.Program{Flag: lipgloss.NewStyle()}, | ||
| }, err) | ||
| } else { | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| rendered := ansiEscapes.ReplaceAllString(output.String(), "") | ||
| assert.Equal(t, 1, spinner.stops) | ||
|
rgarcia marked this conversation as resolved.
|
||
| assert.Equal(t, tt.wantSave, saveCalled) | ||
| assert.Equal(t, tt.wantSuccess, strings.Count(rendered, "SUCCESS"), rendered) | ||
| assert.Equal(t, tt.wantErrors, strings.Count(rendered, "ERROR"), rendered) | ||
| for _, want := range tt.wantOutput { | ||
| assert.Contains(t, rendered, want) | ||
| } | ||
| for _, absent := range tt.absentOutput { | ||
| assert.NotContains(t, rendered, absent) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestLoginSpinnerClearsWaitingOutput(t *testing.T) { | ||
| rawOutput := pterm.RawOutput | ||
| pterm.RawOutput = false | ||
| t.Cleanup(func() { pterm.RawOutput = rawOutput }) | ||
|
|
||
| var output bytes.Buffer | ||
| spinner := newLoginSpinner().WithWriter(&output) | ||
| spinner.IsActive = true | ||
| spinner.UpdateText("Waiting for authentication...") | ||
| require.Contains(t, output.String(), "Waiting for authentication...") | ||
|
|
||
| require.NoError(t, spinner.Stop()) | ||
| assert.NotContains(t, visibleTerminalOutput(output.String()), "Waiting for authentication...") | ||
| } | ||
|
|
||
| func visibleTerminalOutput(output string) string { | ||
| var visible strings.Builder | ||
| line := make([]rune, 0, len(output)) | ||
| cursor := 0 | ||
| for _, r := range output { | ||
| switch r { | ||
| case '\r': | ||
| cursor = 0 | ||
| case '\n': | ||
| visible.WriteString(string(line)) | ||
| visible.WriteRune('\n') | ||
| line = line[:0] | ||
| cursor = 0 | ||
| default: | ||
| if cursor == len(line) { | ||
| line = append(line, r) | ||
| } else { | ||
| line[cursor] = r | ||
| } | ||
| cursor++ | ||
| } | ||
| } | ||
| visible.WriteString(string(line)) | ||
| return visible.String() | ||
| } | ||
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.
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.