mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-20 04:29:15 +00:00
update tests to work with authStrategies
This commit is contained in:
149
tests/client.js
149
tests/client.js
@@ -9,6 +9,7 @@ const Message = require('../src/structures/Message');
|
|||||||
const MessageMedia = require('../src/structures/MessageMedia');
|
const MessageMedia = require('../src/structures/MessageMedia');
|
||||||
const Location = require('../src/structures/Location');
|
const Location = require('../src/structures/Location');
|
||||||
const { MessageTypes, WAState } = require('../src/util/Constants');
|
const { MessageTypes, WAState } = require('../src/util/Constants');
|
||||||
|
const { LegacySessionAuth } = require('../src/authStrategies/LegacySessionAuth');
|
||||||
|
|
||||||
const expect = chai.expect;
|
const expect = chai.expect;
|
||||||
chai.use(chaiAsPromised);
|
chai.use(chaiAsPromised);
|
||||||
@@ -71,7 +72,7 @@ describe('Client', function() {
|
|||||||
|
|
||||||
expect(authenticatedCallback.called).to.equal(true);
|
expect(authenticatedCallback.called).to.equal(true);
|
||||||
|
|
||||||
if(helper.isUsingDeprecatedSession()) {
|
if(helper.isUsingLegacySession()) {
|
||||||
const newSession = authenticatedCallback.args[0][0];
|
const newSession = authenticatedCallback.args[0][0];
|
||||||
expect(newSession).to.have.key([
|
expect(newSession).to.have.key([
|
||||||
'WABrowserId',
|
'WABrowserId',
|
||||||
@@ -87,6 +88,80 @@ describe('Client', function() {
|
|||||||
await client.destroy();
|
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 () {
|
describe('Non-MD only', function () {
|
||||||
if(!isMD) {
|
if(!isMD) {
|
||||||
it('can take over if client was logged in somewhere else with takeoverOnConflict=true', async function() {
|
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();
|
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();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const crypto = require('crypto');
|
const { Client, LegacySessionAuth, LocalAuth } = require('..');
|
||||||
const Client = require('../src/Client');
|
|
||||||
|
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
|
|
||||||
const remoteId = process.env.WWEBJS_TEST_REMOTE_ID;
|
const remoteId = process.env.WWEBJS_TEST_REMOTE_ID;
|
||||||
if(!remoteId) throw new Error('The WWEBJS_TEST_REMOTE_ID environment variable has not been set.');
|
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);
|
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);
|
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() {
|
function getSessionFromEnv() {
|
||||||
if (!isUsingDeprecatedSession()) return null;
|
if (!isUsingLegacySession()) return null;
|
||||||
|
|
||||||
const envSession = process.env.WWEBJS_TEST_SESSION;
|
const envSession = process.env.WWEBJS_TEST_SESSION;
|
||||||
if(envSession) return JSON.parse(envSession);
|
if(envSession) return JSON.parse(envSession);
|
||||||
@@ -34,17 +33,18 @@ function createClient({authenticated, options: additionalOpts}={}) {
|
|||||||
const options = {};
|
const options = {};
|
||||||
|
|
||||||
if(authenticated) {
|
if(authenticated) {
|
||||||
const deprecatedSession = getSessionFromEnv();
|
const legacySession = getSessionFromEnv();
|
||||||
if(deprecatedSession) {
|
if(legacySession) {
|
||||||
options.session = deprecatedSession;
|
options.authStrategy = new LegacySessionAuth({
|
||||||
options.useDeprecatedSessionAuth = true;
|
session: legacySession
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
const clientId = process.env.WWEBJS_TEST_CLIENT_ID;
|
const clientId = process.env.WWEBJS_TEST_CLIENT_ID;
|
||||||
if(!clientId) throw new Error('No session found in environment.');
|
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 || {})};
|
const allOpts = {...options, ...(additionalOpts || {})};
|
||||||
@@ -58,7 +58,7 @@ function sleep(ms) {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
sleep,
|
sleep,
|
||||||
createClient,
|
createClient,
|
||||||
isUsingDeprecatedSession,
|
isUsingLegacySession,
|
||||||
isMD,
|
isMD,
|
||||||
remoteId,
|
remoteId,
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user