github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v7/create_route_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	. "code.cloudfoundry.org/cli/command/v7"
    11  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    12  	"code.cloudfoundry.org/cli/util/configv3"
    13  	"code.cloudfoundry.org/cli/util/ui"
    14  
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("create-route Command", func() {
    21  	var (
    22  		cmd             CreateRouteCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v7fakes.FakeCreateRouteActor
    27  
    28  		executeErr error
    29  
    30  		binaryName string
    31  		domainName string
    32  		spaceName  string
    33  		orgName    string
    34  		hostname   string
    35  		path       string
    36  	)
    37  
    38  	BeforeEach(func() {
    39  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    40  		fakeConfig = new(commandfakes.FakeConfig)
    41  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    42  		fakeActor = new(v7fakes.FakeCreateRouteActor)
    43  
    44  		domainName = "example.com"
    45  		spaceName = "space"
    46  		orgName = "org"
    47  		hostname = ""
    48  		path = ""
    49  
    50  		binaryName = "faceman"
    51  		fakeConfig.BinaryNameReturns(binaryName)
    52  	})
    53  
    54  	JustBeforeEach(func() {
    55  		cmd = CreateRouteCommand{
    56  			RequiredArgs: flag.Domain{
    57  				Domain: domainName,
    58  			},
    59  			Hostname:    hostname,
    60  			Path:        path,
    61  			UI:          testUI,
    62  			Config:      fakeConfig,
    63  			SharedActor: fakeSharedActor,
    64  			Actor:       fakeActor,
    65  		}
    66  		executeErr = cmd.Execute(nil)
    67  	})
    68  
    69  	When("the environment is not set up correctly", func() {
    70  		BeforeEach(func() {
    71  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    72  		})
    73  
    74  		It("returns an error", func() {
    75  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    76  
    77  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    78  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    79  			Expect(checkTargetedOrg).To(BeTrue())
    80  			Expect(checkTargetedSpace).To(BeTrue())
    81  		})
    82  	})
    83  
    84  	When("the environment is setup correctly", func() {
    85  		BeforeEach(func() {
    86  			fakeConfig.CurrentUserReturns(configv3.User{Name: "the-user"}, nil)
    87  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    88  				Name: spaceName,
    89  				GUID: "some-space-guid",
    90  			})
    91  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    92  				Name: orgName,
    93  				GUID: "some-org-guid",
    94  			})
    95  		})
    96  
    97  		It("prints text indicating it is creating a route", func() {
    98  			Expect(executeErr).NotTo(HaveOccurred())
    99  			Expect(testUI.Out).To(Say(`Creating route %s for org %s / space %s as the-user\.\.\.`, domainName, orgName, spaceName))
   100  		})
   101  
   102  		When("passing in a hostname", func() {
   103  			BeforeEach(func() {
   104  				hostname = "flan"
   105  			})
   106  
   107  			It("prints text indicating it is creating a route", func() {
   108  				Expect(executeErr).NotTo(HaveOccurred())
   109  				Expect(testUI.Out).To(Say(`Creating route %s\.%s for org %s / space %s as the-user\.\.\.`, hostname, domainName, orgName, spaceName))
   110  			})
   111  		})
   112  
   113  		When("passing in a path without a '/'", func() {
   114  
   115  			BeforeEach(func() {
   116  				path = "lion"
   117  			})
   118  
   119  			It("prints information about the created route ", func() {
   120  				Expect(executeErr).NotTo(HaveOccurred())
   121  				Expect(testUI.Out).To(Say(`Creating route %s/%s for org %s / space %s as the-user\.\.\.`, domainName, path, orgName, spaceName))
   122  				Expect(testUI.Out).To(Say("Route %s/%s has been created.", domainName, path))
   123  				Expect(testUI.Out).To(Say("OK"))
   124  			})
   125  		})
   126  
   127  		When("passing in a path with a '/'", func() {
   128  
   129  			BeforeEach(func() {
   130  				path = "/lion"
   131  			})
   132  
   133  			It("prints information about the created route ", func() {
   134  				Expect(executeErr).NotTo(HaveOccurred())
   135  				Expect(testUI.Out).To(Say(`Creating route %s%s for org %s / space %s as the-user\.\.\.`, domainName, path, orgName, spaceName))
   136  				Expect(testUI.Out).To(Say("Route %s%s has been created.", domainName, path))
   137  				Expect(testUI.Out).To(Say("OK"))
   138  			})
   139  		})
   140  
   141  		When("creating the route errors", func() {
   142  			BeforeEach(func() {
   143  				fakeActor.CreateRouteReturns(v7action.Warnings{"warnings-1", "warnings-2"}, errors.New("err-create-route"))
   144  			})
   145  
   146  			It("returns an error and displays warnings", func() {
   147  				Expect(executeErr).To(MatchError("err-create-route"))
   148  				Expect(testUI.Err).To(Say("warnings-1"))
   149  				Expect(testUI.Err).To(Say("warnings-2"))
   150  			})
   151  		})
   152  
   153  		When("creating the route is successful", func() {
   154  			BeforeEach(func() {
   155  				fakeActor.CreateRouteReturns(v7action.Warnings{"warnings-1", "warnings-2"}, nil)
   156  			})
   157  
   158  			It("prints all warnings, text indicating creation completion, ok and then a tip", func() {
   159  				Expect(executeErr).ToNot(HaveOccurred())
   160  				Expect(testUI.Err).To(Say("warnings-1"))
   161  				Expect(testUI.Err).To(Say("warnings-2"))
   162  				Expect(testUI.Out).To(Say(`Route %s has been created.`, domainName))
   163  				Expect(testUI.Out).To(Say("OK"))
   164  			})
   165  
   166  			It("creates the route", func() {
   167  				Expect(fakeActor.CreateRouteCallCount()).To(Equal(1))
   168  				expectedOrgName, expectedSpaceName, expectedDomainName, expectedHostname, _ := fakeActor.CreateRouteArgsForCall(0)
   169  				Expect(expectedOrgName).To(Equal(orgName))
   170  				Expect(expectedDomainName).To(Equal(domainName))
   171  				Expect(expectedSpaceName).To(Equal(spaceName))
   172  				Expect(expectedHostname).To(Equal(hostname))
   173  			})
   174  
   175  			When("passing in a hostname", func() {
   176  				BeforeEach(func() {
   177  					hostname = "flan"
   178  				})
   179  
   180  				It("prints all warnings, text indicating creation completion, ok and then a tip", func() {
   181  					Expect(executeErr).ToNot(HaveOccurred())
   182  					Expect(testUI.Err).To(Say("warnings-1"))
   183  					Expect(testUI.Err).To(Say("warnings-2"))
   184  					Expect(testUI.Out).To(Say(`Route %s\.%s has been created.`, hostname, domainName))
   185  					Expect(testUI.Out).To(Say("OK"))
   186  				})
   187  
   188  				It("creates the route", func() {
   189  					Expect(fakeActor.CreateRouteCallCount()).To(Equal(1))
   190  					expectedOrgName, expectedSpaceName, expectedDomainName, expectedHostname, _ := fakeActor.CreateRouteArgsForCall(0)
   191  					Expect(expectedOrgName).To(Equal(orgName))
   192  					Expect(expectedDomainName).To(Equal(domainName))
   193  					Expect(expectedSpaceName).To(Equal(spaceName))
   194  					Expect(expectedHostname).To(Equal(hostname))
   195  				})
   196  			})
   197  		})
   198  
   199  		When("the route already exists", func() {
   200  			BeforeEach(func() {
   201  				fakeActor.CreateRouteReturns(v7action.Warnings{"some-warning"}, actionerror.RouteAlreadyExistsError{Err: errors.New("api error for a route that already exists")})
   202  			})
   203  
   204  			It("displays all warnings, that the route already exists, and does not error", func() {
   205  				Expect(executeErr).ToNot(HaveOccurred())
   206  
   207  				Expect(testUI.Err).To(Say("some-warning"))
   208  				Expect(testUI.Out).To(Say(`Creating route %s for org %s / space %s as the-user\.\.\.`, domainName, orgName, spaceName))
   209  				Expect(testUI.Out).To(Say("api error for a route that already exists"))
   210  				Expect(testUI.Out).To(Say("OK"))
   211  			})
   212  		})
   213  	})
   214  })