Skip to content

Commit

Permalink
feat: require workspace when creating projects
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Sep 18, 2024
1 parent b215681 commit 728082c
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd/cloudx/accountexperience/accountexperience_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
)

func TestMain(m *testing.M) {
ctx, _, _, project, cmd = testhelpers.CreateDefaultAssetsBrowser()
ctx, _, _, _, project, cmd = testhelpers.CreateDefaultAssetsBrowser()
testhelpers.UseStaging()
m.Run()
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/cloudx/identity/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
func TestListIdentities(t *testing.T) {
t.Parallel()

project := testhelpers.CreateProject(ctx, t, nil)
workspace := testhelpers.CreateWorkspace(ctx, t)
project := testhelpers.CreateProject(ctx, t, workspace)
userID := testhelpers.ImportIdentity(ctx, t, project.Id, nil)

t.Run("is not able to list identities if not authenticated and quiet flag", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cloudx/identity/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ var (
)

func TestMain(m *testing.M) {
ctx, _, _, defaultProject, defaultCmd = testhelpers.CreateDefaultAssetsBrowser()
ctx, _, _, _, defaultProject, defaultCmd = testhelpers.CreateDefaultAssetsBrowser()
m.Run()
}
5 changes: 3 additions & 2 deletions cmd/cloudx/oauth2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
)

func TestMain(m *testing.M) {
ctx, _, _, defaultProject, defaultCmd = testhelpers.CreateDefaultAssetsBrowser()
ctx, _, _, _, defaultProject, defaultCmd = testhelpers.CreateDefaultAssetsBrowser()
m.Run()
}

Expand Down Expand Up @@ -140,7 +140,8 @@ func TestImportClient(t *testing.T) {
func TestListClients(t *testing.T) {
t.Parallel()

project := testhelpers.CreateProject(ctx, t, nil)
workspace := testhelpers.CreateWorkspace(ctx, t)
project := testhelpers.CreateProject(ctx, t, workspace)
clientID := testhelpers.CreateClient(ctx, t, project.Id).Get("client_id").String()

t.Run("is not able to list oauth2 clients if not authenticated and quiet flag", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cloudx/organizations/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
)

func TestMain(m *testing.M) {
_, _, _, defaultProject, defaultCmd = testhelpers.CreateDefaultAssetsBrowser()
_, _, _, _, defaultProject, defaultCmd = testhelpers.CreateDefaultAssetsBrowser()
m.Run()
}

Expand Down
11 changes: 8 additions & 3 deletions cmd/cloudx/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ func NewCreateProjectCmd() *cobra.Command {
return err
}

if (len(name) == 0 || len(environment) == 0) && flagx.MustGetBool(cmd, cmdx.FlagQuiet) {
return errors.New("you must specify the --name and --environment flags when using --quiet")
wsID := h.WorkspaceID()
if wsID == nil || len(environment) == 0 {
return errors.New("a workspace and environment is required to create a project")
}

if len(name) == 0 && flagx.MustGetBool(cmd, cmdx.FlagQuiet) {
return errors.New("you must specify the --name flag when using --quiet")
}

for name == "" {
Expand All @@ -51,7 +56,7 @@ func NewCreateProjectCmd() *cobra.Command {
}
}

p, err := h.CreateProject(ctx, name, string(environment), h.WorkspaceID(), useProject)
p, err := h.CreateProject(ctx, name, string(environment), wsID, useProject)
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}
Expand Down
40 changes: 32 additions & 8 deletions cmd/cloudx/project/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"testing"

"github.com/gofrs/uuid"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
Expand All @@ -33,14 +35,34 @@ func TestCreateProject(t *testing.T) {
return
}

t.Run("requires workspace and environment", func(t *testing.T) {
t.Parallel()

ctx := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
testhelpers.SetDefaultProject(ctx, t, defaultProject.Id)

for _, extraArgs := range [][]string{
{},
{"--workspace", uuid.Must(uuid.NewV4()).String()},
{"--environment", "dev"},
} {
_, stderr, err := testhelpers.Cmd(ctx).Exec(nil, append([]string{"create", "project", "--name", testhelpers.TestName(), "--format", "json"}, extraArgs...)...)
require.Error(t, err)
assert.ErrorContains(t, err, "a workspace and environment is required to create a project", stderr)
assert.Contains(t, stderr, "a workspace and environment is required to create a project")
}
})

t.Run("is able to create a project", func(t *testing.T) {
t.Parallel()

ctx := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)
testhelpers.SetDefaultProject(ctx, t, defaultProject.Id)

wsID := testhelpers.CreateWorkspace(ctx, t)

name := testhelpers.TestName()
stdout, _, err := testhelpers.Cmd(ctx).Exec(nil, "create", "project", "--name", name, "--format", "json")
stdout, _, err := testhelpers.Cmd(ctx).Exec(nil, "create", "project", "--name", name, "--format", "json", "--workspace", wsID)
require.NoError(t, err)
assertResult(t, defaultConfig, stdout, name)

Expand All @@ -52,8 +74,9 @@ func TestCreateProject(t *testing.T) {

ctx := testhelpers.WithDuplicatedConfigFile(ctx, t, defaultConfig)

workspace := testhelpers.CreateWorkspace(ctx, t)
name := testhelpers.TestName()
stdout, _, err := testhelpers.Cmd(ctx).Exec(nil, "create", "project", "--name", name, "--use-project", "--format", "json")
stdout, _, err := testhelpers.Cmd(ctx).Exec(nil, "create", "project", "--name", name, "--use-project", "--workspace", workspace, "--environment", "dev", "--format", "json")
require.NoError(t, err)
id, _, _ := assertResult(t, defaultConfig, stdout, name)

Expand All @@ -65,28 +88,29 @@ func TestCreateProject(t *testing.T) {

name := testhelpers.TestName()
stdin := strings.NewReader(name + "\n")
stdout, _, err := defaultCmd.Exec(stdin, "create", "project", "--format", "json")
workspace := testhelpers.CreateWorkspace(ctx, t)
stdout, _, err := defaultCmd.Exec(stdin, "create", "project", "--workspace", workspace, "--environment", "dev", "--format", "json")
require.NoError(t, err)
assertResult(t, defaultConfig, stdout, name)
})

t.Run("is not able to create a project if no name flag and quiet flag", func(t *testing.T) {
t.Parallel()

workspace := testhelpers.CreateWorkspace(ctx, t)
name := testhelpers.TestName()
stdin := strings.NewReader(name)
_, stderr, err := defaultCmd.Exec(stdin, "create", "project", "--quiet")
_, stderr, err := defaultCmd.Exec(stdin, "create", "project", "--quiet", "--workspace", workspace, "--environment", "dev")
require.Error(t, err)
assert.Contains(t, stderr, "you must specify the --name and --environment flags when using --quiet")
assert.Contains(t, stderr, "you must specify the --name flag when using --quiet")
})

t.Run("is not able to create a project if not authenticated and quiet flag", func(t *testing.T) {
t.Parallel()

ctx := testhelpers.WithCleanConfigFile(context.Background(), t)

name := testhelpers.TestName()
_, _, err := testhelpers.Cmd(ctx).Exec(nil, "create", "project", "--name", name, "--quiet")
_, _, err := testhelpers.Cmd(ctx).Exec(nil, "create", "project", "--name", testhelpers.TestName(), "--workspace", uuid.Must(uuid.NewV4()).String(), "--environment", "dev", "--quiet")
require.ErrorIs(t, err, client.ErrNoConfigQuiet)
})

Expand All @@ -95,7 +119,7 @@ func TestCreateProject(t *testing.T) {

ctx := testhelpers.WithEmitAuthFlowTriggeredErr(context.Background(), t)

_, _, err := testhelpers.Cmd(ctx).Exec(nil, "create", "project", "--name", testhelpers.TestName(), "--format", "json")
_, _, err := testhelpers.Cmd(ctx).Exec(nil, "create", "project", "--name", testhelpers.TestName(), "--workspace", uuid.Must(uuid.NewV4()).String(), "--environment", "dev", "--format", "json")
assert.ErrorIs(t, err, testhelpers.ErrAuthFlowTriggered)
})
}
3 changes: 2 additions & 1 deletion cmd/cloudx/project/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ func TestListProject(t *testing.T) {

cmd := testhelpers.Cmd(ctx)

workspace := testhelpers.CreateWorkspace(ctx, t)
projects := make([]*cloud.Project, 3)
projectIDs := make([]string, len(projects))
for k := range projects {
projects[k] = testhelpers.CreateProject(ctx, t, nil)
projects[k] = testhelpers.CreateProject(ctx, t, workspace)
projectIDs[k] = projects[k].Id
}

Expand Down
12 changes: 7 additions & 5 deletions cmd/cloudx/project/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package project_test

import (
"context"
"fmt"
"testing"

cloud "github.com/ory/client-go"
Expand All @@ -14,13 +15,14 @@ import (
)

var (
ctx context.Context
defaultProject, extraProject *cloud.Project
defaultConfig string
defaultCmd *cmdx.CommandExecuter
ctx context.Context
defaultProject, extraProject *cloud.Project
defaultConfig, defaultWorkspaceID string
defaultCmd *cmdx.CommandExecuter
)

func TestMain(m *testing.M) {
ctx, defaultConfig, extraProject, defaultProject, defaultCmd = testhelpers.CreateDefaultAssetsBrowser()
ctx, defaultConfig, defaultWorkspaceID, extraProject, defaultProject, defaultCmd = testhelpers.CreateDefaultAssetsBrowser()
fmt.Println("done setting up, running tests")
m.Run()
}
3 changes: 2 additions & 1 deletion cmd/cloudx/project/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var (
func TestUpdateProject(t *testing.T) {
t.Parallel()

project := testhelpers.CreateProject(ctx, t, nil)
workspace := testhelpers.CreateWorkspace(ctx, t)
project := testhelpers.CreateProject(ctx, t, workspace)

for _, tc := range []struct {
subcommand,
Expand Down
2 changes: 1 addition & 1 deletion cmd/cloudx/relationtuples/relationtuples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
)

func TestMain(m *testing.M) {
_, _, _, defaultProject, defaultCmd = testhelpers.CreateDefaultAssetsBrowser()
_, _, _, _, defaultProject, defaultCmd = testhelpers.CreateDefaultAssetsBrowser()
m.Run()
}

Expand Down
7 changes: 2 additions & 5 deletions cmd/cloudx/testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,8 @@ func Cmd(ctx context.Context) *cmdx.CommandExecuter {
}
}

func CreateProject(ctx context.Context, t testing.TB, workspace *string) *cloud.Project {
args := []string{"create", "project", "--name", TestName(), "--format", "json"}
if workspace != nil {
args = append(args, "--workspace", *workspace)
}
func CreateProject(ctx context.Context, t testing.TB, workspace string) *cloud.Project {
args := []string{"create", "project", "--name", TestName(), "--workspace", workspace, "--format", "json", "--environment", "dev"}
stdout, stderr, err := Cmd(ctx).Exec(nil, args...)
require.NoError(t, err, stderr)
p := cloud.Project{}
Expand Down
8 changes: 5 additions & 3 deletions cmd/cloudx/testhelpers/testmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func UseStaging() {
setEnvIfUnset(client.OryAPIsURLKey, "https://staging.oryapis.dev")
}

func CreateDefaultAssetsBrowser() (ctx context.Context, defaultConfig string, extraProject, defaultProject *cloud.Project, defaultCmd *cmdx.CommandExecuter) {
func CreateDefaultAssetsBrowser() (ctx context.Context, defaultConfig, defaultWorkspaceID string, extraProject, defaultProject *cloud.Project, defaultCmd *cmdx.CommandExecuter) {
UseStaging()

t := MockTestingTForMain{}
Expand All @@ -55,9 +55,11 @@ func CreateDefaultAssetsBrowser() (ctx context.Context, defaultConfig string, ex
require.NoError(t, h.Authenticate(ctx))
// we don't need playwright anymore
cleanup()
fmt.Println("authenticated, creating default assets")

defaultProject = CreateProject(ctx, t, nil)
extraProject = CreateProject(ctx, t, nil)
defaultWorkspaceID = CreateWorkspace(ctx, t)
defaultProject = CreateProject(ctx, t, defaultWorkspaceID)
extraProject = CreateProject(ctx, t, defaultWorkspaceID)

defaultCmd = Cmd(ctx)
return
Expand Down

0 comments on commit 728082c

Please sign in to comment.