-
Notifications
You must be signed in to change notification settings - Fork 903
chore: add webhook tests for notification subsystem #13942
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package dispatch_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/slogtest" | ||
"github.com/coder/serpent" | ||
|
||
"github.com/coder/coder/v2/coderd/notifications/dispatch" | ||
"github.com/coder/coder/v2/coderd/notifications/types" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/testutil" | ||
) | ||
|
||
func TestWebhook(t *testing.T) { | ||
t.Parallel() | ||
|
||
const ( | ||
titleTemplate = "this is the title ({{.Labels.foo}})" | ||
bodyTemplate = "this is the body ({{.Labels.baz}})" | ||
) | ||
|
||
msgPayload := types.MessagePayload{ | ||
Version: "1.0", | ||
NotificationName: "test", | ||
Labels: map[string]string{ | ||
"foo": "bar", | ||
"baz": "quux", | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
serverURL string | ||
serverTimeout time.Duration | ||
serverFn func(uuid.UUID, http.ResponseWriter, *http.Request) | ||
|
||
expectSuccess bool | ||
expectRetryable bool | ||
expectErr string | ||
}{ | ||
{ | ||
name: "successful", | ||
serverFn: func(msgID uuid.UUID, w http.ResponseWriter, r *http.Request) { | ||
var payload dispatch.WebhookPayload | ||
err := json.NewDecoder(r.Body).Decode(&payload) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "application/json", r.Header.Get("Content-Type")) | ||
assert.Equal(t, msgID, payload.MsgID) | ||
assert.Equal(t, msgID.String(), r.Header.Get("X-Message-Id")) | ||
|
||
w.WriteHeader(http.StatusOK) | ||
_, err = w.Write([]byte(fmt.Sprintf("received %s", payload.MsgID))) | ||
assert.NoError(t, err) | ||
}, | ||
expectSuccess: true, | ||
}, | ||
{ | ||
name: "invalid endpoint", | ||
// Build a deliberately invalid URL to fail validation. | ||
serverURL: "invalid .com", | ||
expectSuccess: false, | ||
expectErr: "invalid URL escape", | ||
expectRetryable: false, | ||
}, | ||
{ | ||
name: "timeout", | ||
serverTimeout: time.Nanosecond, | ||
expectSuccess: false, | ||
expectRetryable: true, | ||
expectErr: "request timeout", | ||
}, | ||
{ | ||
name: "non-200 response", | ||
serverFn: func(_ uuid.UUID, w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
}, | ||
expectSuccess: false, | ||
expectRetryable: true, | ||
expectErr: "non-2xx response (500)", | ||
}, | ||
} | ||
|
||
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug) | ||
|
||
// nolint:paralleltest // Irrelevant as of Go v1.22 | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
timeout := testutil.WaitLong | ||
if tc.serverTimeout > 0 { | ||
timeout = tc.serverTimeout | ||
} | ||
|
||
var ( | ||
err error | ||
ctx = testutil.Context(t, timeout) | ||
msgID = uuid.New() | ||
) | ||
|
||
var endpoint *url.URL | ||
if tc.serverURL != "" { | ||
endpoint = &url.URL{Host: tc.serverURL} | ||
} else { | ||
// Mock server to simulate webhook endpoint. | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
tc.serverFn(msgID, w, r) | ||
})) | ||
defer server.Close() | ||
|
||
endpoint, err = url.Parse(server.URL) | ||
require.NoError(t, err) | ||
} | ||
|
||
cfg := codersdk.NotificationsWebhookConfig{ | ||
Endpoint: *serpent.URLOf(endpoint), | ||
} | ||
handler := dispatch.NewWebhookHandler(cfg, logger.With(slog.F("test", tc.name))) | ||
deliveryFn, err := handler.Dispatcher(msgPayload, titleTemplate, bodyTemplate) | ||
require.NoError(t, err) | ||
|
||
retryable, err := deliveryFn(ctx, msgID) | ||
if tc.expectSuccess { | ||
require.NoError(t, err) | ||
require.False(t, retryable) | ||
return | ||
} | ||
|
||
require.ErrorContains(t, err, tc.expectErr) | ||
require.Equal(t, tc.expectRetryable, retryable) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we could add a test that sends bad data (wrong format) too, just to have the case covered?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not actually possible, the webhook dispatch sends a well-formed request.
See
func (w *WebhookHandler) dispatch