github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/organization/org_test.go (about)

     1  package organization_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/commandregistry"
     5  	"code.cloudfoundry.org/cli/cf/flags"
     6  	"code.cloudfoundry.org/cli/cf/models"
     7  	"code.cloudfoundry.org/cli/cf/requirements"
     8  	"code.cloudfoundry.org/cli/plugin/models"
     9  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    10  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    11  
    12  	"code.cloudfoundry.org/cli/cf/api"
    13  	"code.cloudfoundry.org/cli/cf/commands/organization"
    14  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    15  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  var _ = Describe("org command", func() {
    21  	var (
    22  		ui             *testterm.FakeUI
    23  		getOrgModel    *plugin_models.GetOrg_Model
    24  		deps           commandregistry.Dependency
    25  		reqFactory     *requirementsfakes.FakeFactory
    26  		loginReq       *requirementsfakes.FakeRequirement
    27  		orgRequirement *requirementsfakes.FakeOrganizationRequirement
    28  		cmd            organization.ShowOrg
    29  		flagContext    flags.FlagContext
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		ui = new(testterm.FakeUI)
    34  		getOrgModel = new(plugin_models.GetOrg_Model)
    35  
    36  		deps = commandregistry.Dependency{
    37  			UI:          ui,
    38  			Config:      testconfig.NewRepositoryWithDefaults(),
    39  			RepoLocator: api.RepositoryLocator{},
    40  			PluginModels: &commandregistry.PluginModels{
    41  				Organization: getOrgModel,
    42  			},
    43  		}
    44  
    45  		reqFactory = new(requirementsfakes.FakeFactory)
    46  
    47  		loginReq = new(requirementsfakes.FakeRequirement)
    48  		loginReq.ExecuteReturns(nil)
    49  		reqFactory.NewLoginRequirementReturns(loginReq)
    50  
    51  		orgRequirement = new(requirementsfakes.FakeOrganizationRequirement)
    52  		orgRequirement.ExecuteReturns(nil)
    53  		reqFactory.NewOrganizationRequirementReturns(orgRequirement)
    54  
    55  		cmd = organization.ShowOrg{}
    56  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    57  		cmd.SetDependency(deps, false)
    58  	})
    59  
    60  	Describe("Requirements", func() {
    61  		Context("when the wrong number of args are provided", func() {
    62  			BeforeEach(func() {
    63  				err := flagContext.Parse()
    64  				Expect(err).NotTo(HaveOccurred())
    65  			})
    66  
    67  			It("fails with no args", func() {
    68  				_, err := cmd.Requirements(reqFactory, flagContext)
    69  				Expect(err).To(HaveOccurred())
    70  				Expect(ui.Outputs()).To(ContainSubstrings(
    71  					[]string{"FAILED"},
    72  					[]string{"Incorrect Usage. Requires an argument"},
    73  				))
    74  			})
    75  		})
    76  
    77  		Context("when provided exactly one arg", func() {
    78  			var actualRequirements []requirements.Requirement
    79  
    80  			BeforeEach(func() {
    81  				err := flagContext.Parse("my-org")
    82  				Expect(err).NotTo(HaveOccurred())
    83  				actualRequirements, err = cmd.Requirements(reqFactory, flagContext)
    84  				Expect(err).NotTo(HaveOccurred())
    85  			})
    86  
    87  			Context("when no flags are provided", func() {
    88  				It("returns a login requirement", func() {
    89  					Expect(reqFactory.NewLoginRequirementCallCount()).To(Equal(1))
    90  					Expect(actualRequirements).To(ContainElement(loginReq))
    91  				})
    92  
    93  				It("returns an organization requirement", func() {
    94  					Expect(reqFactory.NewOrganizationRequirementCallCount()).To(Equal(1))
    95  					Expect(actualRequirements).To(ContainElement(orgRequirement))
    96  				})
    97  			})
    98  		})
    99  	})
   100  
   101  	Describe("Execute", func() {
   102  		var (
   103  			org        models.Organization
   104  			executeErr error
   105  		)
   106  
   107  		BeforeEach(func() {
   108  			org = models.Organization{
   109  				OrganizationFields: models.OrganizationFields{
   110  					Name: "my-org",
   111  					GUID: "my-org-guid",
   112  					QuotaDefinition: models.QuotaFields{
   113  						Name:                    "cantina-quota",
   114  						MemoryLimit:             512,
   115  						InstanceMemoryLimit:     256,
   116  						RoutesLimit:             2,
   117  						ServicesLimit:           5,
   118  						NonBasicServicesAllowed: true,
   119  						AppInstanceLimit:        7,
   120  						ReservedRoutePorts:      "7",
   121  					},
   122  				},
   123  				Spaces: []models.SpaceFields{
   124  					models.SpaceFields{
   125  						Name: "development",
   126  						GUID: "dev-space-guid-1",
   127  					},
   128  					models.SpaceFields{
   129  						Name: "staging",
   130  						GUID: "staging-space-guid-1",
   131  					},
   132  				},
   133  				Domains: []models.DomainFields{
   134  					models.DomainFields{
   135  						Name: "cfapps.io",
   136  						GUID: "1111",
   137  						OwningOrganizationGUID: "my-org-guid",
   138  						Shared:                 true,
   139  					},
   140  					models.DomainFields{
   141  						Name: "cf-app.com",
   142  						GUID: "2222",
   143  						OwningOrganizationGUID: "my-org-guid",
   144  						Shared:                 false,
   145  					},
   146  				},
   147  				SpaceQuotas: []models.SpaceQuota{
   148  					{Name: "space-quota-1", GUID: "space-quota-1-guid", MemoryLimit: 512, InstanceMemoryLimit: -1},
   149  					{Name: "space-quota-2", GUID: "space-quota-2-guid", MemoryLimit: 256, InstanceMemoryLimit: 128},
   150  				},
   151  			}
   152  
   153  			orgRequirement.GetOrganizationReturns(org)
   154  		})
   155  
   156  		JustBeforeEach(func() {
   157  			executeErr = cmd.Execute(flagContext)
   158  		})
   159  
   160  		Context("when logged in, and provided the name of an org", func() {
   161  			BeforeEach(func() {
   162  				err := flagContext.Parse("my-org")
   163  				Expect(err).NotTo(HaveOccurred())
   164  				cmd.Requirements(reqFactory, flagContext)
   165  			})
   166  
   167  			It("shows the org with the given name", func() {
   168  				Expect(executeErr).NotTo(HaveOccurred())
   169  
   170  				Expect(ui.Outputs()).To(ContainSubstrings(
   171  					[]string{"Getting info for org", "my-org", "my-user"},
   172  					[]string{"OK"},
   173  					[]string{"my-org"},
   174  					[]string{"domains:", "cfapps.io", "cf-app.com"},
   175  					[]string{"quota: ", "cantina-quota", "512M", "256M instance memory limit", "2 routes", "5 services", "paid services allowed", "7 app instance limit", "7 route ports"},
   176  					[]string{"spaces:", "development", "staging"},
   177  					[]string{"space quotas:", "space-quota-1", "space-quota-2"},
   178  				))
   179  			})
   180  
   181  			Context("when ReservedRoutePorts is set to -1", func() {
   182  				BeforeEach(func() {
   183  					org.QuotaDefinition.ReservedRoutePorts = "-1"
   184  					orgRequirement.GetOrganizationReturns(org)
   185  				})
   186  
   187  				It("shows unlimited route ports", func() {
   188  					Expect(executeErr).NotTo(HaveOccurred())
   189  
   190  					Expect(ui.Outputs()).To(ContainSubstrings(
   191  						[]string{"unlimited route ports"},
   192  					))
   193  				})
   194  			})
   195  
   196  			Context("when the reserved route ports field is not provided by the CC API", func() {
   197  				BeforeEach(func() {
   198  					org.QuotaDefinition.ReservedRoutePorts = ""
   199  					orgRequirement.GetOrganizationReturns(org)
   200  				})
   201  
   202  				It("should not display route ports", func() {
   203  					Expect(executeErr).NotTo(HaveOccurred())
   204  
   205  					Expect(ui.Outputs()).NotTo(ContainSubstrings(
   206  						[]string{"route ports"},
   207  					))
   208  				})
   209  			})
   210  
   211  			Context("when the guid flag is provided", func() {
   212  				BeforeEach(func() {
   213  					err := flagContext.Parse("my-org", "--guid")
   214  					Expect(err).NotTo(HaveOccurred())
   215  				})
   216  
   217  				It("shows only the org guid", func() {
   218  					Expect(executeErr).NotTo(HaveOccurred())
   219  
   220  					Expect(ui.Outputs()).To(ContainSubstrings(
   221  						[]string{"my-org-guid"},
   222  					))
   223  					Expect(ui.Outputs()).ToNot(ContainSubstrings(
   224  						[]string{"Getting info for org", "my-org", "my-user"},
   225  					))
   226  				})
   227  			})
   228  
   229  			Context("when invoked by a plugin", func() {
   230  				BeforeEach(func() {
   231  					cmd.SetDependency(deps, true)
   232  				})
   233  
   234  				It("populates the plugin model", func() {
   235  					Expect(executeErr).NotTo(HaveOccurred())
   236  
   237  					Expect(getOrgModel.Guid).To(Equal("my-org-guid"))
   238  					Expect(getOrgModel.Name).To(Equal("my-org"))
   239  					// quota
   240  					Expect(getOrgModel.QuotaDefinition.Name).To(Equal("cantina-quota"))
   241  					Expect(getOrgModel.QuotaDefinition.MemoryLimit).To(Equal(int64(512)))
   242  					Expect(getOrgModel.QuotaDefinition.InstanceMemoryLimit).To(Equal(int64(256)))
   243  					Expect(getOrgModel.QuotaDefinition.RoutesLimit).To(Equal(2))
   244  					Expect(getOrgModel.QuotaDefinition.ServicesLimit).To(Equal(5))
   245  					Expect(getOrgModel.QuotaDefinition.NonBasicServicesAllowed).To(BeTrue())
   246  
   247  					// domains
   248  					Expect(getOrgModel.Domains).To(HaveLen(2))
   249  					Expect(getOrgModel.Domains[0].Name).To(Equal("cfapps.io"))
   250  					Expect(getOrgModel.Domains[0].Guid).To(Equal("1111"))
   251  					Expect(getOrgModel.Domains[0].OwningOrganizationGuid).To(Equal("my-org-guid"))
   252  					Expect(getOrgModel.Domains[0].Shared).To(BeTrue())
   253  					Expect(getOrgModel.Domains[1].Name).To(Equal("cf-app.com"))
   254  					Expect(getOrgModel.Domains[1].Guid).To(Equal("2222"))
   255  					Expect(getOrgModel.Domains[1].OwningOrganizationGuid).To(Equal("my-org-guid"))
   256  					Expect(getOrgModel.Domains[1].Shared).To(BeFalse())
   257  
   258  					// spaces
   259  					Expect(getOrgModel.Spaces).To(HaveLen(2))
   260  					Expect(getOrgModel.Spaces[0].Name).To(Equal("development"))
   261  					Expect(getOrgModel.Spaces[0].Guid).To(Equal("dev-space-guid-1"))
   262  					Expect(getOrgModel.Spaces[1].Name).To(Equal("staging"))
   263  					Expect(getOrgModel.Spaces[1].Guid).To(Equal("staging-space-guid-1"))
   264  
   265  					// space quotas
   266  					Expect(getOrgModel.SpaceQuotas).To(HaveLen(2))
   267  					Expect(getOrgModel.SpaceQuotas[0].Name).To(Equal("space-quota-1"))
   268  					Expect(getOrgModel.SpaceQuotas[0].Guid).To(Equal("space-quota-1-guid"))
   269  					Expect(getOrgModel.SpaceQuotas[0].MemoryLimit).To(Equal(int64(512)))
   270  					Expect(getOrgModel.SpaceQuotas[0].InstanceMemoryLimit).To(Equal(int64(-1)))
   271  					Expect(getOrgModel.SpaceQuotas[1].Name).To(Equal("space-quota-2"))
   272  					Expect(getOrgModel.SpaceQuotas[1].Guid).To(Equal("space-quota-2-guid"))
   273  					Expect(getOrgModel.SpaceQuotas[1].MemoryLimit).To(Equal(int64(256)))
   274  					Expect(getOrgModel.SpaceQuotas[1].InstanceMemoryLimit).To(Equal(int64(128)))
   275  				})
   276  			})
   277  		})
   278  	})
   279  })