0 package db
1
2 import (
3 "testing"
4 "gemigit/test"
5 )
6
7 const invalidGroupName = "mygroup$"
8
9 func TestCreateGroup(t *testing.T) {
10
11 initDB(t)
12
13 user, signature := createUserAndSession(t)
14 group := test.FuncName(t)
15
16 err := user.CreateGroup(group, signature + "a")
17 test.IsNotNil(t, err, "signature should be invalid")
18
19 err = user.CreateGroup(invalidGroupName, signature)
20 test.IsNotNil(t, err, "group name should be invalid")
21
22 test.IsNil(t, user.CreateGroup(group, signature))
23
24 err = user.CreateGroup(group, signature)
25 test.IsNotNil(t, err, "group name should already be taken")
26
27 }
28
29 func TestGetGroupID(t *testing.T) {
30
31 initDB(t)
32
33 user, signature := createUserAndSession(t)
34 group1 := test.FuncName(t) + "a"
35 group2 := test.FuncName(t) + "b"
36
37 test.IsNil(t, user.CreateGroup(group1, signature))
38 test.IsNil(t, user.CreateGroup(group2, signature))
39
40 id1, err := GetGroupID(group1)
41 test.IsNil(t, err)
42
43 id2, err := GetGroupID(group2)
44 test.IsNil(t, err)
45
46 id3, err := GetGroupID(group2)
47 test.IsNil(t, err)
48
49 test.IsNotEqual(t, id1, id2)
50 test.IsEqual(t, id2, id3)
51
52 _, err = GetGroupID(group2 + "a")
53 test.IsNotNil(t, err, "group should be invalid")
54
55 }
56
57 func TestSetGroupDescription(t *testing.T) {
58
59 initDB(t)
60
61 user, signature := createUserAndSession(t)
62 group := test.FuncName(t)
63
64 test.IsNil(t, user.CreateGroup(group, signature))
65
66 id, err := GetGroupID(group)
67 test.IsNil(t, err)
68
69 test.IsNil(t, SetGroupDescription(id, "description"))
70
71 test.IsNotNil(t, SetGroupDescription(-1, "description"),
72 "group id should be invalid")
73
74 test.IsNotNil(t, SetGroupDescription(id, tooLongDescription),
75 "description should be invalid")
76 }
77
78 func TestGetGroupDesc(t *testing.T) {
79
80 initDB(t)
81
82 user, signature := createUserAndSession(t)
83 group := test.FuncName(t)
84 description := group + "-description"
85
86 test.IsNil(t, user.CreateGroup(group, signature))
87
88 id, err := GetGroupID(group)
89 test.IsNil(t, err)
90
91 test.IsNil(t, SetGroupDescription(id, description))
92
93 desc, err := GetGroupDesc(group)
94 test.IsNil(t, err)
95
96 test.IsEqual(t, desc, description)
97
98 _, err = GetGroupDesc(group + "a")
99 test.IsNotNil(t, err, "group should be invalid")
100
101 }
102
103 func TestGetGroupOwner(t *testing.T) {
104
105 initDB(t)
106
107 user, signature := createUserAndSession(t)
108 group := test.FuncName(t)
109
110 test.IsNil(t, user.CreateGroup(group, signature))
111
112 owner, err := GetGroupOwner(group)
113 test.IsNil(t, err)
114
115 test.IsEqual(t, owner.Name, user.Name)
116
117 _, err = GetGroupOwner(group + "a")
118 test.IsNotNil(t, err, "group should be invalid")
119
120 }
121
122 func TestAddUserToGroup(t *testing.T) {
123
124 initDB(t)
125
126 member1, _ := createUserAndSession(t)
127 member2, _ := createUserAndSession(t)
128 user, signature := createUserAndSession(t)
129 group := test.FuncName(t)
130
131 test.IsNil(t, user.CreateGroup(group, signature))
132
133 test.IsNotNil(t, member1.AddUserToGroup(group, member1.Name),
134 "only the group owner can add members")
135
136 test.IsNil(t, user.AddUserToGroup(group, member1.Name))
137 test.IsNil(t, user.AddUserToGroup(group, member2.Name))
138 test.IsNotNil(t, user.AddUserToGroup(group, member2.Name),
139 "user should already be in the group")
140 test.IsNotNil(t, user.AddUserToGroup(group, member2.Name + "a"),
141 "user should be invalid")
142 test.IsNotNil(t, user.AddUserToGroup(group + "a", member2.Name),
143 "group should be invalid")
144
145 }
146
147 func TestDeleteGroup(t *testing.T) {
148
149 initDB(t)
150
151 member1, _ := createUserAndSession(t)
152 member2, _ := createUserAndSession(t)
153 user, signature := createUserAndSession(t)
154 group := test.FuncName(t)
155
156 test.IsNil(t, user.CreateGroup(group, signature))
157 id, err := GetGroupID(group)
158 test.IsNil(t, err)
159
160 test.IsNil(t, user.AddUserToGroup(group, member1.Name))
161 test.IsNil(t, DeleteGroup(id))
162 test.IsNotNil(t, DeleteGroup(id), "group should be invalid")
163 test.IsNotNil(t, user.AddUserToGroup(group, member2.Name),
164 "group should be invalid")
165
166 }
167
168 func TestDeleteMember(t *testing.T) {
169
170 initDB(t)
171
172 member1, _ := createUserAndSession(t)
173 member2, _ := createUserAndSession(t)
174 user, signature := createUserAndSession(t)
175 group := test.FuncName(t)
176
177 test.IsNil(t, user.CreateGroup(group, signature))
178 id, err := GetGroupID(group)
179 test.IsNil(t, err)
180
181 test.IsNil(t, user.AddUserToGroup(group, member1.Name))
182 test.IsNil(t, user.AddUserToGroup(group, member2.Name))
183
184 test.IsNil(t, DeleteMember(member1.ID, id))
185 test.IsNotNil(t, DeleteMember(member1.ID, id),
186 "user should already be deleted")
187
188 test.IsNil(t, DeleteMember(member2.ID, id))
189 test.IsNotNil(t, DeleteMember(member1.ID, id),
190 "user should already be deleted")
191
192 test.IsNotNil(t, DeleteMember(member1.ID, -1),
193 "group should be invalid")
194
195 test.IsNotNil(t, DeleteMember(-1, id),
196 "user should be invalid")
197
198 }
199
200 func TestIsInGroupID(t *testing.T) {
201
202 initDB(t)
203
204 member1, _ := createUserAndSession(t)
205 member2, _ := createUserAndSession(t)
206 user, signature := createUserAndSession(t)
207 group := test.FuncName(t)
208
209 test.IsNil(t, user.CreateGroup(group, signature))
210 id, err := GetGroupID(group)
211 test.IsNil(t, err)
212
213 test.IsNil(t, user.AddUserToGroup(group, member1.Name))
214
215 b, err := member1.IsInGroupID(id)
216 test.IsNil(t, err)
217 test.IsEqual(t, b, false)
218
219 b, err = member2.IsInGroupID(id)
220 test.IsNotNil(t, err, "should not be a member")
221
222 b, err = user.IsInGroupID(id)
223 test.IsNil(t, err)
224 test.IsEqual(t, b, true)
225
226 }
227
228 func TestIsInGroup(t *testing.T) {
229
230 initDB(t)
231
232 member1, _ := createUserAndSession(t)
233 member2, _ := createUserAndSession(t)
234 user, signature := createUserAndSession(t)
235 group := test.FuncName(t)
236
237 test.IsNil(t, user.CreateGroup(group, signature))
238
239 test.IsNil(t, user.AddUserToGroup(group, member1.Name))
240
241 b, err := member1.IsInGroup(group)
242 test.IsNil(t, err)
243 test.IsEqual(t, b, false)
244
245 b, err = member2.IsInGroup(group)
246 test.IsNotNil(t, err, "should not be a member")
247
248 b, err = user.IsInGroup(group)
249 test.IsNil(t, err)
250 test.IsEqual(t, b, true)
251
252 b, err = member2.IsInGroup(group + "a")
253 test.IsNotNil(t, err, "group should be invalid")
254 }
255
256 func TestGetGroups(t *testing.T) {
257
258 initDB(t)
259
260 member1, _ := createUserAndSession(t)
261 member2, _ := createUserAndSession(t)
262 user, signature := createUserAndSession(t)
263 group := test.FuncName(t)
264
265 test.IsNil(t, user.CreateGroup(group, signature))
266 test.IsNil(t, user.CreateGroup(group + "2", signature))
267
268 test.IsNil(t, user.AddUserToGroup(group, member1.Name))
269
270 groups, err := user.GetGroups()
271 test.IsNil(t, err)
272 test.IsEqual(t, len(groups), 2)
273
274 groups, err = member1.GetGroups()
275 test.IsNil(t, err)
276 test.IsEqual(t, len(groups), 1)
277
278 groups, err = member2.GetGroups()
279 test.IsNil(t, err)
280 test.IsEqual(t, len(groups), 0)
281
282 }
283
284 func TestGetMembers(t *testing.T) {
285
286 initDB(t)
287
288 member1, _ := createUserAndSession(t)
289 user, signature := createUserAndSession(t)
290 group := test.FuncName(t)
291
292 test.IsNil(t, user.CreateGroup(group, signature))
293 test.IsNil(t, user.CreateGroup(group + "2", signature))
294
295 members, err := user.GetMembers(group)
296 test.IsNil(t, err)
297 test.IsEqual(t, len(members), 1)
298
299 test.IsNil(t, user.AddUserToGroup(group, member1.Name))
300
301 members, err = user.GetMembers(group)
302 test.IsNil(t, err)
303 test.IsEqual(t, len(members), 2)
304
305 members, err = user.GetMembers(group + "a")
306 test.IsNotNil(t, err, "group should be invalid")
307 }
308