github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/user/login_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 "errors" 8 "strings" 9 10 "github.com/juju/cmd" 11 "github.com/juju/names" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/cmd/juju/user" 16 "github.com/juju/juju/juju" 17 "github.com/juju/juju/jujuclient" 18 coretesting "github.com/juju/juju/testing" 19 ) 20 21 type LoginCommandSuite struct { 22 BaseSuite 23 mockAPI *mockLoginAPI 24 } 25 26 var _ = gc.Suite(&LoginCommandSuite{}) 27 28 func (s *LoginCommandSuite) SetUpTest(c *gc.C) { 29 s.BaseSuite.SetUpTest(c) 30 s.mockAPI = &mockLoginAPI{} 31 } 32 33 func (s *LoginCommandSuite) run(c *gc.C, stdin string, args ...string) (*cmd.Context, juju.NewAPIConnectionParams, error) { 34 var argsOut juju.NewAPIConnectionParams 35 cmd, _ := user.NewLoginCommandForTest(func(args juju.NewAPIConnectionParams) (user.LoginAPI, error) { 36 argsOut = args 37 // The account details are modified in place, so take a copy. 38 accountDetails := *argsOut.AccountDetails 39 argsOut.AccountDetails = &accountDetails 40 return s.mockAPI, nil 41 }, s.store) 42 ctx := coretesting.Context(c) 43 if stdin == "" { 44 stdin = "sekrit\n" 45 } 46 ctx.Stdin = strings.NewReader(stdin) 47 err := coretesting.InitCommand(cmd, args) 48 if err != nil { 49 return nil, argsOut, err 50 } 51 err = cmd.Run(ctx) 52 return ctx, argsOut, err 53 } 54 55 func (s *LoginCommandSuite) TestInit(c *gc.C) { 56 for i, test := range []struct { 57 args []string 58 user string 59 errorString string 60 }{ 61 { 62 // no args is fine 63 }, { 64 args: []string{"foobar"}, 65 user: "foobar", 66 }, { 67 args: []string{"--foobar"}, 68 errorString: "flag provided but not defined: --foobar", 69 }, { 70 args: []string{"foobar", "extra"}, 71 errorString: `unrecognized args: \["extra"\]`, 72 }, 73 } { 74 c.Logf("test %d", i) 75 wrappedCommand, command := user.NewLoginCommandForTest(nil, s.store) 76 err := coretesting.InitCommand(wrappedCommand, test.args) 77 if test.errorString == "" { 78 c.Check(command.User, gc.Equals, test.user) 79 } else { 80 c.Check(err, gc.ErrorMatches, test.errorString) 81 } 82 } 83 } 84 85 func (s *LoginCommandSuite) TestLogin(c *gc.C) { 86 context, args, err := s.run(c, "current-user\nsekrit\n") 87 c.Assert(err, jc.ErrorIsNil) 88 c.Assert(coretesting.Stdout(context), gc.Equals, "") 89 c.Assert(coretesting.Stderr(context), gc.Equals, ` 90 username: password: 91 You are now logged in to "testing" as "current-user@local". 92 `[1:], 93 ) 94 s.assertStorePassword(c, "current-user@local", "") 95 s.assertStoreMacaroon(c, "current-user@local", fakeLocalLoginMacaroon(names.NewUserTag("current-user@local"))) 96 c.Assert(args.AccountDetails, jc.DeepEquals, &jujuclient.AccountDetails{ 97 User: "current-user@local", 98 Password: "sekrit", 99 }) 100 } 101 102 func (s *LoginCommandSuite) TestLoginNewUser(c *gc.C) { 103 err := s.store.RemoveAccount("testing", "current-user@local") 104 c.Assert(err, jc.ErrorIsNil) 105 context, args, err := s.run(c, "", "new-user") 106 c.Assert(err, jc.ErrorIsNil) 107 c.Assert(coretesting.Stdout(context), gc.Equals, "") 108 c.Assert(coretesting.Stderr(context), gc.Equals, ` 109 password: 110 You are now logged in to "testing" as "new-user@local". 111 `[1:], 112 ) 113 s.assertStorePassword(c, "new-user@local", "") 114 s.assertStoreMacaroon(c, "new-user@local", fakeLocalLoginMacaroon(names.NewUserTag("new-user@local"))) 115 c.Assert(args.AccountDetails, jc.DeepEquals, &jujuclient.AccountDetails{ 116 User: "new-user@local", 117 Password: "sekrit", 118 }) 119 } 120 121 func (s *LoginCommandSuite) TestLoginAlreadyLoggedInSameUser(c *gc.C) { 122 _, _, err := s.run(c, "", "current-user") 123 c.Assert(err, jc.ErrorIsNil) 124 } 125 126 func (s *LoginCommandSuite) TestLoginAlreadyLoggedInDifferentUser(c *gc.C) { 127 _, _, err := s.run(c, "", "other-user") 128 c.Assert(err, gc.ErrorMatches, `already logged in 129 130 Run "juju logout" first before attempting to log in as a different user. 131 `) 132 } 133 134 func (s *LoginCommandSuite) TestLoginFail(c *gc.C) { 135 s.mockAPI.SetErrors(errors.New("failed to do something")) 136 _, _, err := s.run(c, "", "current-user") 137 c.Assert(err, gc.ErrorMatches, "failed to create a temporary credential: failed to do something") 138 s.assertStorePassword(c, "current-user@local", "old-password") 139 s.assertStoreMacaroon(c, "current-user@local", nil) 140 } 141 142 type mockLoginAPI struct { 143 mockChangePasswordAPI 144 }