diff --git a/integration_test.go b/integration_test.go index e58be40c..f91d6db4 100644 --- a/integration_test.go +++ b/integration_test.go @@ -5,6 +5,7 @@ package mailgun_test import ( "context" "encoding/json" + "net/http" "testing" "time" @@ -57,3 +58,42 @@ func TestIntegrationMailgunImpl_ListMetrics(t *testing.T) { } } } + +func TestIntegrationWebhooksCRUD(t *testing.T) { + // Arrange + + mg, err := mailgun.NewMailgunFromEnv() + if err != nil { + require.NoError(t, err) + } + + const name = "permanent_fail" + ctx := context.Background() + urls := []string{"https://example.com/1", "https://example.com/2"} + + err = mg.DeleteWebhook(ctx, name) + if err != nil { + // 200 or 404 is expected + status := mailgun.GetStatusFromErr(err) + require.Equal(t, http.StatusNotFound, status, err) + } + time.Sleep(3 * time.Second) + + defer func() { + // Cleanup + _ = mg.DeleteWebhook(ctx, name) + }() + + // Act + + err = mg.CreateWebhook(ctx, name, urls) + require.NoError(t, err) + time.Sleep(3 * time.Second) + + // Assert + + gotUrls, err := mg.GetWebhook(ctx, name) + require.NoError(t, err) + t.Logf("Webhooks: %v", urls) + assert.ElementsMatch(t, urls, gotUrls) +} diff --git a/rest_shim.go b/rest_shim.go index 8837257d..1a163fd8 100644 --- a/rest_shim.go +++ b/rest_shim.go @@ -37,9 +37,10 @@ func (e *UnexpectedResponseError) Error() string { // newError creates a new error condition to be returned. func newError(method, url string, expected []int, got *httpResponse) error { return &UnexpectedResponseError{ - URL: url, Expected: expected, Actual: got.Code, + Method: method, + URL: url, Data: got.Data, } } diff --git a/webhooks.go b/webhooks.go index 462855dd..c82ee4b8 100644 --- a/webhooks.go +++ b/webhooks.go @@ -1,5 +1,7 @@ package mailgun +// https://documentation.mailgun.com/docs/mailgun/api-reference/openapi-final/tag/Webhooks/#tag/Webhooks + import ( "context" "crypto/hmac" @@ -52,7 +54,6 @@ func (mg *MailgunImpl) ListWebhooks(ctx context.Context) (map[string][]string, e } // CreateWebhook installs a new webhook for your domain. -// List of ids - https://documentation.mailgun.com/docs/mailgun/api-reference/openapi-final/tag/Webhooks/#tag/Webhooks func (mg *MailgunImpl) CreateWebhook(ctx context.Context, id string, urls []string) error { r := newHTTPRequest(generateDomainApiUrl(mg, webhooksEndpoint)) r.setClient(mg.Client()) @@ -67,8 +68,8 @@ func (mg *MailgunImpl) CreateWebhook(ctx context.Context, id string, urls []stri } // DeleteWebhook removes the specified webhook from your domain's configuration. -func (mg *MailgunImpl) DeleteWebhook(ctx context.Context, kind string) error { - r := newHTTPRequest(generateDomainApiUrl(mg, webhooksEndpoint) + "/" + kind) +func (mg *MailgunImpl) DeleteWebhook(ctx context.Context, name string) error { + r := newHTTPRequest(generateDomainApiUrl(mg, webhooksEndpoint) + "/" + name) r.setClient(mg.Client()) r.setBasicAuth(basicAuthUser, mg.APIKey()) _, err := makeDeleteRequest(ctx, r) @@ -76,8 +77,8 @@ func (mg *MailgunImpl) DeleteWebhook(ctx context.Context, kind string) error { } // GetWebhook retrieves the currently assigned webhook URL associated with the provided type of webhook. -func (mg *MailgunImpl) GetWebhook(ctx context.Context, kind string) ([]string, error) { - r := newHTTPRequest(generateDomainApiUrl(mg, webhooksEndpoint) + "/" + kind) +func (mg *MailgunImpl) GetWebhook(ctx context.Context, name string) ([]string, error) { + r := newHTTPRequest(generateDomainApiUrl(mg, webhooksEndpoint) + "/" + name) r.setClient(mg.Client()) r.setBasicAuth(basicAuthUser, mg.APIKey()) var body WebHookResponse @@ -91,12 +92,12 @@ func (mg *MailgunImpl) GetWebhook(ctx context.Context, kind string) ([]string, e if len(body.Webhook.Urls) != 0 { return body.Webhook.Urls, nil } - return nil, fmt.Errorf("webhook '%s' returned no urls", kind) + return nil, fmt.Errorf("webhook '%s' returned no urls", name) } // UpdateWebhook replaces one webhook setting for another. -func (mg *MailgunImpl) UpdateWebhook(ctx context.Context, kind string, urls []string) error { - r := newHTTPRequest(generateDomainApiUrl(mg, webhooksEndpoint) + "/" + kind) +func (mg *MailgunImpl) UpdateWebhook(ctx context.Context, name string, urls []string) error { + r := newHTTPRequest(generateDomainApiUrl(mg, webhooksEndpoint) + "/" + name) r.setClient(mg.Client()) r.setBasicAuth(basicAuthUser, mg.APIKey()) p := newUrlEncodedPayload()