This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit tests and minor code cleanup for GetTenantID. (#3467)
- Loading branch information
Showing
3 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package acsengine | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
var ( | ||
mux *http.ServeMux | ||
server *httptest.Server | ||
) | ||
|
||
func setup() func() { | ||
mux = http.NewServeMux() | ||
server = httptest.NewServer(mux) | ||
|
||
return func() { | ||
server.Close() | ||
} | ||
} | ||
|
||
func TestGetTenantID(t *testing.T) { | ||
|
||
tearDown := setup() | ||
defer tearDown() | ||
|
||
expectedTenantID := "96fe9d1-6171-40aa-945b-4c64b63bf655" | ||
mux.HandleFunc("/subscriptions/foobarsubscription", func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("WWW-Authenticate", `authorization_uri="https://login.windows.net/`+expectedTenantID+`"`) | ||
w.WriteHeader(http.StatusUnauthorized) | ||
w.Write([]byte("Unauthorized")) | ||
}) | ||
|
||
tenantID, err := GetTenantID(server.URL, "foobarsubscription") | ||
|
||
if err != nil { | ||
t.Error("Did not expect error") | ||
} | ||
|
||
if tenantID != expectedTenantID { | ||
t.Errorf("expected tenant Id : %s, but got %s", expectedTenantID, tenantID) | ||
} | ||
} | ||
|
||
func TestGetTenantID_UnexpectedResponse(t *testing.T) { | ||
|
||
tearDown := setup() | ||
defer tearDown() | ||
|
||
mux.HandleFunc("/subscriptions/foobarsubscription", func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
}) | ||
|
||
_, err := GetTenantID(server.URL, "foobarsubscription") | ||
|
||
expectedMsg := "Unexpected response from Get Subscription: 400" | ||
|
||
if err == nil || err.Error() != expectedMsg { | ||
t.Errorf("expected error with msg : %s to be thrown", expectedMsg) | ||
} | ||
} | ||
|
||
func TestGetTenantID_InvalidHeader(t *testing.T) { | ||
|
||
tearDown := setup() | ||
defer tearDown() | ||
|
||
mux.HandleFunc("/subscriptions/foobarsubscription", func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
w.Header().Set("fookey", "bazvalue") | ||
return | ||
}) | ||
|
||
_, err := GetTenantID(server.URL, "foobarsubscription") | ||
|
||
expectedMsg := "Header WWW-Authenticate not found in Get Subscription response" | ||
|
||
if err == nil || err.Error() != expectedMsg { | ||
t.Errorf("expected error with msg : %s to be thrown", expectedMsg) | ||
} | ||
} | ||
|
||
func TestGetTenantID_InvalidHeaderValue(t *testing.T) { | ||
|
||
tearDown := setup() | ||
defer tearDown() | ||
|
||
mux.HandleFunc("/subscriptions/foobarsubscription", func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("WWW-Authenticate", `sample_invalid_auth_uri`) | ||
w.WriteHeader(http.StatusUnauthorized) | ||
w.Write([]byte("Unauthorized")) | ||
}) | ||
|
||
_, err := GetTenantID(server.URL, "foobarsubscription") | ||
|
||
expectedMsg := "Could not find the tenant ID in header: WWW-Authenticate \"sample_invalid_auth_uri\"" | ||
|
||
if err == nil || err.Error() != expectedMsg { | ||
t.Errorf("expected error with msg : %s to be thrown", expectedMsg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters