Skip to content

Commit

Permalink
Merge pull request juju#639 from axw/cmd-juju-removeuser-mock
Browse files Browse the repository at this point in the history
cmd/juju: mock out API for RemoveUserCommand tests
  • Loading branch information
jujubot committed Sep 1, 2014
2 parents 623f58c + fcf1342 commit 8ece4e6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
11 changes: 10 additions & 1 deletion cmd/juju/removeuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,17 @@ func (c *RemoveUserCommand) Init(args []string) error {
return cmd.CheckEmpty(args[1:])
}

type removeUserAPI interface {
RemoveUser(user string) error
Close() error
}

var getRemoveUserAPI = func(c *RemoveUserCommand) (removeUserAPI, error) {
return c.NewUserManagerClient()
}

func (c *RemoveUserCommand) Run(_ *cmd.Context) error {
client, err := c.NewUserManagerClient()
client, err := getRemoveUserAPI(c)
if err != nil {
return err
}
Expand Down
32 changes: 25 additions & 7 deletions cmd/juju/removeuser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@ import (
gc "launchpad.net/gocheck"

"github.com/juju/juju/cmd/envcmd"
jujutesting "github.com/juju/juju/juju/testing"
"github.com/juju/juju/testing"
)

type RemoveUserSuite struct {
jujutesting.RepoSuite
testing.FakeJujuHomeSuite
}

var _ = gc.Suite(&RemoveUserSuite{})

func (s *RemoveUserSuite) TestRemoveUser(c *gc.C) {
_, err := testing.RunCommand(c, envcmd.Wrap(&UserAddCommand{}), "foobar")
c.Assert(err, gc.IsNil)

_, err = testing.RunCommand(c, envcmd.Wrap(&RemoveUserCommand{}), "foobar")
c.Assert(err, gc.IsNil)
mock := &mockRemoveUserAPI{}
s.PatchValue(&getRemoveUserAPI, func(*RemoveUserCommand) (removeUserAPI, error) {
return mock, nil
})

users := []string{"foo", "bar", "baz"}
for _, user := range users {
_, err := testing.RunCommand(c, envcmd.Wrap(&RemoveUserCommand{}), user)
c.Assert(err, gc.IsNil)
}
c.Assert(mock.args, gc.DeepEquals, users)
}

func (s *RemoveUserSuite) TestTooManyArgs(c *gc.C) {
Expand All @@ -34,3 +39,16 @@ func (s *RemoveUserSuite) TestNotEnoughArgs(c *gc.C) {
_, err := testing.RunCommand(c, envcmd.Wrap(&RemoveUserCommand{}))
c.Assert(err, gc.ErrorMatches, `no username supplied`)
}

type mockRemoveUserAPI struct {
args []string
}

func (m *mockRemoveUserAPI) Close() error {
return nil
}

func (m *mockRemoveUserAPI) RemoveUser(user string) error {
m.args = append(m.args, user)
return nil
}

0 comments on commit 8ece4e6

Please sign in to comment.