Skip to content

feat: Generate random admin user password in dev mode #1207

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 9 commits into from
Apr 28, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add dev mode test with email/pass from env
  • Loading branch information
mafredri committed Apr 28, 2022
commit 045260dd8b4d48402590116821f55ae4186c3e6d
46 changes: 46 additions & 0 deletions cli/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,52 @@ func TestServer(t *testing.T) {
cancelFunc()
<-done
})
// Duplicated test from "Development" above to test setting email/password via env.
t.Run("Development with email and password from env", func(t *testing.T) {
// Cannot run parallell due to os.Setenv.
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

wantEmail := "myadmin@coder.com"
wantPassword := "testpass42"
os.Setenv("CODER_DEV_ADMIN_EMAIL", wantEmail)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.Setenv handles this for you :-)

defer os.Unsetenv("CODER_DEV_ADMIN_EMAIL")
os.Setenv("CODER_DEV_ADMIN_PASSWORD", wantPassword)
defer os.Unsetenv("CODER_DEV_ADMIN_PASSWORD")

root, cfg := clitest.New(t, "server", "--dev", "--skip-tunnel", "--address", ":0")
var buf strings.Builder
root.SetOutput(&buf)
done := make(chan struct{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: another way to do this is to use sync.WaitGroup:

var wg sync.WaitGroup
go myFunc() {
    ... stuff happens
   wg.Done()
}()
wg.Add(1)
wg.Wait()

go func() {
defer close(done)

err := root.ExecuteContext(ctx)
require.ErrorIs(t, err, context.Canceled)

// Verify that credentials were output to the terminal.
assert.Contains(t, buf.String(), fmt.Sprintf("email: %s", wantEmail), "expected output %q; got no match", wantEmail)
assert.Contains(t, buf.String(), fmt.Sprintf("password: %s", wantPassword), "expected output %q; got no match", wantPassword)
}()
var token string
require.Eventually(t, func() bool {
var err error
token, err = cfg.Session().Read()
return err == nil
}, 15*time.Second, 25*time.Millisecond)
// Verify that authentication was properly set in dev-mode.
accessURL, err := cfg.URL().Read()
require.NoError(t, err)
parsed, err := url.Parse(accessURL)
require.NoError(t, err)
client := codersdk.New(parsed)
client.SessionToken = token
_, err = client.User(ctx, codersdk.Me)
require.NoError(t, err)

cancelFunc()
<-done
})
t.Run("TLSBadVersion", func(t *testing.T) {
t.Parallel()
ctx, cancelFunc := context.WithCancel(context.Background())
Expand Down