test(contract): add center-guild registration contract integration test
This commit is contained in:
@@ -2,13 +2,14 @@ import { INestApplication } from '@nestjs/common';
|
|||||||
import { Test } from '@nestjs/testing';
|
import { Test } from '@nestjs/testing';
|
||||||
import request from 'supertest';
|
import request from 'supertest';
|
||||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||||
|
import { createHmac, randomUUID } from 'crypto';
|
||||||
|
|
||||||
process.env.DB_HOST = '127.0.0.1';
|
process.env.DB_HOST = '127.0.0.1';
|
||||||
process.env.DB_PORT = '3307';
|
process.env.DB_PORT = '3307';
|
||||||
process.env.DB_USER = 'fabric';
|
process.env.DB_USER = 'fabric';
|
||||||
process.env.DB_PASSWORD = 'fabric';
|
process.env.DB_PASSWORD = 'fabric';
|
||||||
process.env.DB_NAME = 'fabric_center';
|
process.env.DB_NAME = 'fabric_center';
|
||||||
process.env.DB_SYNC = 'false';
|
process.env.DB_SYNC = 'true';
|
||||||
process.env.CENTER_SHARED_SECRET = 'test-center-secret';
|
process.env.CENTER_SHARED_SECRET = 'test-center-secret';
|
||||||
process.env.JWT_ACCESS_SECRET = 'test-access-secret';
|
process.env.JWT_ACCESS_SECRET = 'test-access-secret';
|
||||||
process.env.JWT_REFRESH_SECRET = 'test-refresh-secret';
|
process.env.JWT_REFRESH_SECRET = 'test-refresh-secret';
|
||||||
@@ -37,4 +38,32 @@ describe('center integration (mysql + api)', () => {
|
|||||||
expect(res.body.ok).toBe(true);
|
expect(res.body.ok).toBe(true);
|
||||||
expect(res.body.database).toBe('ready');
|
expect(res.body.database).toBe('ready');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('POST /api/nodes/register follows center-guild contract (version + hmac)', async () => {
|
||||||
|
const body = {
|
||||||
|
nodeId: `guild-node-${Date.now()}`,
|
||||||
|
name: 'Guild Node Contract Test',
|
||||||
|
endpoint: `http://guild-${Date.now()}:7002`,
|
||||||
|
};
|
||||||
|
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
const nonce = randomUUID();
|
||||||
|
const canonical = ['POST', '/api/nodes/register', timestamp, nonce, JSON.stringify(body)].join('\n');
|
||||||
|
const signature = createHmac('sha256', process.env.CENTER_SHARED_SECRET as string)
|
||||||
|
.update(canonical)
|
||||||
|
.digest('hex');
|
||||||
|
|
||||||
|
const res = await request(app.getHttpServer())
|
||||||
|
.post('/api/nodes/register')
|
||||||
|
.set('x-fabric-version', '1')
|
||||||
|
.set('x-fabric-timestamp', timestamp)
|
||||||
|
.set('x-fabric-nonce', nonce)
|
||||||
|
.set('x-fabric-signature', signature)
|
||||||
|
.send(body);
|
||||||
|
|
||||||
|
expect(res.status).toBe(201);
|
||||||
|
expect(res.body.status).toBe('accepted');
|
||||||
|
expect(res.body.negotiatedVersion).toBe('1');
|
||||||
|
expect(res.body.node.nodeId).toBe(body.nodeId);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -83,7 +83,7 @@
|
|||||||
### 5.1 自动化测试
|
### 5.1 自动化测试
|
||||||
- [x] 单元测试(auth/service/message/seq)
|
- [x] 单元测试(auth/service/message/seq)
|
||||||
- [x] 集成测试(MySQL + API)
|
- [x] 集成测试(MySQL + API)
|
||||||
- [ ] 合约测试(Center-Guild 协议)
|
- [x] 合约测试(Center-Guild 协议)
|
||||||
|
|
||||||
### 5.2 质量门禁
|
### 5.2 质量门禁
|
||||||
- [x] lint/typecheck/build 全绿
|
- [x] lint/typecheck/build 全绿
|
||||||
|
|||||||
Reference in New Issue
Block a user