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.
This diff is collapsed.
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