github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/featuretests/cmd_juju_login_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package featuretests
     5  
     6  import (
     7  	"io"
     8  	"os"
     9  	"strings"
    10  
    11  	"github.com/juju/cmd"
    12  	"github.com/juju/loggo"
    13  	jc "github.com/juju/testing/checkers"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/cmd/juju/commands"
    17  	"github.com/juju/juju/juju/osenv"
    18  	jujutesting "github.com/juju/juju/juju/testing"
    19  	"github.com/juju/juju/jujuclient"
    20  	"github.com/juju/juju/testing"
    21  )
    22  
    23  type cmdLoginSuite struct {
    24  	jujutesting.JujuConnSuite
    25  }
    26  
    27  func (s *cmdLoginSuite) SetUpTest(c *gc.C) {
    28  	s.JujuConnSuite.SetUpTest(c)
    29  	os.Setenv(osenv.JujuModelEnvKey, "")
    30  }
    31  
    32  func (s *cmdLoginSuite) run(c *gc.C, stdin io.Reader, args ...string) *cmd.Context {
    33  	context := testing.Context(c)
    34  	if stdin != nil {
    35  		context.Stdin = stdin
    36  	}
    37  	command := commands.NewJujuCommand(context)
    38  	c.Assert(testing.InitCommand(command, args), jc.ErrorIsNil)
    39  	err := command.Run(context)
    40  	c.Assert(err, jc.ErrorIsNil, gc.Commentf("stdout: %q; stderr: %q", context.Stdout, context.Stderr))
    41  	loggo.RemoveWriter("warning") // remove logger added by main command
    42  	return context
    43  }
    44  
    45  func (s *cmdLoginSuite) createTestUser(c *gc.C) {
    46  	s.run(c, nil, "add-user", "test")
    47  	s.run(c, nil, "grant", "test", "read", "controller")
    48  	s.changeUserPassword(c, "test", "hunter2")
    49  }
    50  
    51  func (s *cmdLoginSuite) changeUserPassword(c *gc.C, user, password string) {
    52  	s.run(c, strings.NewReader(password+"\n"+password+"\n"), "change-user-password", user)
    53  }
    54  
    55  func (s *cmdLoginSuite) TestLoginCommand(c *gc.C) {
    56  	s.createTestUser(c)
    57  
    58  	// logout "admin" first; we'll need to give it
    59  	// a non-random password before we can do so.
    60  	s.changeUserPassword(c, "admin", "hunter2")
    61  	s.run(c, nil, "logout")
    62  
    63  	context := s.run(c, strings.NewReader("hunter2\nhunter2\n"), "login", "test")
    64  	c.Assert(testing.Stdout(context), gc.Equals, "")
    65  	c.Assert(testing.Stderr(context), gc.Equals, `
    66  please enter password for test on kontroll: 
    67  You are now logged in to "kontroll" as "test".
    68  `[1:])
    69  
    70  	// We should have a macaroon, but no password, in the client store.
    71  	store := jujuclient.NewFileClientStore()
    72  	accountDetails, err := store.AccountDetails("kontroll")
    73  	c.Assert(err, jc.ErrorIsNil)
    74  	c.Assert(accountDetails.Password, gc.Equals, "")
    75  
    76  	// We should be able to login with the macaroon.
    77  	s.run(c, nil, "status")
    78  }