From 289f643a301c5ce1d8455741b7fde1e3e32fb390 Mon Sep 17 00:00:00 2001 From: Kushal Malani Date: Thu, 20 Apr 2023 10:53:55 -0700 Subject: [PATCH] Fixing lint and tests --- cloud/auth/auth.go | 7 ++++++- cloud/organization/organization.go | 6 +++++- cloud/workspace/workspace.go | 2 +- cmd/cloud/setup.go | 9 +++++---- cmd/cloud/setup_test.go | 27 +++++++++++++++++++++------ config/context.go | 8 +++++++- config/context_test.go | 6 +++--- 7 files changed, 48 insertions(+), 17 deletions(-) diff --git a/cloud/auth/auth.go b/cloud/auth/auth.go index 81784da5a..c788259b2 100644 --- a/cloud/auth/auth.go +++ b/cloud/auth/auth.go @@ -290,7 +290,12 @@ func CheckUserSession(c *config.Context, client astro.Client, coreClient astroco break } } - err = c.SetOrganizationContext(activeOrg.Id, activeOrg.ShortName) + + orgProduct := "HYBRID" + if activeOrg.Product != nil { + orgProduct = fmt.Sprintf("%s", *activeOrg.Product) //nolint + } + err = c.SetOrganizationContext(activeOrg.Id, activeOrg.ShortName, orgProduct) if err != nil { return err } diff --git a/cloud/organization/organization.go b/cloud/organization/organization.go index 86822a1ed..08b7426d9 100644 --- a/cloud/organization/organization.go +++ b/cloud/organization/organization.go @@ -117,7 +117,11 @@ func getOrganizationSelection(out io.Writer, coreClient astrocore.CoreClient) (* func SwitchWithContext(domain string, targetOrg *astrocore.Organization, astroClient astro.Client, coreClient astrocore.CoreClient, out io.Writer) error { c, _ := context.GetCurrentContext() // reset org context - _ = c.SetOrganizationContext(targetOrg.Id, targetOrg.ShortName) + orgProduct := "HYBRID" + if targetOrg.Product != nil { + orgProduct = fmt.Sprintf("%s", *targetOrg.Product) //nolint + } + _ = c.SetOrganizationContext(targetOrg.Id, targetOrg.ShortName, orgProduct) // need to reset all relevant keys because of https://github.com/spf13/viper/issues/1106 :shrug _ = c.SetContextKey("token", c.Token) _ = c.SetContextKey("refreshtoken", c.RefreshToken) diff --git a/cloud/workspace/workspace.go b/cloud/workspace/workspace.go index 46bcf2a8a..87e77ed80 100644 --- a/cloud/workspace/workspace.go +++ b/cloud/workspace/workspace.go @@ -151,7 +151,7 @@ func Switch(id string, client astro.Client, out io.Writer) error { return err } - err = c.SetOrganizationContext(c.Organization, c.OrganizationShortName) + err = c.SetOrganizationContext(c.Organization, c.OrganizationShortName, c.OrganizationProduct) if err != nil { return err } diff --git a/cmd/cloud/setup.go b/cmd/cloud/setup.go index ff1cd360b..45a0b3c47 100644 --- a/cmd/cloud/setup.go +++ b/cmd/cloud/setup.go @@ -104,7 +104,7 @@ func Setup(cmd *cobra.Command, args []string, client astro.Client, coreClient as } // Check for APITokens before API keys or refresh tokens - apiToken, err := checkAPIToken(isDeploymentFile, args) + apiToken, err := checkAPIToken(isDeploymentFile, coreClient, args) if err != nil { return err } @@ -321,6 +321,7 @@ func checkAPIKeys(astroClient astro.Client, coreClient astrocore.CoreClient, isD org := orgs[0] orgID := org.Id orgShortName := org.ShortName + orgProduct := fmt.Sprintf("%s", *org.Product) //nolint // If using api keys for virtual runtimes, we dont need to look up for this endpoint if !(len(args) > 0 && strings.HasPrefix(args[0], "vr-")) { @@ -336,14 +337,14 @@ func checkAPIKeys(astroClient astro.Client, coreClient astrocore.CoreClient, isD fmt.Println("no workspace set") } } - err = c.SetOrganizationContext(orgID, orgShortName) + err = c.SetOrganizationContext(orgID, orgShortName, orgProduct) if err != nil { fmt.Println("no organization context set") } return true, nil } -func checkAPIToken(isDeploymentFile bool, args []string) (bool, error) { +func checkAPIToken(isDeploymentFile bool, coreClient astrocore.CoreClient, args []string) (bool, error) { // check os variables astroAPIToken := os.Getenv("ASTRO_API_TOKEN") if astroAPIToken == "" { @@ -413,7 +414,7 @@ func checkAPIToken(isDeploymentFile bool, args []string) (bool, error) { fmt.Println("no workspace set") } } - err = c.SetOrganizationContext(orgID, orgShortName) + err = c.SetOrganizationContext(orgID, orgShortName, orgProduct) if err != nil { fmt.Println("no organization context set") } diff --git a/cmd/cloud/setup_test.go b/cmd/cloud/setup_test.go index 84295d6e0..5a6aac3b0 100644 --- a/cmd/cloud/setup_test.go +++ b/cmd/cloud/setup_test.go @@ -20,7 +20,10 @@ import ( "github.com/stretchr/testify/mock" ) -var errorLogin = errors.New("failed to login") +var ( + errorLogin = errors.New("failed to login") + mockOrganizationProduct = astrocore.OrganizationProductHYBRID +) func TestSetup(t *testing.T) { testUtil.InitTestConfig(testUtil.CloudPlatform) @@ -158,7 +161,7 @@ func TestSetup(t *testing.T) { StatusCode: 200, }, JSON200: &[]astrocore.Organization{ - {AuthServiceId: "auth-service-id", Id: "test-org-id", Name: "test-org-name"}, + {AuthServiceId: "auth-service-id", Id: "test-org-id", Name: "test-org-name", Product: &mockOrganizationProduct}, }, } mockClient := new(astro_mocks.Client) @@ -207,7 +210,7 @@ func TestSetup(t *testing.T) { StatusCode: 200, }, JSON200: &[]astrocore.Organization{ - {AuthServiceId: "auth-service-id", Id: "test-org-id", Name: "test-org-name"}, + {AuthServiceId: "auth-service-id", Id: "test-org-id", Name: "test-org-name", Product: &mockOrganizationProduct}, }, } mockClient := new(astro_mocks.Client) @@ -265,7 +268,7 @@ func TestCheckAPIKeys(t *testing.T) { StatusCode: 200, }, JSON200: &[]astrocore.Organization{ - {AuthServiceId: "auth-service-id", Id: "test-org-id", Name: "test-org-name"}, + {AuthServiceId: "auth-service-id", Id: "test-org-id", Name: "test-org-name", Product: &mockOrganizationProduct}, }, } mockClient := new(astro_mocks.Client) @@ -339,6 +342,16 @@ func TestCheckToken(t *testing.T) { func TestCheckAPIToken(t *testing.T) { testUtil.InitTestConfig(testUtil.CloudPlatform) + mockCoreClient := new(astrocore_mocks.ClientWithResponsesInterface) + mockOrgsResponse := astrocore.ListOrganizationsResponse{ + HTTPResponse: &http.Response{ + StatusCode: 200, + }, + JSON200: &[]astrocore.Organization{ + {AuthServiceId: "auth-service-id", Id: "test-org-id", Name: "test-org-name", Product: &mockOrganizationProduct}, + }, + } + t.Run("test context switch", func(t *testing.T) { permissions := []string{ "", @@ -358,6 +371,8 @@ func TestCheckAPIToken(t *testing.T) { return &mockClaims, nil } + mockCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astrocore.ListOrganizationsParams{}).Return(&mockOrgsResponse, nil).Once() + t.Setenv("ASTRO_API_TOKEN", "token") // Switch context @@ -366,7 +381,7 @@ func TestCheckAPIToken(t *testing.T) { assert.NoError(t, err) // run CheckAPIKeys - _, err = checkAPIToken(true, []string{}) + _, err = checkAPIToken(true, mockCoreClient, []string{}) assert.NoError(t, err) }) @@ -392,7 +407,7 @@ func TestCheckAPIToken(t *testing.T) { assert.NoError(t, err) // run CheckAPIKeys - _, err = checkAPIToken(true, []string{}) + _, err = checkAPIToken(true, mockCoreClient, []string{}) assert.ErrorIs(t, err, errNotAPIToken) }) } diff --git a/config/context.go b/config/context.go index 6b5d900e0..8cfd6c144 100644 --- a/config/context.go +++ b/config/context.go @@ -28,6 +28,7 @@ type Context struct { Domain string `mapstructure:"domain"` Organization string `mapstructure:"organization"` OrganizationShortName string `mapstructure:"organization_short_name"` + OrganizationProduct string `mapstructure:"organization_product"` Workspace string `mapstructure:"workspace"` LastUsedWorkspace string `mapstructure:"last_used_workspace"` Token string `mapstructure:"token"` @@ -151,7 +152,7 @@ func (c *Context) SetContextKey(key, value string) error { } // set organization id and short name in context config -func (c *Context) SetOrganizationContext(orgID, orgShortName string) error { +func (c *Context) SetOrganizationContext(orgID, orgShortName, orgProduct string) error { err := c.SetContextKey("organization", orgID) // c.Organization if err != nil { return err @@ -160,6 +161,11 @@ func (c *Context) SetOrganizationContext(orgID, orgShortName string) error { if err != nil { return err } + + err = c.SetContextKey("organization_product", orgProduct) + if err != nil { + return err + } return nil } diff --git a/config/context_test.go b/config/context_test.go index ebcd28522..59e6dfb4d 100644 --- a/config/context_test.go +++ b/config/context_test.go @@ -165,7 +165,7 @@ func TestGetContexts(t *testing.T) { initTestConfig() ctxs, err := GetContexts() assert.NoError(t, err) - assert.Equal(t, Contexts{Contexts: map[string]Context{"test_com": {"test.com", "test-org-id", "test-org-short-name", "ck05r3bor07h40d02y2hw4n4v", "ck05r3bor07h40d02y2hw4n4v", "token", "", ""}, "example_com": {"example.com", "test-org-id", "test-org-short-name", "ck05r3bor07h40d02y2hw4n4v", "ck05r3bor07h40d02y2hw4n4v", "token", "", ""}}}, ctxs) + assert.Equal(t, Contexts{Contexts: map[string]Context{"test_com": {"test.com", "test-org-id", "test-org-short-name", "", "ck05r3bor07h40d02y2hw4n4v", "ck05r3bor07h40d02y2hw4n4v", "token", "", ""}, "example_com": {"example.com", "test-org-id", "test-org-short-name", "", "ck05r3bor07h40d02y2hw4n4v", "ck05r3bor07h40d02y2hw4n4v", "token", "", ""}}}, ctxs) } func TestSetContextKey(t *testing.T) { @@ -181,7 +181,7 @@ func TestSetOrganizationContext(t *testing.T) { initTestConfig() t.Run("set organization context", func(t *testing.T) { ctx := Context{Domain: "localhost"} - ctx.SetOrganizationContext("org1", "org_short_name_1") + ctx.SetOrganizationContext("org1", "org_short_name_1", "HYBRID") outCtx, err := ctx.GetContext() assert.NoError(t, err) assert.Equal(t, "org1", outCtx.Organization) @@ -191,7 +191,7 @@ func TestSetOrganizationContext(t *testing.T) { t.Run("set organization context error", func(t *testing.T) { ctx := Context{Domain: ""} assert.NoError(t, err) - err = ctx.SetOrganizationContext("org1", "org_short_name_1") + err = ctx.SetOrganizationContext("org1", "org_short_name_1", "HYBRID") assert.Error(t, err) assert.Contains(t, err.Error(), "context config invalid, no domain specified") })