github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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/envcmd"
    18  	"github.com/juju/juju/cmd/juju/user"
    19  	"github.com/juju/juju/testing"
    20  )
    21  
    22  var logger = loggo.GetLogger("juju.cmd.user.test")
    23  
    24  // All of the functionality of the UserInfo api call is contained elsewhere.
    25  // This suite provides basic tests for the "user info" command
    26  type UserInfoCommandSuite struct {
    27  	BaseSuite
    28  }
    29  
    30  var (
    31  	_ = gc.Suite(&UserInfoCommandSuite{})
    32  
    33  	// Mock out timestamps
    34  	dateCreated    = time.Unix(352138205, 0).UTC()
    35  	lastConnection = time.Unix(1388534400, 0).UTC()
    36  )
    37  
    38  func newUserInfoCommand() cmd.Command {
    39  	return envcmd.WrapSystem(user.NewInfoCommand(&fakeUserInfoAPI{}))
    40  }
    41  
    42  type fakeUserInfoAPI struct{}
    43  
    44  func (*fakeUserInfoAPI) Close() error {
    45  	return nil
    46  }
    47  
    48  func (*fakeUserInfoAPI) UserInfo(usernames []string, all usermanager.IncludeDisabled) ([]params.UserInfo, error) {
    49  	logger.Infof("fakeUserInfoAPI.UserInfo(%v, %v)", usernames, all)
    50  	info := params.UserInfo{
    51  		DateCreated:    dateCreated,
    52  		LastConnection: &lastConnection,
    53  	}
    54  	switch usernames[0] {
    55  	case "user-test":
    56  		info.Username = "user-test"
    57  	case "foobar":
    58  		info.Username = "foobar"
    59  		info.DisplayName = "Foo Bar"
    60  	default:
    61  		return nil, common.ErrPerm
    62  	}
    63  	return []params.UserInfo{info}, nil
    64  }
    65  
    66  func (s *UserInfoCommandSuite) TestUserInfo(c *gc.C) {
    67  	context, err := testing.RunCommand(c, newUserInfoCommand())
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	c.Assert(testing.Stdout(context), gc.Equals, `user-name: user-test
    70  display-name: ""
    71  date-created: 1981-02-27
    72  last-connection: 2014-01-01
    73  `)
    74  }
    75  
    76  func (s *UserInfoCommandSuite) TestUserInfoExactTime(c *gc.C) {
    77  	context, err := testing.RunCommand(c, newUserInfoCommand(), "--exact-time")
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	c.Assert(testing.Stdout(context), gc.Equals, `user-name: user-test
    80  display-name: ""
    81  date-created: 1981-02-27 16:10:05 +0000 UTC
    82  last-connection: 2014-01-01 00:00:00 +0000 UTC
    83  `)
    84  }
    85  
    86  func (s *UserInfoCommandSuite) TestUserInfoWithUsername(c *gc.C) {
    87  	context, err := testing.RunCommand(c, newUserInfoCommand(), "foobar")
    88  	c.Assert(err, jc.ErrorIsNil)
    89  	c.Assert(testing.Stdout(context), gc.Equals, `user-name: foobar
    90  display-name: Foo Bar
    91  date-created: 1981-02-27
    92  last-connection: 2014-01-01
    93  `)
    94  }
    95  
    96  func (*UserInfoCommandSuite) TestUserInfoUserDoesNotExist(c *gc.C) {
    97  	_, err := testing.RunCommand(c, newUserInfoCommand(), "barfoo")
    98  	c.Assert(err, gc.ErrorMatches, "permission denied")
    99  }
   100  
   101  func (*UserInfoCommandSuite) TestUserInfoFormatJson(c *gc.C) {
   102  	context, err := testing.RunCommand(c, newUserInfoCommand(), "--format", "json")
   103  	c.Assert(err, jc.ErrorIsNil)
   104  	c.Assert(testing.Stdout(context), gc.Equals, `
   105  {"user-name":"user-test","display-name":"","date-created":"1981-02-27","last-connection":"2014-01-01"}
   106  `[1:])
   107  }
   108  
   109  func (*UserInfoCommandSuite) TestUserInfoFormatJsonWithUsername(c *gc.C) {
   110  	context, err := testing.RunCommand(c, newUserInfoCommand(), "foobar", "--format", "json")
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	c.Assert(testing.Stdout(context), gc.Equals, `
   113  {"user-name":"foobar","display-name":"Foo Bar","date-created":"1981-02-27","last-connection":"2014-01-01"}
   114  `[1:])
   115  }
   116  
   117  func (*UserInfoCommandSuite) TestUserInfoFormatYaml(c *gc.C) {
   118  	context, err := testing.RunCommand(c, newUserInfoCommand(), "--format", "yaml")
   119  	c.Assert(err, jc.ErrorIsNil)
   120  	c.Assert(testing.Stdout(context), gc.Equals, `user-name: user-test
   121  display-name: ""
   122  date-created: 1981-02-27
   123  last-connection: 2014-01-01
   124  `)
   125  }
   126  
   127  func (*UserInfoCommandSuite) TestTooManyArgs(c *gc.C) {
   128  	_, err := testing.RunCommand(c, newUserInfoCommand(), "username", "whoops")
   129  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`)
   130  }
   131  
   132  type userFriendlyDurationSuite struct{}
   133  
   134  var _ = gc.Suite(&userFriendlyDurationSuite{})
   135  
   136  func (*userFriendlyDurationSuite) TestFormat(c *gc.C) {
   137  	now := time.Now()
   138  	for _, test := range []struct {
   139  		other    time.Time
   140  		expected string
   141  	}{
   142  		{
   143  			other:    now,
   144  			expected: "just now",
   145  		}, {
   146  			other:    now.Add(-1 * time.Second),
   147  			expected: "just now",
   148  		}, {
   149  			other:    now.Add(-2 * time.Second),
   150  			expected: "2 seconds ago",
   151  		}, {
   152  			other:    now.Add(-59 * time.Second),
   153  			expected: "59 seconds ago",
   154  		}, {
   155  			other:    now.Add(-60 * time.Second),
   156  			expected: "1 minute ago",
   157  		}, {
   158  			other:    now.Add(-61 * time.Second),
   159  			expected: "1 minute ago",
   160  		}, {
   161  			other:    now.Add(-2 * time.Minute),
   162  			expected: "2 minutes ago",
   163  		}, {
   164  			other:    now.Add(-59 * time.Minute),
   165  			expected: "59 minutes ago",
   166  		}, {
   167  			other:    now.Add(-60 * time.Minute),
   168  			expected: "1 hour ago",
   169  		}, {
   170  			other:    now.Add(-61 * time.Minute),
   171  			expected: "1 hour ago",
   172  		}, {
   173  			other:    now.Add(-2 * time.Hour),
   174  			expected: "2 hours ago",
   175  		}, {
   176  			other:    now.Add(-23 * time.Hour),
   177  			expected: "23 hours ago",
   178  		}, {
   179  			other:    now.Add(-24 * time.Hour),
   180  			expected: now.Add(-24 * time.Hour).Format("2006-01-02"),
   181  		}, {
   182  			other:    now.Add(-96 * time.Hour),
   183  			expected: now.Add(-96 * time.Hour).Format("2006-01-02"),
   184  		},
   185  	} {
   186  		obtained := user.UserFriendlyDuration(test.other, now)
   187  		c.Check(obtained, gc.Equals, test.expected)
   188  	}
   189  }