11package aws
22
33import (
4+ "context"
5+ "strings"
46 "testing"
57
8+ "github.com/aws/aws-sdk-go-v2/aws"
9+ "github.com/aws/aws-sdk-go-v2/service/iam"
10+ "github.com/gruntwork-io/terratest/modules/random"
611 "github.com/stretchr/testify/assert"
12+ "github.com/stretchr/testify/require"
713)
814
915func TestGetIamCurrentUserName (t * testing.T ) {
@@ -19,3 +25,49 @@ func TestGetIamCurrentUserArn(t *testing.T) {
1925 username := GetIamCurrentUserArn (t )
2026 assert .Regexp (t , "^arn:aws:iam::[0-9]{12}:user/.+$" , username )
2127}
28+
29+ func TestGetIAMPolicyDocument (t * testing.T ) {
30+ t .Parallel ()
31+
32+ region := GetRandomRegion (t , nil , nil )
33+
34+ t .Run ("Exists" , func (t * testing.T ) {
35+ iamClient , err := NewIamClientE (t , region )
36+ require .NoError (t , err )
37+
38+ policyDocument := `{
39+ "Version": "2012-10-17",
40+ "Statement": [
41+ {
42+ "Sid": "Stmt1530709892083",
43+ "Action": "*",
44+ "Effect": "Allow",
45+ "Resource": "*"
46+ }
47+ ]
48+ }`
49+ input := & iam.CreatePolicyInput {
50+ PolicyName : aws .String (strings .ToLower (random .UniqueId ())),
51+ PolicyDocument : aws .String (policyDocument ),
52+ }
53+ policy , err := iamClient .CreatePolicy (context .Background (), input )
54+ require .NoError (t , err )
55+
56+ t .Cleanup (func () {
57+ t .Log ("Deleting IAM Policy Document" )
58+ _ , err := iamClient .DeletePolicy (context .Background (), & iam.DeletePolicyInput {
59+ PolicyArn : policy .Policy .Arn ,
60+ })
61+ require .NoError (t , err )
62+ })
63+
64+ p := GetIamPolicyDocument (t , region , * policy .Policy .Arn )
65+ t .Log ("Retrieved Policy Document:" , p )
66+ assert .JSONEq (t , policyDocument , p )
67+ })
68+
69+ t .Run ("DoesNotExist" , func (t * testing.T ) {
70+ _ , err := GetIamPolicyDocumentE (t , region , "arn:aws:iam::1234567890:policy/does-not-exist" )
71+ require .Error (t , err )
72+ })
73+ }
0 commit comments