1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
8
jc "github.com/juju/testing/checkers"
11
"github.com/juju/juju/apiserver/params"
12
"github.com/juju/juju/cmd/juju/model"
13
"github.com/juju/juju/jujuclient"
14
"github.com/juju/juju/jujuclient/jujuclienttesting"
15
"github.com/juju/juju/testing"
18
type grantRevokeSuite struct {
19
testing.FakeJujuXDGDataHomeSuite
20
fake *fakeGrantRevokeAPI
21
cmdFactory func(*fakeGrantRevokeAPI) cmd.Command
22
store *jujuclienttesting.MemStore
26
fooModelUUID = "0701e916-3274-46e4-bd12-c31aff89cee3"
27
barModelUUID = "0701e916-3274-46e4-bd12-c31aff89cee4"
28
bazModelUUID = "0701e916-3274-46e4-bd12-c31aff89cee5"
29
model1ModelUUID = "0701e916-3274-46e4-bd12-c31aff89cee6"
30
model2ModelUUID = "0701e916-3274-46e4-bd12-c31aff89cee7"
33
func (s *grantRevokeSuite) SetUpTest(c *gc.C) {
34
s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
35
s.fake = &fakeGrantRevokeAPI{}
37
// Set up the current controller, and write just enough info
38
// so we don't try to refresh
39
controllerName := "test-master"
41
s.store = jujuclienttesting.NewMemStore()
42
s.store.CurrentControllerName = controllerName
43
s.store.Controllers[controllerName] = jujuclient.ControllerDetails{}
44
s.store.Accounts[controllerName] = jujuclient.AccountDetails{
47
s.store.Models = map[string]*jujuclient.ControllerModels{
49
Models: map[string]jujuclient.ModelDetails{
50
"bob@local/foo": jujuclient.ModelDetails{fooModelUUID},
51
"bob@local/bar": jujuclient.ModelDetails{barModelUUID},
52
"bob@local/baz": jujuclient.ModelDetails{bazModelUUID},
53
"bob@local/model1": jujuclient.ModelDetails{model1ModelUUID},
54
"bob@local/model2": jujuclient.ModelDetails{model2ModelUUID},
60
func (s *grantRevokeSuite) run(c *gc.C, args ...string) (*cmd.Context, error) {
61
command := s.cmdFactory(s.fake)
62
return testing.RunCommand(c, command, args...)
65
func (s *grantRevokeSuite) TestPassesValues(c *gc.C) {
67
models := []string{fooModelUUID, barModelUUID, bazModelUUID}
68
_, err := s.run(c, "sam", "foo", "bar", "baz")
69
c.Assert(err, jc.ErrorIsNil)
70
c.Assert(s.fake.user, jc.DeepEquals, user)
71
c.Assert(s.fake.modelUUIDs, jc.DeepEquals, models)
72
c.Assert(s.fake.access, gc.Equals, "read")
75
func (s *grantRevokeSuite) TestAccess(c *gc.C) {
77
_, err := s.run(c, "--acl", "write", "sam", "model1", "model2")
78
c.Assert(err, jc.ErrorIsNil)
79
c.Assert(s.fake.user, jc.DeepEquals, sam)
80
c.Assert(s.fake.modelUUIDs, jc.DeepEquals, []string{model1ModelUUID, model2ModelUUID})
81
c.Assert(s.fake.access, gc.Equals, "write")
84
func (s *grantRevokeSuite) TestBlockGrant(c *gc.C) {
85
s.fake.err = ¶ms.Error{Code: params.CodeOperationBlocked}
86
_, err := s.run(c, "sam", "foo")
87
c.Assert(err, gc.Equals, cmd.ErrSilent)
88
c.Check(c.GetTestLog(), jc.Contains, "To unblock changes")
91
type grantSuite struct {
95
var _ = gc.Suite(&grantSuite{})
97
func (s *grantSuite) SetUpTest(c *gc.C) {
98
s.grantRevokeSuite.SetUpTest(c)
99
s.cmdFactory = func(fake *fakeGrantRevokeAPI) cmd.Command {
100
c, _ := model.NewGrantCommandForTest(fake, s.store)
105
func (s *grantSuite) TestInit(c *gc.C) {
106
wrappedCmd, grantCmd := model.NewGrantCommandForTest(s.fake, s.store)
107
err := testing.InitCommand(wrappedCmd, []string{})
108
c.Assert(err, gc.ErrorMatches, "no user specified")
110
err = testing.InitCommand(wrappedCmd, []string{"bob", "model1", "model2"})
111
c.Assert(err, jc.ErrorIsNil)
113
c.Assert(grantCmd.User, gc.Equals, "bob")
114
c.Assert(grantCmd.ModelNames, jc.DeepEquals, []string{"model1", "model2"})
116
err = testing.InitCommand(wrappedCmd, []string{})
117
c.Assert(err, gc.ErrorMatches, `no user specified`)
119
err = testing.InitCommand(wrappedCmd, []string{"nomodel"})
120
c.Assert(err, gc.ErrorMatches, `no model specified`)
123
type revokeSuite struct {
127
var _ = gc.Suite(&revokeSuite{})
129
func (s *revokeSuite) SetUpTest(c *gc.C) {
130
s.grantRevokeSuite.SetUpTest(c)
131
s.cmdFactory = func(fake *fakeGrantRevokeAPI) cmd.Command {
132
c, _ := model.NewRevokeCommandForTest(fake, s.store)
137
func (s *revokeSuite) TestInit(c *gc.C) {
138
wrappedCmd, revokeCmd := model.NewRevokeCommandForTest(s.fake, s.store)
139
err := testing.InitCommand(wrappedCmd, []string{})
140
c.Assert(err, gc.ErrorMatches, "no user specified")
142
err = testing.InitCommand(wrappedCmd, []string{"bob", "model1", "model2"})
143
c.Assert(err, jc.ErrorIsNil)
145
c.Assert(revokeCmd.User, gc.Equals, "bob")
146
c.Assert(revokeCmd.ModelNames, jc.DeepEquals, []string{"model1", "model2"})
148
err = testing.InitCommand(wrappedCmd, []string{})
149
c.Assert(err, gc.ErrorMatches, `no user specified`)
151
err = testing.InitCommand(wrappedCmd, []string{"nomodel"})
152
c.Assert(err, gc.ErrorMatches, `no model specified`)
155
type fakeGrantRevokeAPI struct {
162
func (f *fakeGrantRevokeAPI) Close() error { return nil }
164
func (f *fakeGrantRevokeAPI) GrantModel(user, access string, modelUUIDs ...string) error {
165
return f.fake(user, access, modelUUIDs...)
168
func (f *fakeGrantRevokeAPI) RevokeModel(user, access string, modelUUIDs ...string) error {
169
return f.fake(user, access, modelUUIDs...)
172
func (f *fakeGrantRevokeAPI) fake(user, access string, modelUUIDs ...string) error {
175
f.modelUUIDs = modelUUIDs