@@ -5,6 +5,7 @@ package iam
5
5
6
6
import (
7
7
"encoding/json"
8
+ "reflect"
8
9
"testing"
9
10
10
11
"github.com/hashicorp/terraform-provider-aws/internal/errs"
@@ -265,3 +266,127 @@ func TestIsValidAWSPrincipal(t *testing.T) { // nosemgrep:ci.aws-in-func-name
265
266
})
266
267
}
267
268
}
269
+
270
+ func TestIAMPolicyStatementConditionSet_MarshalJSON (t * testing.T ) { // nosemgrep:ci.iam-in-func-name
271
+ t .Parallel ()
272
+
273
+ testcases := map [string ]struct {
274
+ cs IAMPolicyStatementConditionSet
275
+ want []byte
276
+ wantErr bool
277
+ }{
278
+ "invalid value type" : {
279
+ cs : IAMPolicyStatementConditionSet {
280
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : 1 },
281
+ },
282
+ wantErr : true ,
283
+ },
284
+ "single condition single value" : {
285
+ cs : IAMPolicyStatementConditionSet {
286
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : "one/" },
287
+ },
288
+ want : []byte (`{"StringLike":{"s3:prefix":"one/"}}` ),
289
+ },
290
+ "single condition multiple values" : {
291
+ cs : IAMPolicyStatementConditionSet {
292
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : []string {"one/" , "two/" }},
293
+ },
294
+ want : []byte (`{"StringLike":{"s3:prefix":["one/","two/"]}}` ),
295
+ },
296
+ // Multiple distinct conditions
297
+ "multiple condition single value" : {
298
+ cs : IAMPolicyStatementConditionSet {
299
+ {Test : "ArnNotLike" , Variable : "aws:PrincipalArn" , Values : "1" },
300
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : "one/" },
301
+ },
302
+ want : []byte (`{"ArnNotLike":{"aws:PrincipalArn":"1"},"StringLike":{"s3:prefix":"one/"}}` ),
303
+ },
304
+ "multiple condition multiple values" : {
305
+ cs : IAMPolicyStatementConditionSet {
306
+ {Test : "ArnNotLike" , Variable : "aws:PrincipalArn" , Values : []string {"1" , "2" }},
307
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : []string {"one/" , "two/" }},
308
+ },
309
+ want : []byte (`{"ArnNotLike":{"aws:PrincipalArn":["1","2"]},"StringLike":{"s3:prefix":["one/","two/"]}}` ),
310
+ },
311
+ "multiple condition mixed value lengths" : {
312
+ cs : IAMPolicyStatementConditionSet {
313
+ {Test : "ArnNotLike" , Variable : "aws:PrincipalArn" , Values : "1" },
314
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : []string {"one/" , "two/" }},
315
+ },
316
+ want : []byte (`{"ArnNotLike":{"aws:PrincipalArn":"1"},"StringLike":{"s3:prefix":["one/","two/"]}}` ),
317
+ },
318
+ // Multiple conditions with duplicated `test` arguments
319
+ "duplicate condition test single value" : {
320
+ cs : IAMPolicyStatementConditionSet {
321
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : "one/" },
322
+ {Test : "StringLike" , Variable : "s3:versionid" , Values : "abc123" },
323
+ },
324
+ want : []byte (`{"StringLike":{"s3:prefix":"one/","s3:versionid":"abc123"}}` ),
325
+ },
326
+ "duplicate condition test multiple values" : {
327
+ cs : IAMPolicyStatementConditionSet {
328
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : []string {"one/" , "two/" }},
329
+ {Test : "StringLike" , Variable : "s3:versionid" , Values : []string {"abc123" , "def456" }},
330
+ },
331
+ want : []byte (`{"StringLike":{"s3:prefix":["one/","two/"],"s3:versionid":["abc123","def456"]}}` ),
332
+ },
333
+ "duplicate condition test mixed value lengths" : {
334
+ cs : IAMPolicyStatementConditionSet {
335
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : "one/" },
336
+ {Test : "StringLike" , Variable : "s3:versionid" , Values : []string {"abc123" , "def456" }},
337
+ },
338
+ want : []byte (`{"StringLike":{"s3:prefix":"one/","s3:versionid":["abc123","def456"]}}` ),
339
+ },
340
+ "duplicate condition test mixed value lengths reversed" : {
341
+ cs : IAMPolicyStatementConditionSet {
342
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : []string {"one/" , "two/" }},
343
+ {Test : "StringLike" , Variable : "s3:versionid" , Values : "abc123" },
344
+ },
345
+ want : []byte (`{"StringLike":{"s3:prefix":["one/","two/"],"s3:versionid":"abc123"}}` ),
346
+ },
347
+ // Multiple conditions with duplicated `test` and `variable` arguments
348
+ "duplicate condition test and variable single value" : {
349
+ cs : IAMPolicyStatementConditionSet {
350
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : "one/" },
351
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : "two/" },
352
+ },
353
+ want : []byte (`{"StringLike":{"s3:prefix":["one/","two/"]}}` ),
354
+ },
355
+ "duplicate condition test and variable multiple values" : {
356
+ cs : IAMPolicyStatementConditionSet {
357
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : []string {"one/" , "two/" }},
358
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : []string {"three/" , "four/" }},
359
+ },
360
+ want : []byte (`{"StringLike":{"s3:prefix":["one/","two/","three/","four/"]}}` ),
361
+ },
362
+ "duplicate condition test and variable mixed value lengths" : {
363
+ cs : IAMPolicyStatementConditionSet {
364
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : "one/" },
365
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : []string {"three/" , "four/" }},
366
+ },
367
+ want : []byte (`{"StringLike":{"s3:prefix":["one/","three/","four/"]}}` ),
368
+ },
369
+ "duplicate condition test and variable mixed value lengths reversed" : {
370
+ cs : IAMPolicyStatementConditionSet {
371
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : []string {"one/" , "two/" }},
372
+ {Test : "StringLike" , Variable : "s3:prefix" , Values : "three/" },
373
+ },
374
+ want : []byte (`{"StringLike":{"s3:prefix":["one/","two/","three/"]}}` ),
375
+ },
376
+ }
377
+ for name , tc := range testcases {
378
+ tc := tc
379
+ t .Run (name , func (t * testing.T ) {
380
+ t .Parallel ()
381
+
382
+ got , err := tc .cs .MarshalJSON ()
383
+ if (err != nil ) != tc .wantErr {
384
+ t .Errorf ("IAMPolicyStatementConditionSet.MarshalJSON() error = %v, wantErr %v" , err , tc .wantErr )
385
+ return
386
+ }
387
+ if ! reflect .DeepEqual (got , tc .want ) {
388
+ t .Errorf ("IAMPolicyStatementConditionSet.MarshalJSON() = %v, want %v" , string (got ), string (tc .want ))
389
+ }
390
+ })
391
+ }
392
+ }
0 commit comments