@@ -10,6 +10,8 @@ import (
1010 "net/http"
1111 "strings"
1212 "testing"
13+
14+ "github.com/aws/aws-sdk-go-v2/aws"
1315)
1416
1517func TestGetTableName (t * testing.T ) {
@@ -84,3 +86,133 @@ func TestGetRequestID(t *testing.T) {
8486 }
8587 }
8688}
89+
90+ func TestAWSAccountIdFromAWSAccessKey (t * testing.T ) {
91+ tests := []struct {
92+ name string // description of this test case
93+ // Named input parameters for target function.
94+ creds aws.Credentials
95+ want string
96+ wantErr bool
97+ wantErrStr string // error message returned
98+ }{
99+ {
100+ name : "first test" ,
101+ creds : aws.Credentials {
102+ AccountID : "" ,
103+ AccessKeyID : "AKIASAWSR23456AWS357" ,
104+ },
105+ want : "138954266361" ,
106+ wantErr : false ,
107+ },
108+ {
109+ name : "AccountID already exists and access key exists. Should return AccountID immediately" ,
110+ creds : aws.Credentials {
111+ AccountID : "123451234512" ,
112+ AccessKeyID : "ASKDHA123457AKJFHAKS" ,
113+ },
114+ want : "123451234512" ,
115+ wantErr : false ,
116+ },
117+ {
118+ name : "AccountID already exists and access key exists with too short of length. Should return AccountID immediately" ,
119+ creds : aws.Credentials {
120+ AccountID : "123451234512" ,
121+ AccessKeyID : "a" ,
122+ },
123+ want : "123451234512" ,
124+ wantErr : false ,
125+ },
126+ {
127+ name : "AccountID already exists and access key exists with improper format. Should return AccountID immediately" ,
128+ creds : aws.Credentials {
129+ AccountID : "123451234512" ,
130+ AccessKeyID : "a a a. " ,
131+ },
132+ want : "123451234512" ,
133+ wantErr : false ,
134+ },
135+ {
136+ name : "AccountID already exists and access key does not exist. Should return AccountID immediately" ,
137+ creds : aws.Credentials {
138+ AccountID : "123451234512" ,
139+ },
140+ want : "123451234512" ,
141+ wantErr : false ,
142+ },
143+ {
144+ name : "AccountID does not exist and access key does not exist. Should return an error" ,
145+ creds : aws.Credentials {},
146+ want : "" ,
147+ wantErr : true ,
148+ wantErrStr : "no access key id found" ,
149+ },
150+ {
151+ name : "AccountID does not exist and access key is in an improper format. Should return an error" ,
152+ creds : aws.Credentials {
153+ AccessKeyID : "123asdfas" ,
154+ },
155+ want : "" ,
156+ wantErr : true ,
157+ wantErrStr : "improper access key id format" ,
158+ },
159+ {
160+ name : "AccountID does not exist and access key is in an improper format with only one character. Should return an error" ,
161+ creds : aws.Credentials {
162+ AccessKeyID : "a" ,
163+ },
164+ want : "" ,
165+ wantErr : true ,
166+ wantErrStr : "improper access key id format" ,
167+ },
168+ {
169+ name : "AccountID does not exist and access key is in an improper format for decoding." ,
170+ creds : aws.Credentials {
171+ AccessKeyID : "a a a. " ,
172+ },
173+ want : "" ,
174+ wantErr : true ,
175+ wantErrStr : "error decoding access keys" ,
176+ },
177+ {
178+ name : "AccountID does not exist and access key contains non base32 characters" ,
179+ creds : aws.Credentials {
180+ AccessKeyID : "AKIA1234567899876541" ,
181+ },
182+ want : "" ,
183+ wantErr : true ,
184+ wantErrStr : "error decoding access keys" ,
185+ },
186+ {
187+ name : "AccountID does not exist and access key contains non base32 characters and is too short in length" ,
188+ creds : aws.Credentials {
189+ AccessKeyID : "AKIA1818" ,
190+ },
191+ want : "" ,
192+ wantErr : true ,
193+ wantErrStr : "improper access key id format" ,
194+ },
195+ }
196+ for _ , tt := range tests {
197+ t .Run (tt .name , func (t * testing.T ) {
198+ got , gotErr := AWSAccountIdFromAWSAccessKey (tt .creds )
199+ if gotErr != nil {
200+ if ! tt .wantErr {
201+ t .Errorf ("AWSAccountIdFromAWSAccessKey() failed: %v" , gotErr )
202+ } else {
203+ if tt .wantErrStr != gotErr .Error () {
204+ t .Errorf ("AWSAccountIdFromAWSAccessKey() error = %v, want %v" , gotErr .Error (), tt .wantErrStr )
205+ }
206+ }
207+ return
208+ }
209+ if tt .wantErr {
210+ t .Fatal ("AWSAccountIdFromAWSAccessKey() succeeded unexpectedly" )
211+ }
212+ // TODO: update the condition below to compare got with tt.want.
213+ if tt .want != got {
214+ t .Errorf ("AWSAccountIdFromAWSAccessKey() = %v, want %v" , got , tt .want )
215+ }
216+ })
217+ }
218+ }
0 commit comments