forked from johnsto/go-passwordless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_mem_test.go
72 lines (58 loc) · 1.51 KB
/
store_mem_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
package passwordless
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMemStore(t *testing.T) {
ms := NewMemStore()
assert.NotNil(t, ms)
b, exp, err := ms.Exists(nil, "uid")
assert.False(t, b)
assert.True(t, exp.IsZero())
assert.NoError(t, err)
err = ms.Store(nil, "", "uid", -time.Hour)
b, exp, err = ms.Exists(nil, "uid")
assert.False(t, b)
assert.True(t, exp.IsZero())
assert.NoError(t, err)
err = ms.Store(nil, "", "uid", time.Hour)
b, exp, err = ms.Exists(nil, "uid")
assert.True(t, b)
assert.False(t, exp.IsZero())
assert.NoError(t, err)
// Test keys are expired correctly
err = ms.Store(nil, "", "expuid", time.Second)
assert.NoError(t, err)
b, _, _ = ms.Exists(nil, "expuid")
assert.True(t, b)
time.Sleep(time.Second)
ms.Clean()
b, _, _ = ms.Exists(nil, "expuid")
assert.False(t, b)
// Clean up
ms.Release()
time.Sleep(2 * time.Second)
}
func TestMemStoreVerify(t *testing.T) {
ms := NewMemStore()
assert.NotNil(t, ms)
// Token doesn't exist
b, err := ms.Verify(nil, "badtoken", "uid")
assert.False(t, b)
assert.Equal(t, ErrTokenNotFound, err)
// Token expired
err = ms.Store(nil, "", "uid", -time.Hour)
b, err = ms.Verify(nil, "badtoken", "uid")
assert.False(t, b)
assert.Equal(t, ErrTokenNotFound, err)
// Token wrong
err = ms.Store(nil, "token", "uid", time.Hour)
b, err = ms.Verify(nil, "badtoken", "uid")
assert.False(t, b)
assert.NoError(t, err)
// Token correct
b, err = ms.Verify(nil, "token", "uid")
assert.True(t, b)
assert.NoError(t, err)
}