-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunder_test.go
119 lines (107 loc) · 2.62 KB
/
under_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
package main
import (
"encoding/json"
"github.com/cloudflare/cloudflare-go"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_loadAvg(t *testing.T) {
got, err := loadAvg("1.01 0.97 0.94 1/159 2795695")
require.NoError(t, err)
assert.Equal(t, []float64{1.01, 0.97, 0.94}, got)
}
func Test_loadAvgNil(t *testing.T) {
_, err := loadAvg("")
require.Error(t, err)
}
func Test_loadAvgNan(t *testing.T) {
_, err := loadAvg("abc")
require.Error(t, err)
}
const settingsResponse = `
{
"result": [
{
"id": "security_level",
"value": "medium"
}
]
}
`
func Test_app_currentLevel(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/zones/myzone/settings", r.RequestURI)
io.WriteString(w, settingsResponse)
}))
defer s.Close()
a := app{zoneId: "myzone"}
var err error
a.api, err = cloudflare.New("key", "email")
require.NoError(t, err)
a.api.BaseURL = s.URL
got, err := a.currentLevel()
require.NoError(t, err)
assert.Equal(t, "medium", got)
}
type updateSettings struct {
Items []cloudflare.ZoneSetting `json:"items"`
}
func Test_app_setSecurityLevel(t *testing.T) {
patched := false
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
io.WriteString(w, settingsResponse)
case "PATCH":
patched = true
var us updateSettings
err := json.NewDecoder(r.Body).Decode(&us)
require.NoError(t, err)
require.Equal(t, 1, len(us.Items))
assert.Equal(t, "security_level", us.Items[0].ID)
assert.Equal(t, "under_attack", us.Items[0].Value)
io.WriteString(w, `{
"success": true,
"errors": [],
"messages": []
}`)
default:
t.Errorf("There should not have been a %s request", r.Method)
}
}))
defer s.Close()
a := app{
zoneId: "myzone",
}
var err error
a.api, err = cloudflare.New("key", "email")
require.NoError(t, err)
a.api.BaseURL = s.URL
err = a.setSecurityLevel("under_attack")
assert.NoError(t, err)
assert.True(t, patched)
}
func Test_app_setSecurityLevelAlreadyThere(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
io.WriteString(w, settingsResponse)
default:
t.Errorf("There should not have been a %s request", r.Method)
}
}))
defer s.Close()
a := app{
zoneId: "myzone",
}
var err error
a.api, err = cloudflare.New("key", "email")
require.NoError(t, err)
a.api.BaseURL = s.URL
err = a.setSecurityLevel("medium")
assert.NoError(t, err)
}