pushwoosh-client
Advanced tools
Comparing version 1.1.1 to 1.2.0
37
index.js
@@ -112,3 +112,38 @@ var request = require('request'), | ||
PushwooshClient.prototype.registerDevice = function (options, callback) { | ||
var client = this; | ||
if (typeof options.push_token !== 'string') { | ||
return callback(new Error('Device push token must be provided (string)')); | ||
} | ||
if (typeof options.hwid !== 'string') { | ||
return callback(new Error('Device hwid must be provided (string)')); | ||
} | ||
if (typeof options.device_type !== 'number') { | ||
return callback(new Error('Device type must be provided (number)')); | ||
} | ||
var body = { | ||
request: { | ||
application: client.appCode, | ||
push_token: options.push_token, | ||
hwid: options.hwid, | ||
device_type: options.device_type | ||
} | ||
}; | ||
if (options.timezone) { | ||
body.request.timezone = options.timezone; | ||
} | ||
if (options.language) { | ||
body.request.language = options.language; | ||
} | ||
client.sendRequest('registerDevice', body, function(error, response, body){ | ||
if (error) { | ||
return callback(error); | ||
} | ||
client.parseResponse(response, body, callback); | ||
}); | ||
}; | ||
PushwooshClient.prototype.sendRequest = function (method, data, callback) { | ||
@@ -143,2 +178,2 @@ request({ | ||
module.exports = PushwooshClient; | ||
module.exports = PushwooshClient; |
{ | ||
"name": "pushwoosh-client", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "A node js client to consume the Pushwoosh API to send push notifications to mobile devices", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -99,2 +99,22 @@ # pushwoosh-node-client | ||
#### Register device | ||
To register a device's push token in Pushwoosh: | ||
```javascript | ||
var Pushwoosh = require('pushwoosh-client'); | ||
var client= new Pushwoosh('AppCode', 'AuthToken'); | ||
var registerDeviceOptions = { | ||
push_token: 'pushtoken', | ||
hwid: 'hwid', | ||
device_type: 3, | ||
language: 'en', // optional, two-letter code ISO-639-1 | ||
timezone: -3600 // optional, offset in seconds | ||
}; | ||
// this will register the device for the client's 'AppCode' application | ||
client.registerDevice(registerDeviceOptions, function(error, response) { | ||
... | ||
}); | ||
``` | ||
### Tests | ||
@@ -101,0 +121,0 @@ |
@@ -749,1 +749,374 @@ var test = require('tape'), | ||
}); | ||
test('Pushwoosh register token success case with only required params', function (t) { | ||
t.plan(5); | ||
var mockDevice = 'someToken', | ||
mockHwid = 'someHwid', | ||
mockDeviceType = 3, | ||
mockOptions = { | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType | ||
}, | ||
mockResponse = { | ||
statusCode: 200 | ||
}, | ||
mockBodyResponse = { | ||
status_code: 200, | ||
response: {} | ||
}, | ||
expectedBody = { | ||
request: { | ||
application: testAppCode, | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType | ||
} | ||
}; | ||
fraudster.registerMock('request', function (params, callback) { | ||
t.ok(params, 'Params exists'); | ||
t.deepEqual(params.body, expectedBody, 'Body are as expected'); | ||
t.equal(params.method, 'POST', 'Method is POST as expected'); | ||
callback(null, mockResponse, mockBodyResponse); | ||
}); | ||
var PwClient = getCleanTestObject(), | ||
client = new PwClient(testAppCode, testAuthToken); | ||
client.registerDevice(mockOptions, function (err, response) { | ||
t.notOk(err, 'No error as expected'); | ||
t.deepEqual(response, {}, 'response is the same'); | ||
}); | ||
}); | ||
test('Pushwoosh register token success case with required params and timezone', function (t) { | ||
t.plan(5); | ||
var mockDevice = 'someToken', | ||
mockHwid = 'someHwid', | ||
mockDeviceType = 3, | ||
mockTimezone = 'mockTimezone', | ||
mockOptions = { | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType, | ||
timezone: mockTimezone | ||
}, | ||
mockResponse = { | ||
statusCode: 200 | ||
}, | ||
mockBodyResponse = { | ||
status_code: 200, | ||
response: {} | ||
}, | ||
expectedBody = { | ||
request: { | ||
application: testAppCode, | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType, | ||
timezone: mockTimezone | ||
} | ||
}; | ||
fraudster.registerMock('request', function (params, callback) { | ||
t.ok(params, 'Params exists'); | ||
t.deepEqual(params.body, expectedBody, 'Body are as expected'); | ||
t.equal(params.method, 'POST', 'Method is POST as expected'); | ||
callback(null, mockResponse, mockBodyResponse); | ||
}); | ||
var PwClient = getCleanTestObject(), | ||
client = new PwClient(testAppCode, testAuthToken); | ||
client.registerDevice(mockOptions, function (err, response) { | ||
t.notOk(err, 'No error as expected'); | ||
t.deepEqual(response, {}, 'response is the same'); | ||
}); | ||
}); | ||
test('Pushwoosh register token success case with required params and language', function (t) { | ||
t.plan(5); | ||
var mockDevice = 'someToken', | ||
mockHwid = 'someHwid', | ||
mockDeviceType = 3, | ||
mockLanguage = 'mockLanguage', | ||
mockOptions = { | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType, | ||
language: mockLanguage | ||
}, | ||
mockResponse = { | ||
statusCode: 200 | ||
}, | ||
mockBodyResponse = { | ||
status_code: 200, | ||
response: {} | ||
}, | ||
expectedBody = { | ||
request: { | ||
application: testAppCode, | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType, | ||
language: mockLanguage | ||
} | ||
}; | ||
fraudster.registerMock('request', function (params, callback) { | ||
t.ok(params, 'Params exists'); | ||
t.deepEqual(params.body, expectedBody, 'Body are as expected'); | ||
t.equal(params.method, 'POST', 'Method is POST as expected'); | ||
callback(null, mockResponse, mockBodyResponse); | ||
}); | ||
var PwClient = getCleanTestObject(), | ||
client = new PwClient(testAppCode, testAuthToken); | ||
client.registerDevice(mockOptions, function (err, response) { | ||
t.notOk(err, 'No error as expected'); | ||
t.deepEqual(response, {}, 'response is the same'); | ||
}); | ||
}); | ||
test('Pushwoosh register token success case with required params, timezone and language', function (t) { | ||
t.plan(5); | ||
var mockDevice = 'someToken', | ||
mockHwid = 'someHwid', | ||
mockDeviceType = 3, | ||
mockTimezone = 'mockTimezone', | ||
mockLanguage = 'mockLanguage', | ||
mockOptions = { | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType, | ||
timezone: mockTimezone, | ||
language: mockLanguage | ||
}, | ||
mockResponse = { | ||
statusCode: 200 | ||
}, | ||
mockBodyResponse = { | ||
status_code: 200, | ||
response: {} | ||
}, | ||
expectedBody = { | ||
request: { | ||
application: testAppCode, | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType, | ||
timezone: mockTimezone, | ||
language: mockLanguage | ||
} | ||
}; | ||
fraudster.registerMock('request', function (params, callback) { | ||
t.ok(params, 'Params exists'); | ||
t.deepEqual(params.body, expectedBody, 'Body are as expected'); | ||
t.equal(params.method, 'POST', 'Method is POST as expected'); | ||
callback(null, mockResponse, mockBodyResponse); | ||
}); | ||
var PwClient = getCleanTestObject(), | ||
client = new PwClient(testAppCode, testAuthToken); | ||
client.registerDevice(mockOptions, function (err, response) { | ||
t.notOk(err, 'No error as expected'); | ||
t.deepEqual(response, {}, 'response is the same'); | ||
}); | ||
}); | ||
test('Pushwoosh register token error case with no push token', function (t) { | ||
t.plan(2); | ||
var mockHwid = 'someHwid', | ||
mockDeviceType = 3, | ||
mockOptions = { | ||
hwid: mockHwid, | ||
device_type: mockDeviceType | ||
}; | ||
var PwClient = getCleanTestObject(), | ||
client = new PwClient(testAppCode, testAuthToken); | ||
client.registerDevice(mockOptions, function (err, response) { | ||
t.deepEqual(err, new Error('Device push token must be provided'), 'Error as expected'); | ||
t.notOk(response, 'No response as expected'); | ||
}); | ||
}); | ||
test('Pushwoosh register token error case with no hwid', function (t) { | ||
t.plan(2); | ||
var mockDevice = 'someToken', | ||
mockDeviceType = 3, | ||
mockOptions = { | ||
push_token: mockDevice, | ||
device_type: mockDeviceType | ||
}; | ||
var PwClient = getCleanTestObject(), | ||
client = new PwClient(testAppCode, testAuthToken); | ||
client.registerDevice(mockOptions, function (err, response) { | ||
t.deepEqual(err, new Error('Device hwid must be provided'), 'Error as expected'); | ||
t.notOk(response, 'No response as expected'); | ||
}); | ||
}); | ||
test('Pushwoosh register token error case with no device type', function (t) { | ||
t.plan(2); | ||
var mockDevice = 'someToken', | ||
mockHwid = 'someHwid', | ||
mockOptions = { | ||
push_token: mockDevice, | ||
hwid: mockHwid | ||
}; | ||
var PwClient = getCleanTestObject(), | ||
client = new PwClient(testAppCode, testAuthToken); | ||
client.registerDevice(mockOptions, function (err, response) { | ||
t.deepEqual(err, new Error('Device type must be provided'), 'Error as expected'); | ||
t.notOk(response, 'No response as expected'); | ||
}); | ||
}); | ||
test('Pushwoosh register token success case with response code 200 but status code 210', function (t) { | ||
t.plan(5); | ||
var mockDevice = 'someToken', | ||
mockHwid = 'someHwid', | ||
mockDeviceType = 3, | ||
mockOptions = { | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType | ||
}, | ||
mockResponse = { | ||
statusCode: 200 | ||
}, | ||
mockBodyResponse = { | ||
status_code: 210, | ||
response: {}, | ||
status_message: testError | ||
}, | ||
expectedBody = { | ||
request: { | ||
application: testAppCode, | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType | ||
} | ||
}, | ||
expectedResult = {description: 'Argument error', detail: mockBodyResponse.status_message, code: 210}; | ||
fraudster.registerMock('request', function (params, callback) { | ||
t.ok(params, 'Params exists'); | ||
t.deepEqual(params.body, expectedBody, 'Body are as expected'); | ||
t.equal(params.method, 'POST', 'Method is POST as expected'); | ||
callback(null, mockResponse, mockBodyResponse); | ||
}); | ||
var PwClient = getCleanTestObject(), | ||
client = new PwClient(testAppCode, testAuthToken); | ||
client.registerDevice(mockOptions, function (err, response) { | ||
t.notOk(err, 'No error as expected'); | ||
t.deepEqual(response, expectedResult, 'response is the same'); | ||
}); | ||
}); | ||
test('Pushwoosh register token success case with response code 400', function (t) { | ||
t.plan(5); | ||
var mockDevice = 'someToken', | ||
mockHwid = 'someHwid', | ||
mockDeviceType = 3, | ||
mockOptions = { | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType | ||
}, | ||
mockResponse = { | ||
statusCode: 400 | ||
}, | ||
mockBodyResponse = { | ||
response: {} | ||
}, | ||
expectedBody = { | ||
request: { | ||
application: testAppCode, | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType | ||
} | ||
}, | ||
expectedResult = {description: 'Argument error', detail: mockBodyResponse.status_message, code: 210}; | ||
fraudster.registerMock('request', function (params, callback) { | ||
t.ok(params, 'Params exists'); | ||
t.deepEqual(params.body, expectedBody, 'Body are as expected'); | ||
t.equal(params.method, 'POST', 'Method is POST as expected'); | ||
callback(null, mockResponse, mockBodyResponse); | ||
}); | ||
var PwClient = getCleanTestObject(), | ||
client = new PwClient(testAppCode, testAuthToken); | ||
client.registerDevice(mockOptions, function (err, response) { | ||
t.deepEqual(err, new errors.Malformed(), 'Got Malformed Error!'); | ||
t.notOk(response, 'No response as expected'); | ||
}); | ||
}); | ||
test('Pushwoosh register token success case with response code 400', function (t) { | ||
t.plan(5); | ||
var mockDevice = 'someToken', | ||
mockHwid = 'someHwid', | ||
mockDeviceType = 3, | ||
mockOptions = { | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType | ||
}, | ||
mockResponse = { | ||
statusCode: 500 | ||
}, | ||
mockBodyResponse = { | ||
status_code: 500, | ||
response: {} | ||
}, | ||
expectedBody = { | ||
request: { | ||
application: testAppCode, | ||
push_token: mockDevice, | ||
hwid: mockHwid, | ||
device_type: mockDeviceType | ||
} | ||
}, | ||
expectedResult = {description: 'Argument error', detail: mockBodyResponse.status_message, code: 210}; | ||
fraudster.registerMock('request', function (params, callback) { | ||
t.ok(params, 'Params exists'); | ||
t.deepEqual(params.body, expectedBody, 'Body are as expected'); | ||
t.equal(params.method, 'POST', 'Method is POST as expected'); | ||
callback(null, mockResponse, mockBodyResponse); | ||
}); | ||
var PwClient = getCleanTestObject(), | ||
client = new PwClient(testAppCode, testAuthToken); | ||
client.registerDevice(mockOptions, function (err, response) { | ||
t.deepEqual(err, new errors.Internal(), 'Got Internal Error!'); | ||
t.notOk(response, 'No response as expected'); | ||
}); | ||
}); |
47252
1098
124