60 lines
2.0 KiB
TypeScript
60 lines
2.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
import { smsConfig } from "../../src/env.js";
|
|
import { isSmsRecipientAllowed } from "../../src/services/smsService.js";
|
|
|
|
const KEY = "SMS_ALLOWED_RECIPIENT_DIDS";
|
|
const MINE = "did:ethr:0xabc";
|
|
const THEIRS = "did:ethr:0xdef";
|
|
|
|
let saved: string | undefined;
|
|
|
|
beforeEach(() => {
|
|
saved = process.env[KEY];
|
|
delete process.env[KEY];
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (saved === undefined) delete process.env[KEY];
|
|
else process.env[KEY] = saved;
|
|
});
|
|
|
|
describe("isSmsRecipientAllowed", () => {
|
|
it("allows everyone when the variable is absent", () => {
|
|
assert.equal(smsConfig().allowedRecipientDids, undefined);
|
|
assert.equal(isSmsRecipientAllowed(MINE), true);
|
|
assert.equal(isSmsRecipientAllowed(THEIRS), true);
|
|
});
|
|
|
|
it("allows only the listed DIDs when it is set", () => {
|
|
process.env[KEY] = MINE;
|
|
assert.equal(isSmsRecipientAllowed(MINE), true);
|
|
assert.equal(isSmsRecipientAllowed(THEIRS), false);
|
|
});
|
|
|
|
it("accepts a comma-separated list with untidy spacing", () => {
|
|
process.env[KEY] = ` ${MINE} , ${THEIRS} ,`;
|
|
assert.deepEqual(smsConfig().allowedRecipientDids, [MINE, THEIRS]);
|
|
assert.equal(isSmsRecipientAllowed(MINE), true);
|
|
assert.equal(isSmsRecipientAllowed(THEIRS), true);
|
|
assert.equal(isSmsRecipientAllowed("did:ethr:0x999"), false);
|
|
});
|
|
|
|
it("compares case-insensitively, so a checksummed address still matches", () => {
|
|
process.env[KEY] = "did:ethr:0xAbCdEf";
|
|
assert.equal(isSmsRecipientAllowed("did:ethr:0xabcdef"), true);
|
|
assert.equal(isSmsRecipientAllowed("DID:ETHR:0XABCDEF"), true);
|
|
});
|
|
|
|
it("blocks everyone when it is set but empty, rather than allowing everyone", () => {
|
|
process.env[KEY] = "";
|
|
assert.deepEqual(smsConfig().allowedRecipientDids, []);
|
|
assert.equal(isSmsRecipientAllowed(MINE), false);
|
|
});
|
|
|
|
it("blocks everyone when it holds only separators", () => {
|
|
process.env[KEY] = " , , ";
|
|
assert.equal(isSmsRecipientAllowed(MINE), false);
|
|
});
|
|
});
|