forked from 0xERR0R/blocky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocking_test.go
171 lines (162 loc) · 5.22 KB
/
blocking_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package cmd
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"github.com/sirupsen/logrus/hooks/test"
"github.com/0xERR0R/blocky/api"
"github.com/0xERR0R/blocky/log"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Blocking command", func() {
var (
ts *httptest.Server
mockFn func(w http.ResponseWriter, _ *http.Request)
loggerHook *test.Hook
)
JustBeforeEach(func() {
ts = testHTTPAPIServer(mockFn)
})
JustAfterEach(func() {
ts.Close()
})
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {}
loggerHook = test.NewGlobal()
log.Log().AddHook(loggerHook)
})
AfterEach(func() {
loggerHook.Reset()
})
Describe("enable blocking", func() {
When("Enable blocking is called via REST", func() {
It("should enable the blocking status", func() {
Expect(disableBlocking(newBlockingCommand(), []string{})).Should(Succeed())
Expect(loggerHook.LastEntry().Message).Should(Equal("OK"))
})
})
When("Wrong url is used", func() {
It("Should end with error", func() {
apiPort = 0
err := enableBlocking(newBlockingCommand(), []string{})
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("connection refused"))
})
})
When("Server returns internal error", func() {
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}
})
It("Should end with error", func() {
err := enableBlocking(newBlockingCommand(), []string{})
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("500 Internal Server Error"))
})
})
})
Describe("disable blocking", func() {
When("disable blocking is called via REST", func() {
It("should enable the blocking status", func() {
Expect(disableBlocking(newBlockingCommand(), []string{})).Should(Succeed())
Expect(loggerHook.LastEntry().Message).Should(Equal("OK"))
})
})
When("Wrong url is used", func() {
It("Should end with error", func() {
apiPort = 0
err := disableBlocking(newBlockingCommand(), []string{})
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("connection refused"))
})
})
When("Server returns internal error", func() {
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}
})
It("Should end with error", func() {
err := disableBlocking(newBlockingCommand(), []string{})
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("500 Internal Server Error"))
})
})
})
Describe("status blocking", func() {
When("status blocking is called via REST and blocking is enabled", func() {
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {
response, err := json.Marshal(api.BlockingStatus{
Enabled: true,
AutoEnableInSec: uint(5),
})
Expect(err).Should(Succeed())
_, err = w.Write(response)
Expect(err).Should(Succeed())
}
})
It("should query the blocking status", func() {
Expect(statusBlocking(newBlockingCommand(), []string{})).Should(Succeed())
Expect(loggerHook.LastEntry().Message).Should(Equal("blocking enabled"))
})
})
When("status blocking is called via REST and blocking is disabled", func() {
var autoEnable uint
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {
response, err := json.Marshal(api.BlockingStatus{
Enabled: false,
AutoEnableInSec: autoEnable,
DisabledGroups: []string{"abc"},
})
Expect(err).Should(Succeed())
_, err = w.Write(response)
Expect(err).Should(Succeed())
}
})
It("should show the blocking status with time", func() {
autoEnable = 5
Expect(statusBlocking(newBlockingCommand(), []string{})).Should(Succeed())
Expect(loggerHook.LastEntry().Message).Should(Equal("blocking disabled for groups: abc, for 5 seconds"))
})
It("should show the blocking status", func() {
autoEnable = 0
Expect(statusBlocking(newBlockingCommand(), []string{})).Should(Succeed())
Expect(loggerHook.LastEntry().Message).Should(Equal("blocking disabled for groups: abc"))
})
})
When("Wrong url is used", func() {
It("Should end with error", func() {
apiPort = 0
err := statusBlocking(newBlockingCommand(), []string{})
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("connection refused"))
})
})
When("Server returns internal error", func() {
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}
})
It("Should end with error", func() {
err := statusBlocking(newBlockingCommand(), []string{})
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("500 Internal Server Error"))
})
})
})
})
func testHTTPAPIServer(fn func(w http.ResponseWriter, _ *http.Request)) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(fn))
u, _ := url.Parse(ts.URL)
apiHost = u.Hostname()
port, _ := strconv.Atoi(u.Port())
apiPort = uint16(port)
return ts
}