github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/environment/users_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for info.
     3  
     4  package environment_test
     5  
     6  import (
     7  	"time"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/cmd/juju/environment"
    14  	"github.com/juju/juju/testing"
    15  )
    16  
    17  type UsersCommandSuite struct {
    18  	fake *fakeEnvUsersClient
    19  }
    20  
    21  var _ = gc.Suite(&UsersCommandSuite{})
    22  
    23  type fakeEnvUsersClient struct {
    24  	users []params.EnvUserInfo
    25  }
    26  
    27  func (f *fakeEnvUsersClient) Close() error {
    28  	return nil
    29  }
    30  
    31  func (f *fakeEnvUsersClient) EnvironmentUserInfo() ([]params.EnvUserInfo, error) {
    32  	return f.users, nil
    33  }
    34  
    35  func (s *UsersCommandSuite) SetUpTest(c *gc.C) {
    36  	last1 := time.Date(2015, 3, 20, 0, 0, 0, 0, time.UTC)
    37  	last2 := time.Date(2015, 3, 1, 0, 0, 0, 0, time.UTC)
    38  
    39  	userlist := []params.EnvUserInfo{
    40  		{
    41  			UserName:       "admin@local",
    42  			DisplayName:    "admin",
    43  			CreatedBy:      "admin@local",
    44  			DateCreated:    time.Date(2014, 7, 20, 9, 0, 0, 0, time.UTC),
    45  			LastConnection: &last1,
    46  		}, {
    47  			UserName:       "bob@local",
    48  			DisplayName:    "Bob",
    49  			CreatedBy:      "admin@local",
    50  			DateCreated:    time.Date(2015, 2, 15, 9, 0, 0, 0, time.UTC),
    51  			LastConnection: &last2,
    52  		}, {
    53  			UserName:    "charlie@ubuntu.com",
    54  			DisplayName: "Charlie",
    55  			CreatedBy:   "admin@local",
    56  			DateCreated: time.Date(2015, 2, 15, 9, 0, 0, 0, time.UTC),
    57  		},
    58  	}
    59  
    60  	s.fake = &fakeEnvUsersClient{users: userlist}
    61  }
    62  
    63  func (s *UsersCommandSuite) TestEnvUsers(c *gc.C) {
    64  	context, err := testing.RunCommand(c, environment.NewUsersCommand(s.fake))
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	c.Assert(testing.Stdout(context), gc.Equals, ""+
    67  		"NAME                DATE CREATED  LAST CONNECTION\n"+
    68  		"admin@local         2014-07-20    2015-03-20\n"+
    69  		"bob@local           2015-02-15    2015-03-01\n"+
    70  		"charlie@ubuntu.com  2015-02-15    never connected\n"+
    71  		"\n")
    72  }
    73  
    74  func (s *UsersCommandSuite) TestEnvUsersFormatJson(c *gc.C) {
    75  	context, err := testing.RunCommand(c, environment.NewUsersCommand(s.fake), "--format", "json")
    76  	c.Assert(err, jc.ErrorIsNil)
    77  	c.Assert(testing.Stdout(context), gc.Equals, "["+
    78  		`{"user-name":"admin@local","date-created":"2014-07-20","last-connection":"2015-03-20"},`+
    79  		`{"user-name":"bob@local","date-created":"2015-02-15","last-connection":"2015-03-01"},`+
    80  		`{"user-name":"charlie@ubuntu.com","date-created":"2015-02-15","last-connection":"never connected"}`+
    81  		"]\n")
    82  }
    83  
    84  func (s *UsersCommandSuite) TestUserInfoFormatYaml(c *gc.C) {
    85  	context, err := testing.RunCommand(c, environment.NewUsersCommand(s.fake), "--format", "yaml")
    86  	c.Assert(err, jc.ErrorIsNil)
    87  	c.Assert(testing.Stdout(context), gc.Equals, ""+
    88  		"- user-name: admin@local\n"+
    89  		"  date-created: 2014-07-20\n"+
    90  		"  last-connection: 2015-03-20\n"+
    91  		"- user-name: bob@local\n"+
    92  		"  date-created: 2015-02-15\n"+
    93  		"  last-connection: 2015-03-01\n"+
    94  		"- user-name: charlie@ubuntu.com\n"+
    95  		"  date-created: 2015-02-15\n"+
    96  		"  last-connection: never connected\n")
    97  }
    98  
    99  func (s *UsersCommandSuite) TestUnrecognizedArg(c *gc.C) {
   100  	_, err := testing.RunCommand(c, environment.NewUsersCommand(s.fake), "whoops")
   101  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`)
   102  }