@@ -24,10 +24,10 @@ func TestBasicSessionStore(t *testing.T) {
24
24
})
25
25
26
26
// Create a few sessions.
27
- s1 := newSession (t , db , clock , "session 1" , nil )
28
- s2 := newSession (t , db , clock , "session 2" , nil )
29
- s3 := newSession (t , db , clock , "session 3" , nil )
30
- s4 := newSession (t , db , clock , "session 4" , nil )
27
+ s1 := newSession (t , db , clock , "session 1" )
28
+ s2 := newSession (t , db , clock , "session 2" )
29
+ s3 := newSession (t , db , clock , "session 3" )
30
+ s4 := newSession (t , db , clock , "session 4" )
31
31
32
32
// Persist session 1. This should now succeed.
33
33
require .NoError (t , db .CreateSession (s1 ))
@@ -101,10 +101,10 @@ func TestLinkingSessions(t *testing.T) {
101
101
})
102
102
103
103
// Create a new session with no previous link.
104
- s1 := newSession (t , db , clock , "session 1" , nil )
104
+ s1 := newSession (t , db , clock , "session 1" )
105
105
106
106
// Create another session and link it to the first.
107
- s2 := newSession (t , db , clock , "session 2" , & s1 .GroupID )
107
+ s2 := newSession (t , db , clock , "session 2" , withLinkedGroupID ( & s1 .GroupID ) )
108
108
109
109
// Try to persist the second session and assert that it fails due to the
110
110
// linked session not existing in the DB yet.
@@ -141,9 +141,9 @@ func TestLinkedSessions(t *testing.T) {
141
141
// after are all linked to the prior one. All these sessions belong to
142
142
// the same group. The group ID is equivalent to the session ID of the
143
143
// first session.
144
- s1 := newSession (t , db , clock , "session 1" , nil )
145
- s2 := newSession (t , db , clock , "session 2" , & s1 .GroupID )
146
- s3 := newSession (t , db , clock , "session 3" , & s2 .GroupID )
144
+ s1 := newSession (t , db , clock , "session 1" )
145
+ s2 := newSession (t , db , clock , "session 2" , withLinkedGroupID ( & s1 .GroupID ) )
146
+ s3 := newSession (t , db , clock , "session 3" , withLinkedGroupID ( & s2 .GroupID ) )
147
147
148
148
// Persist the sessions.
149
149
require .NoError (t , db .CreateSession (s1 ))
@@ -169,8 +169,8 @@ func TestLinkedSessions(t *testing.T) {
169
169
170
170
// To ensure that different groups don't interfere with each other,
171
171
// let's add another set of linked sessions not linked to the first.
172
- s4 := newSession (t , db , clock , "session 4" , nil )
173
- s5 := newSession (t , db , clock , "session 5" , & s4 .GroupID )
172
+ s4 := newSession (t , db , clock , "session 4" )
173
+ s5 := newSession (t , db , clock , "session 5" , withLinkedGroupID ( & s4 .GroupID ) )
174
174
175
175
require .NotEqual (t , s4 .GroupID , s1 .GroupID )
176
176
@@ -209,7 +209,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
209
209
// function is checked correctly.
210
210
211
211
// Add a new session to the DB.
212
- s1 := newSession (t , db , clock , "label 1" , nil )
212
+ s1 := newSession (t , db , clock , "label 1" )
213
213
require .NoError (t , db .CreateSession (s1 ))
214
214
215
215
// Check that the group passes against an appropriate predicate.
@@ -234,7 +234,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
234
234
require .NoError (t , db .RevokeSession (s1 .LocalPublicKey ))
235
235
236
236
// Add a new session to the same group as the first one.
237
- s2 := newSession (t , db , clock , "label 2" , & s1 .GroupID )
237
+ s2 := newSession (t , db , clock , "label 2" , withLinkedGroupID ( & s1 .GroupID ) )
238
238
require .NoError (t , db .CreateSession (s2 ))
239
239
240
240
// Check that the group passes against an appropriate predicate.
@@ -256,7 +256,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
256
256
require .False (t , ok )
257
257
258
258
// Add a new session that is not linked to the first one.
259
- s3 := newSession (t , db , clock , "completely different" , nil )
259
+ s3 := newSession (t , db , clock , "completely different" )
260
260
require .NoError (t , db .CreateSession (s3 ))
261
261
262
262
// Ensure that the first group is unaffected.
@@ -286,8 +286,18 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
286
286
require .True (t , ok )
287
287
}
288
288
289
+ // testSessionModifier is a functional option that can be used to modify the
290
+ // default test session created by newSession.
291
+ type testSessionModifier func (* Session )
292
+
293
+ func withLinkedGroupID (groupID * ID ) testSessionModifier {
294
+ return func (s * Session ) {
295
+ s .GroupID = * groupID
296
+ }
297
+ }
298
+
289
299
func newSession (t * testing.T , db Store , clock clock.Clock , label string ,
290
- linkedGroupID * ID ) * Session {
300
+ mods ... testSessionModifier ) * Session {
291
301
292
302
id , priv , err := db .GetUnusedIDAndKeyPair ()
293
303
require .NoError (t , err )
@@ -296,11 +306,15 @@ func newSession(t *testing.T, db Store, clock clock.Clock, label string,
296
306
id , priv , label , TypeMacaroonAdmin ,
297
307
clock .Now (),
298
308
time .Date (99999 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
299
- "foo.bar.baz:1234" , true , nil , nil , nil , true , linkedGroupID ,
309
+ "foo.bar.baz:1234" , true , nil , nil , nil , true , nil ,
300
310
[]PrivacyFlag {ClearPubkeys },
301
311
)
302
312
require .NoError (t , err )
303
313
314
+ for _ , mod := range mods {
315
+ mod (session )
316
+ }
317
+
304
318
return session
305
319
}
306
320
0 commit comments