|
5 | 5 | makeTile, |
6 | 6 | randomMongoId, |
7 | 7 | } from '@/fixtures'; |
8 | | -import Alert from '@/models/alert'; |
| 8 | +import Alert, { AlertSource, AlertThresholdType } from '@/models/alert'; |
9 | 9 | import Webhook, { WebhookDocument, WebhookService } from '@/models/webhook'; |
10 | 10 |
|
11 | 11 | const MOCK_TILES = [makeTile(), makeTile(), makeTile(), makeTile(), makeTile()]; |
@@ -116,6 +116,245 @@ describe('alerts router', () => { |
116 | 116 | expect(allAlerts.body.data[0].threshold).toBe(10); |
117 | 117 | }); |
118 | 118 |
|
| 119 | + it('preserves scheduleStartAt when omitted in updates and clears when null', async () => { |
| 120 | + const dashboard = await agent |
| 121 | + .post('/dashboards') |
| 122 | + .send(MOCK_DASHBOARD) |
| 123 | + .expect(200); |
| 124 | + |
| 125 | + const scheduleStartAt = '2024-01-01T00:00:00.000Z'; |
| 126 | + const createdAlert = await agent |
| 127 | + .post('/alerts') |
| 128 | + .send({ |
| 129 | + ...makeAlertInput({ |
| 130 | + dashboardId: dashboard.body.id, |
| 131 | + tileId: dashboard.body.tiles[0].id, |
| 132 | + webhookId: webhook._id.toString(), |
| 133 | + }), |
| 134 | + scheduleStartAt, |
| 135 | + }) |
| 136 | + .expect(200); |
| 137 | + |
| 138 | + const updatePayload = { |
| 139 | + channel: createdAlert.body.data.channel, |
| 140 | + interval: createdAlert.body.data.interval, |
| 141 | + threshold: 10, |
| 142 | + thresholdType: createdAlert.body.data.thresholdType, |
| 143 | + source: createdAlert.body.data.source, |
| 144 | + dashboardId: dashboard.body.id, |
| 145 | + tileId: dashboard.body.tiles[0].id, |
| 146 | + }; |
| 147 | + |
| 148 | + await agent |
| 149 | + .put(`/alerts/${createdAlert.body.data._id}`) |
| 150 | + .send(updatePayload) |
| 151 | + .expect(200); |
| 152 | + |
| 153 | + const alertAfterOmittedScheduleStartAt = await Alert.findById( |
| 154 | + createdAlert.body.data._id, |
| 155 | + ); |
| 156 | + expect( |
| 157 | + alertAfterOmittedScheduleStartAt?.scheduleStartAt?.toISOString(), |
| 158 | + ).toBe(scheduleStartAt); |
| 159 | + |
| 160 | + await agent |
| 161 | + .put(`/alerts/${createdAlert.body.data._id}`) |
| 162 | + .send({ |
| 163 | + ...updatePayload, |
| 164 | + scheduleStartAt: null, |
| 165 | + }) |
| 166 | + .expect(200); |
| 167 | + |
| 168 | + const alertAfterNullScheduleStartAt = await Alert.findById( |
| 169 | + createdAlert.body.data._id, |
| 170 | + ); |
| 171 | + expect(alertAfterNullScheduleStartAt?.scheduleStartAt).toBeNull(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('preserves scheduleOffsetMinutes when schedule fields are omitted in updates', async () => { |
| 175 | + const dashboard = await agent |
| 176 | + .post('/dashboards') |
| 177 | + .send(MOCK_DASHBOARD) |
| 178 | + .expect(200); |
| 179 | + |
| 180 | + const createdAlert = await agent |
| 181 | + .post('/alerts') |
| 182 | + .send({ |
| 183 | + ...makeAlertInput({ |
| 184 | + dashboardId: dashboard.body.id, |
| 185 | + tileId: dashboard.body.tiles[0].id, |
| 186 | + interval: '15m', |
| 187 | + webhookId: webhook._id.toString(), |
| 188 | + }), |
| 189 | + scheduleOffsetMinutes: 2, |
| 190 | + }) |
| 191 | + .expect(200); |
| 192 | + |
| 193 | + await agent |
| 194 | + .put(`/alerts/${createdAlert.body.data._id}`) |
| 195 | + .send({ |
| 196 | + channel: createdAlert.body.data.channel, |
| 197 | + interval: createdAlert.body.data.interval, |
| 198 | + threshold: 10, |
| 199 | + thresholdType: createdAlert.body.data.thresholdType, |
| 200 | + source: createdAlert.body.data.source, |
| 201 | + dashboardId: dashboard.body.id, |
| 202 | + tileId: dashboard.body.tiles[0].id, |
| 203 | + }) |
| 204 | + .expect(200); |
| 205 | + |
| 206 | + const updatedAlert = await Alert.findById(createdAlert.body.data._id); |
| 207 | + expect(updatedAlert?.scheduleOffsetMinutes).toBe(2); |
| 208 | + expect(updatedAlert?.scheduleStartAt).toBeUndefined(); |
| 209 | + }); |
| 210 | + |
| 211 | + it('resets scheduleOffsetMinutes to 0 when scheduleStartAt is set without offset', async () => { |
| 212 | + const dashboard = await agent |
| 213 | + .post('/dashboards') |
| 214 | + .send(MOCK_DASHBOARD) |
| 215 | + .expect(200); |
| 216 | + |
| 217 | + const createdAlert = await agent |
| 218 | + .post('/alerts') |
| 219 | + .send({ |
| 220 | + ...makeAlertInput({ |
| 221 | + dashboardId: dashboard.body.id, |
| 222 | + tileId: dashboard.body.tiles[0].id, |
| 223 | + webhookId: webhook._id.toString(), |
| 224 | + }), |
| 225 | + scheduleOffsetMinutes: 2, |
| 226 | + }) |
| 227 | + .expect(200); |
| 228 | + |
| 229 | + expect(createdAlert.body.data.scheduleOffsetMinutes).toBe(2); |
| 230 | + |
| 231 | + const scheduleStartAt = '2024-01-01T00:00:00.000Z'; |
| 232 | + |
| 233 | + await agent |
| 234 | + .put(`/alerts/${createdAlert.body.data._id}`) |
| 235 | + .send({ |
| 236 | + channel: createdAlert.body.data.channel, |
| 237 | + interval: createdAlert.body.data.interval, |
| 238 | + threshold: createdAlert.body.data.threshold, |
| 239 | + thresholdType: createdAlert.body.data.thresholdType, |
| 240 | + source: createdAlert.body.data.source, |
| 241 | + dashboardId: dashboard.body.id, |
| 242 | + tileId: dashboard.body.tiles[0].id, |
| 243 | + scheduleStartAt, |
| 244 | + }) |
| 245 | + .expect(200); |
| 246 | + |
| 247 | + const updatedAlert = await Alert.findById(createdAlert.body.data._id); |
| 248 | + expect(updatedAlert?.scheduleOffsetMinutes).toBe(0); |
| 249 | + expect(updatedAlert?.scheduleStartAt?.toISOString()).toBe(scheduleStartAt); |
| 250 | + }); |
| 251 | + |
| 252 | + it('resets stale scheduleOffsetMinutes when scheduleStartAt is cleared without offset', async () => { |
| 253 | + const dashboard = await agent |
| 254 | + .post('/dashboards') |
| 255 | + .send(MOCK_DASHBOARD) |
| 256 | + .expect(200); |
| 257 | + |
| 258 | + const staleAlert = await Alert.create({ |
| 259 | + team: team._id, |
| 260 | + channel: { |
| 261 | + type: 'webhook', |
| 262 | + webhookId: webhook._id.toString(), |
| 263 | + }, |
| 264 | + interval: '15m', |
| 265 | + threshold: 8, |
| 266 | + thresholdType: AlertThresholdType.ABOVE, |
| 267 | + source: AlertSource.TILE, |
| 268 | + dashboard: dashboard.body.id, |
| 269 | + tileId: dashboard.body.tiles[0].id, |
| 270 | + scheduleOffsetMinutes: 2, |
| 271 | + scheduleStartAt: new Date('2024-01-01T00:00:00.000Z'), |
| 272 | + }); |
| 273 | + |
| 274 | + await agent |
| 275 | + .put(`/alerts/${staleAlert._id.toString()}`) |
| 276 | + .send({ |
| 277 | + ...makeAlertInput({ |
| 278 | + dashboardId: dashboard.body.id, |
| 279 | + tileId: dashboard.body.tiles[0].id, |
| 280 | + interval: '15m', |
| 281 | + webhookId: webhook._id.toString(), |
| 282 | + }), |
| 283 | + scheduleStartAt: null, |
| 284 | + }) |
| 285 | + .expect(200); |
| 286 | + |
| 287 | + const updatedAlert = await Alert.findById(staleAlert._id); |
| 288 | + expect(updatedAlert?.scheduleOffsetMinutes).toBe(0); |
| 289 | + expect(updatedAlert?.scheduleStartAt).toBeNull(); |
| 290 | + }); |
| 291 | + |
| 292 | + it('rejects scheduleStartAt values more than 1 year in the future', async () => { |
| 293 | + const dashboard = await agent |
| 294 | + .post('/dashboards') |
| 295 | + .send(MOCK_DASHBOARD) |
| 296 | + .expect(200); |
| 297 | + |
| 298 | + const farFutureScheduleStartAt = new Date( |
| 299 | + Date.now() + 366 * 24 * 60 * 60 * 1000, |
| 300 | + ).toISOString(); |
| 301 | + |
| 302 | + await agent |
| 303 | + .post('/alerts') |
| 304 | + .send({ |
| 305 | + ...makeAlertInput({ |
| 306 | + dashboardId: dashboard.body.id, |
| 307 | + tileId: dashboard.body.tiles[0].id, |
| 308 | + webhookId: webhook._id.toString(), |
| 309 | + }), |
| 310 | + scheduleStartAt: farFutureScheduleStartAt, |
| 311 | + }) |
| 312 | + .expect(400); |
| 313 | + }); |
| 314 | + |
| 315 | + it('rejects scheduleStartAt values older than 10 years in the past', async () => { |
| 316 | + const dashboard = await agent |
| 317 | + .post('/dashboards') |
| 318 | + .send(MOCK_DASHBOARD) |
| 319 | + .expect(200); |
| 320 | + |
| 321 | + const tooOldScheduleStartAt = new Date( |
| 322 | + Date.now() - 11 * 365 * 24 * 60 * 60 * 1000, |
| 323 | + ).toISOString(); |
| 324 | + |
| 325 | + await agent |
| 326 | + .post('/alerts') |
| 327 | + .send({ |
| 328 | + ...makeAlertInput({ |
| 329 | + dashboardId: dashboard.body.id, |
| 330 | + tileId: dashboard.body.tiles[0].id, |
| 331 | + webhookId: webhook._id.toString(), |
| 332 | + }), |
| 333 | + scheduleStartAt: tooOldScheduleStartAt, |
| 334 | + }) |
| 335 | + .expect(400); |
| 336 | + }); |
| 337 | + |
| 338 | + it('rejects scheduleOffsetMinutes when scheduleStartAt is provided', async () => { |
| 339 | + const dashboard = await agent |
| 340 | + .post('/dashboards') |
| 341 | + .send(MOCK_DASHBOARD) |
| 342 | + .expect(200); |
| 343 | + |
| 344 | + await agent |
| 345 | + .post('/alerts') |
| 346 | + .send({ |
| 347 | + ...makeAlertInput({ |
| 348 | + dashboardId: dashboard.body.id, |
| 349 | + tileId: dashboard.body.tiles[0].id, |
| 350 | + webhookId: webhook._id.toString(), |
| 351 | + }), |
| 352 | + scheduleOffsetMinutes: 2, |
| 353 | + scheduleStartAt: new Date().toISOString(), |
| 354 | + }) |
| 355 | + .expect(400); |
| 356 | + }); |
| 357 | + |
119 | 358 | it('preserves createdBy field during updates', async () => { |
120 | 359 | const dashboard = await agent |
121 | 360 | .post('/dashboards') |
|
0 commit comments