fix tests for optional deps
authorJustin Wind <justin.wind+git@gmail.com>
Fri, 25 Oct 2024 17:07:12 +0000 (10:07 -0700)
committerJustin Wind <justin.wind+git@gmail.com>
Fri, 25 Oct 2024 17:07:12 +0000 (10:07 -0700)
lib/authenticator.js
test/lib/authenticator.js

index 2534cfa2d5224fe9bdf4c1c407472deb3cd8bd1f..7b61cf4610b322102b9eaa3b747ae95e24690023 100644 (file)
@@ -84,10 +84,10 @@ class Authenticator {
       plain: {},
     };
     try {
-      this.authn.argon2 = require('argon2');
+      this.authn.argon2 = require('argon2'); // eslint-disable-line n/no-missing-require
     } catch (e) { /**/ } // eslint-disable-line no-unused-vars
     try {
-      this.authn.pam = require('node-linux-pam');
+      this.authn.pam = require('node-linux-pam'); // eslint-disable-line n/no-missing-require
       this.forbiddenPAMIdentifiers = options.authenticator.forbiddenPAMIdentifiers ?? ['root'];
     } catch (e) { /**/ } // eslint-disable-line no-unused-vars
 
index 0e55094603e4e9eac0d718c8244633f56ba27891..3b24245af247e19ac2b3906cc6d262b97b7725e3 100644 (file)
@@ -10,9 +10,9 @@ const Config = require('../stub-config');
 
 describe('Authenticator', function () {
   let authenticator, credential, ctx, identifier, password, options;
-  function _authMechanismRequired(a, m) {
+  function _authMechanismRequired(t, a, m) {
     if (!a.authn[m]) { // eslint-disable-line security/detect-object-injection
-      this.skip();
+      t.skip();
     }
   }
 
@@ -144,7 +144,7 @@ describe('Authenticator', function () {
   describe('isValidBasic', function () {
     const b64 = (x) => Buffer.from(x).toString('base64');
     it('succeeds', async function () {
-      _authMechanismRequired(authenticator, 'argon2');
+      _authMechanismRequired(this, authenticator, 'argon2');
       authenticator.db.authenticationGet.resolves({
         identifier,
         credential,
@@ -155,7 +155,7 @@ describe('Authenticator', function () {
       assert.strictEqual(ctx.authenticationId, identifier);
     });
     it('fails', async function () {
-      _authMechanismRequired(authenticator, 'argon2');
+      _authMechanismRequired(this, authenticator, 'argon2');
       authenticator.db.authenticationGet.resolves({
         identifier,
         credential,
@@ -186,7 +186,7 @@ describe('Authenticator', function () {
 
   describe('isValidIdentifierCredential', function () {
     it('succeeds', async function () {
-      _authMechanismRequired(authenticator, 'argon2');
+      _authMechanismRequired(this, authenticator, 'argon2');
       authenticator.db.authenticationGet.resolves({
         identifier,
         credential,
@@ -197,7 +197,7 @@ describe('Authenticator', function () {
     });
     it('succeeds with OTP', async function () {
       const otpKey = Buffer.from('1234567890');
-      _authMechanismRequired(authenticator, 'argon2');
+      _authMechanismRequired(this, authenticator, 'argon2');
       authenticator.db.authenticationGet.resolves({
         identifier,
         credential,
@@ -209,7 +209,7 @@ describe('Authenticator', function () {
       assert.deepStrictEqual(ctx.otpKey, otpKey);
     });
     it('fails', async function () {
-      _authMechanismRequired(authenticator, 'argon2');
+      _authMechanismRequired(this, authenticator, 'argon2');
       authenticator.db.authenticationGet.resolves({
         identifier,
         credential,
@@ -240,7 +240,7 @@ describe('Authenticator', function () {
       assert.strictEqual(ctx.authenticationId, undefined);
     });
     it('covers PAM', async function () {
-      _authMechanismRequired(authenticator, 'pam');
+      _authMechanismRequired(this, authenticator, 'pam');
       sinon.stub(authenticator, '_isValidPAMIdentifier').resolves(true);
       authenticator.db.authenticationGet.resolves({
         identifier,
@@ -255,7 +255,7 @@ describe('Authenticator', function () {
   describe('_isValidPAMIdentifier', function () {
     let authData;
     beforeEach(function () {
-      _authMechanismRequired(authenticator, 'pam');
+      _authMechanismRequired(this, authenticator, 'pam');
       sinon.stub(authenticator.authn.pam, 'pamAuthenticatePromise');
       authData = {
         identifier,
@@ -267,13 +267,11 @@ describe('Authenticator', function () {
       assert.strictEqual(result, true);
     });
     it('covers failure', async function () {
-      _authMechanismRequired(authenticator, 'pam');
       authenticator.authn.pam.pamAuthenticatePromise.rejects(new authenticator.authn.pam.PamError());
       const result = await authenticator._isValidPAMIdentifier(authData, credential);
       assert.strictEqual(result, false);
     });
     it('covers error', async function () {
-      _authMechanismRequired(authenticator, 'pam');
       const expected = new Error('blah');
       authenticator.authn.pam.pamAuthenticatePromise.rejects(expected);
       assert.rejects(() => authenticator._isValidPAMIdentifier(authData, credential), expected);