github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/user/info_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for info.
     3  
     4  package user_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/loggo"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/api/usermanager"
    15  	"github.com/juju/juju/apiserver/common"
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/cmd/juju/user"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  var logger = loggo.GetLogger("juju.cmd.user.test")
    22  
    23  // All of the functionality of the UserInfo api call is contained elsewhere.
    24  // This suite provides basic tests for the "show-user" command
    25  type UserInfoCommandSuite struct {
    26  	BaseSuite
    27  }
    28  
    29  var (
    30  	_ = gc.Suite(&UserInfoCommandSuite{})
    31  
    32  	// Mock out timestamps
    33  	dateCreated    = time.Unix(352138205, 0).UTC()
    34  	lastConnection = time.Unix(1388534400, 0).UTC()
    35  )
    36  
    37  func (s *UserInfoCommandSuite) NewShowUserCommand() cmd.Command {
    38  	return user.NewShowUserCommandForTest(&fakeUserInfoAPI{}, s.store)
    39  }
    40  
    41  type fakeUserInfoAPI struct{}
    42  
    43  func (*fakeUserInfoAPI) Close() error {
    44  	return nil
    45  }
    46  
    47  func (*fakeUserInfoAPI) UserInfo(usernames []string, all usermanager.IncludeDisabled) ([]params.UserInfo, error) {
    48  	logger.Infof("fakeUserInfoAPI.UserInfo(%v, %v)", usernames, all)
    49  	info := params.UserInfo{
    50  		DateCreated:    dateCreated,
    51  		LastConnection: &lastConnection,
    52  	}
    53  	switch usernames[0] {
    54  	case "current-user@local":
    55  		info.Username = "current-user"
    56  		info.Access = "addmodel"
    57  	case "foobar":
    58  		info.Username = "foobar"
    59  		info.DisplayName = "Foo Bar"
    60  		info.Access = "login"
    61  	case "fred@external":
    62  		info.Username = "fred@external"
    63  		info.DisplayName = "Fred External"
    64  		info.Access = "addmodel"
    65  	default:
    66  		return nil, common.ErrPerm
    67  	}
    68  	return []params.UserInfo{info}, nil
    69  }
    70  
    71  func (s *UserInfoCommandSuite) TestUserInfo(c *gc.C) {
    72  	context, err := testing.RunCommand(c, s.NewShowUserCommand())
    73  	c.Assert(err, jc.ErrorIsNil)
    74  	c.Assert(testing.Stdout(context), gc.Equals, `user-name: current-user
    75  access: addmodel
    76  date-created: 1981-02-27
    77  last-connection: 2014-01-01
    78  `)
    79  }
    80  
    81  func (s *UserInfoCommandSuite) TestUserInfoExactTime(c *gc.C) {
    82  	context, err := testing.RunCommand(c, s.NewShowUserCommand(), "--exact-time")
    83  	c.Assert(err, jc.ErrorIsNil)
    84  	c.Assert(testing.Stdout(context), gc.Equals, `user-name: current-user
    85  access: addmodel
    86  date-created: 1981-02-27 16:10:05 +0000 UTC
    87  last-connection: 2014-01-01 00:00:00 +0000 UTC
    88  `)
    89  }
    90  
    91  func (s *UserInfoCommandSuite) TestUserInfoWithUsername(c *gc.C) {
    92  	context, err := testing.RunCommand(c, s.NewShowUserCommand(), "foobar")
    93  	c.Assert(err, jc.ErrorIsNil)
    94  	c.Assert(testing.Stdout(context), gc.Equals, `user-name: foobar
    95  display-name: Foo Bar
    96  access: login
    97  date-created: 1981-02-27
    98  last-connection: 2014-01-01
    99  `)
   100  }
   101  
   102  func (s *UserInfoCommandSuite) TestUserInfoExternalUser(c *gc.C) {
   103  	context, err := testing.RunCommand(c, s.NewShowUserCommand(), "fred@external")
   104  	c.Assert(err, jc.ErrorIsNil)
   105  	c.Assert(testing.Stdout(context), gc.Equals, `user-name: fred@external
   106  display-name: Fred External
   107  access: addmodel
   108  `)
   109  }
   110  
   111  func (s *UserInfoCommandSuite) TestUserInfoUserDoesNotExist(c *gc.C) {
   112  	_, err := testing.RunCommand(c, s.NewShowUserCommand(), "barfoo")
   113  	c.Assert(err, gc.ErrorMatches, "permission denied")
   114  }
   115  
   116  func (s *UserInfoCommandSuite) TestUserInfoFormatJson(c *gc.C) {
   117  	context, err := testing.RunCommand(c, s.NewShowUserCommand(), "--format", "json")
   118  	c.Assert(err, jc.ErrorIsNil)
   119  	c.Assert(testing.Stdout(context), gc.Equals, `
   120  {"user-name":"current-user","access":"addmodel","date-created":"1981-02-27","last-connection":"2014-01-01"}
   121  `[1:])
   122  }
   123  
   124  func (s *UserInfoCommandSuite) TestUserInfoFormatJsonWithUsername(c *gc.C) {
   125  	context, err := testing.RunCommand(c, s.NewShowUserCommand(), "foobar", "--format", "json")
   126  	c.Assert(err, jc.ErrorIsNil)
   127  	c.Assert(testing.Stdout(context), gc.Equals, `
   128  {"user-name":"foobar","display-name":"Foo Bar","access":"login","date-created":"1981-02-27","last-connection":"2014-01-01"}
   129  `[1:])
   130  }
   131  
   132  func (s *UserInfoCommandSuite) TestUserInfoFormatYaml(c *gc.C) {
   133  	context, err := testing.RunCommand(c, s.NewShowUserCommand(), "--format", "yaml")
   134  	c.Assert(err, jc.ErrorIsNil)
   135  	c.Assert(testing.Stdout(context), gc.Equals, `user-name: current-user
   136  access: addmodel
   137  date-created: 1981-02-27
   138  last-connection: 2014-01-01
   139  `)
   140  }
   141  
   142  func (s *UserInfoCommandSuite) TestTooManyArgs(c *gc.C) {
   143  	_, err := testing.RunCommand(c, s.NewShowUserCommand(), "username", "whoops")
   144  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`)
   145  }