|
1 | 1 | import { randomUUID } from 'crypto'; |
| 2 | +import fs from 'fs'; |
2 | 3 | import path from 'path'; |
3 | 4 |
|
4 | 5 | import type { Credentials } from '@rocket.chat/api-client'; |
@@ -361,6 +362,61 @@ describe('[CustomSounds]', () => { |
361 | 362 | .expect(403); |
362 | 363 | }); |
363 | 364 | }); |
| 365 | + |
| 366 | + describe('mutating file on failed validation', () => { |
| 367 | + let soundAId: string; |
| 368 | + let soundBId: string; |
| 369 | + const soundAName = `sound-a-${randomUUID()}`; |
| 370 | + const soundBName = `sound-b-${randomUUID()}`; |
| 371 | + |
| 372 | + before(async () => { |
| 373 | + soundAId = await createCustomSound(soundAName, mockWavAudioPath); |
| 374 | + soundBId = await createCustomSound(soundBName, mockMp3AudioPath); |
| 375 | + }); |
| 376 | + |
| 377 | + after(async () => { |
| 378 | + await deleteCustomSound(soundAId); |
| 379 | + await deleteCustomSound(soundBId); |
| 380 | + }); |
| 381 | + |
| 382 | + it('should not mutate the underlying file or metadata if the update fails validation (e.g., name collision)', async () => { |
| 383 | + await request |
| 384 | + .post(api('custom-sounds.update')) |
| 385 | + .set(credentials) |
| 386 | + .attach('sound', mockMp3AudioPath) |
| 387 | + .field('_id', soundAId) |
| 388 | + .field('name', soundBName) |
| 389 | + .expect(400) |
| 390 | + .expect((res) => { |
| 391 | + expect(res.body).to.have.property('success', false); |
| 392 | + expect(res.body).to.have.property('error', 'The custom sound name is already in use [Custom_Sound_Error_Name_Already_In_Use]'); |
| 393 | + }); |
| 394 | + |
| 395 | + await request |
| 396 | + .get(api('custom-sounds.getOne')) |
| 397 | + .set(credentials) |
| 398 | + .query({ _id: soundAId }) |
| 399 | + .expect(200) |
| 400 | + .expect((res) => { |
| 401 | + expect(res.body.sound).to.have.property('name', soundAName); |
| 402 | + expect(res.body.sound).to.have.property('extension', 'wav'); |
| 403 | + }); |
| 404 | + |
| 405 | + const originalWavBuffer = fs.readFileSync(mockWavAudioPath); |
| 406 | + |
| 407 | + await request |
| 408 | + .get(`/custom-sounds/${soundAId}.wav`) |
| 409 | + .set(credentials) |
| 410 | + .expect(200) |
| 411 | + .expect((res) => { |
| 412 | + expect(res.headers).to.have.property('content-type', 'audio/wav'); |
| 413 | + expect(Buffer.isBuffer(res.body)).to.be.true; |
| 414 | + expect(originalWavBuffer.equals(res.body)).to.be.true; |
| 415 | + }); |
| 416 | + |
| 417 | + await request.get(`/custom-sounds/${soundAId}.mp3`).set(credentials).expect(404); |
| 418 | + }); |
| 419 | + }); |
364 | 420 | }); |
365 | 421 |
|
366 | 422 | describe('[/custom-sounds.list]', () => { |
|
0 commit comments