github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/user/list_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/errors"
    11  	jc "github.com/juju/testing/checkers"
    12  	"github.com/juju/utils/clock"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/api/usermanager"
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/cmd/juju/user"
    18  	"github.com/juju/juju/jujuclient"
    19  	"github.com/juju/juju/testing"
    20  )
    21  
    22  // All of the functionality of the UserInfo api call is contained elsewhere.
    23  // This suite provides basic tests for the "show-user" command
    24  type UserListCommandSuite struct {
    25  	BaseSuite
    26  
    27  	clock fakeClock
    28  }
    29  
    30  var _ = gc.Suite(&UserListCommandSuite{})
    31  
    32  func (s *UserListCommandSuite) newUserListCommand() cmd.Command {
    33  	clock := &fakeClock{now: time.Date(2016, 9, 15, 12, 0, 0, 0, time.UTC)}
    34  	api := &fakeUserListAPI{clock}
    35  	return user.NewListCommandForTest(api, api, s.store, clock)
    36  }
    37  
    38  type fakeUserListAPI struct {
    39  	clock *fakeClock
    40  }
    41  
    42  func (*fakeUserListAPI) Close() error {
    43  	return nil
    44  }
    45  
    46  type fakeClock struct {
    47  	clock.Clock
    48  	now time.Time
    49  }
    50  
    51  func (f *fakeClock) Now() time.Time {
    52  	return f.now
    53  }
    54  
    55  func (f *fakeUserListAPI) ModelUserInfo() ([]params.ModelUserInfo, error) {
    56  	last1 := time.Date(2015, 3, 20, 0, 0, 0, 0, time.UTC)
    57  	last2 := time.Date(2015, 3, 1, 0, 0, 0, 0, time.UTC)
    58  
    59  	userlist := []params.ModelUserInfo{
    60  		{
    61  			UserName:       "admin@local",
    62  			LastConnection: &last1,
    63  			Access:         "write",
    64  		}, {
    65  			UserName:       "adam@local",
    66  			DisplayName:    "Adam",
    67  			LastConnection: &last2,
    68  			Access:         "read",
    69  		}, {
    70  			UserName:    "charlie@ubuntu.com",
    71  			DisplayName: "Charlie",
    72  			Access:      "read",
    73  		},
    74  	}
    75  	return userlist, nil
    76  }
    77  
    78  func (f *fakeUserListAPI) UserInfo(usernames []string, all usermanager.IncludeDisabled) ([]params.UserInfo, error) {
    79  	if len(usernames) > 0 {
    80  		return nil, errors.Errorf("expected no usernames, got %d", len(usernames))
    81  	}
    82  	now := f.clock.Now()
    83  	last1 := time.Date(2014, 1, 1, 0, 0, 0, 0, time.UTC)
    84  	last2 := now.Add(-35 * time.Minute)
    85  	result := []params.UserInfo{
    86  		{
    87  			Username:       "adam",
    88  			DisplayName:    "Adam Zulu",
    89  			Access:         "login",
    90  			DateCreated:    time.Date(2012, 10, 8, 0, 0, 0, 0, time.UTC),
    91  			LastConnection: &last1,
    92  		}, {
    93  			Username:       "barbara",
    94  			DisplayName:    "Barbara Yellow",
    95  			Access:         "addmodel",
    96  			DateCreated:    time.Date(2013, 5, 2, 0, 0, 0, 0, time.UTC),
    97  			LastConnection: &now,
    98  		}, {
    99  			Username:    "charlie",
   100  			DisplayName: "Charlie Xavier",
   101  			Access:      "superuser",
   102  			DateCreated: now.Add(-6 * time.Hour),
   103  		},
   104  	}
   105  	if all {
   106  		result = append(result, params.UserInfo{
   107  			Username:       "davey",
   108  			DisplayName:    "Davey Willow",
   109  			DateCreated:    time.Date(2014, 10, 9, 0, 0, 0, 0, time.UTC),
   110  			LastConnection: &last2,
   111  			Disabled:       true,
   112  		})
   113  	}
   114  	return result, nil
   115  }
   116  
   117  func (s *UserListCommandSuite) SetUpTest(c *gc.C) {
   118  	s.BaseSuite.SetUpTest(c)
   119  	s.store.Accounts["testing"] = jujuclient.AccountDetails{
   120  		User:     "adam@local",
   121  		Password: "password",
   122  	}
   123  }
   124  
   125  func (s *UserListCommandSuite) TestUserInfo(c *gc.C) {
   126  	context, err := testing.RunCommand(c, s.newUserListCommand())
   127  	c.Assert(err, jc.ErrorIsNil)
   128  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   129  		"CONTROLLER: testing\n\n"+
   130  		"NAME     DISPLAY NAME    ACCESS     DATE CREATED  LAST CONNECTION\n"+
   131  		"adam*    Adam Zulu       login      2012-10-08    2014-01-01\n"+
   132  		"barbara  Barbara Yellow  addmodel   2013-05-02    just now\n"+
   133  		"charlie  Charlie Xavier  superuser  6 hours ago   never connected\n"+
   134  		"\n")
   135  }
   136  
   137  func (s *UserListCommandSuite) TestUserInfoWithDisabled(c *gc.C) {
   138  	context, err := testing.RunCommand(c, s.newUserListCommand(), "--all")
   139  	c.Assert(err, jc.ErrorIsNil)
   140  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   141  		"CONTROLLER: testing\n\n"+
   142  		"NAME     DISPLAY NAME    ACCESS     DATE CREATED  LAST CONNECTION\n"+
   143  		"adam*    Adam Zulu       login      2012-10-08    2014-01-01\n"+
   144  		"barbara  Barbara Yellow  addmodel   2013-05-02    just now\n"+
   145  		"charlie  Charlie Xavier  superuser  6 hours ago   never connected\n"+
   146  		"davey    Davey Willow               2014-10-09    35 minutes ago (disabled)\n"+
   147  		"\n")
   148  }
   149  
   150  func (s *UserListCommandSuite) TestUserInfoExactTime(c *gc.C) {
   151  	context, err := testing.RunCommand(c, s.newUserListCommand(), "--exact-time")
   152  	c.Assert(err, jc.ErrorIsNil)
   153  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   154  		"CONTROLLER: testing\n\n"+
   155  		"NAME     DISPLAY NAME    ACCESS     DATE CREATED                   LAST CONNECTION\n"+
   156  		"adam*    Adam Zulu       login      2012-10-08 00:00:00 +0000 UTC  2014-01-01 00:00:00 +0000 UTC\n"+
   157  		"barbara  Barbara Yellow  addmodel   2013-05-02 00:00:00 +0000 UTC  2016-09-15 12:00:00 +0000 UTC\n"+
   158  		"charlie  Charlie Xavier  superuser  2016-09-15 06:00:00 +0000 UTC  never connected\n"+
   159  		"\n")
   160  }
   161  
   162  func (s *UserListCommandSuite) TestUserInfoFormatJson(c *gc.C) {
   163  	context, err := testing.RunCommand(c, s.newUserListCommand(), "--format", "json")
   164  	c.Assert(err, jc.ErrorIsNil)
   165  	c.Assert(testing.Stdout(context), gc.Equals, "["+
   166  		`{"user-name":"adam","display-name":"Adam Zulu","access":"login","date-created":"2012-10-08","last-connection":"2014-01-01"},`+
   167  		`{"user-name":"barbara","display-name":"Barbara Yellow","access":"addmodel","date-created":"2013-05-02","last-connection":"just now"},`+
   168  		`{"user-name":"charlie","display-name":"Charlie Xavier","access":"superuser","date-created":"6 hours ago","last-connection":"never connected"}`+
   169  		"]\n")
   170  }
   171  
   172  func (s *UserListCommandSuite) TestUserInfoFormatYaml(c *gc.C) {
   173  	context, err := testing.RunCommand(c, s.newUserListCommand(), "--format", "yaml")
   174  	c.Assert(err, jc.ErrorIsNil)
   175  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   176  		"- user-name: adam\n"+
   177  		"  display-name: Adam Zulu\n"+
   178  		"  access: login\n"+
   179  		"  date-created: 2012-10-08\n"+
   180  		"  last-connection: 2014-01-01\n"+
   181  		"- user-name: barbara\n"+
   182  		"  display-name: Barbara Yellow\n"+
   183  		"  access: addmodel\n"+
   184  		"  date-created: 2013-05-02\n"+
   185  		"  last-connection: just now\n"+
   186  		"- user-name: charlie\n"+
   187  		"  display-name: Charlie Xavier\n"+
   188  		"  access: superuser\n"+
   189  		"  date-created: 6 hours ago\n"+
   190  		"  last-connection: never connected\n")
   191  }
   192  
   193  func (s *UserListCommandSuite) TestModelUsers(c *gc.C) {
   194  	context, err := testing.RunCommand(c, s.newUserListCommand(), "admin")
   195  	c.Assert(err, jc.ErrorIsNil)
   196  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   197  		"NAME                DISPLAY NAME  ACCESS  LAST CONNECTION\n"+
   198  		"adam@local*         Adam          read    2015-03-01\n"+
   199  		"admin@local                       write   2015-03-20\n"+
   200  		"charlie@ubuntu.com  Charlie       read    never connected\n"+
   201  		"\n")
   202  }
   203  
   204  func (s *UserListCommandSuite) TestModelUsersFormatJson(c *gc.C) {
   205  	context, err := testing.RunCommand(c, s.newUserListCommand(), "admin", "--format", "json")
   206  	c.Assert(err, jc.ErrorIsNil)
   207  	c.Assert(testing.Stdout(context), gc.Equals, "{"+
   208  		`"adam@local":{"display-name":"Adam","access":"read","last-connection":"2015-03-01"},`+
   209  		`"admin@local":{"access":"write","last-connection":"2015-03-20"},`+
   210  		`"charlie@ubuntu.com":{"display-name":"Charlie","access":"read","last-connection":"never connected"}`+
   211  		"}\n")
   212  }
   213  
   214  func (s *UserListCommandSuite) TestModelUsersInfoFormatYaml(c *gc.C) {
   215  	context, err := testing.RunCommand(c, s.newUserListCommand(), "admin", "--format", "yaml")
   216  	c.Assert(err, jc.ErrorIsNil)
   217  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   218  		"adam@local:\n"+
   219  		"  display-name: Adam\n"+
   220  		"  access: read\n"+
   221  		"  last-connection: 2015-03-01\n"+
   222  		"admin@local:\n"+
   223  		"  access: write\n"+
   224  		"  last-connection: 2015-03-20\n"+
   225  		"charlie@ubuntu.com:\n"+
   226  		"  display-name: Charlie\n"+
   227  		"  access: read\n"+
   228  		"  last-connection: never connected\n")
   229  }
   230  
   231  func (s *UserListCommandSuite) TestTooManyArgs(c *gc.C) {
   232  	_, err := testing.RunCommand(c, s.newUserListCommand(), "model", "whoops")
   233  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`)
   234  }