github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/v2action/target_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	. "github.com/liamawhite/cli-with-i18n/actor/v2action"
     5  	"github.com/liamawhite/cli-with-i18n/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, nil)
    25  
    26  		settings = TargetSettings{
    27  			SkipSSLValidation: skipSSLValidation,
    28  		}
    29  	})
    30  
    31  	Describe("SetTarget", func() {
    32  		var expectedAPI, expectedAPIVersion, expectedAuth, expectedMinCLIVersion, expectedDoppler, expectedRouting string
    33  
    34  		BeforeEach(func() {
    35  			expectedAPI = "https://api.foo.com"
    36  			expectedAPIVersion = "2.59.0"
    37  			expectedAuth = "https://login.foo.com"
    38  			expectedMinCLIVersion = "1.0.0"
    39  			expectedDoppler = "wss://doppler.foo.com"
    40  			expectedRouting = "https://api.foo.com/routing"
    41  
    42  			settings.URL = expectedAPI
    43  
    44  			fakeCloudControllerClient.APIReturns(expectedAPI)
    45  			fakeCloudControllerClient.APIVersionReturns(expectedAPIVersion)
    46  			fakeCloudControllerClient.AuthorizationEndpointReturns(expectedAuth)
    47  			fakeCloudControllerClient.MinCLIVersionReturns(expectedMinCLIVersion)
    48  			fakeCloudControllerClient.DopplerEndpointReturns(expectedDoppler)
    49  			fakeCloudControllerClient.RoutingEndpointReturns(expectedRouting)
    50  		})
    51  
    52  		It("targets the passed API", func() {
    53  			_, err := actor.SetTarget(fakeConfig, settings)
    54  			Expect(err).ToNot(HaveOccurred())
    55  
    56  			Expect(fakeCloudControllerClient.TargetCFCallCount()).To(Equal(1))
    57  			connectionSettings := fakeCloudControllerClient.TargetCFArgsForCall(0)
    58  			Expect(connectionSettings.URL).To(Equal(expectedAPI))
    59  			Expect(connectionSettings.SkipSSLValidation).To(BeFalse())
    60  		})
    61  
    62  		It("sets all the target information", func() {
    63  			_, err := actor.SetTarget(fakeConfig, settings)
    64  			Expect(err).ToNot(HaveOccurred())
    65  
    66  			Expect(fakeConfig.SetTargetInformationCallCount()).To(Equal(1))
    67  			api, apiVersion, auth, minCLIVersion, doppler, routing, sslDisabled := fakeConfig.SetTargetInformationArgsForCall(0)
    68  
    69  			Expect(api).To(Equal(expectedAPI))
    70  			Expect(apiVersion).To(Equal(expectedAPIVersion))
    71  			Expect(auth).To(Equal(expectedAuth))
    72  			Expect(minCLIVersion).To(Equal(expectedMinCLIVersion))
    73  			Expect(doppler).To(Equal(expectedDoppler))
    74  			Expect(routing).To(Equal(expectedRouting))
    75  			Expect(sslDisabled).To(Equal(skipSSLValidation))
    76  		})
    77  
    78  		It("clears all the token information", func() {
    79  			_, err := actor.SetTarget(fakeConfig, settings)
    80  			Expect(err).ToNot(HaveOccurred())
    81  
    82  			Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1))
    83  			accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0)
    84  
    85  			Expect(accessToken).To(BeEmpty())
    86  			Expect(refreshToken).To(BeEmpty())
    87  			Expect(sshOAuthClient).To(BeEmpty())
    88  		})
    89  
    90  		Context("when setting the same API and skip SSL configuration", func() {
    91  			var APIURL string
    92  
    93  			BeforeEach(func() {
    94  				APIURL = "https://some-api.com"
    95  				settings.URL = APIURL
    96  				fakeConfig.TargetReturns(APIURL)
    97  				fakeConfig.SkipSSLValidationReturns(skipSSLValidation)
    98  			})
    99  
   100  			It("does not make any API calls", func() {
   101  				warnings, err := actor.SetTarget(fakeConfig, settings)
   102  				Expect(err).NotTo(HaveOccurred())
   103  				Expect(warnings).To(BeNil())
   104  
   105  				Expect(fakeCloudControllerClient.TargetCFCallCount()).To(BeZero())
   106  			})
   107  		})
   108  	})
   109  
   110  	Describe("ClearTarget", func() {
   111  		It("clears all the target information", func() {
   112  			actor.ClearTarget(fakeConfig)
   113  
   114  			Expect(fakeConfig.SetTargetInformationCallCount()).To(Equal(1))
   115  			api, apiVersion, auth, minCLIVersion, doppler, routing, sslDisabled := fakeConfig.SetTargetInformationArgsForCall(0)
   116  
   117  			Expect(api).To(BeEmpty())
   118  			Expect(apiVersion).To(BeEmpty())
   119  			Expect(auth).To(BeEmpty())
   120  			Expect(minCLIVersion).To(BeEmpty())
   121  			Expect(doppler).To(BeEmpty())
   122  			Expect(routing).To(BeEmpty())
   123  			Expect(sslDisabled).To(BeFalse())
   124  		})
   125  
   126  		It("clears all the token information", func() {
   127  			actor.ClearTarget(fakeConfig)
   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  
   138  	Describe("ClearOrganizationAndSpace", func() {
   139  		It("clears all organization and space information", func() {
   140  			actor.ClearOrganizationAndSpace(fakeConfig)
   141  
   142  			Expect(fakeConfig.UnsetOrganizationInformationCallCount()).To(Equal(1))
   143  			Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1))
   144  		})
   145  	})
   146  })