-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpolicy-assignment.spec.ts
More file actions
839 lines (731 loc) · 32.7 KB
/
policy-assignment.spec.ts
File metadata and controls
839 lines (731 loc) · 32.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/**
* Comprehensive tests for the unified PolicyAssignment implementation
*
* This test suite validates the unified PolicyAssignment class that uses
* the VersionedAzapiResource framework. Tests cover automatic version resolution,
* explicit version pinning, schema validation, property transformation, and
* policy assignment-specific functionality.
*/
import { Testing } from "cdktf";
import * as cdktf from "cdktf";
import { ApiVersionManager } from "../../core-azure/lib/version-manager/api-version-manager";
import { VersionSupportLevel } from "../../core-azure/lib/version-manager/interfaces/version-interfaces";
import {
PolicyAssignment,
PolicyAssignmentProps,
} from "../lib/policy-assignment";
import {
ALL_POLICY_ASSIGNMENT_VERSIONS,
POLICY_ASSIGNMENT_TYPE,
} from "../lib/policy-assignment-schemas";
describe("PolicyAssignment - Unified Implementation", () => {
let app: cdktf.App;
let stack: cdktf.TerraformStack;
let manager: ApiVersionManager;
beforeEach(() => {
app = Testing.app();
stack = new cdktf.TerraformStack(app, "TestStack");
manager = ApiVersionManager.instance();
// Ensure Policy Assignment schemas are registered
try {
manager.registerResourceType(
POLICY_ASSIGNMENT_TYPE,
ALL_POLICY_ASSIGNMENT_VERSIONS,
);
} catch (error) {
// Ignore if already registered
}
});
describe("Constructor and Basic Properties", () => {
it("should create policy assignment with automatic latest version resolution", () => {
const props: PolicyAssignmentProps = {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope:
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg",
};
const assignment = new PolicyAssignment(stack, "TestAssignment", props);
expect(assignment).toBeInstanceOf(PolicyAssignment);
expect(assignment.resolvedApiVersion).toBe("2022-06-01"); // Latest version
expect(assignment.props).toBe(props);
expect(assignment.name).toBe("test-assignment");
});
it("should create policy assignment with explicit version pinning", () => {
const props: PolicyAssignmentProps = {
name: "test-assignment-pinned",
apiVersion: "2022-06-01",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
};
const assignment = new PolicyAssignment(stack, "TestAssignment", props);
expect(assignment.resolvedApiVersion).toBe("2022-06-01");
});
it("should create policy assignment with all optional properties", () => {
const props: PolicyAssignmentProps = {
name: "test-assignment-full",
displayName: "Test Policy Assignment",
description: "A test policy assignment for unit testing",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope:
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg",
enforcementMode: "Default",
parameters: {
tagName: {
value: "Environment",
},
},
metadata: {
assignedBy: "admin@example.com",
},
identity: {
type: "SystemAssigned",
},
notScopes: [
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/excluded-rg",
],
nonComplianceMessages: [
{
message: "Resource must comply with policy",
},
],
ignoreChanges: ["metadata"],
enableValidation: true,
enableMigrationAnalysis: true,
enableTransformation: true,
};
const assignment = new PolicyAssignment(stack, "TestAssignment", props);
expect(assignment.props.displayName).toBe("Test Policy Assignment");
expect(assignment.props.description).toBe(
"A test policy assignment for unit testing",
);
expect(assignment.props.enforcementMode).toBe("Default");
expect(assignment.props.parameters).toBeDefined();
expect(assignment.props.metadata).toBeDefined();
expect(assignment.props.identity).toBeDefined();
expect(assignment.props.notScopes).toBeDefined();
expect(assignment.props.nonComplianceMessages).toBeDefined();
});
it("should use default name when name is not provided", () => {
const props: PolicyAssignmentProps = {
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
};
const assignment = new PolicyAssignment(stack, "TestAssignment", props);
expect(assignment.name).toBe("TestAssignment");
});
it("should require policyDefinitionId to be provided", () => {
const props: any = {
name: "test-assignment",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
};
expect(() => {
new PolicyAssignment(stack, "TestAssignment", props);
}).toThrow("Required property 'policyDefinitionId' is missing");
});
it("should require scope to be provided", () => {
const props: any = {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
};
expect(() => {
new PolicyAssignment(stack, "TestAssignment", props);
}).toThrow("Required property 'scope' is missing");
});
});
describe("Framework Integration", () => {
it("should resolve latest API version automatically", () => {
const assignment = new PolicyAssignment(stack, "TestAssignment", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
expect(assignment.resolvedApiVersion).toBe("2022-06-01");
expect(assignment.latestVersion()).toBe("2022-06-01");
});
it("should support all registered API versions", () => {
const assignment = new PolicyAssignment(stack, "TestAssignment", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
const supportedVersions = assignment.supportedVersions();
expect(supportedVersions).toContain("2022-06-01");
});
it("should validate version support", () => {
// Valid version
expect(() => {
new PolicyAssignment(stack, "ValidVersion", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
apiVersion: "2022-06-01",
});
}).not.toThrow();
// Invalid version
expect(() => {
new PolicyAssignment(stack, "InvalidVersion", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
apiVersion: "2020-01-01",
});
}).toThrow("Unsupported API version '2020-01-01'");
});
it("should load correct schema for resolved version", () => {
const assignment = new PolicyAssignment(stack, "TestAssignment", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
apiVersion: "2022-06-01",
});
expect(assignment.schema).toBeDefined();
expect(assignment.schema.resourceType).toBe(POLICY_ASSIGNMENT_TYPE);
expect(assignment.schema.version).toBe("2022-06-01");
expect(assignment.schema.properties).toBeDefined();
});
it("should load version configuration correctly", () => {
const assignment = new PolicyAssignment(stack, "TestAssignment", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
expect(assignment.versionConfig).toBeDefined();
expect(assignment.versionConfig.version).toBe("2022-06-01");
expect(assignment.versionConfig.supportLevel).toBe(
VersionSupportLevel.ACTIVE,
);
});
});
describe("Property Validation", () => {
it("should validate properties when validation is enabled", () => {
const props: PolicyAssignmentProps = {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
enableValidation: true,
};
expect(() => {
new PolicyAssignment(stack, "TestAssignment", props);
}).not.toThrow();
});
it("should have validation results for valid properties", () => {
const assignment = new PolicyAssignment(stack, "TestAssignment", {
name: "valid-assignment",
displayName: "Valid Assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
enableValidation: true,
});
expect(assignment.validationResult).toBeDefined();
expect(assignment.validationResult!.valid).toBe(true);
expect(assignment.validationResult!.errors).toHaveLength(0);
});
it("should skip validation when disabled", () => {
const assignment = new PolicyAssignment(stack, "TestAssignment", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
enableValidation: false,
});
expect(assignment).toBeDefined();
});
});
describe("Migration Analysis", () => {
it("should skip migration analysis for single version", () => {
const assignment = new PolicyAssignment(stack, "TestAssignment", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
apiVersion: "2022-06-01",
});
// Since there's only one version, migration analysis should be skipped
expect(assignment).toBeDefined();
});
it("should skip migration analysis when disabled", () => {
const assignment = new PolicyAssignment(stack, "TestAssignment", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
enableMigrationAnalysis: false,
});
expect(assignment.migrationAnalysis).toBeUndefined();
});
});
describe("Resource Creation and Body", () => {
it("should create correct resource body with minimal properties", () => {
const assignment = new PolicyAssignment(stack, "TestAssignment", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope:
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg",
});
expect(assignment).toBeDefined();
expect(assignment.props.policyDefinitionId).toBeDefined();
expect(assignment.props.scope).toBeDefined();
});
it("should create correct resource body with all properties", () => {
const assignment = new PolicyAssignment(stack, "TestAssignment", {
name: "test-assignment",
displayName: "Test Assignment",
description: "A test assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
enforcementMode: "DoNotEnforce",
parameters: {
tagName: {
value: "Environment",
},
},
metadata: {
assignedBy: "admin@example.com",
},
identity: {
type: "SystemAssigned",
},
notScopes: [
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/excluded-rg",
],
nonComplianceMessages: [
{
message: "Resource must comply",
},
],
});
expect(assignment).toBeDefined();
expect(assignment.props.displayName).toBe("Test Assignment");
expect(assignment.props.enforcementMode).toBe("DoNotEnforce");
expect(assignment.props.identity).toBeDefined();
});
it("should create Terraform outputs", () => {
const assignment = new PolicyAssignment(stack, "TestAssignment", {
name: "test-assignment-outputs",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
expect(assignment.idOutput).toBeInstanceOf(cdktf.TerraformOutput);
expect(assignment.nameOutput).toBeInstanceOf(cdktf.TerraformOutput);
});
});
describe("Enforcement Mode Configuration", () => {
it("should use Default enforcement mode by default", () => {
const assignment = new PolicyAssignment(stack, "DefaultEnforcement", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
expect(assignment.enforcementMode).toBe("Default");
});
it("should support DoNotEnforce enforcement mode", () => {
const assignment = new PolicyAssignment(stack, "DoNotEnforceMode", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
enforcementMode: "DoNotEnforce",
});
expect(assignment.enforcementMode).toBe("DoNotEnforce");
});
it("should support explicit Default enforcement mode", () => {
const assignment = new PolicyAssignment(stack, "ExplicitDefault", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
enforcementMode: "Default",
});
expect(assignment.enforcementMode).toBe("Default");
});
});
describe("Identity Configuration", () => {
it("should support SystemAssigned identity", () => {
const assignment = new PolicyAssignment(stack, "SystemIdentity", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
identity: {
type: "SystemAssigned",
},
});
expect(assignment.props.identity).toBeDefined();
expect(assignment.props.identity!.type).toBe("SystemAssigned");
});
it("should support UserAssigned identity", () => {
const assignment = new PolicyAssignment(stack, "UserIdentity", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
identity: {
type: "UserAssigned",
userAssignedIdentities: {
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/identity":
{},
},
},
});
expect(assignment.props.identity).toBeDefined();
expect(assignment.props.identity!.type).toBe("UserAssigned");
expect(assignment.props.identity!.userAssignedIdentities).toBeDefined();
});
it("should support None identity", () => {
const assignment = new PolicyAssignment(stack, "NoIdentity", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
identity: {
type: "None",
},
});
expect(assignment.props.identity).toBeDefined();
expect(assignment.props.identity!.type).toBe("None");
});
});
describe("Parameters and Metadata", () => {
it("should support policy parameters", () => {
const assignment = new PolicyAssignment(stack, "WithParameters", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
parameters: {
tagName: {
value: "Environment",
},
tagValue: {
value: "Production",
},
},
});
expect(assignment.props.parameters).toBeDefined();
expect(assignment.props.parameters.tagName.value).toBe("Environment");
expect(assignment.props.parameters.tagValue.value).toBe("Production");
});
it("should support metadata", () => {
const assignment = new PolicyAssignment(stack, "WithMetadata", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
metadata: {
assignedBy: "admin@example.com",
assignedDate: "2024-01-01",
parameterScopes: {},
},
});
expect(assignment.props.metadata).toBeDefined();
expect(assignment.props.metadata.assignedBy).toBe("admin@example.com");
});
});
describe("NotScopes and NonComplianceMessages", () => {
it("should support notScopes", () => {
const assignment = new PolicyAssignment(stack, "WithNotScopes", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
notScopes: [
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/excluded-rg-1",
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/excluded-rg-2",
],
});
expect(assignment.props.notScopes).toBeDefined();
expect(assignment.props.notScopes).toHaveLength(2);
});
it("should support nonComplianceMessages", () => {
const assignment = new PolicyAssignment(
stack,
"WithNonComplianceMessages",
{
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
nonComplianceMessages: [
{
message: "Resource must have required tags",
},
{
message: "Resource location is not compliant",
policyDefinitionReferenceId: "locationPolicy",
},
],
},
);
expect(assignment.props.nonComplianceMessages).toBeDefined();
expect(assignment.props.nonComplianceMessages).toHaveLength(2);
expect(assignment.props.nonComplianceMessages![0].message).toBe(
"Resource must have required tags",
);
});
});
describe("Public Methods and Properties", () => {
let assignment: PolicyAssignment;
beforeEach(() => {
assignment = new PolicyAssignment(stack, "TestAssignment", {
name: "test-assignment",
displayName: "Test Assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope:
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg",
enforcementMode: "Default",
});
});
it("should have correct id format", () => {
expect(assignment.id).toMatch(/^\$\{.*\.id\}$/);
});
it("should have resourceId property matching id", () => {
expect(assignment.resourceId).toBe(assignment.id);
});
it("should return correct policyDefinitionId", () => {
expect(assignment.policyDefinitionId).toBe(
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
);
});
it("should return correct scope", () => {
expect(assignment.assignmentScope).toBe(
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg",
);
});
it("should return correct enforcementMode", () => {
expect(assignment.enforcementMode).toBe("Default");
});
});
describe("Ignore Changes Configuration", () => {
it("should apply ignore changes lifecycle rules", () => {
const assignment = new PolicyAssignment(stack, "IgnoreChanges", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
ignoreChanges: ["metadata", "description"],
});
expect(assignment).toBeInstanceOf(PolicyAssignment);
});
it("should handle empty ignore changes array", () => {
const assignment = new PolicyAssignment(stack, "EmptyIgnore", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
ignoreChanges: [],
});
expect(assignment).toBeInstanceOf(PolicyAssignment);
});
});
describe("Error Handling", () => {
it("should handle invalid API versions gracefully", () => {
expect(() => {
new PolicyAssignment(stack, "InvalidAPI", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
apiVersion: "invalid-version",
});
}).toThrow("Unsupported API version 'invalid-version'");
});
it("should handle validation errors when validation is enabled", () => {
expect(() => {
new PolicyAssignment(stack, "ValidationError", {
name: "test-assignment",
policyDefinitionId: undefined as any, // Missing required policyDefinitionId
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
enableValidation: true,
});
}).toThrow("Required property 'policyDefinitionId' is missing");
});
it("should handle schema registration errors gracefully", () => {
expect(() => {
new PolicyAssignment(stack, "SchemaTest", {
name: "test-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
}).not.toThrow();
});
});
describe("JSII Compliance", () => {
it("should have JSII-compliant constructor", () => {
expect(() => {
new PolicyAssignment(stack, "JsiiTest", {
name: "jsii-test",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
}).not.toThrow();
});
it("should have JSII-compliant properties", () => {
const assignment = new PolicyAssignment(stack, "JsiiProps", {
name: "jsii-props",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
expect(typeof assignment.id).toBe("string");
expect(typeof assignment.name).toBe("string");
expect(typeof assignment.resolvedApiVersion).toBe("string");
expect(typeof assignment.policyDefinitionId).toBe("string");
expect(typeof assignment.assignmentScope).toBe("string");
expect(typeof assignment.enforcementMode).toBe("string");
});
it("should have JSII-compliant methods", () => {
const assignment = new PolicyAssignment(stack, "JsiiMethods", {
name: "jsii-methods",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
expect(typeof assignment.latestVersion).toBe("function");
expect(typeof assignment.supportedVersions).toBe("function");
});
it("should serialize complex objects correctly", () => {
const assignment = new PolicyAssignment(stack, "JsiiSerialization", {
name: "jsii-serialization",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
parameters: {
testParam: {
value: "test",
},
},
});
expect(() => JSON.stringify(assignment.validationResult)).not.toThrow();
expect(() => JSON.stringify(assignment.schema)).not.toThrow();
expect(() => JSON.stringify(assignment.versionConfig)).not.toThrow();
});
});
describe("CDK Terraform Integration", () => {
it("should synthesize to valid Terraform configuration", () => {
new PolicyAssignment(stack, "SynthTest", {
name: "synth-test",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
const synthesized = Testing.synth(stack);
expect(synthesized).toBeDefined();
const stackConfig = JSON.parse(synthesized);
expect(stackConfig.resource).toBeDefined();
});
it("should work in complex CDK constructs", () => {
class ComplexConstruct extends cdktf.TerraformStack {
constructor(scope: cdktf.App, id: string) {
super(scope, id);
const assignment1 = new PolicyAssignment(this, "Assignment1", {
name: "assignment-1",
displayName: "First Assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/policy-1",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
const assignment2 = new PolicyAssignment(this, "Assignment2", {
name: "assignment-2",
displayName: "Second Assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/policy-2",
scope:
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg",
apiVersion: "2022-06-01",
});
new cdktf.TerraformOutput(this, "Assignment1Id", {
value: assignment1.id,
});
new cdktf.TerraformOutput(this, "Assignment2Id", {
value: assignment2.id,
});
}
}
expect(() => {
new ComplexConstruct(app, "ComplexStack");
}).not.toThrow();
});
it("should handle multiple policy assignments in the same stack", () => {
const assignment1 = new PolicyAssignment(stack, "Assignment1", {
name: "assignment-1",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/policy-1",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
const assignment2 = new PolicyAssignment(stack, "Assignment2", {
name: "assignment-2",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/policy-2",
scope:
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg",
apiVersion: "2022-06-01",
});
expect(assignment1.resolvedApiVersion).toBe("2022-06-01");
expect(assignment2.resolvedApiVersion).toBe("2022-06-01");
const synthesized = Testing.synth(stack);
expect(synthesized).toBeDefined();
});
});
describe("Scope Configurations", () => {
it("should support management group scope", () => {
const assignment = new PolicyAssignment(stack, "ManagementGroupScope", {
name: "mg-assignment",
policyDefinitionId:
"/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/providers/Microsoft.Management/managementGroups/my-mg",
});
expect(assignment.assignmentScope).toContain(
"/providers/Microsoft.Management/managementGroups/",
);
});
it("should support subscription scope", () => {
const assignment = new PolicyAssignment(stack, "SubscriptionScope", {
name: "subscription-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
});
expect(assignment.assignmentScope).toContain("/subscriptions/");
});
it("should support resource group scope", () => {
const assignment = new PolicyAssignment(stack, "ResourceGroupScope", {
name: "rg-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope:
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg",
});
expect(assignment.assignmentScope).toContain("/resourceGroups/");
});
it("should support resource scope", () => {
const assignment = new PolicyAssignment(stack, "ResourceScope", {
name: "resource-assignment",
policyDefinitionId:
"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/test-policy",
scope:
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.Storage/storageAccounts/teststorage",
});
expect(assignment.assignmentScope).toContain("/providers/");
});
});
});