MCPcopy Index your code
hub / github.com/coder/coder / TestCustomNotification

Function TestCustomNotification

coderd/notifications_test.go:380–488  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

378}
379
380func TestCustomNotification(t *testing.T) {
381 t.Parallel()
382
383 t.Run("BadRequest", func(t *testing.T) {
384 t.Parallel()
385
386 ctx := testutil.Context(t, testutil.WaitShort)
387
388 notifyEnq := &notificationstest.FakeEnqueuer{}
389 ownerClient := coderdtest.New(t, &coderdtest.Options{
390 DeploymentValues: coderdtest.DeploymentValues(t),
391 NotificationsEnqueuer: notifyEnq,
392 })
393
394 // Given: A member user
395 ownerUser := coderdtest.CreateFirstUser(t, ownerClient)
396 memberClient, _ := coderdtest.CreateAnotherUser(t, ownerClient, ownerUser.OrganizationID)
397
398 // When: The member user attempts to send a custom notification with empty title and message
399 err := memberClient.PostCustomNotification(ctx, codersdk.CustomNotificationRequest{
400 Content: &codersdk.CustomNotificationContent{
401 Title: "",
402 Message: "",
403 },
404 })
405
406 // Then: a bad request error is expected with no notifications sent
407 var sdkError *codersdk.Error
408 require.Error(t, err)
409 require.ErrorAsf(t, err, &sdkError, "error should be of type *codersdk.Error")
410 require.Equal(t, http.StatusBadRequest, sdkError.StatusCode())
411 require.Equal(t, "Invalid request body", sdkError.Message)
412
413 sent := notifyEnq.Sent(notificationstest.WithTemplateID(notifications.TemplateTestNotification))
414 require.Len(t, sent, 0)
415 })
416
417 t.Run("SystemUserNotAllowed", func(t *testing.T) {
418 t.Parallel()
419
420 ctx := testutil.Context(t, testutil.WaitShort)
421
422 notifyEnq := &notificationstest.FakeEnqueuer{}
423 ownerClient, db := coderdtest.NewWithDatabase(t, &coderdtest.Options{
424 DeploymentValues: coderdtest.DeploymentValues(t),
425 NotificationsEnqueuer: notifyEnq,
426 })
427
428 // Given: A system user (prebuilds system user)
429 _, token := dbgen.APIKey(t, db, database.APIKey{
430 UserID: database.PrebuildsSystemUserID,
431 LoginType: database.LoginTypeNone,
432 })
433 systemUserClient := codersdk.New(ownerClient.URL)
434 systemUserClient.SetSessionToken(token)
435
436 // When: The system user attempts to send a custom notification
437 err := systemUserClient.PostCustomNotification(ctx, codersdk.CustomNotificationRequest{

Callers

nothing calls this directly

Calls 15

StatusCodeMethod · 0.95
SentMethod · 0.95
ContextFunction · 0.92
NewFunction · 0.92
DeploymentValuesFunction · 0.92
CreateFirstUserFunction · 0.92
CreateAnotherUserFunction · 0.92
WithTemplateIDFunction · 0.92
NewWithDatabaseFunction · 0.92
APIKeyFunction · 0.92
NewFunction · 0.92

Tested by

no test coverage detected