-
Notifications
You must be signed in to change notification settings - Fork 0
/
envuser_test.go
197 lines (166 loc) · 7.34 KB
/
envuser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
"fmt"
"sort"
"github.com/juju/errors"
"github.com/juju/names"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils"
gc "gopkg.in/check.v1"
"github.com/juju/juju/state"
"github.com/juju/juju/testing"
"github.com/juju/juju/testing/factory"
)
type EnvUserSuite struct {
ConnSuite
}
var _ = gc.Suite(&EnvUserSuite{})
func (s *EnvUserSuite) TestAddEnvironmentUser(c *gc.C) {
now := state.NowToTheSecond()
user := s.factory.MakeUser(c, &factory.UserParams{Name: "validusername", NoEnvUser: true})
createdBy := s.factory.MakeUser(c, &factory.UserParams{Name: "createdby"})
envUser, err := s.State.AddEnvironmentUser(user.UserTag(), createdBy.UserTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(envUser.ID(), gc.Equals, fmt.Sprintf("%s:validusername@local", s.envTag.Id()))
c.Assert(envUser.EnvironmentTag(), gc.Equals, s.envTag)
c.Assert(envUser.UserName(), gc.Equals, "validusername@local")
c.Assert(envUser.DisplayName(), gc.Equals, user.DisplayName())
c.Assert(envUser.CreatedBy(), gc.Equals, "createdby@local")
c.Assert(envUser.DateCreated().Equal(now) || envUser.DateCreated().After(now), jc.IsTrue)
c.Assert(envUser.LastConnection(), gc.IsNil)
envUser, err = s.State.EnvironmentUser(user.UserTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(envUser.ID(), gc.Equals, fmt.Sprintf("%s:validusername@local", s.envTag.Id()))
c.Assert(envUser.EnvironmentTag(), gc.Equals, s.envTag)
c.Assert(envUser.UserName(), gc.Equals, "validusername@local")
c.Assert(envUser.DisplayName(), gc.Equals, user.DisplayName())
c.Assert(envUser.CreatedBy(), gc.Equals, "createdby@local")
c.Assert(envUser.DateCreated().Equal(now) || envUser.DateCreated().After(now), jc.IsTrue)
c.Assert(envUser.LastConnection(), gc.IsNil)
}
func (s *EnvUserSuite) TestAddEnvironmentNoUserFails(c *gc.C) {
createdBy := s.factory.MakeUser(c, &factory.UserParams{Name: "createdby"})
_, err := s.State.AddEnvironmentUser(names.NewLocalUserTag("validusername"), createdBy.UserTag())
c.Assert(err, gc.ErrorMatches, `user "validusername" does not exist locally: user "validusername" not found`)
}
func (s *EnvUserSuite) TestAddEnvironmentNoCreatedByUserFails(c *gc.C) {
user := s.factory.MakeUser(c, &factory.UserParams{Name: "validusername"})
_, err := s.State.AddEnvironmentUser(user.UserTag(), names.NewLocalUserTag("createdby"))
c.Assert(err, gc.ErrorMatches, `createdBy user "createdby" does not exist locally: user "createdby" not found`)
}
func (s *EnvUserSuite) TestRemoveEnvironmentUser(c *gc.C) {
user := s.factory.MakeUser(c, &factory.UserParams{Name: "validusername"})
_, err := s.State.EnvironmentUser(user.UserTag())
c.Assert(err, jc.ErrorIsNil)
err = s.State.RemoveEnvironmentUser(user.UserTag())
c.Assert(err, jc.ErrorIsNil)
_, err = s.State.EnvironmentUser(user.UserTag())
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}
func (s *EnvUserSuite) TestRemoveEnvironmentUserFails(c *gc.C) {
user := s.factory.MakeUser(c, &factory.UserParams{NoEnvUser: true})
err := s.State.RemoveEnvironmentUser(user.UserTag())
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}
func (s *EnvUserSuite) TestUpdateLastConnection(c *gc.C) {
now := state.NowToTheSecond()
createdBy := s.factory.MakeUser(c, &factory.UserParams{Name: "createdby"})
user := s.factory.MakeUser(c, &factory.UserParams{Name: "validusername", Creator: createdBy.Tag()})
envUser, err := s.State.EnvironmentUser(user.UserTag())
c.Assert(err, jc.ErrorIsNil)
err = envUser.UpdateLastConnection()
c.Assert(err, jc.ErrorIsNil)
// It is possible that the update is done over a second boundary, so we need
// to check for after now as well as equal.
c.Assert(envUser.LastConnection().After(now) ||
envUser.LastConnection().Equal(now), jc.IsTrue)
}
func (s *EnvUserSuite) TestEnvironmentsForUserNone(c *gc.C) {
tag := names.NewUserTag("non-existent@remote")
environments, err := s.State.EnvironmentsForUser(tag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(environments, gc.HasLen, 0)
}
func (s *EnvUserSuite) TestEnvironmentsForUserNewLocalUser(c *gc.C) {
user := s.factory.MakeUser(c, &factory.UserParams{NoEnvUser: true})
environments, err := s.State.EnvironmentsForUser(user.UserTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(environments, gc.HasLen, 0)
}
func (s *EnvUserSuite) TestEnvironmentsForUser(c *gc.C) {
user := s.factory.MakeUser(c, nil)
environments, err := s.State.EnvironmentsForUser(user.UserTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(environments, gc.HasLen, 1)
c.Assert(environments[0].UUID(), gc.Equals, s.State.EnvironUUID())
}
func (s *EnvUserSuite) newEnvWithOwner(c *gc.C, name string, owner names.UserTag) *state.Environment {
// Don't use the factory to call MakeEnvironment because it may at some
// time in the future be modified to do additional things. Instead call
// the state method directly to create an environment to make sure that
// the owner is able to access the environment.
uuid, err := utils.NewUUID()
c.Assert(err, jc.ErrorIsNil)
cfg := testing.CustomEnvironConfig(c, testing.Attrs{
"name": name,
"uuid": uuid.String(),
})
env, st, err := s.State.NewEnvironment(cfg, owner)
c.Assert(err, jc.ErrorIsNil)
defer st.Close()
return env
}
func (s *EnvUserSuite) TestEnvironmentsForUserEnvOwner(c *gc.C) {
owner := names.NewUserTag("external@remote")
env := s.newEnvWithOwner(c, "test-env", owner)
environments, err := s.State.EnvironmentsForUser(owner)
c.Assert(err, jc.ErrorIsNil)
c.Assert(environments, gc.HasLen, 1)
s.checkSameEnvironment(c, environments[0], env)
}
func (s *EnvUserSuite) checkSameEnvironment(c *gc.C, env1, env2 *state.Environment) {
c.Check(env1.Name(), gc.Equals, env2.Name())
c.Check(env1.UUID(), gc.Equals, env2.UUID())
}
func (s *EnvUserSuite) newEnvWithUser(c *gc.C, name string, user names.UserTag) *state.Environment {
envState := s.factory.MakeEnvironment(c, &factory.EnvParams{Name: name})
defer envState.Close()
newEnv, err := envState.Environment()
c.Assert(err, jc.ErrorIsNil)
_, err = envState.AddEnvironmentUser(user, newEnv.Owner())
c.Assert(err, jc.ErrorIsNil)
return newEnv
}
func (s *EnvUserSuite) TestEnvironmentsForUserOfNewEnv(c *gc.C) {
userTag := names.NewUserTag("external@remote")
env := s.newEnvWithUser(c, "test-env", userTag)
environments, err := s.State.EnvironmentsForUser(userTag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(environments, gc.HasLen, 1)
s.checkSameEnvironment(c, environments[0], env)
}
func (s *EnvUserSuite) TestEnvironmentsForUserMultiple(c *gc.C) {
userTag := names.NewUserTag("external@remote")
expected := []*state.Environment{
s.newEnvWithUser(c, "user1", userTag),
s.newEnvWithUser(c, "user2", userTag),
s.newEnvWithUser(c, "user3", userTag),
s.newEnvWithOwner(c, "owner1", userTag),
s.newEnvWithOwner(c, "owner2", userTag),
}
sort.Sort(UUIDOrder(expected))
environments, err := s.State.EnvironmentsForUser(userTag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(environments, gc.HasLen, len(expected))
sort.Sort(UUIDOrder(environments))
for i := range expected {
s.checkSameEnvironment(c, environments[i], expected[i])
}
}
// UUIDOrder is used to sort the environments into a stable order
type UUIDOrder []*state.Environment
func (a UUIDOrder) Len() int { return len(a) }
func (a UUIDOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a UUIDOrder) Less(i, j int) bool { return a[i].UUID() < a[j].UUID() }