github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/pkg/state/state_test.go (about) 1 package state 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types" 10 11 "github.com/khulnasoft-lab/defsec/pkg/providers/aws" 12 "github.com/khulnasoft-lab/defsec/pkg/providers/aws/s3" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func Test_RegoConversion(t *testing.T) { 18 s := State{ 19 AWS: aws.AWS{ 20 S3: s3.S3{ 21 Buckets: []s3.Bucket{ 22 { 23 Metadata: defsecTypes.NewMetadata( 24 defsecTypes.NewRange("main.tf", 2, 4, "", nil), 25 "aws_s3_bucket.example", 26 ), 27 Name: defsecTypes.String("my-bucket", defsecTypes.NewMetadata( 28 defsecTypes.NewRange("main.tf", 3, 3, "", nil), 29 "aws_s3_bucket.example.bucket", 30 )), 31 }, 32 }, 33 }, 34 }, 35 } 36 converted := s.ToRego() 37 assert.Equal(t, map[string]interface{}{ 38 "aws": map[string]interface{}{ 39 "s3": map[string]interface{}{ 40 "buckets": []interface{}{ 41 map[string]interface{}{ 42 "__defsec_metadata": map[string]interface{}{ 43 "resource": "aws_s3_bucket.example", 44 "sourceprefix": "", 45 "filepath": "main.tf", 46 "startline": 2, 47 "endline": 4, 48 "managed": true, 49 "explicit": false, 50 "fskey": "", 51 }, 52 "name": map[string]interface{}{ 53 "resource": "aws_s3_bucket.example.bucket", 54 "sourceprefix": "", 55 "filepath": "main.tf", 56 "startline": 3, 57 "endline": 3, 58 "value": "my-bucket", 59 "managed": true, 60 "explicit": false, 61 "fskey": "", 62 }, 63 }, 64 }, 65 }, 66 }, 67 }, converted) 68 } 69 70 func Test_JSONPersistenceOfData(t *testing.T) { 71 s := State{ 72 AWS: aws.AWS{ 73 S3: s3.S3{ 74 Buckets: []s3.Bucket{ 75 { 76 Metadata: defsecTypes.NewMetadata( 77 defsecTypes.NewRange("main.tf", 2, 4, "", nil), 78 "aws_s3_bucket.example", 79 ), 80 Name: defsecTypes.String("my-bucket", defsecTypes.NewMetadata( 81 defsecTypes.NewRange("main.tf", 3, 3, "", nil), 82 "aws_s3_bucket.example.bucket", 83 )), 84 }, 85 }, 86 }, 87 }, 88 } 89 data, err := json.Marshal(s) 90 require.NoError(t, err) 91 92 var restored State 93 require.NoError(t, json.Unmarshal(data, &restored)) 94 95 assert.Equal(t, s, restored) 96 }