code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/unshare_private_domain_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  
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("unshare-private-domain command", func() {
    22  	var (
    23  		input           *Buffer
    24  		cmd             UnsharePrivateDomainCommand
    25  		DomainName      = "some-domain-name"
    26  		OrgName         = "some-org-name"
    27  		fakeActor       *v7fakes.FakeActor
    28  		fakeConfig      *commandfakes.FakeConfig
    29  		fakeSharedActor *commandfakes.FakeSharedActor
    30  		testUI          *ui.UI
    31  		binaryName      string
    32  
    33  		executeErr error
    34  	)
    35  
    36  	BeforeEach(func() {
    37  
    38  		input = NewBuffer()
    39  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    40  		fakeActor = new(v7fakes.FakeActor)
    41  		fakeConfig = new(commandfakes.FakeConfig)
    42  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    43  		cmd = UnsharePrivateDomainCommand{
    44  			BaseCommand: BaseCommand{
    45  				Actor:       fakeActor,
    46  				UI:          testUI,
    47  				Config:      fakeConfig,
    48  				SharedActor: fakeSharedActor,
    49  			},
    50  		}
    51  		cmd.RequiredArgs = flag.OrgDomain{
    52  			Organization: OrgName,
    53  			Domain:       DomainName,
    54  		}
    55  		binaryName = "faceman"
    56  		fakeConfig.BinaryNameReturns(binaryName)
    57  
    58  	})
    59  
    60  	JustBeforeEach(func() {
    61  		executeErr = cmd.Execute(nil)
    62  	})
    63  
    64  	When("the user is not logged in", func() {
    65  		BeforeEach(func() {
    66  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{})
    67  		})
    68  
    69  		It("checks target and returns the error", func() {
    70  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    71  			checkedOrg, checkedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    72  			Expect(checkedOrg).To(Equal(false))
    73  			Expect(checkedSpace).To(Equal(false))
    74  
    75  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{}))
    76  		})
    77  	})
    78  
    79  	When("getting the current user fails", func() {
    80  		BeforeEach(func() {
    81  			fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("current-user-error"))
    82  		})
    83  
    84  		It("returns an error", func() {
    85  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    86  			checkedOrg, checkedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    87  			Expect(checkedOrg).To(Equal(false))
    88  			Expect(checkedSpace).To(Equal(false))
    89  
    90  			Expect(executeErr).To(MatchError(errors.New("current-user-error")))
    91  		})
    92  	})
    93  
    94  	When("the environment is setup correctly", func() {
    95  		BeforeEach(func() {
    96  			fakeConfig.CurrentUserReturns(configv3.User{Name: "the-user"}, nil)
    97  		})
    98  
    99  		When("the user says yes", func() {
   100  			BeforeEach(func() {
   101  				_, err := input.Write([]byte("y\n"))
   102  				Expect(err).ToNot(HaveOccurred())
   103  			})
   104  
   105  			When("unsharing the domain errors", func() {
   106  				BeforeEach(func() {
   107  					fakeActor.UnsharePrivateDomainReturns(v7action.Warnings{"warnings-1", "warnings-2"}, errors.New("err-unshare-domain"))
   108  				})
   109  
   110  				It("returns an error and displays warnings", func() {
   111  					Expect(executeErr).To(MatchError("err-unshare-domain"))
   112  					Expect(testUI.Err).To(Say("warnings-1"))
   113  					Expect(testUI.Err).To(Say("warnings-2"))
   114  				})
   115  			})
   116  
   117  			When("unsharing the domain is successful", func() {
   118  				BeforeEach(func() {
   119  					fakeActor.UnsharePrivateDomainReturns(v7action.Warnings{"warnings-1", "warnings-2"}, nil)
   120  				})
   121  
   122  				It("prints all warnings and OK", func() {
   123  					Expect(executeErr).ToNot(HaveOccurred())
   124  					Expect(testUI.Err).To(Say("warnings-1"))
   125  					Expect(testUI.Err).To(Say("warnings-2"))
   126  				})
   127  
   128  				It("unshares the domain", func() {
   129  					Expect(fakeActor.UnsharePrivateDomainCallCount()).To(Equal(1))
   130  					expectedDomainName, expectedOrgName := fakeActor.UnsharePrivateDomainArgsForCall(0)
   131  					Expect(expectedDomainName).To(Equal(DomainName))
   132  					Expect(expectedOrgName).To(Equal(OrgName))
   133  				})
   134  			})
   135  		})
   136  
   137  		When("The user says no", func() {
   138  			BeforeEach(func() {
   139  				_, err := input.Write([]byte("n\n"))
   140  				Expect(err).ToNot(HaveOccurred())
   141  			})
   142  
   143  			It("should confirm nothing was done and exit 0", func() {
   144  				Expect(fakeActor.UnsharePrivateDomainCallCount()).To(Equal(0))
   145  				Expect(executeErr).ToNot(HaveOccurred())
   146  			})
   147  		})
   148  	})
   149  })