blueprint: Make sure name is not empty

The blueprint name should never be empty, as it can cause other problems
like with the blueprints list results. Return an error if one is pushed
to the store, either as a blueprint commit or as a blueprint workspace.

Also adjusts the new test for the new error.

Related: rhbz#1922845
This commit is contained in:
Brian C. Lane 2022-01-20 11:31:29 -08:00 committed by Ondřej Budai
parent a3b415d1df
commit aa8cf41ac2
2 changed files with 9 additions and 1 deletions

View file

@ -198,6 +198,10 @@ func (s *Store) GetBlueprintChanges(name string) []blueprint.Change {
func (s *Store) PushBlueprint(bp blueprint.Blueprint, commitMsg string) error {
return s.change(func() error {
if len(bp.Name) == 0 {
return fmt.Errorf("empty blueprint name not allowed")
}
commit, err := randomSHA1String()
if err != nil {
return err
@ -237,6 +241,10 @@ func (s *Store) PushBlueprint(bp blueprint.Blueprint, commitMsg string) error {
func (s *Store) PushBlueprintToWorkspace(bp blueprint.Blueprint) error {
return s.change(func() error {
if len(bp.Name) == 0 {
return fmt.Errorf("empty blueprint name not allowed")
}
// Make sure the blueprint has default values and that the version is valid
err := bp.Initialize()
if err != nil {

View file

@ -629,7 +629,7 @@ func TestOldBlueprintsUndo(t *testing.T) {
commit := changes.BlueprintsChanges[0].Changes[2].Commit
// Undo a known commit, that is old
test.TestRoute(t, api, true, "POST", "/api/v0/blueprints/undo/test-old-changes/"+commit, ``, http.StatusOK, `{"status":true}`)
test.TestRoute(t, api, true, "POST", "/api/v0/blueprints/undo/test-old-changes/"+commit, ``, http.StatusBadRequest, `{"errors":[{"id":"BlueprintsError", "msg":"empty blueprint name not allowed"}], "status":false}`)
// Check to make sure the undo is not present (can't undo something not there)
test.TestRoute(t, api, true, "GET", "/api/v0/blueprints/changes/test-old-changes", ``, http.StatusOK, `{"blueprints":[{"changes":[{"commit":"","message":"Change tmux version","revision":null,"timestamp":""},{"commit":"","message":"Add tmux package","revision":null,"timestamp":""},{"commit":"","message":"Initial commit","revision":null,"timestamp":""}],"name":"test-old-changes","total":3}],"errors":[],"limit":20,"offset":0}`, ignoreFields...)