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

Function TestUpdateNotificationTemplateMethod

enterprise/coderd/notifications_test.go:32–139  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

30}
31
32func 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

Callers

nothing calls this directly

Calls 12

StatusCodeMethod · 0.95
ContextFunction · 0.92
NewFunction · 0.92
CreateAnotherUserFunction · 0.92
getTemplateByIDFunction · 0.85
createOptsFunction · 0.70
RunMethod · 0.65
EmptyMethod · 0.45
EqualMethod · 0.45
ErrorMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected