Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get user logs #176

Merged
merged 2 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/management/UsersManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ var UsersManager = function (options){
* @type {external:RestClient}
*/
this.identities = new RestClient(options.baseUrl + '/users/:id/identities/:provider/:user_id', clientOptions);

/**
* Provides a simple abstraction layer for user logs
*/
this.userLogs = new RestClient(options.baseUrl + '/users/:id/logs', clientOptions);
};


Expand Down Expand Up @@ -450,5 +455,41 @@ UsersManager.prototype.unlink = function (params, cb) {
return this.identities.delete(params);
};

/**
* Get user's log events.
*
* @method logs
* @memberOf module:management.UsersManager.prototype
*
* @example
* var params = { id: USER_ID, page: 0, per_page: 50, sort: 'date:-1', include_totals: true };
*
* management.users.logs(params, function (err, logs) {
* if (err) {
* // Handle error.
* }
*
* console.log(logs);
* });
*
* @param {Object} params Get logs data.
* @param {String} params.id User id.
* @param {Number} params.per_page Number of logs per page.
* @param {Number} params.page Page number.
* @param {String} params.sort The field to use for sorting. Use field:order where order is 1 for ascending and -1 for descending. For example date:-1.
* @param {Boolean} params.include_totals true if a query summary must be included in the result, false otherwise. Default false;
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
UsersManager.prototype.logs = function (params, cb) {
params = params || {};

if (!params.id || typeof params.id !== 'string') {
throw new ArgumentError('id field is required');
}

return this.userLogs.get(params, cb);
};

module.exports = UsersManager;
28 changes: 28 additions & 0 deletions src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,34 @@ utils.wrapPropertyMethod(ManagementClient, 'unlinkUsers', 'users.unlink');
*/
utils.wrapPropertyMethod(ManagementClient, 'linkUsers', 'users.link');

/**
* Get user's log events.
*
* @method getUserLogs
* @memberOf module:management.ManagementClient.prototype
*
* @example
* var params = { id: USER_ID, page: 0, per_page: 50, sort: 'date:-1', include_totals: true };
*
* management.getUserLogs(params, function (err, logs) {
* if (err) {
* // Handle error.
* }
*
* console.log(logs);
* });
*
* @param {Object} params Get logs data.
* @param {String} params.id User id.
* @param {Number} params.per_page Number of logs per page.
* @param {Number} params.page Page number.
* @param {String} params.sort The field to use for sorting. Use field:order where order is 1 for ascending and -1 for descending. For example date:-1.
* @param {Boolean} params.include_totals true if a query summary must be included in the result, false otherwise. Default false;
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(ManagementClient, 'getUserLogs', 'users.logs');

/**
* Get all blacklisted tokens.
Expand Down
138 changes: 138 additions & 0 deletions test/management/users.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('UsersManager', function () {
'deleteAll',
'unlink',
'link',
'logs',
'deleteMultifactorProvider',
'updateUserMetadata',
'updateAppMetadata'
Expand Down Expand Up @@ -920,4 +921,141 @@ describe('UsersManager', function () {
});
});

describe('#logs', function () {
var data = {
id: 'user_id'
};
var url = (
'/users/' + data.id + '/logs'
);


beforeEach(function () {
this.request = nock(API_URL)
.get(url)
.reply(200);
});


it('should accept a callback', function (done) {
this
.users
.logs(data, done.bind(null, null));
});


it('should return a promise when no callback is given', function (done) {
this
.users
.logs(data)
.then(done.bind(null, null));
});


it('should perform a GET request to ' + url, function (done) {
var request = this.request;

this
.users
.logs(data)
.then(function () {
expect(request.isDone())
.to.be.true;

done();
});
});


it('should pass any errors to the promise catch handler', function (done) {
nock.cleanAll();

var request = nock(API_URL)
.get(url)
.reply(500);

this
.users
.logs(data)
.catch(function (err) {
expect(err)
.to.exist;

done();
});
});


it('should include the token in the authorization header', function (done) {
nock.cleanAll();

var request = nock(API_URL)
.get(url)
.matchHeader('authorization', 'Bearer ' + this.token)
.reply(200);

this
.users
.logs(data)
.then(function () {
expect(request.isDone())
.to.be.true;

done();
});
});

it('should pass the body of the response to the "then" handler', function (done) {
nock.cleanAll();

var response = [{ test: true }];
var request = nock(API_URL)
.get(url)
.reply(200, response);

this
.users
.logs(data)
.then(function (logs) {
expect(logs)
.to.be.an.instanceOf(Array);

expect(logs.length)
.to.equal(response.length);

expect(logs[0].test)
.to.equal(response[0].test);

done();
});
});

it('should pass the parameters in the query-string', function (done) {
nock.cleanAll();

var params = {
page: 0,
per_page: 30
};
var request = nock(API_URL)
.get(url)
.query(params)
.reply(200);

data.page = params.page;
data.per_page = params.per_page;

this
.users
.logs(data)
.then(function () {
expect(request.isDone())
.to.be.true;

done();
});
});

});

});