Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions internal/application/service/knowledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ import (

// Error definitions for knowledge service operations
var (
// ErrInvalidFileType is returned when an unsupported file type is provided
ErrInvalidFileType = errors.New("unsupported file type")
// ErrInvalidFileType is returned when an unsupported file type is provided.
// It is a client error and the upload handlers forward it to the HTTP layer
// unchanged, so it has to be an AppError: a plain error would fall into the
// handler's catch-all branch and be reported as HTTP 500.
ErrInvalidFileType = werrors.NewBadRequestError("unsupported file type")
// ErrInvalidURL is returned when an invalid URL is provided
ErrInvalidURL = errors.New("invalid URL")
// ErrChunkNotFound is returned when a requested chunk cannot be found.
Expand Down
92 changes: 92 additions & 0 deletions internal/handler/knowledge_upload_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package handler

import (
"bytes"
"context"
"mime/multipart"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/Tencent/WeKnora/internal/application/service"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/stretchr/testify/require"
)

type uploadTypeKnowledgeService struct {
interfaces.KnowledgeService
}

func (s *uploadTypeKnowledgeService) CreateKnowledgeFromFile(
_ context.Context,
_ string,
file *multipart.FileHeader,
_ map[string]string,
_ *bool,
_ string,
_ []string,
_ string,
_ *types.KnowledgeProcessOverrides,
) (*types.Knowledge, error) {
// Same gate as the service: an unsupported extension rejects the upload.
if strings.HasSuffix(file.Filename, ".exe") {
return nil, service.ErrInvalidFileType
}
return &types.Knowledge{ID: "doc", Title: file.Filename}, nil
}

func uploadFileRequest(t *testing.T, filename string) *http.Request {
t.Helper()
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, err := writer.CreateFormFile("file", filename)
require.NoError(t, err)
_, err = part.Write([]byte("payload"))
require.NoError(t, err)
require.NoError(t, writer.Close())

req := httptest.NewRequest(http.MethodPost, "/kb/knowledge/file", &body)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req
}

// POST /knowledge-bases/:id/knowledge/file answers 400 for an unsupported
// extension. The service returns ErrInvalidFileType for such files, and the
// handler only forwards *AppError unchanged; a plain sentinel falls into the
// catch-all branch and is reported as HTTP 500.
func TestCreateKnowledgeFromFileRejectsUnsupportedType(t *testing.T) {
h := &KnowledgeHandler{
kgService: &uploadTypeKnowledgeService{},
kbService: &stubKBService{get: func(context.Context, string) (*types.KnowledgeBase, error) {
return &types.KnowledgeBase{ID: "kb", TenantID: 7}, nil
}},
}
r := documentHandlerRouter()
r.POST("/:id/knowledge/file", h.CreateKnowledgeFromFile)

w := httptest.NewRecorder()
r.ServeHTTP(w, uploadFileRequest(t, "malware.exe"))

require.Equal(t, http.StatusBadRequest, w.Code, w.Body.String())
require.Contains(t, w.Body.String(), "unsupported file type")
}

// A supported extension still goes through, so the type check cannot reject
// valid uploads.
func TestCreateKnowledgeFromFileAcceptsSupportedType(t *testing.T) {
h := &KnowledgeHandler{
kgService: &uploadTypeKnowledgeService{},
kbService: &stubKBService{get: func(context.Context, string) (*types.KnowledgeBase, error) {
return &types.KnowledgeBase{ID: "kb", TenantID: 7}, nil
}},
}
r := documentHandlerRouter()
r.POST("/:id/knowledge/file", h.CreateKnowledgeFromFile)

w := httptest.NewRecorder()
r.ServeHTTP(w, uploadFileRequest(t, "notes.md"))

require.Equal(t, http.StatusOK, w.Code, w.Body.String())
}