<commit_before>var $require = require('proxyquire');
var expect = require('chai').expect;
var sinon = require('sinon');
var factory = require('../../xom/client/browserish');


describe('auth0/client/browserish', function() {
  
  it('should export factory function', function() {
    expect(factory).to.be.a('function');
  });
  
  describe('BrowserishClient', function() {
    var getCredential;
    
    
    describe('#enroll', function() {
      
      describe('initiating enrollment', function() {
        var guardian;
        var mfaClient = {
          httpClient: { post: function(){} }
        };
        
        
        var txn;
        
        before(function() {
          getCredential = sinon.stub().yields(null, 'XXXX0x0Xxxx0xXX0xxXX0xXxxxxxxXXX');
          
          var txn = {
            deviceAccount: {
              id: 'dev_xxxXxxX0XXXxXx0X',
              otpSecret: '<KEY>',
              status: 'confirmation_pending',
              recoveryCode: 'XXXXXXX0X0XX0XXXXXXXX0X0'
            },
            enrollmentTxId: 'X0XXxxXX0xx00XXXxXxxXxxXxxx0XXx0',
            featureSwitches: {
              mfaApp: {
                enroll: true
              },
              mfaSms: {
                enroll: false
              }
            },
            transactionToken: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhdXRoMHwwMHh4MDB4MDAwMHgwMHgwMDAweDAwMDAiLCJleHAiOjE0ODE3NTgzMjMsImlzcyI6Imh0dHBzOi8vYWNtZS5ndWFyZGlhbi5hdXRoMC5jb20iLCJhdWQiOiJodHRwczovL2FjbWUuZ3VhcmRpYW4uYXV0aDAuY29tL2FwaSIsImF6cCI6ImJyb3dzZXIiLCJ0eGlkIjoidHhuX1hYeFhYWHgwMHhYMFh4eFh4WFhYMDB4eHhYMFh4MCJ9.<KEY>',
            availableAuthenticationMethods: ['push', 'otp'],
            availableEnrollmentMethods: ['push', 'otp']
          };
          
          sinon.stub(mfaClient.httpClient, 'post').yields(null, txn);
          guardian = sinon.stub().returns(mfaClient);
        });
        
        after(function() {
          mfaClient.httpClient.post.restore();
        });
        
        before(function(done) {
          var factory = $require('../../xom/client/browserish', { 'auth0-guardian-js': guardian });
        
          var client = factory(getCredential);
          client.enroll('auth0|00xx00x0000x00x0000x0000', function(_err, _txn) {
            if (_err) { return done(_err); }
            txn = _txn;
            done();
          });
        });
        
        it('should get authorization credential', function() {
          expect(getCredential).to.have.been.calledOnce;
          var call = getCredential.getCall(0);
          expect(call.args[0]).to.equal('auth0|00xx00x0000x00x0000x0000');
        });
        
        it('should construct MFA API client', function() {
          expect(guardian).to.have.been.calledOnce;
          var call = guardian.getCall(0);
          expect(call.args[0]).to.deep.equal({
            serviceUrl: 'https://hansonhq.guardian.auth0.com',
            ticket: 'XXXX0x0Xxxx0xXX0xxXX0xXxxxxxxXXX'
          });
        });
        
        it('should request MFA API to start flow', function() {
          expect(mfaClient.httpClient.post).to.have.been.calledOnce;
          var call = mfaClient.httpClient.post.getCall(0);
          expect(call.args[0]).to.equal('/api/start-flow');
          expect(call.args[2]).to.equal(null);
        });
        
        it('should yield transaction', function() {
          expect(txn).to.be.an('object')
          expect(txn.enrollmentTxId).to.equal('X0XXxxXX0xx00XXXxXxxXxxXxxx0XXx0');
        });
      }); // initiating enrollment
      
      describe('already enrolled with Google Authenticator', function() {
        var guardian;
        var mfaClient = {
          httpClient: { post: function(){} }
        };
        
        
        var err;
        
        before(function() {
          getCredential = sinon.stub().yields(null, 'XXXX0x0Xxxx0xXX0xxXX0xXxxxxxxXXX');
          
          var txn = {
            deviceAccount: {
              status: 'confirmed',
              pushNotifications: {
                enabled: false
              },
              availableMethods: ['otp'],
              methods: ['otp']
            },
            featureSwitches: {
              mfaApp: {
                enroll: true
              },
              mfaSms: {
                enroll: false
              }
            },
            transactionToken: '<KEY>',
            availableAuthenticationMethods: ['push', 'otp'],
            availableEnrollmentMethods: ['push', 'otp']
          };
          
          sinon.stub(mfaClient.httpClient, 'post').yields(null, txn);
          guardian = sinon.stub().returns(mfaClient);
        });
        
        after(function() {
          mfaClient.httpClient.post.restore();
        });
        
        before(function(done) {
          var factory = $require('../../xom/client/browserish', { 'auth0-guardian-js': guardian });
        
          var client = factory(getCredential);
          client.enroll('auth0|00xx00x0000x00x0000x0000', function(_err) {
            err = _err;
            done();
          });
        });
        
        it('should get authorization credential', function() {
          expect(getCredential).to.have.been.calledOnce;
          var call = getCredential.getCall(0);
          expect(call.args[0]).to.equal('auth0|00xx00x0000x00x0000x0000');
        });
        
        it('should construct MFA API client', function() {
          expect(guardian).to.have.been.calledOnce;
          var call = guardian.getCall(0);
          expect(call.args[0]).to.deep.equal({
            serviceUrl: 'https://hansonhq.guardian.auth0.com',
            ticket: 'XXXX0x0Xxxx0xXX0xxXX0xXxxxxxxXXX'
          });
        });
        
        it('should request MFA API to start flow', function() {
          expect(mfaClient.httpClient.post).to.have.been.calledOnce;
          var call = mfaClient.httpClient.post.getCall(0);
          expect(call.args[0]).to.equal('/api/start-flow');
          expect(call.args[2]).to.equal(null);
        });
        
        it('should yield error', function() {
          expect(err).to.be.an.instanceOf(Error);
          expect(err.message).to.equal('You cannot enroll again. You are already enrolled.');
          expect(err.errorCode).to.equal('already_enrolled');
        });
      }); // already enrolled with Google Authenticator
      
    }); // #enroll
    
    describe('#sendPush', function() {
      
      describe('failure cause by push notifications not being configured', function() {
        var guardian;
        var mfaClient = {<commit_msg>Add test cases.<commit_after>var $require = require('proxyquire');
var expect = require('chai').expect;
var sinon = require('sinon');
var factory = require('../../xom/client/browserish');


describe('auth0/client/browserish', function() {
  
  it('should export factory function', function() {
    expect(factory).to.be.a('function');
  });
  
  describe('BrowserishClient', function() {
    var getCredential;
    
    
    describe('#enroll', function() {
      
      describe('initiating enrollment', function() {
        var guardian;
        var mfaClient = {
          httpClient: { post: function(){} }
        };
        
        
        var txn;
        
        before(function() {
          getCredential = sinon.stub().yields(null, 'XXXX0x0Xxxx0xXX0xxXX0xXxxxxxxXXX');
          
          var txn = {
            deviceAccount: {
              id: 'dev_xxxXxxX0XXXxXx0X',
              otpSecret: '<KEY>',
              status: 'confirmation_pending',
              recoveryCode: 'XXXXXXX0X0XX0XXXXXXXX0X0'
            },
            enrollmentTxId: 'X0XXxxXX0xx00XXXxXxxXxxXxxx0XXx0',
            featureSwitches: {
              mfaApp: {
                enroll: true
              },
              mfaSms: {
                enroll: false
              }
            },
            transactionToken: '<KEY>',
            availableAuthenticationMethods: ['push', 'otp'],
            availableEnrollmentMethods: ['push', 'otp']
          };
          
          sinon.stub(mfaClient.httpClient, 'post').yields(null, txn);
          guardian = sinon.stub().returns(mfaClient);
        });
        
        after(function() {
          mfaClient.httpClient.post.restore();
        });
        
        before(function(done) {
          var factory = $require('../../xom/client/browserish', { 'auth0-guardian-js': guardian });
        
          var client = factory(getCredential);
          client.enroll('auth0|00xx00x0000x00x0000x0000', function(_err, _txn) {
            if (_err) { return done(_err); }
            txn = _txn;
            done();
          });
        });
        
        it('should get authorization credential', function() {
          expect(getCredential).to.have.been.calledOnce;
          var call = getCredential.getCall(0);
          expect(call.args[0]).to.equal('auth0|00xx00x0000x00x0000x0000');
        });
        
        it('should construct MFA API client', function() {
          expect(guardian).to.have.been.calledOnce;
          var call = guardian.getCall(0);
          expect(call.args[0]).to.deep.equal({
            serviceUrl: 'https://hansonhq.guardian.auth0.com',
            ticket: 'XXXX0x0Xxxx0xXX0xxXX0xXxxxxxxXXX'
          });
        });
        
        it('should request MFA API to start flow', function() {
          expect(mfaClient.httpClient.post).to.have.been.calledOnce;
          var call = mfaClient.httpClient.post.getCall(0);
          expect(call.args[0]).to.equal('/api/start-flow');
          expect(call.args[2]).to.equal(null);
        });
        
        it('should yield transaction', function() {
          expect(txn).to.be.an('object')
          expect(txn.enrollmentTxId).to.equal('X0XXxxXX0xx00XXXxXxxXxxXxxx0XXx0');
        });
      }); // initiating enrollment
      
      describe('already enrolled with Google Authenticator', function() {
        var guardian;
        var mfaClient = {
          httpClient: { post: function(){} }
        };
        
        
        var err;
        
        before(function() {
          getCredential = sinon.stub().yields(null, 'XXXX0x0Xxxx0xXX0xxXX0xXxxxxxxXXX');
          
          var txn = {
            deviceAccount: {
              status: 'confirmed',
              pushNotifications: {
                enabled: false
              },
              availableMethods: ['otp'],
              methods: ['otp']
            },
            featureSwitches: {
              mfaApp: {
                enroll: true
              },
              mfaSms: {
                enroll: false
              }
            },
            transactionToken: '<KEY>',
            availableAuthenticationMethods: ['push', 'otp'],
            availableEnrollmentMethods: ['push', 'otp']
          };
          
          sinon.stub(mfaClient.httpClient, 'post').yields(null, txn);
          guardian = sinon.stub().returns(mfaClient);
        });
        
        after(function() {
          mfaClient.httpClient.post.restore();
        });
        
        before(function(done) {
          var factory = $require('../../xom/client/browserish', { 'auth0-guardian-js': guardian });
        
          var client = factory(getCredential);
          client.enroll('auth0|00xx00x0000x00x0000x0000', function(_err) {
            err = _err;
            done();
          });
        });
        
        it('should get authorization credential', function() {
          expect(getCredential).to.have.been.calledOnce;
          var call = getCredential.getCall(0);
          expect(call.args[0]).to.equal('auth0|00xx00x0000x00x0000x0000');
        });
        
        it('should construct MFA API client', function() {
          expect(guardian).to.have.been.calledOnce;
          var call = guardian.getCall(0);
          expect(call.args[0]).to.deep.equal({
            serviceUrl: 'https://hansonhq.guardian.auth0.com',
            ticket: 'XXXX0x0Xxxx0xXX0xxXX0xXxxxxxxXXX'
          });
        });
        
        it('should request MFA API to start flow', function() {
          expect(mfaClient.httpClient.post).to.have.been.calledOnce;
          var call = mfaClient.httpClient.post.getCall(0);
          expect(call.args[0]).to.equal('/api/start-flow');
          expect(call.args[2]).to.equal(null);
        });
        
        it('should yield error', function() {
          expect(err).to.be.an.instanceOf(Error);
          expect(err.message).to.equal('You cannot enroll again. You are already enrolled.');
          expect(err.errorCode).to.equal('already_enrolled');
        });
      }); // already enrolled with Google Authenticator
      
      describe('already enrolled with Auth0 Guardian', function() {
        var guardian;
        var mfaClient = {
          httpClient: { post: function(){} }
        };
        
        
        var err;
        
        before(function() {
          getCredential = sinon.stub().yields(null, 'XXXX0x0Xxxx0xXX0xxXX0xXxxxxxxXXX');
          
          var txn = {
            deviceAccount: {
              status: 'confirmed',
              name: 'John’s iPhone 7',
              phoneNumber: null,
              pushNotifications: {
                enabled: true
              },
              availableMethods: ['push', 'otp'],
              methods: ['push', 'otp']
            },
            featureSwitches: {
              mfaApp: {
                enroll: true
              },
              mfaSms: {
                enroll: false
              }
            },
            transactionToken: '<KEY>',
            availableAuthenticationMethods: ['push', 'otp'],
            availableEnrollmentMethods: ['push', 'otp']
          };
          
          sinon.stub(mfaClient.httpClient, 'post').yields(null, txn);
          guardian = sinon.stub().returns(mfaClient);
        });
        
        after(function() {
          mfaClient.httpClient.post.restore();
        });
        
        before(function(done) {
          var factory = $require('../../xom/client/browserish', { 'auth0-guardian-js': guardian });
        
          var client = factory(getCredential);
          client.enroll('auth0|00xx00x0000x00x0000x0000', function(_err) {
            err = _err;
            done();
          });
        });
        
        it('should get authorization credential', function() {
          expect(getCredential).to.have.been.calledOnce;
          var call = getCredential.getCall(0);
          expect(call.args[0]).to.equal('auth0|00xx00x0000x00x0000x0000');
        });
        
        it('should construct MFA API client', function() {
          expect(guardian).to.have.been.calledOnce;
          var call = guardian.getCall(0);
          expect(call.args[0]).to.deep.equal({
            serviceUrl: 'https://hansonhq.guardian.auth0.com',
            ticket: 'XXXX0x0Xxxx0xXX0xxXX0xXxxxxxxXXX'
          });
        });
        
        it('should request MFA API to start flow', function() {
          expect(mfaClient.httpClient.post).to.have.been.calledOnce;
          var call = mfaClient.httpClient.post.getCall(0);
          expect(call.args[0]).to.equal('/api/start-flow');
          expect(call.args[2]).to.equal(null);
        });
        
        it('should yield error', function() {
          expect(err).to.be.an.instanceOf(Error);
          expect(err.message).to.equal('You cannot enroll again. You are already enrolled.');
          expect(err.errorCode).to.equal('already_enrolled');
        });
      }); // already enrolled with Auth0 Guardian
      
    }); // #enroll
    
    describe('#sendPush', function() {
      
      describe('failure cause by push notifications not being configured', function() {
        var guardian;
        var mfaClient = {
{'fraction_non_alphanumeric': 0.130420999239158, 'fraction_numerical': 0.022888663454222673, 'mean_word_length': 1.7715691442628712, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2989675', 'n_tokens_mistral': 4878, 'n_tokens_neox': 4644, 'n_words': 706}