-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynip_test.go
98 lines (85 loc) · 2.49 KB
/
dynip_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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/sirupsen/logrus"
)
var response string
func Test_updateIP(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, response)
}))
defer ts.Close()
type args struct {
appConfig *AppConfig
logger *logrus.Logger
}
cfg, err := makeTestConfig(ts.URL)
if err != nil {
t.Fatal(err)
}
tlog := logrus.New()
tlog.Level = logrus.InfoLevel
tlog.Out = ioutil.Discard
targs := args{cfg, tlog}
tests := []struct {
name string
args args
want Result
wantErr bool
resp string
}{
{name: "success", args: targs, want: SUCCESS, wantErr: false, resp: respSUCCESS},
{name: "no change", args: targs, want: NOCHANGE, wantErr: false, resp: respNOCHANGE},
{name: "too soon", args: targs, want: TOOSOON, wantErr: true, resp: respTOOSOON},
{name: "bad token", args: targs, want: NOAUTH, wantErr: true, resp: respBADTOKEN},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
response = tt.resp
got, err := updateIP(tt.args.appConfig, tt.args.logger)
if (err != nil) != tt.wantErr {
t.Errorf("updateIP() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("updateIP() = %v, want %v", got, tt.want)
}
})
}
}
func makeTestConfig(surl string) (*AppConfig, error) {
uri, err := url.Parse(surl)
if err != nil {
return nil, err
}
purl := fmt.Sprintf("%s:%s", uri.Hostname(), uri.Port())
m := map[string]string{"url": purl,
"proto": "http",
"hostname": "test.example.com",
"username": "testuser",
"token": "testtoken"}
return NewAppConfigFromMap(m)
}
const (
respSUCCESS = `<HTML><BODY><FONT FACE="sans-serif" SIZE="-1">OK<br />
<hr noshade size="1">
test.example.com updated to 24.114.104.44<br />
</FONT></BODY></HTML>`
respNOCHANGE = `<HTML><BODY><FONT FACE="sans-serif" SIZE="-1">OK<br />
<hr noshade size="1">
no update required for aspen.darklake.ca to 24.114.85.179<br />
</FONT></BODY></HTML>`
respTOOSOON = `<HTML><BODY><FONT FACE="sans-serif" SIZE="-1">TOO_FREQ<br />
<hr noshade size="1">
Increase your time between updates for test.example.com to 600 seconds or more.<br />
</FONT></BODY></HTML>`
respBADTOKEN = `<HTML><BODY><FONT FACE="sans-serif" SIZE="-1">NO_AUTH<br />
<hr noshade size="1">
Failed login attempt logged: user dlauder77, host test.example.com, from 24.114.82.202<br />
</FONT></BODY></HTML>`
)