forked from chenyangguang/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_test.go
More file actions
115 lines (106 loc) · 3.17 KB
/
Copy pathwebhook_test.go
File metadata and controls
115 lines (106 loc) · 3.17 KB
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
package woocommerce
import (
"fmt"
"testing"
"time"
)
func initWebhook() Webhook {
timeNowStr := fmt.Sprintf("%d", time.Now().Unix())
webhook := Webhook{
Name: "order create" + timeNowStr,
Topic: "order.created",
DeliveryUrl: "https://shop.gitvim.com", // your callback url for wooCommerce event cron job to notify
}
return webhook
}
func TestWebhookServiceOp_List(t *testing.T) {
webhooks, err := client.Webhook.List(nil)
if err != nil {
t.Errorf("get webhook fail: %v", err)
t.FailNow()
}
for _, webhook := range webhooks {
t.Log(webhook.ID, webhook.Topic, webhook.Event, webhook.Hooks)
// TestWebhookServiceOp_List: webhook_test.go:12: 5 order.updated updated [woocommerce_update_order woocommerce_order_refunded]
// TestWebhookServiceOp_List: webhook_test.go:12: 4 order.updated updated [woocommerce_update_order woocommerce_order_refunded]
// TestWebhookServiceOp_List: webhook_test.go:12: 3 order.updated updated [woocommerce_update_order woocommerce_order_refunded]
// ...
}
}
func TestWebhookServiceOp_Create(t *testing.T) {
webhook := initWebhook()
res, err := client.Webhook.Create(webhook)
if err != nil {
t.Errorf("res : %v, err: %v", res, err)
t.FailNow()
} else {
t.Logf("res:%v", res)
}
}
func TestWebhookServiceOp_Get(t *testing.T) {
webhook, err := client.Webhook.Get(2, nil)
if err != nil {
t.Errorf("get webhook fail: %v", err)
t.FailNow()
}
t.Logf("order : %v, err: %v", webhook, err)
}
func TestWebhookServiceOp_Update(t *testing.T) {
webhook, err := client.Webhook.Get(2, nil)
if err != nil {
return
}
webhook.Name = webhook.Name + " after updated"
res, err := client.Webhook.Update(webhook)
if err != nil {
t.Errorf("update webhook fail: %v", err)
t.FailNow()
}
t.Logf("webhook result: %v", res)
}
func TestWebhookServiceOp_Delete(t *testing.T) {
options := DeleteOption{
Force: true,
}
webhook, err := client.Webhook.Delete(9, options)
if err != nil {
t.Errorf("delete webhook fail: %v", err)
// if you don't set the options for force = true, you will get the result like below:
// {"code":"woocommerce_rest_trash_not_supported","message":"Webhook\u4e0d\u652f\u6301\u56de\u6536\u7ad9\u3002","data":{"status":501}}
// But delete a webhook is very dangerous operation
// if you absolute need to delete a webhook , just setting force's value as true
}
t.Logf("The webhook you delete is : %v", webhook)
}
func TestWebhookServiceOp_Batch(t *testing.T) {
webhook := initWebhook()
data := WebhookBatchOption{
Create: []Webhook{
webhook,
},
Update: []Webhook{
{
ID: 8,
Name: "batch update operate test",
},
},
Delete: []int64{
10,
},
}
res, err := client.Webhook.Batch(data)
if err != nil {
t.Errorf("delete webhook fail: %v", err)
t.FailNow()
}
t.Logf(" create : %v, update: %v, delete : %v", res.Create, res.Update, res.Delete)
for _, webhook := range res.Create {
t.Logf(" webhook id: %v, webhook Name : %v", webhook.ID, webhook.Name)
}
for _, webhook := range res.Update {
t.Logf(" order id: %v, webhook name : %v", webhook.ID, webhook.Name)
}
for _, webhook := range res.Delete {
t.Logf(" webhook id: %v, webhook status : %v", webhook.ID, webhook.Status)
}
}