generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcaptcha_test.go
90 lines (78 loc) · 2.21 KB
/
captcha_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
// 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 drivers
import (
"fmt"
"html/template"
"strings"
"testing"
"github.com/mojocn/base64Captcha"
)
func TestCaptcha(t *testing.T) {
id := "test"
answer := "expected"
tag := htmlTagIMG
var item base64Captcha.Item
captcha := newCaptcha(id, answer, tag, item)
if captcha.ID() != id {
t.Errorf("expected ID %s, got %s", id, captcha.ID())
}
if captcha.Answer() != answer {
t.Errorf("expected answer %s, got %s", answer, captcha.Answer())
}
if captcha.tag != tag {
t.Errorf("expected tag %s, got %s", tag, captcha.tag)
}
if captcha.item != item {
t.Errorf("expected item %v, got %v", item, captcha.item)
}
}
func TestCaptchaEncodeToString(t *testing.T) {
driver := NewDigit()
c, err := driver.Generate()
if err != nil {
t.Fatal(err)
}
shadowC, _ := c.(*Captcha)
if c.EncodeToString() != shadowC.item.EncodeB64string() {
t.Errorf("expected base64 encode string %q, got %q", shadowC.item.EncodeB64string(), c.EncodeToString())
}
}
func TestCaptchaMediaAttr(t *testing.T) {
driver := NewDigit()
c, err := driver.Generate()
if err != nil {
t.Fatal(err)
}
shadowC, _ := c.(*Captcha)
expected := template.HTMLAttr(fmt.Sprintf(`src="%s"`, shadowC.item.EncodeB64string()))
if shadowC.MediaAttr() != expected {
t.Errorf("expected media attr %v, got %v", expected, shadowC.MediaAttr())
}
}
func TestCaptchaHTMLField(t *testing.T) {
driver := NewDigit()
c, err := driver.Generate()
if err != nil {
t.Fatal(err)
}
fieldName := "captcha_id"
content := string(c.HTMLField(fieldName))
if !strings.Contains(content, `name="`+fieldName+`"`) {
t.Errorf("HTML output doesn't contains input field")
}
if !strings.Contains(content, `src="`+c.EncodeToString()+`"`) {
t.Errorf("HTML output doesn't contains media field")
}
}
func TestCaptchaIsTagAudio(t *testing.T) {
imgCaptcha := &Captcha{tag: htmlTagIMG}
if imgCaptcha.IsTagAudio() {
t.Errorf("expected %t, got %t", true, imgCaptcha.IsTagAudio())
}
audioCaptcha := &Captcha{tag: htmlTagAudio}
if !audioCaptcha.IsTagAudio() {
t.Errorf("expected %t, got %t", true, audioCaptcha.IsTagAudio())
}
}