@@ -113,3 +113,73 @@ func TestConcurrentWriteCredentialsToCache(t *testing.T) {
113113 assert .True (t , testTokenNumber >= 0 && testTokenNumber < 1000 ,
114114 "The token number should be within the expected range" )
115115}
116+
117+ func TestExtractAZPFromToken (t * testing.T ) {
118+ // Test cases
119+ tests := []struct {
120+ name string
121+ token string
122+ expected string
123+ hasError bool
124+ }{
125+ {
126+ name : "Valid token with azp claim" ,
127+ token : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhenAiOiJ0ZXN0LWFwcCJ9.YqenXXXX" , // token with azp: "test-app"
128+ expected : "test-app" ,
129+ hasError : false ,
130+ },
131+ {
132+ name : "Invalid token format" ,
133+ token : "invalid-token" ,
134+ expected : "ast-app" , // Should return default value
135+ hasError : false ,
136+ },
137+ {
138+ name : "Valid token without azp claim" ,
139+ token : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.XXXXX" ,
140+ expected : "ast-app" , // Should return default value
141+ hasError : false ,
142+ },
143+ }
144+
145+ // Run tests
146+ for _ , tt := range tests {
147+ t .Run (tt .name , func (t * testing.T ) {
148+ result , err := extractAZPFromToken (tt .token )
149+
150+ if tt .hasError {
151+ assert .Error (t , err )
152+ } else {
153+ assert .NoError (t , err )
154+ }
155+
156+ assert .Equal (t , tt .expected , result )
157+ })
158+ }
159+ }
160+
161+ func TestGetAPIKeyPayload (t * testing.T ) {
162+ tests := []struct {
163+ name string
164+ token string
165+ expected string
166+ }{
167+ {
168+ name : "Valid token with azp claim" ,
169+ token : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhenAiOiJ0ZXN0LWFwcCJ9.YqenXXXX" ,
170+ expected : "grant_type=refresh_token&client_id=test-app&refresh_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhenAiOiJ0ZXN0LWFwcCJ9.YqenXXXX" ,
171+ },
172+ {
173+ name : "Invalid token" ,
174+ token : "invalid-token" ,
175+ expected : "grant_type=refresh_token&client_id=ast-app&refresh_token=invalid-token" ,
176+ },
177+ }
178+
179+ for _ , tt := range tests {
180+ t .Run (tt .name , func (t * testing.T ) {
181+ result := getAPIKeyPayload (tt .token )
182+ assert .Equal (t , tt .expected , result )
183+ })
184+ }
185+ }
0 commit comments