This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
forked from t0mk/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_juju_user_test.go
136 lines (116 loc) · 4.35 KB
/
cmd_juju_user_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package featuretests
import (
"fmt"
"strings"
"github.com/juju/cmd"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/cmd/juju/commands"
jujutesting "github.com/juju/juju/juju/testing"
"github.com/juju/juju/testing"
"github.com/juju/juju/testing/factory"
)
// UserSuite tests the connectivity of all the user subcommands. These tests
// go from the command line, api client, api server, db. The db changes are
// then checked. Only one test for each command is done here to check
// connectivity. Exhaustive tests are at each layer.
type UserSuite struct {
jujutesting.JujuConnSuite
}
var _ = gc.Suite(&UserSuite{})
func (s *UserSuite) RunUserCommand(c *gc.C, stdin string, args ...string) (*cmd.Context, error) {
context := testing.Context(c)
if stdin != "" {
context.Stdin = strings.NewReader(stdin)
}
jujuCmd := commands.NewJujuCommand(context)
err := testing.InitCommand(jujuCmd, args)
c.Assert(err, jc.ErrorIsNil)
err = jujuCmd.Run(context)
return context, err
}
func (s *UserSuite) TestUserAdd(c *gc.C) {
ctx, err := s.RunUserCommand(c, "", "add-user", "test")
c.Assert(err, jc.ErrorIsNil)
c.Assert(testing.Stdout(ctx), jc.HasPrefix, `User "test" added`)
user, err := s.State.User(names.NewLocalUserTag("test"))
c.Assert(err, jc.ErrorIsNil)
c.Assert(user.IsDisabled(), jc.IsFalse)
}
func (s *UserSuite) TestUserChangePassword(c *gc.C) {
user, err := s.State.User(s.AdminUserTag(c))
c.Assert(err, jc.ErrorIsNil)
c.Assert(user.PasswordValid("dummy-secret"), jc.IsTrue)
_, err = s.RunUserCommand(c, "not-dummy-secret\nnot-dummy-secret\n", "change-user-password")
c.Assert(err, jc.ErrorIsNil)
user.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(user.PasswordValid("dummy-secret"), jc.IsFalse)
c.Assert(user.PasswordValid("not-dummy-secret"), jc.IsTrue)
}
func (s *UserSuite) TestUserInfo(c *gc.C) {
user, err := s.State.User(s.AdminUserTag(c))
c.Assert(err, jc.ErrorIsNil)
c.Assert(user.PasswordValid("dummy-secret"), jc.IsTrue)
ctx, err := s.RunUserCommand(c, "", "show-user")
c.Assert(err, jc.ErrorIsNil)
c.Assert(testing.Stdout(ctx), jc.Contains, "user-name: admin")
}
func (s *UserSuite) TestUserDisable(c *gc.C) {
user := s.Factory.MakeUser(c, &factory.UserParams{Name: "barbara"})
_, err := s.RunUserCommand(c, "", "disable-user", "barbara")
c.Assert(err, jc.ErrorIsNil)
user.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(user.IsDisabled(), jc.IsTrue)
}
func (s *UserSuite) TestUserEnable(c *gc.C) {
user := s.Factory.MakeUser(c, &factory.UserParams{Name: "barbara", Disabled: true})
_, err := s.RunUserCommand(c, "", "enable-user", "barbara")
c.Assert(err, jc.ErrorIsNil)
user.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(user.IsDisabled(), jc.IsFalse)
}
func (s *UserSuite) TestRemoveUserPrompt(c *gc.C) {
expected := `
WARNING! This command will permanently archive the user "jjam" on the "kontroll"
controller.
This action is irreversible. If you wish to temporarily disable the
user please use the`[1:] + " `juju disable-user` " + `command. See
` + " `juju help disable-user` " + `for more details.
Continue (y/N)? `
_ = s.Factory.MakeUser(c, &factory.UserParams{Name: "jjam"})
ctx, _ := s.RunUserCommand(c, "", "remove-user", "jjam")
c.Assert(testing.Stdout(ctx), jc.DeepEquals, expected)
}
func (s *UserSuite) TestRemoveUser(c *gc.C) {
user := s.Factory.MakeUser(c, &factory.UserParams{Name: "jjam"})
_, err := s.RunUserCommand(c, "", "remove-user", "-y", "jjam")
c.Assert(err, jc.ErrorIsNil)
err = user.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(user.IsDeleted(), jc.IsTrue)
}
func (s *UserSuite) TestRemoveUserLongForm(c *gc.C) {
user := s.Factory.MakeUser(c, &factory.UserParams{Name: "jjam"})
_, err := s.RunUserCommand(c, "", "remove-user", "--yes", "jjam")
c.Assert(err, jc.ErrorIsNil)
err = user.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(user.IsDeleted(), jc.IsTrue)
}
func (s *UserSuite) TestUserList(c *gc.C) {
ctx, err := s.RunUserCommand(c, "", "list-users")
c.Assert(err, jc.ErrorIsNil)
periodPattern := `(just now|\d+ \S+ ago)`
expected := fmt.Sprintf(`
Controller: kontroll
Name\s+Display name\s+Access\s+Date created\s+Last connection
admin.*\s+admin\s+superuser\s+%s\s+%s
`[1:], periodPattern, periodPattern)
c.Assert(testing.Stdout(ctx), gc.Matches, expected)
}