<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #37192 - Move /server/service/middleware/endpoint_utils to /server/platform/endpointer - Move /server/service/middleware/authzcheck to /server/platform/middleware/authzcheck - Move /server/service/middleware/ratelimit to /server/platform/middleware/ratelimit # Checklist for submitter - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Refactor** * Reorganized internal endpoint utilities to a centralized platform location for improved code organization and maintainability. No functional changes to existing features or APIs. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
116 lines
2.2 KiB
Go
116 lines
2.2 KiB
Go
package endpointer
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
platform_http "github.com/fleetdm/fleet/v4/server/platform/http"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type foreignKeyError struct{}
|
|
|
|
func (foreignKeyError) IsForeignKey() bool { return true }
|
|
func (foreignKeyError) Error() string { return "" }
|
|
|
|
type alreadyExists struct{}
|
|
|
|
func (alreadyExists) IsExists() bool { return false }
|
|
func (alreadyExists) Error() string { return "" }
|
|
|
|
type newAndExciting struct{}
|
|
|
|
func (newAndExciting) Error() string { return "" }
|
|
|
|
type notFoundError struct {
|
|
platform_http.ErrorWithUUID
|
|
}
|
|
|
|
func (e *notFoundError) Error() string {
|
|
return "not found"
|
|
}
|
|
|
|
func (e *notFoundError) IsNotFound() bool {
|
|
return true
|
|
}
|
|
|
|
// validationError is a test implementation of validationErrorInterface.
|
|
type validationError struct {
|
|
errors []map[string]string
|
|
}
|
|
|
|
func (e validationError) Error() string {
|
|
return "validation failed"
|
|
}
|
|
|
|
func (e validationError) Invalid() []map[string]string {
|
|
return e.errors
|
|
}
|
|
|
|
// permissionError is a test implementation of permissionErrorInterface.
|
|
type permissionError struct {
|
|
message string
|
|
}
|
|
|
|
func (e permissionError) Error() string {
|
|
return e.message
|
|
}
|
|
|
|
func (e permissionError) PermissionError() []map[string]string {
|
|
return nil
|
|
}
|
|
|
|
func TestHandlesErrorsCode(t *testing.T) {
|
|
errorTests := []struct {
|
|
name string
|
|
err error
|
|
code int
|
|
}{
|
|
{
|
|
"validation",
|
|
validationError{errors: []map[string]string{{"name": "a", "reason": "b"}}},
|
|
http.StatusUnprocessableEntity,
|
|
},
|
|
{
|
|
"permission",
|
|
permissionError{message: "a"},
|
|
http.StatusForbidden,
|
|
},
|
|
{
|
|
"foreign key",
|
|
foreignKeyError{},
|
|
http.StatusUnprocessableEntity,
|
|
},
|
|
{
|
|
"data not found",
|
|
¬FoundError{},
|
|
http.StatusNotFound,
|
|
},
|
|
{
|
|
"already exists",
|
|
alreadyExists{},
|
|
http.StatusConflict,
|
|
},
|
|
{
|
|
"status coder",
|
|
platform_http.NewAuthFailedError(""),
|
|
http.StatusUnauthorized,
|
|
},
|
|
{
|
|
"default",
|
|
newAndExciting{},
|
|
http.StatusInternalServerError,
|
|
},
|
|
}
|
|
|
|
for _, tt := range errorTests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
EncodeError(context.Background(), tt.err, recorder, nil)
|
|
assert.Equal(t, recorder.Code, tt.code)
|
|
})
|
|
}
|
|
}
|