Commit ffea3fca authored by aguestuser's avatar aguestuser
Browse files

Merge branch 'ns-fix-tests-post-npdev' into 'master'

[ns] fix unit tests broken by demo day patches to welcome message

See merge request team-friendo/signalboost!97
parents 45d50c75 d2b20a81
......@@ -48,15 +48,19 @@ const dispatch = async ({ commandResult, dispatchable }) => {
}
}
// (CommandResult, Dispatchable) -> MessageType
const parseMessageType = (commandResult, { sender, channel }) => {
if (commandResult.status === statuses.NOOP) {
if (sender.type === PUBLISHER) return BROADCAST_MESSAGE
if (channel.phoneNumber === signupChannel) return SIGNUP_MESSAGE
return BROADCAST_RESPONSE
}
return COMMAND_RESULT
}
const handleSignupMessage = async ({ sock, channel, sender, sdMessage }) => {
const notifications = messagesIn(defaultLanguage).notifications
const oneDay = 60 * 60 * 24
// set expiry
await Promise.all(
channel.publications.map(p =>
signal.setExpiration(sock, channel.phoneNumber, p.publisherPhoneNumber, oneDay),
),
)
// TODO(aguestuser|2019-11-09): send this as a disappearing message
// notify admins of signpu request
await notify({
sock,
......@@ -73,16 +77,6 @@ const handleSignupMessage = async ({ sock, channel, sender, sdMessage }) => {
})
}
// CommandResult -> [MessageType, NotificationType]
const parseMessageType = (commandResult, { sender, channel }) => {
if (commandResult.status === statuses.NOOP) {
if (sender.type === PUBLISHER) return BROADCAST_MESSAGE
if (channel.phoneNumber === signupChannel) return SIGNUP_MESSAGE
return BROADCAST_RESPONSE
}
return COMMAND_RESULT
}
const handleBroadcastResponse = dispatchable => {
const {
channel: { responsesEnabled },
......
......@@ -96,10 +96,7 @@ const updateSafetyNumber = async (db, sock, inboundMsg) => {
)
if (recipient.type === memberTypes.NONE) return Promise.resolve()
if (
recipient.type === memberTypes.PUBLISHER &&
!isWelcomeMessage(sdMessage, channelPhoneNumber)
) {
if (recipient.type === memberTypes.PUBLISHER && !isWelcomeMessage(sdMessage)) {
// If it's a welcome message, someone just re-authorized this recipient, we want to re-trust their keys
return safetyNumberService
.deauthorize(db, sock, channelPhoneNumber, memberPhoneNumber)
......@@ -143,8 +140,8 @@ const classifyPhoneNumber = async (db, channelPhoneNumber, senderPhoneNumber) =>
return { phoneNumber: senderPhoneNumber, type, language }
}
const isWelcomeMessage = (sdMessage, channelPhoneNumber) => {
const phoneNumberPattern = /\+\d{9,15}/
const isWelcomeMessage = sdMessage => {
const phoneNumberPattern = /\+\d{9,15}/g
const headerPattern = /\[.*]\n\n/
const strippedMessage = sdMessage.messageBody
.replace(headerPattern, '')
......@@ -155,9 +152,8 @@ const isWelcomeMessage = (sdMessage, channelPhoneNumber) => {
// properly localize this, by including more languages in the input array here!
[messagesIn(defaultLanguage)].find(
messages =>
strippedMessage === messages.notifications.welcome('', channelPhoneNumber).trim() || //if added by another admin
strippedMessage ===
messages.notifications.welcome(messages.systemName, channelPhoneNumber).trim(), //if added by sysadmin
strippedMessage === messages.notifications.welcome('', '').trim() || //if added by another admin
strippedMessage === messages.notifications.welcome(messages.systemName, '').trim(), //if added by sysadmin
),
)
}
......
......@@ -13,8 +13,11 @@ const {
signal: { welcomeDelay },
} = require('../../config')
const systemName = messagesIn(defaultLanguage).systemName
const welcomeNotification = messagesIn(defaultLanguage).notifications.welcome
const welcomeNotificationOf = channelPhoneNumber =>
messagesIn(defaultLanguage).notifications.welcome(
messagesIn(defaultLanguage).systemName,
channelPhoneNumber,
)
// ({ Database, Socket, string, string }) -> Promise<SignalboostStatus>
const addPublisher = async ({ db, sock, channelPhoneNumber, publisherPhoneNumber }) => {
......@@ -24,12 +27,12 @@ const addPublisher = async ({ db, sock, channelPhoneNumber, publisherPhoneNumber
db,
sock,
channel,
notification: welcomeNotification(systemName, channelPhoneNumber),
notification: welcomeNotificationOf(channelPhoneNumber),
recipients: [publisherPhoneNumber],
})
return {
status: sbStatuses.SUCCESS,
message: welcomeNotification,
message: welcomeNotificationOf(channelPhoneNumber),
}
}
......@@ -44,7 +47,7 @@ const create = async ({ db, sock, phoneNumber, name, publishers }) => {
db,
sock,
channel,
notification: welcomeNotification(systemName, channel.phoneNumber),
notification: welcomeNotificationOf(channel.phoneNumber),
recipients: channel.publications.map(p => p.publisherPhoneNumber),
})
return { status: pNumStatuses.ACTIVE, phoneNumber, name, publishers }
......@@ -82,5 +85,4 @@ module.exports = {
create,
addPublisher,
list,
welcomeNotification,
}
......@@ -23,7 +23,7 @@ describe('messages module', () => {
it('shows publisher phone numbers and subscriber count', () => {
const msg = cr.info.publisher(channel)
expect(msg).to.include(
`publishers: ${channel.publications.map(a => a.channelPhoneNumber).join(', ')}`,
`admins: ${channel.publications.map(a => a.channelPhoneNumber).join(', ')}`,
)
expect(msg).to.include('subscribers: 2')
})
......@@ -32,7 +32,7 @@ describe('messages module', () => {
describe('for subscriber', () => {
it('shows publisher count and subscriber count', () => {
const msg = cr.info.subscriber(channel)
expect(msg).to.include('publishers: 2')
expect(msg).to.include('admins: 2')
expect(msg).to.include('subscribers: 2')
})
})
......
......@@ -58,26 +58,30 @@ describe('messenger service', () => {
describe('parsing a message type from a command result', () => {
it('parses a broadcast message', () => {
const msg = { command: 'foo', status: statuses.NOOP }
expect(messenger.parseMessageType(msg, publisherSender)).to.eql(
const dispatchable = { channel, sender: publisherSender }
expect(messenger.parseMessageType(msg, dispatchable)).to.eql(
messageTypes.BROADCAST_MESSAGE,
)
})
it('parses a broadcast response from a subscriber', () => {
const msg = { command: 'foo', status: statuses.NOOP }
expect(messenger.parseMessageType(msg, subscriberSender)).to.eql(
const dispatchable = { channel, sender: subscriberSender }
expect(messenger.parseMessageType(msg, dispatchable)).to.eql(
messageTypes.BROADCAST_RESPONSE,
)
})
it('parses a broadcast response from a random person', () => {
const msg = { command: 'foo', status: statuses.NOOP }
expect(messenger.parseMessageType(msg, randomSender)).to.eql(messageTypes.BROADCAST_RESPONSE)
const dispatchable = { channel, sender: randomSender }
expect(messenger.parseMessageType(msg, dispatchable)).to.eql(messageTypes.BROADCAST_RESPONSE)
})
it('parses a command result', () => {
const msg = { command: 'JOIN', status: statuses.SUCCESS }
expect(messenger.parseMessageType(msg, randomSender)).to.eql(messageTypes.COMMAND_RESULT)
const dispatchable = { channel, sender: randomSender }
expect(messenger.parseMessageType(msg, dispatchable)).to.eql(messageTypes.COMMAND_RESULT)
})
})
......@@ -263,7 +267,7 @@ describe('messenger service', () => {
const newPublisher = genPhoneNumber()
const sdMessage = `${commands.ADD} ${newPublisher}`
const response = messages.commandResponses.publisher.add.success(newPublisher)
const welcome = messages.notifications.welcome(publisherSender.phoneNumber)
const welcome = messages.notifications.welcome(publisherSender.phoneNumber, channel.phoneNumber)
const alert = messages.notifications.publisherAdded(
publisherSender.phoneNumber,
newPublisher,
......
......@@ -272,9 +272,12 @@ describe('dispatcher service', () => {
})
describe('when message is a welcome message from an admin', () => {
const welcomeMessage = `[foo]\n\n${messagesIn(defaultLanguage).notifications.welcome(
genPhoneNumber(),
channel.phoneNumber,
)}`
const failedWelcomeMessage = {
type: signal.messageTypes.ERROR,
data: {
......@@ -308,6 +311,7 @@ describe('dispatcher service', () => {
describe('when message is a welcome message from a sysadmin', () => {
const welcomeMessage = `[foo]\n\n${messagesIn(defaultLanguage).notifications.welcome(
messagesIn(defaultLanguage).systemName,
channel.phoneNumber,
)}`
const failedWelcomeMessage = {
type: signal.messageTypes.ERROR,
......
......@@ -8,18 +8,19 @@ import messenger from '../../../../app/services/dispatcher/messenger'
import { genPhoneNumber } from '../../../support/factories/phoneNumber'
import { deepChannelAttrs } from '../../../support/factories/channel'
import { statuses } from '../../../../app/constants'
import {
welcomeNotification,
create,
addPublisher,
list,
} from '../../../../app/services/registrar/channel'
import { create, addPublisher, list } from '../../../../app/services/registrar/channel'
import { messagesIn } from '../../../../app/services/dispatcher/messages'
import { defaultLanguage } from '../../../../app/config'
describe('channel registrar', () => {
const db = {}
const sock = {}
const phoneNumber = genPhoneNumber()
const channelPhoneNumber = phoneNumber
const welcomeNotification = messagesIn(defaultLanguage).notifications.welcome(
messagesIn(defaultLanguage).systemName,
channelPhoneNumber,
)
const name = '#blackops'
const publishers = [genPhoneNumber(), genPhoneNumber()]
const publisherPhoneNumber = publishers[0]
......@@ -51,7 +52,9 @@ describe('channel registrar', () => {
updatePhoneNumberStub = sinon.stub(phoneNumberRepository, 'update')
notifyStub = sinon.stub(messenger, 'notify')
findAllDeepStub = sinon.stub(channelRepository, 'findAllDeep')
findByNumberStub = sinon.stub(channelRepository, 'findByPhoneNumber').returns(Promise.resolve(channelInstance))
findByNumberStub = sinon
.stub(channelRepository, 'findByPhoneNumber')
.returns(Promise.resolve(channelInstance))
})
afterEach(() => {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment