Exemplo n.º 1
0
func TestServicePrincipalTokenRefreshUnmarshals(t *testing.T) {
	spt := newServicePrincipalToken()

	expiresOn := strconv.Itoa(int(time.Now().Add(3600 * time.Second).Sub(expirationBase).Seconds()))
	j := newTokenJSON(expiresOn, "resource")
	resp := mocks.NewResponseWithContent(j)
	c := mocks.NewClient()
	s := autorest.DecorateSender(c,
		(func() autorest.SendDecorator {
			return func(s autorest.Sender) autorest.Sender {
				return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
					return resp, nil
				})
			}
		})())
	spt.SetSender(s)

	err := spt.Refresh()
	if err != nil {
		t.Errorf("azure: ServicePrincipalToken#Refresh returned an unexpected error (%v)", err)
	} else if spt.AccessToken != "accessToken" ||
		spt.ExpiresIn != "3600" ||
		spt.ExpiresOn != expiresOn ||
		spt.NotBefore != expiresOn ||
		spt.Resource != "resource" ||
		spt.Type != "Bearer" {
		t.Errorf("azure: ServicePrincipalToken#Refresh failed correctly unmarshal the JSON -- expected %v, received %v",
			j, *spt)
	}
}
Exemplo n.º 2
0
func TestServicePrincipalTokenSetSender(t *testing.T) {
	spt := newServicePrincipalToken()

	var s autorest.Sender
	s = mocks.NewClient()
	spt.SetSender(s)
	if !reflect.DeepEqual(s, spt.sender) {
		t.Error("azure: ServicePrincipalToken#SetSender did not set the sender")
	}
}
Exemplo n.º 3
0
func TestServicePrincipalTokenRefreshPropagatesErrors(t *testing.T) {
	spt := newServicePrincipalToken()

	c := mocks.NewClient()
	c.EmitErrors(1)
	spt.SetSender(c)

	err := spt.Refresh()
	if err == nil {
		t.Error("azure: Failed to propagate the request error")
	}
}
Exemplo n.º 4
0
func TestServicePrincipalTokenRefreshReturnsErrorIfNotOk(t *testing.T) {
	spt := newServicePrincipalToken()

	c := mocks.NewClient()
	c.EmitStatus("401 NotAuthorized", 401)
	spt.SetSender(c)

	err := spt.Refresh()
	if err == nil {
		t.Error("azure: Failed to return an when receiving a status code other than HTTP 200")
	}
}
Exemplo n.º 5
0
func TestServicePrincipalTokenRefreshUsesPOST(t *testing.T) {
	spt := newServicePrincipalToken()

	c := mocks.NewClient()
	s := autorest.DecorateSender(c,
		(func() autorest.SendDecorator {
			return func(s autorest.Sender) autorest.Sender {
				return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
					if r.Method != "POST" {
						t.Errorf("azure: ServicePrincipalToken#Refresh did not correctly set HTTP method -- expected %v, received %v", "POST", r.Method)
					}
					return mocks.NewResponse(), nil
				})
			}
		})())
	spt.SetSender(s)
	spt.Refresh()
}
Exemplo n.º 6
0
func TestServicePrincipalTokenRefreshClosesRequestBody(t *testing.T) {
	spt := newServicePrincipalToken()

	resp := mocks.NewResponse()
	c := mocks.NewClient()
	s := autorest.DecorateSender(c,
		(func() autorest.SendDecorator {
			return func(s autorest.Sender) autorest.Sender {
				return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
					return resp, nil
				})
			}
		})())
	spt.SetSender(s)
	spt.Refresh()

	if resp.Body.(*mocks.Body).IsOpen() {
		t.Error("azure: ServicePrincipalToken#Refresh failed to close the HTTP Response Body")
	}
}
Exemplo n.º 7
0
func TestServicePrincipalTokenRefreshSetsURL(t *testing.T) {
	spt := newServicePrincipalToken()

	c := mocks.NewClient()
	s := autorest.DecorateSender(c,
		(func() autorest.SendDecorator {
			return func(s autorest.Sender) autorest.Sender {
				return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
					u := fmt.Sprintf("https://login.microsoftonline.com/%s/oauth2/token?api-version=1.0", spt.tenantId)
					if r.URL.String() != u {
						t.Errorf("azure: ServicePrincipalToken#Refresh did not correctly set the URL -- expected %v, received %v",
							u, r.URL)
					}
					return mocks.NewResponse(), nil
				})
			}
		})())
	spt.SetSender(s)
	spt.Refresh()
}
Exemplo n.º 8
0
func TestServicePrincipalTokenRefreshSetsMimeType(t *testing.T) {
	spt := newServicePrincipalToken()

	c := mocks.NewClient()
	s := autorest.DecorateSender(c,
		(func() autorest.SendDecorator {
			return func(s autorest.Sender) autorest.Sender {
				return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
					if r.Header.Get(http.CanonicalHeaderKey("Content-Type")) != "application/x-www-form-urlencoded" {
						t.Errorf("azure: ServicePrincipalToken#Refresh did not correctly set Content-Type -- expected %v, received %v",
							"application/x-form-urlencoded",
							r.Header.Get(http.CanonicalHeaderKey("Content-Type")))
					}
					return mocks.NewResponse(), nil
				})
			}
		})())
	spt.SetSender(s)
	spt.Refresh()
}
Exemplo n.º 9
0
func TestServicePrincipalTokenRefreshSetsBody(t *testing.T) {
	spt := newServicePrincipalToken()

	c := mocks.NewClient()
	s := autorest.DecorateSender(c,
		(func() autorest.SendDecorator {
			return func(s autorest.Sender) autorest.Sender {
				return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
					b, err := ioutil.ReadAll(r.Body)
					if err != nil {
						t.Errorf("azure: Failed to read body of Service Principal token request (%v)", err)
					} else if string(b) != defaultFormData {
						t.Errorf("azure: ServicePrincipalToken#Refresh did not correctly set the HTTP Request Body -- expected %v, received %v",
							defaultFormData, string(b))
					}
					return mocks.NewResponse(), nil
				})
			}
		})())
	spt.SetSender(s)
	spt.Refresh()
}