fix: ensure we are talking to coder on first user check (#11130)

This commit is contained in:
Garrett Delfosse
2023-12-11 14:27:32 -05:00
committed by GitHub
parent e37bbe6208
commit b7ea330aea
2 changed files with 43 additions and 0 deletions
+35
View File
@@ -3,6 +3,8 @@ package cli_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"runtime"
"testing"
@@ -36,6 +38,39 @@ func TestLogin(t *testing.T) {
require.ErrorContains(t, err, errMsg)
})
t.Run("InitialUserNonCoderURLFail", func(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Not Found"))
}))
defer ts.Close()
badLoginURL := ts.URL
root, _ := clitest.New(t, "login", badLoginURL)
err := root.Run()
errMsg := fmt.Sprintf("Failed to check server %q for first user, is the URL correct and is coder accessible from your browser?", badLoginURL)
require.ErrorContains(t, err, errMsg)
})
t.Run("InitialUserNonCoderURLSuccess", func(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Coder-Build-Version", "something")
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Not Found"))
}))
defer ts.Close()
badLoginURL := ts.URL
root, _ := clitest.New(t, "login", badLoginURL)
err := root.Run()
// this means we passed the check for a valid coder server
require.ErrorContains(t, err, "the initial user cannot be created in non-interactive mode")
})
t.Run("InitialUserTTY", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)