Skip to content

Commit

Permalink
update tests to work with authStrategies
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroslopez committed Feb 28, 2022
1 parent f6de161 commit 9fe9169
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 86 deletions.
149 changes: 76 additions & 73 deletions tests/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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',
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
});
}
});
});
Expand Down
26 changes: 13 additions & 13 deletions tests/helper.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
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);
}

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);
Expand All @@ -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 || {})};
Expand All @@ -58,7 +58,7 @@ function sleep(ms) {
module.exports = {
sleep,
createClient,
isUsingDeprecatedSession,
isUsingLegacySession,
isMD,
remoteId,
};

0 comments on commit 9fe9169

Please sign in to comment.