(t *testing.T)
| 378 | } |
| 379 | |
| 380 | func 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 := ¬ificationstest.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 := ¬ificationstest.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{ |
nothing calls this directly
no test coverage detected