Files
upstream-rocketchat/apps/meteor/server/lib/notifications/push/apn.spec.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

164 lines
4.1 KiB
TypeScript

import { expect } from 'chai';
import { describe, it, beforeEach } from 'mocha';
import proxyquire from 'proxyquire';
import sinon from 'sinon';
const sandbox = sinon.createSandbox();
const mocks = {
logger: {
debug: sandbox.stub(),
warn: sandbox.stub(),
error: sandbox.stub(),
},
ApnProvider: sandbox.stub(),
};
const apnMock = {
'Provider': mocks.ApnProvider,
'Notification': sandbox.stub(),
'@noCallThru': true,
};
const { initAPN, shutdownAPN } = proxyquire.noCallThru().load('./apn', {
'@parse/node-apn': {
default: apnMock,
...apnMock,
},
'./logger': { logger: mocks.logger },
});
const baseOptions = {
apn: {
cert: 'cert-data',
key: 'key-data',
gateway: undefined as string | undefined,
},
production: false,
};
const buildOptions = (overrides: Record<string, unknown> = {}, apnOverrides: Record<string, unknown> = {}) => ({
...baseOptions,
...overrides,
apn: {
...baseOptions.apn,
...apnOverrides,
},
});
describe('initAPN', () => {
beforeEach(() => {
sandbox.resetHistory();
mocks.ApnProvider.reset();
});
describe('APN provider initialization', () => {
it('should create apn.Provider with correct options', () => {
const options = buildOptions({ production: true }, { gateway: 'gateway.push.apple.com' });
initAPN({
options,
absoluteUrl: 'https://example.com',
});
expect(mocks.ApnProvider.calledWithNew()).to.be.true;
});
it('should pass production flag from options to Provider', () => {
initAPN({
options: buildOptions({ production: true }),
absoluteUrl: 'https://example.com',
});
expect(mocks.ApnProvider.firstCall.args[0]).to.have.property('production', true);
});
it('should pass production false when options.production is false', () => {
initAPN({
options: buildOptions({ production: false }),
absoluteUrl: 'https://example.com',
});
expect(mocks.ApnProvider.firstCall.args[0]).to.have.property('production', false);
});
it('should pass cert and key to Provider', () => {
initAPN({
options: buildOptions({}, { cert: 'my-cert', key: 'my-key' }),
absoluteUrl: 'https://example.com',
});
const providerArgs = mocks.ApnProvider.firstCall.args[0];
expect(providerArgs).to.have.property('cert', 'my-cert');
expect(providerArgs).to.have.property('key', 'my-key');
});
it('should pass gateway to Provider when specified', () => {
initAPN({
options: buildOptions({}, { gateway: 'gateway.push.apple.com' }),
absoluteUrl: 'https://example.com',
});
expect(mocks.ApnProvider.firstCall.args[0]).to.have.property('gateway', 'gateway.push.apple.com');
});
it('should spread all apn options to Provider', () => {
initAPN({
options: buildOptions({ production: true }, { cert: 'c', key: 'k', gateway: 'gateway.sandbox.push.apple.com' }),
absoluteUrl: 'https://example.com',
});
const providerArgs = mocks.ApnProvider.firstCall.args[0];
expect(providerArgs).to.deep.equal({
cert: 'c',
key: 'k',
gateway: 'gateway.sandbox.push.apple.com',
production: true,
});
});
it('should not throw when Provider constructor throws', () => {
mocks.ApnProvider.throws(new Error('APN init failed'));
expect(() =>
initAPN({
options: buildOptions(),
absoluteUrl: 'https://example.com',
}),
).to.not.throw();
});
});
});
describe('shutdownAPN', () => {
beforeEach(() => {
sandbox.resetHistory();
mocks.ApnProvider.reset();
});
it('should shut down the active provider', async () => {
const shutdown = sandbox.stub().resolves();
mocks.ApnProvider.returns({ shutdown });
initAPN({ options: buildOptions(), absoluteUrl: 'https://example.com' });
await shutdownAPN();
expect(shutdown.calledOnce).to.be.true;
});
it('should resolve when no provider was initialized', async () => {
await shutdownAPN();
await shutdownAPN();
});
it('should not reject when provider shutdown fails', async () => {
const shutdown = sandbox.stub().rejects(new Error('shutdown failed'));
mocks.ApnProvider.returns({ shutdown });
initAPN({ options: buildOptions(), absoluteUrl: 'https://example.com' });
await shutdownAPN();
expect(mocks.logger.error.called).to.be.true;
});
});