forked from wangziqi/gongxue-base
test: add remote SMS login smoke
This commit is contained in:
90
scripts/remote-sms-login-smoke-test.js
Normal file
90
scripts/remote-sms-login-smoke-test.js
Normal file
@@ -0,0 +1,90 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import http from 'node:http';
|
||||
import { runRemoteSmsLoginSmoke } from './remote-sms-login-smoke.js';
|
||||
|
||||
const tenantId = '00000000-0000-0000-0000-000000000001';
|
||||
const phone = '13800138000';
|
||||
|
||||
function json(res, status, payload) {
|
||||
res.writeHead(status, { 'content-type': 'application/json' });
|
||||
res.end(JSON.stringify(payload));
|
||||
}
|
||||
|
||||
function bodyJson(req) {
|
||||
return new Promise(resolve => {
|
||||
const chunks = [];
|
||||
req.on('data', chunk => chunks.push(chunk));
|
||||
req.on('end', () => {
|
||||
try {
|
||||
resolve(JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}'));
|
||||
} catch {
|
||||
resolve({});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const seen = [];
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const url = new URL(req.url || '/', 'http://127.0.0.1');
|
||||
seen.push({ path: url.pathname, method: req.method, tenantId: req.headers['x-tenant-id'], origin: req.headers.origin });
|
||||
|
||||
if (url.pathname === '/api/auth/sms/send' && req.method === 'POST') {
|
||||
const body = await bodyJson(req);
|
||||
assert.equal(body.phone, phone);
|
||||
assert.equal(body.purpose, 'login');
|
||||
json(res, 200, {
|
||||
item: { id: 'sms-id', phone, purpose: 'login', provider: 'aliyun-pnvs', status: 'sent' },
|
||||
expireIn: 300,
|
||||
cooldown: 60,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.pathname === '/api/auth/sms/verify' && req.method === 'POST') {
|
||||
const body = await bodyJson(req);
|
||||
assert.equal(body.phone, phone);
|
||||
assert.equal(body.code, '123456');
|
||||
json(res, 200, {
|
||||
user: { id: 'user-id', phone },
|
||||
session: { token: 'session-token' },
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.pathname === '/api/auth/me' && req.method === 'GET') {
|
||||
assert.equal(req.headers.authorization, 'Bearer session-token');
|
||||
json(res, 200, { user: { id: 'user-id', phone } });
|
||||
return;
|
||||
}
|
||||
|
||||
json(res, 404, { code: 'NOT_FOUND', path: url.pathname });
|
||||
});
|
||||
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
|
||||
try {
|
||||
const address = server.address();
|
||||
const result = await runRemoteSmsLoginSmoke(
|
||||
{
|
||||
apiBaseUrl: `http://127.0.0.1:${address.port}`,
|
||||
tenantId,
|
||||
phone,
|
||||
origin: 'https://admin.tjszsb.com',
|
||||
purpose: 'login',
|
||||
code: '123456',
|
||||
timeoutMs: 5000,
|
||||
skipSend: false,
|
||||
skipMe: false,
|
||||
},
|
||||
{ quiet: true },
|
||||
);
|
||||
|
||||
assert.equal(result.sessionToken, 'session-token');
|
||||
assert.equal(seen.some(item => item.path === '/api/auth/sms/send' && item.tenantId === tenantId), true);
|
||||
assert.equal(seen.some(item => item.path === '/api/auth/sms/verify' && item.origin === 'https://admin.tjszsb.com'), true);
|
||||
assert.equal(seen.some(item => item.path === '/api/auth/me'), true);
|
||||
console.log('[PASS] remote SMS login smoke script');
|
||||
} finally {
|
||||
await new Promise(resolve => server.close(resolve));
|
||||
}
|
||||
Reference in New Issue
Block a user