chore: debug log 仅输出app的log fix(commons): fix sanitize not return value. feat(webhooks): add gitea webhooks api. Co-authored-by: Ivan Li <ivanli@live.cn> Co-authored-by: Ivan <ivanli@live.cn> Reviewed-on: #2 Co-Authored-By: Ivan Li <ivan@noreply.%(DOMAIN)s> Co-Committed-By: Ivan Li <ivan@noreply.%(DOMAIN)s>
58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
import { UnauthorizedException } from '@nestjs/common';
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { getRepositoryToken } from '@nestjs/typeorm';
|
|
import { readFile } from 'fs/promises';
|
|
import { join } from 'path';
|
|
import { Repository } from 'typeorm';
|
|
import { PipelineTasksService } from '../pipeline-tasks/pipeline-tasks.service';
|
|
import { WebhookLog } from './webhook-log.entity';
|
|
import { WebhooksService } from './webhooks.service';
|
|
|
|
describe('WebhooksService', () => {
|
|
let service: WebhooksService;
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
WebhooksService,
|
|
{
|
|
provide: PipelineTasksService,
|
|
useValue: {},
|
|
},
|
|
{
|
|
provide: getRepositoryToken(WebhookLog),
|
|
useValue: new Repository(),
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<WebhooksService>(WebhooksService);
|
|
});
|
|
|
|
it('should be defined', () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe('verifySignature', () => {
|
|
const signature =
|
|
'b175e07189a6106f386b62253b18b5879c4b1f3af2f11fe13a294602671e361a';
|
|
const secret = 'boardcat';
|
|
let payload: Buffer;
|
|
beforeAll(async () => {
|
|
payload = await readFile(
|
|
join(__dirname, '../../test/data/gitea-hook-payload.json.bin'),
|
|
);
|
|
});
|
|
it('must be valid', async () => {
|
|
await expect(
|
|
service.verifySignature(payload, signature, secret),
|
|
).resolves.toEqual(undefined);
|
|
});
|
|
it('must be invalid', async () => {
|
|
await expect(
|
|
service.verifySignature(payload, 'test', secret),
|
|
).rejects.toThrowError(UnauthorizedException);
|
|
});
|
|
});
|
|
});
|