Repository: auth0/node-auth0
Pull request #58 (2015-12-14 21:53:49)

Fix bug in updateUserMetadata
- Add test for updateUserMetadata issue
- Implement fix for the issue.


Patch:
diff --git a/src/management/BlacklistedTokensManager.js b/src/management/BlacklistedTokensManager.js
index 4e8416617..ef7d60375 100644
--- a/src/management/BlacklistedTokensManager.js
+++ b/src/management/BlacklistedTokensManager.js
@@ -37,7 +37,7 @@ var BlacklistedTokensManager = function (options) {
    *
    * @type {Object}
    */
-  var clientOptions = {
+  clientOptions = {
     headers: options.headers,
     query: { repeatParams: false }
   };
diff --git a/src/management/ConnectionsManager.js b/src/management/ConnectionsManager.js
index e150d962b..1719a3697 100644
--- a/src/management/ConnectionsManager.js
+++ b/src/management/ConnectionsManager.js
@@ -37,7 +37,7 @@ var ConnectionsManager = function (options) {
    *
    * @type {Object}
    */
-  var apiOptions = {
+  apiOptions = {
     headers: options.headers,
     query: { repeatParams: false }
   };
diff --git a/src/management/DeviceCredentialsManager.js b/src/management/DeviceCredentialsManager.js
index ef0eafc67..d818035d8 100644
--- a/src/management/DeviceCredentialsManager.js
+++ b/src/management/DeviceCredentialsManager.js
@@ -37,7 +37,7 @@ var DeviceCredentialsManager = function (options) {
    *
    * @type {Object}
    */
-  var clientOptions = {
+  clientOptions = {
     headers: options.headers,
     query: { repeatParams: false }
   };
diff --git a/src/management/EmailProviderManager.js b/src/management/EmailProviderManager.js
index 6c760de09..8b03ffb75 100644
--- a/src/management/EmailProviderManager.js
+++ b/src/management/EmailProviderManager.js
@@ -21,7 +21,7 @@ var utils = require('../utils');
  */
 var EmailProviderManager = function (options) {
   if (options === null || typeof options !== 'object') {
-    throw new ArgumentError('Must provide client options');
+    throw new ArgumentError('Must provide client options');h
   }
 
   if (options.baseUrl === null || options.baseUrl === undefined) {
@@ -37,7 +37,7 @@ var EmailProviderManager = function (options) {
    *
    * @type {Object}
    */
-  var clientOptions = {
+  clientOptions = {
     headers: options.headers,
     query: { repeatParams: false }
   };
diff --git a/src/management/JobsManager.js b/src/management/JobsManager.js
index 7ff2a6ae1..cadab5994 100644
--- a/src/management/JobsManager.js
+++ b/src/management/JobsManager.js
@@ -102,16 +102,14 @@ JobsManager.prototype.importUsers = function (data, cb) {
     var req = request
       .post(options.baseUrl + '/jobs/users-imports')
       .field('connection_id', data.connection_id)
-      .attach('users', data.users);
+      .attach('users', data.users)
 
     for (var name in headers) {
       req.set(name, headers[name]);
     }
 
     req.end(function (err, res) {
-      if (err) {
-        reject(err);
-      }
+      if (err) reject(err);
 
       resolve(res);
     });
diff --git a/src/management/StatsManager.js b/src/management/StatsManager.js
index 2c7631b13..cb5ee1235 100644
--- a/src/management/StatsManager.js
+++ b/src/management/StatsManager.js
@@ -89,7 +89,7 @@ StatsManager.prototype.getActiveUsersCount = function (cb) {
   }
 
   // Return a promise.
-  return this.stats.get(options);
+  return this.stats.get(options)
 };
 
 
diff --git a/src/management/TenantManager.js b/src/management/TenantManager.js
index d79b4a348..3532358c3 100644
--- a/src/management/TenantManager.js
+++ b/src/management/TenantManager.js
@@ -81,7 +81,7 @@ TenantManager.prototype.getSettings = function (cb) {
   }
 
   // Return a promise.
-  return this.tenant.get({});
+  return this.tenant.get({})
 };
 
 
diff --git a/src/management/UsersManager.js b/src/management/UsersManager.js
index 9de472edd..7eebdab43 100644
--- a/src/management/UsersManager.js
+++ b/src/management/UsersManager.js
@@ -151,10 +151,10 @@ UsersManager.prototype.updateUserMetadata = function (params, metadata, cb) {
   };
 
   if (cb && cb instanceof Function) {
-    return this.users.patch(params, metadata, cb);
+    return this.users.patch(params, data, cb);
   }
 
-  return this.users.patch(params, metadata);
+  return this.users.patch(params, data);
 };
 
 
diff --git a/src/utils.js b/src/utils.js
index de7e552c8..9f62e181a 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -44,7 +44,7 @@ utils.wrapPropertyMethod = function (Parent, name, propertyMethod) {
       return this[property][method].bind(this[property]);
     }
   });
-};
+}
 
 
 /**
@@ -77,3 +77,4 @@ utils.getRequestPromise = function (settings) {
     });
   });
 };
+


Test patch:
diff --git a/test/management/users.tests.js b/test/management/users.tests.js
index d71b98f45..695fd097c 100644
--- a/test/management/users.tests.js
+++ b/test/management/users.tests.js
@@ -28,7 +28,9 @@ describe('UsersManager', function () {
       'deleteAll',
       'unlink',
       'link',
-      'deleteMultifactorProvider'
+      'deleteMultifactorProvider',
+      'updateUserMetadata',
+      'updateAppMetadata'
     ];
 
     methods.forEach(function (method) {
@@ -831,4 +833,91 @@ describe('UsersManager', function () {
     });
   });
 
+
+  describe('#updateUserMetadata', function () {
+    beforeEach(function () {
+      this.data = {
+        id: 5,
+        foo: 'bar',
+        test: 'data'
+      };
+
+      this.request = nock(API_URL)
+        .patch('/users/' + this.data.id)
+        .reply(200, this.data);
+    });
+
+
+    it('should accept a callback', function (done) {
+      this
+        .users
+        .updateUserMetadata({ id: 5 }, {}, done.bind(null, null));
+    });
+
+
+    it('should return a promise if no callback is given', function (done) {
+      this
+        .users
+        .updateUserMetadata({ id: 5 }, {})
+        .then(done.bind(null, null))
+        .catch(done.bind(null, null));
+    });
+
+
+    it('should perform a PATCH request to /api/v2/users/5', function (done) {
+      var request = this.request;
+
+      this
+        .users
+        .updateUserMetadata({ id: 5 }, {})
+        .then(function () {
+          expect(request.isDone())
+            .to.be.true;
+
+          done();
+        });
+    });
+
+
+    it('should include the metadata in the body of the request', function (done) {
+      nock.cleanAll();
+
+      var request = nock(API_URL)
+        .patch('/users/' + this.data.id, {
+          user_metadata: this.data
+        })
+        .reply(200);
+
+      this
+        .users
+        .updateUserMetadata({ id: 5 }, this.data)
+        .then(function () {
+          expect(request.isDone())
+            .to.be.true;
+
+          done();
+        })
+        .catch(done);
+    });
+
+
+    it('should pass any errors to the promise catch handler', function (done) {
+      nock.cleanAll();
+
+      var request = nock(API_URL)
+        .patch('/users/' + this.data.id)
+        .reply(500);
+
+      this
+        .users
+        .updateUserMetadata({ id: this.data.id }, this.data)
+        .catch(function (err) {
+          expect(err)
+            .to.exist;
+
+          done();
+        });
+    });
+  });
+
 });