github.com/loafoe/cli@v7.1.0+incompatible/command/v7/reset_org_default_isolation_segment_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/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    11  	"code.cloudfoundry.org/cli/resources"
    12  	"code.cloudfoundry.org/cli/util/configv3"
    13  	"code.cloudfoundry.org/cli/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("reset-org-default-isolation-segment Command", func() {
    20  	var (
    21  		cmd             ResetOrgDefaultIsolationSegmentCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v7fakes.FakeActor
    26  		binaryName      string
    27  		executeErr      error
    28  		orgName         string
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v7fakes.FakeActor)
    36  
    37  		cmd = ResetOrgDefaultIsolationSegmentCommand{
    38  			BaseCommand: BaseCommand{
    39  				UI:          testUI,
    40  				Config:      fakeConfig,
    41  				SharedActor: fakeSharedActor,
    42  				Actor:       fakeActor,
    43  			},
    44  		}
    45  
    46  		binaryName = "faceman"
    47  		fakeConfig.BinaryNameReturns(binaryName)
    48  		orgName = "some-org"
    49  
    50  		cmd.RequiredArgs.OrgName = orgName
    51  	})
    52  
    53  	JustBeforeEach(func() {
    54  		executeErr = cmd.Execute(nil)
    55  	})
    56  
    57  	When("checking target fails", func() {
    58  		BeforeEach(func() {
    59  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    60  		})
    61  
    62  		It("returns an error", func() {
    63  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    64  
    65  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    66  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    67  			Expect(checkTargetedOrg).To(BeTrue())
    68  			Expect(checkTargetedSpace).To(BeFalse())
    69  		})
    70  	})
    71  
    72  	When("checking file succeeds", func() {
    73  		BeforeEach(func() {
    74  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    75  				Name: orgName,
    76  				GUID: "some-org-guid",
    77  			})
    78  		})
    79  
    80  		When("the user is not logged in", func() {
    81  			var expectedErr error
    82  
    83  			BeforeEach(func() {
    84  				expectedErr = errors.New("some current user error")
    85  				fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    86  			})
    87  
    88  			It("return an error", func() {
    89  				Expect(executeErr).To(Equal(expectedErr))
    90  
    91  				Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
    92  			})
    93  		})
    94  
    95  		When("the user is logged in", func() {
    96  			BeforeEach(func() {
    97  				fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    98  			})
    99  
   100  			When("the org lookup is unsuccessful", func() {
   101  				BeforeEach(func() {
   102  					fakeActor.GetOrganizationByNameReturns(resources.Organization{}, v7action.Warnings{"warning-1", "warning-2"}, actionerror.OrganizationNotFoundError{Name: orgName})
   103  				})
   104  
   105  				It("returns the warnings and error", func() {
   106  					Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: orgName}))
   107  					Expect(testUI.Err).To(Say("warning-1"))
   108  					Expect(testUI.Err).To(Say("warning-2"))
   109  				})
   110  			})
   111  
   112  			When("the org lookup is successful", func() {
   113  				BeforeEach(func() {
   114  					fakeActor.GetOrganizationByNameReturns(resources.Organization{
   115  						Name: orgName,
   116  						GUID: "some-org-guid",
   117  					}, v7action.Warnings{"warning-1", "warning-2"}, nil)
   118  				})
   119  
   120  				When("the reset succeeds", func() {
   121  					BeforeEach(func() {
   122  						fakeActor.ResetOrganizationDefaultIsolationSegmentReturns(v7action.Warnings{"warning-3", "warning-4"}, nil)
   123  					})
   124  
   125  					It("displays the header and okay", func() {
   126  						Expect(executeErr).ToNot(HaveOccurred())
   127  
   128  						Expect(testUI.Out).To(Say("Resetting default isolation segment of org %s as banana...", orgName))
   129  
   130  						Expect(testUI.Out).To(Say("OK\n\n"))
   131  
   132  						Expect(testUI.Err).To(Say("warning-1"))
   133  						Expect(testUI.Err).To(Say("warning-2"))
   134  						Expect(testUI.Err).To(Say("warning-3"))
   135  						Expect(testUI.Err).To(Say("warning-4"))
   136  
   137  						Expect(testUI.Out).To(Say("TIP: Restart applications in spaces without assigned isolation segments to move them to the platform default."))
   138  
   139  						Expect(fakeActor.ResetOrganizationDefaultIsolationSegmentCallCount()).To(Equal(1))
   140  						orgGUID := fakeActor.ResetOrganizationDefaultIsolationSegmentArgsForCall(0)
   141  						Expect(orgGUID).To(Equal("some-org-guid"))
   142  					})
   143  				})
   144  
   145  				When("the reset errors", func() {
   146  					var expectedErr error
   147  					BeforeEach(func() {
   148  						expectedErr = errors.New("some error")
   149  						fakeActor.ResetOrganizationDefaultIsolationSegmentReturns(v7action.Warnings{"warning-3", "warning-4"}, expectedErr)
   150  					})
   151  
   152  					It("returns the warnings and error", func() {
   153  						Expect(executeErr).To(MatchError(expectedErr))
   154  
   155  						Expect(testUI.Out).To(Say("Resetting default isolation segment of org %s as banana...", orgName))
   156  						Expect(testUI.Err).To(Say("warning-1"))
   157  						Expect(testUI.Err).To(Say("warning-2"))
   158  						Expect(testUI.Err).To(Say("warning-3"))
   159  						Expect(testUI.Err).To(Say("warning-4"))
   160  					})
   161  				})
   162  			})
   163  		})
   164  	})
   165  })