diff --git a/tests/client.js b/tests/client.js index c99d672..7bfcc1b 100644 --- a/tests/client.js +++ b/tests/client.js @@ -9,6 +9,7 @@ const Message = require('../src/structures/Message'); const MessageMedia = require('../src/structures/MessageMedia'); const Location = require('../src/structures/Location'); const { MessageTypes, WAState } = require('../src/util/Constants'); +const { LegacySessionAuth } = require('../src/authStrategies/LegacySessionAuth'); const expect = chai.expect; chai.use(chaiAsPromised); @@ -71,7 +72,7 @@ describe('Client', function() { expect(authenticatedCallback.called).to.equal(true); - if(helper.isUsingDeprecatedSession()) { + if(helper.isUsingLegacySession()) { const newSession = authenticatedCallback.args[0][0]; expect(newSession).to.have.key([ 'WABrowserId', @@ -87,6 +88,80 @@ describe('Client', function() { await client.destroy(); }); + describe('LegacySessionAuth', function () { + it('should fail auth if session is invalid', async function() { + this.timeout(40000); + + const authFailCallback = sinon.spy(); + const qrCallback = sinon.spy(); + const readyCallback = sinon.spy(); + + const client = helper.createClient({ + options: { + authStrategy: new LegacySessionAuth({ + session: { + WABrowserId: 'invalid', + WASecretBundle: 'invalid', + WAToken1: 'invalid', + WAToken2: 'invalid' + }, + restartOnAuthFail: false, + }), + } + }); + + client.on('qr', qrCallback); + client.on('auth_failure', authFailCallback); + client.on('ready', readyCallback); + + client.initialize(); + + await helper.sleep(25000); + + expect(authFailCallback.called).to.equal(true); + expect(authFailCallback.args[0][0]).to.equal('Unable to log in. Are the session details valid?'); + + expect(readyCallback.called).to.equal(false); + expect(qrCallback.called).to.equal(false); + + await client.destroy(); + }); + + it('can restart without a session if session was invalid and restartOnAuthFail=true', async function() { + this.timeout(40000); + + const authFailCallback = sinon.spy(); + const qrCallback = sinon.spy(); + + const client = helper.createClient({ + options: { + authStrategy: new LegacySessionAuth({ + session: { + WABrowserId: 'invalid', + WASecretBundle: 'invalid', + WAToken1: 'invalid', + WAToken2: 'invalid' + }, + restartOnAuthFail: true, + }), + } + }); + + client.on('auth_failure', authFailCallback); + client.on('qr', qrCallback); + + client.initialize(); + + await helper.sleep(35000); + + expect(authFailCallback.called).to.equal(true); + expect(qrCallback.called).to.equal(true); + expect(qrCallback.args[0][0]).to.have.lengthOf(152); + + await client.destroy(); + }); + }); + describe('Non-MD only', function () { if(!isMD) { it('can take over if client was logged in somewhere else with takeoverOnConflict=true', async function() { @@ -127,78 +202,6 @@ describe('Client', function() { await client1.destroy(); }); - - it('should fail auth if session is invalid', async function() { - this.timeout(40000); - - const authFailCallback = sinon.spy(); - const qrCallback = sinon.spy(); - const readyCallback = sinon.spy(); - - const client = helper.createClient({ - options: { - session: { - WABrowserId: 'invalid', - WASecretBundle: 'invalid', - WAToken1: 'invalid', - WAToken2: 'invalid' - }, - authTimeoutMs: 10000, - restartOnAuthFail: false, - useDeprecatedSessionAuth: true - } - }); - - client.on('qr', qrCallback); - client.on('auth_failure', authFailCallback); - client.on('ready', readyCallback); - - client.initialize(); - - await helper.sleep(25000); - - expect(authFailCallback.called).to.equal(true); - expect(authFailCallback.args[0][0]).to.equal('Unable to log in. Are the session details valid?'); - - expect(readyCallback.called).to.equal(false); - expect(qrCallback.called).to.equal(false); - - await client.destroy(); - }); - - it('can restart without a session if session was invalid and restartOnAuthFail=true', async function() { - this.timeout(40000); - - const authFailCallback = sinon.spy(); - const qrCallback = sinon.spy(); - - const client = helper.createClient({ - options:{ - session: { - WABrowserId: 'invalid', - WASecretBundle: 'invalid', - WAToken1: 'invalid', - WAToken2: 'invalid' - }, - authTimeoutMs: 10000, - restartOnAuthFail: true, - useDeprecatedSessionAuth: true - } - }); - - client.on('auth_failure', authFailCallback); - client.on('qr', qrCallback); - - client.initialize(); - - await helper.sleep(35000); - - expect(authFailCallback.called).to.equal(true); - expect(qrCallback.called).to.equal(true); - expect(qrCallback.args[0][0]).to.have.lengthOf(152); - - await client.destroy(); - }); } }); }); diff --git a/tests/helper.js b/tests/helper.js index bcd22a3..fc9ddae 100644 --- a/tests/helper.js +++ b/tests/helper.js @@ -1,13 +1,12 @@ const path = require('path'); -const crypto = require('crypto'); -const Client = require('../src/Client'); +const { Client, LegacySessionAuth, LocalAuth } = require('..'); require('dotenv').config(); const remoteId = process.env.WWEBJS_TEST_REMOTE_ID; if(!remoteId) throw new Error('The WWEBJS_TEST_REMOTE_ID environment variable has not been set.'); -function isUsingDeprecatedSession() { +function isUsingLegacySession() { return Boolean(process.env.WWEBJS_TEST_SESSION || process.env.WWEBJS_TEST_SESSION_PATH); } @@ -15,10 +14,10 @@ function isMD() { return Boolean(process.env.WWEBJS_TEST_MD); } -if(isUsingDeprecatedSession() && isMD()) throw 'Cannot use deprecated sessions with WWEBJS_TEST_MD=true'; +if(isUsingLegacySession() && isMD()) throw 'Cannot use legacy sessions with WWEBJS_TEST_MD=true'; function getSessionFromEnv() { - if (!isUsingDeprecatedSession()) return null; + if (!isUsingLegacySession()) return null; const envSession = process.env.WWEBJS_TEST_SESSION; if(envSession) return JSON.parse(envSession); @@ -34,17 +33,18 @@ function createClient({authenticated, options: additionalOpts}={}) { const options = {}; if(authenticated) { - const deprecatedSession = getSessionFromEnv(); - if(deprecatedSession) { - options.session = deprecatedSession; - options.useDeprecatedSessionAuth = true; + const legacySession = getSessionFromEnv(); + if(legacySession) { + options.authStrategy = new LegacySessionAuth({ + session: legacySession + }); } else { const clientId = process.env.WWEBJS_TEST_CLIENT_ID; if(!clientId) throw new Error('No session found in environment.'); - options.clientId = clientId; + options.authStrategy = new LocalAuth({ + clientId + }); } - } else { - options.clientId = crypto.randomBytes(5).toString('hex'); } const allOpts = {...options, ...(additionalOpts || {})}; @@ -58,7 +58,7 @@ function sleep(ms) { module.exports = { sleep, createClient, - isUsingDeprecatedSession, + isUsingLegacySession, isMD, remoteId, }; \ No newline at end of file