github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/api/usermanager" 15 "github.com/juju/juju/apiserver/params" 16 "github.com/juju/juju/cmd/envcmd" 17 "github.com/juju/juju/cmd/juju/user" 18 "github.com/juju/juju/testing" 19 ) 20 21 // All of the functionality of the UserInfo api call is contained elsewhere. 22 // This suite provides basic tests for the "user info" command 23 type UserListCommandSuite struct { 24 BaseSuite 25 } 26 27 var _ = gc.Suite(&UserListCommandSuite{}) 28 29 func newUserListCommand() cmd.Command { 30 return envcmd.Wrap(user.NewListCommand(&fakeUserListAPI{})) 31 } 32 33 type fakeUserListAPI struct{} 34 35 func (*fakeUserListAPI) Close() error { 36 return nil 37 } 38 39 func (f *fakeUserListAPI) UserInfo(usernames []string, all usermanager.IncludeDisabled) ([]params.UserInfo, error) { 40 if len(usernames) > 0 { 41 return nil, errors.Errorf("expected no usernames, got %d", len(usernames)) 42 } 43 now := time.Now().UTC().Round(time.Second) 44 last1 := time.Date(2014, 1, 1, 0, 0, 0, 0, time.UTC) 45 // The extra two seconds here are needed to make sure 46 // we don't get intermittent failures in formatting. 47 last2 := now.Add(-35*time.Minute + -2*time.Second) 48 result := []params.UserInfo{ 49 { 50 Username: "adam", 51 DisplayName: "Adam Zulu", 52 DateCreated: time.Date(2012, 10, 8, 0, 0, 0, 0, time.UTC), 53 LastConnection: &last1, 54 }, { 55 Username: "barbara", 56 DisplayName: "Barbara Yellow", 57 DateCreated: time.Date(2013, 5, 2, 0, 0, 0, 0, time.UTC), 58 LastConnection: &now, 59 }, { 60 Username: "charlie", 61 DisplayName: "Charlie Xavier", 62 // The extra two minutes here are needed to make sure 63 // we don't get intermittent failures in formatting. 64 DateCreated: now.Add(-6*time.Hour + -2*time.Minute), 65 }, 66 } 67 if all { 68 result = append(result, params.UserInfo{ 69 Username: "davey", 70 DisplayName: "Davey Willow", 71 DateCreated: time.Date(2014, 10, 9, 0, 0, 0, 0, time.UTC), 72 LastConnection: &last2, 73 Disabled: true, 74 }) 75 } 76 return result, nil 77 } 78 79 func (s *UserListCommandSuite) TestUserInfo(c *gc.C) { 80 context, err := testing.RunCommand(c, newUserListCommand()) 81 c.Assert(err, jc.ErrorIsNil) 82 c.Assert(testing.Stdout(context), gc.Equals, ""+ 83 "NAME DISPLAY NAME DATE CREATED LAST CONNECTION\n"+ 84 "adam Adam Zulu 2012-10-08 2014-01-01\n"+ 85 "barbara Barbara Yellow 2013-05-02 just now\n"+ 86 "charlie Charlie Xavier 6 hours ago never connected\n"+ 87 "\n") 88 } 89 90 func (s *UserListCommandSuite) TestUserInfoWithDisabled(c *gc.C) { 91 context, err := testing.RunCommand(c, newUserListCommand(), "--all") 92 c.Assert(err, jc.ErrorIsNil) 93 c.Assert(testing.Stdout(context), gc.Equals, ""+ 94 "NAME DISPLAY NAME DATE CREATED LAST CONNECTION\n"+ 95 "adam Adam Zulu 2012-10-08 2014-01-01\n"+ 96 "barbara Barbara Yellow 2013-05-02 just now\n"+ 97 "charlie Charlie Xavier 6 hours ago never connected\n"+ 98 "davey Davey Willow 2014-10-09 35 minutes ago (disabled)\n"+ 99 "\n") 100 } 101 102 func (s *UserListCommandSuite) TestUserInfoExactTime(c *gc.C) { 103 context, err := testing.RunCommand(c, newUserListCommand(), "--exact-time") 104 c.Assert(err, jc.ErrorIsNil) 105 dateRegex := `\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \+0000 UTC` 106 c.Assert(testing.Stdout(context), gc.Matches, ""+ 107 "NAME DISPLAY NAME DATE CREATED LAST CONNECTION\n"+ 108 "adam Adam Zulu 2012-10-08 00:00:00 \\+0000 UTC 2014-01-01 00:00:00 \\+0000 UTC\n"+ 109 "barbara Barbara Yellow 2013-05-02 00:00:00 \\+0000 UTC "+dateRegex+"\n"+ 110 "charlie Charlie Xavier "+dateRegex+" never connected\n"+ 111 "\n") 112 } 113 114 func (*UserListCommandSuite) TestUserInfoFormatJson(c *gc.C) { 115 context, err := testing.RunCommand(c, newUserListCommand(), "--format", "json") 116 c.Assert(err, jc.ErrorIsNil) 117 c.Assert(testing.Stdout(context), gc.Equals, "["+ 118 `{"user-name":"adam","display-name":"Adam Zulu","date-created":"2012-10-08","last-connection":"2014-01-01"},`+ 119 `{"user-name":"barbara","display-name":"Barbara Yellow","date-created":"2013-05-02","last-connection":"just now"},`+ 120 `{"user-name":"charlie","display-name":"Charlie Xavier","date-created":"6 hours ago","last-connection":"never connected"}`+ 121 "]\n") 122 } 123 124 func (*UserListCommandSuite) TestUserInfoFormatYaml(c *gc.C) { 125 context, err := testing.RunCommand(c, newUserListCommand(), "--format", "yaml") 126 c.Assert(err, jc.ErrorIsNil) 127 c.Assert(testing.Stdout(context), gc.Equals, ""+ 128 "- user-name: adam\n"+ 129 " display-name: Adam Zulu\n"+ 130 " date-created: 2012-10-08\n"+ 131 " last-connection: 2014-01-01\n"+ 132 "- user-name: barbara\n"+ 133 " display-name: Barbara Yellow\n"+ 134 " date-created: 2013-05-02\n"+ 135 " last-connection: just now\n"+ 136 "- user-name: charlie\n"+ 137 " display-name: Charlie Xavier\n"+ 138 " date-created: 6 hours ago\n"+ 139 " last-connection: never connected\n") 140 } 141 142 func (*UserListCommandSuite) TestTooManyArgs(c *gc.C) { 143 _, err := testing.RunCommand(c, newUserListCommand(), "whoops") 144 c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`) 145 }