update tests to work with authStrategies

This commit is contained in:
Pedro Lopez
2022-02-27 22:18:54 -04:00
parent f6de161c7d
commit 9fe91692cf
2 changed files with 89 additions and 86 deletions

View File

@@ -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,
};