/
webhook_test.go
110 lines (88 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
package repository
import (
"context"
"testing"
"time"
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/stretchr/testify/assert"
"github.com/allisson/postmand"
)
func makeWebhook() postmand.Webhook {
return postmand.Webhook{
ID: uuid.New(),
Name: "Test",
URL: "https://httpbin.org/post",
ContentType: "application/json",
Active: true,
ValidStatusCodes: pq.Int32Array{200, 201},
MaxDeliveryAttempts: 1,
DeliveryAttemptTimeout: 1,
RetryMinBackoff: 1,
RetryMaxBackoff: 1,
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
}
}
func TestTransaction(t *testing.T) {
ctx := context.Background()
t.Run("Create webhook", func(t *testing.T) {
th := newTestHelper()
defer th.db.Close()
webhook := makeWebhook()
err := th.webhookRepository.Create(ctx, &webhook)
assert.Nil(t, err)
})
t.Run("Update webhook", func(t *testing.T) {
th := newTestHelper()
defer th.db.Close()
webhook := makeWebhook()
err := th.webhookRepository.Create(ctx, &webhook)
assert.Nil(t, err)
webhook.ValidStatusCodes = pq.Int32Array{200, 201, 204}
err = th.webhookRepository.Update(ctx, &webhook)
assert.Nil(t, err)
options := postmand.RepositoryGetOptions{Filters: map[string]interface{}{"id": webhook.ID}}
webhookFromRepository, err := th.webhookRepository.Get(ctx, options)
assert.Nil(t, err)
assert.Equal(t, pq.Int32Array{200, 201, 204}, webhookFromRepository.ValidStatusCodes)
})
t.Run("Delete webhook", func(t *testing.T) {
th := newTestHelper()
defer th.db.Close()
webhook := makeWebhook()
err := th.webhookRepository.Create(ctx, &webhook)
assert.Nil(t, err)
err = th.webhookRepository.Delete(ctx, webhook.ID)
assert.Nil(t, err)
options := postmand.RepositoryGetOptions{Filters: map[string]interface{}{"id": webhook.ID}}
_, err = th.webhookRepository.Get(ctx, options)
assert.Equal(t, postmand.ErrWebhookNotFound, err)
})
t.Run("Get webhook", func(t *testing.T) {
th := newTestHelper()
defer th.db.Close()
webhook := makeWebhook()
err := th.webhookRepository.Create(ctx, &webhook)
assert.Nil(t, err)
options := postmand.RepositoryGetOptions{Filters: map[string]interface{}{"id": webhook.ID}}
webhookFromRepository, err := th.webhookRepository.Get(ctx, options)
assert.Nil(t, err)
assert.Equal(t, webhook.ID, webhookFromRepository.ID)
})
t.Run("List webhooks", func(t *testing.T) {
th := newTestHelper()
defer th.db.Close()
webhook1 := makeWebhook()
err := th.webhookRepository.Create(ctx, &webhook1)
assert.Nil(t, err)
webhook2 := makeWebhook()
err = th.webhookRepository.Create(ctx, &webhook2)
assert.Nil(t, err)
options := postmand.RepositoryListOptions{Limit: 1, Offset: 0, OrderBy: "created_at", Order: "DESC"}
webhooks, err := th.webhookRepository.List(ctx, options)
assert.Nil(t, err)
assert.Len(t, webhooks, 1)
assert.Equal(t, webhook2.ID, webhooks[0].ID)
})
}