58 lines
1.8 KiB
Go
58 lines
1.8 KiB
Go
package httpapi
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"context"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/go-chi/chi/v5"
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestHandleUpdateAPIKeyRejectsEmptyName(t *testing.T) {
|
||
|
|
t.Parallel()
|
||
|
|
s := &Server{}
|
||
|
|
cid := uuid.MustParse("11111111-1111-1111-1111-111111111111")
|
||
|
|
uid := uuid.MustParse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")
|
||
|
|
keyID := uuid.MustParse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb")
|
||
|
|
|
||
|
|
ctx := context.WithValue(context.Background(), ctxUserID, uid)
|
||
|
|
ctx = context.WithValue(ctx, ctxCompanyID, cid)
|
||
|
|
ctx = context.WithValue(ctx, ctxRole, "admin")
|
||
|
|
|
||
|
|
rctx := chi.NewRouteContext()
|
||
|
|
rctx.URLParams.Add("id", keyID.String())
|
||
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, rctx)
|
||
|
|
|
||
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/api-keys/"+keyID.String(), bytes.NewBufferString(`{"name":" "}`)).WithContext(ctx)
|
||
|
|
rec := httptest.NewRecorder()
|
||
|
|
s.handleUpdateAPIKey(rec, req)
|
||
|
|
if rec.Code != http.StatusBadRequest {
|
||
|
|
t.Fatalf("status = %d body=%s, want 400", rec.Code, rec.Body.String())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestHandleUpdateAPIKeyRejectsInvalidID(t *testing.T) {
|
||
|
|
t.Parallel()
|
||
|
|
s := &Server{}
|
||
|
|
cid := uuid.MustParse("11111111-1111-1111-1111-111111111111")
|
||
|
|
uid := uuid.MustParse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")
|
||
|
|
|
||
|
|
ctx := context.WithValue(context.Background(), ctxUserID, uid)
|
||
|
|
ctx = context.WithValue(ctx, ctxCompanyID, cid)
|
||
|
|
ctx = context.WithValue(ctx, ctxRole, "admin")
|
||
|
|
|
||
|
|
rctx := chi.NewRouteContext()
|
||
|
|
rctx.URLParams.Add("id", "not-a-uuid")
|
||
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, rctx)
|
||
|
|
|
||
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/api-keys/not-a-uuid", bytes.NewBufferString(`{"name":"ok"}`)).WithContext(ctx)
|
||
|
|
rec := httptest.NewRecorder()
|
||
|
|
s.handleUpdateAPIKey(rec, req)
|
||
|
|
if rec.Code != http.StatusBadRequest {
|
||
|
|
t.Fatalf("status = %d body=%s, want 400", rec.Code, rec.Body.String())
|
||
|
|
}
|
||
|
|
}
|