github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	"net/http"
     8  	"net/url"
     9  	"path/filepath"
    10  	"time"
    11  
    12  	"github.com/juju/cmd"
    13  	"github.com/juju/errors"
    14  	"github.com/juju/persistent-cookiejar"
    15  	jc "github.com/juju/testing/checkers"
    16  	"github.com/juju/utils"
    17  	gc "gopkg.in/check.v1"
    18  
    19  	"github.com/juju/juju/cmd/juju/user"
    20  	coretesting "github.com/juju/juju/testing"
    21  )
    22  
    23  type LogoutCommandSuite struct {
    24  	BaseSuite
    25  }
    26  
    27  var _ = gc.Suite(&LogoutCommandSuite{})
    28  
    29  func (s *LogoutCommandSuite) SetUpTest(c *gc.C) {
    30  	s.BaseSuite.SetUpTest(c)
    31  }
    32  
    33  func (s *LogoutCommandSuite) run(c *gc.C, args ...string) (*cmd.Context, error) {
    34  	cmd, _ := user.NewLogoutCommandForTest(s.store)
    35  	return coretesting.RunCommand(c, cmd, args...)
    36  }
    37  
    38  func (s *LogoutCommandSuite) TestInit(c *gc.C) {
    39  	for i, test := range []struct {
    40  		args        []string
    41  		errorString string
    42  	}{
    43  		{
    44  		// no args is fine
    45  		}, {
    46  			args:        []string{"foobar"},
    47  			errorString: `unrecognized args: \["foobar"\]`,
    48  		}, {
    49  			args:        []string{"--foobar"},
    50  			errorString: "flag provided but not defined: --foobar",
    51  		},
    52  	} {
    53  		c.Logf("test %d", i)
    54  		wrappedCommand, _ := user.NewLogoutCommandForTest(s.store)
    55  		err := coretesting.InitCommand(wrappedCommand, test.args)
    56  		if test.errorString == "" {
    57  			c.Check(err, jc.ErrorIsNil)
    58  		} else {
    59  			c.Check(err, gc.ErrorMatches, test.errorString)
    60  		}
    61  	}
    62  }
    63  
    64  func (s *LogoutCommandSuite) TestLogout(c *gc.C) {
    65  	cookiefile := filepath.Join(utils.Home(), ".go-cookies")
    66  	jar, err := cookiejar.New(&cookiejar.Options{Filename: cookiefile})
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	cont, err := s.store.CurrentController()
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	host := s.store.Controllers[cont].APIEndpoints[0]
    71  	u, err := url.Parse("https://" + host)
    72  	c.Assert(err, jc.ErrorIsNil)
    73  	other, err := url.Parse("https://www.example.com")
    74  	c.Assert(err, jc.ErrorIsNil)
    75  
    76  	// we hav to set the expiration or it's not considered a "persistent"
    77  	// cookie, and the jar won't save it.
    78  	jar.SetCookies(u, []*http.Cookie{{
    79  		Name:    "foo",
    80  		Value:   "bar",
    81  		Expires: time.Now().Add(time.Hour * 24)}})
    82  	jar.SetCookies(other, []*http.Cookie{{
    83  		Name:    "baz",
    84  		Value:   "bat",
    85  		Expires: time.Now().Add(time.Hour * 24)}})
    86  	err = jar.Save()
    87  	c.Assert(err, jc.ErrorIsNil)
    88  
    89  	s.setPassword(c, "testing", "")
    90  	ctx, err := s.run(c)
    91  	c.Assert(err, jc.ErrorIsNil)
    92  	c.Assert(coretesting.Stdout(ctx), gc.Equals, "")
    93  	c.Assert(coretesting.Stderr(ctx), gc.Equals, `
    94  Logged out. You are no longer logged into any controllers.
    95  `[1:],
    96  	)
    97  	_, err = s.store.AccountDetails("testing")
    98  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
    99  
   100  	jar, err = cookiejar.New(&cookiejar.Options{Filename: cookiefile})
   101  	c.Assert(err, jc.ErrorIsNil)
   102  	cookies := jar.Cookies(other)
   103  	c.Assert(cookies, gc.HasLen, 1)
   104  }
   105  
   106  func (s *LogoutCommandSuite) TestLogoutCount(c *gc.C) {
   107  	// Create multiple controllers. We'll log out of each one
   108  	// to observe the messages printed out by "logout".
   109  	s.setPassword(c, "testing", "")
   110  	controllers := []string{"testing", "testing2", "testing3"}
   111  	details := s.store.Accounts["testing"]
   112  	for _, controller := range controllers {
   113  		s.store.Controllers[controller] = s.store.Controllers["testing"]
   114  		err := s.store.UpdateAccount(controller, details)
   115  		c.Assert(err, jc.ErrorIsNil)
   116  	}
   117  
   118  	expected := []string{
   119  		"Logged out. You are still logged into 2 controllers.\n",
   120  		"Logged out. You are still logged into 1 controller.\n",
   121  		"Logged out. You are no longer logged into any controllers.\n",
   122  	}
   123  
   124  	for i, controller := range controllers {
   125  		ctx, err := s.run(c, "-c", controller)
   126  		c.Assert(err, jc.ErrorIsNil)
   127  		c.Assert(coretesting.Stdout(ctx), gc.Equals, "")
   128  		c.Assert(coretesting.Stderr(ctx), gc.Equals, expected[i])
   129  	}
   130  }
   131  
   132  func (s *LogoutCommandSuite) TestLogoutWithPassword(c *gc.C) {
   133  	s.assertStorePassword(c, "current-user", "old-password", "")
   134  	_, err := s.run(c)
   135  	c.Assert(err, gc.NotNil)
   136  	c.Assert(err.Error(), gc.Equals, `preventing account loss
   137  
   138  It appears that you have not changed the password for
   139  your account. If this is the case, change the password
   140  first before logging out, so that you can log in again
   141  afterwards. To change your password, run the command
   142  "juju change-user-password".
   143  
   144  If you are sure you want to log out, and it is safe to
   145  clear the credentials from the client, then you can run
   146  this command again with the "--force" flag.
   147  `)
   148  }
   149  
   150  func (s *LogoutCommandSuite) TestLogoutWithPasswordForced(c *gc.C) {
   151  	s.assertStorePassword(c, "current-user", "old-password", "")
   152  	_, err := s.run(c, "--force")
   153  	c.Assert(err, jc.ErrorIsNil)
   154  	_, err = s.store.AccountDetails("testing")
   155  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
   156  }
   157  
   158  func (s *LogoutCommandSuite) TestLogoutNotLoggedIn(c *gc.C) {
   159  	delete(s.store.Accounts, "testing")
   160  	ctx, err := s.run(c)
   161  	c.Assert(err, jc.ErrorIsNil)
   162  	c.Assert(coretesting.Stdout(ctx), gc.Equals, "")
   163  	c.Assert(coretesting.Stderr(ctx), gc.Equals, `
   164  Logged out. You are no longer logged into any controllers.
   165  `[1:],
   166  	)
   167  }