23
25
var _ = gc.Suite(&AddUserSuite{})
27
func newAddUserCommand() cmd.Command {
28
return envcmd.Wrap(&AddUserCommand{})
25
31
func (s *AddUserSuite) TestAddUser(c *gc.C) {
27
_, err := testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "password"})
33
_, err := testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "password"})
28
34
c.Assert(err, gc.IsNil)
30
_, err = testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "newpassword"})
36
_, err = testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "newpassword"})
31
37
c.Assert(err, gc.ErrorMatches, "Failed to create user: user already exists")
34
40
func (s *AddUserSuite) TestTooManyArgs(c *gc.C) {
35
_, err := testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "password", "whoops"})
41
_, err := testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "password", "whoops"})
36
42
c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`)
39
45
func (s *AddUserSuite) TestNotEnoughArgs(c *gc.C) {
40
_, err := testing.RunCommand(c, &AddUserCommand{}, []string{})
46
_, err := testing.RunCommand(c, newAddUserCommand(), []string{})
41
47
c.Assert(err, gc.ErrorMatches, `no username supplied`)
50
56
tempFile, err := ioutil.TempFile("", "adduser-test")
52
58
c.Assert(err, gc.IsNil)
53
_, err = testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "password", "-o", tempFile.Name()})
59
_, err = testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "password", "-o", tempFile.Name()})
54
60
c.Assert(err, gc.IsNil)
55
61
data, err := ioutil.ReadFile(tempFile.Name())
56
62
result := map[string]interface{}{}
65
71
"password": "password",
66
72
"state-servers": []interface{}{},
68
ctx, err := testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "password"})
74
ctx, err := testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "password"})
69
75
c.Assert(err, gc.IsNil)
70
76
stdout := ctx.Stdout.(*bytes.Buffer).Bytes()
71
77
result := map[string]interface{}{}
80
86
tempFile, err := ioutil.TempFile("", "adduser-test")
82
88
c.Assert(err, gc.IsNil)
83
_, err = testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "password", "-o", tempFile.Name(), "--format", "json"})
89
_, err = testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "password", "-o", tempFile.Name(), "--format", "json"})
84
90
c.Assert(err, gc.IsNil)
85
91
data, err := ioutil.ReadFile(tempFile.Name())
86
92
c.Assert(string(data), gc.DeepEquals, expected)
89
95
func (s *AddUserSuite) TestJenvJsonFileOutput(c *gc.C) {
90
96
expected := `{"User":"foobar","Password":"password","state-servers":null,"ca-cert":""}
92
ctx, err := testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "password", "--format", "json"})
98
ctx, err := testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "password", "--format", "json"})
93
99
c.Assert(err, gc.IsNil)
94
100
stdout := ctx.Stdout.(*bytes.Buffer).String()
95
101
c.Assert(stdout, gc.DeepEquals, expected)
98
104
func (s *AddUserSuite) TestGeneratePassword(c *gc.C) {
99
ctx, err := testing.RunCommand(c, &AddUserCommand{}, []string{"foobar"})
105
ctx, err := testing.RunCommand(c, newAddUserCommand(), []string{"foobar"})
100
106
c.Assert(err, gc.IsNil)
101
107
stdout := ctx.Stdout.(*bytes.Buffer).Bytes()
102
108
var d map[string]interface{}