Repository: auth0/auth0.js
Pull request #492 (2017-08-21 18:35:06)

Using transaction manager on passwordlessStart


Patch:
diff --git a/src/web-auth/index.js b/src/web-auth/index.js
index cd8a4db..4f19fa9 100644
--- a/src/web-auth/index.js
+++ b/src/web-auth/index.js
@@ -337,6 +337,21 @@ WebAuth.prototype.changePassword = function(options, cb) {
  * @see   {@link https://auth0.com/docs/api/authentication#passwordless}
  */
 WebAuth.prototype.passwordlessStart = function(options, cb) {
+  var authParams = objectHelper
+    .merge(this.baseOptions, [
+      'responseType',
+      'responseMode',
+      'redirectUri',
+      'scope',
+      'audience',
+      '_csrf',
+      'state',
+      '_intstate',
+      'nonce'
+    ])
+    .with(options.authParams);
+
+  options.authParams = this.transactionManager.process(authParams);
   return this.client.passwordless.start(options, cb);
 };
 


Test patch:
diff --git a/test/web-auth/web-auth.test.js b/test/web-auth/web-auth.test.js
index db81a70..d3dc92b 100644
--- a/test/web-auth/web-auth.test.js
+++ b/test/web-auth/web-auth.test.js
@@ -738,7 +738,56 @@ describe('auth0.WebAuth', function() {
     });
 
     afterEach(function() {
-      request.post.restore();
+      TransactionManager.prototype.process.restore();
+      if (request.post.restore) {
+        request.post.restore();
+      }
+      if (this.auth0.client.passwordless.start.restore) {
+        this.auth0.client.passwordless.start.restore();
+      }
+    });
+    it('should call `transactionManager.process` with merged params', function() {
+      stub(this.auth0.client.passwordless, 'start', function() {});
+      spy(TransactionManager.prototype, 'process');
+      var expectedOptions = {
+        responseType: 'code',
+        redirectUri: 'http://page.com/callback',
+        auth: 'params'
+      };
+
+      this.auth0.passwordlessStart(
+        {
+          connection: 'sms',
+          phoneNumber: '+55165134',
+          verificationCode: '123456',
+          authParams: {
+            auth: 'params'
+          }
+        },
+        function(err, data) {
+          return 'cb';
+        }
+      );
+      var mock = TransactionManager.prototype.process;
+      expect(mock.calledOnce).to.be(true);
+      expect(mock.firstCall.args[0]).to.be.eql(expectedOptions);
+    });
+    it('should call `passwordless.start` with params from transactionManager', function() {
+      var expectedOptions = {
+        authParams: {
+          from: 'transactionManager'
+        }
+      };
+      var mockVerify = stub(this.auth0.client.passwordless, 'start', function() {});
+      stub(TransactionManager.prototype, 'process', function() {
+        return expectedOptions.authParams;
+      });
+
+      this.auth0.passwordlessStart({}, function(err, data) {
+        return 'cb';
+      });
+      expect(mockVerify.calledOnce).to.be(true);
+      expect(mockVerify.firstCall.args[0]).to.be.eql(expectedOptions);
     });
 
     it('should call passwordless start sms with all the options', function(done) {
@@ -752,7 +801,8 @@ describe('auth0.WebAuth', function() {
             send: 'code',
             authParams: {
               redirect_uri: 'http://page.com/callback',
-              response_type: 'code'
+              response_type: 'code',
+              from: 'tm'
             }
           },
           headers: {
@@ -766,6 +816,10 @@ describe('auth0.WebAuth', function() {
         });
       });
 
+      stub(TransactionManager.prototype, 'process', function() {
+        return { from: 'tm' };
+      });
+
       this.auth0.passwordlessStart(
         {
           connection: 'the_connection',
@@ -791,7 +845,8 @@ describe('auth0.WebAuth', function() {
             send: 'code',
             authParams: {
               redirect_uri: 'http://page.com/callback',
-              response_type: 'code'
+              response_type: 'code',
+              from: 'tm'
             }
           },
           headers: {
@@ -805,6 +860,10 @@ describe('auth0.WebAuth', function() {
         });
       });
 
+      stub(TransactionManager.prototype, 'process', function() {
+        return { from: 'tm' };
+      });
+
       this.auth0.passwordlessStart(
         {
           connection: 'the_connection',
@@ -913,9 +972,6 @@ describe('auth0.WebAuth', function() {
       });
       stub(this.auth0.client.passwordless, 'verify', function() {});
       spy(TransactionManager.prototype, 'process');
-      var expectedOptions = {
-        responseType: undefined
-      };
       expect(() => this.auth0.passwordlessVerify({})).to.throwError(
         /responseType option is required/
       );