validateContact: Return error instead of logging

This commit is contained in:
Scott Nonnenberg 2018-05-08 10:31:16 -07:00
parent bb01218e83
commit adbe989949
2 changed files with 55 additions and 54 deletions

View File

@ -1,5 +1,7 @@
const { omit, compact, map } = require('lodash');
const { toLogFormat } = require('./errors');
exports.parseAndWriteContactAvatar = upgradeAttachment => async (
contact,
context = {}
@ -18,10 +20,15 @@ exports.parseAndWriteContactAvatar = upgradeAttachment => async (
// eliminates empty numbers, emails, and addresses; adds type if not provided
const contactWithCleanedElements = parseContact(contactWithUpdatedAvatar);
// We'll log if the contact is invalid, leave everything as-is
validateContact(contactWithCleanedElements, {
const error = exports._validateContact(contactWithCleanedElements, {
messageId: idForLogging(message),
});
if (error) {
console.log(
'Contact.parseAndWriteContactAvatar: contact was malformed.',
toLogFormat(error)
);
}
return contactWithCleanedElements;
};
@ -41,15 +48,14 @@ function idForLogging(message) {
return `${message.source}.${message.sourceDevice} ${message.sent_at}`;
}
function validateContact(contact, options = {}) {
exports._validateContact = (contact, options = {}) => {
const { messageId } = options;
const { name, number, email, address, organization } = contact;
if ((!name || !name.displayName) && !organization) {
console.log(
return new Error(
`Message ${messageId}: Contact had neither 'displayName' nor 'organization'`
);
return false;
}
if (
@ -57,14 +63,13 @@ function validateContact(contact, options = {}) {
(!email || !email.length) &&
(!address || !address.length)
) {
console.log(
return new Error(
`Message ${messageId}: Contact had no included numbers, email or addresses`
);
return false;
}
return true;
}
return null;
};
function cleanBasicItem(item) {
if (!item.value) {

View File

@ -7,9 +7,9 @@ const {
} = require('../../../js/modules/string_to_array_buffer');
describe('Contact', () => {
describe('parseAndWriteContactAvatar', () => {
const NUMBER = '+12025550099';
const NUMBER = '+12025550099';
describe('parseAndWriteContactAvatar', () => {
it('handles message with no avatar in contact', async () => {
const upgradeAttachment = sinon
.stub()
@ -246,49 +246,7 @@ describe('Contact', () => {
assert.deepEqual(result, expected);
});
it('logs if contact has no name.displayName or organization', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
source: NUMBER,
sourceDevice: '1',
sent_at: 1232132,
contact: [
{
name: {
name: 'Someone',
},
number: [
{
type: 1,
value: NUMBER,
},
],
},
],
};
const expected = {
name: {
name: 'Someone',
},
number: [
{
type: 1,
value: NUMBER,
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('removes invalid elements then logs if no values remain in contact', async () => {
it('removes invalid elements if no values remain in contact', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
@ -353,4 +311,42 @@ describe('Contact', () => {
assert.deepEqual(result, message.contact[0]);
});
});
describe('_validateContact', () => {
it('returns error if contact has no name.displayName or organization', () => {
const messageId = 'the-message-id';
const contact = {
name: {
name: 'Someone',
},
number: [
{
type: 1,
value: NUMBER,
},
],
};
const expected =
"Message the-message-id: Contact had neither 'displayName' nor 'organization'";
const result = Contact._validateContact(contact, { messageId });
assert.deepEqual(result.message, expected);
});
it('logs if no values remain in contact', async () => {
const messageId = 'the-message-id';
const contact = {
name: {
displayName: 'Someone Somewhere',
},
number: [],
email: [],
};
const expected =
'Message the-message-id: Contact had no included numbers, email or addresses';
const result = Contact._validateContact(contact, { messageId });
assert.deepEqual(result.message, expected);
});
});
});