33
32
type RegisterSuite struct {
34
33
testing.FakeJujuXDGDataHomeSuite
35
apiConnection *mockAPIConnection
36
legacystore configstore.Storage
37
store jujuclient.ClientStore
39
apiOpenControllerName string
40
apiOpenAccountName string
41
apiOpenModelName string
42
server *httptest.Server
43
httpHandler http.Handler
34
apiConnection *mockAPIConnection
35
store *jujuclienttesting.MemStore
37
refreshModels func(jujuclient.ClientStore, string, string) error
38
refreshModelsControllerName string
39
refreshModelsAccountName string
40
server *httptest.Server
41
httpHandler http.Handler
46
44
var _ = gc.Suite(&RegisterSuite{})
60
58
controllerTag: testing.ModelTag,
61
59
addr: serverURL.Host,
63
s.apiOpenControllerName = ""
64
s.apiOpenAccountName = ""
65
s.apiOpenModelName = ""
61
s.refreshModelsControllerName = ""
62
s.refreshModelsAccountName = ""
63
s.refreshModels = func(store jujuclient.ClientStore, controllerName, accountName string) error {
64
s.refreshModelsControllerName = controllerName
65
s.refreshModelsAccountName = accountName
67
s.legacystore = configstore.NewMem()
68
69
s.store = jujuclienttesting.NewMemStore()
69
s.PatchValue(&configstore.Default, func() (configstore.Storage, error) {
70
return s.legacystore, nil
74
72
func (s *RegisterSuite) TearDownTest(c *gc.C) {
85
83
return s.apiConnection, nil
88
func (s *RegisterSuite) newAPIRoot(store jujuclient.ClientStore, controllerName, accountName, modelName string) (api.Connection, error) {
89
if s.apiOpenError != nil {
90
return nil, s.apiOpenError
92
s.apiOpenControllerName = controllerName
93
s.apiOpenAccountName = accountName
94
s.apiOpenModelName = modelName
95
return s.apiConnection, nil
98
86
func (s *RegisterSuite) run(c *gc.C, stdin io.Reader, args ...string) (*cmd.Context, error) {
99
command := controller.NewRegisterCommandForTest(s.apiOpen, s.newAPIRoot, s.store)
87
command := controller.NewRegisterCommandForTest(s.apiOpen, s.refreshModels, s.store)
100
88
err := testing.InitCommand(command, args)
101
89
c.Assert(err, jc.ErrorIsNil)
102
90
ctx := testing.Context(c)
143
131
func (s *RegisterSuite) TestRegister(c *gc.C) {
132
ctx := s.testRegister(c)
133
c.Assert(s.refreshModelsControllerName, gc.Equals, "controller-name")
134
c.Assert(s.refreshModelsAccountName, gc.Equals, "bob@local")
135
stderr := testing.Stderr(ctx)
136
c.Assert(stderr, gc.Equals, `
137
Please set a name for this controller:
141
Welcome, bob. You are now logged into "controller-name".
143
There are no models available. You can create models with
144
"juju create-model", or you can ask an administrator or owner
145
of a model to grant access to that model with "juju grant".
150
func (s *RegisterSuite) TestRegisterOneModel(c *gc.C) {
151
s.refreshModels = func(store jujuclient.ClientStore, controller, account string) error {
152
err := store.UpdateModel(controller, account, "theoneandonly", jujuclient.ModelDetails{
153
ModelUUID: "df136476-12e9-11e4-8a70-b2227cce2b54",
155
c.Assert(err, jc.ErrorIsNil)
160
s.store.Models["controller-name"].AccountModels["bob@local"].CurrentModel,
161
gc.Equals, "theoneandonly",
165
func (s *RegisterSuite) TestRegisterMultipleModels(c *gc.C) {
166
s.refreshModels = func(store jujuclient.ClientStore, controller, account string) error {
167
for _, name := range [...]string{"model1", "model2"} {
168
err := store.UpdateModel(controller, account, name, jujuclient.ModelDetails{
169
ModelUUID: "df136476-12e9-11e4-8a70-b2227cce2b54",
171
c.Assert(err, jc.ErrorIsNil)
175
ctx := s.testRegister(c)
177
// When there are multiple models, no current model will be set.
178
// Instead, the command will output the list of models and inform
179
// the user how to set the current model.
180
_, err := s.store.CurrentModel("controller-name", "bob@local")
181
c.Assert(err, jc.Satisfies, errors.IsNotFound)
183
stderr := testing.Stderr(ctx)
184
c.Assert(stderr, gc.Equals, `
185
Please set a name for this controller:
189
Welcome, bob. You are now logged into "controller-name".
191
There are 2 models available. Use "juju switch" to select
199
func (s *RegisterSuite) testRegister(c *gc.C) *cmd.Context {
144
200
secretKey := []byte(strings.Repeat("X", 32))
145
201
respNonce := []byte(strings.Repeat("X", 24))
169
225
registrationData := s.encodeRegistrationData(c, "bob", secretKey)
170
226
stdin := strings.NewReader("controller-name\nhunter2\nhunter2\n")
171
_, err = s.run(c, stdin, registrationData)
227
ctx, err := s.run(c, stdin, registrationData)
172
228
c.Assert(err, jc.ErrorIsNil)
174
230
// There should have been one POST command to "/register".
204
260
User: "bob@local",
205
261
Password: "hunter2",
208
// The command should have logged into the controller with the
209
// information we checked above.
210
c.Assert(s.apiOpenControllerName, gc.Equals, "controller-name")
211
c.Assert(s.apiOpenAccountName, gc.Equals, "bob@local")
212
c.Assert(s.apiOpenModelName, gc.Equals, "")
215
266
func (s *RegisterSuite) TestRegisterInvalidRegistrationData(c *gc.C) {
231
282
func (s *RegisterSuite) TestRegisterControllerNameExists(c *gc.C) {
232
err := s.legacystore.CreateInfo("controller-name").Write()
233
c.Assert(err, jc.ErrorIsNil)
283
err := s.store.UpdateController("controller-name", jujuclient.ControllerDetails{
284
ControllerUUID: "df136476-12e9-11e4-8a70-b2227cce2b54",
285
CACert: testing.CACert,
235
288
secretKey := []byte(strings.Repeat("X", 32))
236
289
registrationData := s.encodeRegistrationData(c, "bob", secretKey)
281
334
_, err = s.run(c, stdin, registrationData)
282
335
c.Assert(err, gc.ErrorMatches, "xyz")
284
_, err = s.legacystore.ReadInfo("controller-name")
337
_, err = s.store.ControllerByName("controller-name")
285
338
c.Assert(err, jc.Satisfies, errors.IsNotFound)