Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Branding: support page_background with gradient #99

Merged
merged 5 commits into from
Apr 4, 2020
Merged
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
103 changes: 103 additions & 0 deletions management/branding.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package management

import (
"encoding/json"
"fmt"
)

type Branding struct {
// Change password page settings
Colors *BrandingColors `json:"colors,omitempty"`
Expand All @@ -16,10 +21,108 @@ type Branding struct {
type BrandingColors struct {
// Accent color
Primary *string `json:"primary,omitempty"`

// Page background color
// Only one of PageBackground and PageBackgroundGradient should be set.
// If both fields are set, PageBackground takes priority.
PageBackground *string `json:"-"`

// Page background gradient
// Only one of PageBackground and PageBackgroundGradient should be set.
// If both fields are set, PageBackground takes priority.
PageBackgroundGradient *BrandingPageBackgroundGradient `json:"-"`
}

type BrandingPageBackgroundGradient struct {
Type *string `json:"type,omitempty"`
Start *string `json:"start,omitempty"`
End *string `json:"end,omitempty"`
AngleDegree *int `json:"angle_deg,omitempty"`
}

type jsonPageBackgroundSolid struct {
PageBackground *string `json:"page_background,omitempty"`
}

type jsonPageBackgroundGradient struct {
PageBackgroundGradient *BrandingPageBackgroundGradient `json:"page_background,omitempty"`
}

// MarshalJSON implements the json.Marshaler interface.
//
// It is required to handle the json field page_background, which can either
// be a hex color string, or an object describing a gradient.
func (bc *BrandingColors) MarshalJSON() ([]byte, error) {
type brandingColors BrandingColors
type brandingColorsWrapper struct {
*brandingColors
RawPageBackground interface{} `json:"page_background,omitempty"`
}

alias := &brandingColorsWrapper{(*brandingColors)(bc), nil}

if bc.PageBackground != nil && bc.PageBackgroundGradient != nil {
return nil, fmt.Errorf("only one of PageBackground and PageBackgroundGradient is allowed")
} else if bc.PageBackground != nil {
alias.RawPageBackground = bc.PageBackground
} else if bc.PageBackgroundGradient != nil {
alias.RawPageBackground = bc.PageBackgroundGradient
}

return json.Marshal(alias)
}

// UnmarshalJSON implements the json.Unmarshaler interface.
//
// It is required to handle the json field page_background, which can either
// be a hex color string, or an object describing a gradient.
func (bc *BrandingColors) UnmarshalJSON(data []byte) error {
type brandingColors BrandingColors
type brandingColorsWrapper struct {
*brandingColors
RawPageBackground interface{} `json:"page_background,omitempty"`
}

alias := &brandingColorsWrapper{(*brandingColors)(bc), nil}

err := json.Unmarshal(data, alias)
if err != nil {
return err
}

if alias.RawPageBackground != nil {
// Use type-switch to determine wether its a constant or gradient page background
switch rawPageBackground := alias.RawPageBackground.(type) {
case string:
// Constant Page Background
bc.PageBackground = &rawPageBackground
bc.PageBackgroundGradient = nil

case map[string]interface{}:
// Gradient Page Background
gradient := &BrandingPageBackgroundGradient{}

// Marshal map back to JSON to Unmarshal into correct struct
gradientJSON, err := json.Marshal(rawPageBackground)
if err != nil {
return err
}

err = json.Unmarshal(gradientJSON, gradient)
if err != nil {
return err
}

bc.PageBackgroundGradient = gradient
bc.PageBackground = nil
default:
return fmt.Errorf("unexpected type for field page_background")
}
}

return nil
}

type BrandingFont struct {
// URL for the custom font. Must use HTTPS.
URL *string `json:"url,omitempty"`
Expand Down
88 changes: 88 additions & 0 deletions management/branding_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package management

import (
"encoding/json"
"reflect"
"testing"

"gopkg.in/auth0.v4"
Expand Down Expand Up @@ -37,4 +39,90 @@ func TestBranding(t *testing.T) {
branding, _ = m.Branding.Read()
t.Logf("%v\n", branding)
})

t.Run("Update with gradient", func(t *testing.T) {
err = m.Branding.Update(&Branding{
Colors: &BrandingColors{
Primary: auth0.String("#ea5323"),
PageBackgroundGradient: &BrandingPageBackgroundGradient{
Type: auth0.String("linear-gradient"),
Start: auth0.String("#000000"),
End: auth0.String("#ffffff"),
AngleDegree: auth0.Int(35),
},
},
FaviconURL: auth0.String("https://mycompany.org/favicon.ico"),
LogoURL: auth0.String("https://mycompany.org/logo.png"),
Font: &BrandingFont{
URL: auth0.String("https://mycompany.org/font.otf"),
},
})
if err != nil {
t.Error(err)
}
branding, _ = m.Branding.Read()
t.Logf("%v\n", branding)
})
}

func TestBrandingColors(t *testing.T) {
var serializerTests = []struct {
name string
bc *BrandingColors
json string
}{
{
name: "Solid background",
bc: &BrandingColors{
Primary: auth0.String("#ea5323"),
PageBackground: auth0.String("#000000"),
},
json: "{\"primary\":\"#ea5323\",\"page_background\":\"#000000\"}",
},
{
name: "Gradient background",
bc: &BrandingColors{
Primary: auth0.String("#ea5323"),
PageBackgroundGradient: &BrandingPageBackgroundGradient{
Type: auth0.String("linear-gradient"),
Start: auth0.String("#000000"),
End: auth0.String("#ffffff"),
AngleDegree: auth0.Int(35),
},
},
json: "{\"primary\":\"#ea5323\",\"page_background\":{\"type\":\"linear-gradient\",\"start\":\"#000000\",\"end\":\"#ffffff\",\"angle_deg\":35}}",
},
{
name: "No background",
bc: &BrandingColors{
Primary: auth0.String("#ea5323"),
},
json: "{\"primary\":\"#ea5323\"}",
},
}

for _, tt := range serializerTests {
t.Run(tt.name, func(t *testing.T) {
// Check custom Marshal
serializedBrandingColors, err := json.Marshal(tt.bc)
if err != nil {
t.Error(err)
}

if string(serializedBrandingColors) != tt.json {
t.Errorf("serialization: expected %v, got %v", tt.json, string(serializedBrandingColors))
}

// Check custom Unmarshal
var deserializedBrandingColors BrandingColors
err = json.Unmarshal([]byte(tt.json), &deserializedBrandingColors)
if err != nil {
t.Error(err)
}

if !reflect.DeepEqual(&deserializedBrandingColors, tt.bc) {
t.Errorf("deserialization: expected %v, got %v", tt.bc, &deserializedBrandingColors)
}
})
}
}
45 changes: 45 additions & 0 deletions management/management.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.