(t *testing.T)
| 30 | } |
| 31 | |
| 32 | func TestUpdateNotificationTemplateMethod(t *testing.T) { |
| 33 | t.Parallel() |
| 34 | |
| 35 | t.Run("Happy path", func(t *testing.T) { |
| 36 | t.Parallel() |
| 37 | |
| 38 | ctx := testutil.Context(t, testutil.WaitSuperLong) |
| 39 | api, _ := coderdenttest.New(t, createOpts(t)) |
| 40 | |
| 41 | var ( |
| 42 | method = string(database.NotificationMethodSmtp) |
| 43 | templateID = notifications.TemplateWorkspaceDeleted |
| 44 | ) |
| 45 | |
| 46 | // Given: a template whose method is initially empty (i.e. deferring to the global method value). |
| 47 | template, err := getTemplateByID(t, ctx, api, templateID) |
| 48 | require.NoError(t, err) |
| 49 | require.NotNil(t, template) |
| 50 | require.Empty(t, template.Method) |
| 51 | |
| 52 | // When: calling the API to update the method. |
| 53 | require.NoError(t, api.UpdateNotificationTemplateMethod(ctx, notifications.TemplateWorkspaceDeleted, method), "initial request to set the method failed") |
| 54 | |
| 55 | // Then: the method should be set. |
| 56 | template, err = getTemplateByID(t, ctx, api, templateID) |
| 57 | require.NoError(t, err) |
| 58 | require.NotNil(t, template) |
| 59 | require.Equal(t, method, template.Method) |
| 60 | }) |
| 61 | |
| 62 | t.Run("Insufficient permissions", func(t *testing.T) { |
| 63 | t.Parallel() |
| 64 | |
| 65 | ctx := testutil.Context(t, testutil.WaitSuperLong) |
| 66 | |
| 67 | // Given: the first user which has an "owner" role, and another user which does not. |
| 68 | api, firstUser := coderdenttest.New(t, createOpts(t)) |
| 69 | anotherClient, _ := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID) |
| 70 | |
| 71 | // When: calling the API as an unprivileged user. |
| 72 | err := anotherClient.UpdateNotificationTemplateMethod(ctx, notifications.TemplateWorkspaceDeleted, string(database.NotificationMethodWebhook)) |
| 73 | |
| 74 | // Then: the request is denied because of insufficient permissions. |
| 75 | var sdkError *codersdk.Error |
| 76 | require.Error(t, err) |
| 77 | require.ErrorAsf(t, err, &sdkError, "error should be of type *codersdk.Error") |
| 78 | require.Equal(t, http.StatusNotFound, sdkError.StatusCode()) |
| 79 | require.Equal(t, "Resource not found or you do not have access to this resource", sdkError.Response.Message) |
| 80 | }) |
| 81 | |
| 82 | t.Run("Invalid notification method", func(t *testing.T) { |
| 83 | t.Parallel() |
| 84 | |
| 85 | ctx := testutil.Context(t, testutil.WaitSuperLong) |
| 86 | |
| 87 | // Given: the first user which has an "owner" role |
| 88 | api, _ := coderdenttest.New(t, createOpts(t)) |
| 89 |
nothing calls this directly
no test coverage detected