Unverified Commit 5ddb1de0 authored by zeynel's avatar zeynel Committed by GitHub

Merge pull request #4 from Bsociety/master

Add behavior tests for client and notification
parents 8402ed4e 28f21f05
root = true
[*.js]
indent_style = space
end_of_line = lf
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
...@@ -19,7 +19,8 @@ A Node.js client library for [OneSignal](https://onesignal.com/) API. ...@@ -19,7 +19,8 @@ A Node.js client library for [OneSignal](https://onesignal.com/) API.
* [Editing a device](#editing-a-device) * [Editing a device](#editing-a-device)
* [CSV Export](#csv-export) * [CSV Export](#csv-export)
* [Opening track](#opening-track) * [Opening track](#opening-track)
* [Tests](#tests)
## Installation ## Installation
``` ```
...@@ -194,9 +195,9 @@ var firstNotification = new OneSignal.Notification({ ...@@ -194,9 +195,9 @@ var firstNotification = new OneSignal.Notification({
} }
}); });
firstNotification.setTargetDevices(["1dd608f2-c6a1-11e3-851d-000c2940e62c", firstNotification.setTargetDevices(["1dd608f2-c6a1-11e3-851d-000c2940e62c",
"2dd608f2-c6a1-11e3-851d-000c2940e62c"]); "2dd608f2-c6a1-11e3-851d-000c2940e62c"]);
myClient.sendNotification(firstNotification, function (err, httpResponse,data) { myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
if (err) { if (err) {
console.log('Something went wrong...'); console.log('Something went wrong...');
...@@ -222,7 +223,7 @@ You can cancel a notification simply by calling `.cancel(notificationId, callbac ...@@ -222,7 +223,7 @@ You can cancel a notification simply by calling `.cancel(notificationId, callbac
// this will cancel the notification for current app (myClient.app) // this will cancel the notification for current app (myClient.app)
myClient.cancelNotification('notificationId', function (err, httpResponse, data) { myClient.cancelNotification('notificationId', function (err, httpResponse, data) {
if (err) { if (err) {
} }
}) })
``` ```
...@@ -230,7 +231,7 @@ myClient.cancelNotification('notificationId', function (err, httpResponse, data) ...@@ -230,7 +231,7 @@ myClient.cancelNotification('notificationId', function (err, httpResponse, data)
### Viewing push notifications ### Viewing push notifications
To view all push notifications for an app: To view all push notifications for an app:
``` js ``` js
var myClient = new OneSignal.Client({ var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX', userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' } app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
...@@ -383,7 +384,7 @@ myClient.csvExport({ extra_fields: ['location'] }, function (err, httpResponse, ...@@ -383,7 +384,7 @@ myClient.csvExport({ extra_fields: ['location'] }, function (err, httpResponse,
}); });
``` ```
## Opening track ### Opening track
``` js ``` js
var myClient = new OneSignal.Client({ var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX', userAuthKey: 'XXXXXX',
...@@ -395,6 +396,13 @@ myClient.trackOpen('notificationId', { opened: true }, function (err, httpRespon ...@@ -395,6 +396,13 @@ myClient.trackOpen('notificationId', { opened: true }, function (err, httpRespon
}); });
``` ```
## Tests
Running all tests:
```bash
$ npm test
```
## License ## License
This project is under the MIT license. This project is under the MIT license.
\ No newline at end of file
...@@ -4,45 +4,46 @@ var request = require('request'); ...@@ -4,45 +4,46 @@ var request = require('request');
var constants = require('./constants'); var constants = require('./constants');
var ALLOWED_CREDENTIALS = [ var ALLOWED_CREDENTIALS = [
{ name: 'userAuthKey', type: 'string' }, { name: 'userAuthKey', type: 'string' },
{ name: 'app', type: 'object', requiredFields: ['appAuthKey', 'appId'] }, { name: 'app', type: 'object', requiredFields: ['appAuthKey', 'appId'] },
{ name: 'apps', type: 'object'} { name: 'apps', type: 'object'}
]; ];
/** /**
* make a basic request * make a basic request
* @param url * @param url
* @param apiKey * @param apiKey
* @param method : [ GET, POSt, PUT ...] * @param method : [ GET, POST, PUT ...]
* @param body * @param body
* @param callback (err, httpResponse, body) * @param callback (err, httpResponse, body)
*/ */
var basicRequest = function (url, apiKey, method, body, callback) { var basicRequest = function (url, apiKey, method, body, callback) {
var options = { var options = {
url: url, url: url,
method: method method: method
}; };
if (apiKey) { if (apiKey) {
options.headers = { options.headers = {
'Content-Type': 'application/json; charset=utf-8', 'Content-Type': 'application/json; charset=utf-8',
'Authorization': 'Basic ' + apiKey 'Authorization': 'Basic ' + apiKey
} }
} }
if (body) { if (body) {
options.body = body; options.body = body;
options.json = true; options.json = true;
} }
return new Promise(function (resolve, reject) {
request(options, function (err, httpResponse, data) { return new Promise(function (resolve, reject) {
if (err) { request(options, function (err, httpResponse, data) {
callback && callback(err, httpResponse, data); if (err) {
reject(err); callback && callback(err, httpResponse, data);
} else { return reject(err);
callback && callback(err, httpResponse, data); }
resolve({ httpResponse: httpResponse, data: data });
} callback && callback(err, httpResponse, data);
}); return resolve({ httpResponse: httpResponse, data: data });
}); });
});
}; };
/** /**
...@@ -52,51 +53,51 @@ var basicRequest = function (url, apiKey, method, body, callback) { ...@@ -52,51 +53,51 @@ var basicRequest = function (url, apiKey, method, body, callback) {
* @returns {boolean} * @returns {boolean}
*/ */
var checkCredential = function (credentialName, credential) { var checkCredential = function (credentialName, credential) {
for (var i = 0; i < ALLOWED_CREDENTIALS.length; i++) { for (var i = 0; i < ALLOWED_CREDENTIALS.length; i++) {
if (ALLOWED_CREDENTIALS[i].name === credentialName) { if (ALLOWED_CREDENTIALS[i].name === credentialName) {
if (typeof credential !== ALLOWED_CREDENTIALS[i].type) { if (typeof credential !== ALLOWED_CREDENTIALS[i].type) {
throw credentialName + ' must be a ' + ALLOWED_CREDENTIALS[i].type; throw credentialName + ' must be a ' + ALLOWED_CREDENTIALS[i].type;
} }
if (ALLOWED_CREDENTIALS[i].requiredFields) { if (ALLOWED_CREDENTIALS[i].requiredFields) {
for (var j = 0; j < ALLOWED_CREDENTIALS[i].requiredFields.length; j++) { for (var j = 0; j < ALLOWED_CREDENTIALS[i].requiredFields.length; j++) {
if (!(ALLOWED_CREDENTIALS[i].requiredFields[j] in credential)) { if (!(ALLOWED_CREDENTIALS[i].requiredFields[j] in credential)) {
throw credentialName + ' must contain ' + ALLOWED_CREDENTIALS[i].requiredFields[j] throw credentialName + ' must contain ' + ALLOWED_CREDENTIALS[i].requiredFields[j]
} }
} }
} }
return true; return true;
} }
} }
return false; return false;
}; };
/** /**
* *
* @param credentials { JSON } * @param credentials { JSON }
* @constructor * @constructor
*/ */
var Client = function (credentials) { var Client = function (credentials) {
if (typeof credentials !== 'object') { if (typeof credentials !== 'object') {
throw 'credentials parameter must be a JSON object' throw 'credentials parameter must be a JSON object'
} }
this.API_URI = constants.API_ROOT; this.API_URI = constants.API_ROOT;
for (var key in credentials) { for (var key in credentials) {
if (credentials.hasOwnProperty(key) && checkCredential(key, credentials[key])) { if (credentials.hasOwnProperty(key) && checkCredential(key, credentials[key])) {
this[key] = credentials[key]; this[key] = credentials[key];
} }
} }
}; };
/** /**
* *
* @param rootUrl { String } default 'https://onesignal.com/api/v1'; * @param rootUrl { String } default 'https://onesignal.com/api/v1';
*/ */
Client.prototype.setRootUrl = function (rootUrl) { Client.prototype.setRootUrl = function (rootUrl) {
if (!rootUrl) { if (!rootUrl) {
throw 'You must set a valid rootUsrl.' throw 'You must set a valid rootUsrl.'
} }
this.API_URI = rootUrl; this.API_URI = rootUrl;
}; };
/** /**
...@@ -104,8 +105,8 @@ Client.prototype.setRootUrl = function (rootUrl) { ...@@ -104,8 +105,8 @@ Client.prototype.setRootUrl = function (rootUrl) {
* @param app { JSON } {} * @param app { JSON } {}
*/ */
Client.prototype.setApp = function (app) { Client.prototype.setApp = function (app) {
checkCredential('app', app); checkCredential('app', app);
this.app = app; this.app = app;
}; };
/** /**
...@@ -114,20 +115,19 @@ Client.prototype.setApp = function (app) { ...@@ -114,20 +115,19 @@ Client.prototype.setApp = function (app) {
* @param callback * @param callback
*/ */
Client.prototype.sendNotification = function (notification, callback) { Client.prototype.sendNotification = function (notification, callback) {
if (!notification || !notification.postBody) { if (!notification || !notification.postBody) {
throw 'notification parameter must be a typeof Notification object.'; throw 'notification parameter must be a typeof Notification object.';
} }
var postBody = notification.postBody; var postBody = notification.postBody;
if (this.apps && this.apps.length > 0) { if (this.apps && this.apps.length > 0) {
postBody.app_ids = this.apps; postBody.app_ids = this.apps;
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.userAuthKey, 'POST', postBody, callback); return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.userAuthKey, 'POST', postBody, callback);
} else if (this.app) { }
postBody.app_id = this.app.appId; if (this.app) {
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.app.appAuthKey, 'POST', postBody, callback); postBody.app_id = this.app.appId;
} else { return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.app.appAuthKey, 'POST', postBody, callback);
throw 'You must set either an "app" or "apps" on Client'; }
} throw 'You must set either an "app" or "apps" on Client';
}; };
/** /**
...@@ -136,76 +136,76 @@ Client.prototype.sendNotification = function (notification, callback) { ...@@ -136,76 +136,76 @@ Client.prototype.sendNotification = function (notification, callback) {
* @param callback * @param callback
*/ */
Client.prototype.cancelNotification = function (notificationId, callback) { Client.prototype.cancelNotification = function (notificationId, callback) {
if (!this.app) { if (!this.app) {
throw 'You must define an "app" object.' throw 'You must define an "app" object.'
} }
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId; var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId;
return basicRequest(notificationUri, this.app.appAuthKey, 'DELETE', null, callback); return basicRequest(notificationUri, this.app.appAuthKey, 'DELETE', null, callback);
}; };
/** /**
* *
* @param notificationId { String } * @param notificationId { String }
* @param callback * @param callback
*/ */
Client.prototype.viewNotification = function (notificationId, callback) { Client.prototype.viewNotification = function (notificationId, callback) {
if (!this.app) { if (!this.app) {
throw 'You must define an "app" object.' throw 'You must define an "app" object.'
} }
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId; var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId;
return basicRequest(notificationUri, this.app.appAuthKey, 'GET', null, callback); return basicRequest(notificationUri, this.app.appAuthKey, 'GET', null, callback);
}; };
/** /**
* *
* @param query { String } ex: limit:100&offset=9 * @param query { String } ex: limit:100&offset=9
* @param callback * @param callback
*/ */
Client.prototype.viewNotifications = function (query, callback) { Client.prototype.viewNotifications = function (query, callback) {
if (!this.app) { if (!this.app) {
throw 'You must define an "app" object.' throw 'You must define an "app" object.'
} }
var appUri = this.API_URI + constants.NOTIFICATIONS_PATH + '?app_id=' + this.app.appId + '&' + query; var appUri = this.API_URI + constants.NOTIFICATIONS_PATH + '?app_id=' + this.app.appId + '&' + query;
return basicRequest(appUri, this.app.appAuthKey, 'GET', null, callback); return basicRequest(appUri, this.app.appAuthKey, 'GET', null, callback);
}; };
/** /**
* *
* @param callback * @param callback
*/ */
Client.prototype.viewApps = function (callback) { Client.prototype.viewApps = function (callback) {
if (!this.userAuthKey) { if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client' throw 'You must define "userAuthKey" on Client'
} }
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'GET', null, callback); return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'GET', null, callback);
}; };
/** /**
* *
* @param appId { String } * @param appId { String }
* @param callback * @param callback
*/ */
Client.prototype.viewApp = function (appId, callback) { Client.prototype.viewApp = function (appId, callback) {
if (!this.userAuthKey) { if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client' throw 'You must define "userAuthKey" on Client'
} }
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + appId, this.userAuthKey, 'GET', null, callback); return basicRequest(this.API_URI + constants.APPS_PATH + '/' + appId, this.userAuthKey, 'GET', null, callback);
}; };
/** /**
* *
* @param body { JSON } * @param body { JSON }
* @param callback * @param callback
*/ */
Client.prototype.createApp = function (body, callback) { Client.prototype.createApp = function (body, callback) {
if (!body.name) { if (!body.name) {
throw 'You must specify a name in body'; throw 'You must specify a name in body';
} }
if (!this.userAuthKey) { if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client' throw 'You must define "userAuthKey" on Client'
} }
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'POST', body, callback); return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'POST', body, callback);
}; };
/** /**
...@@ -214,56 +214,56 @@ Client.prototype.createApp = function (body, callback) { ...@@ -214,56 +214,56 @@ Client.prototype.createApp = function (body, callback) {
* @param callback * @param callback
*/ */
Client.prototype.updateApp = function (body, callback) { Client.prototype.updateApp = function (body, callback) {
if (!this.app) { if (!this.app) {
throw 'You must define an "app" object.' throw 'You must define an "app" object.'
} }
if (!this.userAuthKey) { if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client' throw 'You must define "userAuthKey" on Client'
} }
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + this.app.appId, this.userAuthKey, 'PUT', body, callback); return basicRequest(this.API_URI + constants.APPS_PATH + '/' + this.app.appId, this.userAuthKey, 'PUT', body, callback);
}; };
/** /**
* *
* @param query { String } ex: limit=200&offset=10 * @param query { String } ex: limit=200&offset=10
* @param callback * @param callback
*/ */
Client.prototype.viewDevices = function (query, callback) { Client.prototype.viewDevices = function (query, callback) {
if (!this.app) { if (!this.app) {
throw 'You must define an "app" object.' throw 'You must define an "app" object.'
} }
var viewUri = this.API_URI + constants.DEVICES_PATH + '?app_id=' + this.app.appId + '&' + query; var viewUri = this.API_URI + constants.DEVICES_PATH + '?app_id=' + this.app.appId + '&' + query;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback); return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
}; };
/** /**
* *
* @param deviceId { String } * @param deviceId { String }
* @param callback * @param callback
*/ */
Client.prototype.viewDevice = function (deviceId, callback) { Client.prototype.viewDevice = function (deviceId, callback) {
if (!this.app) { if (!this.app) {
throw 'You must define an "app" object.' throw 'You must define an "app" object.'
} }
var viewUri = this.API_URI + constants.DEVICES_PATH + '/' + deviceId + '?app_id=' + this.app.appId; var viewUri = this.API_URI + constants.DEVICES_PATH + '/' + deviceId + '?app_id=' + this.app.appId;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback); return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
}; };
/** /**
* *
* @param body { JSON } * @param body { JSON }
* @param callback * @param callback
*/ */
Client.prototype.addDevice = function (body, callback) { Client.prototype.addDevice = function (body, callback) {
if (!this.app) { if (!this.app) {
throw 'You must define an "app" object.' throw 'You must define an "app" object.'
} }
if (!('app_id' in body)) { if (!('app_id' in body)) {
body.app_id = this.app.appId; body.app_id = this.app.appId;
} }
return basicRequest(this.API_URI + constants.DEVICES_PATH, this.app.appAuthKey, 'POST', body, callback); return basicRequest(this.API_URI + constants.DEVICES_PATH, this.app.appAuthKey, 'POST', body, callback);
}; };
/** /**
...@@ -273,10 +273,10 @@ Client.prototype.addDevice = function (body, callback) { ...@@ -273,10 +273,10 @@ Client.prototype.addDevice = function (body, callback) {
* @param callback * @param callback
*/ */
Client.prototype.editDevice = function (deviceId, body, callback) { Client.prototype.editDevice = function (deviceId, body, callback) {
if (!this.app) { if (!this.app) {
throw 'You must define an "app" object.' throw 'You must define an "app" object.'
} }
return basicRequest(this.API_URI + constants.DEVICES_PATH + '/' + deviceId, this.app.appAuthKey, 'PUT', body, callback); return basicRequest(this.API_URI + constants.DEVICES_PATH + '/' + deviceId, this.app.appAuthKey, 'PUT', body, callback);
}; };
/** /**
...@@ -286,13 +286,13 @@ Client.prototype.editDevice = function (deviceId, body, callback) { ...@@ -286,13 +286,13 @@ Client.prototype.editDevice = function (deviceId, body, callback) {
* @param callback * @param callback
*/ */
Client.prototype.trackOpen = function (notificationId, body, callback) { Client.prototype.trackOpen = function (notificationId, body, callback) {
if (!this.app) { if (!this.app) {
throw 'You must define an "app" object.' throw 'You must define an "app" object.'
} }
if (!('app_id' in body)) { if (!('app_id' in body)) {
body.app_id = this.app.appId; body.app_id = this.app.appId;
} }
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId, this.app.appAuthKey, 'PUT', body, callback); return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId, this.app.appAuthKey, 'PUT', body, callback);
}; };
/** /**
...@@ -301,12 +301,11 @@ Client.prototype.trackOpen = function (notificationId, body, callback) { ...@@ -301,12 +301,11 @@ Client.prototype.trackOpen = function (notificationId, body, callback) {
* @param callback * @param callback
*/ */
Client.prototype.csvExport = function (body, callback) { Client.prototype.csvExport = function (body, callback) {
if (!this.app) { if (!this.app) {
throw 'You must define an "app" object.' throw 'You must define an "app" object.'
} }
var csvUri = this.API_URI + constants.DEVICES_PATH + '/csv_export' + '?app_id=' + this.app.appId; var csvUri = this.API_URI + constants.DEVICES_PATH + '/csv_export' + '?app_id=' + this.app.appId;
return basicRequest(csvUri, this.app.appAuthKey, 'POST', body, callback); return basicRequest(csvUri, this.app.appAuthKey, 'POST', body, callback);
}; };
module.exports = Client; module.exports = Client;
var Constants = { var Constants = {
API_ROOT: 'https://onesignal.com/api/v1', API_ROOT: 'https://onesignal.com/api/v1',
/** PATHS **/ /** PATHS **/
NOTIFICATIONS_PATH: '/notifications', NOTIFICATIONS_PATH: '/notifications',
APPS_PATH: '/apps', APPS_PATH: '/apps',
......
...@@ -12,24 +12,31 @@ var ALLOWED_FIELDS = ['contents', 'included_segments', 'excluded_segments', 'fil ...@@ -12,24 +12,31 @@ var ALLOWED_FIELDS = ['contents', 'included_segments', 'excluded_segments', 'fil
/** /**
* *
* @param initialBody The body must include either one of these: contents, content_available, template_id * @param initialBody The body must include either one of these: contents, content_available, template_id
* @constructor * @constructor
*/ */
var Notification = function (initialBody) { var Notification = function (initialBody) {
if (typeof initialBody !== 'object') { if (typeof initialBody !== 'object') {
throw 'Body must be a JSON object'; throw 'Body must be a JSON object';
} }
this.postBody = {}; this.postBody = {};
if ('contents' in initialBody) { if ('contents' in initialBody) {
this.postBody.contents = initialBody.contents; this.postBody.contents = initialBody.contents;
} else if ('content_available' in initialBody){ return;
}
if ('content_available' in initialBody) {
this.postBody.content_available = initialBody.content_available; this.postBody.content_available = initialBody.content_available;
} else if ('template_id' in initialBody) { return;
}
if ('template_id' in initialBody) {
this.postBody.template_id = initialBody.template_id; this.postBody.template_id = initialBody.template_id;
} else { return;
throw 'Body must include one of the following fields: contents, content_available, template_id' }
} throw 'Body must include one of the following fields: contents, content_available, template_id'
}; };
/** /**
...@@ -48,15 +55,15 @@ Notification.prototype.setParameter = function (name, value) { ...@@ -48,15 +55,15 @@ Notification.prototype.setParameter = function (name, value) {
}; };
/** /**
* *
* @param contents * @param contents
*/ */
Notification.prototype.setContent = function (contents) { Notification.prototype.setContent = function (contents) {
this.postBody.contents = contents; this.postBody.contents = contents;
}; };
/** /**
* *
* @param included_segments The segment names you want to target * @param included_segments The segment names you want to target
*/ */
Notification.prototype.setIncludedSegments = function (included_segments) { Notification.prototype.setIncludedSegments = function (included_segments) {
...@@ -64,23 +71,23 @@ Notification.prototype.setIncludedSegments = function (included_segments) { ...@@ -64,23 +71,23 @@ Notification.prototype.setIncludedSegments = function (included_segments) {
}; };
/** /**
* *
* @param excluded_segments Segment that will be excluded when sending * @param excluded_segments Segment that will be excluded when sending
*/ */
Notification.prototype.setExcludedSegments = function (excluded_segments) { Notification.prototype.setExcludedSegments = function (excluded_segments) {
this.postBody.excluded_segments = excluded_segments; this.postBody.excluded_segments = excluded_segments;
}; };
/** /**
* *
* @param filters * @param filters
*/ */
Notification.prototype.setFilters = function (filters) { Notification.prototype.setFilters = function (filters) {
this.postBody.filters = filters; this.postBody.filters = filters;
}; };
/** /**
* *
* @param include_player_ids Specific players to send your notification to * @param include_player_ids Specific players to send your notification to
*/ */
Notification.prototype.setTargetDevices = function (include_player_ids) { Notification.prototype.setTargetDevices = function (include_player_ids) {
......
'use strict';
var expect = require('chai').expect;
var OneSignal = require('../lib');
var ClientMock = require('./mocks/client');
var NotificationMock = require('./mocks/notification');
var Constants = require('../lib/constants');
describe('Client Tests', function () {
describe('Create Client', function () {
it('Expect to throw an error with non object data when creating a client', function () {
var client = ClientMock.invalidClientWithEmptyData;
try {
var response = new OneSignal.Client(client);
expect(response).to.equal(undefined);
} catch (err) {
expect(err).to.be.an('string');
expect(err).to.equal('credentials parameter must be a JSON object');
}
})
it('Expect to throw an error with invalid userAuthKey format', function () {
var client = ClientMock.invalidUserAuthKey;
try {
var response = new OneSignal.Client(client);
expect(response).to.be.equal(undefined);
} catch (err) {
expect(err).to.be.an('string');
expect(err).to.equal('userAuthKey must be a string');
}
})
it('Expect to throw an error without app object', function () {
var client = ClientMock.invalidAppProperty;
try {
var response = new OneSignal.Client(client);
expect(response).to.equal(undefined);
} catch (err) {
expect(err).to.be.an('object');
}
})
it('Expect to throw an error without appAuthKey property on app object', function () {
var client = ClientMock.invalidAppWithouAuthProperty;
try {
var response = new OneSignal.Client(client);
expect(response).to.equal(undefined);
} catch (err) {
expect(err).to.be.an('string');
expect(err).to.equal('app must contain appAuthKey');
}
})
it('Expect to throw an error without appId property on app object', function () {
var client = ClientMock.invalidAppWithoutIdProperty;
try {
var response = new OneSignal.Client(client);
expect(response).to.equal(undefined);
} catch (err) {
expect(err).to.be.an('string');
expect(err).to.equal('app must contain appId');
}
})
it('Expect to valid a JSON object to create a client', function () {
var client = ClientMock.validClient;
var response = new OneSignal.Client(client);
expect(response).to.be.an('object');
expect(response.API_URI).to.be.an('string');
expect(response.API_URI).to.equal(Constants.API_ROOT);
expect(response.userAuthKey).to.equal(client.userAuthKey);
expect(response.app).to.be.an('object');
expect(response.app.appAuthKey).to.equal(client.app.appAuthKey);
expect(response.app.appId).to.equal(client.app.appId);
})
it('Expect to create empty client', function () {
var client = ClientMock.validEmptyClient;
var response = new OneSignal.Client(client);
expect(response).to.be.an('object');
expect(response.API_URI).to.equal(Constants.API_ROOT);
})
it('Expect to set userAuthKey and app object for empty client', function () {
var client = ClientMock.validEmptyClient;
var response = new OneSignal.Client(client);
response.userAuthKey = ClientMock.validUserAuth;
response.setApp(ClientMock.validSetApp);
expect(response).to.be.an('object');
expect(response.API_URI).to.be.an('string');
expect(response.API_URI).to.equal(Constants.API_ROOT);
expect(response.userAuthKey).to.equal(ClientMock.validUserAuth);
expect(response.app).to.be.an('object');
expect(response.app.appAuthKey).to.equal(ClientMock.validSetApp.appAuthKey);
expect(response.app.appId).to.equal(ClientMock.validSetApp.appId);
})
})
describe('Send Notification', function () {
it('Expect to throw an error when sending a notification withou a notification object', function () {
var client = ClientMock.validClient;
var clientObject = new OneSignal.Client(client);
var notification = NotificationMock.emptyNotification;
try {
var response = clientObject.sendNotification(notification);
expect(response).to.equal(undefined);
} catch (err) {
expect(err).to.be.an('string');
expect(err).to.equal('notification parameter must be a typeof Notification object.');
}
})
it('Expect to throw an error when sending a notification for client without app', function () {
var client = ClientMock.validEmptyClient;
var clientObject = new OneSignal.Client(client);
var notification = NotificationMock.validWithContents;
var notificationObject = new OneSignal.Notification(notification);
try {
var response = clientObject.sendNotification(notificationObject);
expect(response).to.equal(undefined);
} catch (err) {
expect(err).to.be.an('string');
expect(err).to.equal('You must set either an "app" or "apps" on Client');
}
})
})
})
module.exports = {
invalidClientWithNonObjectData: '',
invalidUserAuthKey: {
userAuthKey: 101010
},
invalidAppProperty: {
userAuthKey: 'XXXX',
other: {}
},
invalidAppWithouAuthProperty: {
userAuthKey: 'XXXXX',
app: {
otherAuthKey: 'XXXX',
appId: 'XXXX'
}
},
invalidAppWithoutIdProperty: {
userAuthKey: 'XXXXX',
app: {
appAuthKey: 'XXXX',
otherID: 'XXXX'
}
},
validEmptyClient: {},
validUserAuth: 'XXXX',
validSetApp: {
appAuthKey: 'XXXX',
appId: 'XXXX'
},
validClient: {
userAuthKey: 'XXXXXX',
app: {
appAuthKey: 'XXXXX',
appId: 'XXXXX'
}
}
}
module.exports = {
emptyNotification: '',
invalidNotification: {
other: 'xxxx'
},
validWithContents: {
contents: {
en: 'Test english',
pt: 'Test portuguese'
}
},
validWithContentAvailable: {
content_available: true
},
validWithTemplateId: {
template_id: 'test'
},
invalidParameter: {
name: 'other',
value: 'other'
},
validParameter: {
name: 'filters',
value: 'test'
}
}
'use strict';
var expect = require('chai').expect;
var OneSignal = require('../lib');
var NotificationMock = require('./mocks/notification');
var Constants = require('../lib/constants');
describe('Notification Tests', function () {
describe('Create Notification', function () {
it('Expect to throw an error with non object data when creating a notification', function () {
var notification = NotificationMock.emptyNotification;
try {
var response = new OneSignal.Notification(notification);
expect(response).to.equal(undefined);
} catch (err) {
expect(err).to.be.an('string');
expect(err).to.equal('Body must be a JSON object');
}
})
it('Expect to throw an error with object without one of required fields on notification', function () {
var notification = NotificationMock.invalidNotification;
try {
var response = new OneSignal.Notification(notification);
expect(response).to.equal(undefined);
} catch (err) {
expect(err).to.be.an('string');
expect(err).to.equal(
'Body must include one of the following fields: contents, content_available, template_id');
}
})
it('Expect to valid notification with contents', function () {
var notification = NotificationMock.validWithContents;
var response = new OneSignal.Notification(notification);
expect(response).to.be.an('object');
expect(response.postBody).to.be.an('object');
expect(response.postBody.contents).to.be.an('object');
expect(response.postBody.contents).to.have.property('en');
expect(response.postBody.contents).to.have.property('pt');
})
it('Expect to valid notification with content available', function () {
var notification = NotificationMock.validWithContentAvailable;
var response = new OneSignal.Notification(notification);
expect(response).to.be.an('object');
expect(response.postBody).to.be.an('object');
expect(response.postBody.content_available).to.be.an('boolean');
expect(response.postBody.content_available)
.to.equal(notification.content_available);
})
it('Expect to valid notification with template ID', function () {
var notification = NotificationMock.validWithTemplateId;
var response = new OneSignal.Notification(notification);
expect(response).to.be.an('object');
expect(response.postBody).to.be.an('object');
expect(response.postBody.template_id).to.be.an('string');
expect(response.postBody.template_id).to.equal(notification.template_id);
})
})
describe('Setting OneSignal Properties', function () {
it('Expect to throw an error when setting invalid parameter', function () {
var notification = NotificationMock.validWithContents;
var notificationObject = new OneSignal.Notification(notification);
try {
var parameter = NotificationMock.invalidParameter;
notificationObject.setParameter(parameter.name, parameter.value);
expect(response).to.equal(undefined);
} catch (err) {
expect(err).to.be.an('string');
expect(err).to.equal(
'"other" is not present in documentation. You should add a exclamation'
.concat(' mark to the begging of the name, if you want to set it : !other'));
}
})
it('Expect to valid data when setting parameter', function () {
var notification = NotificationMock.validWithContents;
var notificationObject = new OneSignal.Notification(notification);
var parameter = NotificationMock.validParameter;
notificationObject.setParameter(parameter.name, parameter.value);
var response = notificationObject;
expect(response).to.be.an('object');
expect(response.postBody).to.be.an('object');
expect(response.postBody.filters).to.be.an('string');
expect(response.postBody.filters).to.equal(parameter.value);
})
})
})
'use strict';
var expect = require('chai').expect;
var OneSignal = require('../lib');
\ No newline at end of file
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