code.cloudfoundry.org/cli@v7.1.0+incompatible/actor/v2action/target_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/actor/v2action"
     5  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("Targeting", func() {
    12  	var (
    13  		actor             *Actor
    14  		skipSSLValidation bool
    15  
    16  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    17  		fakeConfig                *v2actionfakes.FakeConfig
    18  		settings                  TargetSettings
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    23  		fakeConfig = new(v2actionfakes.FakeConfig)
    24  		actor = NewActor(fakeCloudControllerClient, nil, fakeConfig)
    25  
    26  		settings = TargetSettings{
    27  			SkipSSLValidation: skipSSLValidation,
    28  		}
    29  	})
    30  
    31  	Describe("ClearTarget", func() {
    32  		It("clears all the target information", func() {
    33  			actor.ClearTarget()
    34  			Expect(fakeConfig.SetTargetInformationCallCount()).To(Equal(1))
    35  			targetInfoArgs := fakeConfig.SetTargetInformationArgsForCall(0)
    36  
    37  			Expect(targetInfoArgs.Api).To(BeEmpty())
    38  			Expect(targetInfoArgs.ApiVersion).To(BeEmpty())
    39  			Expect(targetInfoArgs.Auth).To(BeEmpty())
    40  			Expect(targetInfoArgs.MinCLIVersion).To(BeEmpty())
    41  			Expect(targetInfoArgs.Doppler).To(BeEmpty())
    42  			Expect(targetInfoArgs.LogCache).To(BeEmpty())
    43  			Expect(targetInfoArgs.Routing).To(BeEmpty())
    44  			Expect(targetInfoArgs.SkipSSLValidation).To(BeFalse())
    45  		})
    46  
    47  		It("clears all the token information", func() {
    48  			actor.ClearTarget()
    49  
    50  			Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1))
    51  			accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0)
    52  
    53  			Expect(accessToken).To(BeEmpty())
    54  			Expect(refreshToken).To(BeEmpty())
    55  			Expect(sshOAuthClient).To(BeEmpty())
    56  		})
    57  	})
    58  
    59  	Describe("MinCLIVersion", func() {
    60  		var version string
    61  
    62  		BeforeEach(func() {
    63  			fakeCloudControllerClient.MinCLIVersionReturns("10.9.8")
    64  		})
    65  
    66  		JustBeforeEach(func() {
    67  			version = actor.MinCLIVersion()
    68  		})
    69  
    70  		It("returns the version that the API reports", func() {
    71  			Expect(version).To(Equal("10.9.8"))
    72  		})
    73  	})
    74  
    75  	Describe("SetTarget", func() {
    76  		var expectedAPI, expectedAPIVersion, expectedAuth, expectedMinCLIVersion, expectedDoppler, expectedLogCache, expectedRouting string
    77  
    78  		BeforeEach(func() {
    79  			expectedAPI = "https://api.foo.com"
    80  			expectedAPIVersion = "2.59.0"
    81  			expectedAuth = "https://login.foo.com"
    82  			expectedMinCLIVersion = "1.0.0"
    83  			expectedDoppler = "wss://doppler.foo.com"
    84  			expectedLogCache = "https://log-cache.foo.com"
    85  			expectedRouting = "https://api.foo.com/routing"
    86  
    87  			settings.URL = expectedAPI
    88  
    89  			fakeCloudControllerClient.APIReturns(expectedAPI)
    90  			fakeCloudControllerClient.APIVersionReturns(expectedAPIVersion)
    91  			fakeCloudControllerClient.AuthorizationEndpointReturns(expectedAuth)
    92  			fakeCloudControllerClient.MinCLIVersionReturns(expectedMinCLIVersion)
    93  			fakeCloudControllerClient.DopplerEndpointReturns(expectedDoppler)
    94  			fakeCloudControllerClient.LogCacheEndpointReturns(expectedLogCache)
    95  			fakeCloudControllerClient.RoutingEndpointReturns(expectedRouting)
    96  		})
    97  
    98  		It("targets the passed API", func() {
    99  			_, err := actor.SetTarget(settings)
   100  			Expect(err).ToNot(HaveOccurred())
   101  
   102  			Expect(fakeCloudControllerClient.TargetCFCallCount()).To(Equal(1))
   103  			connectionSettings := fakeCloudControllerClient.TargetCFArgsForCall(0)
   104  			Expect(connectionSettings.URL).To(Equal(expectedAPI))
   105  			Expect(connectionSettings.SkipSSLValidation).To(BeFalse())
   106  		})
   107  
   108  		It("sets all the target information", func() {
   109  			_, err := actor.SetTarget(settings)
   110  			Expect(err).ToNot(HaveOccurred())
   111  
   112  			Expect(fakeConfig.SetTargetInformationCallCount()).To(Equal(1))
   113  			targetInfoArgs := fakeConfig.SetTargetInformationArgsForCall(0)
   114  
   115  			Expect(targetInfoArgs.Api).To(Equal(expectedAPI))
   116  			Expect(targetInfoArgs.ApiVersion).To(Equal(expectedAPIVersion))
   117  			Expect(targetInfoArgs.Auth).To(Equal(expectedAuth))
   118  			Expect(targetInfoArgs.MinCLIVersion).To(Equal(expectedMinCLIVersion))
   119  			Expect(targetInfoArgs.Doppler).To(Equal(expectedDoppler))
   120  			Expect(targetInfoArgs.LogCache).To(Equal(expectedLogCache))
   121  			Expect(targetInfoArgs.Routing).To(Equal(expectedRouting))
   122  			Expect(targetInfoArgs.SkipSSLValidation).To(Equal(skipSSLValidation))
   123  		})
   124  
   125  		It("clears all the token information", func() {
   126  			_, err := actor.SetTarget(settings)
   127  			Expect(err).ToNot(HaveOccurred())
   128  
   129  			Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1))
   130  			accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0)
   131  
   132  			Expect(accessToken).To(BeEmpty())
   133  			Expect(refreshToken).To(BeEmpty())
   134  			Expect(sshOAuthClient).To(BeEmpty())
   135  		})
   136  
   137  		When("setting the same API and skip SSL configuration", func() {
   138  			var APIURL string
   139  
   140  			BeforeEach(func() {
   141  				APIURL = "https://some-api.com"
   142  				settings.URL = APIURL
   143  				fakeConfig.TargetReturns(APIURL)
   144  				fakeConfig.SkipSSLValidationReturns(skipSSLValidation)
   145  			})
   146  
   147  			It("does not make any API calls", func() {
   148  				warnings, err := actor.SetTarget(settings)
   149  				Expect(err).NotTo(HaveOccurred())
   150  				Expect(warnings).To(BeNil())
   151  
   152  				Expect(fakeCloudControllerClient.TargetCFCallCount()).To(BeZero())
   153  			})
   154  		})
   155  	})
   156  
   157  })