-
Notifications
You must be signed in to change notification settings - Fork 3
/
entity_test.go
95 lines (89 loc) · 2.72 KB
/
entity_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
package postmand
import (
"encoding/json"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/lib/pq"
_ "github.com/lib/pq"
"github.com/stretchr/testify/assert"
)
func TestWebhook(t *testing.T) {
var tests = []struct {
kind string
request Webhook
expectedPayload string
}{
{
"required fields",
Webhook{},
`{"content_type":"cannot be blank","delivery_attempt_timeout":"cannot be blank","max_delivery_attempts":"cannot be blank","name":"cannot be blank","retry_max_backoff":"cannot be blank","retry_min_backoff":"cannot be blank","url":"cannot be blank","valid_status_codes":"cannot be blank"}`,
},
{
"Short name",
Webhook{ID: uuid.New(), Name: "A", URL: "https://httpbin.org/post", ContentType: "application/json", ValidStatusCodes: pq.Int32Array{200, 201}, MaxDeliveryAttempts: 1, DeliveryAttemptTimeout: 1, RetryMinBackoff: 1, RetryMaxBackoff: 1},
`{"name":"the length must be between 3 and 255"}`,
},
{
"Long name",
Webhook{ID: uuid.New(), Name: strings.Repeat("A", 300), URL: "https://httpbin.org/post", ContentType: "application/json", ValidStatusCodes: pq.Int32Array{200, 201}, MaxDeliveryAttempts: 1, DeliveryAttemptTimeout: 1, RetryMinBackoff: 1, RetryMaxBackoff: 1},
`{"name":"the length must be between 3 and 255"}`,
},
}
for _, tt := range tests {
t.Run(tt.kind, func(t *testing.T) {
err := tt.request.Validate()
assert.NotNil(t, err)
errorPayload, err := json.Marshal(err)
assert.Nil(t, err)
assert.Equal(t, tt.expectedPayload, string(errorPayload))
})
}
webhook := Webhook{
ID: uuid.New(),
Name: "AAA",
URL: "https://httpbin.org/post",
ContentType: "application/json",
ValidStatusCodes: pq.Int32Array{200, 201},
MaxDeliveryAttempts: 1,
DeliveryAttemptTimeout: 1,
RetryMinBackoff: 1,
RetryMaxBackoff: 1,
}
err := webhook.Validate()
assert.Nil(t, err)
}
func TestDelivery(t *testing.T) {
var tests = []struct {
kind string
request Delivery
expectedPayload string
}{
{
"required fields",
Delivery{},
`{"webhook_id":"must be a valid UUID v4"}`,
},
}
for _, tt := range tests {
t.Run(tt.kind, func(t *testing.T) {
err := tt.request.Validate()
assert.NotNil(t, err)
errorPayload, err := json.Marshal(err)
assert.Nil(t, err)
assert.Equal(t, tt.expectedPayload, string(errorPayload))
})
}
delivery := Delivery{
ID: uuid.New(),
WebhookID: uuid.New(),
Payload: `{"success": true}`,
ScheduledAt: time.Now().UTC(),
Status: DeliveryStatusPending,
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
}
err := delivery.Validate()
assert.Nil(t, err)
}