Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cloud/organization/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

const (
AstronomerConnectionErrMsg = "cannot connect to Astronomer. Try to log in with astro login or check your internet connection and user permissions. If you are using an API Key or Token make sure your context is correct.\n\nDetails"
listOrganizationsLimit = 1000
)

var (
Expand All @@ -42,7 +43,10 @@ func newTableOut() *printutil.Table {
}

func ListOrganizations(platformCoreClient astroplatformcore.CoreClient) ([]astroplatformcore.Organization, error) {
organizationListParams := &astroplatformcore.ListOrganizationsParams{}
limit := listOrganizationsLimit
organizationListParams := &astroplatformcore.ListOrganizationsParams{
Limit: &limit,
}
resp, err := platformCoreClient.ListOrganizationsWithResponse(http_context.Background(), organizationListParams)
if err != nil {
return nil, err
Expand Down
73 changes: 56 additions & 17 deletions cloud/organization/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
astroplatformcore_mocks "github.com/astronomer/astro-cli/astro-client-platform-core/mocks"
"github.com/astronomer/astro-cli/config"
testUtil "github.com/astronomer/astro-cli/pkg/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)
Expand Down Expand Up @@ -82,13 +83,19 @@ var (
errNetwork = errors.New("network error")
)

func matchListOrganizationsLimit() interface{} {
return mock.MatchedBy(func(p *astroplatformcore.ListOrganizationsParams) bool {
return p != nil && p.Limit != nil && *p.Limit == listOrganizationsLimit
})
}

func (s *Suite) TestList() {
// initialize empty config
testUtil.InitTestConfig(testUtil.LocalPlatform)

s.Run("organization list success", func() {
mockClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()

buf := new(bytes.Buffer)
err := List(buf, mockClient)
Expand All @@ -98,7 +105,7 @@ func (s *Suite) TestList() {

s.Run("organization network error", func() {
mockClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(nil, errNetwork).Once()
mockClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(nil, errNetwork).Once()
buf := new(bytes.Buffer)
err := List(buf, mockClient)
s.Contains(err.Error(), "network error")
Expand All @@ -107,21 +114,53 @@ func (s *Suite) TestList() {

s.Run("organization list error", func() {
mockClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockErrorResponse, nil).Once()
mockClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockErrorResponse, nil).Once()
buf := new(bytes.Buffer)
err := List(buf, mockClient)
s.Contains(err.Error(), "failed to fetch organizations")
mockClient.AssertExpectations(s.T())
})
}

func TestListOrganizations(t *testing.T) {
mockClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)

limit := listOrganizationsLimit
orgs := []astroplatformcore.Organization{
{Id: "org-1", Name: "Org 1"},
{Id: "org-2", Name: "Org 2"},
}

resp := &astroplatformcore.ListOrganizationsResponse{
HTTPResponse: &http.Response{StatusCode: http.StatusOK},
JSON200: &astroplatformcore.OrganizationsPaginated{
Organizations: orgs,
},
}

mockClient.On("ListOrganizationsWithResponse", mock.Anything, mock.MatchedBy(func(p *astroplatformcore.ListOrganizationsParams) bool {
return p != nil && p.Limit != nil && *p.Limit == limit
}),
mock.Anything,
).
Return(resp, nil).
Once()

organizations, err := ListOrganizations(mockClient)
assert.NoError(t, err)
assert.Len(t, organizations, 2)
assert.Equal(t, orgs, organizations)

mockClient.AssertExpectations(t)
}

func (s *Suite) TestGetOrganizationSelection() {
// initialize empty config
testUtil.InitTestConfig(testUtil.LocalPlatform)
s.Run("get organiation selection success", func() {
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()

// mock os.Stdin
input := []byte("1")
Expand All @@ -144,7 +183,7 @@ func (s *Suite) TestGetOrganizationSelection() {

s.Run("get organization selection list error", func() {
mockClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockErrorResponse, nil).Once()
mockClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockErrorResponse, nil).Once()

buf := new(bytes.Buffer)
_, err := getOrganizationSelection(buf, mockClient)
Expand All @@ -154,7 +193,7 @@ func (s *Suite) TestGetOrganizationSelection() {

s.Run("get organization selection select error", func() {
mockClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()

// mock os.Stdin
input := []byte("3")
Expand All @@ -181,7 +220,7 @@ func (s *Suite) TestSwitch() {
s.Run("successful switch with name", func() {
mockCoreClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()
CheckUserSession = func(c *config.Context, coreClient astrocore.CoreClient, platformCoreClient astroplatformcore.CoreClient, out io.Writer) error {
return nil
}
Expand All @@ -196,7 +235,7 @@ func (s *Suite) TestSwitch() {
s.Run("switching to a current organization", func() {
mockCoreClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()

buf := new(bytes.Buffer)
err := Switch("org1", mockCoreClient, mockPlatformCoreClient, buf, false)
Expand All @@ -209,7 +248,7 @@ func (s *Suite) TestSwitch() {
s.Run("successful switch without name", func() {
mockCoreClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()
CheckUserSession = func(c *config.Context, coreClient astrocore.CoreClient, platformCoreClient astroplatformcore.CoreClient, out io.Writer) error {
return nil
}
Expand All @@ -234,7 +273,7 @@ func (s *Suite) TestSwitch() {
s.Run("failed switch wrong name", func() {
mockCoreClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()
CheckUserSession = func(c *config.Context, coreClient astrocore.CoreClient, platformCoreClient astroplatformcore.CoreClient, out io.Writer) error {
return nil
}
Expand All @@ -248,7 +287,7 @@ func (s *Suite) TestSwitch() {
s.Run("failed switch bad selection", func() {
mockCoreClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()
CheckUserSession = func(c *config.Context, coreClient astrocore.CoreClient, platformCoreClient astroplatformcore.CoreClient, out io.Writer) error {
return nil
}
Expand Down Expand Up @@ -283,7 +322,7 @@ func (s *Suite) TestSwitch() {
}
mockCoreClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()
CheckUserSession = func(c *config.Context, coreClient astrocore.CoreClient, platformCoreClient astroplatformcore.CoreClient, out io.Writer) error {
return nil
}
Expand Down Expand Up @@ -359,7 +398,7 @@ func (s *Suite) TestExportAuditLogs() {
s.Run("export audit logs success", func() {
mockPlatformClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()
mockClient.On("GetOrganizationAuditLogsWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&mockOKAuditLogResponse, nil).Once()
err := ExportAuditLogs(mockClient, mockPlatformClient, "", "", 1)
s.NoError(err)
Expand All @@ -369,7 +408,7 @@ func (s *Suite) TestExportAuditLogs() {
s.Run("export audit logs and select org success", func() {
mockPlatformClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()
mockClient.On("GetOrganizationAuditLogsWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&mockOKAuditLogResponse, nil).Once()
err := ExportAuditLogs(mockClient, mockPlatformClient, "org1", "", 1)
s.NoError(err)
Expand All @@ -379,7 +418,7 @@ func (s *Suite) TestExportAuditLogs() {
s.Run("export failure", func() {
mockPlatformClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()
mockClient.On("GetOrganizationAuditLogsWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(nil, errNetwork).Once()
err := ExportAuditLogs(mockClient, mockPlatformClient, "", "", 1)
s.Contains(err.Error(), "network error")
Expand All @@ -389,7 +428,7 @@ func (s *Suite) TestExportAuditLogs() {
s.Run("list failure", func() {
mockPlatformClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(nil, errNetwork).Once()
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(nil, errNetwork).Once()
err := ExportAuditLogs(mockClient, mockPlatformClient, "org1", "", 1)
s.Contains(err.Error(), "network error")
mockPlatformClient.AssertExpectations(s.T())
Expand All @@ -398,7 +437,7 @@ func (s *Suite) TestExportAuditLogs() {
s.Run("organization list error", func() {
mockPlatformClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, matchListOrganizationsLimit()).Return(&mockOKResponse, nil).Once()
mockClient.On("GetOrganizationAuditLogsWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&mockOKAuditLogResponseError, nil).Once()
err := ExportAuditLogs(mockClient, mockPlatformClient, "", "", 1)
s.Contains(err.Error(), "failed to fetch organizations audit logs")
Expand Down
24 changes: 18 additions & 6 deletions cmd/cloud/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,9 @@ func TestCheckAPIToken(t *testing.T) {
return &mockClaims, nil
}

mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOrgsResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, mock.MatchedBy(func(p *astroplatformcore.ListOrganizationsParams) bool {
return p != nil && p.Limit != nil && *p.Limit == 1000
})).Return(&mockOrgsResponse, nil).Once()

t.Setenv("ASTRO_API_TOKEN", "token")

Expand All @@ -495,7 +497,9 @@ func TestCheckAPIToken(t *testing.T) {
return nil, errors.New("Failed to parse token")
}

mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOrgsResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, mock.MatchedBy(func(p *astroplatformcore.ListOrganizationsParams) bool {
return p != nil && p.Limit != nil && *p.Limit == 1000
})).Return(&mockOrgsResponse, nil).Once()

t.Setenv("ASTRO_API_TOKEN", "token")

Expand All @@ -517,7 +521,9 @@ func TestCheckAPIToken(t *testing.T) {
return &mockClaims, nil
}

mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOrgsResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, mock.MatchedBy(func(p *astroplatformcore.ListOrganizationsParams) bool {
return p != nil && p.Limit != nil && *p.Limit == 1000
})).Return(&mockOrgsResponse, nil).Once()

t.Setenv("ASTRO_API_TOKEN", "token")
err := config.ResetCurrentContext()
Expand Down Expand Up @@ -551,7 +557,9 @@ func TestCheckAPIToken(t *testing.T) {
return &mockClaims, nil
}

mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOrgsResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, mock.MatchedBy(func(p *astroplatformcore.ListOrganizationsParams) bool {
return p != nil && p.Limit != nil && *p.Limit == 1000
})).Return(&mockOrgsResponse, nil).Once()

t.Setenv("ASTRO_API_TOKEN", "token")

Expand Down Expand Up @@ -591,7 +599,9 @@ func TestCheckAPIToken(t *testing.T) {
return &mockClaims, nil
}

mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOrgsResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, mock.MatchedBy(func(p *astroplatformcore.ListOrganizationsParams) bool {
return p != nil && p.Limit != nil && *p.Limit == 1000
})).Return(&mockOrgsResponse, nil).Once()

t.Setenv("ASTRO_API_TOKEN", "token")

Expand Down Expand Up @@ -630,7 +640,9 @@ func TestCheckAPIToken(t *testing.T) {
return &mockClaims, nil
}

mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOrgsResponse, nil).Once()
mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, mock.MatchedBy(func(p *astroplatformcore.ListOrganizationsParams) bool {
return p != nil && p.Limit != nil && *p.Limit == 1000
})).Return(&mockOrgsResponse, nil).Once()

t.Setenv("ASTRO_API_TOKEN", "token")

Expand Down