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