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

Function TestNotificationPreferences

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

Source from the content-addressed store, hash-verified

110}
111
112func TestNotificationPreferences(t *testing.T) {
113 t.Parallel()
114
115 t.Run("Initial state", func(t *testing.T) {
116 t.Parallel()
117
118 ctx := testutil.Context(t, testutil.WaitSuperLong)
119 api := coderdtest.New(t, createOpts(t))
120 firstUser := coderdtest.CreateFirstUser(t, api)
121
122 // Given: a member in its initial state.
123 memberClient, member := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
124
125 // When: calling the API.
126 prefs, err := memberClient.GetUserNotificationPreferences(ctx, member.ID)
127 require.NoError(t, err)
128
129 // Then: no preferences will be returned.
130 require.Len(t, prefs, 0)
131 })
132
133 t.Run("Insufficient permissions", func(t *testing.T) {
134 t.Parallel()
135
136 ctx := testutil.Context(t, testutil.WaitSuperLong)
137 api := coderdtest.New(t, createOpts(t))
138 firstUser := coderdtest.CreateFirstUser(t, api)
139
140 // Given: 2 members.
141 _, member1 := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
142 member2Client, _ := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
143
144 // When: attempting to retrieve the preferences of another member.
145 _, err := member2Client.GetUserNotificationPreferences(ctx, member1.ID)
146
147 // Then: the API should reject the request.
148 var sdkError *codersdk.Error
149 require.Error(t, err)
150 require.ErrorAsf(t, err, &sdkError, "error should be of type *codersdk.Error")
151 // NOTE: ExtractUserParam gets in the way here, and returns a 400 Bad Request instead of a 403 Forbidden.
152 // This is not ideal, and we should probably change this behavior.
153 require.Equal(t, http.StatusNotFound, sdkError.StatusCode())
154 })
155
156 t.Run("Admin may read any users' preferences", func(t *testing.T) {
157 t.Parallel()
158
159 ctx := testutil.Context(t, testutil.WaitSuperLong)
160 api := coderdtest.New(t, createOpts(t))
161 firstUser := coderdtest.CreateFirstUser(t, api)
162
163 // Given: a member.
164 _, member := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
165
166 // When: attempting to retrieve the preferences of another member as an admin.
167 prefs, err := api.GetUserNotificationPreferences(ctx, member.ID)
168
169 // Then: the API should not reject the request.

Callers

nothing calls this directly

Calls 13

StatusCodeMethod · 0.95
ContextFunction · 0.92
NewFunction · 0.92
CreateFirstUserFunction · 0.92
CreateAnotherUserFunction · 0.92
createOptsFunction · 0.70
RunMethod · 0.65
LenMethod · 0.45
ErrorMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected