github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7action/space_feature_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/resources"
     8  
     9  	. "code.cloudfoundry.org/cli/actor/v7action"
    10  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("space features", func() {
    17  	var (
    18  		actor                     *Actor
    19  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    24  		fakeConfig := new(v7actionfakes.FakeConfig)
    25  		actor = NewActor(fakeCloudControllerClient, fakeConfig, nil, nil, nil, nil)
    26  	})
    27  
    28  	Describe("GetSpaceFeature", func() {
    29  		var (
    30  			spaceName   string
    31  			orgGUID     string
    32  			featureName string
    33  			enabled     bool
    34  			warnings    Warnings
    35  			executeErr  error
    36  		)
    37  
    38  		BeforeEach(func() {
    39  			spaceName = "some-space-name"
    40  			orgGUID = "some-org-guid"
    41  			featureName = "ssh"
    42  
    43  			fakeCloudControllerClient.GetSpacesReturns(
    44  				[]resources.Space{
    45  					resources.Space{
    46  						Name: spaceName,
    47  						GUID: "some-space-guid",
    48  					},
    49  				},
    50  				ccv3.IncludedResources{},
    51  				ccv3.Warnings{"get-space-warning"},
    52  				nil,
    53  			)
    54  
    55  			fakeCloudControllerClient.GetSpaceFeatureReturns(
    56  				true,
    57  				ccv3.Warnings{"get-space-feature-warning"},
    58  				nil,
    59  			)
    60  		})
    61  
    62  		JustBeforeEach(func() {
    63  			enabled, warnings, executeErr = actor.GetSpaceFeature(spaceName, orgGUID, featureName)
    64  		})
    65  
    66  		It("finds the space and retrieves the feature value", func() {
    67  			Expect(executeErr).NotTo(HaveOccurred())
    68  			Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
    69  			query := fakeCloudControllerClient.GetSpacesArgsForCall(0)
    70  			Expect(query).To(ConsistOf(
    71  				ccv3.Query{Key: ccv3.NameFilter, Values: []string{spaceName}},
    72  				ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}},
    73  				ccv3.Query{Key: ccv3.PerPage, Values: []string{"1"}},
    74  				ccv3.Query{Key: ccv3.Page, Values: []string{"1"}},
    75  			))
    76  
    77  			Expect(fakeCloudControllerClient.GetSpaceFeatureCallCount()).To(Equal(1))
    78  			inputSpaceGUID, inputFeature := fakeCloudControllerClient.GetSpaceFeatureArgsForCall(0)
    79  			Expect(inputSpaceGUID).To(Equal("some-space-guid"))
    80  			Expect(inputFeature).To(Equal(featureName))
    81  
    82  			Expect(enabled).To(BeTrue())
    83  			Expect(warnings).To(ConsistOf("get-space-warning", "get-space-feature-warning"))
    84  		})
    85  
    86  		Context("when the space does not exist", func() {
    87  			BeforeEach(func() {
    88  				fakeCloudControllerClient.GetSpacesReturns(
    89  					[]resources.Space{},
    90  					ccv3.IncludedResources{},
    91  					ccv3.Warnings{"get-space-warning"},
    92  					nil,
    93  				)
    94  			})
    95  
    96  			It("returns a SpaceNotFoundError", func() {
    97  				Expect(executeErr).To(HaveOccurred())
    98  				Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: spaceName}))
    99  				Expect(warnings).To(ConsistOf("get-space-warning"))
   100  			})
   101  		})
   102  
   103  		Context("when an API error occurs", func() {
   104  			BeforeEach(func() {
   105  				fakeCloudControllerClient.GetSpaceFeatureReturns(
   106  					true,
   107  					ccv3.Warnings{"get-space-feature-warning"},
   108  					errors.New("space-feature-error"),
   109  				)
   110  			})
   111  
   112  			It("returns the error and warnings", func() {
   113  				Expect(executeErr).To(HaveOccurred())
   114  				Expect(executeErr).To(MatchError("space-feature-error"))
   115  				Expect(warnings).To(ConsistOf("get-space-warning", "get-space-feature-warning"))
   116  			})
   117  		})
   118  	})
   119  
   120  	Describe("AllowSpaceSSH", func() {
   121  
   122  		var (
   123  			spaceName  string
   124  			spaceGUID  string
   125  			orgGUID    string
   126  			enabled    bool
   127  			feature    = "ssh"
   128  			warnings   Warnings
   129  			executeErr error
   130  		)
   131  
   132  		BeforeEach(func() {
   133  			spaceName = "some-space-name"
   134  			spaceGUID = "some-space-guid"
   135  			enabled = true
   136  			feature = "ssh"
   137  			orgGUID = "some-org-guid"
   138  			fakeCloudControllerClient.GetSpacesReturns(
   139  				[]resources.Space{
   140  					resources.Space{
   141  						Name: spaceName,
   142  						GUID: spaceGUID,
   143  					},
   144  				},
   145  				ccv3.IncludedResources{},
   146  				ccv3.Warnings{"get-space-warning"},
   147  				nil,
   148  			)
   149  
   150  			fakeCloudControllerClient.UpdateSpaceFeatureReturns(
   151  				ccv3.Warnings{"update-space-feature-warning"},
   152  				nil,
   153  			)
   154  		})
   155  
   156  		JustBeforeEach(func() {
   157  			warnings, executeErr = actor.UpdateSpaceFeature(spaceName, orgGUID, enabled, feature)
   158  		})
   159  
   160  		When("the GetSpaceFeature returns an error", func() {
   161  			BeforeEach(func() {
   162  				fakeCloudControllerClient.GetSpaceFeatureReturns(
   163  					false,
   164  					ccv3.Warnings{"get-space-feature-warning"},
   165  					errors.New("get-ssh-error"),
   166  				)
   167  			})
   168  
   169  			It("returns a the error", func() {
   170  				Expect(executeErr).To(MatchError("get-ssh-error"))
   171  			})
   172  
   173  		})
   174  
   175  		When("space ssh is already enabled", func() {
   176  			BeforeEach(func() {
   177  				fakeCloudControllerClient.GetSpaceFeatureReturns(
   178  					true,
   179  					ccv3.Warnings{"get-space-feature-warning"},
   180  					nil,
   181  				)
   182  			})
   183  
   184  			When("trying to set ssh to enabled", func() {
   185  				It("returns a descriptive error", func() {
   186  					Expect(executeErr).To(MatchError(actionerror.SpaceSSHAlreadyEnabledError{
   187  						Space: spaceName}))
   188  				})
   189  
   190  			})
   191  
   192  			When("trying to set ssh to disabled", func() {
   193  
   194  				BeforeEach(func() {
   195  					enabled = false
   196  				})
   197  
   198  				It("disables the ssh feature for the space", func() {
   199  					Expect(executeErr).NotTo(HaveOccurred())
   200  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   201  					query := fakeCloudControllerClient.GetSpacesArgsForCall(0)
   202  					Expect(query).To(ConsistOf(
   203  						ccv3.Query{Key: ccv3.NameFilter, Values: []string{spaceName}},
   204  						ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}},
   205  						ccv3.Query{Key: ccv3.PerPage, Values: []string{"1"}},
   206  						ccv3.Query{Key: ccv3.Page, Values: []string{"1"}},
   207  					))
   208  
   209  					Expect(fakeCloudControllerClient.UpdateSpaceFeatureCallCount()).To(Equal(1))
   210  
   211  					spaceGUID, enable, feature := fakeCloudControllerClient.UpdateSpaceFeatureArgsForCall(0)
   212  
   213  					Expect(spaceGUID).To(Equal(spaceGUID))
   214  					Expect(enable).To(Equal(false))
   215  					Expect(feature).To(Equal("ssh"))
   216  					Expect(warnings).To(ConsistOf("get-space-warning", "get-space-feature-warning", "update-space-feature-warning"))
   217  
   218  				})
   219  			})
   220  		})
   221  
   222  		When("space ssh is already disabled", func() {
   223  			BeforeEach(func() {
   224  				fakeCloudControllerClient.GetSpaceFeatureReturns(
   225  					false,
   226  					ccv3.Warnings{"get-space-feature-warning"},
   227  					nil,
   228  				)
   229  			})
   230  
   231  			When("trying to set ssh to disabled", func() {
   232  
   233  				BeforeEach(func() {
   234  					enabled = false
   235  				})
   236  
   237  				It("returns a descriptive error", func() {
   238  					Expect(executeErr).To(MatchError(actionerror.SpaceSSHAlreadyDisabledError{
   239  						Space: spaceName}))
   240  				})
   241  
   242  			})
   243  
   244  			When("trying to set ssh to enabled", func() {
   245  				It("enables the ssh feature for the space", func() {
   246  					Expect(executeErr).NotTo(HaveOccurred())
   247  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   248  					query := fakeCloudControllerClient.GetSpacesArgsForCall(0)
   249  					Expect(query).To(ConsistOf(
   250  						ccv3.Query{Key: ccv3.NameFilter, Values: []string{spaceName}},
   251  						ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}},
   252  						ccv3.Query{Key: ccv3.PerPage, Values: []string{"1"}},
   253  						ccv3.Query{Key: ccv3.Page, Values: []string{"1"}},
   254  					))
   255  
   256  					Expect(fakeCloudControllerClient.UpdateSpaceFeatureCallCount()).To(Equal(1))
   257  
   258  					spaceGUID, enable, feature := fakeCloudControllerClient.UpdateSpaceFeatureArgsForCall(0)
   259  
   260  					Expect(spaceGUID).To(Equal(spaceGUID))
   261  					Expect(enable).To(Equal(true))
   262  					Expect(feature).To(Equal("ssh"))
   263  					Expect(warnings).To(ConsistOf("get-space-warning", "get-space-feature-warning", "update-space-feature-warning"))
   264  
   265  				})
   266  			})
   267  		})
   268  		When("the update space feature returns an error", func() {
   269  			BeforeEach(func() {
   270  				fakeCloudControllerClient.UpdateSpaceFeatureReturns(
   271  					ccv3.Warnings{"get-space-feature-warning"},
   272  					errors.New("update-ssh-error"),
   273  				)
   274  			})
   275  
   276  			It("returns a the error", func() {
   277  				Expect(executeErr).To(MatchError("update-ssh-error"))
   278  			})
   279  
   280  		})
   281  
   282  		It("enables the ssh feature for the space", func() {
   283  			Expect(executeErr).NotTo(HaveOccurred())
   284  			Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   285  			query := fakeCloudControllerClient.GetSpacesArgsForCall(0)
   286  			Expect(query).To(ConsistOf(
   287  				ccv3.Query{Key: ccv3.NameFilter, Values: []string{spaceName}},
   288  				ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}},
   289  				ccv3.Query{Key: ccv3.PerPage, Values: []string{"1"}},
   290  				ccv3.Query{Key: ccv3.Page, Values: []string{"1"}},
   291  			))
   292  
   293  			Expect(fakeCloudControllerClient.UpdateSpaceFeatureCallCount()).To(Equal(1))
   294  
   295  			spaceGUID, enable, feature := fakeCloudControllerClient.UpdateSpaceFeatureArgsForCall(0)
   296  
   297  			Expect(spaceGUID).To(Equal(spaceGUID))
   298  			Expect(enable).To(Equal(true))
   299  			Expect(feature).To(Equal("ssh"))
   300  			Expect(warnings).To(ConsistOf("get-space-warning", "update-space-feature-warning"))
   301  
   302  		})
   303  	})
   304  })