add tests for attachment metadata

This commit is contained in:
Audric Ackermann 2022-04-07 14:47:54 +10:00
parent afd63c230e
commit a9cc9a7294
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4
6 changed files with 309 additions and 18 deletions

View File

@ -100,7 +100,9 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
}
autoBind(this);
window.contextMenuShown = false;
if (window) {
window.contextMenuShown = false;
}
this.getMessageModelProps();
}

View File

@ -218,7 +218,7 @@ async function _runJob(job: any) {
hasAttachments,
hasVisualMediaAttachments,
hasFileAttachments,
} = await getAttachmentMetadata(found);
} = getAttachmentMetadata(found);
found.set({ hasAttachments, hasVisualMediaAttachments, hasFileAttachments });
}

View File

@ -1,7 +0,0 @@
import { getPasswordHash } from '../../ts/data/data';
export async function hasPassword() {
const hash = await getPasswordHash();
return !!hash;
}

View File

@ -8,6 +8,7 @@ import { OpenGroupMessageV2 } from '../../../session/apis/open_group_api/opengro
import { TestUtils } from '..';
import { OpenGroupRequestCommonType } from '../../../session/apis/open_group_api/opengroupV2/ApiUtil';
import { OpenGroupVisibleMessage } from '../../../session/messages/outgoing/visibleMessage/OpenGroupVisibleMessage';
import { MessageModel } from '../../../models/message';
export function generateVisibleMessage({
identifier,
@ -55,6 +56,15 @@ export function generateClosedGroupMessage(groupId?: string): ClosedGroupVisible
});
}
export function generateFakeIncomingPrivateMessage(): MessageModel {
const convoId = TestUtils.generateFakePubKeyStr();
return new MessageModel({
conversationId: convoId,
source: convoId,
type: 'incoming',
});
}
interface MockConversationParams {
id?: string;
members?: Array<string>;

View File

@ -0,0 +1,286 @@
import { expect } from 'chai';
import { SignalService } from '../../protobuf';
import {
getAttachmentMetadata,
hasFileAttachmentInMessage,
hasVisualMediaAttachmentInMessage,
} from '../../types/message/initializeAttachmentMetadata';
import { generateFakeIncomingPrivateMessage, stubWindowLog } from '../test-utils/utils';
// tslint:disable-next-line: max-func-body-length
describe('initializeAttachmentMetadata', () => {
beforeEach(() => {
stubWindowLog();
});
describe('hasAttachmentInMessage', () => {
it('no attachments should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
expect(hasFileAttachmentInMessage(msgModel)).to.be.false;
});
it('empty list attachments should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({ attachments: [] });
expect(hasFileAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment has undefined content type should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({ attachments: [{ contentType: undefined }] });
expect(hasFileAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment has null content type should return true', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({ attachments: [{ contentType: null }] });
expect(hasFileAttachmentInMessage(msgModel)).to.be.true;
});
it('first attachment is gif should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({ attachments: [{ contentType: 'image/gif' }] });
expect(hasFileAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment is gif should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({ attachments: [{ contentType: 'image/gif' }] });
expect(hasFileAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment is jpeg should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({ attachments: [{ contentType: 'image/jpeg' }] });
expect(hasFileAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment is png should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({ attachments: [{ contentType: 'image/png' }] });
expect(hasFileAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment is JPG should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({ attachments: [{ contentType: 'image/JPG' }] });
expect(hasFileAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment is PNG should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({ attachments: [{ contentType: 'image/PNG' }] });
expect(hasFileAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment is audio should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({ attachments: [{ contentType: 'audio/mp3' }] });
expect(hasFileAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment is flagged as voice message should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [
{ contentType: 'audio/mp3', flags: SignalService.AttachmentPointer.Flags.VOICE_MESSAGE },
],
});
expect(hasFileAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment is flagged as voice message but no content type is false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [
{ contentType: undefined, flags: SignalService.AttachmentPointer.Flags.VOICE_MESSAGE },
],
});
expect(hasFileAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment content type is audio and other is null should return true', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({ attachments: [{ contentType: 'audio/mp3' }, { contentType: null }] });
expect(hasFileAttachmentInMessage(msgModel)).to.be.true;
});
it('first attachment content type is audio and other is null should return true', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [{ contentType: 'audio/mp3' }, { contentType: 'file/whatever' }],
});
expect(hasFileAttachmentInMessage(msgModel)).to.be.true;
});
});
describe('hasVisualMediaAttachmentInMessage', () => {
it('no attachments should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
expect(hasVisualMediaAttachmentInMessage(msgModel)).to.be.false;
});
it('empty attachments list should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [],
});
expect(hasVisualMediaAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment type is undefined should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [{ contentType: undefined }],
});
expect(hasVisualMediaAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment type is null should return false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [{ contentType: null }],
});
expect(hasVisualMediaAttachmentInMessage(msgModel)).to.be.false;
});
it('first attachment type is image/whatever should return true', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [{ contentType: 'image/whatever' }],
});
expect(hasVisualMediaAttachmentInMessage(msgModel)).to.be.true;
});
it('first attachment type is jpeg should return true', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [{ contentType: 'image/jpeg' }],
});
expect(hasVisualMediaAttachmentInMessage(msgModel)).to.be.true;
});
it('first attachment type is png should return true', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [{ contentType: 'image/png' }],
});
expect(hasVisualMediaAttachmentInMessage(msgModel)).to.be.true;
});
it('first attachment type is JPG should return true', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [{ contentType: 'image/JPG' }],
});
expect(hasVisualMediaAttachmentInMessage(msgModel)).to.be.true;
});
it('multiple attachments where one is not image and one is returns true', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [{ contentType: 'audio/whatever' }, { contentType: 'image/JPG' }],
});
expect(hasVisualMediaAttachmentInMessage(msgModel)).to.be.true;
});
it('multiple attachments where both are images returns true', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [{ contentType: 'image/whatever' }, { contentType: 'image/JPG' }],
});
expect(hasVisualMediaAttachmentInMessage(msgModel)).to.be.true;
});
it('multiple attachments where none are images returns false', () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set({
attachments: [{ contentType: 'audio/whatever' }, { contentType: 'audio/JPG' }],
});
expect(hasVisualMediaAttachmentInMessage(msgModel)).to.be.false;
});
});
describe('getAttachmentMetadata', () => {
it('no attachments should return false x3', async () => {
const msgModel = generateFakeIncomingPrivateMessage();
const mt = await getAttachmentMetadata(msgModel);
expect(mt.hasAttachments).to.be.eq(0);
expect(mt.hasFileAttachments).to.be.eq(0);
expect(mt.hasVisualMediaAttachments).to.be.eq(0);
});
it('empty attachments [] should return false x3', async () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set('attachments', []);
const mt = await getAttachmentMetadata(msgModel);
expect(mt.hasAttachments).to.be.eq(0);
expect(mt.hasFileAttachments).to.be.eq(0);
expect(mt.hasVisualMediaAttachments).to.be.eq(0);
});
it('has one image attachment only', async () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set('attachments', [{ contentType: 'image/jpeg' }]);
const mt = await getAttachmentMetadata(msgModel);
expect(mt.hasAttachments).to.be.eq(1);
expect(mt.hasFileAttachments).to.be.eq(0);
expect(mt.hasVisualMediaAttachments).to.be.eq(1);
});
it('has two image attachment only', async () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set('attachments', [{ contentType: 'image/jpeg' }, { contentType: 'image/jpeg' }]);
const mt = await getAttachmentMetadata(msgModel);
expect(mt.hasAttachments).to.be.eq(1);
expect(mt.hasFileAttachments).to.be.eq(0);
expect(mt.hasVisualMediaAttachments).to.be.eq(1);
});
it('has one audio attachment only', async () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set('attachments', [{ contentType: 'audio/mp3' }]);
const mt = await getAttachmentMetadata(msgModel);
expect(mt.hasAttachments).to.be.eq(1);
expect(mt.hasFileAttachments).to.be.eq(0);
expect(mt.hasVisualMediaAttachments).to.be.eq(0);
});
it('has one file attachment only', async () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set('attachments', [{ contentType: 'whatever' }]);
const mt = await getAttachmentMetadata(msgModel);
expect(mt.hasAttachments).to.be.eq(1);
expect(mt.hasFileAttachments).to.be.eq(1);
expect(mt.hasVisualMediaAttachments).to.be.eq(0);
});
it('has two file attachment only', async () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set('attachments', [{ contentType: 'whatever' }, { contentType: 'whatever' }]);
const mt = await getAttachmentMetadata(msgModel);
expect(mt.hasAttachments).to.be.eq(1);
expect(mt.hasFileAttachments).to.be.eq(1);
expect(mt.hasVisualMediaAttachments).to.be.eq(0);
});
it('has two attachments with undefined contenttype', async () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set('attachments', [{ contentType: undefined }, { contentType: undefined }]);
const mt = await getAttachmentMetadata(msgModel);
expect(mt.hasAttachments).to.be.eq(1);
expect(mt.hasFileAttachments).to.be.eq(0);
expect(mt.hasVisualMediaAttachments).to.be.eq(0);
});
it('has two attachments with null contenttype', async () => {
const msgModel = generateFakeIncomingPrivateMessage();
msgModel.set('attachments', [{ contentType: null }, { contentType: null }]);
const mt = await getAttachmentMetadata(msgModel);
expect(mt.hasAttachments).to.be.eq(1);
expect(mt.hasFileAttachments).to.be.eq(1);
expect(mt.hasVisualMediaAttachments).to.be.eq(0);
});
});
});

View File

@ -1,23 +1,23 @@
import { MessageModel } from '../../models/message';
import * as Attachment from '../Attachment';
const hasAttachment = (predicate: (value: Attachment.Attachment) => boolean) => (
const hasAttachmentInMessage = (predicate: (value: Attachment.Attachment) => boolean) => (
message: MessageModel
): boolean => Boolean((message.get('attachments') || []).some(predicate));
const hasFileAttachment = hasAttachment(Attachment.isFile);
const hasVisualMediaAttachment = hasAttachment(Attachment.isVisualMedia);
export const hasFileAttachmentInMessage = hasAttachmentInMessage(Attachment.isFile);
export const hasVisualMediaAttachmentInMessage = hasAttachmentInMessage(Attachment.isVisualMedia);
export const getAttachmentMetadata = async (
export const getAttachmentMetadata = (
message: MessageModel
): Promise<{
): {
hasAttachments: 1 | 0;
hasFileAttachments: 1 | 0;
hasVisualMediaAttachments: 1 | 0;
}> => {
const hasAttachments = Boolean(message.get('attachments').length) ? 1 : 0;
const hasFileAttachments = hasFileAttachment(message) ? 1 : 0;
const hasVisualMediaAttachments = hasVisualMediaAttachment(message) ? 1 : 0;
} => {
const hasAttachments = Boolean((message.get('attachments') || []).length) ? 1 : 0;
const hasFileAttachments = hasFileAttachmentInMessage(message) ? 1 : 0;
const hasVisualMediaAttachments = hasVisualMediaAttachmentInMessage(message) ? 1 : 0;
return {
hasAttachments,