github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"context"
     8  	"net/http"
     9  	"net/url"
    10  
    11  	"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-06-01/subscriptions"
    12  	"github.com/Azure/go-autorest/autorest"
    13  	"github.com/Azure/go-autorest/autorest/adal"
    14  	"github.com/Azure/go-autorest/autorest/mocks"
    15  	"github.com/juju/testing"
    16  	jc "github.com/juju/testing/checkers"
    17  	gc "gopkg.in/check.v1"
    18  
    19  	"github.com/juju/juju/provider/azure/internal/azureauth"
    20  )
    21  
    22  type OAuthConfigSuite struct {
    23  	testing.IsolationSuite
    24  }
    25  
    26  var _ = gc.Suite(&OAuthConfigSuite{})
    27  
    28  const fakeTenantId = "11111111-1111-1111-1111-111111111111"
    29  
    30  func oauthConfigSender() autorest.Sender {
    31  	sender := mocks.NewSender()
    32  	resp := mocks.NewResponseWithStatus("", http.StatusUnauthorized)
    33  	mocks.SetResponseHeaderValues(resp, "WWW-Authenticate", []string{
    34  		`authorization_uri="https://testing.invalid/` + fakeTenantId + `"`,
    35  	})
    36  	sender.AppendResponse(resp)
    37  	return sender
    38  }
    39  
    40  func (s *OAuthConfigSuite) TestOAuthConfig(c *gc.C) {
    41  	client := subscriptions.Client{subscriptions.NewWithBaseURI("https://testing.invalid")}
    42  	client.Sender = oauthConfigSender()
    43  	sdkCtx := context.Background()
    44  	cfg, tenantId, err := azureauth.OAuthConfig(sdkCtx, client, "https://testing.invalid", "subscription-id")
    45  	c.Assert(err, jc.ErrorIsNil)
    46  	c.Assert(tenantId, gc.Equals, fakeTenantId)
    47  
    48  	mustParseURL := func(s string) url.URL {
    49  		u, err := url.Parse(s)
    50  		c.Assert(err, jc.ErrorIsNil)
    51  		return *u
    52  	}
    53  
    54  	baseURL := "https://testing.invalid/11111111-1111-1111-1111-111111111111"
    55  	expectedCfg := &adal.OAuthConfig{
    56  		AuthorityEndpoint:  mustParseURL(baseURL),
    57  		AuthorizeEndpoint:  mustParseURL(baseURL + "/oauth2/authorize?api-version=1.0"),
    58  		TokenEndpoint:      mustParseURL(baseURL + "/oauth2/token?api-version=1.0"),
    59  		DeviceCodeEndpoint: mustParseURL(baseURL + "/oauth2/devicecode?api-version=1.0"),
    60  	}
    61  	c.Assert(cfg, jc.DeepEquals, expectedCfg)
    62  }