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

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "code.cloudfoundry.org/cli/actor/v7action"
     7  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Targeting", func() {
    15  	var (
    16  		actor                     *Actor
    17  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    18  		fakeConfig                *v7actionfakes.FakeConfig
    19  
    20  		settings          TargetSettings
    21  		skipSSLValidation bool
    22  		targetedURL       string
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		actor, fakeCloudControllerClient, fakeConfig, _, _, _, _ = NewTestActor()
    27  
    28  	})
    29  
    30  	Describe("SetTarget", func() {
    31  		var (
    32  			expectedAPI        string
    33  			expectedAPIVersion string
    34  			expectedAuth       string
    35  			expectedDoppler    string
    36  			expectedLogCache   string
    37  			expectedRouting    string
    38  
    39  			err      error
    40  			warnings Warnings
    41  		)
    42  
    43  		BeforeEach(func() {
    44  			expectedAPI = "https://api.foo.com"
    45  			expectedAPIVersion = "3.81.0"
    46  			expectedAuth = "https://login.foo.com"
    47  			expectedDoppler = "wss://doppler.foo.com"
    48  			expectedLogCache = "https://log-cache.foo.com"
    49  			expectedRouting = "https://api.foo.com/routing"
    50  
    51  			skipSSLValidation = true
    52  			targetedURL = expectedAPI
    53  			var meta struct {
    54  				Version            string `json:"version"`
    55  				HostKeyFingerprint string `json:"host_key_fingerprint"`
    56  				OAuthClient        string `json:"oath_client"`
    57  			}
    58  			meta.Version = expectedAPIVersion
    59  
    60  			rootResponse := ccv3.Info{
    61  				Links: ccv3.InfoLinks{
    62  					CCV3: ccv3.APILink{
    63  						Meta: meta,
    64  					},
    65  					Logging: ccv3.APILink{
    66  						HREF: expectedDoppler,
    67  					},
    68  					LogCache: ccv3.APILink{
    69  						HREF: expectedLogCache,
    70  					},
    71  					Routing: ccv3.APILink{
    72  						HREF: expectedRouting,
    73  					},
    74  					Login: ccv3.APILink{
    75  						HREF: expectedAuth,
    76  					},
    77  				},
    78  			}
    79  			fakeCloudControllerClient.TargetCFReturns(rootResponse, ccv3.Warnings{"info-warning"}, nil)
    80  		})
    81  
    82  		JustBeforeEach(func() {
    83  			settings = TargetSettings{
    84  				SkipSSLValidation: skipSSLValidation,
    85  				URL:               targetedURL,
    86  			}
    87  			warnings, err = actor.SetTarget(settings)
    88  		})
    89  
    90  		It("targets CF with the expected arguments", func() {
    91  			Expect(fakeCloudControllerClient.TargetCFCallCount()).To(Equal(1))
    92  			connectionSettings := fakeCloudControllerClient.TargetCFArgsForCall(0)
    93  			Expect(connectionSettings.URL).To(Equal(expectedAPI))
    94  			Expect(connectionSettings.SkipSSLValidation).To(BeTrue())
    95  		})
    96  
    97  		When("targeting CF fails", func() {
    98  			BeforeEach(func() {
    99  				fakeCloudControllerClient.TargetCFReturns(ccv3.Info{}, ccv3.Warnings{"info-warning"}, errors.New("target-error"))
   100  			})
   101  
   102  			It("returns an error and all warnings", func() {
   103  				Expect(err).To(HaveOccurred())
   104  				Expect(err).To(MatchError("target-error"))
   105  				Expect(warnings).To(ConsistOf(Warnings{"info-warning"}))
   106  
   107  				Expect(fakeCloudControllerClient.TargetCFCallCount()).To(Equal(1))
   108  			})
   109  		})
   110  
   111  		It("sets all the target information", func() {
   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.Doppler).To(Equal(expectedDoppler))
   119  			Expect(targetInfoArgs.LogCache).To(Equal(expectedLogCache))
   120  			Expect(targetInfoArgs.Routing).To(Equal(expectedRouting))
   121  			Expect(targetInfoArgs.SkipSSLValidation).To(Equal(skipSSLValidation))
   122  		})
   123  
   124  		It("clears all the token information", func() {
   125  			Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1))
   126  			accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0)
   127  
   128  			Expect(accessToken).To(BeEmpty())
   129  			Expect(refreshToken).To(BeEmpty())
   130  			Expect(sshOAuthClient).To(BeEmpty())
   131  		})
   132  
   133  		It("succeeds and returns all warnings", func() {
   134  			Expect(err).ToNot(HaveOccurred())
   135  			Expect(warnings).To(ConsistOf(Warnings{"info-warning"}))
   136  		})
   137  	})
   138  
   139  	Describe("ClearTarget", func() {
   140  		It("clears all the target information", func() {
   141  			actor.ClearTarget()
   142  
   143  			Expect(fakeConfig.SetTargetInformationCallCount()).To(Equal(1))
   144  			targetInfoArgs := fakeConfig.SetTargetInformationArgsForCall(0)
   145  
   146  			Expect(targetInfoArgs.Api).To(BeEmpty())
   147  			Expect(targetInfoArgs.ApiVersion).To(BeEmpty())
   148  			Expect(targetInfoArgs.Auth).To(BeEmpty())
   149  			Expect(targetInfoArgs.MinCLIVersion).To(BeEmpty())
   150  			Expect(targetInfoArgs.Doppler).To(BeEmpty())
   151  			Expect(targetInfoArgs.LogCache).To(BeEmpty())
   152  			Expect(targetInfoArgs.Routing).To(BeEmpty())
   153  			Expect(targetInfoArgs.SkipSSLValidation).To(BeFalse())
   154  		})
   155  
   156  		It("clears all the token information", func() {
   157  			actor.ClearTarget()
   158  
   159  			Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1))
   160  			accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0)
   161  
   162  			Expect(accessToken).To(BeEmpty())
   163  			Expect(refreshToken).To(BeEmpty())
   164  			Expect(sshOAuthClient).To(BeEmpty())
   165  		})
   166  	})
   167  })