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