Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DE-1349 fix func newError() and added integration test for CreateWebhook #340

Merged
merged 5 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package mailgun_test
import (
"context"
"encoding/json"
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion rest_shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
17 changes: 9 additions & 8 deletions webhooks.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mailgun

// https://documentation.mailgun.com/docs/mailgun/api-reference/openapi-final/tag/Webhooks/#tag/Webhooks

import (
"context"
"crypto/hmac"
Expand Down Expand Up @@ -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())
Expand All @@ -67,17 +68,17 @@ 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)
return err
}

// 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
Expand All @@ -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()
Expand Down