~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/user/logout_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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"]
 
59
        details.Macaroon = "a-macaroon"
 
60
        s.store.Accounts["testing"] = 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.AccountDetails("testing")
 
69
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
 
70
}
 
71
 
 
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)
 
82
        }
 
83
 
 
84
        expected := []string{
 
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",
 
88
        }
 
89
 
 
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])
 
95
        }
 
96
}
 
97
 
 
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)
 
101
        _, err := s.run(c)
 
102
        c.Assert(err, gc.NotNil)
 
103
        c.Assert(err.Error(), gc.Equals, `preventing account loss
 
104
 
 
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".
 
110
 
 
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.
 
114
`)
 
115
}
 
116
 
 
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)
 
124
}
 
125
 
 
126
func (s *LogoutCommandSuite) TestLogoutNotLoggedIn(c *gc.C) {
 
127
        delete(s.store.Accounts, "testing")
 
128
        ctx, err := s.run(c)
 
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.
 
133
`[1:],
 
134
        )
 
135
}