fix(api/v2): return ErrProjectDoesNotExist for unknown project identifiers

This commit is contained in:
kolaente 2026-06-10 09:30:42 +02:00 committed by kolaente
parent 0a879e56a8
commit 5cdc785b49
3 changed files with 17 additions and 7 deletions

View File

@ -448,6 +448,20 @@ func GetProjectSimpleByID(s *xorm.Session, projectID int64) (project *Project, e
return
}
// GetProjectSimpleByIdentifier gets a project by its textual identifier (e.g. "PROJ").
// Identifiers are stored uppercase, so the lookup normalizes the input.
func GetProjectSimpleByIdentifier(s *xorm.Session, identifier string) (project *Project, err error) {
project, exists, err := getProjectSimple(s, builder.Eq{"identifier": strings.ToUpper(identifier)})
if err != nil {
return nil, err
}
if !exists {
return nil, ErrProjectDoesNotExist{}
}
return
}
func getProjectSimple(s *xorm.Session, cond builder.Cond) (project *Project, exists bool, err error) {
project = &Project{}
exists, err = s.

View File

@ -19,7 +19,6 @@ package apiv2
import (
"context"
"strconv"
"strings"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/models"
@ -218,13 +217,9 @@ func resolveProjectIdentifier(raw string) (int64, error) {
}
s := db.NewSession()
defer s.Close()
project := &models.Project{}
has, err := s.Where("identifier = ?", strings.ToUpper(raw)).Get(project)
project, err := models.GetProjectSimpleByIdentifier(s, raw)
if err != nil {
return 0, translateDomainError(err)
}
if !has {
return 0, huma.Error404NotFound("Project not found")
}
return project.ID, nil
}

View File

@ -204,9 +204,10 @@ func TestHumaTask_ReadByIndex(t *testing.T) {
require.Equal(t, http.StatusOK, rec.Code, "body: %s", rec.Body.String())
assert.Contains(t, rec.Body.String(), `"id":1`)
})
t.Run("Unknown identifier returns 404", func(t *testing.T) {
t.Run("Unknown identifier returns ErrProjectDoesNotExist", func(t *testing.T) {
rec := get("does-not-exist", "1")
assert.Equal(t, http.StatusNotFound, rec.Code, "body: %s", rec.Body.String())
assert.Contains(t, rec.Body.String(), fmt.Sprintf(`"code":%d`, models.ErrCodeProjectDoesNotExist))
})
t.Run("Nonexistent index returns 404", func(t *testing.T) {
rec := get("1", "99999")