github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/user/user_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package user_test
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	goyaml "gopkg.in/yaml.v1"
    13  
    14  	"github.com/juju/juju/cmd/envcmd"
    15  	"github.com/juju/juju/cmd/juju/user"
    16  	"github.com/juju/juju/environs/configstore"
    17  	"github.com/juju/juju/juju/osenv"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  type UserCommandSuite struct {
    22  	testing.BaseSuite
    23  }
    24  
    25  var _ = gc.Suite(&UserCommandSuite{})
    26  
    27  var expectedUserCommmandNames = []string{
    28  	"add",
    29  	"change-password",
    30  	"credentials",
    31  	"disable",
    32  	"enable",
    33  	"help",
    34  	"info",
    35  	"list",
    36  }
    37  
    38  func (s *UserCommandSuite) TestHelp(c *gc.C) {
    39  	// Check the help output
    40  	ctx, err := testing.RunCommand(c, user.NewSuperCommand(), "--help")
    41  	c.Assert(err, jc.ErrorIsNil)
    42  	namesFound := testing.ExtractCommandsFromHelpOutput(ctx)
    43  	c.Assert(namesFound, gc.DeepEquals, expectedUserCommmandNames)
    44  }
    45  
    46  type BaseSuite struct {
    47  	testing.FakeJujuHomeSuite
    48  }
    49  
    50  func (s *BaseSuite) SetUpTest(c *gc.C) {
    51  	s.FakeJujuHomeSuite.SetUpTest(c)
    52  	memstore := configstore.NewMem()
    53  	s.PatchValue(&configstore.Default, func() (configstore.Storage, error) {
    54  		return memstore, nil
    55  	})
    56  	os.Setenv(osenv.JujuEnvEnvKey, "testing")
    57  	info := memstore.CreateInfo("testing")
    58  	info.SetBootstrapConfig(map[string]interface{}{"random": "extra data"})
    59  	info.SetAPIEndpoint(configstore.APIEndpoint{
    60  		Addresses:   []string{"127.0.0.1:12345"},
    61  		Hostnames:   []string{"localhost:12345"},
    62  		CACert:      testing.CACert,
    63  		EnvironUUID: "env-uuid",
    64  	})
    65  	info.SetAPICredentials(configstore.APICredentials{
    66  		User:     "user-test",
    67  		Password: "password",
    68  	})
    69  	err := info.Write()
    70  	c.Assert(err, jc.ErrorIsNil)
    71  	s.PatchValue(user.ReadPassword, func() (string, error) {
    72  		return "sekrit", nil
    73  	})
    74  	err = envcmd.WriteCurrentSystem("testing")
    75  	c.Assert(err, jc.ErrorIsNil)
    76  }
    77  
    78  func (s *BaseSuite) assertServerFileMatches(c *gc.C, serverfile, username, password string) {
    79  	yaml, err := ioutil.ReadFile(serverfile)
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	var content envcmd.ServerFile
    82  	err = goyaml.Unmarshal(yaml, &content)
    83  	c.Assert(err, jc.ErrorIsNil)
    84  
    85  	c.Assert(content.Username, gc.Equals, username)
    86  	c.Assert(content.Password, gc.Equals, password)
    87  	c.Assert(content.CACert, gc.Equals, testing.CACert)
    88  	c.Assert(content.Addresses, jc.DeepEquals, []string{"127.0.0.1:12345"})
    89  }