github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/configuration/coreconfig/api_config_refresher_test.go (about)

     1  package coreconfig_test
     2  
     3  import (
     4  	. "github.com/cloudfoundry/cli/cf/configuration/coreconfig"
     5  	"github.com/cloudfoundry/cli/testhelpers/configuration"
     6  
     7  	"github.com/cloudfoundry/cli/cf/configuration/coreconfig/coreconfigfakes"
     8  	"github.com/cloudfoundry/cli/cf/i18n"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("APIConfigRefresher", func() {
    14  	Describe("LoggregatorEndpoint", func() {
    15  		r := APIConfigRefresher{}
    16  
    17  		var (
    18  			ccInfo *CCInfo
    19  		)
    20  
    21  		Context("when the loggregator endpoint is specified in the CCInfo", func() {
    22  			BeforeEach(func() {
    23  				ccInfo = &CCInfo{
    24  					LoggregatorEndpoint: "some-endpoint",
    25  				}
    26  			})
    27  
    28  			It("returns the endpoint from the CCInfo", func() {
    29  				endpoint := r.LoggregatorEndpoint(ccInfo, "a-url-that-doesn't-matter")
    30  
    31  				Expect(endpoint).To(Equal("some-endpoint"))
    32  			})
    33  		})
    34  
    35  		Context("when the loggregator endpoint is not specified in the CCInfo", func() {
    36  			BeforeEach(func() {
    37  				ccInfo = &CCInfo{
    38  					LoggregatorEndpoint: "",
    39  				}
    40  			})
    41  
    42  			It("extrapolates the loggregator URL based on the API URL (SSL API)", func() {
    43  				endpoint := r.LoggregatorEndpoint(ccInfo, "https://127.0.0.1:443")
    44  				Expect(endpoint).To(Equal("wss://loggregator.0.0.1:443"))
    45  			})
    46  
    47  			It("extrapolates the loggregator URL if there is a trailing slash", func() {
    48  				endpoint := r.LoggregatorEndpoint(ccInfo, "https://127.0.0.1:443/")
    49  				Expect(endpoint).To(Equal("wss://loggregator.0.0.1:443"))
    50  			})
    51  
    52  			It("extrapolates the loggregator URL based on the API URL (non-SSL API)", func() {
    53  				endpoint := r.LoggregatorEndpoint(ccInfo, "http://127.0.0.1:80")
    54  				Expect(endpoint).To(Equal("ws://loggregator.0.0.1:80"))
    55  			})
    56  		})
    57  	})
    58  	Describe("Refresh", func() {
    59  		BeforeEach(func() {
    60  			config := configuration.NewRepositoryWithDefaults()
    61  			i18n.T = i18n.Init(config)
    62  		})
    63  
    64  		Context("when the cloud controller returns an insecure api endpoint", func() {
    65  			var (
    66  				r            APIConfigRefresher
    67  				ccInfo       *CCInfo
    68  				endpointRepo *coreconfigfakes.FakeEndpointRepository
    69  			)
    70  
    71  			BeforeEach(func() {
    72  				ccInfo = &CCInfo{
    73  					LoggregatorEndpoint: "some-endpoint",
    74  				}
    75  				endpointRepo = new(coreconfigfakes.FakeEndpointRepository)
    76  
    77  				r = APIConfigRefresher{
    78  					EndpointRepo: endpointRepo,
    79  					Config:       new(coreconfigfakes.FakeReadWriter),
    80  					Endpoint:     "api.some.endpoint.com",
    81  				}
    82  			})
    83  
    84  			It("gives a warning", func() {
    85  				endpointRepo.GetCCInfoReturns(ccInfo, "api.some.endpoint.com", nil)
    86  				warning, err := r.Refresh()
    87  				Expect(err).NotTo(HaveOccurred())
    88  				Expect(warning.Warn()).To(Equal("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended\n"))
    89  			})
    90  		})
    91  
    92  		Context("when the cloud controller returns a secure api endpoint", func() {
    93  			var (
    94  				r            APIConfigRefresher
    95  				ccInfo       *CCInfo
    96  				endpointRepo *coreconfigfakes.FakeEndpointRepository
    97  			)
    98  
    99  			BeforeEach(func() {
   100  				ccInfo = &CCInfo{
   101  					LoggregatorEndpoint: "some-endpoint",
   102  				}
   103  				endpointRepo = new(coreconfigfakes.FakeEndpointRepository)
   104  
   105  				r = APIConfigRefresher{
   106  					EndpointRepo: endpointRepo,
   107  					Config:       new(coreconfigfakes.FakeReadWriter),
   108  					Endpoint:     "api.some.endpoint.com",
   109  				}
   110  			})
   111  
   112  			It("gives a warning", func() {
   113  				endpointRepo.GetCCInfoReturns(ccInfo, "https://api.some.endpoint.com", nil)
   114  				warning, err := r.Refresh()
   115  				Expect(err).NotTo(HaveOccurred())
   116  				Expect(warning).To(BeNil())
   117  			})
   118  		})
   119  	})
   120  })