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,6 +19,7 @@ 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
......@@ -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.
......@@ -13,7 +13,7 @@ var ALLOWED_CREDENTIALS = [
* 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)
*/
......@@ -32,15 +32,16 @@ var basicRequest = function (url, apiKey, method, body, callback) {
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 });
return reject(err);
}
callback && callback(err, httpResponse, data);
return resolve({ httpResponse: httpResponse, data: data });
});
});
};
......@@ -121,13 +122,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';
}
throw 'You must set either an "app" or "apps" on Client';
};
/**
......@@ -308,5 +308,4 @@ Client.prototype.csvExport = function (body, callback) {
return basicRequest(csvUri, this.app.appAuthKey, 'POST', body, callback);
};
module.exports = Client;
......@@ -20,16 +20,23 @@ 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){
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 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