github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/model/users_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for info.
     3  
     4  package model_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/model"
    14  	"github.com/juju/juju/cmd/modelcmd"
    15  	"github.com/juju/juju/jujuclient"
    16  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  type UsersCommandSuite struct {
    21  	testing.FakeJujuXDGDataHomeSuite
    22  	fake  *fakeModelUsersClient
    23  	store *jujuclienttesting.MemStore
    24  }
    25  
    26  var _ = gc.Suite(&UsersCommandSuite{})
    27  
    28  type fakeModelUsersClient struct {
    29  	users []params.ModelUserInfo
    30  }
    31  
    32  func (f *fakeModelUsersClient) Close() error {
    33  	return nil
    34  }
    35  
    36  func (f *fakeModelUsersClient) ModelUserInfo() ([]params.ModelUserInfo, error) {
    37  	return f.users, nil
    38  }
    39  
    40  func (s *UsersCommandSuite) SetUpTest(c *gc.C) {
    41  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    42  	last1 := time.Date(2015, 3, 20, 0, 0, 0, 0, time.UTC)
    43  	last2 := time.Date(2015, 3, 1, 0, 0, 0, 0, time.UTC)
    44  
    45  	userlist := []params.ModelUserInfo{
    46  		{
    47  			UserName:       "admin@local",
    48  			LastConnection: &last1,
    49  			Access:         "write",
    50  		}, {
    51  			UserName:       "bob@local",
    52  			DisplayName:    "Bob",
    53  			LastConnection: &last2,
    54  			Access:         "read",
    55  		}, {
    56  			UserName:    "charlie@ubuntu.com",
    57  			DisplayName: "Charlie",
    58  			Access:      "read",
    59  		},
    60  	}
    61  
    62  	s.fake = &fakeModelUsersClient{users: userlist}
    63  
    64  	err := modelcmd.WriteCurrentController("testing")
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	s.store = jujuclienttesting.NewMemStore()
    67  	s.store.Controllers["testing"] = jujuclient.ControllerDetails{}
    68  	s.store.Accounts["testing"] = &jujuclient.ControllerAccounts{
    69  		CurrentAccount: "admin@local",
    70  	}
    71  }
    72  
    73  func (s *UsersCommandSuite) TestModelUsers(c *gc.C) {
    74  	context, err := testing.RunCommand(c, model.NewUsersCommandForTest(s.fake, s.store), "-m", "admin")
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	c.Assert(testing.Stdout(context), gc.Equals, ""+
    77  		"NAME                          ACCESS  LAST CONNECTION\n"+
    78  		"admin@local                   write   2015-03-20\n"+
    79  		"bob@local (Bob)               read    2015-03-01\n"+
    80  		"charlie@ubuntu.com (Charlie)  read    never connected\n"+
    81  		"\n")
    82  }
    83  
    84  func (s *UsersCommandSuite) TestModelUsersFormatJson(c *gc.C) {
    85  	context, err := testing.RunCommand(c, model.NewUsersCommandForTest(s.fake, s.store), "-m", "admin", "--format", "json")
    86  	c.Assert(err, jc.ErrorIsNil)
    87  	c.Assert(testing.Stdout(context), gc.Equals, "{"+
    88  		`"admin@local":{"access":"write","last-connection":"2015-03-20"},`+
    89  		`"bob@local":{"display-name":"Bob","access":"read","last-connection":"2015-03-01"},`+
    90  		`"charlie@ubuntu.com":{"display-name":"Charlie","access":"read","last-connection":"never connected"}`+
    91  		"}\n")
    92  }
    93  
    94  func (s *UsersCommandSuite) TestUserInfoFormatYaml(c *gc.C) {
    95  	context, err := testing.RunCommand(c, model.NewUsersCommandForTest(s.fake, s.store), "-m", "admin", "--format", "yaml")
    96  	c.Assert(err, jc.ErrorIsNil)
    97  	c.Assert(testing.Stdout(context), gc.Equals, ""+
    98  		"admin@local:\n"+
    99  		"  access: write\n"+
   100  		"  last-connection: 2015-03-20\n"+
   101  		"bob@local:\n"+
   102  		"  display-name: Bob\n"+
   103  		"  access: read\n"+
   104  		"  last-connection: 2015-03-01\n"+
   105  		"charlie@ubuntu.com:\n"+
   106  		"  display-name: Charlie\n"+
   107  		"  access: read\n"+
   108  		"  last-connection: never connected\n")
   109  }
   110  
   111  func (s *UsersCommandSuite) TestUnrecognizedArg(c *gc.C) {
   112  	_, err := testing.RunCommand(c, model.NewUsersCommandForTest(s.fake, s.store), "-m", "admin", "whoops")
   113  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`)
   114  }