2024-08-29 20:10:09 +02:00
|
|
|
import { plainToInstance } from 'class-transformer';
|
2024-07-10 14:37:50 +02:00
|
|
|
import { defaults, SystemConfig } from 'src/config';
|
2024-08-29 20:10:09 +02:00
|
|
|
import { SystemConfigDto } from 'src/dtos/system-config.dto';
|
2024-07-21 01:00:46 +02:00
|
|
|
import { AlbumUserEntity } from 'src/entities/album-user.entity';
|
2024-08-19 20:03:33 -04:00
|
|
|
import { AssetFileEntity } from 'src/entities/asset-files.entity';
|
|
|
|
|
import { AssetFileType, UserMetadataKey } from 'src/enum';
|
2024-07-10 14:37:50 +02:00
|
|
|
import { IAlbumRepository } from 'src/interfaces/album.interface';
|
|
|
|
|
import { IAssetRepository } from 'src/interfaces/asset.interface';
|
2024-09-30 15:50:34 -04:00
|
|
|
import { IEventRepository } from 'src/interfaces/event.interface';
|
2024-10-18 13:51:34 -06:00
|
|
|
import { IJobRepository, INotifyAlbumUpdateJob, JobName, JobStatus } from 'src/interfaces/job.interface';
|
2024-07-10 14:37:50 +02:00
|
|
|
import { IUserRepository } from 'src/interfaces/user.interface';
|
2025-01-23 18:10:17 -05:00
|
|
|
import { EmailTemplate } from 'src/repositories/notification.repository';
|
2024-07-10 14:37:50 +02:00
|
|
|
import { NotificationService } from 'src/services/notification.service';
|
2025-02-07 17:26:49 -05:00
|
|
|
import { INotificationRepository, ISystemMetadataRepository } from 'src/types';
|
2024-07-21 01:00:46 +02:00
|
|
|
import { albumStub } from 'test/fixtures/album.stub';
|
|
|
|
|
import { assetStub } from 'test/fixtures/asset.stub';
|
|
|
|
|
import { userStub } from 'test/fixtures/user.stub';
|
2024-10-02 10:54:35 -04:00
|
|
|
import { newTestService } from 'test/utils';
|
2024-07-10 14:37:50 +02:00
|
|
|
import { Mocked } from 'vitest';
|
|
|
|
|
|
|
|
|
|
const configs = {
|
|
|
|
|
smtpDisabled: Object.freeze<SystemConfig>({
|
|
|
|
|
...defaults,
|
|
|
|
|
notifications: {
|
|
|
|
|
smtp: {
|
|
|
|
|
...defaults.notifications.smtp,
|
|
|
|
|
enabled: false,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
smtpEnabled: Object.freeze<SystemConfig>({
|
|
|
|
|
...defaults,
|
|
|
|
|
notifications: {
|
|
|
|
|
smtp: {
|
|
|
|
|
...defaults.notifications.smtp,
|
|
|
|
|
enabled: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
smtpTransport: Object.freeze<SystemConfig>({
|
|
|
|
|
...defaults,
|
|
|
|
|
notifications: {
|
|
|
|
|
smtp: {
|
|
|
|
|
...defaults.notifications.smtp,
|
|
|
|
|
enabled: true,
|
|
|
|
|
transport: {
|
|
|
|
|
ignoreCert: false,
|
|
|
|
|
host: 'localhost',
|
|
|
|
|
port: 587,
|
|
|
|
|
username: 'test',
|
|
|
|
|
password: 'test',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
describe(NotificationService.name, () => {
|
2024-10-02 10:54:35 -04:00
|
|
|
let sut: NotificationService;
|
|
|
|
|
|
2024-07-21 01:00:46 +02:00
|
|
|
let albumMock: Mocked<IAlbumRepository>;
|
|
|
|
|
let assetMock: Mocked<IAssetRepository>;
|
2024-09-07 13:21:05 -04:00
|
|
|
let eventMock: Mocked<IEventRepository>;
|
2024-07-21 01:00:46 +02:00
|
|
|
let jobMock: Mocked<IJobRepository>;
|
|
|
|
|
let notificationMock: Mocked<INotificationRepository>;
|
2024-07-10 14:37:50 +02:00
|
|
|
let systemMock: Mocked<ISystemMetadataRepository>;
|
|
|
|
|
let userMock: Mocked<IUserRepository>;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2024-10-02 10:54:35 -04:00
|
|
|
({ sut, albumMock, assetMock, eventMock, jobMock, notificationMock, systemMock, userMock } =
|
|
|
|
|
newTestService(NotificationService));
|
2024-07-10 14:37:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should work', () => {
|
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
|
2024-09-30 10:35:11 -04:00
|
|
|
describe('onConfigUpdate', () => {
|
|
|
|
|
it('should emit client and server events', () => {
|
2024-11-05 16:30:56 +00:00
|
|
|
const update = { oldConfig: defaults, newConfig: defaults };
|
2024-09-30 10:35:11 -04:00
|
|
|
expect(sut.onConfigUpdate(update)).toBeUndefined();
|
2024-09-30 15:50:34 -04:00
|
|
|
expect(eventMock.clientBroadcast).toHaveBeenCalledWith('on_config_update');
|
2024-09-30 10:35:11 -04:00
|
|
|
expect(eventMock.serverSend).toHaveBeenCalledWith('config.update', update);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-10 14:37:50 +02:00
|
|
|
describe('onConfigValidateEvent', () => {
|
|
|
|
|
it('validates smtp config when enabling smtp', async () => {
|
|
|
|
|
const oldConfig = configs.smtpDisabled;
|
|
|
|
|
const newConfig = configs.smtpEnabled;
|
|
|
|
|
|
|
|
|
|
notificationMock.verifySmtp.mockResolvedValue(true);
|
2024-08-15 16:12:41 -04:00
|
|
|
await expect(sut.onConfigValidate({ oldConfig, newConfig })).resolves.not.toThrow();
|
2024-07-10 14:37:50 +02:00
|
|
|
expect(notificationMock.verifySmtp).toHaveBeenCalledWith(newConfig.notifications.smtp.transport);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('validates smtp config when transport changes', async () => {
|
|
|
|
|
const oldConfig = configs.smtpEnabled;
|
|
|
|
|
const newConfig = configs.smtpTransport;
|
|
|
|
|
|
|
|
|
|
notificationMock.verifySmtp.mockResolvedValue(true);
|
2024-08-15 16:12:41 -04:00
|
|
|
await expect(sut.onConfigValidate({ oldConfig, newConfig })).resolves.not.toThrow();
|
2024-07-10 14:37:50 +02:00
|
|
|
expect(notificationMock.verifySmtp).toHaveBeenCalledWith(newConfig.notifications.smtp.transport);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('skips smtp validation when there are no changes', async () => {
|
|
|
|
|
const oldConfig = { ...configs.smtpEnabled };
|
|
|
|
|
const newConfig = { ...configs.smtpEnabled };
|
|
|
|
|
|
2024-08-15 16:12:41 -04:00
|
|
|
await expect(sut.onConfigValidate({ oldConfig, newConfig })).resolves.not.toThrow();
|
2024-07-10 14:37:50 +02:00
|
|
|
expect(notificationMock.verifySmtp).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
2024-08-29 20:10:09 +02:00
|
|
|
it('skips smtp validation with DTO when there are no changes', async () => {
|
|
|
|
|
const oldConfig = { ...configs.smtpEnabled };
|
|
|
|
|
const newConfig = plainToInstance(SystemConfigDto, configs.smtpEnabled);
|
|
|
|
|
|
|
|
|
|
await expect(sut.onConfigValidate({ oldConfig, newConfig })).resolves.not.toThrow();
|
|
|
|
|
expect(notificationMock.verifySmtp).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-10 14:37:50 +02:00
|
|
|
it('skips smtp validation when smtp is disabled', async () => {
|
|
|
|
|
const oldConfig = { ...configs.smtpEnabled };
|
|
|
|
|
const newConfig = { ...configs.smtpDisabled };
|
|
|
|
|
|
2024-08-15 16:12:41 -04:00
|
|
|
await expect(sut.onConfigValidate({ oldConfig, newConfig })).resolves.not.toThrow();
|
2024-07-10 14:37:50 +02:00
|
|
|
expect(notificationMock.verifySmtp).not.toHaveBeenCalled();
|
|
|
|
|
});
|
2024-10-08 23:08:49 +02:00
|
|
|
|
|
|
|
|
it('should fail if smtp configuration is invalid', async () => {
|
|
|
|
|
const oldConfig = configs.smtpDisabled;
|
|
|
|
|
const newConfig = configs.smtpEnabled;
|
|
|
|
|
|
|
|
|
|
notificationMock.verifySmtp.mockRejectedValue(new Error('Failed validating smtp'));
|
|
|
|
|
await expect(sut.onConfigValidate({ oldConfig, newConfig })).rejects.toBeInstanceOf(Error);
|
|
|
|
|
});
|
2024-07-10 14:37:50 +02:00
|
|
|
});
|
2024-07-21 01:00:46 +02:00
|
|
|
|
2024-09-12 14:12:39 -04:00
|
|
|
describe('onAssetHide', () => {
|
|
|
|
|
it('should send connected clients an event', () => {
|
|
|
|
|
sut.onAssetHide({ assetId: 'asset-id', userId: 'user-id' });
|
|
|
|
|
expect(eventMock.clientSend).toHaveBeenCalledWith('on_asset_hidden', 'user-id', 'asset-id');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onAssetShow', () => {
|
|
|
|
|
it('should queue the generate thumbnail job', async () => {
|
|
|
|
|
await sut.onAssetShow({ assetId: 'asset-id', userId: 'user-id' });
|
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
2024-09-28 13:47:24 -04:00
|
|
|
name: JobName.GENERATE_THUMBNAILS,
|
2024-09-12 14:12:39 -04:00
|
|
|
data: { id: 'asset-id', notify: true },
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-21 01:00:46 +02:00
|
|
|
describe('onUserSignupEvent', () => {
|
|
|
|
|
it('skips when notify is false', async () => {
|
2024-08-15 16:12:41 -04:00
|
|
|
await sut.onUserSignup({ id: '', notify: false });
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should queue notify signup event if notify is true', async () => {
|
2024-08-15 16:12:41 -04:00
|
|
|
await sut.onUserSignup({ id: '', notify: true });
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
|
name: JobName.NOTIFY_SIGNUP,
|
|
|
|
|
data: { id: '', tempPassword: undefined },
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onAlbumUpdateEvent', () => {
|
|
|
|
|
it('should queue notify album update event', async () => {
|
2024-10-18 13:51:34 -06:00
|
|
|
await sut.onAlbumUpdate({ id: 'album', recipientIds: ['42'] });
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
|
name: JobName.NOTIFY_ALBUM_UPDATE,
|
2024-10-18 13:51:34 -06:00
|
|
|
data: { id: 'album', recipientIds: ['42'], delay: 300_000 },
|
2024-07-21 01:00:46 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onAlbumInviteEvent', () => {
|
|
|
|
|
it('should queue notify album invite event', async () => {
|
2024-08-15 16:12:41 -04:00
|
|
|
await sut.onAlbumInvite({ id: '', userId: '42' });
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
|
name: JobName.NOTIFY_ALBUM_INVITE,
|
|
|
|
|
data: { id: '', recipientId: '42' },
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-08 23:08:49 +02:00
|
|
|
describe('onSessionDeleteEvent', () => {
|
|
|
|
|
it('should send a on_session_delete client event', () => {
|
|
|
|
|
vi.useFakeTimers();
|
|
|
|
|
sut.onSessionDelete({ sessionId: 'id' });
|
|
|
|
|
expect(eventMock.clientSend).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
|
|
vi.advanceTimersByTime(500);
|
|
|
|
|
|
|
|
|
|
expect(eventMock.clientSend).toHaveBeenCalledWith('on_session_delete', 'id', 'id');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-09-12 14:12:39 -04:00
|
|
|
describe('onAssetTrash', () => {
|
|
|
|
|
it('should send connected clients an event', () => {
|
|
|
|
|
sut.onAssetTrash({ assetId: 'asset-id', userId: 'user-id' });
|
|
|
|
|
expect(eventMock.clientSend).toHaveBeenCalledWith('on_asset_trash', 'user-id', ['asset-id']);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onAssetDelete', () => {
|
|
|
|
|
it('should send connected clients an event', () => {
|
|
|
|
|
sut.onAssetDelete({ assetId: 'asset-id', userId: 'user-id' });
|
|
|
|
|
expect(eventMock.clientSend).toHaveBeenCalledWith('on_asset_delete', 'user-id', 'asset-id');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onAssetsTrash', () => {
|
|
|
|
|
it('should send connected clients an event', () => {
|
|
|
|
|
sut.onAssetsTrash({ assetIds: ['asset-id'], userId: 'user-id' });
|
|
|
|
|
expect(eventMock.clientSend).toHaveBeenCalledWith('on_asset_trash', 'user-id', ['asset-id']);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onAssetsRestore', () => {
|
|
|
|
|
it('should send connected clients an event', () => {
|
|
|
|
|
sut.onAssetsRestore({ assetIds: ['asset-id'], userId: 'user-id' });
|
|
|
|
|
expect(eventMock.clientSend).toHaveBeenCalledWith('on_asset_restore', 'user-id', ['asset-id']);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onStackCreate', () => {
|
|
|
|
|
it('should send connected clients an event', () => {
|
|
|
|
|
sut.onStackCreate({ stackId: 'stack-id', userId: 'user-id' });
|
2024-09-30 15:50:34 -04:00
|
|
|
expect(eventMock.clientSend).toHaveBeenCalledWith('on_asset_stack_update', 'user-id');
|
2024-09-12 14:12:39 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onStackUpdate', () => {
|
|
|
|
|
it('should send connected clients an event', () => {
|
|
|
|
|
sut.onStackUpdate({ stackId: 'stack-id', userId: 'user-id' });
|
2024-09-30 15:50:34 -04:00
|
|
|
expect(eventMock.clientSend).toHaveBeenCalledWith('on_asset_stack_update', 'user-id');
|
2024-09-12 14:12:39 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onStackDelete', () => {
|
|
|
|
|
it('should send connected clients an event', () => {
|
|
|
|
|
sut.onStackDelete({ stackId: 'stack-id', userId: 'user-id' });
|
2024-09-30 15:50:34 -04:00
|
|
|
expect(eventMock.clientSend).toHaveBeenCalledWith('on_asset_stack_update', 'user-id');
|
2024-09-12 14:12:39 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onStacksDelete', () => {
|
|
|
|
|
it('should send connected clients an event', () => {
|
|
|
|
|
sut.onStacksDelete({ stackIds: ['stack-id'], userId: 'user-id' });
|
2024-09-30 15:50:34 -04:00
|
|
|
expect(eventMock.clientSend).toHaveBeenCalledWith('on_asset_stack_update', 'user-id');
|
2024-09-12 14:12:39 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-21 01:00:46 +02:00
|
|
|
describe('sendTestEmail', () => {
|
|
|
|
|
it('should throw error if user could not be found', async () => {
|
|
|
|
|
await expect(sut.sendTestEmail('', configs.smtpTransport.notifications.smtp)).rejects.toThrow('User not found');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw error if smtp validation fails', async () => {
|
|
|
|
|
userMock.get.mockResolvedValue(userStub.admin);
|
|
|
|
|
notificationMock.verifySmtp.mockRejectedValue('');
|
|
|
|
|
|
|
|
|
|
await expect(sut.sendTestEmail('', configs.smtpTransport.notifications.smtp)).rejects.toThrow(
|
|
|
|
|
'Failed to verify SMTP configuration',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should send email to default domain', async () => {
|
|
|
|
|
userMock.get.mockResolvedValue(userStub.admin);
|
|
|
|
|
notificationMock.verifySmtp.mockResolvedValue(true);
|
|
|
|
|
notificationMock.renderEmail.mockResolvedValue({ html: '', text: '' });
|
|
|
|
|
|
|
|
|
|
await expect(sut.sendTestEmail('', configs.smtpTransport.notifications.smtp)).resolves.not.toThrow();
|
|
|
|
|
expect(notificationMock.renderEmail).toHaveBeenCalledWith({
|
|
|
|
|
template: EmailTemplate.TEST_EMAIL,
|
|
|
|
|
data: { baseUrl: 'http://localhost:2283', displayName: userStub.admin.name },
|
|
|
|
|
});
|
|
|
|
|
expect(notificationMock.sendEmail).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
subject: 'Test email from Immich',
|
|
|
|
|
smtp: configs.smtpTransport.notifications.smtp.transport,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should send email to external domain', async () => {
|
|
|
|
|
userMock.get.mockResolvedValue(userStub.admin);
|
|
|
|
|
notificationMock.verifySmtp.mockResolvedValue(true);
|
|
|
|
|
notificationMock.renderEmail.mockResolvedValue({ html: '', text: '' });
|
|
|
|
|
systemMock.get.mockResolvedValue({ server: { externalDomain: 'https://demo.immich.app' } });
|
|
|
|
|
|
|
|
|
|
await expect(sut.sendTestEmail('', configs.smtpTransport.notifications.smtp)).resolves.not.toThrow();
|
|
|
|
|
expect(notificationMock.renderEmail).toHaveBeenCalledWith({
|
|
|
|
|
template: EmailTemplate.TEST_EMAIL,
|
|
|
|
|
data: { baseUrl: 'https://demo.immich.app', displayName: userStub.admin.name },
|
|
|
|
|
});
|
|
|
|
|
expect(notificationMock.sendEmail).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
subject: 'Test email from Immich',
|
|
|
|
|
smtp: configs.smtpTransport.notifications.smtp.transport,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should send email with replyTo', async () => {
|
|
|
|
|
userMock.get.mockResolvedValue(userStub.admin);
|
|
|
|
|
notificationMock.verifySmtp.mockResolvedValue(true);
|
|
|
|
|
notificationMock.renderEmail.mockResolvedValue({ html: '', text: '' });
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
sut.sendTestEmail('', { ...configs.smtpTransport.notifications.smtp, replyTo: 'demo@immich.app' }),
|
|
|
|
|
).resolves.not.toThrow();
|
|
|
|
|
expect(notificationMock.renderEmail).toHaveBeenCalledWith({
|
|
|
|
|
template: EmailTemplate.TEST_EMAIL,
|
|
|
|
|
data: { baseUrl: 'http://localhost:2283', displayName: userStub.admin.name },
|
|
|
|
|
});
|
|
|
|
|
expect(notificationMock.sendEmail).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
subject: 'Test email from Immich',
|
|
|
|
|
smtp: configs.smtpTransport.notifications.smtp.transport,
|
|
|
|
|
replyTo: 'demo@immich.app',
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('handleUserSignup', () => {
|
|
|
|
|
it('should skip if user could not be found', async () => {
|
|
|
|
|
await expect(sut.handleUserSignup({ id: '' })).resolves.toBe(JobStatus.SKIPPED);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be successful', async () => {
|
|
|
|
|
userMock.get.mockResolvedValue(userStub.admin);
|
|
|
|
|
systemMock.get.mockResolvedValue({ server: {} });
|
|
|
|
|
notificationMock.renderEmail.mockResolvedValue({ html: '', text: '' });
|
|
|
|
|
|
|
|
|
|
await expect(sut.handleUserSignup({ id: '' })).resolves.toBe(JobStatus.SUCCESS);
|
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
|
name: JobName.SEND_EMAIL,
|
|
|
|
|
data: expect.objectContaining({ subject: 'Welcome to Immich' }),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('handleAlbumInvite', () => {
|
|
|
|
|
it('should skip if album could not be found', async () => {
|
|
|
|
|
await expect(sut.handleAlbumInvite({ id: '', recipientId: '' })).resolves.toBe(JobStatus.SKIPPED);
|
|
|
|
|
expect(userMock.get).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should skip if recipient could not be found', async () => {
|
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.empty);
|
|
|
|
|
|
|
|
|
|
await expect(sut.handleAlbumInvite({ id: '', recipientId: '' })).resolves.toBe(JobStatus.SKIPPED);
|
|
|
|
|
expect(assetMock.getById).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should skip if the recipient has email notifications disabled', async () => {
|
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.empty);
|
|
|
|
|
userMock.get.mockResolvedValue({
|
|
|
|
|
...userStub.user1,
|
|
|
|
|
metadata: [
|
|
|
|
|
{
|
|
|
|
|
key: UserMetadataKey.PREFERENCES,
|
|
|
|
|
value: { emailNotifications: { enabled: false, albumInvite: true } },
|
|
|
|
|
userId: userStub.user1.id,
|
|
|
|
|
user: userStub.user1,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(sut.handleAlbumInvite({ id: '', recipientId: '' })).resolves.toBe(JobStatus.SKIPPED);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should skip if the recipient has email notifications for album invite disabled', async () => {
|
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.empty);
|
|
|
|
|
userMock.get.mockResolvedValue({
|
|
|
|
|
...userStub.user1,
|
|
|
|
|
metadata: [
|
|
|
|
|
{
|
|
|
|
|
key: UserMetadataKey.PREFERENCES,
|
|
|
|
|
value: { emailNotifications: { enabled: true, albumInvite: false } },
|
|
|
|
|
userId: userStub.user1.id,
|
|
|
|
|
user: userStub.user1,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(sut.handleAlbumInvite({ id: '', recipientId: '' })).resolves.toBe(JobStatus.SKIPPED);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should send invite email', async () => {
|
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.empty);
|
|
|
|
|
userMock.get.mockResolvedValue({
|
|
|
|
|
...userStub.user1,
|
|
|
|
|
metadata: [
|
|
|
|
|
{
|
|
|
|
|
key: UserMetadataKey.PREFERENCES,
|
|
|
|
|
value: { emailNotifications: { enabled: true, albumInvite: true } },
|
|
|
|
|
userId: userStub.user1.id,
|
|
|
|
|
user: userStub.user1,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
systemMock.get.mockResolvedValue({ server: {} });
|
|
|
|
|
notificationMock.renderEmail.mockResolvedValue({ html: '', text: '' });
|
|
|
|
|
|
|
|
|
|
await expect(sut.handleAlbumInvite({ id: '', recipientId: '' })).resolves.toBe(JobStatus.SUCCESS);
|
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
|
name: JobName.SEND_EMAIL,
|
|
|
|
|
data: expect.objectContaining({ subject: expect.stringContaining('You have been added to a shared album') }),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should send invite email without album thumbnail if thumbnail asset does not exist', async () => {
|
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.emptyWithValidThumbnail);
|
|
|
|
|
userMock.get.mockResolvedValue({
|
|
|
|
|
...userStub.user1,
|
|
|
|
|
metadata: [
|
|
|
|
|
{
|
|
|
|
|
key: UserMetadataKey.PREFERENCES,
|
|
|
|
|
value: { emailNotifications: { enabled: true, albumInvite: true } },
|
|
|
|
|
userId: userStub.user1.id,
|
|
|
|
|
user: userStub.user1,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
systemMock.get.mockResolvedValue({ server: {} });
|
|
|
|
|
notificationMock.renderEmail.mockResolvedValue({ html: '', text: '' });
|
|
|
|
|
|
|
|
|
|
await expect(sut.handleAlbumInvite({ id: '', recipientId: '' })).resolves.toBe(JobStatus.SUCCESS);
|
2024-08-19 20:03:33 -04:00
|
|
|
expect(assetMock.getById).toHaveBeenCalledWith(albumStub.emptyWithValidThumbnail.albumThumbnailAssetId, {
|
|
|
|
|
files: true,
|
|
|
|
|
});
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
|
name: JobName.SEND_EMAIL,
|
|
|
|
|
data: expect.objectContaining({
|
|
|
|
|
subject: expect.stringContaining('You have been added to a shared album'),
|
|
|
|
|
imageAttachments: undefined,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should send invite email with album thumbnail as jpeg', async () => {
|
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.emptyWithValidThumbnail);
|
|
|
|
|
userMock.get.mockResolvedValue({
|
|
|
|
|
...userStub.user1,
|
|
|
|
|
metadata: [
|
|
|
|
|
{
|
|
|
|
|
key: UserMetadataKey.PREFERENCES,
|
|
|
|
|
value: { emailNotifications: { enabled: true, albumInvite: true } },
|
|
|
|
|
userId: userStub.user1.id,
|
|
|
|
|
user: userStub.user1,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
systemMock.get.mockResolvedValue({ server: {} });
|
|
|
|
|
notificationMock.renderEmail.mockResolvedValue({ html: '', text: '' });
|
2024-08-19 20:03:33 -04:00
|
|
|
assetMock.getById.mockResolvedValue({
|
|
|
|
|
...assetStub.image,
|
|
|
|
|
files: [{ assetId: 'asset-id', type: AssetFileType.THUMBNAIL, path: 'path-to-thumb.jpg' } as AssetFileEntity],
|
|
|
|
|
});
|
2024-07-21 01:00:46 +02:00
|
|
|
|
|
|
|
|
await expect(sut.handleAlbumInvite({ id: '', recipientId: '' })).resolves.toBe(JobStatus.SUCCESS);
|
2024-08-19 20:03:33 -04:00
|
|
|
expect(assetMock.getById).toHaveBeenCalledWith(albumStub.emptyWithValidThumbnail.albumThumbnailAssetId, {
|
|
|
|
|
files: true,
|
|
|
|
|
});
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
|
name: JobName.SEND_EMAIL,
|
|
|
|
|
data: expect.objectContaining({
|
|
|
|
|
subject: expect.stringContaining('You have been added to a shared album'),
|
|
|
|
|
imageAttachments: [{ filename: 'album-thumbnail.jpg', path: expect.anything(), cid: expect.anything() }],
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should send invite email with album thumbnail and arbitrary extension', async () => {
|
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.emptyWithValidThumbnail);
|
|
|
|
|
userMock.get.mockResolvedValue({
|
|
|
|
|
...userStub.user1,
|
|
|
|
|
metadata: [
|
|
|
|
|
{
|
|
|
|
|
key: UserMetadataKey.PREFERENCES,
|
|
|
|
|
value: { emailNotifications: { enabled: true, albumInvite: true } },
|
|
|
|
|
userId: userStub.user1.id,
|
|
|
|
|
user: userStub.user1,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
systemMock.get.mockResolvedValue({ server: {} });
|
|
|
|
|
notificationMock.renderEmail.mockResolvedValue({ html: '', text: '' });
|
|
|
|
|
assetMock.getById.mockResolvedValue(assetStub.image);
|
|
|
|
|
|
|
|
|
|
await expect(sut.handleAlbumInvite({ id: '', recipientId: '' })).resolves.toBe(JobStatus.SUCCESS);
|
2024-08-19 20:03:33 -04:00
|
|
|
expect(assetMock.getById).toHaveBeenCalledWith(albumStub.emptyWithValidThumbnail.albumThumbnailAssetId, {
|
|
|
|
|
files: true,
|
|
|
|
|
});
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
|
name: JobName.SEND_EMAIL,
|
|
|
|
|
data: expect.objectContaining({
|
|
|
|
|
subject: expect.stringContaining('You have been added to a shared album'),
|
|
|
|
|
imageAttachments: [{ filename: 'album-thumbnail.ext', path: expect.anything(), cid: expect.anything() }],
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('handleAlbumUpdate', () => {
|
|
|
|
|
it('should skip if album could not be found', async () => {
|
2024-10-18 13:51:34 -06:00
|
|
|
await expect(sut.handleAlbumUpdate({ id: '', recipientIds: ['1'] })).resolves.toBe(JobStatus.SKIPPED);
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(userMock.get).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should skip if owner could not be found', async () => {
|
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.emptyWithValidThumbnail);
|
|
|
|
|
|
2024-10-18 13:51:34 -06:00
|
|
|
await expect(sut.handleAlbumUpdate({ id: '', recipientIds: ['1'] })).resolves.toBe(JobStatus.SKIPPED);
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(systemMock.get).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should skip recipient that could not be looked up', async () => {
|
|
|
|
|
albumMock.getById.mockResolvedValue({
|
|
|
|
|
...albumStub.emptyWithValidThumbnail,
|
|
|
|
|
albumUsers: [{ user: { id: userStub.user1.id } } as AlbumUserEntity],
|
|
|
|
|
});
|
|
|
|
|
userMock.get.mockResolvedValueOnce(userStub.user1);
|
|
|
|
|
notificationMock.renderEmail.mockResolvedValue({ html: '', text: '' });
|
|
|
|
|
|
2024-10-18 13:51:34 -06:00
|
|
|
await sut.handleAlbumUpdate({ id: '', recipientIds: [userStub.user1.id] });
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(userStub.user1.id, { withDeleted: false });
|
|
|
|
|
expect(notificationMock.renderEmail).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should skip recipient with disabled email notifications', async () => {
|
|
|
|
|
albumMock.getById.mockResolvedValue({
|
|
|
|
|
...albumStub.emptyWithValidThumbnail,
|
|
|
|
|
albumUsers: [{ user: { id: userStub.user1.id } } as AlbumUserEntity],
|
|
|
|
|
});
|
|
|
|
|
userMock.get.mockResolvedValue({
|
|
|
|
|
...userStub.user1,
|
|
|
|
|
metadata: [
|
|
|
|
|
{
|
|
|
|
|
key: UserMetadataKey.PREFERENCES,
|
|
|
|
|
value: { emailNotifications: { enabled: false, albumUpdate: true } },
|
|
|
|
|
user: userStub.user1,
|
|
|
|
|
userId: userStub.user1.id,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
notificationMock.renderEmail.mockResolvedValue({ html: '', text: '' });
|
|
|
|
|
|
2024-10-18 13:51:34 -06:00
|
|
|
await sut.handleAlbumUpdate({ id: '', recipientIds: [userStub.user1.id] });
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(userStub.user1.id, { withDeleted: false });
|
|
|
|
|
expect(notificationMock.renderEmail).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should skip recipient with disabled email notifications for the album update event', async () => {
|
|
|
|
|
albumMock.getById.mockResolvedValue({
|
|
|
|
|
...albumStub.emptyWithValidThumbnail,
|
|
|
|
|
albumUsers: [{ user: { id: userStub.user1.id } } as AlbumUserEntity],
|
|
|
|
|
});
|
|
|
|
|
userMock.get.mockResolvedValue({
|
|
|
|
|
...userStub.user1,
|
|
|
|
|
metadata: [
|
|
|
|
|
{
|
|
|
|
|
key: UserMetadataKey.PREFERENCES,
|
|
|
|
|
value: { emailNotifications: { enabled: true, albumUpdate: false } },
|
|
|
|
|
user: userStub.user1,
|
|
|
|
|
userId: userStub.user1.id,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
notificationMock.renderEmail.mockResolvedValue({ html: '', text: '' });
|
|
|
|
|
|
2024-10-18 13:51:34 -06:00
|
|
|
await sut.handleAlbumUpdate({ id: '', recipientIds: [userStub.user1.id] });
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(userStub.user1.id, { withDeleted: false });
|
|
|
|
|
expect(notificationMock.renderEmail).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should send email', async () => {
|
|
|
|
|
albumMock.getById.mockResolvedValue({
|
|
|
|
|
...albumStub.emptyWithValidThumbnail,
|
|
|
|
|
albumUsers: [{ user: { id: userStub.user1.id } } as AlbumUserEntity],
|
|
|
|
|
});
|
|
|
|
|
userMock.get.mockResolvedValue(userStub.user1);
|
|
|
|
|
notificationMock.renderEmail.mockResolvedValue({ html: '', text: '' });
|
|
|
|
|
|
2024-10-18 13:51:34 -06:00
|
|
|
await sut.handleAlbumUpdate({ id: '', recipientIds: [userStub.user1.id] });
|
2024-07-21 01:00:46 +02:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(userStub.user1.id, { withDeleted: false });
|
|
|
|
|
expect(notificationMock.renderEmail).toHaveBeenCalled();
|
|
|
|
|
expect(jobMock.queue).toHaveBeenCalled();
|
|
|
|
|
});
|
2024-10-18 13:51:34 -06:00
|
|
|
|
|
|
|
|
it('should add new recipients for new images if job is already queued', async () => {
|
|
|
|
|
jobMock.removeJob.mockResolvedValue({ id: '1', recipientIds: ['2', '3', '4'] } as INotifyAlbumUpdateJob);
|
|
|
|
|
await sut.onAlbumUpdate({ id: '1', recipientIds: ['1', '2', '3'] } as INotifyAlbumUpdateJob);
|
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
|
name: JobName.NOTIFY_ALBUM_UPDATE,
|
|
|
|
|
data: {
|
|
|
|
|
id: '1',
|
|
|
|
|
delay: 300_000,
|
|
|
|
|
recipientIds: ['1', '2', '3', '4'],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-07-21 01:00:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('handleSendEmail', () => {
|
|
|
|
|
it('should skip if smtp notifications are disabled', async () => {
|
|
|
|
|
systemMock.get.mockResolvedValue({ notifications: { smtp: { enabled: false } } });
|
|
|
|
|
await expect(sut.handleSendEmail({ html: '', subject: '', text: '', to: '' })).resolves.toBe(JobStatus.SKIPPED);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should send mail successfully', async () => {
|
|
|
|
|
systemMock.get.mockResolvedValue({ notifications: { smtp: { enabled: true, from: 'test@immich.app' } } });
|
|
|
|
|
notificationMock.sendEmail.mockResolvedValue({ messageId: '', response: '' });
|
|
|
|
|
|
|
|
|
|
await expect(sut.handleSendEmail({ html: '', subject: '', text: '', to: '' })).resolves.toBe(JobStatus.SUCCESS);
|
|
|
|
|
expect(notificationMock.sendEmail).toHaveBeenCalledWith(expect.objectContaining({ replyTo: 'test@immich.app' }));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should send mail with replyTo successfully', async () => {
|
|
|
|
|
systemMock.get.mockResolvedValue({
|
|
|
|
|
notifications: { smtp: { enabled: true, from: 'test@immich.app', replyTo: 'demo@immich.app' } },
|
|
|
|
|
});
|
|
|
|
|
notificationMock.sendEmail.mockResolvedValue({ messageId: '', response: '' });
|
|
|
|
|
|
|
|
|
|
await expect(sut.handleSendEmail({ html: '', subject: '', text: '', to: '' })).resolves.toBe(JobStatus.SUCCESS);
|
|
|
|
|
expect(notificationMock.sendEmail).toHaveBeenCalledWith(expect.objectContaining({ replyTo: 'demo@immich.app' }));
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-07-10 14:37:50 +02:00
|
|
|
});
|