github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/cmdtesting"
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/cmd/juju/user"
    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) TestInit(c *gc.C) {
    37  	table := []struct {
    38  		args        []string
    39  		confirm     bool
    40  		errorString string
    41  	}{{
    42  		confirm:     false,
    43  		errorString: "no username supplied",
    44  	}, {
    45  		args:        []string{"--yes"},
    46  		confirm:     true,
    47  		errorString: "no username supplied",
    48  	}, {
    49  		args:    []string{"--yes", "jjam"},
    50  		confirm: true,
    51  	}}
    52  	for _, test := range table {
    53  		wrappedCommand, command := user.NewRemoveCommandForTest(s.mockAPI, s.store)
    54  		err := cmdtesting.InitCommand(wrappedCommand, test.args)
    55  		c.Check(command.ConfirmDelete, jc.DeepEquals, test.confirm)
    56  		if test.errorString == "" {
    57  			c.Check(err, jc.ErrorIsNil)
    58  		} else {
    59  			c.Check(err, gc.ErrorMatches, test.errorString)
    60  		}
    61  	}
    62  }
    63  
    64  func (s *RemoveUserCommandSuite) TestRemove(c *gc.C) {
    65  	username := "testing"
    66  	command, _ := user.NewRemoveCommandForTest(s.mockAPI, s.store)
    67  	_, err := cmdtesting.RunCommand(c, command, "-y", username)
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	c.Assert(s.mockAPI.username, gc.Equals, username)
    70  
    71  }
    72  
    73  func (s *RemoveUserCommandSuite) TestRemovePrompts(c *gc.C) {
    74  	username := "testing"
    75  	expected := `WARNING! This command will permanently archive the user "testing" on the "testing"
    76  controller.
    77  
    78  This action is irreversible. If you wish to temporarily disable the
    79  user please use the` + " `juju disable-user` " + `command. See
    80  ` + " `juju help disable-user` " + `for more details.
    81  
    82  Continue (y/N)? `
    83  	command, _ := user.NewRemoveCommandForTest(s.mockAPI, s.store)
    84  	ctx, _ := cmdtesting.RunCommand(c, command, username)
    85  	c.Assert(cmdtesting.Stdout(ctx), jc.DeepEquals, expected)
    86  
    87  }