Files
coder/cli/whoami_test.go
T
dylanhuff-at-coder db68c6c9fe fix: add codersdk JSON response decoder for typed API endpoints (#27804)
`coder whoami` and `coder list` can surface low-level JSON decode errors
when a reverse proxy, SSO portal, or incorrect Coder URL returns HTML
with a successful HTTP status.

Add a shared SDK JSON response decoder and use it for the user and
workspace list endpoints so these commands return a structured,
actionable API response error instead. Refs #27044

Reviewed and updated by Coder Agents on behalf of @dylanhuff-at-coder.
2026-08-05 10:42:13 -07:00

73 lines
2.0 KiB
Go

package cli_test
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/codersdk"
)
func TestWhoami(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.Run()
require.Error(t, err)
})
t.Run("OK", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
inv, root := clitest.New(t, "whoami")
clitest.SetupConfig(t, client, root)
buf := new(bytes.Buffer)
inv.Stdout = buf
err := inv.Run()
require.NoError(t, err)
whoami := buf.String()
require.NotEmpty(t, whoami)
})
t.Run("HTMLResponse", func(t *testing.T) {
t.Parallel()
// Simulate an SSO portal or misconfigured reverse proxy that
// returns 200 OK with an HTML body instead of JSON.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("<!DOCTYPE html><html><head><title>Sign in</title></head><body>Sign in</body></html>"))
}))
defer srv.Close()
parsedURL, err := url.Parse(srv.URL)
require.NoError(t, err)
client := codersdk.New(parsedURL)
client.SetSessionToken("test-token")
inv, root := clitest.New(t, "whoami")
clitest.SetupConfig(t, client, root)
err = inv.Run()
require.Error(t, err)
var sdkErr *codersdk.Error
require.True(t, errors.As(err, &sdkErr))
require.Equal(t, http.StatusOK, sdkErr.StatusCode())
require.Contains(t, sdkErr.Message, "HTML response instead of JSON")
require.Contains(t, sdkErr.Helper, "/api/v2")
require.NotContains(t, err.Error(), "invalid character")
require.NotContains(t, err.Error(), "unexpected status code")
})
}