2022-05-12 17:32:56 +00:00
|
|
|
package cli_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/url"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2022-05-24 07:58:39 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-05-12 17:32:56 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/coder/coder/cli/clitest"
|
|
|
|
"github.com/coder/coder/coderd/database/postgres"
|
|
|
|
"github.com/coder/coder/codersdk"
|
|
|
|
"github.com/coder/coder/pty/ptytest"
|
2022-08-01 12:45:05 +00:00
|
|
|
"github.com/coder/coder/testutil"
|
2022-05-12 17:32:56 +00:00
|
|
|
)
|
|
|
|
|
2022-05-18 16:33:33 +00:00
|
|
|
// nolint:paralleltest
|
2022-05-12 17:32:56 +00:00
|
|
|
func TestResetPassword(t *testing.T) {
|
2022-05-18 16:33:33 +00:00
|
|
|
// postgres.Open() seems to be creating race conditions when run in parallel.
|
|
|
|
// t.Parallel()
|
2022-05-12 17:32:56 +00:00
|
|
|
|
|
|
|
if runtime.GOOS != "linux" || testing.Short() {
|
|
|
|
// Skip on non-Linux because it spawns a PostgreSQL instance.
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
const email = "some@one.com"
|
|
|
|
const username = "example"
|
|
|
|
const oldPassword = "password"
|
|
|
|
const newPassword = "password2"
|
|
|
|
|
|
|
|
// start postgres and coder server processes
|
|
|
|
|
|
|
|
connectionURL, closeFunc, err := postgres.Open()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer closeFunc()
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
serverDone := make(chan struct{})
|
2022-07-25 16:24:32 +00:00
|
|
|
serverCmd, cfg := clitest.New(t,
|
|
|
|
"server",
|
|
|
|
"--address", ":0",
|
2022-10-07 13:05:56 +00:00
|
|
|
"--access-url", "example.com",
|
2022-07-25 16:24:32 +00:00
|
|
|
"--postgres-url", connectionURL,
|
|
|
|
"--cache-dir", t.TempDir(),
|
|
|
|
)
|
2022-05-12 17:32:56 +00:00
|
|
|
go func() {
|
|
|
|
defer close(serverDone)
|
|
|
|
err = serverCmd.ExecuteContext(ctx)
|
2022-09-20 04:56:51 +00:00
|
|
|
assert.NoError(t, err)
|
2022-05-12 17:32:56 +00:00
|
|
|
}()
|
2022-07-22 17:02:49 +00:00
|
|
|
var rawURL string
|
2022-05-12 17:32:56 +00:00
|
|
|
require.Eventually(t, func() bool {
|
2022-07-22 17:02:49 +00:00
|
|
|
rawURL, err = cfg.URL().Read()
|
|
|
|
return err == nil && rawURL != ""
|
2022-08-01 12:45:05 +00:00
|
|
|
}, testutil.WaitLong, testutil.IntervalFast)
|
2022-07-22 17:02:49 +00:00
|
|
|
accessURL, err := url.Parse(rawURL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
client := codersdk.New(accessURL)
|
|
|
|
|
2022-05-12 17:32:56 +00:00
|
|
|
_, err = client.CreateFirstUser(ctx, codersdk.CreateFirstUserRequest{
|
|
|
|
Email: email,
|
|
|
|
Username: username,
|
|
|
|
Password: oldPassword,
|
|
|
|
OrganizationName: "example",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// reset the password
|
|
|
|
|
|
|
|
resetCmd, cmdCfg := clitest.New(t, "reset-password", "--postgres-url", connectionURL, username)
|
|
|
|
clitest.SetupConfig(t, client, cmdCfg)
|
|
|
|
cmdDone := make(chan struct{})
|
|
|
|
pty := ptytest.New(t)
|
|
|
|
resetCmd.SetIn(pty.Input())
|
|
|
|
resetCmd.SetOut(pty.Output())
|
|
|
|
go func() {
|
|
|
|
defer close(cmdDone)
|
|
|
|
err = resetCmd.Execute()
|
2022-05-24 07:58:39 +00:00
|
|
|
assert.NoError(t, err)
|
2022-05-12 17:32:56 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
matches := []struct {
|
|
|
|
output string
|
|
|
|
input string
|
|
|
|
}{
|
|
|
|
{"Enter new", newPassword},
|
|
|
|
{"Confirm", newPassword},
|
|
|
|
}
|
|
|
|
for _, match := range matches {
|
|
|
|
pty.ExpectMatch(match.output)
|
|
|
|
pty.WriteLine(match.input)
|
|
|
|
}
|
|
|
|
<-cmdDone
|
|
|
|
|
|
|
|
// now try logging in
|
|
|
|
|
|
|
|
_, err = client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{
|
|
|
|
Email: email,
|
|
|
|
Password: oldPassword,
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
_, err = client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{
|
|
|
|
Email: email,
|
|
|
|
Password: newPassword,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cancelFunc()
|
|
|
|
<-serverDone
|
|
|
|
}
|