github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/azure/internal/azureauth/discovery_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/mocks" 12 "github.com/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/provider/azure/internal/azureauth" 17 ) 18 19 type DiscoverySuite struct { 20 testing.IsolationSuite 21 } 22 23 var _ = gc.Suite(&DiscoverySuite{}) 24 25 func (*DiscoverySuite) TestDiscoverAuthorizationURI(c *gc.C) { 26 sender := mocks.NewSender() 27 resp := mocks.NewResponseWithStatus("", http.StatusUnauthorized) 28 mocks.SetResponseHeaderValues(resp, "WWW-Authenticate", []string{ 29 `foo bar authorization_uri="https://testing.invalid/meep" baz`, 30 }) 31 sender.AppendResponse(resp) 32 33 client := subscriptions.NewClient() 34 client.Sender = sender 35 authURI, err := azureauth.DiscoverAuthorizationURI(client, "subscription_id") 36 c.Assert(err, jc.ErrorIsNil) 37 c.Assert(authURI, jc.DeepEquals, &url.URL{ 38 Scheme: "https", 39 Host: "testing.invalid", 40 Path: "/meep", 41 }) 42 } 43 44 func (*DiscoverySuite) TestDiscoverAuthorizationURIMissingHeader(c *gc.C) { 45 sender := mocks.NewSender() 46 resp := mocks.NewResponseWithStatus("", http.StatusUnauthorized) 47 sender.AppendResponse(resp) 48 49 client := subscriptions.NewClient() 50 client.Sender = sender 51 _, err := azureauth.DiscoverAuthorizationURI(client, "subscription_id") 52 c.Assert(err, gc.ErrorMatches, `WWW-Authenticate header not found`) 53 } 54 55 func (*DiscoverySuite) TestDiscoverAuthorizationURIHeaderMismatch(c *gc.C) { 56 sender := mocks.NewSender() 57 resp := mocks.NewResponseWithStatus("", http.StatusUnauthorized) 58 mocks.SetResponseHeaderValues(resp, "WWW-Authenticate", []string{`foo bar baz`}) 59 sender.AppendResponse(resp) 60 61 client := subscriptions.NewClient() 62 client.Sender = sender 63 _, err := azureauth.DiscoverAuthorizationURI(client, "subscription_id") 64 c.Assert(err, gc.ErrorMatches, `authorization_uri not found in WWW-Authenticate header \("foo bar baz"\)`) 65 } 66 67 func (*DiscoverySuite) TestDiscoverAuthorizationURIUnexpectedSuccess(c *gc.C) { 68 sender := mocks.NewSender() 69 resp := mocks.NewResponseWithStatus("", http.StatusOK) 70 sender.AppendResponse(resp) 71 72 client := subscriptions.NewClient() 73 client.Sender = sender 74 _, err := azureauth.DiscoverAuthorizationURI(client, "subscription_id") 75 c.Assert(err, gc.ErrorMatches, "expected unauthorized error response") 76 } 77 78 func (*DiscoverySuite) TestDiscoverAuthorizationURIUnexpectedStatusCode(c *gc.C) { 79 sender := mocks.NewSender() 80 resp := mocks.NewResponseWithStatus("", http.StatusNotFound) 81 sender.AppendResponse(resp) 82 83 client := subscriptions.NewClient() 84 client.Sender = sender 85 _, err := azureauth.DiscoverAuthorizationURI(client, "subscription_id") 86 c.Assert(err, gc.ErrorMatches, "expected unauthorized error response, got 404: .*") 87 } 88 89 func (*DiscoverySuite) TestAuthorizationURITenantID(c *gc.C) { 90 tenantId, err := azureauth.AuthorizationURITenantID(&url.URL{Path: "/3671f5a9-c0d0-472b-a80c-48135cf5a9f1"}) 91 c.Assert(err, jc.ErrorIsNil) 92 c.Assert(tenantId, gc.Equals, "3671f5a9-c0d0-472b-a80c-48135cf5a9f1") 93 } 94 95 func (*DiscoverySuite) TestAuthorizationURITenantIDError(c *gc.C) { 96 url, err := url.Parse("https://testing.invalid/foo") 97 c.Assert(err, jc.ErrorIsNil) 98 _, err = azureauth.AuthorizationURITenantID(url) 99 c.Assert(err, gc.ErrorMatches, `authorization_uri "https://testing.invalid/foo" not valid`) 100 }