generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
manager_test.go
136 lines (115 loc) · 3.02 KB
/
manager_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
128
129
130
131
132
133
134
135
136
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package captchas
import (
"context"
"errors"
"html/template"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCaseSensitive(t *testing.T) {
m := &Manager{}
CaseSensitive(true)(m)
assert.True(t, m.caseSensitive)
CaseSensitive(false)(m)
assert.False(t, m.caseSensitive)
}
func TestManagerIsEqual(t *testing.T) {
m := &Manager{}
assert.False(t, m.isEqual("foo", ""))
assert.False(t, m.isEqual("", "foo"))
CaseSensitive(true)(m)
assert.False(t, m.isEqual("foo", "Foo"))
CaseSensitive(false)(m)
assert.True(t, m.isEqual("foo", "Foo"))
}
type testStore struct {
errGet error
errSet error
}
func (s *testStore) Get(ctx context.Context, id string, clear bool) (string, error) {
if s.errGet != nil {
return "", s.errGet
}
if clear {
return "getAndDel", nil
}
return "get", nil
}
func (s *testStore) Set(ctx context.Context, id, answer string) error {
if s.errSet != nil {
return s.errSet
}
return nil
}
type testDriver struct {
captcha Captcha
}
func (d *testDriver) Generate() (Captcha, error) {
if d.captcha != nil {
return d.captcha, nil
}
return nil, errors.New("unsupport to generate captcha")
}
type testCaptcha struct {
id string
answer string
}
func (c *testCaptcha) ID() string {
return c.id
}
func (c *testCaptcha) Answer() string {
return c.answer
}
func (c *testCaptcha) EncodeToString() string {
return ""
}
func (c *testCaptcha) HTMLField(fieldName string) template.HTML {
return template.HTML("")
}
func TestNew(t *testing.T) {
store := &testStore{}
driver := &testDriver{}
m := New(store, driver, CaseSensitive(false))
assert.False(t, m.caseSensitive)
assert.Equal(t, store, m.store)
assert.Equal(t, driver, m.driver)
}
func TestManagerGenerate(t *testing.T) {
driver := &testDriver{}
m := New(&testStore{}, driver)
captcha1, err1 := m.Generate(nil)
captcha2, err2 := driver.Generate()
assert.Equal(t, captcha2, captcha1)
assert.Equal(t, err2, err1)
expectedCaptcha := &testCaptcha{id: "foo", answer: "bar"}
m.driver = &testDriver{captcha: expectedCaptcha}
captcha, err := m.Generate(nil)
assert.Nil(t, err)
assert.Equal(t, expectedCaptcha, captcha)
}
func TestManagerGet(t *testing.T) {
store := &testStore{}
m := New(store, &testDriver{})
for _, clear := range []bool{true, false} {
val1, err1 := m.Get(context.TODO(), "foo", clear)
val2, err2 := store.Get(context.TODO(), "foo", clear)
assert.Equal(t, val2, val1)
assert.Equal(t, err2, err1)
}
}
func TestManagerVerify(t *testing.T) {
store := &testStore{}
m := New(store, &testDriver{})
for _, clear := range []bool{true, false} {
err1 := m.Verify(nil, "foo", "bar", clear)
assert.Equal(t, ErrCaptchaIncorrect, err1)
}
assert.Nil(t, m.Verify(nil, "", "get", false))
assert.Nil(t, m.Verify(nil, "", "getAndDel", true))
errGet := errors.New("failed to get answer")
m.store = &testStore{errGet: errGet}
assert.Equal(t, errGet, m.Verify(nil, "", "", false))
}