forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush-notifications.processor.spec.ts
More file actions
161 lines (139 loc) · 4.93 KB
/
Copy pathpush-notifications.processor.spec.ts
File metadata and controls
161 lines (139 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { ConfigService } from '@nestjs/config';
import { Repository } from 'typeorm';
import { PushNotificationProcessor } from './push-notifications.processor';
import { DeviceRegistration } from './entities/device-registration.entity';
import { NotificationPreference, NotificationEventType } from './entities/notification-preference.entity';
import { Job } from 'bull';
// Mock firebase-admin
const mockSendEachForMulticast = jest.fn();
jest.mock('firebase-admin', () => ({
credential: {
cert: jest.fn(),
},
initializeApp: jest.fn(() => ({
messaging: () => ({
sendEachForMulticast: mockSendEachForMulticast,
}),
})),
apps: [],
app: jest.fn(() => ({
messaging: () => ({
sendEachForMulticast: mockSendEachForMulticast,
}),
})),
}));
describe('PushNotificationProcessor', () => {
let processor: PushNotificationProcessor;
let deviceRepo: Repository<DeviceRegistration>;
let prefRepo: Repository<NotificationPreference>;
let configService: ConfigService;
const mockDeviceRepo = {
find: jest.fn(),
delete: jest.fn(),
};
const mockPrefRepo = {
findOne: jest.fn(),
};
const mockConfigService = {
get: jest.fn((key) => {
if (key === 'FIREBASE_SERVICE_ACCOUNT') return JSON.stringify({ project_id: 'test' });
return null;
}),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
PushNotificationProcessor,
{
provide: getRepositoryToken(DeviceRegistration),
useValue: mockDeviceRepo,
},
{
provide: getRepositoryToken(NotificationPreference),
useValue: mockPrefRepo,
},
{
provide: ConfigService,
useValue: mockConfigService,
},
],
}).compile();
processor = module.get<PushNotificationProcessor>(PushNotificationProcessor);
deviceRepo = module.get<Repository<DeviceRegistration>>(getRepositoryToken(DeviceRegistration));
prefRepo = module.get<Repository<NotificationPreference>>(getRepositoryToken(NotificationPreference));
configService = module.get<ConfigService>(ConfigService);
// Reset mocks
mockSendEachForMulticast.mockReset();
mockDeviceRepo.find.mockReset();
mockDeviceRepo.delete.mockReset();
mockPrefRepo.findOne.mockReset();
});
it('should be defined', () => {
expect(processor).toBeDefined();
});
describe('handleSendPush', () => {
const jobData = {
userId: 'user1',
eventType: NotificationEventType.SPLIT_CREATED,
title: 'Title',
body: 'Body',
data: { key: 'value' },
};
const mockJob = { data: jobData } as Job;
it('should not send when there are no active devices', async () => {
mockDeviceRepo.find.mockResolvedValue([]);
await processor.handleSendPush(mockJob);
expect(mockDeviceRepo.find).toHaveBeenCalled();
expect(mockSendEachForMulticast).not.toHaveBeenCalled();
});
it('should not send when Firebase is unavailable', async () => {
mockDeviceRepo.find.mockResolvedValue([{ deviceToken: 'token1' }]);
(processor as any).firebaseApp = undefined;
await processor.handleSendPush(mockJob);
expect(mockSendEachForMulticast).not.toHaveBeenCalled();
});
it('should send if enabled and not quiet hours', async () => {
mockPrefRepo.findOne.mockResolvedValue({
pushEnabled: true,
});
mockDeviceRepo.find.mockResolvedValue([
{ deviceToken: 'token1' },
{ deviceToken: 'token2' },
]);
mockSendEachForMulticast.mockResolvedValue({
failureCount: 0,
successCount: 2,
responses: [],
});
await processor.handleSendPush(mockJob);
expect(mockDeviceRepo.find).toHaveBeenCalled();
expect(mockSendEachForMulticast).toHaveBeenCalledWith(expect.objectContaining({
tokens: ['token1', 'token2'],
notification: { title: 'Title', body: 'Body' },
}));
});
it('should handle failed tokens', async () => {
mockPrefRepo.findOne.mockResolvedValue({
pushEnabled: true,
});
mockDeviceRepo.find.mockResolvedValue([
{ deviceToken: 'token1' },
{ deviceToken: 'token2' },
]);
mockSendEachForMulticast.mockResolvedValue({
failureCount: 1,
successCount: 1,
responses: [
{ success: true },
{ success: false, error: { code: 'messaging/invalid-registration-token' } },
],
});
await processor.handleSendPush(mockJob);
// We expect delete to be called.
// Since we can't easily match In(['token2']), we just check it was called.
expect(mockDeviceRepo.delete).toHaveBeenCalled();
});
});
});