Skip to content

Commit

Permalink
Fix unit tests and logic for promptScreen Rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya18101 committed Dec 20, 2024
1 parent 5f06ba2 commit 697a07b
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 71 deletions.
30 changes: 20 additions & 10 deletions management/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,18 +454,19 @@ type PromptRendering struct {

// MarshalJSON implements a custom [json.Marshaler].
func (c *PromptRendering) MarshalJSON() ([]byte, error) {
copyPromptRendering := *c

if c.RenderingMode != nil && *c.RenderingMode == RenderingModeStandard {
copyPromptRendering.ContextConfiguration = nil
copyPromptRendering.DefaultHeadTagsDisabled = nil
copyPromptRendering.HeadTags = nil
type RenderingSubSet struct {
RenderingMode *RenderingMode `json:"rendering_mode,omitempty"`
ContextConfiguration *[]string `json:"context_configuration,omitempty"`
DefaultHeadTagsDisabled *bool `json:"default_head_tags_disabled,omitempty"`
HeadTags []interface{} `json:"head_tags,omitempty"`
}
copyPromptRendering.Tenant = nil
copyPromptRendering.Prompt = nil
copyPromptRendering.Screen = nil

return json.Marshal(&copyPromptRendering)
return json.Marshal(&RenderingSubSet{
RenderingMode: c.RenderingMode,
ContextConfiguration: c.ContextConfiguration,
DefaultHeadTagsDisabled: c.DefaultHeadTagsDisabled,
HeadTags: c.HeadTags,
})
}

// MarshalJSON implements a custom [json.Marshaler].
Expand Down Expand Up @@ -632,10 +633,19 @@ func (m *PromptManager) ReadRendering(ctx context.Context, prompt PromptType, sc
err = m.management.Request(ctx, "GET", m.management.URI("prompts", string(prompt), "screen", string(screen), "rendering"), &c, opts...)
return
}
func (c *PromptRendering) cleanForPatch() *PromptRendering {
if c.RenderingMode != nil && *c.RenderingMode == RenderingModeStandard {
return &PromptRendering{
RenderingMode: c.RenderingMode,
}
}
return c
}

// UpdateRendering updates the settings for the ACUL.
//
// See: https://auth0.com/docs/api/management/v2/prompts/patch-rendering
func (m *PromptManager) UpdateRendering(ctx context.Context, prompt PromptType, screen ScreenName, c *PromptRendering, opts ...RequestOption) error {
c = c.cleanForPatch()
return m.management.Request(ctx, "PATCH", m.management.URI("prompts", string(prompt), "screen", string(screen), "rendering"), c, opts...)
}
48 changes: 29 additions & 19 deletions management/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestPromptManager_ReadRendering(t *testing.T) {

_ = givenACustomDomain(t)
_ = givenAUniversalLoginTemplate(t)
expected := givenAPromptRendering(t)
expected := givenAPromptRendering(t, RenderingModeAdvanced)
actual, err := api.Prompt.ReadRendering(context.Background(), PromptSignup, ScreenSignup)
assert.NoError(t, err)
assert.Equal(t, expected.GetRenderingMode(), actual.GetRenderingMode())
Expand All @@ -143,15 +143,19 @@ func TestPromptManager_ReadRendering(t *testing.T) {
assert.Equal(t, ScreenSignup, *actual.GetScreen())
}

func TestPromptManager_UpdateRendering(t *testing.T) {
// Able to update the renderingMode to advanced and the setting configs when parsing the advanced renderingMode in payload

Check failure on line 146 in management/prompt_test.go

View workflow job for this annotation

GitHub Actions / Checks

Comment should end in a period (godot)
func TestPromptManager_UpdateRenderingWithAdvancedMode(t *testing.T) {
configureHTTPTestRecordings(t)

_ = givenACustomDomain(t)
_ = givenAUniversalLoginTemplate(t)
_ = givenAPromptRendering(t)
updateData := &PromptRendering{}
updateData.ContextConfiguration = &[]string{"branding.settings", "branding.themes.default", "client.logo_uri"}
updateData.DefaultHeadTagsDisabled = auth0.Bool(true)
_ = givenAPromptRendering(t, RenderingModeStandard)

updateData := &PromptRendering{
RenderingMode: &RenderingModeAdvanced,
ContextConfiguration: &[]string{"branding.settings", "branding.themes.default", "client.logo_uri"},
DefaultHeadTagsDisabled: auth0.Bool(true),
}

err := api.Prompt.UpdateRendering(context.Background(), PromptSignup, ScreenSignup, updateData)
assert.NoError(t, err)
Expand All @@ -164,17 +168,21 @@ func TestPromptManager_UpdateRendering(t *testing.T) {
assert.Equal(t, ScreenSignup, *actual.GetScreen())
}

// Unable to update the setting configs and only able to update the renderingMode to standard when parsing the standard renderingMode in payload

Check failure on line 171 in management/prompt_test.go

View workflow job for this annotation

GitHub Actions / Checks

Comment should end in a period (godot)
func TestPromptManager_UpdateRenderingWithStandardMode(t *testing.T) {
configureHTTPTestRecordings(t)

_ = givenACustomDomain(t)
_ = givenAUniversalLoginTemplate(t)
expected := givenAPromptRendering(t)
expected.RenderingMode = &RenderingModeStandard
expected.ContextConfiguration = &[]string{"branding.settings", "branding.themes.default", "client.logo_uri"}
expected.DefaultHeadTagsDisabled = auth0.Bool(true)
expected := givenAPromptRendering(t, RenderingModeAdvanced)

updateData := &PromptRendering{
RenderingMode: &RenderingModeStandard,
ContextConfiguration: &[]string{"branding.settings", "branding.themes.default", "client.logo_uri"},
DefaultHeadTagsDisabled: auth0.Bool(true),
}

err := api.Prompt.UpdateRendering(context.Background(), PromptSignup, ScreenSignup, expected)
err := api.Prompt.UpdateRendering(context.Background(), PromptSignup, ScreenSignup, updateData)
assert.NoError(t, err)

actual, err := api.Prompt.ReadRendering(context.Background(), PromptSignup, ScreenSignup)
Expand All @@ -187,16 +195,18 @@ func TestPromptManager_UpdateRenderingWithStandardMode(t *testing.T) {
assert.Equal(t, ScreenSignup, *actual.GetScreen())
}

func TestPromptManager_UpdateRenderingWithAdvancedMode(t *testing.T) {
// Able to update the setting's configs even the existing renderingMode is standard since renderingMode is not parsed in payload(updateData)

Check failure on line 198 in management/prompt_test.go

View workflow job for this annotation

GitHub Actions / Checks

Comment should end in a period (godot)
func TestPromptManager_UpdateRendering(t *testing.T) {
configureHTTPTestRecordings(t)

_ = givenACustomDomain(t)
_ = givenAUniversalLoginTemplate(t)
_ = givenAPromptRendering(t)
updateData := &PromptRendering{}
updateData.RenderingMode = &RenderingModeAdvanced
updateData.ContextConfiguration = &[]string{"branding.settings", "branding.themes.default", "client.logo_uri"}
updateData.DefaultHeadTagsDisabled = auth0.Bool(true)
_ = givenAPromptRendering(t, RenderingModeStandard)

updateData := &PromptRendering{
ContextConfiguration: &[]string{"branding.settings", "branding.themes.default", "client.logo_uri"},
DefaultHeadTagsDisabled: auth0.Bool(true),
}

err := api.Prompt.UpdateRendering(context.Background(), PromptSignup, ScreenSignup, updateData)
assert.NoError(t, err)
Expand Down Expand Up @@ -375,11 +385,11 @@ func givenAPartialPrompt(t *testing.T, prompt PromptType) *PromptScreenPartials
return partials
}

func givenAPromptRendering(t *testing.T) *PromptRendering {
func givenAPromptRendering(t *testing.T, mode RenderingMode) *PromptRendering {
t.Helper()

settings := &PromptRendering{
RenderingMode: &RenderingModeStandard,
RenderingMode: &mode,
ContextConfiguration: &[]string{"branding.settings", "branding.themes.default"},
DefaultHeadTagsDisabled: auth0.Bool(false),
HeadTags: []interface{}{
Expand Down
16 changes: 8 additions & 8 deletions test/data/recordings/TestPromptManager_ReadRendering.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interactions:
remote_addr: ""
request_uri: ""
body: |
{"domain":"1734684889.auth.uat.auth0.com","type":"auth0_managed_certs","tls_policy":"recommended"}
{"domain":"1734696838.auth.uat.auth0.com","type":"auth0_managed_certs","tls_policy":"recommended"}
form: {}
headers:
Content-Type:
Expand All @@ -34,9 +34,9 @@ interactions:
headers:
Content-Type:
- application/json; charset=utf-8
status: 201 Created
code: 201
duration: 923.825708ms
status: 409 Conflict
code: 409
duration: 1.190303208s
- id: 1
request:
proto: HTTP/1.1
Expand Down Expand Up @@ -72,7 +72,7 @@ interactions:
- application/json; charset=utf-8
status: 201 Created
code: 201
duration: 2.953769208s
duration: 778.887334ms
- id: 2
request:
proto: HTTP/1.1
Expand Down Expand Up @@ -108,7 +108,7 @@ interactions:
- application/json; charset=utf-8
status: 200 OK
code: 200
duration: 538.750459ms
duration: 430.98975ms
- id: 3
request:
proto: HTTP/1.1
Expand Down Expand Up @@ -143,7 +143,7 @@ interactions:
- application/json; charset=utf-8
status: 200 OK
code: 200
duration: 568.930541ms
duration: 428.221208ms
- id: 4
request:
proto: HTTP/1.1
Expand Down Expand Up @@ -178,7 +178,7 @@ interactions:
- application/json; charset=utf-8
status: 204 No Content
code: 204
duration: 593.201584ms
duration: 544.41275ms
- id: 5
request:
proto: HTTP/1.1
Expand Down
40 changes: 20 additions & 20 deletions test/data/recordings/TestPromptManager_UpdateRendering.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ interactions:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 100
content_length: 99
transfer_encoding: []
trailer: {}
host: go-auth0-dev.eu.auth0.com
remote_addr: ""
request_uri: ""
body: |
{"domain":"1733383424.auth.uat.auth0.com","type":"auth0_managed_certs","tls_policy":"recommended"}
{"domain":"1734698728.auth.uat.auth0.com","type":"auth0_managed_certs","tls_policy":"recommended"}
form: {}
headers:
Content-Type:
- application/json
User-Agent:
- Go-Auth0/1.12.0
- Go-Auth0/1.13.0
url: https://go-auth0-dev.eu.auth0.com/api/v2/custom-domains
method: POST
response:
Expand Down Expand Up @@ -55,7 +55,7 @@ interactions:
Content-Type:
- application/json
User-Agent:
- Go-Auth0/1.12.0
- Go-Auth0/1.13.0
url: https://go-auth0-dev.eu.auth0.com/api/v2/branding/templates/universal-login
method: PUT
response:
Expand All @@ -72,26 +72,26 @@ interactions:
- application/json; charset=utf-8
status: 201 Created
code: 201
duration: 2.698890708s
duration: 779.822584ms
- id: 2
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 408
content_length: 30
transfer_encoding: []
trailer: {}
host: go-auth0-dev.eu.auth0.com
remote_addr: ""
request_uri: ""
body: |
{"rendering_mode":"advanced","context_configuration":["branding.settings","branding.themes.default"],"default_head_tags_disabled":false,"head_tags":[{"attributes":{"async":true,"defer":true,"integrity":["sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g=="],"src":"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"},"content":"","tag":"script"}]}
{"rendering_mode":"standard"}
form: {}
headers:
Content-Type:
- application/json
User-Agent:
- Go-Auth0/1.12.0
- Go-Auth0/1.13.0
url: https://go-auth0-dev.eu.auth0.com/api/v2/prompts/signup/screen/signup/rendering
method: PATCH
response:
Expand All @@ -100,15 +100,15 @@ interactions:
proto_minor: 0
transfer_encoding: []
trailer: {}
content_length: -1
uncompressed: true
body: '{"rendering_mode":"advanced","context_configuration":["branding.settings","branding.themes.default"],"default_head_tags_disabled":false,"head_tags":[{"attributes":{"async":true,"defer":true,"integrity":["sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g=="],"src":"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"},"content":"","tag":"script"}]}'
content_length: 29
uncompressed: false
body: '{"rendering_mode":"standard"}'
headers:
Content-Type:
- application/json; charset=utf-8
status: 200 OK
code: 200
duration: 442.125917ms
duration: 454.494375ms
- id: 3
request:
proto: HTTP/1.1
Expand All @@ -127,7 +127,7 @@ interactions:
Content-Type:
- application/json
User-Agent:
- Go-Auth0/1.12.0
- Go-Auth0/1.13.0
url: https://go-auth0-dev.eu.auth0.com/api/v2/prompts/signup/screen/signup/rendering
method: PATCH
response:
Expand All @@ -144,7 +144,7 @@ interactions:
- application/json; charset=utf-8
status: 200 OK
code: 200
duration: 431.61225ms
duration: 425.653542ms
- id: 4
request:
proto: HTTP/1.1
Expand All @@ -162,7 +162,7 @@ interactions:
Content-Type:
- application/json
User-Agent:
- Go-Auth0/1.12.0
- Go-Auth0/1.13.0
url: https://go-auth0-dev.eu.auth0.com/api/v2/prompts/signup/screen/signup/rendering
method: GET
response:
Expand All @@ -173,13 +173,13 @@ interactions:
trailer: {}
content_length: -1
uncompressed: true
body: '{"tenant":"go-auth0-dev.eu.auth0.com","prompt":"signup","screen":"signup","rendering_mode":"advanced","context_configuration":["branding.settings","branding.themes.default","client.logo_uri"],"default_head_tags_disabled":true,"head_tags":[{"tag":"script","content":"","attributes":{"src":"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js","async":true,"defer":true,"integrity":["sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g=="]}}]}'
body: '{"tenant":"go-auth0-dev.eu.auth0.com","prompt":"signup","screen":"signup","rendering_mode":"standard","context_configuration":["branding.settings","branding.themes.default","client.logo_uri"],"default_head_tags_disabled":true,"head_tags":[{"tag":"script","content":"","attributes":{"src":"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js","async":true,"defer":true,"integrity":["sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g=="]}}]}'
headers:
Content-Type:
- application/json; charset=utf-8
status: 200 OK
code: 200
duration: 418.176625ms
duration: 476.536875ms
- id: 5
request:
proto: HTTP/1.1
Expand All @@ -197,7 +197,7 @@ interactions:
Content-Type:
- application/json
User-Agent:
- Go-Auth0/1.12.0
- Go-Auth0/1.13.0
url: https://go-auth0-dev.eu.auth0.com/api/v2/branding/templates/universal-login
method: DELETE
response:
Expand All @@ -214,7 +214,7 @@ interactions:
- application/json; charset=utf-8
status: 204 No Content
code: 204
duration: 464.237167ms
duration: 699.654875ms
- id: 6
request:
proto: HTTP/1.1
Expand All @@ -232,7 +232,7 @@ interactions:
Content-Type:
- application/json
User-Agent:
- Go-Auth0/1.12.0
- Go-Auth0/1.13.0
url: https://go-auth0-dev.eu.auth0.com/api/v2/custom-domains/cd_hW5C18CQPXRsKpDz
method: DELETE
response:
Expand Down
Loading

0 comments on commit 697a07b

Please sign in to comment.