mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
bug: Cleaner error message for non logged-in users (#1670)
* add helper text to unauthorized error messages * fix lint error, add unit tests * fix test name * fix test name * fix lint errors in test * add unauthorized test for templates create * remove unnecessary variable * remove Error struct, change error message * change [url] to <url>
This commit is contained in:
+10
@@ -7,6 +7,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/kirsle/configdir"
|
||||
"github.com/mattn/go-isatty"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -113,6 +115,10 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
|
||||
if err != nil || rawURL == "" {
|
||||
rawURL, err = root.URL().Read()
|
||||
if err != nil {
|
||||
// If the configuration files are absent, the user is logged out
|
||||
if os.IsNotExist(err) {
|
||||
return nil, xerrors.New("You are not logged in. Try logging in using 'coder login <url>'.")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -124,6 +130,10 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
|
||||
if err != nil || token == "" {
|
||||
token, err = root.Session().Read()
|
||||
if err != nil {
|
||||
// If the configuration files are absent, the user is logged out
|
||||
if os.IsNotExist(err) {
|
||||
return nil, xerrors.New("You are not logged in. Try logging in using 'coder login <url>'.")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
+39
-14
@@ -7,22 +7,47 @@ import (
|
||||
|
||||
"github.com/coder/coder/cli/clitest"
|
||||
"github.com/coder/coder/coderd/coderdtest"
|
||||
"github.com/coder/coder/codersdk"
|
||||
"github.com/coder/coder/pty/ptytest"
|
||||
)
|
||||
|
||||
func TestUserList(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, nil)
|
||||
coderdtest.CreateFirstUser(t, client)
|
||||
cmd, root := clitest.New(t, "users", "list")
|
||||
clitest.SetupConfig(t, client, root)
|
||||
pty := ptytest.New(t)
|
||||
cmd.SetIn(pty.Input())
|
||||
cmd.SetOut(pty.Output())
|
||||
errC := make(chan error)
|
||||
go func() {
|
||||
errC <- cmd.Execute()
|
||||
}()
|
||||
require.NoError(t, <-errC)
|
||||
pty.ExpectMatch("coder.com")
|
||||
t.Run("List", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, nil)
|
||||
coderdtest.CreateFirstUser(t, client)
|
||||
cmd, root := clitest.New(t, "users", "list")
|
||||
clitest.SetupConfig(t, client, root)
|
||||
pty := ptytest.New(t)
|
||||
cmd.SetIn(pty.Input())
|
||||
cmd.SetOut(pty.Output())
|
||||
errC := make(chan error)
|
||||
go func() {
|
||||
errC <- cmd.Execute()
|
||||
}()
|
||||
require.NoError(t, <-errC)
|
||||
pty.ExpectMatch("coder.com")
|
||||
})
|
||||
t.Run("NoURLFileErrorHasHelperText", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cmd, _ := clitest.New(t, "users", "list")
|
||||
|
||||
_, err := cmd.ExecuteC()
|
||||
|
||||
require.Contains(t, err.Error(), "Try logging in using 'coder login <url>'.")
|
||||
})
|
||||
t.Run("SessionAuthErrorHasHelperText", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := coderdtest.New(t, nil)
|
||||
cmd, root := clitest.New(t, "users", "list")
|
||||
clitest.SetupConfig(t, client, root)
|
||||
|
||||
_, err := cmd.ExecuteC()
|
||||
|
||||
var apiErr *codersdk.Error
|
||||
require.ErrorAs(t, err, &apiErr)
|
||||
require.Contains(t, err.Error(), "Try logging in using 'coder login <url>'.")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user