-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmessage_test.go
160 lines (139 loc) · 6.4 KB
/
message_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package strillone_test
import (
"fmt"
"testing"
"github.com/dnsimple/dnsimple-go/dnsimple/webhook"
"github.com/dnsimple/strillone"
"github.com/stretchr/testify/assert"
)
type TestMessagingService struct {
Name string
}
func NewTestMessagingService(name string) *TestMessagingService {
return &TestMessagingService{Name: name}
}
func (*TestMessagingService) FormatLink(url, name string) string {
return fmt.Sprintf("<%s|%s>", url, name)
}
func (*TestMessagingService) PostEvent(_ *webhook.Event) (string, error) {
return "ok", nil
}
func Test_Message_AccountUserInviteEvent(t *testing.T) {
service := NewTestMessagingService("dummyMessagingService")
payload := `{
"name": "account.user_invite",
"actor": {"pretty": "[email protected]"},
"account": {"display": "xxxxxxxx", "identifier": "xxxxxxxx"},
"data": {
"account": {
"id": 12345,
"email": "[email protected]"
},
"account_invitation": {
"email": "[email protected]",
"account_id": 12345
}
}
}`
event, err := webhook.ParseEvent([]byte(payload))
assert.NoError(t, err)
result := strillone.Message(service, event)
assert.Equal(t, "[email protected] invited [email protected] to account <12345|https://dnsimple.com/a/12345/account/members>", result)
}
func Test_Message_AccountUserInvitationAcceptEvent(t *testing.T) {
service := NewTestMessagingService("dummyMessagingService")
payload := `{
"name":"account.user_invitation_accept",
"actor": {"pretty": "[email protected]"},
"account": {"display": "xxxxxxxx", "identifier": "xxxxxxxx"},
"data":{
"account":{
"id": 12345,
"email":"[email protected]"
},
"account_invitation":{
"email":"[email protected]",
"account_id":12345,
"invitation_sent_at":"2020-05-12T18:42:44Z",
"invitation_accepted_at":"2020-05-12T18:43:44Z"
}
}
}`
event, err := webhook.ParseEvent([]byte(payload))
assert.NoError(t, err)
result := strillone.Message(service, event)
assert.Equal(t, "[email protected] accepted invitation to account <12345|https://dnsimple.com/a/12345/account/members>", result)
}
func Test_Message_AccountUserInvitationRevokeEvent(t *testing.T) {
service := NewTestMessagingService("dummyMessagingService")
payload := `{
"name":"account.user_invitation_revoke",
"actor": {"pretty": "[email protected]"},
"account": {"display": "xxxxxxxx", "identifier": "xxxxxxxx"},
"data":{
"account":{
"id": 12345,
"email":"[email protected]"
},
"account_invitation":{
"email":"[email protected]",
"account_id":12345,
"invitation_sent_at":"2020-05-12T18:42:44Z",
"invitation_accepted_at":null
}
}
}`
event, err := webhook.ParseEvent([]byte(payload))
assert.NoError(t, err)
result := strillone.Message(service, event)
assert.Equal(t, "[email protected] rejected invitation to account <12345|https://dnsimple.com/a/12345/account/members>", result)
}
func Test_Message_AccountUserRemoveEvent(t *testing.T) {
service := NewTestMessagingService("dummyMessagingService")
payload := `{
"name":"account.user_remove",
"actor": {"pretty": "[email protected]"},
"account": {"display": "xxxxxxxx", "identifier": "xxxxxxxx"},
"data":{
"user":{
"id":1120,
"email":"[email protected]"
},
"account":{
"id":12345,
"email":"[email protected]"
}
}
}`
event, err := webhook.ParseEvent([]byte(payload))
assert.NoError(t, err)
result := strillone.Message(service, event)
assert.Equal(t, "[email protected] removed [email protected] from account <12345|https://dnsimple.com/a/12345/account/members>", result)
}
func Test_Message_DomainTransferLockDisableEvent(t *testing.T) {
service := NewTestMessagingService("dummyMessagingService")
payload := `{"data": {"domain": {"id": 1, "name": "example.com", "state": "registered", "account_id": 1010, "auto_renew": false, "created_at": "2023-03-02T02:39:18Z", "expires_at": "2024-03-02T02:39:22Z", "expires_on": "2024-03-02", "updated_at": "2023-08-31T06:46:48Z", "unicode_name": "example.com", "private_whois": false, "registrant_id": 101}}, "name": "domain.transfer_lock_disable", "actor": {"id": "1010", "entity": "account", "pretty": "[email protected]"}, "account": {"id": 1010, "display": "xxxxxxx-xxxxxxx-xxxxxxx", "identifier": "[email protected]"}, "api_version": "v2", "request_identifier": "0f31483c-c303-497b-8a88-2edb48aa111e"}`
event, err := webhook.ParseEvent([]byte(payload))
assert.NoError(t, err)
result := strillone.Message(service, event)
assert.Equal(t, "[<xxxxxxx-xxxxxxx-xxxxxxx|https://dnsimple.com/a/1010/account>] [email protected] disabled transfer lock for the domain <example.com|https://dnsimple.com/a/1010/domains/example.com>", result)
}
func Test_Message_DomainTransferLockEnableEvent(t *testing.T) {
service := NewTestMessagingService("dummyMessagingService")
payload := `{"data": {"domain": {"id": 1, "name": "example.com", "state": "registered", "account_id": 1010, "auto_renew": false, "created_at": "2023-03-02T02:39:18Z", "expires_at": "2024-03-02T02:39:22Z", "expires_on": "2024-03-02", "updated_at": "2023-08-31T06:46:48Z", "unicode_name": "example.com", "private_whois": false, "registrant_id": 101}}, "name": "domain.transfer_lock_enable", "actor": {"id": "1010", "entity": "account", "pretty": "[email protected]"}, "account": {"id": 1010, "display": "xxxxxxx-xxxxxxx-xxxxxxx", "identifier": "[email protected]"}, "api_version": "v2", "request_identifier": "0f31483c-c303-497b-8a88-2edb48aa111e"}`
event, err := webhook.ParseEvent([]byte(payload))
assert.NoError(t, err)
result := strillone.Message(service, event)
assert.Equal(t, "[<xxxxxxx-xxxxxxx-xxxxxxx|https://dnsimple.com/a/1010/account>] [email protected] enabled transfer lock for the domain <example.com|https://dnsimple.com/a/1010/domains/example.com>", result)
}
func Test_Message_DefaultMessage(t *testing.T) {
service := NewTestMessagingService("dummyMessagingService")
account := webhook.Account{Identifier: "ID", Display: "[email protected]"}
actor := webhook.Actor{Pretty: "[email protected]"}
event := webhook.Event{Actor: &actor, Account: &account, Name: "event.name"}
result := strillone.Message(service, &event)
assert.Equal(t, "[<[email protected]|https://dnsimple.com/a/0/account>] [email protected] performed event.name", result)
}
func Test_fmtURL(t *testing.T) {
assert.Equal(t, "https://dnsimple.com/a/1010/domains/1", strillone.FmtURL("/a/%v/domains/%v", "1010", 1))
}