-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstore_test.go
127 lines (116 loc) · 3.16 KB
/
store_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
package main
import (
"bou.ke/monkey"
"net/http/httptest"
"reflect"
"testing"
"time"
)
var mockNow = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
func TestHashStruct(t *testing.T) {
var in = StoreEntry{"secret1", 5, 0, mockNow, 3, "authtoken"}
var wanted = "0Y3Mkcz36xM0hwrSnVw3PMebEMfa27Oi1mmuaELD4-Q"
got := hashStruct(in)
if got != wanted {
t.Errorf("got %v, wanted %v", got, wanted)
}
}
type StoreEntryInput struct {
secret string
maxClicks int
validFor int
authToken string
id string
}
type StoreEntryOutput struct {
StoreEntry
id string
}
type StoreEntryTestPair struct {
in StoreEntryInput
out StoreEntryOutput
}
var StoreEntryTestPairs = []StoreEntryTestPair{
{
StoreEntryInput{"secret1", 5, 3, "authtoken", "id1"},
StoreEntryOutput{StoreEntry{"secret1", 5, 0, mockNow, 3, "authtoken"}, "id1"},
},
{
StoreEntryInput{"secret2", 2, 3, "authtoken", ""},
StoreEntryOutput{StoreEntry{"secret2", 2, 0, mockNow, 3, "authtoken"}, "iLbLBYFzULLUfB84p8VHldWd4VnHg0mZq_5S45p0lEk"},
},
}
func TestStore_NewEntry(t *testing.T) {
monkey.Patch(time.Now, func() time.Time {
return mockNow
})
defer monkey.Unpatch(time.Now)
store := make(secretStore)
for _, p := range StoreEntryTestPairs {
outId := store.NewEntry(p.in.secret, p.in.maxClicks, p.in.validFor, p.in.authToken, p.in.id)
if outId != p.out.id {
t.Errorf("got %v, wanted %v", outId, p.out.id)
}
outEntry, ok := store.GetEntry(outId)
if !ok {
t.Errorf("new entry not found under %v", outId)
}
if !reflect.DeepEqual(p.out.StoreEntry, outEntry) {
t.Errorf("got %v, wanted %v", p.out.StoreEntry, outEntry)
}
}
}
func TestStore_GetEntryInfo(t *testing.T) {
store := make(secretStore)
store.NewEntry("secret", 1, 1, "auth", "testid")
out, ok := store.GetEntryInfoHidden("testid")
if !ok {
t.Errorf("new entry not found under %v", "testid")
}
wanted := "http://localhost:/api/v1/get/testid"
if out.ApiUrl != wanted {
t.Errorf("got %v, wanted %v", out.ApiUrl, wanted)
}
wanted = "http://localhost:/g?id=testid"
if out.Url != wanted {
t.Errorf("got %v, wanted %v", out.Url, wanted)
}
wanted = hiddenString
if out.Secret != wanted {
t.Errorf("got %v, wanted %v", out.Secret, wanted)
}
}
func TestStore_Click(t *testing.T) {
clicks := 2
store := make(secretStore)
store.NewEntry("secret", clicks, 1, "auth", "testid")
_, ok := store.GetEntry("testid")
if !ok {
t.Errorf("new entry not found under %v", "testid")
}
req := httptest.NewRequest("GET", "/testid", nil)
for i := 0; i < clicks; i++ {
store.Click("testid", req)
}
_, ok = store.GetEntry("testid")
if ok {
t.Errorf("new entry found under %v, but it should not be there", "testid")
}
}
func TestStore_Expiry(t *testing.T) {
store := make(secretStore)
store.NewEntry("secret", 1, 150, "auth", "testid")
_, ok := store.GetEntry("testid")
if !ok {
t.Errorf("new entry not found under %v", "testid")
}
expFactor = func(v int) time.Duration {
return time.Millisecond * time.Duration(v)
}
go store.Expiry(time.Millisecond * 200)
time.Sleep(time.Millisecond * 300)
_, ok = store.GetEntry("testid")
if ok {
t.Errorf("new entry found under %v, but it should be expired", "testid")
}
}