Skip to content

Commit 9c58aa3

Browse files
committed
Updated tests
1 parent 8303efa commit 9c58aa3

File tree

2 files changed

+6
-40
lines changed

2 files changed

+6
-40
lines changed

apps/managedidentity/managedidentity.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -306,26 +306,11 @@ func acquireTokenForAzureArc(ctx context.Context, client Client, resource string
306306
return base.AuthResult{}, err
307307
}
308308

309-
secondResponse, err := client.httpClient.Do(secondRequest)
309+
tokenResponse, err := client.getTokenForRequest(secondRequest)
310310
if err != nil {
311311
return base.AuthResult{}, err
312312
}
313-
defer secondResponse.Body.Close()
314-
315-
responseBytes, err := io.ReadAll(secondResponse.Body)
316-
if err != nil {
317-
return base.AuthResult{}, fmt.Errorf("failed to read second azure arc response body: %s", err)
318-
}
319-
320-
var r accesstokens.TokenResponse
321-
err = json.Unmarshal(responseBytes, &r)
322-
if err != nil {
323-
return base.AuthResult{}, fmt.Errorf("failed to unmarshal second response body: %s", err)
324-
}
325-
326-
r.GrantedScopes.Slice = append(r.GrantedScopes.Slice, secondRequest.URL.Query().Get(resourceQueryParameterName))
327-
328-
return authResultFromToken(client.authParams, r)
313+
return authResultFromToken(client.authParams, tokenResponse)
329314
}
330315

331316
func createFakeAuthParams(client Client) (authority.AuthParams, error) {

apps/managedidentity/managedidentity_test.go

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ func TestAzureArcAcquireTokenReturnsToken(t *testing.T) {
347347
testCaseFilePath := getMockFilePath(t)
348348
type ArcRequest struct {
349349
name string
350-
statusCode int
351350
headers map[string]string
352351
expectedError string
353352
platform string
@@ -362,31 +361,27 @@ func TestAzureArcAcquireTokenReturnsToken(t *testing.T) {
362361
}{
363362
{resource: resource, miType: SystemAssigned(), apiVersion: azureArcAPIVersion, request: ArcRequest{
364363
name: "No www-authenticate header",
365-
statusCode: http.StatusUnauthorized,
366364
headers: map[string]string{},
367365
expectedError: "response has no www-authenticate header",
368366
platform: runtime.GOOS,
369367
shouldFail: true,
370368
}},
371369
{resource: resource, miType: SystemAssigned(), apiVersion: azureArcAPIVersion, request: ArcRequest{
372370
name: "Basic realm= not found",
373-
statusCode: http.StatusUnauthorized,
374371
headers: map[string]string{wwwAuthenticateHeaderName: "Basic "},
375372
expectedError: "basic realm= not found in the string, instead found: Basic ",
376373
platform: runtime.GOOS,
377374
shouldFail: true,
378375
}},
379376
{resource: resource, miType: SystemAssigned(), apiVersion: azureArcAPIVersion, request: ArcRequest{
380377
name: "Platform not supported",
381-
statusCode: http.StatusUnauthorized,
382378
headers: map[string]string{wwwAuthenticateHeaderName: "Basic realm=/path/to/secret.key"},
383379
expectedError: "platform not supported, expected linux or windows",
384380
platform: "freebsd",
385381
shouldFail: true,
386382
}},
387383
{resource: resource, miType: SystemAssigned(), apiVersion: azureArcAPIVersion, request: ArcRequest{
388384
name: "Invalid file extension",
389-
statusCode: http.StatusUnauthorized,
390385
headers: map[string]string{wwwAuthenticateHeaderName: "Basic realm=/path/to/secret.txt"},
391386
expectedError: "invalid file extension, expected .key, got .txt",
392387
platform: runtime.GOOS,
@@ -395,17 +390,15 @@ func TestAzureArcAcquireTokenReturnsToken(t *testing.T) {
395390
}},
396391
{resource: resource, miType: SystemAssigned(), apiVersion: azureArcAPIVersion, request: ArcRequest{
397392
name: "Invalid file path",
398-
statusCode: http.StatusUnauthorized,
399393
headers: map[string]string{wwwAuthenticateHeaderName: "Basic realm=" + filepath.Join("path", "to", "secret.key")},
400394
expectedError: "invalid file path, expected " + testCaseFilePath + ", got " + filepath.Join("path", "to"),
401395
platform: runtime.GOOS,
402396
createMockFile: true,
403397
shouldFail: true,
404398
}},
405399
{resource: resource, miType: SystemAssigned(), apiVersion: azureArcAPIVersion, request: ArcRequest{
406-
name: "Unable to get file info",
407-
statusCode: http.StatusUnauthorized,
408-
headers: map[string]string{wwwAuthenticateHeaderName: basicRealm + filepath.Join(testCaseFilePath, wrongSecretKey)},
400+
name: "Unable to get file info",
401+
headers: map[string]string{wwwAuthenticateHeaderName: basicRealm + filepath.Join(testCaseFilePath, wrongSecretKey)},
409402
expectedError: func() string {
410403
if runtime.GOOS == "windows" {
411404
return "failed to get metadata for " + filepath.Join(testCaseFilePath, wrongSecretKey) + " due to error: CreateFile " + filepath.Join(testCaseFilePath, wrongSecretKey) + ": The system cannot find the file specified."
@@ -418,27 +411,15 @@ func TestAzureArcAcquireTokenReturnsToken(t *testing.T) {
418411
}},
419412
{resource: resource, miType: SystemAssigned(), apiVersion: azureArcAPIVersion, request: ArcRequest{
420413
name: "Invalid secret file size",
421-
statusCode: http.StatusUnauthorized,
422414
headers: map[string]string{wwwAuthenticateHeaderName: basicRealm + filepath.Join(testCaseFilePath, secretKey)},
423415
expectedError: "invalid secret file size, expected 4096, file size was 5000",
424416
platform: runtime.GOOS,
425417
createMockFile: true,
426418
shouldFail: true,
427419
}},
428-
// {resource: resourceDefaultSuffix, miType: UserAssignedClientID("FakeClientID"), apiVersion: azureArcAPIVersion,
429-
// request: ArcRequest{
430-
// name: "success",
431-
// statusCode: http.StatusUnauthorized,
432-
// headers: map[string]string{wwwAuthenticateHeaderName: basicRealm + filepath.Join(testCaseFilePath, secretKey)},
433-
// expectedError: "",
434-
// platform: runtime.GOOS,
435-
// createMockFile: true,
436-
// shouldFail: false,
437-
// }},
438420
{resource: resourceDefaultSuffix, miType: SystemAssigned(), apiVersion: azureArcAPIVersion,
439421
request: ArcRequest{
440422
name: "success",
441-
statusCode: http.StatusUnauthorized,
442423
headers: map[string]string{wwwAuthenticateHeaderName: basicRealm + filepath.Join(testCaseFilePath, secretKey)},
443424
expectedError: "",
444425
platform: runtime.GOOS,
@@ -473,7 +454,7 @@ func TestAzureArcAcquireTokenReturnsToken(t *testing.T) {
473454
for k, v := range testCase.request.headers {
474455
headers.Set(k, v)
475456
}
476-
mockClient.AppendResponse(mock.WithHTTPStatusCode(testCase.request.statusCode),
457+
mockClient.AppendResponse(mock.WithHTTPStatusCode(http.StatusUnauthorized),
477458
mock.WithHTTPHeader(headers),
478459
mock.WithCallback(func(r *http.Request) {
479460
localUrl = r.URL
@@ -484,7 +465,7 @@ func TestAzureArcAcquireTokenReturnsToken(t *testing.T) {
484465
t.Fatalf(errorFormingJsonResponse, err.Error())
485466
}
486467

487-
mockClient.AppendResponse(mock.WithHTTPStatusCode(http.StatusUnauthorized), mock.WithHTTPHeader(headers),
468+
mockClient.AppendResponse(mock.WithHTTPStatusCode(http.StatusOK), mock.WithHTTPHeader(headers),
488469
mock.WithBody(responseBody), mock.WithCallback(func(r *http.Request) {
489470
localUrl = r.URL
490471
}))

0 commit comments

Comments
 (0)