github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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 case "foobar": 57 info.Username = "foobar" 58 info.DisplayName = "Foo Bar" 59 default: 60 return nil, common.ErrPerm 61 } 62 return []params.UserInfo{info}, nil 63 } 64 65 func (s *UserInfoCommandSuite) TestUserInfo(c *gc.C) { 66 context, err := testing.RunCommand(c, s.NewShowUserCommand()) 67 c.Assert(err, jc.ErrorIsNil) 68 c.Assert(testing.Stdout(context), gc.Equals, `user-name: current-user 69 display-name: "" 70 date-created: 1981-02-27 71 last-connection: 2014-01-01 72 `) 73 } 74 75 func (s *UserInfoCommandSuite) TestUserInfoExactTime(c *gc.C) { 76 context, err := testing.RunCommand(c, s.NewShowUserCommand(), "--exact-time") 77 c.Assert(err, jc.ErrorIsNil) 78 c.Assert(testing.Stdout(context), gc.Equals, `user-name: current-user 79 display-name: "" 80 date-created: 1981-02-27 16:10:05 +0000 UTC 81 last-connection: 2014-01-01 00:00:00 +0000 UTC 82 `) 83 } 84 85 func (s *UserInfoCommandSuite) TestUserInfoWithUsername(c *gc.C) { 86 context, err := testing.RunCommand(c, s.NewShowUserCommand(), "foobar") 87 c.Assert(err, jc.ErrorIsNil) 88 c.Assert(testing.Stdout(context), gc.Equals, `user-name: foobar 89 display-name: Foo Bar 90 date-created: 1981-02-27 91 last-connection: 2014-01-01 92 `) 93 } 94 95 func (s *UserInfoCommandSuite) TestUserInfoUserDoesNotExist(c *gc.C) { 96 _, err := testing.RunCommand(c, s.NewShowUserCommand(), "barfoo") 97 c.Assert(err, gc.ErrorMatches, "permission denied") 98 } 99 100 func (s *UserInfoCommandSuite) TestUserInfoFormatJson(c *gc.C) { 101 context, err := testing.RunCommand(c, s.NewShowUserCommand(), "--format", "json") 102 c.Assert(err, jc.ErrorIsNil) 103 c.Assert(testing.Stdout(context), gc.Equals, ` 104 {"user-name":"current-user","display-name":"","date-created":"1981-02-27","last-connection":"2014-01-01"} 105 `[1:]) 106 } 107 108 func (s *UserInfoCommandSuite) TestUserInfoFormatJsonWithUsername(c *gc.C) { 109 context, err := testing.RunCommand(c, s.NewShowUserCommand(), "foobar", "--format", "json") 110 c.Assert(err, jc.ErrorIsNil) 111 c.Assert(testing.Stdout(context), gc.Equals, ` 112 {"user-name":"foobar","display-name":"Foo Bar","date-created":"1981-02-27","last-connection":"2014-01-01"} 113 `[1:]) 114 } 115 116 func (s *UserInfoCommandSuite) TestUserInfoFormatYaml(c *gc.C) { 117 context, err := testing.RunCommand(c, s.NewShowUserCommand(), "--format", "yaml") 118 c.Assert(err, jc.ErrorIsNil) 119 c.Assert(testing.Stdout(context), gc.Equals, `user-name: current-user 120 display-name: "" 121 date-created: 1981-02-27 122 last-connection: 2014-01-01 123 `) 124 } 125 126 func (s *UserInfoCommandSuite) TestTooManyArgs(c *gc.C) { 127 _, err := testing.RunCommand(c, s.NewShowUserCommand(), "username", "whoops") 128 c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`) 129 }