153 lines
4.1 KiB
JavaScript
153 lines
4.1 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const UserService = require('../../../../app/service/systems/auth/user');
|
|
|
|
function createServiceHarness() {
|
|
const cacheWrites = [];
|
|
const cacheStore = new Map();
|
|
|
|
const app = {
|
|
async setCacheAsync(cacheKey, data, expireSeconds = 300) {
|
|
cacheWrites.push({
|
|
cacheKey,
|
|
data,
|
|
expireSeconds,
|
|
});
|
|
|
|
if (data === null || data === undefined || expireSeconds <= 1) {
|
|
cacheStore.delete(cacheKey);
|
|
return;
|
|
}
|
|
|
|
cacheStore.set(cacheKey, data);
|
|
},
|
|
async getCacheAsync(cacheKey) {
|
|
return cacheStore.has(cacheKey) ? cacheStore.get(cacheKey) : null;
|
|
},
|
|
coreLogger: {
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
app,
|
|
userId: 1001,
|
|
user: {
|
|
id: 1001,
|
|
},
|
|
};
|
|
|
|
const service = new UserService(ctx);
|
|
|
|
return {
|
|
app,
|
|
ctx,
|
|
service,
|
|
cacheWrites,
|
|
cacheStore,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/auth.user.bindWechatCacheExpiry.test.js', () => {
|
|
it('should generate pending code with fixed 60s ttl and keep same code without extending', async () => {
|
|
const fixedNow = 1760000000000;
|
|
const originalNow = Date.now;
|
|
Date.now = () => fixedNow;
|
|
|
|
try {
|
|
const { service, cacheWrites } = createServiceHarness();
|
|
service.findYe4sBindingByAccountId = async () => null;
|
|
|
|
const first = await service.refreshWechatBindState(1001, null, {
|
|
forceNewCode: false,
|
|
});
|
|
|
|
assert.equal(first.state.status, 'pending');
|
|
assert.match(String(first.state.code || ''), /^(?=.*[A-Z])(?=.*\d)[A-Z\d]{6}$/);
|
|
assert.equal(first.state.expires_at, fixedNow + 60000);
|
|
|
|
const firstWriteCount = cacheWrites.length;
|
|
assert.equal(firstWriteCount, 2);
|
|
for (const write of cacheWrites) {
|
|
assert.equal(write.expireSeconds, 60);
|
|
}
|
|
|
|
const second = await service.refreshWechatBindState(1001, first.state, {
|
|
forceNewCode: false,
|
|
});
|
|
|
|
assert.equal(second.state.code, first.state.code);
|
|
assert.equal(second.state.status, 'pending');
|
|
assert.equal(cacheWrites.length, firstWriteCount);
|
|
} finally {
|
|
Date.now = originalNow;
|
|
}
|
|
});
|
|
|
|
it('should return scanned status when scan key exists', async () => {
|
|
const fixedNow = 1760000000000;
|
|
const originalNow = Date.now;
|
|
Date.now = () => fixedNow;
|
|
|
|
try {
|
|
const { service, cacheStore } = createServiceHarness();
|
|
service.findYe4sBindingByAccountId = async () => null;
|
|
|
|
const pending = await service.refreshWechatBindState(1001, null, {
|
|
forceNewCode: false,
|
|
});
|
|
const code = pending.state.code;
|
|
|
|
cacheStore.set(`user:wechat:bind:scan:${code}`, {
|
|
account_id: 1001,
|
|
code,
|
|
status: 'scanned',
|
|
scanned_at: new Date(fixedNow + 1000).toISOString(),
|
|
});
|
|
|
|
const result = await service.refreshWechatBindState(1001, pending.state, {
|
|
forceNewCode: false,
|
|
});
|
|
|
|
assert.equal(result.state.status, 'scanned');
|
|
assert.equal(result.state.code, code);
|
|
assert.ok(result.state.scanned_at);
|
|
} finally {
|
|
Date.now = originalNow;
|
|
}
|
|
});
|
|
|
|
it('should rotate code when forceNewCode is enabled', async () => {
|
|
const fixedNow = 1760000000000;
|
|
const originalNow = Date.now;
|
|
Date.now = () => fixedNow;
|
|
|
|
try {
|
|
const { service, cacheWrites } = createServiceHarness();
|
|
service.findYe4sBindingByAccountId = async () => null;
|
|
|
|
const oldState = {
|
|
account_id: 1001,
|
|
status: 'pending',
|
|
code: 'A1B2C3',
|
|
expires_at: fixedNow + 30000,
|
|
};
|
|
|
|
const result = await service.refreshWechatBindState(1001, oldState, {
|
|
forceNewCode: true,
|
|
});
|
|
|
|
assert.equal(result.state.status, 'pending');
|
|
assert.notEqual(result.state.code, oldState.code);
|
|
assert.equal(result.state.expires_at, fixedNow + 60000);
|
|
|
|
const oldCodeInvalidation = cacheWrites.find(item => item && item.data === null && item.expireSeconds === 1);
|
|
assert.ok(oldCodeInvalidation);
|
|
} finally {
|
|
Date.now = originalNow;
|
|
}
|
|
});
|
|
});
|