github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/user/add_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package user_test 5 6 import ( 7 "strings" 8 9 "github.com/juju/cmd" 10 "github.com/juju/errors" 11 "github.com/juju/names" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/api/base" 16 "github.com/juju/juju/apiserver/common" 17 "github.com/juju/juju/cmd/juju/user" 18 "github.com/juju/juju/testing" 19 ) 20 21 // All of the functionality of the AddUser api call is contained elsewhere. 22 // This suite provides basic tests for the "add-user" command 23 type UserAddCommandSuite struct { 24 BaseSuite 25 mockAPI *mockAddUserAPI 26 } 27 28 var _ = gc.Suite(&UserAddCommandSuite{}) 29 30 func (s *UserAddCommandSuite) SetUpTest(c *gc.C) { 31 s.BaseSuite.SetUpTest(c) 32 s.mockAPI = &mockAddUserAPI{} 33 s.mockAPI.secretKey = []byte(strings.Repeat("X", 32)) 34 } 35 36 func (s *UserAddCommandSuite) run(c *gc.C, args ...string) (*cmd.Context, error) { 37 addCommand, _ := user.NewAddCommandForTest(s.mockAPI, s.store, &mockModelApi{}) 38 return testing.RunCommand(c, addCommand, args...) 39 } 40 41 func (s *UserAddCommandSuite) TestInit(c *gc.C) { 42 for i, test := range []struct { 43 args []string 44 user string 45 displayname string 46 models string 47 acl string 48 outPath string 49 errorString string 50 }{{ 51 errorString: "no username supplied", 52 }, { 53 args: []string{"foobar"}, 54 user: "foobar", 55 }, { 56 args: []string{"foobar", "Foo Bar"}, 57 user: "foobar", 58 displayname: "Foo Bar", 59 }, { 60 args: []string{"foobar", "Foo Bar", "extra"}, 61 errorString: `unrecognized args: \["extra"\]`, 62 }, { 63 args: []string{"foobar", "--models", "foo,bar", "--acl=read"}, 64 user: "foobar", 65 models: "foo,bar", 66 acl: "read", 67 }, { 68 args: []string{"foobar", "--models", "baz", "--acl=write"}, 69 user: "foobar", 70 models: "baz", 71 acl: "write", 72 }} { 73 c.Logf("test %d (%q)", i, test.args) 74 wrappedCommand, command := user.NewAddCommandForTest(s.mockAPI, s.store, &mockModelApi{}) 75 err := testing.InitCommand(wrappedCommand, test.args) 76 if test.errorString == "" { 77 c.Check(err, jc.ErrorIsNil) 78 c.Check(command.User, gc.Equals, test.user) 79 c.Check(command.DisplayName, gc.Equals, test.displayname) 80 if len(test.models) > 0 { 81 c.Check(command.ModelNames, gc.Equals, test.models) 82 } 83 if test.acl != "" { 84 c.Check(command.ModelAccess, gc.Equals, test.acl) 85 } 86 } else { 87 c.Check(err, gc.ErrorMatches, test.errorString) 88 } 89 } 90 } 91 92 func (s *UserAddCommandSuite) TestAddUserWithUsername(c *gc.C) { 93 context, err := s.run(c, "foobar") 94 c.Assert(err, jc.ErrorIsNil) 95 c.Assert(s.mockAPI.username, gc.Equals, "foobar") 96 c.Assert(s.mockAPI.displayname, gc.Equals, "") 97 c.Assert(s.mockAPI.access, gc.Equals, "read") 98 c.Assert(s.mockAPI.models, gc.HasLen, 0) 99 expected := ` 100 User "foobar" added 101 Please send this command to foobar: 102 juju register MD0TBmZvb2JhcjAREw8xMjcuMC4wLjE6MTIzNDUEIFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhY 103 104 "foobar" has not been granted access to any models. You can use "juju grant" to grant access. 105 `[1:] 106 c.Assert(testing.Stdout(context), gc.Equals, expected) 107 c.Assert(testing.Stderr(context), gc.Equals, "") 108 } 109 110 func (s *UserAddCommandSuite) TestAddUserWithUsernameAndACL(c *gc.C) { 111 context, err := s.run(c, "--acl", "write", "foobar") 112 c.Assert(err, jc.ErrorIsNil) 113 c.Assert(s.mockAPI.username, gc.Equals, "foobar") 114 c.Assert(s.mockAPI.displayname, gc.Equals, "") 115 c.Assert(s.mockAPI.access, gc.Equals, "write") 116 c.Assert(s.mockAPI.models, gc.HasLen, 0) 117 expected := ` 118 User "foobar" added 119 Please send this command to foobar: 120 juju register MD0TBmZvb2JhcjAREw8xMjcuMC4wLjE6MTIzNDUEIFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhY 121 122 "foobar" has not been granted access to any models. You can use "juju grant" to grant access. 123 `[1:] 124 c.Assert(testing.Stdout(context), gc.Equals, expected) 125 c.Assert(testing.Stderr(context), gc.Equals, "") 126 } 127 128 func (s *UserAddCommandSuite) TestAddUserWithUsernameAndDisplayname(c *gc.C) { 129 context, err := s.run(c, "foobar", "Foo Bar") 130 c.Assert(err, jc.ErrorIsNil) 131 c.Assert(s.mockAPI.username, gc.Equals, "foobar") 132 c.Assert(s.mockAPI.displayname, gc.Equals, "Foo Bar") 133 c.Assert(s.mockAPI.access, gc.Equals, "read") 134 c.Assert(s.mockAPI.models, gc.HasLen, 0) 135 expected := ` 136 User "Foo Bar (foobar)" added 137 Please send this command to foobar: 138 juju register MD0TBmZvb2JhcjAREw8xMjcuMC4wLjE6MTIzNDUEIFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhY 139 140 "Foo Bar (foobar)" has not been granted access to any models. You can use "juju grant" to grant access. 141 `[1:] 142 c.Assert(testing.Stdout(context), gc.Equals, expected) 143 c.Assert(testing.Stderr(context), gc.Equals, "") 144 } 145 146 type mockModelApi struct{} 147 148 func (m *mockModelApi) ListModels(user string) ([]base.UserModel, error) { 149 return []base.UserModel{{Name: "model", UUID: "modeluuid"}}, nil 150 } 151 152 func (m *mockModelApi) Close() error { 153 return nil 154 } 155 156 func (s *UserAddCommandSuite) TestAddUserWithModelAccess(c *gc.C) { 157 context, err := s.run(c, "foobar", "--models", "model") 158 c.Assert(err, jc.ErrorIsNil) 159 c.Assert(s.mockAPI.username, gc.Equals, "foobar") 160 c.Assert(s.mockAPI.displayname, gc.Equals, "") 161 c.Assert(s.mockAPI.access, gc.Equals, "read") 162 c.Assert(s.mockAPI.models, gc.DeepEquals, []string{"modeluuid"}) 163 expected := ` 164 User "foobar" added 165 User "foobar" granted read access to model "model" 166 Please send this command to foobar: 167 juju register MD0TBmZvb2JhcjAREw8xMjcuMC4wLjE6MTIzNDUEIFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhY 168 `[1:] 169 c.Assert(testing.Stdout(context), gc.Equals, expected) 170 c.Assert(testing.Stderr(context), gc.Equals, "") 171 } 172 173 func (s *UserAddCommandSuite) TestBlockAddUser(c *gc.C) { 174 // Block operation 175 s.mockAPI.blocked = true 176 _, err := s.run(c, "foobar", "Foo Bar") 177 c.Assert(err, gc.ErrorMatches, cmd.ErrSilent.Error()) 178 // msg is logged 179 stripped := strings.Replace(c.GetTestLog(), "\n", "", -1) 180 c.Check(stripped, gc.Matches, ".*To unblock changes.*") 181 } 182 183 func (s *UserAddCommandSuite) TestAddUserErrorResponse(c *gc.C) { 184 s.mockAPI.failMessage = "failed to create user, chaos ensues" 185 _, err := s.run(c, "foobar") 186 c.Assert(err, gc.ErrorMatches, s.mockAPI.failMessage) 187 } 188 189 type mockAddUserAPI struct { 190 failMessage string 191 blocked bool 192 secretKey []byte 193 194 username string 195 displayname string 196 password string 197 access string 198 models []string 199 } 200 201 func (m *mockAddUserAPI) AddUser(username, displayname, password, access string, models ...string) (names.UserTag, []byte, error) { 202 if m.blocked { 203 return names.UserTag{}, nil, common.OperationBlockedError("the operation has been blocked") 204 } 205 m.username = username 206 m.displayname = displayname 207 m.password = password 208 m.access = access 209 m.models = models 210 if m.failMessage != "" { 211 return names.UserTag{}, nil, errors.New(m.failMessage) 212 } 213 return names.NewLocalUserTag(username), m.secretKey, nil 214 } 215 216 func (*mockAddUserAPI) Close() error { 217 return nil 218 }