Skip to content

Commit 40de4e2

Browse files
committed
Add 'TestAccCognitoIDPManagedLoginBranding_updateFromBasic'.
1 parent 39901a2 commit 40de4e2

File tree

2 files changed

+74
-19
lines changed

2 files changed

+74
-19
lines changed

internal/service/cognitoidp/managed_login_branding.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -312,32 +312,41 @@ func (r *managedLoginBrandingResource) Update(ctx context.Context, request resou
312312
return
313313
}
314314

315-
// Updated settings work in a PATCH model: https://docs.aws.amazon.com/cognito/latest/developerguide/managed-login-brandingeditor.html#branding-designer-api.
316-
var patch string
317-
var err error
318-
if oldSettings, newSettings := fwflex.StringValueFromFramework(ctx, old.Settings), fwflex.StringValueFromFramework(ctx, new.Settings); oldSettings != "" {
319-
patch, err = tfjson.CreateMergePatchFromStrings(oldSettings, newSettings)
315+
if oldSettings, newSettings := fwflex.StringValueFromFramework(ctx, old.Settings), fwflex.StringValueFromFramework(ctx, new.Settings); newSettings != oldSettings {
316+
if oldSettings == "" {
317+
var err error
318+
input.Settings, err = tfsmithy.DocumentFromJSONString(newSettings, document.NewLazyDocument)
320319

321-
if err != nil {
322-
response.Diagnostics.AddError("creating JSON merge patch", err.Error())
320+
if err != nil {
321+
response.Diagnostics.AddError("creating Smithy document", err.Error())
323322

324-
return
325-
}
326-
input.UseCognitoProvidedValues = false
327-
} else if newSettings != "" {
328-
patch = newSettings
329-
input.UseCognitoProvidedValues = false
330-
}
323+
return
324+
}
331325

332-
input.Settings, err = tfsmithy.DocumentFromJSONString(patch, document.NewLazyDocument)
326+
input.UseCognitoProvidedValues = false
327+
} else if newSettings != "" {
328+
// Updated settings work in a PATCH model: https://docs.aws.amazon.com/cognito/latest/developerguide/managed-login-brandingeditor.html#branding-designer-api.
329+
patch, err := tfjson.CreateMergePatchFromStrings(oldSettings, newSettings)
333330

334-
if err != nil {
335-
response.Diagnostics.AddError("creating Smithy document", err.Error())
331+
if err != nil {
332+
response.Diagnostics.AddError("creating JSON merge patch", err.Error())
336333

337-
return
334+
return
335+
}
336+
337+
input.Settings, err = tfsmithy.DocumentFromJSONString(patch, document.NewLazyDocument)
338+
339+
if err != nil {
340+
response.Diagnostics.AddError("creating Smithy document", err.Error())
341+
342+
return
343+
}
344+
345+
input.UseCognitoProvidedValues = false
346+
}
338347
}
339348

340-
_, err = conn.UpdateManagedLoginBranding(ctx, &input)
349+
_, err := conn.UpdateManagedLoginBranding(ctx, &input)
341350

342351
if err != nil {
343352
response.Diagnostics.AddError(fmt.Sprintf("updating Cognito Managed Login Branding (%s)", managedLoginBrandingID), err.Error())

internal/service/cognitoidp/managed_login_branding_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,52 @@ func TestAccCognitoIDPManagedLoginBranding_updateFromBasic(t *testing.T) {
209209
})
210210
}
211211

212+
func TestAccCognitoIDPManagedLoginBranding_updateToBasic(t *testing.T) {
213+
ctx := acctest.Context(t)
214+
var v awstypes.ManagedLoginBrandingType
215+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
216+
resourceName := "aws_cognito_managed_login_branding.test"
217+
218+
resource.ParallelTest(t, resource.TestCase{
219+
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckIdentityProvider(ctx, t) },
220+
ErrorCheck: acctest.ErrorCheck(t, names.CognitoIDPServiceID),
221+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
222+
CheckDestroy: testAccCheckManagedLoginBrandingDestroy(ctx),
223+
Steps: []resource.TestStep{
224+
{
225+
Config: testAccManagedLoginBrandingConfig_settings(rName),
226+
Check: resource.ComposeAggregateTestCheckFunc(
227+
testAccCheckManagedLoginBrandingExists(ctx, resourceName, &v),
228+
),
229+
ConfigPlanChecks: resource.ConfigPlanChecks{
230+
PreApply: []plancheck.PlanCheck{
231+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionCreate),
232+
},
233+
},
234+
ConfigStateChecks: []statecheck.StateCheck{
235+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("settings"), knownvalue.NotNull()),
236+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("use_cognito_provided_values"), knownvalue.Bool(false)),
237+
},
238+
},
239+
{
240+
Config: testAccManagedLoginBrandingConfig_basic(rName),
241+
Check: resource.ComposeAggregateTestCheckFunc(
242+
testAccCheckManagedLoginBrandingExists(ctx, resourceName, &v),
243+
),
244+
ConfigPlanChecks: resource.ConfigPlanChecks{
245+
PreApply: []plancheck.PlanCheck{
246+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
247+
},
248+
},
249+
ConfigStateChecks: []statecheck.StateCheck{
250+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("settings"), knownvalue.Null()),
251+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("use_cognito_provided_values"), knownvalue.Bool(true)),
252+
},
253+
},
254+
},
255+
})
256+
}
257+
212258
func testAccCheckManagedLoginBrandingDestroy(ctx context.Context) resource.TestCheckFunc {
213259
return func(s *terraform.State) error {
214260
conn := acctest.Provider.Meta().(*conns.AWSClient).CognitoIDPClient(ctx)

0 commit comments

Comments
 (0)