From 454b3489aa64a61b258da79a23bfebc7567ff884 Mon Sep 17 00:00:00 2001 From: Jake Shorty Date: Fri, 17 Jun 2022 22:45:04 +0000 Subject: [PATCH] Add test for billable owner in stdout --- pkg/cmd/codespace/create_test.go | 129 ++++++++++++++++++++++++------- 1 file changed, 99 insertions(+), 30 deletions(-) diff --git a/pkg/cmd/codespace/create_test.go b/pkg/cmd/codespace/create_test.go index 238709faf..42b0fd731 100644 --- a/pkg/cmd/codespace/create_test.go +++ b/pkg/cmd/codespace/create_test.go @@ -29,12 +29,6 @@ func TestApp_Create(t *testing.T) { name: "create codespace with default branch and 30m idle timeout", fields: fields{ apiClient: &apiClientMock{ - GetCodespacePreFlightFunc: func(ctx context.Context, nwo string) (*api.User, error) { - return &api.User{ - Login: "monalisa", - Type: "User", - }, nil - }, GetRepositoryFunc: func(ctx context.Context, nwo string) (*api.Repository, error) { return &api.Repository{ ID: 1234, @@ -42,6 +36,12 @@ func TestApp_Create(t *testing.T) { DefaultBranch: "main", }, nil }, + GetCodespacePreFlightFunc: func(ctx context.Context, nwo string) (*api.User, error) { + return &api.User{ + Login: "monalisa", + Type: "User", + }, nil + }, ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]api.DevContainerEntry, error) { return []api.DevContainerEntry{{Path: ".devcontainer/devcontainer.json"}}, nil }, @@ -86,12 +86,6 @@ func TestApp_Create(t *testing.T) { name: "create codespace with default branch shows idle timeout notice if present", fields: fields{ apiClient: &apiClientMock{ - GetCodespacePreFlightFunc: func(ctx context.Context, nwo string) (*api.User, error) { - return &api.User{ - Login: "monalisa", - Type: "User", - }, nil - }, GetRepositoryFunc: func(ctx context.Context, nwo string) (*api.Repository, error) { return &api.Repository{ ID: 1234, @@ -99,6 +93,12 @@ func TestApp_Create(t *testing.T) { DefaultBranch: "main", }, nil }, + GetCodespacePreFlightFunc: func(ctx context.Context, nwo string) (*api.User, error) { + return &api.User{ + Login: "monalisa", + Type: "User", + }, nil + }, GetCodespacesMachinesFunc: func(ctx context.Context, repoID int, branch, location string) ([]*api.Machine, error) { return []*api.Machine{ { @@ -140,12 +140,6 @@ func TestApp_Create(t *testing.T) { name: "create codespace with default branch with default devcontainer if no path provided and no devcontainer files exist in the repo", fields: fields{ apiClient: &apiClientMock{ - GetCodespacePreFlightFunc: func(ctx context.Context, nwo string) (*api.User, error) { - return &api.User{ - Login: "monalisa", - Type: "User", - }, nil - }, GetRepositoryFunc: func(ctx context.Context, nwo string) (*api.Repository, error) { return &api.Repository{ ID: 1234, @@ -153,6 +147,12 @@ func TestApp_Create(t *testing.T) { DefaultBranch: "main", }, nil }, + GetCodespacePreFlightFunc: func(ctx context.Context, nwo string) (*api.User, error) { + return &api.User{ + Login: "monalisa", + Type: "User", + }, nil + }, ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]api.DevContainerEntry, error) { return []api.DevContainerEntry{}, nil }, @@ -199,12 +199,6 @@ func TestApp_Create(t *testing.T) { name: "returns error when getting devcontainer paths fails", fields: fields{ apiClient: &apiClientMock{ - GetCodespacePreFlightFunc: func(ctx context.Context, nwo string) (*api.User, error) { - return &api.User{ - Login: "monalisa", - Type: "User", - }, nil - }, GetRepositoryFunc: func(ctx context.Context, nwo string) (*api.Repository, error) { return &api.Repository{ ID: 1234, @@ -212,6 +206,12 @@ func TestApp_Create(t *testing.T) { DefaultBranch: "main", }, nil }, + GetCodespacePreFlightFunc: func(ctx context.Context, nwo string) (*api.User, error) { + return &api.User{ + Login: "monalisa", + Type: "User", + }, nil + }, ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]api.DevContainerEntry, error) { return nil, fmt.Errorf("some error") }, @@ -230,12 +230,6 @@ func TestApp_Create(t *testing.T) { name: "create codespace with default branch does not show idle timeout notice if not conntected to terminal", fields: fields{ apiClient: &apiClientMock{ - GetCodespacePreFlightFunc: func(ctx context.Context, nwo string) (*api.User, error) { - return &api.User{ - Login: "monalisa", - Type: "User", - }, nil - }, GetRepositoryFunc: func(ctx context.Context, nwo string) (*api.Repository, error) { return &api.Repository{ ID: 1234, @@ -243,6 +237,12 @@ func TestApp_Create(t *testing.T) { DefaultBranch: "main", }, nil }, + GetCodespacePreFlightFunc: func(ctx context.Context, nwo string) (*api.User, error) { + return &api.User{ + Login: "monalisa", + Type: "User", + }, nil + }, ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]api.DevContainerEntry, error) { return []api.DevContainerEntry{}, nil }, @@ -339,6 +339,75 @@ Open this URL in your browser to review and authorize additional permissions: ex Alternatively, you can run "create" with the "--default-permissions" option to continue without authorizing additional permissions. `, }, + { + name: "returns error when user can't create codepaces for a repository", + fields: fields{ + apiClient: &apiClientMock{ + GetRepositoryFunc: func(ctx context.Context, nwo string) (*api.Repository, error) { + return &api.Repository{ + ID: 1234, + FullName: nwo, + DefaultBranch: "main", + }, nil + }, + GetCodespacePreFlightFunc: func(ctx context.Context, nwo string) (*api.User, error) { + return nil, fmt.Errorf("some error") + }, + }, + }, + opts: createOptions{ + repo: "megacorp/private", + branch: "", + machine: "GIGA", + showStatus: false, + idleTimeout: 30 * time.Minute, + }, + wantErr: fmt.Errorf("error checking codespace ownership: some error"), + }, + { + name: "mentions billable owner when org covers codepaces for a repository", + fields: fields{ + apiClient: &apiClientMock{ + GetRepositoryFunc: func(ctx context.Context, nwo string) (*api.Repository, error) { + return &api.Repository{ + ID: 1234, + FullName: nwo, + DefaultBranch: "main", + }, nil + }, + GetCodespacePreFlightFunc: func(ctx context.Context, nwo string) (*api.User, error) { + return &api.User{ + Type: "Organization", + Login: "megacorp", + }, nil + }, + ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]api.DevContainerEntry, error) { + return []api.DevContainerEntry{{Path: ".devcontainer/devcontainer.json"}}, nil + }, + GetCodespacesMachinesFunc: func(ctx context.Context, repoID int, branch, location string) ([]*api.Machine, error) { + return []*api.Machine{ + { + Name: "GIGA", + DisplayName: "Gigabits of a machine", + }, + }, nil + }, + CreateCodespaceFunc: func(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error) { + return &api.Codespace{ + Name: "megacorp-private-abcd1234", + }, nil + }, + }, + }, + opts: createOptions{ + repo: "megacorp/private", + branch: "", + machine: "GIGA", + showStatus: false, + idleTimeout: 30 * time.Minute, + }, + wantStdout: "✓ Codespaces usage for this repository is paid for by megacorp\nmegacorp-private-abcd1234\n", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {