MediaService: Refactor saveFile

The function now expects a `Note` object instead of a noteId
and a `User` instead of a username to
make it more consistent with other functions.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-08-29 22:28:21 +02:00
parent 341e3a3e5a
commit fe26f1689c
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
9 changed files with 72 additions and 118 deletions

View file

@ -98,10 +98,12 @@ describe('MediaService', () => {
});
describe('saveFile', () => {
let user: User;
let note: Note;
beforeEach(() => {
const user = User.create('hardcoded', 'Testy') as User;
user = User.create('hardcoded', 'Testy') as User;
const alias = 'alias';
const note = Note.create(user, alias);
note = Note.create(user, alias);
jest.spyOn(userRepo, 'findOne').mockResolvedValueOnce(user);
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(note);
});
@ -126,22 +128,22 @@ describe('MediaService', () => {
return [fileName, null];
},
);
const url = await service.saveFile(testImage, 'hardcoded', 'test');
const url = await service.saveFile(testImage, user, note);
expect(url).toEqual(fileId);
});
describe('fails:', () => {
it('MIME type not identifiable', async () => {
await expect(
service.saveFile(Buffer.alloc(1), 'hardcoded', 'test'),
service.saveFile(Buffer.alloc(1), user, note),
).rejects.toThrow(ClientError);
});
it('MIME type not supported', async () => {
const testText = await fs.readFile('test/public-api/fixtures/test.zip');
await expect(
service.saveFile(testText, 'hardcoded', 'test'),
).rejects.toThrow(ClientError);
await expect(service.saveFile(testText, user, note)).rejects.toThrow(
ClientError,
);
});
});
});