github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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  		spaceGUID  string
    34  		orgName    string
    35  		hostname   string
    36  		path       string
    37  	)
    38  
    39  	BeforeEach(func() {
    40  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    41  		fakeConfig = new(commandfakes.FakeConfig)
    42  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    43  		fakeActor = new(v7fakes.FakeCreateRouteActor)
    44  
    45  		domainName = "example.com"
    46  		spaceName = "space"
    47  		spaceGUID = "space-guid"
    48  		orgName = "org"
    49  		hostname = ""
    50  		path = ""
    51  
    52  		binaryName = "faceman"
    53  		fakeConfig.BinaryNameReturns(binaryName)
    54  	})
    55  
    56  	JustBeforeEach(func() {
    57  		cmd = CreateRouteCommand{
    58  			RequiredArgs: flag.Domain{
    59  				Domain: domainName,
    60  			},
    61  			Hostname:    hostname,
    62  			Path:        flag.V7RoutePath{Path: path},
    63  			UI:          testUI,
    64  			Config:      fakeConfig,
    65  			SharedActor: fakeSharedActor,
    66  			Actor:       fakeActor,
    67  		}
    68  		executeErr = cmd.Execute(nil)
    69  	})
    70  
    71  	When("the environment is not set up correctly", func() {
    72  		BeforeEach(func() {
    73  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    74  		})
    75  
    76  		It("returns an error", func() {
    77  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    78  
    79  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    80  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    81  			Expect(checkTargetedOrg).To(BeTrue())
    82  			Expect(checkTargetedSpace).To(BeTrue())
    83  		})
    84  	})
    85  
    86  	When("the environment is setup correctly", func() {
    87  		BeforeEach(func() {
    88  			fakeConfig.CurrentUserReturns(configv3.User{Name: "the-user"}, nil)
    89  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    90  				Name: spaceName,
    91  				GUID: spaceGUID,
    92  			})
    93  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    94  				Name: orgName,
    95  				GUID: "some-org-guid",
    96  			})
    97  		})
    98  
    99  		It("prints text indicating it is creating a route", func() {
   100  			Expect(executeErr).NotTo(HaveOccurred())
   101  			Expect(testUI.Out).To(Say(`Creating route %s for org %s / space %s as the-user\.\.\.`, domainName, orgName, spaceName))
   102  		})
   103  
   104  		When("passing in a hostname", func() {
   105  			BeforeEach(func() {
   106  				hostname = "flan"
   107  			})
   108  
   109  			It("prints text indicating it is creating a route", func() {
   110  				Expect(executeErr).NotTo(HaveOccurred())
   111  				Expect(testUI.Out).To(Say(`Creating route %s\.%s for org %s / space %s as the-user\.\.\.`, hostname, domainName, orgName, spaceName))
   112  			})
   113  		})
   114  
   115  		When("passing in a path", func() {
   116  			BeforeEach(func() {
   117  				path = "/lion"
   118  			})
   119  
   120  			It("prints information about the created route ", func() {
   121  				Expect(executeErr).NotTo(HaveOccurred())
   122  				Expect(testUI.Out).To(Say(`Creating route %s%s for org %s / space %s as the-user\.\.\.`, domainName, path, orgName, spaceName))
   123  				Expect(testUI.Out).To(Say("Route %s%s has been created.", domainName, path))
   124  				Expect(testUI.Out).To(Say("OK"))
   125  			})
   126  		})
   127  
   128  		When("creating the route errors", func() {
   129  			BeforeEach(func() {
   130  				fakeActor.CreateRouteReturns(v7action.Route{}, v7action.Warnings{"warnings-1", "warnings-2"}, errors.New("err-create-route"))
   131  			})
   132  
   133  			It("returns an error and displays warnings", func() {
   134  				Expect(executeErr).To(MatchError("err-create-route"))
   135  				Expect(testUI.Err).To(Say("warnings-1"))
   136  				Expect(testUI.Err).To(Say("warnings-2"))
   137  			})
   138  		})
   139  
   140  		When("creating the route is successful", func() {
   141  			BeforeEach(func() {
   142  				fakeActor.CreateRouteReturns(v7action.Route{}, v7action.Warnings{"warnings-1", "warnings-2"}, nil)
   143  			})
   144  
   145  			It("prints all warnings, text indicating creation completion, ok and then a tip", func() {
   146  				Expect(executeErr).ToNot(HaveOccurred())
   147  				Expect(testUI.Err).To(Say("warnings-1"))
   148  				Expect(testUI.Err).To(Say("warnings-2"))
   149  				Expect(testUI.Out).To(Say(`Route %s has been created.`, domainName))
   150  				Expect(testUI.Out).To(Say("OK"))
   151  			})
   152  
   153  			It("creates the route", func() {
   154  				Expect(fakeActor.CreateRouteCallCount()).To(Equal(1))
   155  				expectedSpaceGUID, expectedDomainName, expectedHostname, _ := fakeActor.CreateRouteArgsForCall(0)
   156  				Expect(expectedSpaceGUID).To(Equal(spaceGUID))
   157  				Expect(expectedDomainName).To(Equal(domainName))
   158  				Expect(expectedHostname).To(Equal(hostname))
   159  			})
   160  
   161  			When("passing in a hostname", func() {
   162  				BeforeEach(func() {
   163  					hostname = "flan"
   164  				})
   165  
   166  				It("prints all warnings, text indicating creation completion, ok and then a tip", func() {
   167  					Expect(executeErr).ToNot(HaveOccurred())
   168  					Expect(testUI.Err).To(Say("warnings-1"))
   169  					Expect(testUI.Err).To(Say("warnings-2"))
   170  					Expect(testUI.Out).To(Say(`Route %s\.%s has been created.`, hostname, domainName))
   171  					Expect(testUI.Out).To(Say("OK"))
   172  				})
   173  
   174  				It("creates the route", func() {
   175  					Expect(fakeActor.CreateRouteCallCount()).To(Equal(1))
   176  					expectedSpaceGUID, expectedDomainName, expectedHostname, _ := fakeActor.CreateRouteArgsForCall(0)
   177  					Expect(expectedSpaceGUID).To(Equal(spaceGUID))
   178  					Expect(expectedDomainName).To(Equal(domainName))
   179  					Expect(expectedHostname).To(Equal(hostname))
   180  				})
   181  			})
   182  		})
   183  
   184  		When("the route already exists", func() {
   185  			BeforeEach(func() {
   186  				fakeActor.CreateRouteReturns(v7action.Route{}, v7action.Warnings{"some-warning"}, actionerror.RouteAlreadyExistsError{Err: errors.New("api error for a route that already exists")})
   187  			})
   188  
   189  			It("displays all warnings, that the route already exists, and does not error", func() {
   190  				Expect(executeErr).ToNot(HaveOccurred())
   191  
   192  				Expect(testUI.Err).To(Say("some-warning"))
   193  				Expect(testUI.Err).To(Say("api error for a route that already exists"))
   194  				Expect(testUI.Out).To(Say(`Creating route %s for org %s / space %s as the-user\.\.\.`, domainName, orgName, spaceName))
   195  				Expect(testUI.Out).To(Say("OK"))
   196  			})
   197  		})
   198  	})
   199  })