session-desktop/test/backup_test.js

227 lines
5.9 KiB
JavaScript
Raw Normal View History

2018-04-20 21:11:56 +02:00
/* global Signal: false */
/* global assert: false */
'use strict';
2018-04-20 21:11:56 +02:00
describe('Backup', () => {
describe('_sanitizeFileName', () => {
it('leaves a basic string alone', () => {
const initial = 'Hello, how are you #5 (\'fine\' + great).jpg';
const expected = initial;
assert.strictEqual(Signal.Backup._sanitizeFileName(initial), expected);
});
2018-04-20 21:11:56 +02:00
it('replaces all unknown characters', () => {
const initial = '!@$%^&*=';
const expected = '________';
assert.strictEqual(Signal.Backup._sanitizeFileName(initial), expected);
});
});
2018-04-20 21:11:56 +02:00
describe('_trimFileName', () => {
it('handles a file with no extension', () => {
const initial = '0123456789012345678901234567890123456789';
const expected = '012345678901234567890123456789';
assert.strictEqual(Signal.Backup._trimFileName(initial), expected);
});
2018-04-20 21:11:56 +02:00
it('handles a file with a long extension', () => {
const initial = '0123456789012345678901234567890123456789.01234567890123456789';
const expected = '012345678901234567890123456789';
assert.strictEqual(Signal.Backup._trimFileName(initial), expected);
});
2018-04-20 21:11:56 +02:00
it('handles a file with a normal extension', () => {
const initial = '01234567890123456789012345678901234567890123456789.jpg';
const expected = '012345678901234567890123.jpg';
assert.strictEqual(Signal.Backup._trimFileName(initial), expected);
});
});
2018-04-20 21:11:56 +02:00
describe('_getExportAttachmentFileName', () => {
it('uses original filename if attachment has one', () => {
const message = {
body: 'something',
};
2018-04-20 21:11:56 +02:00
const index = 0;
const attachment = {
fileName: 'blah.jpg',
};
2018-04-20 21:11:56 +02:00
const expected = 'blah.jpg';
2018-04-20 21:11:56 +02:00
const actual = Signal.Backup._getExportAttachmentFileName(
message,
index,
attachment
);
assert.strictEqual(actual, expected);
});
2018-04-20 21:11:56 +02:00
it('uses attachment id if no filename', () => {
const message = {
body: 'something',
};
2018-04-20 21:11:56 +02:00
const index = 0;
const attachment = {
id: '123',
};
2018-04-20 21:11:56 +02:00
const expected = '123';
2018-04-20 21:11:56 +02:00
const actual = Signal.Backup._getExportAttachmentFileName(
message,
index,
attachment
);
assert.strictEqual(actual, expected);
});
2018-04-20 21:11:56 +02:00
it('uses filename and contentType if available', () => {
const message = {
body: 'something',
};
2018-04-20 21:11:56 +02:00
const index = 0;
const attachment = {
id: '123',
2018-04-20 21:11:56 +02:00
contentType: 'image/jpeg',
};
2018-04-20 21:11:56 +02:00
const expected = '123.jpeg';
2018-04-20 21:11:56 +02:00
const actual = Signal.Backup._getExportAttachmentFileName(
message,
index,
attachment
);
assert.strictEqual(actual, expected);
});
2018-04-20 21:11:56 +02:00
it('handles strange contentType', () => {
const message = {
body: 'something',
};
2018-04-20 21:11:56 +02:00
const index = 0;
const attachment = {
id: '123',
2018-04-20 21:11:56 +02:00
contentType: 'something',
};
2018-04-20 21:11:56 +02:00
const expected = '123.something';
2018-04-20 21:11:56 +02:00
const actual = Signal.Backup._getExportAttachmentFileName(
message,
index,
attachment
);
assert.strictEqual(actual, expected);
});
});
2018-04-20 21:11:56 +02:00
describe('_getAnonymousAttachmentFileName', () => {
it('uses message id', () => {
const message = {
id: 'id-45',
body: 'something',
};
2018-04-20 21:11:56 +02:00
const index = 0;
const attachment = {
fileName: 'blah.jpg',
};
2018-04-20 21:11:56 +02:00
const expected = 'id-45';
2018-04-20 21:11:56 +02:00
const actual = Signal.Backup._getAnonymousAttachmentFileName(
message,
index,
attachment
);
assert.strictEqual(actual, expected);
});
2018-04-20 21:11:56 +02:00
it('appends index if it is above zero', () => {
const message = {
id: 'id-45',
body: 'something',
};
2018-04-20 21:11:56 +02:00
const index = 1;
const attachment = {
fileName: 'blah.jpg',
};
2018-04-20 21:11:56 +02:00
const expected = 'id-45-1';
2018-04-20 21:11:56 +02:00
const actual = Signal.Backup._getAnonymousAttachmentFileName(
message,
index,
attachment
);
assert.strictEqual(actual, expected);
});
});
2018-04-20 21:11:56 +02:00
describe('_getConversationDirName', () => {
it('uses name if available', () => {
const conversation = {
active_at: 123,
name: '0123456789012345678901234567890123456789',
2018-04-20 21:11:56 +02:00
id: 'id',
};
2018-04-20 21:11:56 +02:00
const expected = '123 (012345678901234567890123456789 id)';
assert.strictEqual(Signal.Backup._getConversationDirName(conversation), expected);
});
2018-04-20 21:11:56 +02:00
it('uses just id if name is not available', () => {
const conversation = {
active_at: 123,
2018-04-20 21:11:56 +02:00
id: 'id',
};
2018-04-20 21:11:56 +02:00
const expected = '123 (id)';
assert.strictEqual(Signal.Backup._getConversationDirName(conversation), expected);
});
2018-04-20 21:11:56 +02:00
it('uses inactive for missing active_at', () => {
const conversation = {
name: 'name',
2018-04-20 21:11:56 +02:00
id: 'id',
};
2018-04-20 21:11:56 +02:00
const expected = 'inactive (name id)';
assert.strictEqual(Signal.Backup._getConversationDirName(conversation), expected);
});
});
2018-04-20 21:11:56 +02:00
describe('_getConversationLoggingName', () => {
it('uses plain id if conversation is private', () => {
const conversation = {
active_at: 123,
id: 'id',
2018-04-20 21:11:56 +02:00
type: 'private',
};
2018-04-20 21:11:56 +02:00
const expected = '123 (id)';
assert.strictEqual(
Signal.Backup._getConversationLoggingName(conversation),
expected
);
});
2018-04-20 21:11:56 +02:00
it('uses just id if name is not available', () => {
const conversation = {
active_at: 123,
id: 'groupId',
2018-04-20 21:11:56 +02:00
type: 'group',
};
2018-04-20 21:11:56 +02:00
const expected = '123 ([REDACTED_GROUP]pId)';
assert.strictEqual(
Signal.Backup._getConversationLoggingName(conversation),
expected
);
});
2018-04-20 21:11:56 +02:00
it('uses inactive for missing active_at', () => {
const conversation = {
id: 'id',
2018-04-20 21:11:56 +02:00
type: 'private',
};
2018-04-20 21:11:56 +02:00
const expected = 'inactive (id)';
assert.strictEqual(
Signal.Backup._getConversationLoggingName(conversation),
expected
);
});
});
});