github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/azure/internal/azureauth/oauth_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package azureauth_test
     5  
     6  import (
     7  	"net/http"
     8  	"net/url"
     9  
    10  	"github.com/Azure/azure-sdk-for-go/arm/resources/subscriptions"
    11  	"github.com/Azure/go-autorest/autorest"
    12  	"github.com/Azure/go-autorest/autorest/azure"
    13  	"github.com/Azure/go-autorest/autorest/mocks"
    14  	"github.com/juju/testing"
    15  	jc "github.com/juju/testing/checkers"
    16  	gc "gopkg.in/check.v1"
    17  
    18  	"github.com/juju/juju/provider/azure/internal/azureauth"
    19  )
    20  
    21  type OAuthConfigSuite struct {
    22  	testing.IsolationSuite
    23  }
    24  
    25  var _ = gc.Suite(&OAuthConfigSuite{})
    26  
    27  const fakeTenantId = "11111111-1111-1111-1111-111111111111"
    28  
    29  func oauthConfigSender() autorest.Sender {
    30  	sender := mocks.NewSender()
    31  	resp := mocks.NewResponseWithStatus("", http.StatusUnauthorized)
    32  	mocks.SetResponseHeaderValues(resp, "WWW-Authenticate", []string{
    33  		`authorization_uri="https://testing.invalid/` + fakeTenantId + `"`,
    34  	})
    35  	sender.AppendResponse(resp)
    36  	return sender
    37  }
    38  
    39  func (s *OAuthConfigSuite) TestOAuthConfig(c *gc.C) {
    40  	client := subscriptions.Client{subscriptions.NewWithBaseURI("https://testing.invalid")}
    41  	client.Sender = oauthConfigSender()
    42  	cfg, tenantId, err := azureauth.OAuthConfig(client, "https://testing.invalid", "subscription-id")
    43  	c.Assert(err, jc.ErrorIsNil)
    44  	c.Assert(tenantId, gc.Equals, fakeTenantId)
    45  
    46  	baseURL := url.URL{
    47  		Scheme:   "https",
    48  		Host:     "testing.invalid",
    49  		RawQuery: "api-version=1.0",
    50  	}
    51  	expectedCfg := &azure.OAuthConfig{
    52  		AuthorizeEndpoint:  baseURL,
    53  		TokenEndpoint:      baseURL,
    54  		DeviceCodeEndpoint: baseURL,
    55  	}
    56  	expectedCfg.AuthorizeEndpoint.Path = "/11111111-1111-1111-1111-111111111111/oauth2/authorize"
    57  	expectedCfg.TokenEndpoint.Path = "/11111111-1111-1111-1111-111111111111/oauth2/token"
    58  	expectedCfg.DeviceCodeEndpoint.Path = "/11111111-1111-1111-1111-111111111111/oauth2/devicecode"
    59  
    60  	c.Assert(cfg, jc.DeepEquals, expectedCfg)
    61  }