Right now, the only way to create a chat is through `POST /api/v2/chats`. That's fine for clients, but an integration running inside coderd (say, a Slack bot) would have to store or mint an API key just to call its own server. So, we'll pull chat creation out of the handler into a shared function, and add `api.CreateChatAsUser` for in-process callers. They get the same validation, errors, and audit entry as the endpoint, and the endpoint's behaviour doesn't change. This is the same pattern `chatCreateWorkspace` already uses to let chatd create workspaces as a user. I've also exported the chat daemon for in-process callers. Chat errors are now just values that carry the endpoint's status, message, and cause, so in-process callers can use `errors.As` on them (e.g. to spot a prompt hook denial) rather than decoding an HTTP response. The audit entry is written in the background, as auto-archive already does, rather than through a fake request. As with an API key, a dormant user is activated on use, whilst a suspended or deleted user gets a 403. The PRs above this one do the same for sending, interrupting, and updating chats.
35 lines
884 B
Go
35 lines
884 B
Go
package httperror_test
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/coderd/httpapi/httperror"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
func TestWrapResponseError(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
resp := codersdk.Response{Message: "Not found."}
|
|
err := xerrors.Errorf("lookup: %w", httperror.WrapResponseError(http.StatusNotFound, resp, sql.ErrNoRows))
|
|
|
|
require.ErrorIs(t, err, sql.ErrNoRows)
|
|
responder, ok := httperror.IsResponder(err)
|
|
require.True(t, ok)
|
|
status, got := responder.Response()
|
|
require.Equal(t, http.StatusNotFound, status)
|
|
require.Equal(t, resp, got)
|
|
require.Equal(t, http.StatusNotFound, httperror.StatusCode(err))
|
|
}
|
|
|
|
func TestStatusCode(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
require.Equal(t, http.StatusInternalServerError, httperror.StatusCode(xerrors.New("plain")))
|
|
}
|