Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prompt handler tests #226

Merged
merged 1 commit into from
Dec 27, 2024
Merged
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
54 changes: 26 additions & 28 deletions api/handler/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
)

type PromptHandler struct {
pc component.PromptComponent
sc component.SensitiveComponent
repo component.RepoComponent
prompt component.PromptComponent
sensitive component.SensitiveComponent
repo component.RepoComponent
}

func NewPromptHandler(cfg *config.Config) (*PromptHandler, error) {
Expand All @@ -33,13 +33,12 @@ func NewPromptHandler(cfg *config.Config) (*PromptHandler, error) {
}
repo, err := component.NewRepoComponent(cfg)
if err != nil {
return nil, fmt.Errorf("error creating repo component:%w", err)
return nil, fmt.Errorf("failed to create repo component: %w", err)
}

return &PromptHandler{
pc: promptComp,
sc: sc,
repo: repo,
prompt: promptComp,
sensitive: sc,
repo: repo,
}, nil
}

Expand Down Expand Up @@ -89,7 +88,7 @@ func (h *PromptHandler) Index(ctx *gin.Context) {
return
}

prompts, total, err := h.pc.IndexPromptRepo(ctx, filter, per, page)
prompts, total, err := h.prompt.IndexPromptRepo(ctx, filter, per, page)
if err != nil {
slog.Error("Failed to get prompts dataset", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -125,7 +124,7 @@ func (h *PromptHandler) ListPrompt(ctx *gin.Context) {
return
}

detail, err := h.pc.Show(ctx, namespace, name, currentUser)
detail, err := h.prompt.Show(ctx, namespace, name, currentUser)
if err != nil {
if errors.Is(err, component.ErrUnauthorized) {
httpbase.UnauthorizedError(ctx, err)
Expand All @@ -141,7 +140,7 @@ func (h *PromptHandler) ListPrompt(ctx *gin.Context) {
Name: name,
CurrentUser: currentUser,
}
data, err := h.pc.ListPrompt(ctx, req)
data, err := h.prompt.ListPrompt(ctx, req)
if err != nil {
slog.Error("Failed to list prompts of repo", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -190,7 +189,7 @@ func (h *PromptHandler) GetPrompt(ctx *gin.Context) {
CurrentUser: currentUser,
Path: convertFilePathFromRoute(filePath),
}
data, err := h.pc.GetPrompt(ctx, req)
data, err := h.prompt.GetPrompt(ctx, req)
if err != nil {
slog.Error("Failed to get prompt of repo", slog.Any("req", req), slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -232,7 +231,7 @@ func (h *PromptHandler) CreatePrompt(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
_, err = h.sc.CheckRequestV2(ctx, body)
_, err = h.sensitive.CheckRequestV2(ctx, body)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand All @@ -244,8 +243,7 @@ func (h *PromptHandler) CreatePrompt(ctx *gin.Context) {
Name: name,
CurrentUser: currentUser,
}

data, err := h.pc.CreatePrompt(ctx, req, body)
data, err := h.prompt.CreatePrompt(ctx, req, body)
if err != nil {
slog.Error("Failed to create prompt file of repo", slog.Any("req", req), slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -294,7 +292,7 @@ func (h *PromptHandler) UpdatePrompt(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
_, err = h.sc.CheckRequestV2(ctx, body)
_, err = h.sensitive.CheckRequestV2(ctx, body)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand All @@ -307,7 +305,7 @@ func (h *PromptHandler) UpdatePrompt(ctx *gin.Context) {
CurrentUser: currentUser,
Path: convertFilePathFromRoute(filePath),
}
data, err := h.pc.UpdatePrompt(ctx, req, body)
data, err := h.prompt.UpdatePrompt(ctx, req, body)
if err != nil {
slog.Error("Failed to update prompt file of repo", slog.Any("req", req), slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -357,7 +355,7 @@ func (h *PromptHandler) DeletePrompt(ctx *gin.Context) {
CurrentUser: currentUser,
Path: convertFilePathFromRoute(filePath),
}
err = h.pc.DeletePrompt(ctx, req)
err = h.prompt.DeletePrompt(ctx, req)
if err != nil {
slog.Error("Failed to remove prompt file of repo", slog.Any("req", req), slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -387,7 +385,7 @@ func (h *PromptHandler) Relations(ctx *gin.Context) {
return
}
currentUser := httpbase.GetCurrentUser(ctx)
detail, err := h.pc.Relations(ctx, namespace, name, currentUser)
detail, err := h.prompt.Relations(ctx, namespace, name, currentUser)
if err != nil {
if errors.Is(err, component.ErrUnauthorized) {
httpbase.UnauthorizedError(ctx, err)
Expand Down Expand Up @@ -439,7 +437,7 @@ func (h *PromptHandler) SetRelations(ctx *gin.Context) {
req.Name = name
req.CurrentUser = currentUser

err = h.pc.SetRelationModels(ctx, req)
err = h.prompt.SetRelationModels(ctx, req)
if err != nil {
slog.Error("Failed to set models for prompt", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -486,7 +484,7 @@ func (h *PromptHandler) AddModelRelation(ctx *gin.Context) {
req.Name = name
req.CurrentUser = currentUser

err = h.pc.AddRelationModel(ctx, req)
err = h.prompt.AddRelationModel(ctx, req)
if err != nil {
slog.Error("Failed to add model for prompt", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -533,7 +531,7 @@ func (h *PromptHandler) DelModelRelation(ctx *gin.Context) {
req.Name = name
req.CurrentUser = currentUser

err = h.pc.DelRelationModel(ctx, req)
err = h.prompt.DelRelationModel(ctx, req)
if err != nil {
slog.Error("Failed to delete dataset for model", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -567,15 +565,15 @@ func (h *PromptHandler) Create(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
_, err := h.sc.CheckRequestV2(ctx, req)
_, err := h.sensitive.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
return
}
req.Username = currentUser

prompt, err := h.pc.CreatePromptRepo(ctx, req)
prompt, err := h.prompt.CreatePromptRepo(ctx, req)
if err != nil {
slog.Error("Failed to create prompt repo", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -616,7 +614,7 @@ func (h *PromptHandler) Update(ctx *gin.Context) {
return
}

_, err := h.sc.CheckRequestV2(ctx, req)
_, err := h.sensitive.CheckRequestV2(ctx, req)
if err != nil {
slog.Error("failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error())
Expand All @@ -633,7 +631,7 @@ func (h *PromptHandler) Update(ctx *gin.Context) {
req.Namespace = namespace
req.Name = name

prompt, err := h.pc.UpdatePromptRepo(ctx, req)
prompt, err := h.prompt.UpdatePromptRepo(ctx, req)
if err != nil {
slog.Error("Failed to update prompt repo", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -669,7 +667,7 @@ func (h *PromptHandler) Delete(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
err = h.pc.RemoveRepo(ctx, namespace, name, currentUser)
err = h.prompt.RemoveRepo(ctx, namespace, name, currentUser)
if err != nil {
slog.Error("Failed to delete prompt repo", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -778,7 +776,7 @@ func (h *PromptHandler) Tags(ctx *gin.Context) {
func (h *PromptHandler) UpdateTags(ctx *gin.Context) {
currentUser := httpbase.GetCurrentUser(ctx)
if currentUser == "" {
httpbase.UnauthorizedError(ctx, httpbase.ErrorNeedLogin)
httpbase.UnauthorizedError(ctx, component.ErrUserNotFound)
return
}
namespace, name, err := common.GetNamespaceAndNameFromContext(ctx)
Expand Down
Loading
Loading