Skip to content

Commit

Permalink
refactor: remove deprecated UpdateProjectKey in favor of PatchProject
Browse files Browse the repository at this point in the history
  • Loading branch information
corban-beaird committed Jun 7, 2024
1 parent 78038dd commit 668e4a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
18 changes: 0 additions & 18 deletions master/internal/project/postgres_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,24 +234,6 @@ RetryLoop:
return errors.Wrapf(err, "error inserting project %s into database", p.Name)
}

// UpdateProjectKey updates the key of a project.
func UpdateProjectKey(ctx context.Context, projectID int, key string) error {
if len(key) > MaxProjectKeyLength {
return fmt.Errorf("project key must be at most %d characters", MaxProjectKeyLength)
}
if projectID == 0 {
return fmt.Errorf("invalid project ID")
}
return db.Bun().RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
_, err := tx.NewUpdate().
Model(&model.Project{ID: projectID, Key: key}).WherePK("id").Column("key").Exec(ctx)
if err != nil && strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return fmt.Errorf("project key %s is already in use", key)
}
return err
})
}

// ValidateProjectKey validates a project key.
func ValidateProjectKey(key string) error {
switch {
Expand Down
42 changes: 38 additions & 4 deletions master/internal/project/postgres_project_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/wrapperspb"
"gopkg.in/oauth2.v3/utils/uuid"
"gotest.tools/assert"

internaldb "github.com/determined-ai/determined/master/internal/db"
"github.com/determined-ai/determined/master/pkg/etc"
"github.com/determined-ai/determined/master/pkg/model"
"github.com/determined-ai/determined/proto/pkg/projectv1"
)

func TestProjectByName(t *testing.T) {
Expand Down Expand Up @@ -52,12 +55,18 @@ func TestGetProjectByKey(t *testing.T) {
internaldb.MustMigrateTestPostgres(t, db, internaldb.MigrationsFromDB)

// add a workspace, and project
user := internaldb.RequireMockUser(t, db)
workspaceID, _ := internaldb.RequireMockWorkspaceID(t, db, "")
projectID, _ := internaldb.RequireMockProjectID(t, db, workspaceID, false)

t.Run("valid project key", func(t *testing.T) {
key := uuid.Must(uuid.NewRandom()).String()[:MaxProjectKeyLength]
err := UpdateProjectKey(context.Background(), projectID, key)
_, err := UpdateProject(
context.Background(),
int32(projectID),
user,
&projectv1.PatchProject{Key: &wrapperspb.StringValue{Value: key}},
)
require.NoError(t, err)
project, err := GetProjectByKey(context.Background(), key)
require.NoError(t, err)
Expand All @@ -81,21 +90,46 @@ func TestUpdateProjectKey(t *testing.T) {
workspaceID, _ := internaldb.RequireMockWorkspaceID(t, db, "")
projectID, _ := internaldb.RequireMockProjectID(t, db, workspaceID, false)

user := model.User{
Username: uuid.Must(uuid.NewRandom()).String(),
Admin: true,
}
_, err := internaldb.HackAddUser(
context.Background(),
&user,
)
require.NoError(t, err)

t.Run("update project key", func(t *testing.T) {
key := uuid.Must(uuid.NewRandom()).String()[:MaxProjectKeyLength]
err := UpdateProjectKey(context.Background(), projectID, key)
_, err := UpdateProject(
context.Background(),
int32(projectID),
user,
&projectv1.PatchProject{Key: &wrapperspb.StringValue{Value: key}},
)
require.NoError(t, err)
})

t.Run("update project key with invalid project id", func(t *testing.T) {
key := uuid.Must(uuid.NewRandom()).String()[:MaxProjectKeyLength]
err := UpdateProjectKey(context.Background(), 0, key)
_, err := UpdateProject(
context.Background(),
0,
user,
&projectv1.PatchProject{Key: &wrapperspb.StringValue{Value: key}},
)
require.Error(t, err)
})

t.Run("update project key with invalid key", func(t *testing.T) {
invalidKey := strings.Repeat("a", MaxProjectKeyLength+1)
err := UpdateProjectKey(context.Background(), projectID, invalidKey)
_, err := UpdateProject(
context.Background(),
int32(projectID),
user,
&projectv1.PatchProject{Key: &wrapperspb.StringValue{Value: invalidKey}},
)
require.Error(t, err)
})
}

0 comments on commit 668e4a1

Please sign in to comment.