feat: implement chatbot provider system with Discord integration

Add comprehensive chatbot provider architecture supporting Discord webhooks with extensible design for future Slack and Nextcloud integration. Includes dependency injection, signature verification, comprehensive test suite, and full documentation.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jonathan Flatt
2025-05-27 19:27:49 -05:00
parent 9498935eb8
commit d20f9eec2d
18 changed files with 3967 additions and 12 deletions

View File

@@ -9,6 +9,8 @@ This directory contains the test framework for the Claude Webhook service. The t
/unit # Unit tests for individual components
/controllers # Tests for controllers
/services # Tests for services
/providers # Tests for chatbot providers
/security # Security-focused tests
/utils # Tests for utility functions
/integration # Integration tests between components
/github # GitHub integration tests
@@ -33,6 +35,9 @@ npm test
# Run only unit tests
npm run test:unit
# Run only chatbot provider tests
npm run test:chatbot
# Run only integration tests
npm run test:integration
@@ -52,14 +57,25 @@ npm run test:watch
Unit tests focus on testing individual components in isolation. They use Jest's mocking capabilities to replace dependencies with test doubles. These tests are fast and reliable, making them ideal for development and CI/CD pipelines.
#### Chatbot Provider Tests
The chatbot provider system includes comprehensive unit tests for:
- **Base Provider Interface** (`ChatbotProvider.test.js`): Tests the abstract base class and inheritance patterns
- **Discord Provider** (`DiscordProvider.test.js`): Tests Discord-specific webhook handling, signature verification, and message parsing
- **Provider Factory** (`ProviderFactory.test.js`): Tests dependency injection and provider management
- **Security Tests** (`signature-verification.test.js`): Tests webhook signature verification and security edge cases
- **Payload Tests** (`discord-payloads.test.js`): Tests real Discord webhook payloads and edge cases
Example:
```javascript
// Test for awsCredentialProvider.js
describe('AWS Credential Provider', () => {
test('should get credentials from AWS profile', async () => {
const credentials = await awsCredentialProvider.getCredentials();
expect(credentials).toBeDefined();
// Test for DiscordProvider.js
describe('Discord Provider', () => {
test('should parse Discord slash command correctly', () => {
const payload = { type: 2, data: { name: 'claude' } };
const result = provider.parseWebhookPayload(payload);
expect(result.type).toBe('command');
});
});
```

View File

@@ -0,0 +1,355 @@
const request = require('supertest');
const express = require('express');
const bodyParser = require('body-parser');
const chatbotRoutes = require('../../../src/routes/chatbot');
// Mock dependencies
jest.mock('../../../src/controllers/chatbotController', () => ({
handleDiscordWebhook: jest.fn(),
handleSlackWebhook: jest.fn(),
handleNextcloudWebhook: jest.fn(),
getProviderStats: jest.fn()
}));
const chatbotController = require('../../../src/controllers/chatbotController');
describe('Chatbot Integration Tests', () => {
let app;
beforeEach(() => {
app = express();
// Middleware to capture raw body for signature verification
app.use(bodyParser.json({
verify: (req, res, buf) => {
req.rawBody = buf;
}
}));
// Mount chatbot routes
app.use('/api/webhooks/chatbot', chatbotRoutes);
jest.clearAllMocks();
});
describe('Discord webhook endpoint', () => {
it('should route to Discord webhook handler', async () => {
chatbotController.handleDiscordWebhook.mockImplementation((req, res) => {
res.status(200).json({ success: true });
});
const discordPayload = {
type: 1 // PING
};
const response = await request(app)
.post('/api/webhooks/chatbot/discord')
.send(discordPayload)
.expect(200);
expect(chatbotController.handleDiscordWebhook).toHaveBeenCalledTimes(1);
expect(response.body).toEqual({ success: true });
});
it('should handle Discord slash command webhook', async () => {
chatbotController.handleDiscordWebhook.mockImplementation((req, res) => {
res.status(200).json({
success: true,
message: 'Command processed successfully',
context: {
provider: 'discord',
userId: 'user123'
}
});
});
const slashCommandPayload = {
type: 2, // APPLICATION_COMMAND
data: {
name: 'claude',
options: [
{
name: 'command',
value: 'help me with this code'
}
]
},
channel_id: '123456789',
member: {
user: {
id: 'user123',
username: 'testuser'
}
},
token: 'interaction_token',
id: 'interaction_id'
};
const response = await request(app)
.post('/api/webhooks/chatbot/discord')
.set('x-signature-ed25519', 'mock_signature')
.set('x-signature-timestamp', '1234567890')
.send(slashCommandPayload)
.expect(200);
expect(chatbotController.handleDiscordWebhook).toHaveBeenCalledTimes(1);
expect(response.body.success).toBe(true);
});
it('should handle Discord component interaction webhook', async () => {
chatbotController.handleDiscordWebhook.mockImplementation((req, res) => {
res.status(200).json({ success: true });
});
const componentPayload = {
type: 3, // MESSAGE_COMPONENT
data: {
custom_id: 'help_button'
},
channel_id: '123456789',
user: {
id: 'user123',
username: 'testuser'
},
token: 'interaction_token',
id: 'interaction_id'
};
await request(app)
.post('/api/webhooks/chatbot/discord')
.send(componentPayload)
.expect(200);
expect(chatbotController.handleDiscordWebhook).toHaveBeenCalledTimes(1);
});
it('should pass raw body for signature verification', async () => {
chatbotController.handleDiscordWebhook.mockImplementation((req, res) => {
// Verify that req.rawBody is available
expect(req.rawBody).toBeInstanceOf(Buffer);
res.status(200).json({ success: true });
});
await request(app)
.post('/api/webhooks/chatbot/discord')
.send({ type: 1 });
expect(chatbotController.handleDiscordWebhook).toHaveBeenCalledTimes(1);
});
});
describe('Slack webhook endpoint', () => {
it('should route to Slack webhook handler', async () => {
chatbotController.handleSlackWebhook.mockImplementation((req, res) => {
res.status(200).json({ success: true });
});
const slackPayload = {
type: 'url_verification',
challenge: 'test_challenge'
};
const response = await request(app)
.post('/api/webhooks/chatbot/slack')
.send(slackPayload)
.expect(200);
expect(chatbotController.handleSlackWebhook).toHaveBeenCalledTimes(1);
expect(response.body).toEqual({ success: true });
});
it('should handle Slack slash command webhook', async () => {
chatbotController.handleSlackWebhook.mockImplementation((req, res) => {
res.status(200).json({
success: true,
message: 'Command processed successfully'
});
});
const slashCommandPayload = {
type: 'slash_commands',
command: '/claude',
text: 'help me debug this function',
user_id: 'U1234567',
user_name: 'testuser',
channel_id: 'C1234567',
team_id: 'T1234567'
};
await request(app)
.post('/api/webhooks/chatbot/slack')
.send(slashCommandPayload)
.expect(200);
expect(chatbotController.handleSlackWebhook).toHaveBeenCalledTimes(1);
});
});
describe('Nextcloud webhook endpoint', () => {
it('should route to Nextcloud webhook handler', async () => {
chatbotController.handleNextcloudWebhook.mockImplementation((req, res) => {
res.status(200).json({ success: true });
});
const nextcloudPayload = {
type: 'chat_message',
message: '@claude help me with this file',
user: 'testuser',
conversation: 'general'
};
const response = await request(app)
.post('/api/webhooks/chatbot/nextcloud')
.send(nextcloudPayload)
.expect(200);
expect(chatbotController.handleNextcloudWebhook).toHaveBeenCalledTimes(1);
expect(response.body).toEqual({ success: true });
});
});
describe('Provider stats endpoint', () => {
it('should return provider statistics', async () => {
chatbotController.getProviderStats.mockImplementation((req, res) => {
res.json({
success: true,
stats: {
totalRegistered: 3,
totalInitialized: 1,
availableProviders: ['discord', 'slack', 'nextcloud'],
initializedProviders: ['discord']
},
providers: {
discord: {
name: 'DiscordProvider',
initialized: true,
botMention: '@claude'
}
},
timestamp: '2024-01-01T00:00:00.000Z'
});
});
const response = await request(app)
.get('/api/webhooks/chatbot/stats')
.expect(200);
expect(chatbotController.getProviderStats).toHaveBeenCalledTimes(1);
expect(response.body.success).toBe(true);
expect(response.body.stats).toBeDefined();
expect(response.body.providers).toBeDefined();
});
it('should handle stats endpoint errors', async () => {
chatbotController.getProviderStats.mockImplementation((req, res) => {
res.status(500).json({
error: 'Failed to get provider statistics',
message: 'Stats service unavailable'
});
});
const response = await request(app)
.get('/api/webhooks/chatbot/stats')
.expect(500);
expect(response.body.error).toBe('Failed to get provider statistics');
});
});
describe('Error handling', () => {
it('should handle Discord webhook controller errors', async () => {
chatbotController.handleDiscordWebhook.mockImplementation((req, res) => {
res.status(500).json({
error: 'Internal server error',
errorReference: 'err-12345',
timestamp: '2024-01-01T00:00:00.000Z',
provider: 'discord'
});
});
const response = await request(app)
.post('/api/webhooks/chatbot/discord')
.send({ type: 1 })
.expect(500);
expect(response.body.error).toBe('Internal server error');
expect(response.body.errorReference).toBeDefined();
expect(response.body.provider).toBe('discord');
});
it('should handle Slack webhook controller errors', async () => {
chatbotController.handleSlackWebhook.mockImplementation((req, res) => {
res.status(401).json({
error: 'Invalid webhook signature'
});
});
await request(app)
.post('/api/webhooks/chatbot/slack')
.send({ test: 'payload' })
.expect(401);
});
it('should handle invalid JSON payloads', async () => {
// This test ensures that malformed JSON is handled by Express
const response = await request(app)
.post('/api/webhooks/chatbot/discord')
.set('Content-Type', 'application/json')
.send('invalid json{')
.expect(400);
expect(response.body).toMatchObject({
type: expect.any(String)
});
});
it('should handle missing Content-Type', async () => {
chatbotController.handleDiscordWebhook.mockImplementation((req, res) => {
res.status(200).json({ success: true });
});
await request(app)
.post('/api/webhooks/chatbot/discord')
.send('plain text payload')
.expect(200);
});
});
describe('Request validation', () => {
it('should accept valid Discord webhook requests', async () => {
chatbotController.handleDiscordWebhook.mockImplementation((req, res) => {
expect(req.body).toEqual({ type: 1 });
expect(req.headers['content-type']).toContain('application/json');
res.status(200).json({ type: 1 });
});
await request(app)
.post('/api/webhooks/chatbot/discord')
.set('Content-Type', 'application/json')
.send({ type: 1 })
.expect(200);
});
it('should handle large payloads gracefully', async () => {
chatbotController.handleDiscordWebhook.mockImplementation((req, res) => {
res.status(200).json({ success: true });
});
const largePayload = {
type: 2,
data: {
name: 'claude',
options: [{
name: 'command',
value: 'A'.repeat(2000) // Large command
}]
}
};
await request(app)
.post('/api/webhooks/chatbot/discord')
.send(largePayload)
.expect(200);
});
});
});

View File

@@ -0,0 +1,350 @@
const chatbotController = require('../../../src/controllers/chatbotController');
const claudeService = require('../../../src/services/claudeService');
const providerFactory = require('../../../src/providers/ProviderFactory');
// Mock dependencies
jest.mock('../../../src/services/claudeService');
jest.mock('../../../src/providers/ProviderFactory');
jest.mock('../../../src/utils/logger', () => ({
createLogger: () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn()
})
}));
jest.mock('../../../src/utils/sanitize', () => ({
sanitizeBotMentions: jest.fn(msg => msg)
}));
describe('chatbotController', () => {
let req, res, mockProvider;
beforeEach(() => {
req = {
method: 'POST',
path: '/api/webhooks/chatbot/discord',
headers: {
'user-agent': 'Discord-Webhooks/1.0',
'content-type': 'application/json'
},
body: {}
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis()
};
mockProvider = {
verifyWebhookSignature: jest.fn().mockReturnValue(true),
parseWebhookPayload: jest.fn(),
extractBotCommand: jest.fn(),
sendResponse: jest.fn().mockResolvedValue(),
getUserId: jest.fn(),
isUserAuthorized: jest.fn().mockReturnValue(true),
formatErrorMessage: jest.fn().mockReturnValue('Error message'),
getProviderName: jest.fn().mockReturnValue('DiscordProvider'),
getBotMention: jest.fn().mockReturnValue('@claude')
};
providerFactory.getProvider.mockReturnValue(mockProvider);
providerFactory.createFromEnvironment.mockResolvedValue(mockProvider);
providerFactory.getStats.mockReturnValue({
totalRegistered: 1,
totalInitialized: 1,
availableProviders: ['discord'],
initializedProviders: ['discord']
});
providerFactory.getAllProviders.mockReturnValue(new Map([['discord', mockProvider]]));
claudeService.processCommand.mockResolvedValue('Claude response');
jest.clearAllMocks();
});
describe('handleChatbotWebhook', () => {
it('should handle successful webhook with valid signature', async () => {
mockProvider.parseWebhookPayload.mockReturnValue({
type: 'command',
content: 'help me',
userId: 'user123',
username: 'testuser',
channelId: 'channel123'
});
mockProvider.extractBotCommand.mockReturnValue({
command: 'help me',
originalMessage: 'help me'
});
mockProvider.getUserId.mockReturnValue('user123');
await chatbotController.handleChatbotWebhook(req, res, 'discord');
expect(mockProvider.verifyWebhookSignature).toHaveBeenCalledWith(req);
expect(mockProvider.parseWebhookPayload).toHaveBeenCalledWith(req.body);
expect(claudeService.processCommand).toHaveBeenCalledWith({
repoFullName: null,
issueNumber: null,
command: 'help me',
isPullRequest: false,
branchName: null,
chatbotContext: {
provider: 'discord',
userId: 'user123',
username: 'testuser',
channelId: 'channel123',
guildId: undefined
}
});
expect(mockProvider.sendResponse).toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
success: true,
message: 'Command processed successfully'
}));
});
it('should return 401 for invalid webhook signature', async () => {
mockProvider.verifyWebhookSignature.mockReturnValue(false);
await chatbotController.handleChatbotWebhook(req, res, 'discord');
expect(res.status).toHaveBeenCalledWith(401);
expect(res.json).toHaveBeenCalledWith({
error: 'Invalid webhook signature'
});
expect(claudeService.processCommand).not.toHaveBeenCalled();
});
it('should handle signature verification errors', async () => {
mockProvider.verifyWebhookSignature.mockImplementation(() => {
throw new Error('Signature verification failed');
});
await chatbotController.handleChatbotWebhook(req, res, 'discord');
expect(res.status).toHaveBeenCalledWith(401);
expect(res.json).toHaveBeenCalledWith({
error: 'Signature verification failed',
message: 'Signature verification failed'
});
});
it('should handle immediate responses like Discord PING', async () => {
mockProvider.parseWebhookPayload.mockReturnValue({
type: 'ping',
shouldRespond: true,
responseData: { type: 1 }
});
await chatbotController.handleChatbotWebhook(req, res, 'discord');
expect(res.json).toHaveBeenCalledWith({ type: 1 });
expect(claudeService.processCommand).not.toHaveBeenCalled();
});
it('should skip processing for unknown message types', async () => {
mockProvider.parseWebhookPayload.mockReturnValue({
type: 'unknown',
shouldRespond: false
});
await chatbotController.handleChatbotWebhook(req, res, 'discord');
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({
message: 'Webhook received but no command detected'
});
expect(claudeService.processCommand).not.toHaveBeenCalled();
});
it('should skip processing when no bot command is found', async () => {
mockProvider.parseWebhookPayload.mockReturnValue({
type: 'command',
content: 'hello world',
userId: 'user123'
});
mockProvider.extractBotCommand.mockReturnValue(null);
await chatbotController.handleChatbotWebhook(req, res, 'discord');
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({
message: 'Webhook received but no bot mention found'
});
expect(claudeService.processCommand).not.toHaveBeenCalled();
});
it('should handle unauthorized users', async () => {
mockProvider.parseWebhookPayload.mockReturnValue({
type: 'command',
content: 'help me',
userId: 'unauthorized_user',
username: 'baduser'
});
mockProvider.extractBotCommand.mockReturnValue({
command: 'help me'
});
mockProvider.getUserId.mockReturnValue('unauthorized_user');
mockProvider.isUserAuthorized.mockReturnValue(false);
await chatbotController.handleChatbotWebhook(req, res, 'discord');
expect(mockProvider.sendResponse).toHaveBeenCalledWith(
expect.anything(),
'❌ Sorry, only authorized users can trigger Claude commands.'
);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({
message: 'Unauthorized user - command ignored',
context: {
provider: 'discord',
userId: 'unauthorized_user'
}
});
expect(claudeService.processCommand).not.toHaveBeenCalled();
});
it('should handle Claude service errors gracefully', async () => {
mockProvider.parseWebhookPayload.mockReturnValue({
type: 'command',
content: 'help me',
userId: 'user123',
username: 'testuser'
});
mockProvider.extractBotCommand.mockReturnValue({
command: 'help me'
});
mockProvider.getUserId.mockReturnValue('user123');
claudeService.processCommand.mockRejectedValue(new Error('Claude service error'));
await chatbotController.handleChatbotWebhook(req, res, 'discord');
expect(mockProvider.sendResponse).toHaveBeenCalledWith(
expect.anything(),
'Error message'
);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
success: false,
error: 'Failed to process command'
}));
});
it('should handle provider initialization failure', async () => {
providerFactory.getProvider.mockReturnValue(null);
providerFactory.createFromEnvironment.mockRejectedValue(new Error('Provider init failed'));
await chatbotController.handleChatbotWebhook(req, res, 'discord');
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({
error: 'Provider initialization failed',
message: 'Provider init failed'
});
});
it('should handle payload parsing errors', async () => {
mockProvider.parseWebhookPayload.mockImplementation(() => {
throw new Error('Invalid payload');
});
await chatbotController.handleChatbotWebhook(req, res, 'discord');
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({
error: 'Invalid payload format',
message: 'Invalid payload'
});
});
it('should handle unexpected errors', async () => {
providerFactory.getProvider.mockImplementation(() => {
throw new Error('Unexpected error');
});
await chatbotController.handleChatbotWebhook(req, res, 'discord');
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
error: 'Internal server error',
provider: 'discord'
}));
});
});
describe('handleDiscordWebhook', () => {
it('should call handleChatbotWebhook with discord provider', async () => {
const spy = jest.spyOn(chatbotController, 'handleChatbotWebhook');
spy.mockResolvedValue();
await chatbotController.handleDiscordWebhook(req, res);
expect(spy).toHaveBeenCalledWith(req, res, 'discord');
spy.mockRestore();
});
});
describe('handleSlackWebhook', () => {
it('should call handleChatbotWebhook with slack provider', async () => {
const spy = jest.spyOn(chatbotController, 'handleChatbotWebhook');
spy.mockResolvedValue();
await chatbotController.handleSlackWebhook(req, res);
expect(spy).toHaveBeenCalledWith(req, res, 'slack');
spy.mockRestore();
});
});
describe('handleNextcloudWebhook', () => {
it('should call handleChatbotWebhook with nextcloud provider', async () => {
const spy = jest.spyOn(chatbotController, 'handleChatbotWebhook');
spy.mockResolvedValue();
await chatbotController.handleNextcloudWebhook(req, res);
expect(spy).toHaveBeenCalledWith(req, res, 'nextcloud');
spy.mockRestore();
});
});
describe('getProviderStats', () => {
it('should return provider statistics successfully', async () => {
await chatbotController.getProviderStats(req, res);
expect(res.json).toHaveBeenCalledWith({
success: true,
stats: {
totalRegistered: 1,
totalInitialized: 1,
availableProviders: ['discord'],
initializedProviders: ['discord']
},
providers: {
discord: {
name: 'DiscordProvider',
initialized: true,
botMention: '@claude'
}
},
timestamp: expect.any(String)
});
});
it('should handle errors when getting stats', async () => {
providerFactory.getStats.mockImplementation(() => {
throw new Error('Stats error');
});
await chatbotController.getProviderStats(req, res);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({
error: 'Failed to get provider statistics',
message: 'Stats error'
});
});
});
});

View File

@@ -0,0 +1,226 @@
const ChatbotProvider = require('../../../src/providers/ChatbotProvider');
describe('ChatbotProvider', () => {
let provider;
beforeEach(() => {
provider = new ChatbotProvider({
botMention: '@testbot',
authorizedUsers: ['user1', 'user2']
});
});
describe('constructor', () => {
it('should initialize with default config', () => {
const defaultProvider = new ChatbotProvider();
expect(defaultProvider.config).toEqual({});
expect(defaultProvider.name).toBe('ChatbotProvider');
});
it('should initialize with provided config', () => {
expect(provider.config.botMention).toBe('@testbot');
expect(provider.config.authorizedUsers).toEqual(['user1', 'user2']);
});
});
describe('abstract methods', () => {
it('should throw error for initialize()', async () => {
await expect(provider.initialize()).rejects.toThrow('initialize() must be implemented by subclass');
});
it('should throw error for verifyWebhookSignature()', () => {
expect(() => provider.verifyWebhookSignature({})).toThrow('verifyWebhookSignature() must be implemented by subclass');
});
it('should throw error for parseWebhookPayload()', () => {
expect(() => provider.parseWebhookPayload({})).toThrow('parseWebhookPayload() must be implemented by subclass');
});
it('should throw error for extractBotCommand()', () => {
expect(() => provider.extractBotCommand('')).toThrow('extractBotCommand() must be implemented by subclass');
});
it('should throw error for sendResponse()', async () => {
await expect(provider.sendResponse({}, '')).rejects.toThrow('sendResponse() must be implemented by subclass');
});
it('should throw error for getUserId()', () => {
expect(() => provider.getUserId({})).toThrow('getUserId() must be implemented by subclass');
});
});
describe('formatErrorMessage()', () => {
it('should format error message with reference ID and timestamp', () => {
const error = new Error('Test error');
const errorId = 'test-123';
const message = provider.formatErrorMessage(error, errorId);
expect(message).toContain('❌ An error occurred');
expect(message).toContain('Reference: test-123');
expect(message).toContain('Please check with an administrator');
});
});
describe('isUserAuthorized()', () => {
it('should return false for null/undefined userId', () => {
expect(provider.isUserAuthorized(null)).toBe(false);
expect(provider.isUserAuthorized(undefined)).toBe(false);
expect(provider.isUserAuthorized('')).toBe(false);
});
it('should return true for authorized users from config', () => {
expect(provider.isUserAuthorized('user1')).toBe(true);
expect(provider.isUserAuthorized('user2')).toBe(true);
});
it('should return false for unauthorized users', () => {
expect(provider.isUserAuthorized('unauthorized')).toBe(false);
});
it('should use environment variables when no config provided', () => {
const originalEnv = process.env.AUTHORIZED_USERS;
process.env.AUTHORIZED_USERS = 'envuser1,envuser2';
const envProvider = new ChatbotProvider();
expect(envProvider.isUserAuthorized('envuser1')).toBe(true);
expect(envProvider.isUserAuthorized('envuser2')).toBe(true);
expect(envProvider.isUserAuthorized('unauthorized')).toBe(false);
process.env.AUTHORIZED_USERS = originalEnv;
});
it('should use default authorized user when no config or env provided', () => {
const originalUsers = process.env.AUTHORIZED_USERS;
const originalDefault = process.env.DEFAULT_AUTHORIZED_USER;
delete process.env.AUTHORIZED_USERS;
process.env.DEFAULT_AUTHORIZED_USER = 'defaultuser';
const defaultProvider = new ChatbotProvider();
expect(defaultProvider.isUserAuthorized('defaultuser')).toBe(true);
expect(defaultProvider.isUserAuthorized('other')).toBe(false);
process.env.AUTHORIZED_USERS = originalUsers;
process.env.DEFAULT_AUTHORIZED_USER = originalDefault;
});
it('should fallback to admin when no config provided', () => {
const originalUsers = process.env.AUTHORIZED_USERS;
const originalDefault = process.env.DEFAULT_AUTHORIZED_USER;
delete process.env.AUTHORIZED_USERS;
delete process.env.DEFAULT_AUTHORIZED_USER;
const fallbackProvider = new ChatbotProvider();
expect(fallbackProvider.isUserAuthorized('admin')).toBe(true);
expect(fallbackProvider.isUserAuthorized('other')).toBe(false);
process.env.AUTHORIZED_USERS = originalUsers;
process.env.DEFAULT_AUTHORIZED_USER = originalDefault;
});
});
describe('getProviderName()', () => {
it('should return the class name', () => {
expect(provider.getProviderName()).toBe('ChatbotProvider');
});
});
describe('getBotMention()', () => {
it('should return bot mention from config', () => {
expect(provider.getBotMention()).toBe('@testbot');
});
it('should return bot mention from environment variable', () => {
const originalEnv = process.env.BOT_USERNAME;
process.env.BOT_USERNAME = '@envbot';
const envProvider = new ChatbotProvider();
expect(envProvider.getBotMention()).toBe('@envbot');
process.env.BOT_USERNAME = originalEnv;
});
it('should return default bot mention when no config provided', () => {
const originalEnv = process.env.BOT_USERNAME;
delete process.env.BOT_USERNAME;
const defaultProvider = new ChatbotProvider();
expect(defaultProvider.getBotMention()).toBe('@ClaudeBot');
process.env.BOT_USERNAME = originalEnv;
});
});
});
// Test concrete implementation to verify inheritance works correctly
class TestChatbotProvider extends ChatbotProvider {
async initialize() {
this.initialized = true;
}
verifyWebhookSignature(req) {
return req.valid === true;
}
parseWebhookPayload(payload) {
return { type: 'test', content: payload.message };
}
extractBotCommand(message) {
if (message.includes('@testbot')) {
return { command: message.replace('@testbot', '').trim() };
}
return null;
}
async sendResponse(context, response) {
context.lastResponse = response;
}
getUserId(context) {
return context.userId;
}
}
describe('ChatbotProvider inheritance', () => {
let testProvider;
beforeEach(() => {
testProvider = new TestChatbotProvider({ botMention: '@testbot' });
});
it('should allow concrete implementation to override abstract methods', async () => {
await testProvider.initialize();
expect(testProvider.initialized).toBe(true);
expect(testProvider.verifyWebhookSignature({ valid: true })).toBe(true);
expect(testProvider.verifyWebhookSignature({ valid: false })).toBe(false);
const parsed = testProvider.parseWebhookPayload({ message: 'hello' });
expect(parsed.type).toBe('test');
expect(parsed.content).toBe('hello');
const command = testProvider.extractBotCommand('@testbot help me');
expect(command.command).toBe('help me');
const context = { userId: '123' };
await testProvider.sendResponse(context, 'test response');
expect(context.lastResponse).toBe('test response');
expect(testProvider.getUserId({ userId: '456' })).toBe('456');
});
it('should inherit base class utility methods', () => {
expect(testProvider.getProviderName()).toBe('TestChatbotProvider');
expect(testProvider.getBotMention()).toBe('@testbot');
expect(testProvider.isUserAuthorized).toBeDefined();
expect(testProvider.formatErrorMessage).toBeDefined();
});
});

View File

@@ -0,0 +1,370 @@
const DiscordProvider = require('../../../src/providers/DiscordProvider');
const axios = require('axios');
// Mock dependencies
jest.mock('axios');
jest.mock('../../../src/utils/logger', () => ({
createLogger: () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn()
})
}));
jest.mock('../../../src/utils/secureCredentials', () => ({
get: jest.fn()
}));
const mockSecureCredentials = require('../../../src/utils/secureCredentials');
describe('DiscordProvider', () => {
let provider;
let originalEnv;
beforeEach(() => {
originalEnv = { ...process.env };
// Mock credentials
mockSecureCredentials.get.mockImplementation((key) => {
const mockCreds = {
'DISCORD_BOT_TOKEN': 'mock_bot_token',
'DISCORD_PUBLIC_KEY': '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
'DISCORD_APPLICATION_ID': '123456789012345678'
};
return mockCreds[key];
});
provider = new DiscordProvider({
authorizedUsers: ['user1', 'user2']
});
// Reset axios mock
axios.post.mockReset();
});
afterEach(() => {
process.env = originalEnv;
jest.clearAllMocks();
});
describe('initialization', () => {
it('should initialize successfully with valid credentials', async () => {
await expect(provider.initialize()).resolves.toBeUndefined();
expect(provider.botToken).toBe('mock_bot_token');
expect(provider.publicKey).toBe('0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef');
expect(provider.applicationId).toBe('123456789012345678');
});
it('should use environment variables when secure credentials not available', async () => {
mockSecureCredentials.get.mockReturnValue(null);
process.env.DISCORD_BOT_TOKEN = 'env_bot_token';
process.env.DISCORD_PUBLIC_KEY = 'env_public_key';
process.env.DISCORD_APPLICATION_ID = 'env_app_id';
await provider.initialize();
expect(provider.botToken).toBe('env_bot_token');
expect(provider.publicKey).toBe('env_public_key');
expect(provider.applicationId).toBe('env_app_id');
});
it('should throw error when required credentials are missing', async () => {
mockSecureCredentials.get.mockReturnValue(null);
delete process.env.DISCORD_BOT_TOKEN;
delete process.env.DISCORD_PUBLIC_KEY;
await expect(provider.initialize()).rejects.toThrow('Discord bot token and public key are required');
});
});
describe('verifyWebhookSignature', () => {
beforeEach(async () => {
await provider.initialize();
});
it('should return false when signature headers are missing', () => {
const req = { headers: {} };
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should return false when only timestamp is present', () => {
const req = {
headers: { 'x-signature-timestamp': '1234567890' }
};
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should return false when only signature is present', () => {
const req = {
headers: { 'x-signature-ed25519': 'some_signature' }
};
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should return true in test mode', () => {
process.env.NODE_ENV = 'test';
const req = {
headers: {
'x-signature-ed25519': 'invalid_signature',
'x-signature-timestamp': '1234567890'
}
};
expect(provider.verifyWebhookSignature(req)).toBe(true);
});
it('should handle crypto verification errors gracefully', () => {
const req = {
headers: {
'x-signature-ed25519': 'invalid_signature_format',
'x-signature-timestamp': '1234567890'
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
// This should not throw, but return false due to invalid signature
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
});
describe('parseWebhookPayload', () => {
it('should parse PING interaction', () => {
const payload = { type: 1 };
const result = provider.parseWebhookPayload(payload);
expect(result.type).toBe('ping');
expect(result.shouldRespond).toBe(true);
expect(result.responseData).toEqual({ type: 1 });
});
it('should parse APPLICATION_COMMAND interaction', () => {
const payload = {
type: 2,
data: {
name: 'help',
options: [
{ name: 'topic', value: 'discord' }
]
},
channel_id: '123456789',
guild_id: '987654321',
member: {
user: {
id: 'user123',
username: 'testuser'
}
},
token: 'interaction_token',
id: 'interaction_id'
};
const result = provider.parseWebhookPayload(payload);
expect(result.type).toBe('command');
expect(result.command).toBe('help');
expect(result.options).toHaveLength(1);
expect(result.channelId).toBe('123456789');
expect(result.guildId).toBe('987654321');
expect(result.userId).toBe('user123');
expect(result.username).toBe('testuser');
expect(result.content).toBe('help topic:discord');
expect(result.interactionToken).toBe('interaction_token');
expect(result.interactionId).toBe('interaction_id');
});
it('should parse MESSAGE_COMPONENT interaction', () => {
const payload = {
type: 3,
data: {
custom_id: 'button_click'
},
channel_id: '123456789',
user: {
id: 'user123',
username: 'testuser'
},
token: 'interaction_token',
id: 'interaction_id'
};
const result = provider.parseWebhookPayload(payload);
expect(result.type).toBe('component');
expect(result.customId).toBe('button_click');
expect(result.userId).toBe('user123');
expect(result.username).toBe('testuser');
});
it('should handle unknown interaction types', () => {
const payload = { type: 999 };
const result = provider.parseWebhookPayload(payload);
expect(result.type).toBe('unknown');
expect(result.shouldRespond).toBe(false);
});
it('should handle payload parsing errors', () => {
expect(() => provider.parseWebhookPayload(null)).toThrow();
});
});
describe('buildCommandContent', () => {
it('should build command content with name only', () => {
const commandData = { name: 'help' };
const result = provider.buildCommandContent(commandData);
expect(result).toBe('help');
});
it('should build command content with options', () => {
const commandData = {
name: 'help',
options: [
{ name: 'topic', value: 'discord' },
{ name: 'format', value: 'detailed' }
]
};
const result = provider.buildCommandContent(commandData);
expect(result).toBe('help topic:discord format:detailed');
});
it('should handle empty command data', () => {
expect(provider.buildCommandContent(null)).toBe('');
expect(provider.buildCommandContent(undefined)).toBe('');
expect(provider.buildCommandContent({})).toBe('');
});
});
describe('extractBotCommand', () => {
it('should extract command from content', () => {
const result = provider.extractBotCommand('help me with discord');
expect(result.command).toBe('help me with discord');
expect(result.originalMessage).toBe('help me with discord');
});
it('should return null for empty content', () => {
expect(provider.extractBotCommand('')).toBeNull();
expect(provider.extractBotCommand(null)).toBeNull();
expect(provider.extractBotCommand(undefined)).toBeNull();
});
});
describe('sendResponse', () => {
beforeEach(async () => {
await provider.initialize();
axios.post.mockResolvedValue({ data: { id: 'message_id' } });
});
it('should skip response for ping interactions', async () => {
const context = { type: 'ping' };
await provider.sendResponse(context, 'test response');
expect(axios.post).not.toHaveBeenCalled();
});
it('should send follow-up message for interactions with token', async () => {
const context = {
type: 'command',
interactionToken: 'test_token',
interactionId: 'test_id'
};
await provider.sendResponse(context, 'test response');
expect(axios.post).toHaveBeenCalledWith(
`https://discord.com/api/v10/webhooks/${provider.applicationId}/test_token`,
{ content: 'test response', flags: 0 },
{
headers: {
'Authorization': `Bot ${provider.botToken}`,
'Content-Type': 'application/json'
}
}
);
});
it('should send channel message when no interaction token', async () => {
const context = {
type: 'command',
channelId: '123456789'
};
await provider.sendResponse(context, 'test response');
expect(axios.post).toHaveBeenCalledWith(
'https://discord.com/api/v10/channels/123456789/messages',
{ content: 'test response' },
{
headers: {
'Authorization': `Bot ${provider.botToken}`,
'Content-Type': 'application/json'
}
}
);
});
it('should handle axios errors', async () => {
axios.post.mockRejectedValue(new Error('Network error'));
const context = {
type: 'command',
channelId: '123456789'
};
await expect(provider.sendResponse(context, 'test response')).rejects.toThrow('Network error');
});
});
describe('splitLongMessage', () => {
it('should return single message when under limit', () => {
const result = provider.splitLongMessage('short message', 2000);
expect(result).toEqual(['short message']);
});
it('should split long messages by lines', () => {
const longMessage = 'line1\n'.repeat(50) + 'final line';
const result = provider.splitLongMessage(longMessage, 100);
expect(result.length).toBeGreaterThan(1);
expect(result.every(msg => msg.length <= 100)).toBe(true);
});
it('should split very long single lines', () => {
const longLine = 'a'.repeat(3000);
const result = provider.splitLongMessage(longLine, 2000);
expect(result.length).toBe(2);
expect(result[0].length).toBe(2000);
expect(result[1].length).toBe(1000);
});
});
describe('getUserId', () => {
it('should return userId from context', () => {
const context = { userId: 'user123' };
expect(provider.getUserId(context)).toBe('user123');
});
});
describe('formatErrorMessage', () => {
it('should format Discord-specific error message', () => {
const error = new Error('Test error');
const errorId = 'test-123';
const message = provider.formatErrorMessage(error, errorId);
expect(message).toContain('🚫 **Error Processing Command**');
expect(message).toContain('**Reference ID:** `test-123`');
expect(message).toContain('Please contact an administrator');
});
});
describe('getBotMention', () => {
it('should return Discord-specific bot mention', () => {
const provider = new DiscordProvider({ botMention: 'custombot' });
expect(provider.getBotMention()).toBe('custombot');
});
it('should return default bot mention', () => {
const provider = new DiscordProvider();
expect(provider.getBotMention()).toBe('claude');
});
});
});

View File

@@ -0,0 +1,326 @@
const ProviderFactory = require('../../../src/providers/ProviderFactory');
const DiscordProvider = require('../../../src/providers/DiscordProvider');
const ChatbotProvider = require('../../../src/providers/ChatbotProvider');
// Mock dependencies
jest.mock('../../../src/utils/logger', () => ({
createLogger: () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn()
})
}));
// Mock DiscordProvider to avoid initialization issues in tests
jest.mock('../../../src/providers/DiscordProvider');
describe('ProviderFactory', () => {
let factory;
let originalEnv;
beforeEach(() => {
originalEnv = { ...process.env };
// Clear the factory singleton and create fresh instance for each test
jest.resetModules();
const ProviderFactoryClass = require('../../../src/providers/ProviderFactory').constructor;
factory = new ProviderFactoryClass();
// Mock DiscordProvider
DiscordProvider.mockImplementation(() => ({
initialize: jest.fn().mockResolvedValue(),
getProviderName: jest.fn().mockReturnValue('DiscordProvider'),
getBotMention: jest.fn().mockReturnValue('@claude')
}));
});
afterEach(() => {
process.env = originalEnv;
jest.clearAllMocks();
});
describe('initialization', () => {
it('should initialize with discord provider registered', () => {
expect(factory.getAvailableProviders()).toContain('discord');
});
it('should start with empty providers map', () => {
expect(factory.getAllProviders().size).toBe(0);
});
});
describe('registerProvider', () => {
class TestProvider extends ChatbotProvider {
async initialize() {}
verifyWebhookSignature() { return true; }
parseWebhookPayload() { return {}; }
extractBotCommand() { return null; }
async sendResponse() {}
getUserId() { return 'test'; }
}
it('should register new provider', () => {
factory.registerProvider('test', TestProvider);
expect(factory.getAvailableProviders()).toContain('test');
});
it('should handle case-insensitive provider names', () => {
factory.registerProvider('TEST', TestProvider);
expect(factory.getAvailableProviders()).toContain('test');
});
});
describe('createProvider', () => {
it('should create and cache discord provider', async () => {
const provider = await factory.createProvider('discord');
expect(provider).toBeInstanceOf(DiscordProvider);
expect(DiscordProvider).toHaveBeenCalledWith({});
// Should return cached instance on second call
const provider2 = await factory.createProvider('discord');
expect(provider2).toBe(provider);
expect(DiscordProvider).toHaveBeenCalledTimes(1);
});
it('should create provider with custom config', async () => {
const config = { botMention: '@custombot', authorizedUsers: ['user1'] };
await factory.createProvider('discord', config);
expect(DiscordProvider).toHaveBeenCalledWith(config);
});
it('should merge with default config', async () => {
factory.setDefaultConfig({ globalSetting: true });
const config = { botMention: '@custombot' };
await factory.createProvider('discord', config);
expect(DiscordProvider).toHaveBeenCalledWith({
globalSetting: true,
botMention: '@custombot'
});
});
it('should throw error for unknown provider', async () => {
await expect(factory.createProvider('unknown')).rejects.toThrow(
'Unknown provider: unknown. Available providers: discord'
);
});
it('should handle provider initialization errors', async () => {
DiscordProvider.mockImplementation(() => {
throw new Error('Initialization failed');
});
await expect(factory.createProvider('discord')).rejects.toThrow(
'Failed to create discord provider: Initialization failed'
);
});
});
describe('getProvider', () => {
it('should return existing provider', async () => {
const provider = await factory.createProvider('discord');
expect(factory.getProvider('discord')).toBe(provider);
});
it('should return null for non-existent provider', () => {
expect(factory.getProvider('nonexistent')).toBeNull();
});
it('should be case-insensitive', async () => {
const provider = await factory.createProvider('discord');
expect(factory.getProvider('DISCORD')).toBe(provider);
});
});
describe('setDefaultConfig', () => {
it('should set default configuration', () => {
const config = { globalSetting: true, defaultUser: 'admin' };
factory.setDefaultConfig(config);
expect(factory.defaultConfig).toEqual(config);
});
});
describe('updateProviderConfig', () => {
it('should recreate provider with new config', async () => {
// Create initial provider
await factory.createProvider('discord', { botMention: '@oldbot' });
expect(DiscordProvider).toHaveBeenCalledTimes(1);
// Update config
await factory.updateProviderConfig('discord', { botMention: '@newbot' });
expect(DiscordProvider).toHaveBeenCalledTimes(2);
expect(DiscordProvider).toHaveBeenLastCalledWith({ botMention: '@newbot' });
});
});
describe('getEnvironmentConfig', () => {
it('should extract Discord config from environment', () => {
process.env.DISCORD_BOT_TOKEN = 'test_token';
process.env.DISCORD_PUBLIC_KEY = 'test_key';
process.env.DISCORD_APPLICATION_ID = 'test_id';
process.env.DISCORD_AUTHORIZED_USERS = 'user1,user2,user3';
process.env.DISCORD_BOT_MENTION = '@discordbot';
const config = factory.getEnvironmentConfig('discord');
expect(config).toEqual({
botToken: 'test_token',
publicKey: 'test_key',
applicationId: 'test_id',
authorizedUsers: ['user1', 'user2', 'user3'],
botMention: '@discordbot'
});
});
it('should extract Slack config from environment', () => {
process.env.SLACK_BOT_TOKEN = 'xoxb-token';
process.env.SLACK_SIGNING_SECRET = 'signing_secret';
process.env.SLACK_AUTHORIZED_USERS = 'slackuser1,slackuser2';
process.env.SLACK_BOT_MENTION = '@slackbot';
const config = factory.getEnvironmentConfig('slack');
expect(config).toEqual({
botToken: 'xoxb-token',
signingSecret: 'signing_secret',
authorizedUsers: ['slackuser1', 'slackuser2'],
botMention: '@slackbot'
});
});
it('should extract Nextcloud config from environment', () => {
process.env.NEXTCLOUD_SERVER_URL = 'https://nextcloud.example.com';
process.env.NEXTCLOUD_USERNAME = 'claude_bot';
process.env.NEXTCLOUD_PASSWORD = 'secret_password';
process.env.NEXTCLOUD_AUTHORIZED_USERS = 'ncuser1,ncuser2';
process.env.NEXTCLOUD_BOT_MENTION = '@claudebot';
const config = factory.getEnvironmentConfig('nextcloud');
expect(config).toEqual({
serverUrl: 'https://nextcloud.example.com',
username: 'claude_bot',
password: 'secret_password',
authorizedUsers: ['ncuser1', 'ncuser2'],
botMention: '@claudebot'
});
});
it('should remove undefined values from config', () => {
// Only set some env vars
process.env.DISCORD_BOT_TOKEN = 'test_token';
// Don't set DISCORD_PUBLIC_KEY
const config = factory.getEnvironmentConfig('discord');
expect(config).toEqual({
botToken: 'test_token'
});
expect(config.hasOwnProperty('publicKey')).toBe(false);
});
});
describe('createFromEnvironment', () => {
it('should create provider using environment config', async () => {
process.env.DISCORD_BOT_TOKEN = 'env_token';
process.env.DISCORD_AUTHORIZED_USERS = 'envuser1,envuser2';
await factory.createFromEnvironment('discord');
expect(DiscordProvider).toHaveBeenCalledWith({
botToken: 'env_token',
authorizedUsers: ['envuser1', 'envuser2']
});
});
});
describe('createMultipleProviders', () => {
class MockSlackProvider extends ChatbotProvider {
async initialize() {}
verifyWebhookSignature() { return true; }
parseWebhookPayload() { return {}; }
extractBotCommand() { return null; }
async sendResponse() {}
getUserId() { return 'slack'; }
}
beforeEach(() => {
factory.registerProvider('slack', MockSlackProvider);
});
it('should create multiple providers successfully', async () => {
const config = {
discord: { botMention: '@discord' },
slack: { botMention: '@slack' }
};
const results = await factory.createMultipleProviders(config);
expect(results.size).toBe(2);
expect(results.has('discord')).toBe(true);
expect(results.has('slack')).toBe(true);
});
it('should handle partial failures gracefully', async () => {
const config = {
discord: { botMention: '@discord' },
unknown: { botMention: '@unknown' }
};
const results = await factory.createMultipleProviders(config);
expect(results.size).toBe(1);
expect(results.has('discord')).toBe(true);
expect(results.has('unknown')).toBe(false);
});
});
describe('cleanup', () => {
it('should clear all providers', async () => {
await factory.createProvider('discord');
expect(factory.getAllProviders().size).toBe(1);
await factory.cleanup();
expect(factory.getAllProviders().size).toBe(0);
});
});
describe('getStats', () => {
it('should return provider statistics', async () => {
await factory.createProvider('discord');
const stats = factory.getStats();
expect(stats).toEqual({
totalRegistered: 1,
totalInitialized: 1,
availableProviders: ['discord'],
initializedProviders: ['discord']
});
});
it('should return correct stats when no providers initialized', () => {
const stats = factory.getStats();
expect(stats).toEqual({
totalRegistered: 1, // discord is registered by default
totalInitialized: 0,
availableProviders: ['discord'],
initializedProviders: []
});
});
});
describe('singleton behavior', () => {
it('should be a singleton when imported normally', () => {
// This tests the actual exported singleton
const factory1 = require('../../../src/providers/ProviderFactory');
const factory2 = require('../../../src/providers/ProviderFactory');
expect(factory1).toBe(factory2);
});
});
});

View File

@@ -0,0 +1,502 @@
const DiscordProvider = require('../../../src/providers/DiscordProvider');
// Mock dependencies
jest.mock('../../../src/utils/logger', () => ({
createLogger: () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn()
})
}));
jest.mock('../../../src/utils/secureCredentials', () => ({
get: jest.fn().mockReturnValue('mock_value')
}));
describe('Discord Payload Processing Tests', () => {
let provider;
beforeEach(() => {
provider = new DiscordProvider();
});
describe('Real Discord Payload Examples', () => {
it('should parse Discord PING interaction correctly', () => {
const pingPayload = {
id: '123456789012345678',
type: 1,
version: 1
};
const result = provider.parseWebhookPayload(pingPayload);
expect(result).toEqual({
type: 'ping',
shouldRespond: true,
responseData: { type: 1 }
});
});
it('should parse Discord slash command without options', () => {
const slashCommandPayload = {
id: '123456789012345678',
application_id: '987654321098765432',
type: 2,
data: {
id: '456789012345678901',
name: 'claude',
type: 1,
resolved: {},
options: []
},
guild_id: '111111111111111111',
channel_id: '222222222222222222',
member: {
user: {
id: '333333333333333333',
username: 'testuser',
discriminator: '1234',
avatar: 'avatar_hash'
},
roles: ['444444444444444444'],
permissions: '2147483647'
},
token: 'unique_interaction_token',
version: 1
};
const result = provider.parseWebhookPayload(slashCommandPayload);
expect(result).toEqual({
type: 'command',
command: 'claude',
options: [],
channelId: '222222222222222222',
guildId: '111111111111111111',
userId: '333333333333333333',
username: 'testuser',
content: 'claude',
interactionToken: 'unique_interaction_token',
interactionId: '123456789012345678'
});
});
it('should parse Discord slash command with string option', () => {
const slashCommandWithOptionsPayload = {
id: '123456789012345678',
application_id: '987654321098765432',
type: 2,
data: {
id: '456789012345678901',
name: 'claude',
type: 1,
options: [
{
name: 'prompt',
type: 3,
value: 'Help me debug this Python function'
}
]
},
guild_id: '111111111111111111',
channel_id: '222222222222222222',
member: {
user: {
id: '333333333333333333',
username: 'developer',
discriminator: '5678'
}
},
token: 'another_interaction_token',
version: 1
};
const result = provider.parseWebhookPayload(slashCommandWithOptionsPayload);
expect(result).toEqual({
type: 'command',
command: 'claude',
options: [
{
name: 'prompt',
type: 3,
value: 'Help me debug this Python function'
}
],
channelId: '222222222222222222',
guildId: '111111111111111111',
userId: '333333333333333333',
username: 'developer',
content: 'claude prompt:Help me debug this Python function',
interactionToken: 'another_interaction_token',
interactionId: '123456789012345678'
});
});
it('should parse Discord slash command with multiple options', () => {
const multiOptionPayload = {
id: '123456789012345678',
type: 2,
data: {
name: 'claude',
options: [
{
name: 'action',
type: 3,
value: 'review'
},
{
name: 'file',
type: 3,
value: 'src/main.js'
},
{
name: 'verbose',
type: 5,
value: true
}
]
},
channel_id: '222222222222222222',
member: {
user: {
id: '333333333333333333',
username: 'reviewer'
}
},
token: 'multi_option_token'
};
const result = provider.parseWebhookPayload(multiOptionPayload);
expect(result.content).toBe('claude action:review file:src/main.js verbose:true');
expect(result.options).toHaveLength(3);
});
it('should parse Discord button interaction', () => {
const buttonInteractionPayload = {
id: '123456789012345678',
application_id: '987654321098765432',
type: 3,
data: {
component_type: 2,
custom_id: 'help_button_click'
},
guild_id: '111111111111111111',
channel_id: '222222222222222222',
member: {
user: {
id: '333333333333333333',
username: 'buttonclicker'
}
},
message: {
id: '555555555555555555',
content: 'Original message content'
},
token: 'button_interaction_token',
version: 1
};
const result = provider.parseWebhookPayload(buttonInteractionPayload);
expect(result).toEqual({
type: 'component',
customId: 'help_button_click',
channelId: '222222222222222222',
guildId: '111111111111111111',
userId: '333333333333333333',
username: 'buttonclicker',
interactionToken: 'button_interaction_token',
interactionId: '123456789012345678'
});
});
it('should parse Discord select menu interaction', () => {
const selectMenuPayload = {
id: '123456789012345678',
type: 3,
data: {
component_type: 3,
custom_id: 'language_select',
values: ['javascript', 'python']
},
channel_id: '222222222222222222',
user: {
id: '333333333333333333',
username: 'selector'
},
token: 'select_interaction_token'
};
const result = provider.parseWebhookPayload(selectMenuPayload);
expect(result).toEqual({
type: 'component',
customId: 'language_select',
channelId: '222222222222222222',
guildId: undefined,
userId: '333333333333333333',
username: 'selector',
interactionToken: 'select_interaction_token',
interactionId: '123456789012345678'
});
});
it('should handle Discord DM (no guild_id)', () => {
const dmPayload = {
id: '123456789012345678',
type: 2,
data: {
name: 'claude',
options: [
{
name: 'question',
value: 'How do I use async/await in JavaScript?'
}
]
},
channel_id: '222222222222222222',
user: {
id: '333333333333333333',
username: 'dmuser'
},
token: 'dm_interaction_token'
};
const result = provider.parseWebhookPayload(dmPayload);
expect(result.guildId).toBeUndefined();
expect(result.userId).toBe('333333333333333333');
expect(result.username).toBe('dmuser');
expect(result.type).toBe('command');
});
it('should handle payload with missing optional fields', () => {
const minimalPayload = {
id: '123456789012345678',
type: 2,
data: {
name: 'claude'
},
channel_id: '222222222222222222',
user: {
id: '333333333333333333',
username: 'minimaluser'
},
token: 'minimal_token'
};
const result = provider.parseWebhookPayload(minimalPayload);
expect(result).toEqual({
type: 'command',
command: 'claude',
options: [],
channelId: '222222222222222222',
guildId: undefined,
userId: '333333333333333333',
username: 'minimaluser',
content: 'claude',
interactionToken: 'minimal_token',
interactionId: '123456789012345678'
});
});
});
describe('Edge Cases and Error Handling', () => {
it('should handle payload with null data gracefully', () => {
const nullDataPayload = {
id: '123456789012345678',
type: 2,
data: null,
channel_id: '222222222222222222',
user: {
id: '333333333333333333',
username: 'nulluser'
},
token: 'null_token'
};
expect(() => provider.parseWebhookPayload(nullDataPayload)).not.toThrow();
const result = provider.parseWebhookPayload(nullDataPayload);
expect(result.content).toBe('');
});
it('should handle payload with missing user information', () => {
const noUserPayload = {
id: '123456789012345678',
type: 2,
data: {
name: 'claude'
},
channel_id: '222222222222222222',
token: 'no_user_token'
};
const result = provider.parseWebhookPayload(noUserPayload);
expect(result.userId).toBeUndefined();
expect(result.username).toBeUndefined();
});
it('should handle unknown interaction type gracefully', () => {
const unknownTypePayload = {
id: '123456789012345678',
type: 999, // Unknown type
data: {
name: 'claude'
},
channel_id: '222222222222222222',
user: {
id: '333333333333333333',
username: 'unknownuser'
},
token: 'unknown_token'
};
const result = provider.parseWebhookPayload(unknownTypePayload);
expect(result).toEqual({
type: 'unknown',
shouldRespond: false
});
});
it('should handle very large option values', () => {
const largeValuePayload = {
id: '123456789012345678',
type: 2,
data: {
name: 'claude',
options: [
{
name: 'code',
value: 'x'.repeat(4000) // Very large value
}
]
},
channel_id: '222222222222222222',
user: {
id: '333333333333333333',
username: 'largeuser'
},
token: 'large_token'
};
expect(() => provider.parseWebhookPayload(largeValuePayload)).not.toThrow();
const result = provider.parseWebhookPayload(largeValuePayload);
expect(result.content).toContain('claude code:');
expect(result.content.length).toBeGreaterThan(4000);
});
it('should handle special characters in usernames', () => {
const specialCharsPayload = {
id: '123456789012345678',
type: 2,
data: {
name: 'claude'
},
channel_id: '222222222222222222',
user: {
id: '333333333333333333',
username: 'user-with_special.chars123'
},
token: 'special_token'
};
const result = provider.parseWebhookPayload(specialCharsPayload);
expect(result.username).toBe('user-with_special.chars123');
});
it('should handle unicode characters in option values', () => {
const unicodePayload = {
id: '123456789012345678',
type: 2,
data: {
name: 'claude',
options: [
{
name: 'message',
value: 'Hello 世界! 🚀 How are you?'
}
]
},
channel_id: '222222222222222222',
user: {
id: '333333333333333333',
username: 'unicodeuser'
},
token: 'unicode_token'
};
const result = provider.parseWebhookPayload(unicodePayload);
expect(result.content).toBe('claude message:Hello 世界! 🚀 How are you?');
});
});
describe('buildCommandContent function', () => {
it('should handle complex nested options structure', () => {
const complexCommandData = {
name: 'claude',
options: [
{
name: 'subcommand',
type: 1,
options: [
{
name: 'param1',
value: 'value1'
},
{
name: 'param2',
value: 'value2'
}
]
}
]
};
// Note: Current implementation flattens all options
const result = provider.buildCommandContent(complexCommandData);
expect(result).toContain('claude');
});
it('should handle boolean option values', () => {
const booleanCommandData = {
name: 'claude',
options: [
{
name: 'verbose',
value: true
},
{
name: 'silent',
value: false
}
]
};
const result = provider.buildCommandContent(booleanCommandData);
expect(result).toBe('claude verbose:true silent:false');
});
it('should handle numeric option values', () => {
const numericCommandData = {
name: 'claude',
options: [
{
name: 'count',
value: 42
},
{
name: 'rate',
value: 3.14
}
]
};
const result = provider.buildCommandContent(numericCommandData);
expect(result).toBe('claude count:42 rate:3.14');
});
});
});

View File

@@ -0,0 +1,411 @@
const crypto = require('crypto');
const DiscordProvider = require('../../../src/providers/DiscordProvider');
// Mock dependencies
jest.mock('../../../src/utils/logger', () => ({
createLogger: () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn()
})
}));
jest.mock('../../../src/utils/secureCredentials', () => ({
get: jest.fn()
}));
const mockSecureCredentials = require('../../../src/utils/secureCredentials');
describe('Signature Verification Security Tests', () => {
let provider;
const validPublicKey = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
const validPrivateKey = 'abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789';
beforeEach(() => {
mockSecureCredentials.get.mockImplementation((key) => {
const mockCreds = {
'DISCORD_BOT_TOKEN': 'mock_bot_token',
'DISCORD_PUBLIC_KEY': validPublicKey,
'DISCORD_APPLICATION_ID': '123456789012345678'
};
return mockCreds[key];
});
provider = new DiscordProvider();
});
afterEach(() => {
jest.clearAllMocks();
});
describe('Discord Ed25519 Signature Verification', () => {
beforeEach(async () => {
await provider.initialize();
});
it('should reject requests with missing signature headers', () => {
const req = {
headers: {},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should reject requests with only timestamp header', () => {
const req = {
headers: {
'x-signature-timestamp': '1234567890'
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should reject requests with only signature header', () => {
const req = {
headers: {
'x-signature-ed25519': 'some_signature'
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should handle invalid signature format gracefully', () => {
const req = {
headers: {
'x-signature-ed25519': 'invalid_hex_signature',
'x-signature-timestamp': '1234567890'
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
// Should not throw an error, but return false
expect(() => provider.verifyWebhookSignature(req)).not.toThrow();
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should handle invalid public key format gracefully', async () => {
// Override with invalid key format
mockSecureCredentials.get.mockImplementation((key) => {
if (key === 'DISCORD_PUBLIC_KEY') return 'invalid_key_format';
return 'mock_value';
});
const invalidProvider = new DiscordProvider();
await invalidProvider.initialize();
const req = {
headers: {
'x-signature-ed25519': '64byte_hex_signature_placeholder_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
'x-signature-timestamp': '1234567890'
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(invalidProvider.verifyWebhookSignature(req)).toBe(false);
});
it('should bypass verification in test mode', () => {
const originalEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'test';
const req = {
headers: {
'x-signature-ed25519': 'completely_invalid_signature',
'x-signature-timestamp': '1234567890'
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(provider.verifyWebhookSignature(req)).toBe(true);
process.env.NODE_ENV = originalEnv;
});
it('should handle crypto verification errors without throwing', () => {
// Mock crypto.verify to throw an error
const originalVerify = crypto.verify;
crypto.verify = jest.fn().mockImplementation(() => {
throw new Error('Crypto verification failed');
});
const req = {
headers: {
'x-signature-ed25519': '64byte_hex_signature_placeholder_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
'x-signature-timestamp': '1234567890'
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(() => provider.verifyWebhookSignature(req)).not.toThrow();
expect(provider.verifyWebhookSignature(req)).toBe(false);
// Restore original function
crypto.verify = originalVerify;
});
it('should construct verification message correctly', () => {
const timestamp = '1234567890';
const body = 'test body content';
const expectedMessage = timestamp + body;
const req = {
headers: {
'x-signature-ed25519': '64byte_hex_signature_placeholder_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
'x-signature-timestamp': timestamp
},
rawBody: Buffer.from(body),
body: { test: 'data' }
};
// Mock crypto.verify to capture the message parameter
const originalVerify = crypto.verify;
const mockVerify = jest.fn().mockReturnValue(false);
crypto.verify = mockVerify;
provider.verifyWebhookSignature(req);
expect(mockVerify).toHaveBeenCalledWith(
'ed25519',
Buffer.from(expectedMessage),
expect.any(Buffer), // public key buffer
expect.any(Buffer) // signature buffer
);
crypto.verify = originalVerify;
});
it('should use rawBody when available', () => {
const timestamp = '1234567890';
const rawBodyContent = 'raw body content';
const bodyContent = { parsed: 'json' };
const req = {
headers: {
'x-signature-ed25519': '64byte_hex_signature_placeholder_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
'x-signature-timestamp': timestamp
},
rawBody: Buffer.from(rawBodyContent),
body: bodyContent
};
const originalVerify = crypto.verify;
const mockVerify = jest.fn().mockReturnValue(false);
crypto.verify = mockVerify;
provider.verifyWebhookSignature(req);
// Should use rawBody, not JSON.stringify(body)
expect(mockVerify).toHaveBeenCalledWith(
'ed25519',
Buffer.from(timestamp + rawBodyContent),
expect.any(Buffer),
expect.any(Buffer)
);
crypto.verify = originalVerify;
});
it('should fallback to JSON.stringify when rawBody is unavailable', () => {
const timestamp = '1234567890';
const bodyContent = { test: 'data' };
const expectedMessage = timestamp + JSON.stringify(bodyContent);
const req = {
headers: {
'x-signature-ed25519': '64byte_hex_signature_placeholder_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
'x-signature-timestamp': timestamp
},
// No rawBody provided
body: bodyContent
};
const originalVerify = crypto.verify;
const mockVerify = jest.fn().mockReturnValue(false);
crypto.verify = mockVerify;
provider.verifyWebhookSignature(req);
expect(mockVerify).toHaveBeenCalledWith(
'ed25519',
Buffer.from(expectedMessage),
expect.any(Buffer),
expect.any(Buffer)
);
crypto.verify = originalVerify;
});
});
describe('Security Edge Cases', () => {
beforeEach(async () => {
await provider.initialize();
});
it('should handle empty signature gracefully', () => {
const req = {
headers: {
'x-signature-ed25519': '',
'x-signature-timestamp': '1234567890'
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should handle empty timestamp gracefully', () => {
const req = {
headers: {
'x-signature-ed25519': '64byte_hex_signature_placeholder_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
'x-signature-timestamp': ''
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should handle signature with wrong length', () => {
const req = {
headers: {
'x-signature-ed25519': 'short_sig',
'x-signature-timestamp': '1234567890'
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should handle very long signature without crashing', () => {
const req = {
headers: {
'x-signature-ed25519': 'a'.repeat(1000), // Very long signature
'x-signature-timestamp': '1234567890'
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(() => provider.verifyWebhookSignature(req)).not.toThrow();
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should handle unicode characters in timestamp', () => {
const req = {
headers: {
'x-signature-ed25519': '64byte_hex_signature_placeholder_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
'x-signature-timestamp': '123😀567890'
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(() => provider.verifyWebhookSignature(req)).not.toThrow();
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should handle null/undefined headers safely', () => {
const req = {
headers: {
'x-signature-ed25519': null,
'x-signature-timestamp': undefined
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(provider.verifyWebhookSignature(req)).toBe(false);
});
it('should handle Buffer conversion errors gracefully', () => {
// Mock Buffer.from to throw an error
const originalBufferFrom = Buffer.from;
Buffer.from = jest.fn().mockImplementation((data) => {
if (typeof data === 'string' && data.includes('signature')) {
throw new Error('Buffer conversion failed');
}
return originalBufferFrom(data);
});
const req = {
headers: {
'x-signature-ed25519': 'invalid_signature_that_causes_buffer_error',
'x-signature-timestamp': '1234567890'
},
rawBody: Buffer.from('test body'),
body: { test: 'data' }
};
expect(() => provider.verifyWebhookSignature(req)).not.toThrow();
expect(provider.verifyWebhookSignature(req)).toBe(false);
Buffer.from = originalBufferFrom;
});
});
describe('Timing Attack Prevention', () => {
beforeEach(async () => {
await provider.initialize();
});
it('should have consistent timing for different signature lengths', async () => {
const shortSig = 'abc';
const longSig = 'a'.repeat(128);
const timestamp = '1234567890';
const req1 = {
headers: {
'x-signature-ed25519': shortSig,
'x-signature-timestamp': timestamp
},
rawBody: Buffer.from('test'),
body: {}
};
const req2 = {
headers: {
'x-signature-ed25519': longSig,
'x-signature-timestamp': timestamp
},
rawBody: Buffer.from('test'),
body: {}
};
// Both should return false, and ideally take similar time
const start1 = process.hrtime.bigint();
const result1 = provider.verifyWebhookSignature(req1);
const end1 = process.hrtime.bigint();
const start2 = process.hrtime.bigint();
const result2 = provider.verifyWebhookSignature(req2);
const end2 = process.hrtime.bigint();
expect(result1).toBe(false);
expect(result2).toBe(false);
// Both operations should complete in reasonable time (less than 100ms)
const time1 = Number(end1 - start1) / 1000000; // Convert to milliseconds
const time2 = Number(end2 - start2) / 1000000;
expect(time1).toBeLessThan(100);
expect(time2).toBeLessThan(100);
});
});
});