Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame] | 1 | package webhooks |
| 2 | |
| 3 | import ( |
| 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 Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame] | 11 | const ( |
| 12 | groupID = "10eb3219-1b12-4b34-b1e4-e10ee4f24c65" |
| 13 | policyID = "2b48d247-0282-4b9d-8775-5c4b67e8e649" |
Brad Ison | 20644be | 2016-03-28 19:13:05 -0400 | [diff] [blame] | 14 | firstID = "2bd1822c-58c5-49fd-8b3d-ed44781a58d1" // FirstWebhook |
| 15 | secondID = "76711c36-dfbe-4f5e-bea6-cded99690515" // SecondWebhook |
Brad Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame] | 16 | ) |
| 17 | |
Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame] | 18 | func TestList(t *testing.T) { |
| 19 | th.SetupHTTP() |
| 20 | defer th.TeardownHTTP() |
| 21 | HandleWebhookListSuccessfully(t) |
| 22 | |
Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame] | 23 | 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 Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame] | 51 | |
| 52 | func 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 Ison | cd10b15 | 2016-04-06 16:12:26 -0400 | [diff] [blame] | 70 | webhooks, err := Create(client, groupID, policyID, opts).Extract() |
Brad Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame] | 71 | |
| 72 | th.AssertNoErr(t, err) |
| 73 | th.CheckDeepEquals(t, FirstWebhook, webhooks[0]) |
| 74 | th.CheckDeepEquals(t, SecondWebhook, webhooks[1]) |
| 75 | } |
Brad Ison | 20644be | 2016-03-28 19:13:05 -0400 | [diff] [blame] | 76 | |
| 77 | func 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 Ison | d9ebfb9 | 2016-03-28 21:11:17 -0400 | [diff] [blame] | 89 | |
| 90 | func 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 Ison | b5d5548 | 2016-03-29 13:19:14 -0400 | [diff] [blame] | 107 | |
Brad Ison | 5ed9e9f | 2016-04-06 16:23:05 -0400 | [diff] [blame^] | 108 | func 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 Ison | b5d5548 | 2016-03-29 13:19:14 -0400 | [diff] [blame] | 123 | func 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 | } |