github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	"strings"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/loggo"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/cmd/juju/commands"
    16  	jujutesting "github.com/juju/juju/juju/testing"
    17  	"github.com/juju/juju/jujuclient"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  type cmdLoginSuite struct {
    22  	jujutesting.JujuConnSuite
    23  }
    24  
    25  func (s *cmdLoginSuite) run(c *gc.C, stdin io.Reader, args ...string) *cmd.Context {
    26  	context := testing.Context(c)
    27  	if stdin != nil {
    28  		context.Stdin = stdin
    29  	}
    30  	command := commands.NewJujuCommand(context)
    31  	c.Assert(testing.InitCommand(command, args), jc.ErrorIsNil)
    32  	c.Assert(command.Run(context), jc.ErrorIsNil)
    33  	loggo.RemoveWriter("warning") // remove logger added by main command
    34  	return context
    35  }
    36  
    37  func (s *cmdLoginSuite) createTestUser(c *gc.C) {
    38  	s.run(c, nil, "add-user", "test", "--models", "admin")
    39  	s.changeUserPassword(c, "test", "hunter2")
    40  }
    41  
    42  func (s *cmdLoginSuite) changeUserPassword(c *gc.C, user, password string) {
    43  	s.run(c, strings.NewReader(password+"\n"+password+"\n"), "change-user-password", user)
    44  }
    45  
    46  func (s *cmdLoginSuite) TestLoginCommand(c *gc.C) {
    47  	s.createTestUser(c)
    48  
    49  	// logout "admin" first; we'll need to give it
    50  	// a non-random password before we can do so.
    51  	s.changeUserPassword(c, "admin", "hunter2")
    52  	s.run(c, nil, "logout")
    53  
    54  	context := s.run(c, strings.NewReader("hunter2\nhunter2\n"), "login", "test")
    55  	c.Assert(testing.Stdout(context), gc.Equals, "")
    56  	c.Assert(testing.Stderr(context), gc.Equals, `
    57  password: 
    58  You are now logged in to "kontroll" as "test@local".
    59  `[1:])
    60  
    61  	// We should have a macaroon, but no password, in the client store.
    62  	store := jujuclient.NewFileClientStore()
    63  	accountDetails, err := store.AccountByName("kontroll", "test@local")
    64  	c.Assert(err, jc.ErrorIsNil)
    65  	c.Assert(accountDetails.Password, gc.Equals, "")
    66  	c.Assert(accountDetails.Macaroon, gc.Not(gc.Equals), "")
    67  
    68  	// We should be able to login with the macaroon.
    69  	s.run(c, nil, "status")
    70  }