feat(M03-D): 完成场景码NFC与受控WiFi
This commit is contained in:
@@ -36,6 +36,9 @@ const contentVerifySql = read('database/migrations/2026061808_m03b_decoration_ad
|
||||
const discoveryUpSql = read('database/migrations/2026061809_m03c_store_discovery.up.sql');
|
||||
const discoveryDownSql = read('database/migrations/2026061809_m03c_store_discovery.down.sql');
|
||||
const discoveryVerifySql = read('database/migrations/2026061809_m03c_store_discovery.verify.sql');
|
||||
const accessUpSql = read('database/migrations/2026061810_m03d_scene_wifi_access.up.sql');
|
||||
const accessDownSql = read('database/migrations/2026061810_m03d_scene_wifi_access.down.sql');
|
||||
const accessVerifySql = read('database/migrations/2026061810_m03d_scene_wifi_access.verify.sql');
|
||||
|
||||
const coreTables = [
|
||||
'qipai_schema_migrations',
|
||||
@@ -170,5 +173,15 @@ for (const column of ['city', 'district']) {
|
||||
}
|
||||
assert.match(discoveryUpSql, /idx_qipai_stores_tenant_city_status/);
|
||||
assert.match(discoveryUpSql, /idx_qipai_stores_tenant_coordinates/);
|
||||
for (const table of [
|
||||
'qipai_scene_codes', 'qipai_scene_scan_events', 'qipai_order_user_access'
|
||||
]) {
|
||||
assert.match(accessUpSql, new RegExp(`CREATE TABLE IF NOT EXISTS ${table}`));
|
||||
assert.match(accessDownSql, new RegExp(`DROP TABLE IF EXISTS ${table}`));
|
||||
assert.match(accessVerifySql, new RegExp(`'${table}'`));
|
||||
}
|
||||
assert.match(accessUpSql, /scan_count BIGINT UNSIGNED/);
|
||||
assert.match(accessUpSql, /target_type = 'STORE'/);
|
||||
assert.match(accessUpSql, /PRIMARY KEY \(tenant_id, order_id, user_id\)/);
|
||||
|
||||
console.log('PASS: M01-B through M03-C migration contracts are present.');
|
||||
console.log('PASS: M01-B through M03-D migration contracts are present.');
|
||||
|
||||
@@ -20,7 +20,8 @@ assert.match(plan.file, /2026061805_m02c_rbac\.up\.sql/);
|
||||
assert.match(plan.file, /2026061806_m02d_user_management\.up\.sql/);
|
||||
assert.match(plan.file, /2026061807_m03a_store_room_domain\.up\.sql/);
|
||||
assert.match(plan.file, /2026061808_m03b_decoration_ads_media\.up\.sql/);
|
||||
assert.match(plan.file, /2026061809_m03c_store_discovery\.up\.sql$/);
|
||||
assert.match(plan.file, /2026061809_m03c_store_discovery\.up\.sql/);
|
||||
assert.match(plan.file, /2026061810_m03d_scene_wifi_access\.up\.sql$/);
|
||||
assert.match(plan.checksum, /^[a-f0-9]{64}$/);
|
||||
assert.ok(plan.statements.length >= 11);
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { buildApp } from '../dist/app.js';
|
||||
import { signAccessToken } from '../dist/auth/jwt.js';
|
||||
import { StoreAccessError, StoreAccessRepository } from '../dist/stores/access-repository.js';
|
||||
|
||||
const transactionCalls = [];
|
||||
const connection = {
|
||||
async beginTransaction() { transactionCalls.push('begin'); },
|
||||
async commit() { transactionCalls.push('commit'); },
|
||||
async rollback() { transactionCalls.push('rollback'); },
|
||||
release() { transactionCalls.push('release'); },
|
||||
async execute(sql) {
|
||||
if (sql.includes('FROM qipai_scene_codes') && sql.includes('code = ?')) {
|
||||
return [[{
|
||||
id: 1, tenantId: 7, targetType: 'ROOM', storeId: 11,
|
||||
roomId: 31, generation: 1, scanCount: 0
|
||||
}], []];
|
||||
}
|
||||
return [{ affectedRows: 1 }, []];
|
||||
}
|
||||
};
|
||||
const repository = new StoreAccessRepository({
|
||||
async getConnection() { return connection; },
|
||||
async execute(sql) {
|
||||
if (sql.includes('qipai_order_user_access')) return [[{ total: 0 }], []];
|
||||
return [[], []];
|
||||
}
|
||||
});
|
||||
const resolved = await repository.resolveScene({
|
||||
code: 'abcdefghijklmnop', sourceType: 'NFC', traceId: 'trace',
|
||||
ip: '127.0.0.1', userAgent: 'test'
|
||||
});
|
||||
assert.equal(resolved.page, '/pages/room/detail');
|
||||
assert.deepEqual(resolved.permissions, []);
|
||||
assert.ok(transactionCalls.includes('commit'));
|
||||
await assert.rejects(
|
||||
() => repository.getWifi({
|
||||
tenantId: '7', userId: '21',
|
||||
access: { roles: ['CUSTOMER'], capabilities: [], storeIds: [] },
|
||||
storeId: '11', traceId: 'trace', ip: '127.0.0.1', userAgent: 'test'
|
||||
}),
|
||||
(error) => error instanceof StoreAccessError && error.code === 'WIFI_ACCESS_FORBIDDEN'
|
||||
);
|
||||
|
||||
const secret = 'test-only-jwt-secret-with-at-least-32-characters';
|
||||
const token = signAccessToken({
|
||||
sub: '21', sid: '5c4d3af8-c63c-4edb-bf95-b84127bb3f6e',
|
||||
tid: '7', aid: '9', rv: 1
|
||||
}, secret, 900);
|
||||
const app = await buildApp({
|
||||
storeAccess: {
|
||||
jwtSecret: secret,
|
||||
authRepository: {
|
||||
async validateSession() {
|
||||
return {
|
||||
id: '5c4d3af8-c63c-4edb-bf95-b84127bb3f6e',
|
||||
tenantId: '7', platformAppId: '9', expiresAt: new Date(Date.now() + 60000),
|
||||
user: {
|
||||
id: '21', tenantId: '7', userType: 'STAFF', status: 'ACTIVE',
|
||||
roleVersion: 1, nickname: '', avatarUrl: '', phone: ''
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
accessControl: {
|
||||
async getAccessProfile() {
|
||||
return { roles: ['TENANT_ADMIN'], capabilities: ['tenant.manage'], storeIds: [] };
|
||||
}
|
||||
},
|
||||
repository: {
|
||||
async regenerateScene() { return { sceneCodeId: '1', code: 'abcdefghijklmnop', generation: 1 }; },
|
||||
async revokeScene() { return { sceneCodeId: '1', revoked: true }; },
|
||||
async resolveScene() {
|
||||
return {
|
||||
targetType: 'STORE', storeId: '11', roomId: null,
|
||||
page: '/pages/store/detail', permissions: []
|
||||
};
|
||||
},
|
||||
async sceneStats() { return []; },
|
||||
async getWifi() { return { ssid: 'QIPAI', password: '<test-only>' }; }
|
||||
}
|
||||
}
|
||||
});
|
||||
const generated = await app.inject({
|
||||
method: 'POST', url: '/admin-api/scene-codes/regenerate',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { targetType: 'STORE', storeId: '11' }
|
||||
});
|
||||
assert.equal(generated.statusCode, 201);
|
||||
const scan = await app.inject({
|
||||
method: 'POST', url: '/app-api/scenes/resolve',
|
||||
payload: { code: 'abcdefghijklmnop', sourceType: 'QRCODE' }
|
||||
});
|
||||
assert.equal(scan.statusCode, 200);
|
||||
assert.deepEqual(scan.json().data.permissions, []);
|
||||
await app.close();
|
||||
|
||||
console.log('PASS: M03-D scene navigation has no door permission and Wi-Fi access is guarded.');
|
||||
Reference in New Issue
Block a user