github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/create_space_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	. "code.cloudfoundry.org/cli/command/v6"
    11  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("CreateSpaceCommand", func() {
    19  	var (
    20  		fakeConfig      *commandfakes.FakeConfig
    21  		fakeActor       *v6fakes.FakeCreateSpaceActor
    22  		fakeSharedActor *commandfakes.FakeSharedActor
    23  		testUI          *ui.UI
    24  		spaceName       string
    25  		cmd             CreateSpaceCommand
    26  
    27  		executeErr error
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    32  		fakeConfig = new(commandfakes.FakeConfig)
    33  		fakeActor = new(v6fakes.FakeCreateSpaceActor)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		spaceName = "some-space"
    36  
    37  		cmd = CreateSpaceCommand{
    38  			UI:           testUI,
    39  			Config:       fakeConfig,
    40  			Actor:        fakeActor,
    41  			SharedActor:  fakeSharedActor,
    42  			RequiredArgs: flag.Space{Space: spaceName},
    43  		}
    44  	})
    45  
    46  	JustBeforeEach(func() {
    47  		executeErr = cmd.Execute(nil)
    48  	})
    49  
    50  	It("checks for user being logged in", func() {
    51  		Expect(fakeSharedActor.RequireCurrentUserCallCount()).To(Equal(1))
    52  	})
    53  
    54  	When("user is not logged in", func() {
    55  		expectedErr := errors.New("not logged in and/or can't verify login because of error")
    56  
    57  		BeforeEach(func() {
    58  			fakeSharedActor.RequireCurrentUserReturns("", expectedErr)
    59  		})
    60  
    61  		It("returns the error", func() {
    62  			Expect(executeErr).To(MatchError(expectedErr))
    63  		})
    64  	})
    65  
    66  	When("user is logged in", func() {
    67  		var username string
    68  
    69  		BeforeEach(func() {
    70  			username = "some-guy"
    71  			fakeSharedActor.RequireCurrentUserReturns(username, nil)
    72  		})
    73  
    74  		When("user specifies an org using the -o flag", func() {
    75  			var specifiedOrgName string
    76  
    77  			BeforeEach(func() {
    78  				specifiedOrgName = "specified-org"
    79  				cmd.Organization = specifiedOrgName
    80  			})
    81  
    82  			It("does not require a targeted org", func() {
    83  				Expect(fakeSharedActor.RequireTargetedOrgCallCount()).To(Equal(0))
    84  			})
    85  
    86  			It("creates a space in the specified org with no errors", func() {
    87  				Expect(executeErr).ToNot(HaveOccurred())
    88  				Expect(testUI.Out).To(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, specifiedOrgName, username))
    89  
    90  				Expect(testUI.Out).To(Say("OK\n\n"))
    91  
    92  				Expect(fakeActor.CreateSpaceCallCount()).To(Equal(1))
    93  				inputSpace, inputOrg, _ := fakeActor.CreateSpaceArgsForCall(0)
    94  				Expect(inputSpace).To(Equal(spaceName))
    95  				Expect(inputOrg).To(Equal(specifiedOrgName))
    96  			})
    97  
    98  			When("an org is targeted", func() {
    99  				BeforeEach(func() {
   100  					fakeSharedActor.RequireTargetedOrgReturns("do-not-use-this-org", nil)
   101  				})
   102  
   103  				It("uses the specified org, not the targeted org", func() {
   104  					Expect(executeErr).ToNot(HaveOccurred())
   105  					Expect(testUI.Out).To(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, specifiedOrgName, username))
   106  					_, inputOrg, _ := fakeActor.CreateSpaceArgsForCall(0)
   107  					Expect(inputOrg).To(Equal(specifiedOrgName))
   108  				})
   109  			})
   110  		})
   111  
   112  		When("no org is specified using the -o flag", func() {
   113  			It("requires a targeted org", func() {
   114  				Expect(fakeSharedActor.RequireTargetedOrgCallCount()).To(Equal(1))
   115  			})
   116  
   117  			When("no org is targeted", func() {
   118  				BeforeEach(func() {
   119  					fakeSharedActor.RequireTargetedOrgReturns("", errors.New("check target error"))
   120  				})
   121  
   122  				It("returns an error", func() {
   123  					Expect(executeErr).To(MatchError("check target error"))
   124  				})
   125  			})
   126  
   127  			When("an org is targeted", func() {
   128  				var orgName string
   129  
   130  				BeforeEach(func() {
   131  					fakeSharedActor.RequireTargetedOrgReturns(orgName, nil)
   132  				})
   133  
   134  				It("attempts to create a space with the specified name in the targeted org", func() {
   135  					Expect(executeErr).ToNot(HaveOccurred())
   136  					Expect(fakeActor.CreateSpaceCallCount()).To(Equal(1))
   137  					inputSpace, inputOrg, _ := fakeActor.CreateSpaceArgsForCall(0)
   138  					Expect(inputSpace).To(Equal(spaceName))
   139  					Expect(inputOrg).To(Equal(orgName))
   140  				})
   141  
   142  				When("creating the space succeeds", func() {
   143  					BeforeEach(func() {
   144  						fakeActor.CreateSpaceReturns(
   145  							v2action.Space{GUID: "some-space-guid", OrganizationGUID: "some-org-guid"},
   146  							v2action.Warnings{"warn-1", "warn-2"},
   147  							nil,
   148  						)
   149  					})
   150  
   151  					It("tells you that it plans to give the user space roles", func() {
   152  						Expect(executeErr).ToNot(HaveOccurred())
   153  						Expect(testUI.Out).To(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, orgName, username))
   154  
   155  						Expect(testUI.Err).To(Say("warn-1\nwarn-2\n"))
   156  						Expect(testUI.Out).To(Say("OK\n"))
   157  
   158  						Expect(testUI.Out).To(Say(`Assigning role SpaceManager to user %s in org %s / space %s as %s\.\.\.`, username, orgName, spaceName, username))
   159  						Expect(testUI.Out).To(Say("OK\n"))
   160  						Expect(testUI.Out).To(Say(`Assigning role SpaceDeveloper to user %s in org %s / space %s as %s\.\.\.`, username, orgName, spaceName, username))
   161  						Expect(testUI.Out).To(Say("OK\n\n"))
   162  						Eventually(testUI.Out).Should(Say(`TIP: Use 'cf target -o "%s" -s "%s"' to target new space`, orgName, spaceName))
   163  					})
   164  
   165  					It("attempts to make the user a space manager", func() {
   166  						Expect(fakeActor.GrantSpaceManagerByUsernameCallCount()).To(Equal(1))
   167  						orgGUID, spaceGUID, usernameArg := fakeActor.GrantSpaceManagerByUsernameArgsForCall(0)
   168  						Expect(orgGUID).To(Equal("some-org-guid"))
   169  						Expect(spaceGUID).To(Equal("some-space-guid"))
   170  						Expect(usernameArg).To(Equal(username))
   171  					})
   172  
   173  					When("making the user a space manager succeeds", func() {
   174  						BeforeEach(func() {
   175  							fakeActor.GrantSpaceManagerByUsernameReturns(
   176  								v2action.Warnings{"space-manager-warning-1", "space-manager-warning-2"},
   177  								nil,
   178  							)
   179  						})
   180  
   181  						It("prints the warnings", func() {
   182  							Expect(executeErr).ToNot(HaveOccurred())
   183  							Expect(testUI.Err).To(Say("space-manager-warning-1\nspace-manager-warning-2\n"))
   184  						})
   185  
   186  						It("attempts to make the user a space developer", func() {
   187  							Expect(fakeActor.GrantSpaceDeveloperByUsernameCallCount()).To(Equal(1))
   188  							spaceGUID, usernameArg := fakeActor.GrantSpaceDeveloperByUsernameArgsForCall(0)
   189  							Expect(spaceGUID).To(Equal("some-space-guid"))
   190  							Expect(usernameArg).To(Equal(username))
   191  						})
   192  
   193  						When("making the user a space developer succeeds", func() {
   194  							BeforeEach(func() {
   195  								fakeActor.GrantSpaceDeveloperByUsernameReturns(
   196  									v2action.Warnings{"space-developer-warning", "other-warning"},
   197  									nil,
   198  								)
   199  							})
   200  
   201  							It("prints the warnings", func() {
   202  								Expect(testUI.Err).To(Say("space-developer-warning"))
   203  								Expect(testUI.Err).To(Say("other-warning"))
   204  							})
   205  						})
   206  
   207  						When("making the user a space developer fails", func() {
   208  							BeforeEach(func() {
   209  								fakeActor.GrantSpaceDeveloperByUsernameReturns(
   210  									v2action.Warnings{"space-developer-warning", "other-warning"},
   211  									errors.New("Some terrible failure case"),
   212  								)
   213  							})
   214  
   215  							It("returns the error", func() {
   216  								Expect(executeErr).To(MatchError("Some terrible failure case"))
   217  							})
   218  
   219  							It("prints the warnings", func() {
   220  								Expect(testUI.Err).To(Say("space-developer-warning"))
   221  								Expect(testUI.Err).To(Say("other-warning"))
   222  							})
   223  						})
   224  					})
   225  
   226  					When("making the user a space manager fails", func() {
   227  						BeforeEach(func() {
   228  							fakeActor.GrantSpaceManagerByUsernameReturns(
   229  								v2action.Warnings{"space-manager-warning-1", "space-manager-warning-2"},
   230  								errors.New("some error"),
   231  							)
   232  						})
   233  
   234  						It("should print all the warnings and returns the error", func() {
   235  							Expect(executeErr).To(MatchError("some error"))
   236  							Expect(testUI.Out).To(Say(`Assigning role SpaceManager to user %s in org %s / space %s as %s\.\.\.`, username, orgName, spaceName, username))
   237  							Expect(testUI.Err).To(Say("space-manager-warning-1\nspace-manager-warning-2\n"))
   238  						})
   239  
   240  						It("should not try to grant the user SpaceDeveloper", func() {
   241  							Expect(fakeActor.GrantSpaceDeveloperByUsernameCallCount()).To(Equal(0))
   242  						})
   243  					})
   244  				})
   245  
   246  				When("creating the space fails", func() {
   247  					BeforeEach(func() {
   248  						fakeActor.CreateSpaceReturns(
   249  							v2action.Space{},
   250  							v2action.Warnings{"some warning"},
   251  							errors.New("some error"),
   252  						)
   253  					})
   254  
   255  					It("should print the warnings and return the error", func() {
   256  						Expect(executeErr).To(MatchError("some error"))
   257  						Expect(testUI.Err).To(Say("some warning\n"))
   258  					})
   259  				})
   260  
   261  				When("quota is not specified", func() {
   262  					BeforeEach(func() {
   263  						cmd.Quota = ""
   264  					})
   265  					It("attempts to create the space with no quota", func() {
   266  						Expect(executeErr).ToNot(HaveOccurred())
   267  						_, _, inputQuota := fakeActor.CreateSpaceArgsForCall(0)
   268  						Expect(inputQuota).To(BeEmpty())
   269  					})
   270  
   271  				})
   272  
   273  				When("quota is specified", func() {
   274  					BeforeEach(func() {
   275  						cmd.Quota = "some-quota"
   276  					})
   277  
   278  					It("attempts to create the space with no quota", func() {
   279  						Expect(executeErr).ToNot(HaveOccurred())
   280  						_, _, inputQuota := fakeActor.CreateSpaceArgsForCall(0)
   281  						Expect(inputQuota).To(Equal("some-quota"))
   282  					})
   283  				})
   284  			})
   285  		})
   286  
   287  		When("the server returns an already exists error", func() {
   288  			BeforeEach(func() {
   289  				fakeActor.CreateSpaceReturns(v2action.Space{}, v2action.Warnings{"already-exists-warnings"}, actionerror.SpaceNameTakenError{})
   290  			})
   291  
   292  			It("displays a warning but does not error", func() {
   293  				Expect(executeErr).ToNot(HaveOccurred())
   294  				Expect(testUI.Err).To(Say("already-exists-warnings"))
   295  				Expect(testUI.Out).To(Say("OK\n"))
   296  				Expect(testUI.Err).To(Say("Space %s already exists", spaceName))
   297  			})
   298  		})
   299  	})
   300  })