Skip to content

Commit f75b2a3

Browse files
committed
session: use clock for created at time
1 parent 1d4f923 commit f75b2a3

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

session/store_test.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ var testTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
1616
// store.
1717
func TestBasicSessionStore(t *testing.T) {
1818
// Set up a new DB.
19-
db, err := NewDB(t.TempDir(), "test.db", clock.NewTestClock(testTime))
19+
clock := clock.NewTestClock(testTime)
20+
db, err := NewDB(t.TempDir(), "test.db", clock)
2021
require.NoError(t, err)
2122
t.Cleanup(func() {
2223
_ = db.Close()
2324
})
2425

2526
// Create a few sessions.
26-
s1 := newSession(t, db, "session 1", nil)
27-
s2 := newSession(t, db, "session 2", nil)
28-
s3 := newSession(t, db, "session 3", nil)
29-
s4 := newSession(t, db, "session 4", nil)
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)
3031

3132
// Persist session 1. This should now succeed.
3233
require.NoError(t, db.CreateSession(s1))
@@ -92,17 +93,18 @@ func TestBasicSessionStore(t *testing.T) {
9293
// TestLinkingSessions tests that session linking works as expected.
9394
func TestLinkingSessions(t *testing.T) {
9495
// Set up a new DB.
95-
db, err := NewDB(t.TempDir(), "test.db", clock.NewTestClock(testTime))
96+
clock := clock.NewTestClock(testTime)
97+
db, err := NewDB(t.TempDir(), "test.db", clock)
9698
require.NoError(t, err)
9799
t.Cleanup(func() {
98100
_ = db.Close()
99101
})
100102

101103
// Create a new session with no previous link.
102-
s1 := newSession(t, db, "session 1", nil)
104+
s1 := newSession(t, db, clock, "session 1", nil)
103105

104106
// Create another session and link it to the first.
105-
s2 := newSession(t, db, "session 2", &s1.GroupID)
107+
s2 := newSession(t, db, clock, "session 2", &s1.GroupID)
106108

107109
// Try to persist the second session and assert that it fails due to the
108110
// linked session not existing in the DB yet.
@@ -128,7 +130,8 @@ func TestLinkingSessions(t *testing.T) {
128130
// of the GetGroupID and GetSessionIDs methods.
129131
func TestLinkedSessions(t *testing.T) {
130132
// Set up a new DB.
131-
db, err := NewDB(t.TempDir(), "test.db", clock.NewTestClock(testTime))
133+
clock := clock.NewTestClock(testTime)
134+
db, err := NewDB(t.TempDir(), "test.db", clock)
132135
require.NoError(t, err)
133136
t.Cleanup(func() {
134137
_ = db.Close()
@@ -138,9 +141,9 @@ func TestLinkedSessions(t *testing.T) {
138141
// after are all linked to the prior one. All these sessions belong to
139142
// the same group. The group ID is equivalent to the session ID of the
140143
// first session.
141-
s1 := newSession(t, db, "session 1", nil)
142-
s2 := newSession(t, db, "session 2", &s1.GroupID)
143-
s3 := newSession(t, db, "session 3", &s2.GroupID)
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)
144147

145148
// Persist the sessions.
146149
require.NoError(t, db.CreateSession(s1))
@@ -166,8 +169,8 @@ func TestLinkedSessions(t *testing.T) {
166169

167170
// To ensure that different groups don't interfere with each other,
168171
// let's add another set of linked sessions not linked to the first.
169-
s4 := newSession(t, db, "session 4", nil)
170-
s5 := newSession(t, db, "session 5", &s4.GroupID)
172+
s4 := newSession(t, db, clock, "session 4", nil)
173+
s5 := newSession(t, db, clock, "session 5", &s4.GroupID)
171174

172175
require.NotEqual(t, s4.GroupID, s1.GroupID)
173176

@@ -195,7 +198,8 @@ func TestLinkedSessions(t *testing.T) {
195198
// method correctly checks if each session in a group passes a predicate.
196199
func TestCheckSessionGroupPredicate(t *testing.T) {
197200
// Set up a new DB.
198-
db, err := NewDB(t.TempDir(), "test.db", clock.NewTestClock(testTime))
201+
clock := clock.NewTestClock(testTime)
202+
db, err := NewDB(t.TempDir(), "test.db", clock)
199203
require.NoError(t, err)
200204
t.Cleanup(func() {
201205
_ = db.Close()
@@ -205,7 +209,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
205209
// function is checked correctly.
206210

207211
// Add a new session to the DB.
208-
s1 := newSession(t, db, "label 1", nil)
212+
s1 := newSession(t, db, clock, "label 1", nil)
209213
require.NoError(t, db.CreateSession(s1))
210214

211215
// Check that the group passes against an appropriate predicate.
@@ -230,7 +234,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
230234
require.NoError(t, db.RevokeSession(s1.LocalPublicKey))
231235

232236
// Add a new session to the same group as the first one.
233-
s2 := newSession(t, db, "label 2", &s1.GroupID)
237+
s2 := newSession(t, db, clock, "label 2", &s1.GroupID)
234238
require.NoError(t, db.CreateSession(s2))
235239

236240
// Check that the group passes against an appropriate predicate.
@@ -252,7 +256,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
252256
require.False(t, ok)
253257

254258
// Add a new session that is not linked to the first one.
255-
s3 := newSession(t, db, "completely different", nil)
259+
s3 := newSession(t, db, clock, "completely different", nil)
256260
require.NoError(t, db.CreateSession(s3))
257261

258262
// Ensure that the first group is unaffected.
@@ -282,15 +286,15 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
282286
require.True(t, ok)
283287
}
284288

285-
func newSession(t *testing.T, db Store, label string,
289+
func newSession(t *testing.T, db Store, clock clock.Clock, label string,
286290
linkedGroupID *ID) *Session {
287291

288292
id, priv, err := db.GetUnusedIDAndKeyPair()
289293
require.NoError(t, err)
290294

291295
session, err := buildSession(
292296
id, priv, label, TypeMacaroonAdmin,
293-
time.Now(),
297+
clock.Now(),
294298
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
295299
"foo.bar.baz:1234", true, nil, nil, nil, true, linkedGroupID,
296300
[]PrivacyFlag{ClearPubkeys},

0 commit comments

Comments
 (0)