github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/featuretests/cmd_juju_user_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package featuretests
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/names"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/cmd/envcmd"
    15  	"github.com/juju/juju/cmd/juju/user"
    16  	jujutesting "github.com/juju/juju/juju/testing"
    17  	"github.com/juju/juju/testing"
    18  	"github.com/juju/juju/testing/factory"
    19  )
    20  
    21  // UserSuite tests the connectivity of all the user subcommands. These tests
    22  // go from the command line, api client, api server, db. The db changes are
    23  // then checked.  Only one test for each command is done here to check
    24  // connectivity.  Exhaustive tests are at each layer.
    25  type UserSuite struct {
    26  	jujutesting.JujuConnSuite
    27  }
    28  
    29  var _ = gc.Suite(&UserSuite{})
    30  
    31  func (s *UserSuite) SetUpTest(c *gc.C) {
    32  	s.JujuConnSuite.SetUpTest(c)
    33  	envcmd.WriteCurrentEnvironment("dummyenv")
    34  }
    35  
    36  func (s *UserSuite) RunUserCommand(c *gc.C, args ...string) (*cmd.Context, error) {
    37  	command := user.NewSuperCommand()
    38  	return testing.RunCommand(c, command, args...)
    39  }
    40  
    41  func (s *UserSuite) TestUserAdd(c *gc.C) {
    42  	ctx, err := s.RunUserCommand(c, "add", "test")
    43  	c.Assert(err, jc.ErrorIsNil)
    44  	c.Assert(testing.Stderr(ctx), jc.HasPrefix, `user "test" added`)
    45  	user, err := s.State.User(names.NewLocalUserTag("test"))
    46  	c.Assert(err, jc.ErrorIsNil)
    47  	c.Assert(user.IsDisabled(), jc.IsFalse)
    48  }
    49  
    50  func (s *UserSuite) TestUserChangePassword(c *gc.C) {
    51  	user, err := s.State.User(s.AdminUserTag(c))
    52  	c.Assert(err, jc.ErrorIsNil)
    53  	c.Assert(user.PasswordValid("dummy-secret"), jc.IsTrue)
    54  	_, err = s.RunUserCommand(c, "change-password", "--generate")
    55  	c.Assert(err, jc.ErrorIsNil)
    56  	user.Refresh()
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	c.Assert(user.PasswordValid("dummy-secret"), jc.IsFalse)
    59  }
    60  
    61  func (s *UserSuite) TestUserInfo(c *gc.C) {
    62  	user, err := s.State.User(s.AdminUserTag(c))
    63  	c.Assert(err, jc.ErrorIsNil)
    64  	c.Assert(user.PasswordValid("dummy-secret"), jc.IsTrue)
    65  	ctx, err := s.RunUserCommand(c, "info")
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	c.Assert(testing.Stdout(ctx), jc.Contains, "user-name: dummy-admin")
    68  }
    69  
    70  func (s *UserSuite) TestUserDisable(c *gc.C) {
    71  	user := s.Factory.MakeUser(c, &factory.UserParams{Name: "barbara"})
    72  	_, err := s.RunUserCommand(c, "disable", "barbara")
    73  	c.Assert(err, jc.ErrorIsNil)
    74  	user.Refresh()
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	c.Assert(user.IsDisabled(), jc.IsTrue)
    77  }
    78  
    79  func (s *UserSuite) TestUserEnable(c *gc.C) {
    80  	user := s.Factory.MakeUser(c, &factory.UserParams{Name: "barbara", Disabled: true})
    81  	_, err := s.RunUserCommand(c, "enable", "barbara")
    82  	c.Assert(err, jc.ErrorIsNil)
    83  	user.Refresh()
    84  	c.Assert(err, jc.ErrorIsNil)
    85  	c.Assert(user.IsDisabled(), jc.IsFalse)
    86  }
    87  
    88  func (s *UserSuite) TestUserList(c *gc.C) {
    89  	ctx, err := s.RunUserCommand(c, "list")
    90  	c.Assert(err, jc.ErrorIsNil)
    91  	periodPattern := `(just now|\d+ \S+ ago)`
    92  	expected := fmt.Sprintf(`
    93  NAME\s+DISPLAY NAME\s+DATE CREATED\s+LAST CONNECTION
    94  dummy-admin\s+dummy-admin\s+%s\s+%s
    95  
    96  `[1:], periodPattern, periodPattern)
    97  	c.Assert(testing.Stdout(ctx), gc.Matches, expected)
    98  }