github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/securitygroup/update_security_group_test.go (about) 1 package securitygroup_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 7 "code.cloudfoundry.org/cli/cf/api/securitygroups/securitygroupsfakes" 8 "code.cloudfoundry.org/cli/cf/commandregistry" 9 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 10 "code.cloudfoundry.org/cli/cf/errors" 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 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 17 18 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 19 . "github.com/onsi/ginkgo" 20 . "github.com/onsi/gomega" 21 ) 22 23 var _ = Describe("update-security-group command", func() { 24 var ( 25 ui *testterm.FakeUI 26 securityGroupRepo *securitygroupsfakes.FakeSecurityGroupRepo 27 requirementsFactory *requirementsfakes.FakeFactory 28 configRepo coreconfig.Repository 29 deps commandregistry.Dependency 30 ) 31 32 updateCommandDependency := func(pluginCall bool) { 33 deps.UI = ui 34 deps.RepoLocator = deps.RepoLocator.SetSecurityGroupRepository(securityGroupRepo) 35 deps.Config = configRepo 36 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("update-security-group").SetDependency(deps, pluginCall)) 37 } 38 39 BeforeEach(func() { 40 ui = &testterm.FakeUI{} 41 requirementsFactory = new(requirementsfakes.FakeFactory) 42 securityGroupRepo = new(securitygroupsfakes.FakeSecurityGroupRepo) 43 configRepo = testconfig.NewRepositoryWithDefaults() 44 }) 45 46 runCommand := func(args ...string) bool { 47 return testcmd.RunCLICommand("update-security-group", args, requirementsFactory, updateCommandDependency, false, ui) 48 } 49 50 Describe("requirements", func() { 51 It("fails when the user is not logged in", func() { 52 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 53 Expect(runCommand("the-security-group")).To(BeFalse()) 54 }) 55 56 It("fails with usage when a name is not provided", func() { 57 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 58 runCommand() 59 Expect(ui.Outputs()).To(ContainSubstrings( 60 []string{"Incorrect Usage", "Requires", "arguments"}, 61 )) 62 }) 63 64 It("fails with usage when a file path is not provided", func() { 65 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 66 runCommand("my-group-name") 67 Expect(ui.Outputs()).To(ContainSubstrings( 68 []string{"Incorrect Usage", "Requires", "arguments"}, 69 )) 70 }) 71 }) 72 73 Context("when the user is logged in", func() { 74 var tempFile *os.File 75 BeforeEach(func() { 76 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 77 securityGroup := models.SecurityGroup{ 78 SecurityGroupFields: models.SecurityGroupFields{ 79 Name: "my-group-name", 80 GUID: "my-group-guid", 81 }, 82 } 83 securityGroupRepo.ReadReturns(securityGroup, nil) 84 tempFile, _ = ioutil.TempFile("", "") 85 }) 86 87 AfterEach(func() { 88 tempFile.Close() 89 os.Remove(tempFile.Name()) 90 }) 91 92 JustBeforeEach(func() { 93 runCommand("my-group-name", tempFile.Name()) 94 }) 95 96 Context("when the file specified has valid json", func() { 97 BeforeEach(func() { 98 tempFile.Write([]byte(`[{"protocol":"udp","port":"8080-9090","destination":"198.41.191.47/1"}]`)) 99 }) 100 101 It("displays a message describing what its going to do", func() { 102 Expect(ui.Outputs()).To(ContainSubstrings( 103 []string{"Updating security group", "my-group-name", "my-user"}, 104 []string{"OK"}, 105 []string{"TIP: Changes will not apply to existing running applications until they are restarted."}, 106 )) 107 }) 108 109 It("updates the security group with those rules, obviously", func() { 110 jsonData := []map[string]interface{}{ 111 {"protocol": "udp", "port": "8080-9090", "destination": "198.41.191.47/1"}, 112 } 113 114 _, jsonArg := securityGroupRepo.UpdateArgsForCall(0) 115 116 Expect(jsonArg).To(Equal(jsonData)) 117 }) 118 119 Context("when the API returns an error", func() { 120 Context("some sort of awful terrible error that we were not prescient enough to anticipate", func() { 121 BeforeEach(func() { 122 securityGroupRepo.UpdateReturns(errors.New("Wops I failed")) 123 }) 124 125 It("fails loudly", func() { 126 Expect(ui.Outputs()).To(ContainSubstrings( 127 []string{"Updating security group", "my-group-name"}, 128 []string{"FAILED"}, 129 )) 130 }) 131 }) 132 }) 133 134 Context("when the file specified has invalid json", func() { 135 BeforeEach(func() { 136 tempFile.Write([]byte(`[{noquote: thiswontwork}]`)) 137 }) 138 139 It("freaks out", func() { 140 Expect(ui.Outputs()).To(ContainSubstrings( 141 []string{"FAILED"}, 142 )) 143 }) 144 }) 145 }) 146 }) 147 })