github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/disable_org_isolation_command_test.go (about)

     1  package v3_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v3action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  	"code.cloudfoundry.org/cli/command/v3"
    12  	"code.cloudfoundry.org/cli/command/v3/v3fakes"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("disable-org-isolation Command", func() {
    21  	var (
    22  		cmd              v3.DisableOrgIsolationCommand
    23  		testUI           *ui.UI
    24  		fakeConfig       *commandfakes.FakeConfig
    25  		fakeSharedActor  *commandfakes.FakeSharedActor
    26  		fakeActor        *v3fakes.FakeDisableOrgIsolationActor
    27  		binaryName       string
    28  		executeErr       error
    29  		isolationSegment string
    30  		org              string
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    35  		fakeConfig = new(commandfakes.FakeConfig)
    36  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    37  		fakeActor = new(v3fakes.FakeDisableOrgIsolationActor)
    38  
    39  		cmd = v3.DisableOrgIsolationCommand{
    40  			UI:          testUI,
    41  			Config:      fakeConfig,
    42  			SharedActor: fakeSharedActor,
    43  			Actor:       fakeActor,
    44  		}
    45  
    46  		binaryName = "faceman"
    47  		fakeConfig.BinaryNameReturns(binaryName)
    48  		org = "org1"
    49  		isolationSegment = "segment1"
    50  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3)
    51  	})
    52  
    53  	JustBeforeEach(func() {
    54  		executeErr = cmd.Execute(nil)
    55  	})
    56  
    57  	Context("when the API version is below the minimum", func() {
    58  		BeforeEach(func() {
    59  			fakeActor.CloudControllerAPIVersionReturns("0.0.0")
    60  		})
    61  
    62  		It("returns a MinimumAPIVersionNotMetError", func() {
    63  			Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{
    64  				CurrentVersion: "0.0.0",
    65  				MinimumVersion: ccversion.MinVersionIsolationSegmentV3,
    66  			}))
    67  		})
    68  	})
    69  
    70  	Context("when checking target fails", func() {
    71  		BeforeEach(func() {
    72  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    73  		})
    74  
    75  		It("returns an error", func() {
    76  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    77  
    78  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    79  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    80  			Expect(checkTargetedOrg).To(BeFalse())
    81  			Expect(checkTargetedSpace).To(BeFalse())
    82  		})
    83  	})
    84  
    85  	Context("when user is logged in", func() {
    86  		BeforeEach(func() {
    87  			fakeConfig.CurrentUserReturns(configv3.User{Name: "admin"}, nil)
    88  			cmd.RequiredArgs.OrganizationName = org
    89  			cmd.RequiredArgs.IsolationSegmentName = isolationSegment
    90  		})
    91  
    92  		It("Outputs a mesaage", func() {
    93  			Expect(testUI.Out).To(Say("Removing entitlement to isolation segment segment1 from org org1 as admin..."))
    94  		})
    95  
    96  		Context("when revoking is successful", func() {
    97  			BeforeEach(func() {
    98  				fakeActor.DeleteIsolationSegmentOrganizationByNameReturns(v3action.Warnings{"warning 1", "warning 2"}, nil)
    99  			})
   100  
   101  			It("Isolation segnment is revoked from org", func() {
   102  				Expect(executeErr).ToNot(HaveOccurred())
   103  
   104  				Expect(testUI.Out).To(Say("OK"))
   105  				Expect(testUI.Err).To(Say("warning 1"))
   106  				Expect(testUI.Err).To(Say("warning 2"))
   107  
   108  				Expect(fakeActor.DeleteIsolationSegmentOrganizationByNameCallCount()).To(Equal(1))
   109  				actualIsolationSegmentName, actualOrgName := fakeActor.DeleteIsolationSegmentOrganizationByNameArgsForCall(0)
   110  				Expect(actualIsolationSegmentName).To(Equal(isolationSegment))
   111  				Expect(actualOrgName).To(Equal(org))
   112  			})
   113  		})
   114  
   115  		Context("generic error while revoking segment isolation", func() {
   116  			var expectedErr error
   117  
   118  			BeforeEach(func() {
   119  				expectedErr = errors.New("ZOMG")
   120  				fakeActor.DeleteIsolationSegmentOrganizationByNameReturns(v3action.Warnings{"warning 1", "warning 2"}, expectedErr)
   121  			})
   122  
   123  			It("returns the error", func() {
   124  				Expect(executeErr).To(MatchError(expectedErr))
   125  				Expect(testUI.Err).To(Say("warning 1"))
   126  				Expect(testUI.Err).To(Say("warning 2"))
   127  			})
   128  		})
   129  	})
   130  })