blob: 950c90d1040a2b0fe4dace9e801f0e4da42b5f9e [file] [log] [blame]
Brad Ison366a7a02016-03-27 17:06:21 -04001package webhooks
2
3import (
4 "testing"
5
6 "github.com/rackspace/gophercloud/pagination"
7 th "github.com/rackspace/gophercloud/testhelper"
8 "github.com/rackspace/gophercloud/testhelper/client"
9)
10
Brad Isone6e0ec12016-03-27 21:26:46 -040011const (
12 groupID = "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
13 policyID = "2b48d247-0282-4b9d-8775-5c4b67e8e649"
Brad Ison20644be2016-03-28 19:13:05 -040014 firstID = "2bd1822c-58c5-49fd-8b3d-ed44781a58d1" // FirstWebhook
15 secondID = "76711c36-dfbe-4f5e-bea6-cded99690515" // SecondWebhook
Brad Isone6e0ec12016-03-27 21:26:46 -040016)
17
Brad Ison366a7a02016-03-27 17:06:21 -040018func TestList(t *testing.T) {
19 th.SetupHTTP()
20 defer th.TeardownHTTP()
21 HandleWebhookListSuccessfully(t)
22
Brad Ison366a7a02016-03-27 17:06:21 -040023 pages := 0
24 pager := List(client.ServiceClient(), groupID, policyID)
25
26 err := pager.EachPage(func(page pagination.Page) (bool, error) {
27 pages++
28
29 webhooks, err := ExtractWebhooks(page)
30
31 if err != nil {
32 return false, err
33 }
34
35 if len(webhooks) != 2 {
36 t.Fatalf("Expected 2 policies, got %d", len(webhooks))
37 }
38
39 th.CheckDeepEquals(t, FirstWebhook, webhooks[0])
40 th.CheckDeepEquals(t, SecondWebhook, webhooks[1])
41
42 return true, nil
43 })
44
45 th.AssertNoErr(t, err)
46
47 if pages != 1 {
48 t.Errorf("Expected 1 page, saw %d", pages)
49 }
50}
Brad Isone6e0ec12016-03-27 21:26:46 -040051
52func TestCreate(t *testing.T) {
53 th.SetupHTTP()
54 defer th.TeardownHTTP()
55 HandleWebhookCreateSuccessfully(t)
56
57 client := client.ServiceClient()
58 opts := CreateOpts{
59 {
60 Name: "first hook",
61 },
62 {
63 Name: "second hook",
64 Metadata: map[string]string{
65 "notes": "a note about this webhook",
66 },
67 },
68 }
69
Brad Isoncd10b152016-04-06 16:12:26 -040070 webhooks, err := Create(client, groupID, policyID, opts).Extract()
Brad Isone6e0ec12016-03-27 21:26:46 -040071
72 th.AssertNoErr(t, err)
73 th.CheckDeepEquals(t, FirstWebhook, webhooks[0])
74 th.CheckDeepEquals(t, SecondWebhook, webhooks[1])
75}
Brad Ison20644be2016-03-28 19:13:05 -040076
77func TestGet(t *testing.T) {
78 th.SetupHTTP()
79 defer th.TeardownHTTP()
80 HandleWebhookGetSuccessfully(t)
81
82 client := client.ServiceClient()
83
84 webhook, err := Get(client, groupID, policyID, firstID).Extract()
85
86 th.AssertNoErr(t, err)
87 th.CheckDeepEquals(t, FirstWebhook, *webhook)
88}
Brad Isond9ebfb92016-03-28 21:11:17 -040089
90func TestUpdate(t *testing.T) {
91 th.SetupHTTP()
92 defer th.TeardownHTTP()
93 HandleWebhookUpdateSuccessfully(t)
94
95 client := client.ServiceClient()
96 opts := UpdateOpts{
97 Name: "updated hook",
98 Metadata: map[string]string{
99 "new-key": "some data",
100 },
101 }
102
103 err := Update(client, groupID, policyID, firstID, opts).ExtractErr()
104
105 th.AssertNoErr(t, err)
106}
Brad Isonb5d55482016-03-29 13:19:14 -0400107
Brad Ison5ed9e9f2016-04-06 16:23:05 -0400108func TestUpdateNoMetadata(t *testing.T) {
109 th.SetupHTTP()
110 defer th.TeardownHTTP()
111 HandleWebhookUpdateSuccessfully(t)
112
113 client := client.ServiceClient()
114 opts := UpdateOpts{
115 Name: "updated hook",
116 }
117
118 err := Update(client, groupID, policyID, firstID, opts).ExtractErr()
119
120 th.AssertEquals(t, ErrNoMetadata, err)
121}
122
Brad Isonb5d55482016-03-29 13:19:14 -0400123func TestDelete(t *testing.T) {
124 th.SetupHTTP()
125 defer th.TeardownHTTP()
126 HandleWebhookDeleteSuccessfully(t)
127
128 client := client.ServiceClient()
129 err := Delete(client, groupID, policyID, firstID).ExtractErr()
130
131 th.AssertNoErr(t, err)
132}