fix(api/v2): return ErrProjectDoesNotExist for unknown project identifiers
This commit is contained in:
parent
0a879e56a8
commit
5cdc785b49
|
|
@ -448,6 +448,20 @@ func GetProjectSimpleByID(s *xorm.Session, projectID int64) (project *Project, e
|
||||||
return
|
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) {
|
func getProjectSimple(s *xorm.Session, cond builder.Cond) (project *Project, exists bool, err error) {
|
||||||
project = &Project{}
|
project = &Project{}
|
||||||
exists, err = s.
|
exists, err = s.
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ package apiv2
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/db"
|
"code.vikunja.io/api/pkg/db"
|
||||||
"code.vikunja.io/api/pkg/models"
|
"code.vikunja.io/api/pkg/models"
|
||||||
|
|
@ -218,13 +217,9 @@ func resolveProjectIdentifier(raw string) (int64, error) {
|
||||||
}
|
}
|
||||||
s := db.NewSession()
|
s := db.NewSession()
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
project := &models.Project{}
|
project, err := models.GetProjectSimpleByIdentifier(s, raw)
|
||||||
has, err := s.Where("identifier = ?", strings.ToUpper(raw)).Get(project)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, translateDomainError(err)
|
return 0, translateDomainError(err)
|
||||||
}
|
}
|
||||||
if !has {
|
|
||||||
return 0, huma.Error404NotFound("Project not found")
|
|
||||||
}
|
|
||||||
return project.ID, nil
|
return project.ID, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -204,9 +204,10 @@ func TestHumaTask_ReadByIndex(t *testing.T) {
|
||||||
require.Equal(t, http.StatusOK, rec.Code, "body: %s", rec.Body.String())
|
require.Equal(t, http.StatusOK, rec.Code, "body: %s", rec.Body.String())
|
||||||
assert.Contains(t, rec.Body.String(), `"id":1`)
|
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")
|
rec := get("does-not-exist", "1")
|
||||||
assert.Equal(t, http.StatusNotFound, rec.Code, "body: %s", rec.Body.String())
|
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) {
|
t.Run("Nonexistent index returns 404", func(t *testing.T) {
|
||||||
rec := get("1", "99999")
|
rec := get("1", "99999")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue