github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/commands/featureflag/feature_flags_test.go (about)

     1  package featureflag_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/featureflags/featureflagsfakes"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/commands/featureflag"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    14  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    15  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    16  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    17  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("feature-flags command", func() {
    23  	var (
    24  		ui                  *testterm.FakeUI
    25  		requirementsFactory *requirementsfakes.FakeFactory
    26  		configRepo          coreconfig.Repository
    27  		flagRepo            *featureflagsfakes.FakeFeatureFlagRepository
    28  		deps                commandregistry.Dependency
    29  	)
    30  
    31  	updateCommandDependency := func(pluginCall bool) {
    32  		deps.UI = ui
    33  		deps.RepoLocator = deps.RepoLocator.SetFeatureFlagRepository(flagRepo)
    34  		deps.Config = configRepo
    35  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("feature-flags").SetDependency(deps, pluginCall))
    36  	}
    37  
    38  	BeforeEach(func() {
    39  		ui = &testterm.FakeUI{}
    40  		configRepo = testconfig.NewRepositoryWithDefaults()
    41  		requirementsFactory = new(requirementsfakes.FakeFactory)
    42  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    43  		flagRepo = new(featureflagsfakes.FakeFeatureFlagRepository)
    44  	})
    45  
    46  	runCommand := func(args ...string) bool {
    47  		return testcmd.RunCLICommand("feature-flags", args, requirementsFactory, updateCommandDependency, false, ui)
    48  	}
    49  
    50  	Describe("requirements", func() {
    51  		It("requires the user to be logged in", func() {
    52  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    53  			Expect(runCommand()).ToNot(HavePassedRequirements())
    54  		})
    55  
    56  		Context("when arguments are provided", func() {
    57  			var cmd commandregistry.Command
    58  			var flagContext flags.FlagContext
    59  
    60  			BeforeEach(func() {
    61  				cmd = &featureflag.ListFeatureFlags{}
    62  				cmd.SetDependency(deps, false)
    63  				flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    64  			})
    65  
    66  			It("should fail with usage", func() {
    67  				flagContext.Parse("blahblah")
    68  
    69  				reqs, err := cmd.Requirements(requirementsFactory, flagContext)
    70  				Expect(err).NotTo(HaveOccurred())
    71  
    72  				err = testcmd.RunRequirements(reqs)
    73  				Expect(err).To(HaveOccurred())
    74  				Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
    75  				Expect(err.Error()).To(ContainSubstring("No argument required"))
    76  			})
    77  		})
    78  	})
    79  
    80  	Describe("when logged in", func() {
    81  		BeforeEach(func() {
    82  			flags := []models.FeatureFlag{
    83  				{
    84  					Name:         "user_org_creation",
    85  					Enabled:      true,
    86  					ErrorMessage: "error",
    87  				},
    88  				{
    89  					Name:    "private_domain_creation",
    90  					Enabled: false,
    91  				},
    92  				{
    93  					Name:    "app_bits_upload",
    94  					Enabled: true,
    95  				},
    96  				{
    97  					Name:    "app_scaling",
    98  					Enabled: true,
    99  				},
   100  				{
   101  					Name:    "route_creation",
   102  					Enabled: false,
   103  				},
   104  			}
   105  			flagRepo.ListReturns(flags, nil)
   106  		})
   107  
   108  		It("lists the state of all feature flags", func() {
   109  			runCommand()
   110  			Expect(ui.Outputs()).To(ContainSubstrings(
   111  				[]string{"Retrieving status of all flagged features as my-user..."},
   112  				[]string{"Feature", "State"},
   113  				[]string{"user_org_creation", "enabled"},
   114  				[]string{"private_domain_creation", "disabled"},
   115  				[]string{"app_bits_upload", "enabled"},
   116  				[]string{"app_scaling", "enabled"},
   117  				[]string{"route_creation", "disabled"},
   118  			))
   119  		})
   120  
   121  		Context("when an error occurs", func() {
   122  			BeforeEach(func() {
   123  				flagRepo.ListReturns(nil, errors.New("An error occurred."))
   124  			})
   125  
   126  			It("fails with an error", func() {
   127  				runCommand()
   128  				Expect(ui.Outputs()).To(ContainSubstrings(
   129  					[]string{"FAILED"},
   130  					[]string{"An error occurred."},
   131  				))
   132  			})
   133  		})
   134  	})
   135  })