feat(M03-D): 完成场景码NFC与受控WiFi
This commit is contained in:
@@ -16,6 +16,7 @@ import { UserManagementRepository } from '../dist/auth/user-management-repositor
|
||||
import { StoreRoomRepository, StoreRoomError } from '../dist/stores/store-room-repository.js';
|
||||
import { ContentRepository, ContentError } from '../dist/content/content-repository.js';
|
||||
import { StoreDiscoveryRepository } from '../dist/stores/store-discovery-repository.js';
|
||||
import { StoreAccessRepository, StoreAccessError } from '../dist/stores/access-repository.js';
|
||||
import {
|
||||
executeMigrationPlan,
|
||||
loadMigrationPlan,
|
||||
@@ -31,6 +32,7 @@ const expectedTables = [
|
||||
'qipai_legacy_table_mappings',
|
||||
'qipai_media_assets',
|
||||
'qipai_members',
|
||||
'qipai_order_user_access',
|
||||
'qipai_orders',
|
||||
'qipai_outbox_events',
|
||||
'qipai_payments',
|
||||
@@ -41,6 +43,8 @@ const expectedTables = [
|
||||
'qipai_room_categories',
|
||||
'qipai_room_disabled_periods',
|
||||
'qipai_rooms',
|
||||
'qipai_scene_codes',
|
||||
'qipai_scene_scan_events',
|
||||
'qipai_schema_migrations',
|
||||
'qipai_store_business_hours',
|
||||
'qipai_store_decorations',
|
||||
@@ -73,10 +77,11 @@ async function readMigrationVersions(pool) {
|
||||
const [rows] = await pool.query(
|
||||
`SELECT version, name
|
||||
FROM qipai_schema_migrations
|
||||
WHERE version IN (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
WHERE version IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ORDER BY version`,
|
||||
['2026061601', '2026061802', '2026061803', '2026061804',
|
||||
'2026061805', '2026061806', '2026061807', '2026061808', '2026061809']
|
||||
'2026061805', '2026061806', '2026061807', '2026061808', '2026061809',
|
||||
'2026061810']
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
@@ -425,6 +430,103 @@ async function assertStoreDiscovery(pool, context) {
|
||||
})).length, 0);
|
||||
}
|
||||
|
||||
async function assertSceneAndWifiAccess(pool, context) {
|
||||
const [adminRows] = await pool.query(
|
||||
`SELECT u.id FROM qipai_users u
|
||||
INNER JOIN qipai_user_roles ur ON ur.tenant_id = u.tenant_id AND ur.user_id = u.id
|
||||
INNER JOIN qipai_roles r ON r.id = ur.role_id AND r.tenant_id = ur.tenant_id
|
||||
WHERE u.tenant_id = ? AND r.code = 'TENANT_ADMIN' LIMIT 1`,
|
||||
[context.tenantId]
|
||||
);
|
||||
const [customerRows] = await pool.query(
|
||||
`SELECT u.id FROM qipai_users u
|
||||
INNER JOIN qipai_user_identities i
|
||||
ON i.tenant_id = u.tenant_id AND i.user_id = u.id
|
||||
WHERE u.tenant_id = ? AND i.openid = 'm02b-openid-a' LIMIT 1`,
|
||||
[context.tenantId]
|
||||
);
|
||||
const [targetRows] = await pool.query(
|
||||
`SELECT s.id AS storeId, r.id AS roomId
|
||||
FROM qipai_stores s
|
||||
INNER JOIN qipai_rooms r ON r.tenant_id = s.tenant_id AND r.store_id = s.id
|
||||
WHERE s.tenant_id = ? AND s.name = 'M03A Store' LIMIT 1`,
|
||||
[context.tenantId]
|
||||
);
|
||||
const adminId = String(adminRows[0].id);
|
||||
const customerId = String(customerRows[0].id);
|
||||
const storeId = String(targetRows[0].storeId);
|
||||
const roomId = String(targetRows[0].roomId);
|
||||
const rbac = new RbacRepository(pool);
|
||||
const adminAccess = await rbac.getAccessProfile(context.tenantId, adminId);
|
||||
const customerAccess = await rbac.getAccessProfile(context.tenantId, customerId);
|
||||
const actor = {
|
||||
tenantId: context.tenantId, userId: adminId, access: adminAccess,
|
||||
traceId: 'm03d-live-test', ip: '127.0.0.1', userAgent: 'M03-D live test'
|
||||
};
|
||||
const repository = new StoreAccessRepository(pool);
|
||||
const first = await repository.regenerateScene(actor, {
|
||||
targetType: 'ROOM', storeId, roomId
|
||||
});
|
||||
const firstResolved = await repository.resolveScene({
|
||||
code: first.code, sourceType: 'QRCODE', traceId: 'm03d-scan-1',
|
||||
ip: '127.0.0.1', userAgent: 'M03-D scan'
|
||||
});
|
||||
assert.equal(firstResolved.roomId, roomId);
|
||||
assert.deepEqual(firstResolved.permissions, []);
|
||||
const second = await repository.regenerateScene(actor, {
|
||||
targetType: 'ROOM', storeId, roomId
|
||||
});
|
||||
assert.equal(second.generation, 2);
|
||||
await assert.rejects(
|
||||
() => repository.resolveScene({
|
||||
code: first.code, sourceType: 'NFC', traceId: 'm03d-old-code',
|
||||
ip: '127.0.0.1', userAgent: 'M03-D old code'
|
||||
}),
|
||||
(error) => error instanceof StoreAccessError && error.code === 'SCENE_CODE_INVALID'
|
||||
);
|
||||
await repository.resolveScene({
|
||||
code: second.code, sourceType: 'NFC', traceId: 'm03d-scan-2',
|
||||
ip: '127.0.0.1', userAgent: 'M03-D NFC'
|
||||
});
|
||||
assert.equal((await repository.sceneStats(actor, storeId))[0].scanCount, 1);
|
||||
|
||||
await assert.rejects(
|
||||
() => repository.getWifi({
|
||||
tenantId: context.tenantId, userId: customerId, access: customerAccess, storeId,
|
||||
traceId: 'm03d-wifi-denied', ip: '127.0.0.1', userAgent: 'M03-D denied'
|
||||
}),
|
||||
(error) => error instanceof StoreAccessError && error.code === 'WIFI_ACCESS_FORBIDDEN'
|
||||
);
|
||||
const [orderResult] = await pool.query(
|
||||
`INSERT INTO qipai_orders
|
||||
(tenant_id, store_id, room_id, order_no, status, start_at, end_at)
|
||||
VALUES (?, ?, ?, 'M03D-WIFI-ORDER', 'IN_USE',
|
||||
DATE_SUB(UTC_TIMESTAMP(3), INTERVAL 10 MINUTE),
|
||||
DATE_ADD(UTC_TIMESTAMP(3), INTERVAL 50 MINUTE))`,
|
||||
[context.tenantId, storeId, roomId]
|
||||
);
|
||||
await pool.query(
|
||||
`INSERT INTO qipai_order_user_access (tenant_id, order_id, user_id)
|
||||
VALUES (?, ?, ?)`,
|
||||
[context.tenantId, orderResult.insertId, customerId]
|
||||
);
|
||||
const wifi = await repository.getWifi({
|
||||
tenantId: context.tenantId, userId: customerId, access: customerAccess, storeId,
|
||||
traceId: 'm03d-wifi-allowed', ip: '127.0.0.1', userAgent: 'M03-D allowed'
|
||||
});
|
||||
assert.equal(wifi.ssid, 'M03A-WIFI');
|
||||
assert.equal(wifi.password, 'sanitized-password');
|
||||
const [auditRows] = await pool.query(
|
||||
`SELECT CAST(metadata AS CHAR) AS metadata
|
||||
FROM qipai_audit_logs
|
||||
WHERE tenant_id = ? AND trace_id = 'm03d-wifi-allowed'`,
|
||||
[context.tenantId]
|
||||
);
|
||||
assert.equal(auditRows.length, 1);
|
||||
assert.match(auditRows[0].metadata, /M03A-WIFI/);
|
||||
assert.doesNotMatch(auditRows[0].metadata, /sanitized-password/);
|
||||
}
|
||||
|
||||
async function assertContentManagement(pool, context) {
|
||||
const [adminRows] = await pool.query(
|
||||
`SELECT u.id FROM qipai_users u
|
||||
@@ -521,7 +623,8 @@ try {
|
||||
{ version: '2026061806', name: 'm02d_user_management' },
|
||||
{ version: '2026061807', name: 'm03a_store_room_domain' },
|
||||
{ version: '2026061808', name: 'm03b_decoration_ads_media' },
|
||||
{ version: '2026061809', name: 'm03c_store_discovery' }
|
||||
{ version: '2026061809', name: 'm03c_store_discovery' },
|
||||
{ version: '2026061810', name: 'm03d_scene_wifi_access' }
|
||||
]);
|
||||
await assertTaskDurability(pool);
|
||||
const loginContext = await assertPlatformTenantIsolation(pool);
|
||||
@@ -530,6 +633,7 @@ try {
|
||||
await assertStoreRoomDomain(pool, loginContext);
|
||||
await assertContentManagement(pool, loginContext);
|
||||
await assertStoreDiscovery(pool, loginContext);
|
||||
await assertSceneAndWifiAccess(pool, loginContext);
|
||||
await assertLegacyCompatibility(pool);
|
||||
console.log('PASS: first up, verify, tenant isolation and revocable auth checks completed.');
|
||||
|
||||
@@ -550,7 +654,8 @@ try {
|
||||
{ version: '2026061806', name: 'm02d_user_management' },
|
||||
{ version: '2026061807', name: 'm03a_store_room_domain' },
|
||||
{ version: '2026061808', name: 'm03b_decoration_ads_media' },
|
||||
{ version: '2026061809', name: 'm03c_store_discovery' }
|
||||
{ version: '2026061809', name: 'm03c_store_discovery' },
|
||||
{ version: '2026061810', name: 'm03d_scene_wifi_access' }
|
||||
]);
|
||||
await assertLegacyCompatibility(pool);
|
||||
console.log('PASS: second up and verify restored the schema.');
|
||||
@@ -598,7 +703,13 @@ try {
|
||||
'platform advertisement rejection',
|
||||
'city fallback store filtering',
|
||||
'server-side distance sorting',
|
||||
'empty manual city result'
|
||||
'empty manual city result',
|
||||
'scene regeneration revokes old code',
|
||||
'QR and NFC navigation without permissions',
|
||||
'scene scan statistics',
|
||||
'Wi-Fi denied without active order',
|
||||
'Wi-Fi allowed by active order grant',
|
||||
'Wi-Fi audit excludes password'
|
||||
]
|
||||
}, null, 2));
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user