-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot_test.go
122 lines (99 loc) · 2.68 KB
/
bot_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
package browser
import (
"fmt"
"log"
"os"
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/yaml.v2"
)
// testBots is a map of user agent strings for each bot from the bots.yml file
var testBots map[string]string
// initTestBots reads the bots.yml file and
// unmarshals it into the testPlatforms map.
func initTestBots() {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("failed to get working directory: %v", err)
}
wd = strings.Split(wd, "/browser")[0]
yamlFile, err := os.ReadFile(fmt.Sprintf("%s/browser/assets/test/bots.yml", wd))
if err != nil {
log.Fatalf("failed to read file: %v", err)
}
var data map[string]string
if err := yaml.Unmarshal(yamlFile, &data); err != nil {
log.Fatalf("failed to unmarshal: %v", err)
}
testBots = data
}
func TestNewBot(t *testing.T) {
Convey("Subject: #NewBot", t, func() {
Convey("It returns a new Bot", func() {
b, err := NewBot("")
So(err, ShouldBeNil)
So(b, ShouldHaveSameTypeAs, &Bot{})
})
})
}
func TestBotIsBot(t *testing.T) {
Convey("Subject: #IsBot", t, func() {
Convey("When the user agent is a bot", func() {
Convey("When the user agent is empty", func() {
Convey("It returns true", func() {
b, _ := NewBot("")
So(b.IsBot(), ShouldBeTrue)
})
})
Convey("When the user agent has bot keywords", func() {
Convey("It returns true", func() {
b, _ := NewBot(testBots["monitoring"])
So(b.IsBot(), ShouldBeTrue)
})
})
Convey("When the user agent is a known bot", func() {
Convey("It returns true", func() {
b, _ := NewBot(testBots["apis-google"])
So(b.IsBot(), ShouldBeTrue)
})
})
})
})
}
func TestBotName(t *testing.T) {
Convey("Subject: #Name", t, func() {
Convey("When the user agent is a bot", func() {
Convey("When the user agent is empty", func() {
Convey("It returns Generic Bot", func() {
b, _ := NewBot("")
So(b.Name(), ShouldEqual, "Generic Bot")
})
})
Convey("When the user agent is a known bot", func() {
Convey("It returns the name of the bot", func() {
b, _ := NewBot(testBots["apis-google"])
So(b.Name(), ShouldEqual, "APIs-Google")
})
})
})
})
}
func TestBotWhy(t *testing.T) {
Convey("Subject: #Why", t, func() {
Convey("When the user agent is a bot", func() {
Convey("When the user agent is empty", func() {
Convey("It returns an empty string", func() {
b, _ := NewBot("")
So(b.Why(), ShouldEqual, "*bots.Empty")
})
})
Convey("When the user agent is a known bot", func() {
Convey("It returns the type of bot", func() {
b, _ := NewBot(testBots["apis-google"])
So(b.Why(), ShouldEqual, "*bots.Known")
})
})
})
})
}