-
Notifications
You must be signed in to change notification settings - Fork 7
/
webhook_test.go
120 lines (94 loc) · 2.98 KB
/
webhook_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
package main
import (
"net/url"
"testing"
"time"
"github.com/hashicorp/go-retryablehttp"
"github.com/jonhadfield/githosts-utils"
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
)
const exampleWebHookURL = "https://webhook.example.com"
var testProviderBackupResults = []ProviderBackupResults{
{
Provider: "GitHub",
Results: githosts.ProviderBackupResult{
BackupResults: []githosts.RepoBackupResults{
{
Repo: "https://github.com/jonhadfield/githosts-utils",
Status: "ok",
Error: nil,
},
{
Repo: "https://github.com/jonhadfield/soba",
Status: "ok",
Error: nil,
},
},
Error: nil,
},
},
}
func (j sobaTime) Add(d time.Duration) sobaTime {
return sobaTime{
Time: j.Time.Add(d),
f: time.RFC3339,
}
}
func TestWebhookLongFormat(t *testing.T) {
defer gock.Off()
u, err := url.Parse(exampleWebHookURL)
require.NoError(t, err)
theTime := sobaTime{
Time: time.Date(2024, 1, 15, 14, 30, 45, 100, time.UTC),
f: time.RFC3339,
}
start := theTime.Add(-time.Minute * 20)
end := theTime.Add(-time.Second * 10)
json := `{"app":"soba","type":"backups.complete","stats":{"succeeded":2,"failed":0},"timestamp":"2024-01-15T14:30:45Z","data":{"started_at":"2024-01-15T14:10:45Z","finished_at":"2024-01-15T14:30:35Z","results":[{"provider":"GitHub","results":{"BackupResults":[{"repo":"https://github.com/jonhadfield/githosts-utils","status":"ok"},{"repo":"https://github.com/jonhadfield/soba","status":"ok"}],"Error":null}}]}}`
gock.New(exampleWebHookURL).
Post(u.Path).
MatchHeader("Content-Type", "application/json").
MatchType("json").
JSON(json).
Reply(200)
gock.Observe(gock.DumpRequest)
c := retryablehttp.NewClient()
gock.InterceptClient(c.HTTPClient)
backupResults := BackupResults{
StartedAt: start,
FinishedAt: end,
Results: &testProviderBackupResults,
}
require.NoError(t, sendWebhook(c, theTime, backupResults, exampleWebHookURL, ""))
require.True(t, gock.IsDone())
}
func TestWebhookShortFormat(t *testing.T) {
t.Log("Testing webhook")
defer gock.Off()
u, err := url.Parse(exampleWebHookURL)
require.NoError(t, err)
theTime := sobaTime{
Time: time.Date(2024, 1, 15, 14, 30, 45, 100, time.UTC),
f: time.RFC3339,
}
start := theTime.Add(-time.Minute * 20)
end := theTime.Add(-time.Second * 10)
json := `{"app":"soba","type":"backups.complete","stats":{"succeeded":2,"failed":0},"timestamp":"2024-01-15T14:30:45Z","data":{"started_at":"2024-01-15T14:10:45Z","finished_at":"2024-01-15T14:30:35Z"}}`
gock.New(exampleWebHookURL).
Post(u.Path).
MatchHeader("Content-Type", "application/json").
MatchType("json").
JSON(json).
Reply(200)
gock.Observe(gock.DumpRequest)
c := retryablehttp.NewClient()
gock.InterceptClient(c.HTTPClient)
backupResults := BackupResults{
StartedAt: start,
FinishedAt: end,
Results: &testProviderBackupResults,
}
require.NoError(t, sendWebhook(c, theTime, backupResults, exampleWebHookURL, "short"))
require.True(t, gock.IsDone())
}