51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
package httpapi
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"context"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/auth"
|
||
|
|
"github.com/go-chi/chi/v5"
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestHandleAdminSetStaffRoleRejectsSelf(t *testing.T) {
|
||
|
|
t.Parallel()
|
||
|
|
uid := uuid.MustParse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")
|
||
|
|
s := &Server{Auth: &auth.Service{}}
|
||
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/admin/users/"+uid.String()+"/staff-role",
|
||
|
|
bytes.NewBufferString(`{"staff_role":"support_staff"}`))
|
||
|
|
ctx := context.WithValue(context.Background(), ctxUserID, uid)
|
||
|
|
rctx := chi.NewRouteContext()
|
||
|
|
rctx.URLParams.Add("id", uid.String())
|
||
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, rctx)
|
||
|
|
req = req.WithContext(ctx)
|
||
|
|
rec := httptest.NewRecorder()
|
||
|
|
s.handleAdminSetStaffRole(rec, req)
|
||
|
|
if rec.Code != http.StatusForbidden {
|
||
|
|
t.Fatalf("status = %d body=%s, want 403", rec.Code, rec.Body.String())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestHandleAdminSetStaffRoleInvalidJSON(t *testing.T) {
|
||
|
|
t.Parallel()
|
||
|
|
actor := uuid.MustParse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")
|
||
|
|
target := uuid.MustParse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb")
|
||
|
|
s := &Server{Auth: &auth.Service{}}
|
||
|
|
req := httptest.NewRequest(http.MethodPatch, "/api/admin/users/"+target.String()+"/staff-role",
|
||
|
|
bytes.NewBufferString(`{`))
|
||
|
|
ctx := context.WithValue(context.Background(), ctxUserID, actor)
|
||
|
|
rctx := chi.NewRouteContext()
|
||
|
|
rctx.URLParams.Add("id", target.String())
|
||
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, rctx)
|
||
|
|
req = req.WithContext(ctx)
|
||
|
|
rec := httptest.NewRecorder()
|
||
|
|
s.handleAdminSetStaffRole(rec, req)
|
||
|
|
if rec.Code != http.StatusBadRequest {
|
||
|
|
t.Fatalf("status = %d, want 400", rec.Code)
|
||
|
|
}
|
||
|
|
}
|