github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/user/remove_test.go (about) 1 // Copyright 2012-2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 package user_test 4 5 import ( 6 "github.com/juju/cmd" 7 "github.com/juju/juju/cmd/juju/user" 8 "github.com/juju/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 ) 12 13 type RemoveUserCommandSuite struct { 14 BaseSuite 15 mockAPI *mockRemoveUserAPI 16 } 17 18 var _ = gc.Suite(&RemoveUserCommandSuite{}) 19 20 func (s *RemoveUserCommandSuite) SetUpTest(c *gc.C) { 21 s.BaseSuite.SetUpTest(c) 22 s.mockAPI = &mockRemoveUserAPI{} 23 } 24 25 type mockRemoveUserAPI struct { 26 username string 27 } 28 29 func (*mockRemoveUserAPI) Close() error { return nil } 30 31 func (m *mockRemoveUserAPI) RemoveUser(username string) error { 32 m.username = username 33 return nil 34 } 35 36 func (s *RemoveUserCommandSuite) run(c *gc.C, name string) (*cmd.Context, error) { 37 removeCommand, _ := user.NewRemoveCommandForTest(s.mockAPI, s.store) 38 return testing.RunCommand(c, removeCommand, name) 39 } 40 41 func (s *RemoveUserCommandSuite) TestInit(c *gc.C) { 42 table := []struct { 43 args []string 44 confirm bool 45 errorString string 46 }{{ 47 confirm: false, 48 errorString: "no username supplied", 49 }, { 50 args: []string{"--yes"}, 51 confirm: true, 52 errorString: "no username supplied", 53 }, { 54 args: []string{"--yes", "jjam"}, 55 confirm: true, 56 }} 57 for _, test := range table { 58 wrappedCommand, command := user.NewRemoveCommandForTest(s.mockAPI, s.store) 59 err := testing.InitCommand(wrappedCommand, test.args) 60 c.Check(command.ConfirmDelete, jc.DeepEquals, test.confirm) 61 if test.errorString == "" { 62 c.Check(err, jc.ErrorIsNil) 63 } else { 64 c.Check(err, gc.ErrorMatches, test.errorString) 65 } 66 } 67 } 68 69 func (s *RemoveUserCommandSuite) TestRemove(c *gc.C) { 70 username := "testing" 71 command, _ := user.NewRemoveCommandForTest(s.mockAPI, s.store) 72 _, err := testing.RunCommand(c, command, "-y", username) 73 c.Assert(err, jc.ErrorIsNil) 74 c.Assert(s.mockAPI.username, gc.Equals, username) 75 76 } 77 78 func (s *RemoveUserCommandSuite) TestRemovePrompts(c *gc.C) { 79 username := "testing" 80 expected := ` 81 WARNING! This command will remove the user "testing" from the "testing" controller. 82 83 Continue (y/N)? `[1:] 84 command, _ := user.NewRemoveCommandForTest(s.mockAPI, s.store) 85 ctx, _ := testing.RunCommand(c, command, username) 86 c.Assert(testing.Stdout(ctx), jc.DeepEquals, expected) 87 88 }