-
Notifications
You must be signed in to change notification settings - Fork 0
/
withSecCheck_test.go
89 lines (71 loc) · 1.92 KB
/
withSecCheck_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
package main
import (
"errors"
"github.com/geofflane/tzone-go/data"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"time"
)
type TestUserDb struct {
Users map[string]data.User
Usage int
Done chan bool
}
func (db *TestUserDb) Close() error {
// No op
return nil
}
func (db *TestUserDb) Authenticate(token string) (data.User, error) {
u, ok := db.Users[token]
if !ok {
return u, errors.New("No such user")
}
return u, nil
}
func (db *TestUserDb) RecordUsage(u data.User) {
db.Usage += 1
db.Done <- true
}
func buildHttpParams() (w http.ResponseWriter, r *http.Request) {
w = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "http://example.com?token=test", nil)
return
}
func buildHandlerFunc(called *bool) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
*called = true
})
}
func waitForDone(c chan bool) {
// record usage is async, so need to wait for it to be set
// supposedly this would handle a timeout
select {
case <-time.After(10 * time.Nanosecond):
println("Timeout")
case _ = <-c:
println("Done")
}
}
func TestPassingGoodTokenCallsBaseMethod(t *testing.T) {
var called bool
doneChan := make(chan bool)
defer close(doneChan)
w, req := buildHttpParams()
userDb := &TestUserDb{map[string]data.User{"test": data.User{}}, 0, doneChan}
secCheck := WithSecurityCheck{userDb, buildHandlerFunc(&called)}
secCheck.ServeHTTP(w, req)
waitForDone(doneChan)
assert.True(t, called, "handler was not called")
assert.Equal(t, 1, userDb.Usage)
}
func TestPassingBadTokenDoesNotCallBaseMethod(t *testing.T) {
var called bool
w, req := buildHttpParams()
userDb := &TestUserDb{map[string]data.User{"different": data.User{}}, 0, nil}
secCheck := WithSecurityCheck{userDb, buildHandlerFunc(&called)}
secCheck.ServeHTTP(w, req)
assert.False(t, called, "handler was not supposed to be called")
assert.Equal(t, 0, userDb.Usage)
}