github.com/orange-cloudfoundry/cli@v7.1.0+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  	. "code.cloudfoundry.org/cli/api/uaa"
    12  	"code.cloudfoundry.org/cli/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  )
    32  
    33  var _ = SynchronizedBeforeSuite(func() []byte {
    34  	return []byte{}
    35  }, func(data []byte) {
    36  	server = NewTLSServer()
    37  	uaaServer = NewTLSServer()
    38  
    39  	testAuthURL, err := url.Parse(server.URL())
    40  	Expect(err).ToNot(HaveOccurred())
    41  	TestAuthorizationResource = testAuthURL.Host
    42  
    43  	testUAAURL, err := url.Parse(uaaServer.URL())
    44  	Expect(err).ToNot(HaveOccurred())
    45  	TestUAAResource = testUAAURL.Host
    46  
    47  	// Suppresses ginkgo server logs
    48  	server.HTTPTestServer.Config.ErrorLog = log.New(&bytes.Buffer{}, "", 0)
    49  })
    50  
    51  var _ = SynchronizedAfterSuite(func() {
    52  	server.Close()
    53  }, func() {})
    54  
    55  var _ = BeforeEach(func() {
    56  	server.Reset()
    57  })
    58  
    59  func NewTestConfig() *uaafakes.FakeConfig {
    60  	config := new(uaafakes.FakeConfig)
    61  	config.BinaryNameReturns("CF CLI UAA API Test")
    62  	config.BinaryVersionReturns("Unknown")
    63  	config.UAAOAuthClientReturns("client-id")
    64  	config.UAAOAuthClientSecretReturns("client-secret")
    65  	config.SkipSSLValidationReturns(true)
    66  	return config
    67  }
    68  
    69  func NewTestUAAClientAndStore(config Config) *Client {
    70  	SetupBootstrapResponse()
    71  
    72  	client := NewClient(config)
    73  
    74  	// the 'uaaServer' is discovered via the bootstrapping when we hit the /login
    75  	// endpoint on 'server'
    76  	err := client.SetupResources(server.URL())
    77  	Expect(err).ToNot(HaveOccurred())
    78  
    79  	return client
    80  }
    81  
    82  func SetupBootstrapResponse() {
    83  	response := strings.Replace(`{
    84  				"links": {
    85  					"uaa": "SERVER_URL"
    86  				}
    87  			}`, "SERVER_URL", uaaServer.URL(), -1)
    88  
    89  	server.AppendHandlers(
    90  		CombineHandlers(
    91  			VerifyRequest(http.MethodGet, "/login"),
    92  			RespondWith(http.StatusOK, response),
    93  		),
    94  	)
    95  }
    96  
    97  func verifyRequestHost(host string) http.HandlerFunc {
    98  	return func(_ http.ResponseWriter, req *http.Request) {
    99  		Expect(req.Host).To(Equal(host))
   100  	}
   101  }