1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
8
"github.com/juju/errors"
9
jc "github.com/juju/testing/checkers"
10
gc "gopkg.in/check.v1"
12
"github.com/juju/juju/cmd/juju/user"
13
coretesting "github.com/juju/juju/testing"
16
type LogoutCommandSuite struct {
20
var _ = gc.Suite(&LogoutCommandSuite{})
22
func (s *LogoutCommandSuite) SetUpTest(c *gc.C) {
23
s.BaseSuite.SetUpTest(c)
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...)
31
func (s *LogoutCommandSuite) TestInit(c *gc.C) {
32
for i, test := range []struct {
39
args: []string{"foobar"},
40
errorString: `unrecognized args: \["foobar"\]`,
42
args: []string{"--foobar"},
43
errorString: "flag provided but not defined: --foobar",
47
wrappedCommand, _ := user.NewLogoutCommandForTest(s.store)
48
err := coretesting.InitCommand(wrappedCommand, test.args)
49
if test.errorString == "" {
50
c.Check(err, jc.ErrorIsNil)
52
c.Check(err, gc.ErrorMatches, test.errorString)
57
func (s *LogoutCommandSuite) TestLogout(c *gc.C) {
58
details := s.store.Accounts["testing"]
59
details.Macaroon = "a-macaroon"
60
s.store.Accounts["testing"] = details
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.
68
_, err = s.store.AccountDetails("testing")
69
c.Assert(err, jc.Satisfies, errors.IsNotFound)
72
func (s *LogoutCommandSuite) TestLogoutCount(c *gc.C) {
73
// Create multiple controllers. We'll log out of each one
74
// to observe the messages printed out by "logout".
75
controllers := []string{"testing", "testing2", "testing3"}
76
details := s.store.Accounts["testing"]
77
details.Macaroon = "a-macaroon"
78
for _, controller := range controllers {
79
s.store.Controllers[controller] = s.store.Controllers["testing"]
80
err := s.store.UpdateAccount(controller, details)
81
c.Assert(err, jc.ErrorIsNil)
85
"Logged out. You are still logged into 2 controllers.\n",
86
"Logged out. You are still logged into 1 controller.\n",
87
"Logged out. You are no longer logged into any controllers.\n",
90
for i, controller := range controllers {
91
ctx, err := s.run(c, "-c", controller)
92
c.Assert(err, jc.ErrorIsNil)
93
c.Assert(coretesting.Stdout(ctx), gc.Equals, "")
94
c.Assert(coretesting.Stderr(ctx), gc.Equals, expected[i])
98
func (s *LogoutCommandSuite) TestLogoutWithoutMacaroon(c *gc.C) {
99
s.assertStorePassword(c, "current-user@local", "old-password")
100
s.assertStoreMacaroon(c, "current-user@local", nil)
102
c.Assert(err, gc.NotNil)
103
c.Assert(err.Error(), gc.Equals, `preventing account loss
105
It appears that you have not changed the password for
106
your account. If this is the case, change the password
107
first before logging out, so that you can log in again
108
afterwards. To change your password, run the command
109
"juju change-user-password".
111
If you are sure you want to log out, and it is safe to
112
clear the credentials from the client, then you can run
113
this command again with the "--force" flag.
117
func (s *LogoutCommandSuite) TestLogoutWithoutMacaroonForced(c *gc.C) {
118
s.assertStorePassword(c, "current-user@local", "old-password")
119
s.assertStoreMacaroon(c, "current-user@local", nil)
120
_, err := s.run(c, "--force")
121
c.Assert(err, jc.ErrorIsNil)
122
_, err = s.store.AccountDetails("testing")
123
c.Assert(err, jc.Satisfies, errors.IsNotFound)
126
func (s *LogoutCommandSuite) TestLogoutNotLoggedIn(c *gc.C) {
127
delete(s.store.Accounts, "testing")
129
c.Assert(err, jc.ErrorIsNil)
130
c.Assert(coretesting.Stdout(ctx), gc.Equals, "")
131
c.Assert(coretesting.Stderr(ctx), gc.Equals, `
132
Logged out. You are no longer logged into any controllers.