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

Retrieve a user's Guardian enrollments #181

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
30 changes: 30 additions & 0 deletions src/management/UsersManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,17 @@ var UsersManager = function (options){

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

/**
* Provides an abstraction layer for retrieving Guardian enrollments.
*
* @type {external:RestClient}
*/
this.enrollments = new RestClient(options.baseUrl + '/users/:id/enrollments', clientOptions);
};


Expand Down Expand Up @@ -492,4 +501,25 @@ UsersManager.prototype.logs = function (params, cb) {
return this.userLogs.get(params, cb);
};

/**
* Get a list of Guardian enrollments.
*
* @method getGuardianEnrollments
* @memberOf module:management.UsersManager.prototype
*
* @example
* management.users.getGuardianEnrollments({ id: USER_ID }, function (err, enrollments) {
* console.log(enrollments);
* });
*
* @param {Object} data The user data object.
* @param {String} data.id The user id.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
UsersManager.prototype.getGuardianEnrollments = function () {
return this.enrollments.get.apply(this.enrollments, arguments);
};

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

/**
* Get a list of a user's Guardian enrollments.
*
* @method getGuardianEnrollments
* @memberOf module:management.ManagementClient.prototype
*
* @example
* management.getGuardianEnrollments({ id: USER_ID }, function (err, enrollments) {
* console.log(enrollments);
* });
*
* @param {Object} data The user data object.
* @param {String} data.id The user id.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(ManagementClient, 'getGuardianEnrollments', 'users.getGuardianEnrollments');


/**
* Get all blacklisted tokens.
*
Expand Down
87 changes: 81 additions & 6 deletions test/management/users.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ describe('UsersManager', function () {
'logs',
'deleteMultifactorProvider',
'updateUserMetadata',
'updateAppMetadata'
'updateAppMetadata',
'getGuardianEnrollments'
];

methods.forEach(function (method) {
Expand Down Expand Up @@ -929,7 +930,6 @@ describe('UsersManager', function () {
'/users/' + data.id + '/logs'
);


beforeEach(function () {
this.request = nock(API_URL)
.get(url)
Expand All @@ -943,15 +943,13 @@ describe('UsersManager', function () {
.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;

Expand All @@ -966,7 +964,6 @@ describe('UsersManager', function () {
});
});


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

Expand Down Expand Up @@ -1055,7 +1052,85 @@ describe('UsersManager', function () {
done();
});
});

});

describe('#getGuardianEnrollments', function () {
var data = {
id: 5,
};

beforeEach(function () {
this.request = nock(API_URL)
.get('/users/' + data.id + '/enrollments')
.reply(200);
});


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


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

it('should perform a GET request to /api/v2/users/5/enrollments', function (done) {
var request = this.request;

this
.users
.getGuardianEnrollments(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('/users/' + data.id + '/enrollments')
.reply(500);

this
.users
.getGuardianEnrollments(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('/users/' + data.id + '/enrollments')
.matchHeader('authorization', 'Bearer ' + this.token)
.reply(200);

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

done();
});
});
});
});