mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +08:00
* Add client for agent * Cleanup code * Fix linting error * Rename routes to be simpler * Rename workspace history to workspace build * Refactor HTTP middlewares to use UUIDs * Cleanup routes * Compiles! * Fix files and organizations * Fix querying * Fix agent lock * Cleanup database abstraction * Add parameters * Fix linting errors * Fix log race * Lock on close wait * Fix log cleanup * Fix e2e tests * Fix upstream version of opencensus-go * Update coderdtest.go * Fix coverpkg * Fix codecov ignore
93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/cli/clitest"
|
|
"github.com/coder/coder/coderd/coderdtest"
|
|
"github.com/coder/coder/pty/ptytest"
|
|
)
|
|
|
|
func TestLogin(t *testing.T) {
|
|
t.Parallel()
|
|
t.Run("InitialUserNoTTY", func(t *testing.T) {
|
|
t.Parallel()
|
|
client := coderdtest.New(t, nil)
|
|
root, _ := clitest.New(t, "login", client.URL.String())
|
|
err := root.Execute()
|
|
require.Error(t, err)
|
|
})
|
|
|
|
t.Run("InitialUserTTY", func(t *testing.T) {
|
|
t.Parallel()
|
|
client := coderdtest.New(t, nil)
|
|
// The --force-tty flag is required on Windows, because the `isatty` library does not
|
|
// accurately detect Windows ptys when they are not attached to a process:
|
|
// https://github.com/mattn/go-isatty/issues/59
|
|
root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty")
|
|
pty := ptytest.New(t)
|
|
root.SetIn(pty.Input())
|
|
root.SetOut(pty.Output())
|
|
go func() {
|
|
err := root.Execute()
|
|
require.NoError(t, err)
|
|
}()
|
|
|
|
matches := []string{
|
|
"first user?", "y",
|
|
"username", "testuser",
|
|
"organization", "testorg",
|
|
"email", "user@coder.com",
|
|
"password", "password",
|
|
}
|
|
for i := 0; i < len(matches); i += 2 {
|
|
match := matches[i]
|
|
value := matches[i+1]
|
|
pty.ExpectMatch(match)
|
|
pty.WriteLine(value)
|
|
}
|
|
pty.ExpectMatch("Welcome to Coder")
|
|
})
|
|
|
|
t.Run("ExistingUserValidTokenTTY", func(t *testing.T) {
|
|
t.Parallel()
|
|
client := coderdtest.New(t, nil)
|
|
coderdtest.CreateFirstUser(t, client)
|
|
|
|
root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty", "--no-open")
|
|
pty := ptytest.New(t)
|
|
root.SetIn(pty.Input())
|
|
root.SetOut(pty.Output())
|
|
go func() {
|
|
err := root.Execute()
|
|
require.NoError(t, err)
|
|
}()
|
|
|
|
pty.ExpectMatch("Paste your token here:")
|
|
pty.WriteLine(client.SessionToken)
|
|
pty.ExpectMatch("Welcome to Coder")
|
|
})
|
|
|
|
t.Run("ExistingUserInvalidTokenTTY", func(t *testing.T) {
|
|
t.Parallel()
|
|
client := coderdtest.New(t, nil)
|
|
coderdtest.CreateFirstUser(t, client)
|
|
|
|
root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty", "--no-open")
|
|
pty := ptytest.New(t)
|
|
root.SetIn(pty.Input())
|
|
root.SetOut(pty.Output())
|
|
go func() {
|
|
err := root.Execute()
|
|
// An error is expected in this case, since the login wasn't successful:
|
|
require.Error(t, err)
|
|
}()
|
|
|
|
pty.ExpectMatch("Paste your token here:")
|
|
pty.WriteLine("an-invalid-token")
|
|
pty.ExpectMatch("That's not a valid token!")
|
|
})
|
|
}
|