Skip to content

Commit

Permalink
DXCDT-572: Add ability to fetch all applications in ul customize cmd (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught authored Oct 23, 2023
1 parent 1088509 commit b1a4528
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 12 deletions.
72 changes: 60 additions & 12 deletions internal/cli/universal_login_customize.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ const (

type (
universalLoginBrandingData struct {
Settings *management.Branding `json:"settings"`
Template *management.BrandingUniversalLogin `json:"template"`
Theme *management.BrandingTheme `json:"theme"`
Tenant *tenantData `json:"tenant"`
Prompts []*promptData `json:"prompts"`
Applications []*applicationData `json:"applications"`
Prompts []*promptData `json:"prompts"`
Settings *management.Branding `json:"settings"`
Template *management.BrandingUniversalLogin `json:"template"`
Theme *management.BrandingTheme `json:"theme"`
Tenant *tenantData `json:"tenant"`
}

tenantData struct {
FriendlyName string `json:"friendly_name"`
EnabledLocales []string `json:"enabled_locales"`
Domain string `json:"domain"`
applicationData struct {
ID string `json:"id"`
Name string `json:"name"`
LogoURL string `json:"logo_url"`
Metadata map[string]interface{} `json:"metadata"`
}

promptData struct {
Expand All @@ -49,6 +51,12 @@ type (
CustomText map[string]interface{} `json:"custom_text,omitempty"`
}

tenantData struct {
FriendlyName string `json:"friendly_name"`
EnabledLocales []string `json:"enabled_locales"`
Domain string `json:"domain"`
}

errorData struct {
Error string `json:"error"`
}
Expand Down Expand Up @@ -213,14 +221,21 @@ func fetchUniversalLoginBrandingData(
return err
})

var applications []*applicationData
group.Go(func() (err error) {
applications, err = fetchAllApplications(ctx, api)
return err
})

if err := group.Wait(); err != nil {
return nil, err
}

return &universalLoginBrandingData{
Settings: brandingSettings,
Template: currentTemplate,
Theme: currentTheme,
Applications: applications,
Settings: brandingSettings,
Template: currentTemplate,
Theme: currentTheme,
Tenant: &tenantData{
FriendlyName: tenant.GetFriendlyName(),
EnabledLocales: tenant.GetEnabledLocales(),
Expand Down Expand Up @@ -340,6 +355,39 @@ func fetchPromptCustomTextWithDefaults(
}, nil
}

func fetchAllApplications(ctx context.Context, api *auth0.API) ([]*applicationData, error) {
var applications []*applicationData
var page int
for {
clientList, err := api.Client.List(
ctx,
management.Page(page),
management.PerPage(100),
management.IncludeFields("client_id", "name", "logo_uri", "client_metadata"),
)
if err != nil {
return nil, err
}

for _, client := range clientList.Clients {
applications = append(applications, &applicationData{
ID: client.GetClientID(),
Name: client.GetName(),
LogoURL: client.GetLogoURI(),
Metadata: client.GetClientMetadata(),
})
}

if !clientList.HasNext() {
break
}

page++
}

return applications, nil
}

func startWebSocketServer(
ctx context.Context,
api *auth0.API,
Expand Down
128 changes: 128 additions & 0 deletions internal/cli/universal_login_customize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,23 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) {
nil,
)

mockClientAPI := mock.NewMockClientAPI(ctrl)
mockClientAPI.
EXPECT().
List(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&management.ClientList{
Clients: []*management.Client{
{
ClientID: auth0.String("1"),
Name: auth0.String("My App"),
LogoURI: auth0.String("https://my-app.example.com/image.png"),
ClientMetadata: &map[string]interface{}{"meta": "meta"},
},
},
}, nil)

mockAPI := &auth0.API{
Client: mockClientAPI,
Branding: mockBrandingAPI,
BrandingTheme: mockBrandingThemeAPI,
Prompt: mockPromptAPI,
Expand All @@ -178,6 +194,14 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) {
return mockAPI
},
expectedData: &universalLoginBrandingData{
Applications: []*applicationData{
{
ID: "1",
Name: "My App",
LogoURL: "https://my-app.example.com/image.png",
Metadata: map[string]interface{}{"meta": "meta"},
},
},
Settings: &management.Branding{
Colors: &management.BrandingColors{
Primary: auth0.String("#334455"),
Expand Down Expand Up @@ -292,7 +316,23 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) {
nil,
)

mockClientAPI := mock.NewMockClientAPI(ctrl)
mockClientAPI.
EXPECT().
List(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&management.ClientList{
Clients: []*management.Client{
{
ClientID: auth0.String("1"),
Name: auth0.String("My App"),
LogoURI: auth0.String("https://my-app.example.com/image.png"),
ClientMetadata: &map[string]interface{}{"meta": "meta"},
},
},
}, nil)

mockAPI := &auth0.API{
Client: mockClientAPI,
Branding: mockBrandingAPI,
BrandingTheme: mockBrandingThemeAPI,
Prompt: mockPromptAPI,
Expand All @@ -302,6 +342,14 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) {
return mockAPI
},
expectedData: &universalLoginBrandingData{
Applications: []*applicationData{
{
ID: "1",
Name: "My App",
LogoURL: "https://my-app.example.com/image.png",
Metadata: map[string]interface{}{"meta": "meta"},
},
},
Settings: &management.Branding{
Colors: &management.BrandingColors{
Primary: auth0.String(defaultPrimaryColor),
Expand Down Expand Up @@ -420,7 +468,23 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) {
nil,
)

mockClientAPI := mock.NewMockClientAPI(ctrl)
mockClientAPI.
EXPECT().
List(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&management.ClientList{
Clients: []*management.Client{
{
ClientID: auth0.String("1"),
Name: auth0.String("My App"),
LogoURI: auth0.String("https://my-app.example.com/image.png"),
ClientMetadata: &map[string]interface{}{"meta": "meta"},
},
},
}, nil)

mockAPI := &auth0.API{
Client: mockClientAPI,
Branding: mockBrandingAPI,
BrandingTheme: mockBrandingThemeAPI,
Prompt: mockPromptAPI,
Expand All @@ -430,6 +494,14 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) {
return mockAPI
},
expectedData: &universalLoginBrandingData{
Applications: []*applicationData{
{
ID: "1",
Name: "My App",
LogoURL: "https://my-app.example.com/image.png",
Metadata: map[string]interface{}{"meta": "meta"},
},
},
Settings: &management.Branding{
Colors: &management.BrandingColors{
Primary: auth0.String("#334455"),
Expand Down Expand Up @@ -551,7 +623,23 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) {
nil,
)

mockClientAPI := mock.NewMockClientAPI(ctrl)
mockClientAPI.
EXPECT().
List(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&management.ClientList{
Clients: []*management.Client{
{
ClientID: auth0.String("1"),
Name: auth0.String("My App"),
LogoURI: auth0.String("https://my-app.example.com/image.png"),
ClientMetadata: &map[string]interface{}{"meta": "meta"},
},
},
}, nil)

mockAPI := &auth0.API{
Client: mockClientAPI,
Branding: mockBrandingAPI,
BrandingTheme: mockBrandingThemeAPI,
Prompt: mockPromptAPI,
Expand All @@ -561,6 +649,14 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) {
return mockAPI
},
expectedData: &universalLoginBrandingData{
Applications: []*applicationData{
{
ID: "1",
Name: "My App",
LogoURL: "https://my-app.example.com/image.png",
Metadata: map[string]interface{}{"meta": "meta"},
},
},
Settings: &management.Branding{
Colors: &management.BrandingColors{
Primary: auth0.String("#334455"),
Expand Down Expand Up @@ -740,7 +836,23 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) {
Read(gomock.Any()).
Return(nil, fmt.Errorf("failed to fetch tenant data"))

mockClientAPI := mock.NewMockClientAPI(ctrl)
mockClientAPI.
EXPECT().
List(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&management.ClientList{
Clients: []*management.Client{
{
ClientID: auth0.String("1"),
Name: auth0.String("My App"),
LogoURI: auth0.String("https://my-app.example.com/image.png"),
ClientMetadata: &map[string]interface{}{"meta": "meta"},
},
},
}, nil)

mockAPI := &auth0.API{
Client: mockClientAPI,
Branding: mockBrandingAPI,
BrandingTheme: mockBrandingThemeAPI,
Prompt: mockPromptAPI,
Expand Down Expand Up @@ -803,7 +915,23 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) {
CustomText(gomock.Any(), "login", "en").
Return(nil, fmt.Errorf("failed to fetch custom text"))

mockClientAPI := mock.NewMockClientAPI(ctrl)
mockClientAPI.
EXPECT().
List(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&management.ClientList{
Clients: []*management.Client{
{
ClientID: auth0.String("1"),
Name: auth0.String("My App"),
LogoURI: auth0.String("https://my-app.example.com/image.png"),
ClientMetadata: &map[string]interface{}{"meta": "meta"},
},
},
}, nil)

mockAPI := &auth0.API{
Client: mockClientAPI,
Branding: mockBrandingAPI,
BrandingTheme: mockBrandingThemeAPI,
Prompt: mockPromptAPI,
Expand Down

0 comments on commit b1a4528

Please sign in to comment.