Commit 4cc4a041 authored by Fellipe Capelli's avatar Fellipe Capelli

Add tests for client and notification

parent 5da1d758
......@@ -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.
......@@ -54,7 +54,7 @@ var basicRequest = function (url, apiKey, method, body, callback) {
var checkCredential = function (credentialName, credential) {
for (var i = 0, credentialLen = ALLOWED_CREDENTIALS.length; i < credentialLen; i++) {
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;
}
if (ALLOWED_CREDENTIALS[i].requiredFields) {
......@@ -121,12 +121,12 @@ Client.prototype.sendNotification = function (notification, callback) {
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) {
}
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';
}
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';
};
/**
......
......@@ -19,17 +19,24 @@ var ALLOWED_FIELDS = ['contents', 'included_segments', 'excluded_segments', 'fil
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'
};
/**
......
'use strict';
var expect = require('chai').expect;
var OneSignal = require('../lib');
var ClientMock = require('./mocks/client');
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);
})
})
})
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'
}
}
'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);
})
})
})
'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