From abb6b2eaa6519ddee7a44b9e49461bfea327a4ed Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Thu, 5 Jan 2023 18:23:43 +0100 Subject: [PATCH] DXCDT-303: Fix orgs update cmd (#583) --- internal/cli/organizations.go | 97 +++++++++++------------- test/integration/scripts/get-org-id.sh | 6 ++ test/integration/scripts/test-cleanup.sh | 16 ++++ test/integration/test-cases.yaml | 48 +++++++++++- 4 files changed, 113 insertions(+), 54 deletions(-) create mode 100755 test/integration/scripts/get-org-id.sh diff --git a/internal/cli/organizations.go b/internal/cli/organizations.go index 89eb06151..c8938292b 100644 --- a/internal/cli/organizations.go +++ b/internal/cli/organizations.go @@ -229,46 +229,42 @@ func createOrganizationCmd(cli *cli) *cobra.Command { return err } - o := &management.Organization{ + newOrg := &management.Organization{ Name: &inputs.Name, DisplayName: &inputs.DisplayName, - Metadata: &inputs.Metadata, } - isLogoURLSet := len(inputs.LogoURL) > 0 - isAccentColorSet := len(inputs.AccentColor) > 0 - isBackgroundColorSet := len(inputs.BackgroundColor) > 0 - isAnyColorSet := isAccentColorSet || isBackgroundColorSet - - if isLogoURLSet || isAnyColorSet { - o.Branding = &management.OrganizationBranding{} - - if isLogoURLSet { - o.Branding.LogoURL = &inputs.LogoURL - } - - if isAnyColorSet { - colors := make(map[string]string) + if inputs.Metadata != nil { + newOrg.Metadata = &inputs.Metadata + } - if isAccentColorSet { - colors[apiOrganizationColorPrimary] = inputs.AccentColor - } + branding := management.OrganizationBranding{} + if inputs.LogoURL != "" { + branding.LogoURL = &inputs.LogoURL + } - if isBackgroundColorSet { - colors[apiOrganizationColorPageBackground] = inputs.BackgroundColor - } + colors := make(map[string]string) + if inputs.AccentColor != "" { + colors[apiOrganizationColorPrimary] = inputs.AccentColor + } + if inputs.BackgroundColor != "" { + colors[apiOrganizationColorPageBackground] = inputs.BackgroundColor + } + if len(colors) > 0 { + branding.Colors = &colors + } - o.Branding.Colors = &colors - } + if branding.String() != "{}" { + newOrg.Branding = &branding } if err := ansi.Waiting(func() error { - return cli.api.Organization.Create(o) + return cli.api.Organization.Create(newOrg) }); err != nil { - return fmt.Errorf("An unexpected error occurred while attempting to create an organization with name '%s': %w", inputs.Name, err) + return fmt.Errorf("failed to create an organization with name '%s': %w", inputs.Name, err) } - cli.renderer.OrganizationCreate(o) + cli.renderer.OrganizationCreate(newOrg) return nil }, } @@ -317,46 +313,42 @@ func updateOrganizationCmd(cli *cli) *cobra.Command { } } - var current *management.Organization + var oldOrg *management.Organization err := ansi.Waiting(func() error { var err error - current, err = cli.api.Organization.Read(inputs.ID) + oldOrg, err = cli.api.Organization.Read(inputs.ID) return err }) if err != nil { - return fmt.Errorf("Failed to fetch organization with ID: %s %v", inputs.ID, err) + return fmt.Errorf("failed to fetch organization with ID: %s %w", inputs.ID, err) } - if err := organizationDisplay.AskU(cmd, &inputs.DisplayName, current.DisplayName); err != nil { + if err := organizationDisplay.AskU(cmd, &inputs.DisplayName, oldOrg.DisplayName); err != nil { return err } if inputs.DisplayName == "" { - inputs.DisplayName = current.GetDisplayName() + inputs.DisplayName = oldOrg.GetDisplayName() } - // Prepare organization payload for update. This will also be - // re-hydrated by the SDK, which we'll use below during - // display. - o := &management.Organization{ - ID: current.ID, + newOrg := &management.Organization{ DisplayName: &inputs.DisplayName, } isLogoURLSet := len(inputs.LogoURL) > 0 isAccentColorSet := len(inputs.AccentColor) > 0 isBackgroundColorSet := len(inputs.BackgroundColor) > 0 - currentHasBranding := current.Branding != nil - currentHasColors := currentHasBranding && current.Branding.Colors != nil + currentHasBranding := oldOrg.Branding != nil + currentHasColors := currentHasBranding && oldOrg.Branding.Colors != nil needToAddColors := isAccentColorSet || isBackgroundColorSet || currentHasColors if isLogoURLSet || needToAddColors { - o.Branding = &management.OrganizationBranding{} + newOrg.Branding = &management.OrganizationBranding{} if isLogoURLSet { - o.Branding.LogoURL = &inputs.LogoURL + newOrg.Branding.LogoURL = &inputs.LogoURL } else if currentHasBranding { - o.Branding.LogoURL = current.Branding.LogoURL + newOrg.Branding.LogoURL = oldOrg.Branding.LogoURL } if needToAddColors { @@ -364,33 +356,32 @@ func updateOrganizationCmd(cli *cli) *cobra.Command { if isAccentColorSet { colors[apiOrganizationColorPrimary] = inputs.AccentColor - } else if currentHasColors && len(current.Branding.GetColors()[apiOrganizationColorPrimary]) > 0 { - colors[apiOrganizationColorPrimary] = current.Branding.GetColors()[apiOrganizationColorPrimary] + } else if currentHasColors && len(oldOrg.Branding.GetColors()[apiOrganizationColorPrimary]) > 0 { + colors[apiOrganizationColorPrimary] = oldOrg.Branding.GetColors()[apiOrganizationColorPrimary] } if isBackgroundColorSet { colors[apiOrganizationColorPageBackground] = inputs.BackgroundColor - } else if currentHasColors && len(current.Branding.GetColors()[apiOrganizationColorPageBackground]) > 0 { - colors[apiOrganizationColorPageBackground] = current.Branding.GetColors()[apiOrganizationColorPageBackground] + } else if currentHasColors && len(oldOrg.Branding.GetColors()[apiOrganizationColorPageBackground]) > 0 { + colors[apiOrganizationColorPageBackground] = oldOrg.Branding.GetColors()[apiOrganizationColorPageBackground] } - o.Branding.Colors = &colors + newOrg.Branding.Colors = &colors } } - if len(inputs.Metadata) == 0 { - o.Metadata = current.Metadata - } else { - o.Metadata = &inputs.Metadata + newOrg.Metadata = oldOrg.Metadata + if len(inputs.Metadata) != 0 { + newOrg.Metadata = &inputs.Metadata } if err = ansi.Waiting(func() error { - return cli.api.Organization.Update(inputs.ID, o) + return cli.api.Organization.Update(inputs.ID, newOrg) }); err != nil { return err } - cli.renderer.OrganizationUpdate(o) + cli.renderer.OrganizationUpdate(newOrg) return nil }, } diff --git a/test/integration/scripts/get-org-id.sh b/test/integration/scripts/get-org-id.sh new file mode 100755 index 000000000..b5ec98ef1 --- /dev/null +++ b/test/integration/scripts/get-org-id.sh @@ -0,0 +1,6 @@ +#! /bin/bash + +org=$( auth0 orgs create -n integration-test-org-better -d "Integration Test Better Organization" --json --no-input ) + +mkdir -p ./test/integration/identifiers +echo "$org" | jq -r '.["id"]' > ./test/integration/identifiers/org-id diff --git a/test/integration/scripts/test-cleanup.sh b/test/integration/scripts/test-cleanup.sh index f20930530..a8da99c0f 100755 --- a/test/integration/scripts/test-cleanup.sh +++ b/test/integration/scripts/test-cleanup.sh @@ -88,3 +88,19 @@ for rule in $( echo "${rules}" | jq -r '.[] | @base64' ); do $( auth0 rules delete "$id") fi done + +orgs=$( auth0 orgs list --json --no-input ) + +for org in $( echo "${orgs}" | jq -r '.[] | @base64' ); do + _jq() { + echo "${org}" | base64 --decode | jq -r "${1}" + } + + id=$(_jq '.id') + name=$(_jq '.name') + if [[ $name = integration-test-org-* ]] + then + echo deleting "$name" + $( auth0 orgs delete "$id") + fi +done diff --git a/test/integration/test-cases.yaml b/test/integration/test-cases.yaml index 8457936a6..255573f57 100644 --- a/test/integration/test-cases.yaml +++ b/test/integration/test-cases.yaml @@ -450,7 +450,7 @@ tests: email: betteruser@example.com # Name is not being displayed, hence using email exit-code: 0 - # Test 'roles create' + # Test 'roles create' roles create and check data: command: auth0 roles create --name integration-test-role-new1 --description testRole --json --no-input exit-code: 0 @@ -664,3 +664,49 @@ tests: api patch tenant settings with wrong json: command: auth0 api patch "tenants/settings" --data "{\"idle_session_lifetime:72}" exit-code: 1 + + create organization and check json output: + command: auth0 orgs create --name integration-test-org-new --display "Integration Test Organization" --json --no-input + exit-code: 0 + stdout: + json: + name: "integration-test-org-new" + display_name: "Integration Test Organization" + + create organization and check table output: + command: auth0 orgs create --name integration-test-org-new2 --display "Integration Test Organization2" --no-input + exit-code: 0 + stdout: + contains: + - NAME integration-test-org-new2 + - DISPLAY NAME Integration Test Organization2 + + create organization to use in other tests: + command: ./test/integration/scripts/get-org-id.sh + exit-code: 0 + + show organization and check json output: + command: auth0 orgs show $(cat ./test/integration/identifiers/org-id) --json + exit-code: 0 + stdout: + json: + name: "integration-test-org-better" + display_name: "Integration Test Better Organization" + + show organization and check table output: + command: auth0 orgs show $(cat ./test/integration/identifiers/org-id) + exit-code: 0 + stdout: + contains: + - NAME integration-test-org-better + - DISPLAY NAME Integration Test Better Organization + + update organization: + command: auth0 orgs update $(cat ./test/integration/identifiers/org-id) -d "Integration Test Updated Organization" -a "#00FFAA" -b "#AA1166" --json --no-input + exit-code: 0 + stdout: + json: + name: "integration-test-org-better" + display_name: "Integration Test Updated Organization" + branding.colors.page_background: "#AA1166" + branding.colors.primary: "#00FFAA"