github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/cmd/juju/user/logout_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package user_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/cmd/juju/user"
    13  	coretesting "github.com/juju/juju/testing"
    14  )
    15  
    16  type LogoutCommandSuite struct {
    17  	BaseSuite
    18  }
    19  
    20  var _ = gc.Suite(&LogoutCommandSuite{})
    21  
    22  func (s *LogoutCommandSuite) SetUpTest(c *gc.C) {
    23  	s.BaseSuite.SetUpTest(c)
    24  }
    25  
    26  func (s *LogoutCommandSuite) run(c *gc.C, args ...string) (*cmd.Context, error) {
    27  	cmd, _ := user.NewLogoutCommandForTest(s.store)
    28  	return coretesting.RunCommand(c, cmd, args...)
    29  }
    30  
    31  func (s *LogoutCommandSuite) TestInit(c *gc.C) {
    32  	for i, test := range []struct {
    33  		args        []string
    34  		errorString string
    35  	}{
    36  		{
    37  		// no args is fine
    38  		}, {
    39  			args:        []string{"foobar"},
    40  			errorString: `unrecognized args: \["foobar"\]`,
    41  		}, {
    42  			args:        []string{"--foobar"},
    43  			errorString: "flag provided but not defined: --foobar",
    44  		},
    45  	} {
    46  		c.Logf("test %d", i)
    47  		wrappedCommand, _ := user.NewLogoutCommandForTest(s.store)
    48  		err := coretesting.InitCommand(wrappedCommand, test.args)
    49  		if test.errorString == "" {
    50  			c.Check(err, jc.ErrorIsNil)
    51  		} else {
    52  			c.Check(err, gc.ErrorMatches, test.errorString)
    53  		}
    54  	}
    55  }
    56  
    57  func (s *LogoutCommandSuite) TestLogout(c *gc.C) {
    58  	details := s.store.Accounts["testing"].Accounts["current-user@local"]
    59  	details.Macaroon = "a-macaroon"
    60  	s.store.Accounts["testing"].Accounts["current-user@local"] = details
    61  	ctx, err := s.run(c)
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	c.Assert(coretesting.Stdout(ctx), gc.Equals, "")
    64  	c.Assert(coretesting.Stderr(ctx), gc.Equals, `
    65  Logged out. You are no longer logged into any controllers.
    66  `[1:],
    67  	)
    68  	_, err = s.store.CurrentAccount("testing")
    69  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
    70  	_, err = s.store.AccountByName("testing", "current-user@local")
    71  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
    72  }
    73  
    74  func (s *LogoutCommandSuite) TestLogoutCount(c *gc.C) {
    75  	// Create multiple controllers. We'll log out of each one
    76  	// to observe the messages printed out by "logout".
    77  	controllers := []string{"testing", "testing2", "testing3"}
    78  	details := s.store.Accounts["testing"].Accounts["current-user@local"]
    79  	details.Macaroon = "a-macaroon"
    80  	for _, controller := range controllers {
    81  		s.store.Controllers[controller] = s.store.Controllers["testing"]
    82  		s.store.Controllers[controller] = s.store.Controllers["testing"]
    83  		err := s.store.UpdateAccount(controller, "current-user@local", details)
    84  		c.Assert(err, jc.ErrorIsNil)
    85  		err = s.store.SetCurrentAccount(controller, "current-user@local")
    86  		c.Assert(err, jc.ErrorIsNil)
    87  	}
    88  
    89  	expected := []string{
    90  		"Logged out. You are still logged into 2 controllers.\n",
    91  		"Logged out. You are still logged into 1 controller.\n",
    92  		"Logged out. You are no longer logged into any controllers.\n",
    93  	}
    94  
    95  	for i, controller := range controllers {
    96  		ctx, err := s.run(c, "-c", controller)
    97  		c.Assert(err, jc.ErrorIsNil)
    98  		c.Assert(coretesting.Stdout(ctx), gc.Equals, "")
    99  		c.Assert(coretesting.Stderr(ctx), gc.Equals, expected[i])
   100  	}
   101  }
   102  
   103  func (s *LogoutCommandSuite) TestLogoutWithoutMacaroon(c *gc.C) {
   104  	s.assertStorePassword(c, "current-user@local", "old-password")
   105  	s.assertStoreMacaroon(c, "current-user@local", nil)
   106  	_, err := s.run(c)
   107  	c.Assert(err, gc.NotNil)
   108  	c.Assert(err.Error(), gc.Equals, `preventing account loss
   109  
   110  It appears that you have not changed the password for
   111  your account. If this is the case, change the password
   112  first before logging out, so that you can log in again
   113  afterwards. To change your password, run the command
   114  "juju change-user-password".
   115  
   116  If you are sure you want to log out, and it is safe to
   117  clear the credentials from the client, then you can run
   118  this command again with the "--force" flag.
   119  `)
   120  }
   121  
   122  func (s *LogoutCommandSuite) TestLogoutWithoutMacaroonForced(c *gc.C) {
   123  	s.assertStorePassword(c, "current-user@local", "old-password")
   124  	s.assertStoreMacaroon(c, "current-user@local", nil)
   125  	_, err := s.run(c, "--force")
   126  	c.Assert(err, jc.ErrorIsNil)
   127  	_, err = s.store.CurrentAccount("testing")
   128  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
   129  }
   130  
   131  func (s *LogoutCommandSuite) TestLogoutNotLoggedIn(c *gc.C) {
   132  	s.store.Accounts["testing"].CurrentAccount = ""
   133  	ctx, err := s.run(c)
   134  	c.Assert(err, jc.ErrorIsNil)
   135  	c.Assert(coretesting.Stdout(ctx), gc.Equals, "")
   136  	c.Assert(coretesting.Stderr(ctx), gc.Equals, `
   137  Logged out. You are no longer logged into any controllers.
   138  `[1:],
   139  	)
   140  }