Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
fix(commands): Assign default TTL to send-tab commands
Browse files Browse the repository at this point in the history
  • Loading branch information
eoger committed Oct 4, 2018
1 parent 315ef5c commit 6afb0e3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/routes/devices-and-sessions.js
Expand Up @@ -19,6 +19,10 @@ const HEX_STRING = validators.HEX_STRING
const DEVICES_SCHEMA = require('../devices').schema
const PUSH_PAYLOADS_SCHEMA_PATH = path.resolve(__dirname, '../../docs/pushpayloads.schema.json')

// Assign a default TTL for well-known commands if a request didn't specify it.
const DEFAULT_COMMAND_TTL = new Map([
['https://identity.mozilla.com/cmd/open-uri', 30 * 24 * 3600], // 30 days
])

module.exports = (log, db, config, customs, push, pushbox, devices) => {
// Loads and compiles a json validator for the payloads received
Expand Down Expand Up @@ -298,7 +302,8 @@ module.exports = (log, db, config, customs, push, pushbox, devices) => {
handler: async function (request) {
log.begin('Account.invokeDeviceCommand', request)

const {target, command, payload, ttl} = request.payload
const {target, command, payload} = request.payload
let {ttl} = request.payload
const sessionToken = request.auth.credentials
const uid = sessionToken.uid
const sender = sessionToken.deviceId
Expand All @@ -310,6 +315,10 @@ module.exports = (log, db, config, customs, push, pushbox, devices) => {
if (! device.availableCommands.hasOwnProperty(command)) {
throw error.unavailableDeviceCommand()
}
// 0 is perfectly acceptable TTL, hence the strict equality check.
if (ttl === undefined && DEFAULT_COMMAND_TTL.has(command)) {
ttl = DEFAULT_COMMAND_TTL.get(command);
}
const data = {
command,
payload,
Expand Down
50 changes: 46 additions & 4 deletions test/local/routes/devices-and-sessions.js
Expand Up @@ -629,7 +629,8 @@ describe('/account/devices/invoke_command', function () {
id: 'bogusid1',
type: 'mobile',
availableCommands: {
bogusCommandName: 'bogusData'
bogusCommandName: 'bogusData',
'https://identity.mozilla.com/cmd/open-uri': 'morebogusdata',
}
},
{
Expand Down Expand Up @@ -657,9 +658,7 @@ describe('/account/devices/invoke_command', function () {

it('stores commands using the pushbox service and sends a notification', () => {
const mockPushbox = mocks.mockPushbox({
store: sinon.spy(() => {
return Promise.resolve({ index: 15 })
})
store: sinon.spy(async () => ({ index: 15 }))
})
const target = 'bogusid1'
const sender = 'bogusid2'
Expand Down Expand Up @@ -701,6 +700,49 @@ describe('/account/devices/invoke_command', function () {
})
})

it('uses a default TTL for send-tab commands with no TTL specified', () => {
const THIRTY_DAYS_IN_SECS = 30 * 24 * 3600;
const commandSendTab = 'https://identity.mozilla.com/cmd/open-uri'
const mockPushbox = mocks.mockPushbox({
store: sinon.spy(async () => ({ index: 15 }))
})
const target = 'bogusid1'
const sender = 'bogusid2'
const payload = { 'bogus': 'payload' }
mockRequest.payload = {
target,
command: commandSendTab,
payload
}
const route = getRoute(makeRoutes({
customs: mockCustoms,
log: mockLog,
push: mockPush,
pushbox: mockPushbox,
db: mockDB
}), '/account/devices/invoke_command')

return runTest(route, mockRequest).then(() => {
assert.equal(mockPushbox.store.callCount, 1, 'pushbox was called')
assert.calledWithExactly(mockPushbox.store, uid, target, {
command: commandSendTab,
payload,
sender
}, THIRTY_DAYS_IN_SECS)

assert.equal(mockPush.notifyCommandReceived.callCount, 1, 'notifyCommandReceived was called')
assert.calledWithExactly(mockPush.notifyCommandReceived,
uid,
mockDevices[0],
commandSendTab,
sender,
15,
'https://public.url/v1/account/device/commands?index=15&limit=1',
THIRTY_DAYS_IN_SECS
)
})
})

it('rejects if sending to an unknown device', () => {
const mockPushbox = mocks.mockPushbox()
const target = 'unknowndevice'
Expand Down

0 comments on commit 6afb0e3

Please sign in to comment.