|
1 | 1 | package idaas_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "testing" |
6 | 7 |
|
| 8 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/types" |
7 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
8 | 11 | "github.com/okta/terraform-provider-okta/okta/acctest" |
9 | 12 | "github.com/okta/terraform-provider-okta/okta/resources" |
@@ -227,6 +230,44 @@ func TestAccResourceOktaAppSignOnPolicyRules_chains(t *testing.T) { |
227 | 230 | }) |
228 | 231 | } |
229 | 232 |
|
| 233 | +// TestAccResourceOktaAppSignOnPolicyRules_chains_misaligned_keys verifies that |
| 234 | +// chains with non-alphabetical JSON key ordering (e.g., userVerification before |
| 235 | +// method) are normalized during plan, preventing "Provider produced inconsistent |
| 236 | +// result after apply" errors. This regression test covers OKTA-1184047. |
| 237 | +func TestAccResourceOktaAppSignOnPolicyRules_chains_misaligned_keys(t *testing.T) { |
| 238 | + resourceName := fmt.Sprintf("%s.test_chains_misaligned", resources.OktaIDaaSAppSignOnPolicyRules) |
| 239 | + mgr := newFixtureManager("resources", resources.OktaIDaaSAppSignOnPolicyRules, t.Name()) |
| 240 | + config := mgr.GetFixtures("chains_misaligned_keys.tf", t) |
| 241 | + acctest.OktaResourceTest(t, resource.TestCase{ |
| 242 | + PreCheck: acctest.AccPreCheck(t), |
| 243 | + ErrorCheck: testAccErrorChecks(t), |
| 244 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactoriesForTestAcc(t), |
| 245 | + CheckDestroy: checkAppSignOnPolicyRuleDestroy, |
| 246 | + Steps: []resource.TestStep{ |
| 247 | + { |
| 248 | + Config: config, |
| 249 | + Check: resource.ComposeTestCheckFunc( |
| 250 | + resource.TestCheckResourceAttrSet(resourceName, "id"), |
| 251 | + resource.TestCheckResourceAttrSet(resourceName, "policy_id"), |
| 252 | + resource.TestCheckResourceAttr(resourceName, "rule.#", "1"), |
| 253 | + resource.TestCheckResourceAttrSet(resourceName, "rule.0.id"), |
| 254 | + resource.TestCheckResourceAttr(resourceName, "rule.0.name", fmt.Sprintf("MisalignedKeys-testAcc_%s", mgr.SeedStr())), |
| 255 | + resource.TestCheckResourceAttr(resourceName, "rule.0.chains.#", "1"), |
| 256 | + // Verify the chain is stored |
| 257 | + resource.TestCheckResourceAttrSet(resourceName, "rule.0.chains.0"), |
| 258 | + ), |
| 259 | + }, |
| 260 | + { |
| 261 | + // Idempotency check — this should succeed without "inconsistent result" error. |
| 262 | + // Before the fix, this step would fail with: |
| 263 | + // "Provider produced inconsistent result after apply" |
| 264 | + Config: config, |
| 265 | + PlanOnly: true, |
| 266 | + }, |
| 267 | + }, |
| 268 | + }) |
| 269 | +} |
| 270 | + |
230 | 271 | // TestAccResourceOktaAppSignOnPolicyRules_keep_me_signed_in verifies that the |
231 | 272 | // keep_me_signed_in (KMSI / "Option to stay signed in") block on the plural |
232 | 273 | // resource round-trips correctly across multiple rules. The config defines four |
@@ -318,3 +359,69 @@ func TestAccResourceOktaAppSignOnPolicyRules_keep_me_signed_in(t *testing.T) { |
318 | 359 | }, |
319 | 360 | }) |
320 | 361 | } |
| 362 | + |
| 363 | +func TestChainsPlanModifier(t *testing.T) { |
| 364 | + modifier := idaas.ChainsPlanModifier{} |
| 365 | + |
| 366 | + tests := []struct { |
| 367 | + name string |
| 368 | + planValue string |
| 369 | + expectedValue string |
| 370 | + wantErr bool |
| 371 | + }{ |
| 372 | + { |
| 373 | + name: "already alphabetical", |
| 374 | + planValue: `{"key":"okta_verify","method":"push","userVerification":"OPTIONAL"}`, |
| 375 | + expectedValue: `{"key":"okta_verify","method":"push","userVerification":"OPTIONAL"}`, |
| 376 | + }, |
| 377 | + { |
| 378 | + name: "userVerification before method", |
| 379 | + planValue: `{"key":"okta_verify","userVerification":"OPTIONAL","method":"push"}`, |
| 380 | + expectedValue: `{"key":"okta_verify","method":"push","userVerification":"OPTIONAL"}`, |
| 381 | + }, |
| 382 | + { |
| 383 | + name: "complex nested non-alphabetical keys", |
| 384 | + planValue: `{"authenticationMethods":[{"userVerification":"OPTIONAL","method":"push","key":"okta_verify"}],"reauthenticateIn":"PT0S","next":[]}`, |
| 385 | + expectedValue: `{"authenticationMethods":[{"key":"okta_verify","method":"push","userVerification":"OPTIONAL"}],"next":[],"reauthenticateIn":"PT0S"}`, |
| 386 | + }, |
| 387 | + { |
| 388 | + name: "invalid JSON returns error diagnostic", |
| 389 | + planValue: `not-valid-json`, |
| 390 | + wantErr: true, |
| 391 | + }, |
| 392 | + } |
| 393 | + |
| 394 | + for _, tt := range tests { |
| 395 | + t.Run(tt.name, func(t *testing.T) { |
| 396 | + ctx := context.Background() |
| 397 | + listValue, diags := types.ListValueFrom(ctx, types.StringType, []string{tt.planValue}) |
| 398 | + if diags.HasError() { |
| 399 | + t.Fatalf("failed to create list value: %v", diags) |
| 400 | + } |
| 401 | + |
| 402 | + req := planmodifier.ListRequest{PlanValue: listValue} |
| 403 | + resp := &planmodifier.ListResponse{PlanValue: listValue} |
| 404 | + modifier.PlanModifyList(ctx, req, resp) |
| 405 | + |
| 406 | + if tt.wantErr { |
| 407 | + if !resp.Diagnostics.HasError() { |
| 408 | + t.Fatal("expected error diagnostic, got none") |
| 409 | + } |
| 410 | + return |
| 411 | + } |
| 412 | + |
| 413 | + if resp.Diagnostics.HasError() { |
| 414 | + t.Fatalf("unexpected error: %v", resp.Diagnostics) |
| 415 | + } |
| 416 | + |
| 417 | + var result []string |
| 418 | + resp.PlanValue.ElementsAs(ctx, &result, false) |
| 419 | + if len(result) != 1 { |
| 420 | + t.Fatalf("expected 1 element, got %d", len(result)) |
| 421 | + } |
| 422 | + if result[0] != tt.expectedValue { |
| 423 | + t.Errorf("got %s, want %s", result[0], tt.expectedValue) |
| 424 | + } |
| 425 | + }) |
| 426 | + } |
| 427 | +} |
0 commit comments