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.
* [Editing a device](#editing-a-device)
* [CSV Export](#csv-export)
* [Opening track](#opening-track)
* [Tests](#tests)
## Installation
```
......@@ -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"]);
myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
if (err) {
console.log('Something went wrong...');
......@@ -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)
myClient.cancelNotification('notificationId', function (err, httpResponse, data) {
if (err) {
}
})
```
......@@ -230,7 +231,7 @@ myClient.cancelNotification('notificationId', function (err, httpResponse, data)
### Viewing push notifications
To view all push notifications for an app:
``` js
``` js
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
......@@ -383,7 +384,7 @@ myClient.csvExport({ extra_fields: ['location'] }, function (err, httpResponse,
});
```
## Opening track
### Opening track
``` js
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
......@@ -395,6 +396,13 @@ myClient.trackOpen('notificationId', { opened: true }, function (err, httpRespon
});
```
## Tests
Running all tests:
```bash
$ npm test
```
## License
This project is under the MIT license.
\ No newline at end of file
This project is under the MIT license.
......@@ -4,45 +4,46 @@ var request = require('request');
var constants = require('./constants');
var ALLOWED_CREDENTIALS = [
{ name: 'userAuthKey', type: 'string' },
{ name: 'app', type: 'object', requiredFields: ['appAuthKey', 'appId'] },
{ name: 'apps', type: 'object'}
{ name: 'userAuthKey', type: 'string' },
{ name: 'app', type: 'object', requiredFields: ['appAuthKey', 'appId'] },
{ name: 'apps', type: 'object'}
];
/**
* make a basic request
* @param url
* @param apiKey
* @param method : [ GET, POSt, PUT ...]
* @param method : [ GET, POST, PUT ...]
* @param body
* @param callback (err, httpResponse, body)
*/
var basicRequest = function (url, apiKey, method, body, callback) {
var options = {
url: url,
method: method
};
if (apiKey) {
options.headers = {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': 'Basic ' + apiKey
}
}
if (body) {
options.body = body;
options.json = true;
}
return new Promise(function (resolve, reject) {
request(options, function (err, httpResponse, data) {
if (err) {
callback && callback(err, httpResponse, data);
reject(err);
} else {
callback && callback(err, httpResponse, data);
resolve({ httpResponse: httpResponse, data: data });
}
});
var options = {
url: url,
method: method
};
if (apiKey) {
options.headers = {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': 'Basic ' + apiKey
}
}
if (body) {
options.body = body;
options.json = true;
}
return new Promise(function (resolve, reject) {
request(options, function (err, httpResponse, data) {
if (err) {
callback && callback(err, httpResponse, data);
return reject(err);
}
callback && callback(err, httpResponse, data);
return resolve({ httpResponse: httpResponse, data: data });
});
});
};
/**
......@@ -52,51 +53,51 @@ var basicRequest = function (url, apiKey, method, body, callback) {
* @returns {boolean}
*/
var checkCredential = function (credentialName, credential) {
for (var i = 0; i < ALLOWED_CREDENTIALS.length; i++) {
if (ALLOWED_CREDENTIALS[i].name === credentialName) {
if (typeof credential !== ALLOWED_CREDENTIALS[i].type) {
throw credentialName + ' must be a ' + ALLOWED_CREDENTIALS[i].type;
}
if (ALLOWED_CREDENTIALS[i].requiredFields) {
for (var j = 0; j < ALLOWED_CREDENTIALS[i].requiredFields.length; j++) {
if (!(ALLOWED_CREDENTIALS[i].requiredFields[j] in credential)) {
throw credentialName + ' must contain ' + ALLOWED_CREDENTIALS[i].requiredFields[j]
}
}
}
return true;
}
}
return false;
for (var i = 0; i < ALLOWED_CREDENTIALS.length; i++) {
if (ALLOWED_CREDENTIALS[i].name === credentialName) {
if (typeof credential !== ALLOWED_CREDENTIALS[i].type) {
throw credentialName + ' must be a ' + ALLOWED_CREDENTIALS[i].type;
}
if (ALLOWED_CREDENTIALS[i].requiredFields) {
for (var j = 0; j < ALLOWED_CREDENTIALS[i].requiredFields.length; j++) {
if (!(ALLOWED_CREDENTIALS[i].requiredFields[j] in credential)) {
throw credentialName + ' must contain ' + ALLOWED_CREDENTIALS[i].requiredFields[j]
}
}
}
return true;
}
}
return false;
};
/**
*
*
* @param credentials { JSON }
* @constructor
*/
var Client = function (credentials) {
if (typeof credentials !== 'object') {
throw 'credentials parameter must be a JSON object'
}
this.API_URI = constants.API_ROOT;
for (var key in credentials) {
if (credentials.hasOwnProperty(key) && checkCredential(key, credentials[key])) {
this[key] = credentials[key];
}
}
if (typeof credentials !== 'object') {
throw 'credentials parameter must be a JSON object'
}
this.API_URI = constants.API_ROOT;
for (var key in credentials) {
if (credentials.hasOwnProperty(key) && checkCredential(key, credentials[key])) {
this[key] = credentials[key];
}
}
};
/**
*
*
* @param rootUrl { String } default 'https://onesignal.com/api/v1';
*/
Client.prototype.setRootUrl = function (rootUrl) {
if (!rootUrl) {
throw 'You must set a valid rootUsrl.'
}
this.API_URI = rootUrl;
if (!rootUrl) {
throw 'You must set a valid rootUsrl.'
}
this.API_URI = rootUrl;
};
/**
......@@ -104,8 +105,8 @@ Client.prototype.setRootUrl = function (rootUrl) {
* @param app { JSON } {}
*/
Client.prototype.setApp = function (app) {
checkCredential('app', app);
this.app = app;
checkCredential('app', app);
this.app = app;
};
/**
......@@ -114,20 +115,19 @@ Client.prototype.setApp = function (app) {
* @param callback
*/
Client.prototype.sendNotification = function (notification, callback) {
if (!notification || !notification.postBody) {
throw 'notification parameter must be a typeof Notification object.';
}
var postBody = notification.postBody;
if (this.apps && this.apps.length > 0) {
postBody.app_ids = this.apps;
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.userAuthKey, 'POST', postBody, callback);
} else if (this.app) {
postBody.app_id = this.app.appId;
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.app.appAuthKey, 'POST', postBody, callback);
} else {
throw 'You must set either an "app" or "apps" on Client';
}
if (!notification || !notification.postBody) {
throw 'notification parameter must be a typeof Notification object.';
}
var postBody = notification.postBody;
if (this.apps && this.apps.length > 0) {
postBody.app_ids = this.apps;
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.userAuthKey, 'POST', postBody, callback);
}
if (this.app) {
postBody.app_id = this.app.appId;
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';
};
/**
......@@ -136,76 +136,76 @@ Client.prototype.sendNotification = function (notification, callback) {
* @param callback
*/
Client.prototype.cancelNotification = function (notificationId, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId;
return basicRequest(notificationUri, this.app.appAuthKey, 'DELETE', null, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId;
return basicRequest(notificationUri, this.app.appAuthKey, 'DELETE', null, callback);
};
/**
*
*
* @param notificationId { String }
* @param callback
*/
Client.prototype.viewNotification = function (notificationId, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId;
return basicRequest(notificationUri, this.app.appAuthKey, 'GET', null, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId;
return basicRequest(notificationUri, this.app.appAuthKey, 'GET', null, callback);
};
/**
*
*
* @param query { String } ex: limit:100&offset=9
* @param callback
*/
Client.prototype.viewNotifications = function (query, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var appUri = this.API_URI + constants.NOTIFICATIONS_PATH + '?app_id=' + this.app.appId + '&' + query;
return basicRequest(appUri, this.app.appAuthKey, 'GET', null, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
var appUri = this.API_URI + constants.NOTIFICATIONS_PATH + '?app_id=' + this.app.appId + '&' + query;
return basicRequest(appUri, this.app.appAuthKey, 'GET', null, callback);
};
/**
*
*
* @param callback
*/
Client.prototype.viewApps = function (callback) {
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'GET', null, callback);
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'GET', null, callback);
};
/**
*
*
* @param appId { String }
* @param callback
*/
Client.prototype.viewApp = function (appId, callback) {
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + appId, this.userAuthKey, 'GET', null, callback);
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + appId, this.userAuthKey, 'GET', null, callback);
};
/**
*
*
* @param body { JSON }
* @param callback
*/
Client.prototype.createApp = function (body, callback) {
if (!body.name) {
throw 'You must specify a name in body';
}
if (!body.name) {
throw 'You must specify a name in body';
}
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'POST', body, callback);
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'POST', body, callback);
};
/**
......@@ -214,56 +214,56 @@ Client.prototype.createApp = function (body, callback) {
* @param callback
*/
Client.prototype.updateApp = function (body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + this.app.appId, this.userAuthKey, 'PUT', body, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
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 callback
*/
Client.prototype.viewDevices = function (query, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var viewUri = this.API_URI + constants.DEVICES_PATH + '?app_id=' + this.app.appId + '&' + query;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
var viewUri = this.API_URI + constants.DEVICES_PATH + '?app_id=' + this.app.appId + '&' + query;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
};
/**
*
*
* @param deviceId { String }
* @param callback
*/
Client.prototype.viewDevice = function (deviceId, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var viewUri = this.API_URI + constants.DEVICES_PATH + '/' + deviceId + '?app_id=' + this.app.appId;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
var viewUri = this.API_URI + constants.DEVICES_PATH + '/' + deviceId + '?app_id=' + this.app.appId;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
};
/**
*
*
* @param body { JSON }
* @param callback
*/
Client.prototype.addDevice = function (body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
}
return basicRequest(this.API_URI + constants.DEVICES_PATH, this.app.appAuthKey, 'POST', body, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
}
return basicRequest(this.API_URI + constants.DEVICES_PATH, this.app.appAuthKey, 'POST', body, callback);
};
/**
......@@ -273,10 +273,10 @@ Client.prototype.addDevice = function (body, callback) {
* @param callback
*/
Client.prototype.editDevice = function (deviceId, body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
return basicRequest(this.API_URI + constants.DEVICES_PATH + '/' + deviceId, this.app.appAuthKey, 'PUT', body, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
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) {
* @param callback
*/
Client.prototype.trackOpen = function (notificationId, body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
}
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId, this.app.appAuthKey, 'PUT', body, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
}
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) {
* @param callback
*/
Client.prototype.csvExport = function (body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var csvUri = this.API_URI + constants.DEVICES_PATH + '/csv_export' + '?app_id=' + this.app.appId;
return basicRequest(csvUri, this.app.appAuthKey, 'POST', body, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
var csvUri = this.API_URI + constants.DEVICES_PATH + '/csv_export' + '?app_id=' + this.app.appId;
return basicRequest(csvUri, this.app.appAuthKey, 'POST', body, callback);
};
module.exports = Client;
var Constants = {
API_ROOT: 'https://onesignal.com/api/v1',
/** PATHS **/
NOTIFICATIONS_PATH: '/notifications',
APPS_PATH: '/apps',
......
......@@ -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
* @constructor
*/
var Notification = function (initialBody) {
if (typeof initialBody !== 'object') {
throw 'Body must be a JSON object';
}
}
this.postBody = {};
if ('contents' in initialBody) {
this.postBody.contents = initialBody.contents;
} else if ('content_available' in initialBody){
this.postBody.contents = initialBody.contents;
return;
}
if ('content_available' in initialBody) {
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;
} else {
throw 'Body must include one of the following fields: contents, content_available, template_id'
}
return;
}
throw 'Body must include one of the following fields: contents, content_available, template_id'
};
/**
......@@ -48,15 +55,15 @@ Notification.prototype.setParameter = function (name, value) {
};
/**
*
*
* @param contents
*/
Notification.prototype.setContent = function (contents) {
this.postBody.contents = contents;
this.postBody.contents = contents;
};
/**
*
*
* @param included_segments The segment names you want to target
*/
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
*/
Notification.prototype.setExcludedSegments = function (excluded_segments) {
this.postBody.excluded_segments = excluded_segments;
this.postBody.excluded_segments = excluded_segments;
};
/**
*
*
* @param 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
*/
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