github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/cmd/cmdtesting"
    13  	"github.com/juju/loggo"
    14  	jc "github.com/juju/testing/checkers"
    15  	gc "gopkg.in/check.v1"
    16  
    17  	"github.com/juju/juju/cmd/juju/commands"
    18  	"github.com/juju/juju/juju/osenv"
    19  	jujutesting "github.com/juju/juju/juju/testing"
    20  	"github.com/juju/juju/jujuclient"
    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 := cmdtesting.Context(c)
    34  	if stdin != nil {
    35  		context.Stdin = stdin
    36  	}
    37  	command := commands.NewJujuCommand(context)
    38  	c.Assert(cmdtesting.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", "-u", "test")
    64  	c.Assert(cmdtesting.Stdout(context), gc.Equals, "")
    65  	c.Assert(cmdtesting.Stderr(context), gc.Equals, `
    66  please enter password for test on kontroll: 
    67  Welcome, test. You are now logged into "kontroll".
    68  
    69  Current model set to "admin/controller".
    70  `[1:])
    71  
    72  	// We should have a macaroon, but no password, in the client store.
    73  	store := jujuclient.NewFileClientStore()
    74  	accountDetails, err := store.AccountDetails("kontroll")
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	c.Assert(accountDetails.Password, gc.Equals, "")
    77  
    78  	// We should be able to login with the macaroon.
    79  	s.run(c, nil, "status")
    80  }