github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/api/uaa/uaa_suite_test.go (about)

     1  package uaa_test
     2  
     3  import (
     4  	"bytes"
     5  	"log"
     6  	"net/http"
     7  	"net/url"
     8  	"strings"
     9  	"testing"
    10  
    11  	. "github.com/liamawhite/cli-with-i18n/api/uaa"
    12  	"github.com/liamawhite/cli-with-i18n/api/uaa/uaafakes"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/ghttp"
    16  )
    17  
    18  func TestUaa(t *testing.T) {
    19  	RegisterFailHandler(Fail)
    20  	RunSpecs(t, "UAA Suite")
    21  }
    22  
    23  var (
    24  	// we create two servers in order to test that requests using different
    25  	// resources are going to the correct server
    26  	server    *Server
    27  	uaaServer *Server
    28  
    29  	TestAuthorizationResource string
    30  	TestUAAResource           string
    31  	TestSuiteFakeStore        *uaafakes.FakeUAAEndpointStore
    32  )
    33  
    34  var _ = SynchronizedBeforeSuite(func() []byte {
    35  	return []byte{}
    36  }, func(data []byte) {
    37  	server = NewTLSServer()
    38  	uaaServer = NewTLSServer()
    39  
    40  	testAuthURL, err := url.Parse(server.URL())
    41  	Expect(err).ToNot(HaveOccurred())
    42  	TestAuthorizationResource = testAuthURL.Host
    43  
    44  	testUAAURL, err := url.Parse(uaaServer.URL())
    45  	Expect(err).ToNot(HaveOccurred())
    46  	TestUAAResource = testUAAURL.Host
    47  
    48  	// Suppresses ginkgo server logs
    49  	server.HTTPTestServer.Config.ErrorLog = log.New(&bytes.Buffer{}, "", 0)
    50  })
    51  
    52  var _ = SynchronizedAfterSuite(func() {
    53  	server.Close()
    54  }, func() {})
    55  
    56  var _ = BeforeEach(func() {
    57  	server.Reset()
    58  })
    59  
    60  func NewTestUAAClientAndStore() *Client {
    61  	SetupBootstrapResponse()
    62  
    63  	client := NewClient(Config{
    64  		AppName:           "CF CLI UAA API Test",
    65  		AppVersion:        "Unknown",
    66  		ClientID:          "client-id",
    67  		ClientSecret:      "client-secret",
    68  		SkipSSLValidation: true,
    69  	})
    70  
    71  	// the 'uaaServer' is discovered via the bootstrapping when we hit the /login
    72  	// endpoint on 'server'
    73  	TestSuiteFakeStore = new(uaafakes.FakeUAAEndpointStore)
    74  	err := client.SetupResources(TestSuiteFakeStore, server.URL())
    75  	Expect(err).ToNot(HaveOccurred())
    76  
    77  	return client
    78  }
    79  
    80  func SetupBootstrapResponse() {
    81  	response := strings.Replace(`{
    82  				"links": {
    83  					"uaa": "SERVER_URL"
    84  				}
    85  			}`, "SERVER_URL", uaaServer.URL(), -1)
    86  
    87  	server.AppendHandlers(
    88  		CombineHandlers(
    89  			VerifyRequest(http.MethodGet, "/login"),
    90  			RespondWith(http.StatusOK, response),
    91  		),
    92  	)
    93  }
    94  
    95  func verifyRequestHost(host string) http.HandlerFunc {
    96  	return func(_ http.ResponseWriter, req *http.Request) {
    97  		Expect(req.Host).To(Equal(host))
    98  	}
    99  }