Skip to content

Commit b20c97e

Browse files
committed
Make sendAPN test independent of cexpired cert
1 parent 6d2bd21 commit b20c97e

File tree

6 files changed

+63
-67
lines changed

6 files changed

+63
-67
lines changed

.github/copilot-instructions.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
# Copilot Instructions for AI Agents
22

33
## Project Overview
4+
45
This repository implements a Node.js module for sending push notifications across multiple platforms: Apple (APN), Google (GCM/FCM), Windows (WNS), Amazon (ADM), and Web-Push. The core logic is in `lib/` and `src/`, with each platform handled by a dedicated file (e.g., `sendAPN.js`, `sendFCM.js`).
56

67
## Architecture & Data Flow
8+
79
- **Entry Point:** Use `PushNotifications` from `lib/index.js` or `src/index.js`.
810
- **Settings:** Each platform's credentials/config are passed to the constructor. See the example in `README.md`.
911
- **Sending:** The main method is `push.send(registrationIds, data, callback)` or as a Promise. It auto-detects device type and routes to the correct sender.
1012
- **Platform Senders:** Each sender (e.g., `sendAPN.js`, `sendFCM.js`) implements the logic for its platform. Shared utilities are in `lib/utils/` and `src/utils/`.
1113
- **RegId Detection:** Device type is inferred from the registration ID structure. See `PN.getPushMethodByRegId` for details.
1214

1315
## Developer Workflows
16+
1417
- **Install:** `npm install`
1518
- **Test:** Run all tests with `npm test`. Tests are in `test/` and cover basic flows and platform-specific cases.
1619
- **Debug:** Use the callback or Promise error/result from `push.send`. Each result object includes method, success/failure counts, and error details per device.
1720
- **Build:** No build step required for basic usage. ES6 is used, but compatible with ES5 via Babel if needed.
1821

1922
## Conventions & Patterns
23+
2024
- **Platform-specific files:** Each push service has its own file for isolation and clarity.
2125
- **Unified Data Model:** The `data` object for notifications is normalized across platforms. See `README.md` for all supported fields.
2226
- **Error Handling:** Errors are unified and returned in the result array from `push.send`.
@@ -25,6 +29,7 @@ This repository implements a Node.js module for sending push notifications acros
2529
- **Constants:** Use constants from `constants.js` for platform types.
2630

2731
## Integration Points
32+
2833
- **External Libraries:**
2934
- APN: `node-apn`
3035
- FCM: `firebase-admin`
@@ -35,12 +40,15 @@ This repository implements a Node.js module for sending push notifications acros
3540
- **Credentials:** Place service account keys and certificates in appropriate locations (see `README.md` for examples).
3641

3742
## Key Files & Directories
43+
3844
- `lib/` and `src/`: Main implementation (mirrored structure)
3945
- `test/`: Test cases and sample credentials
4046
- `README.md`: Usage, configuration, and data model reference
4147

4248
## Example Usage
49+
4350
See `README.md` for a full example of settings, registration ID formats, and sending notifications.
4451

4552
---
53+
4654
For unclear or incomplete sections, please provide feedback or specify which workflows, patterns, or integrations need further documentation.

package-lock.json

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/push-notifications/basic.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ describe('push-notifications: instantiation and class properties', () => {
5353
keyId: 'testKeyId',
5454
teamId: 'testTeamId',
5555
},
56-
cert: path.resolve('test/send/cert.pem'),
57-
key: path.resolve('test/send/key.pem'),
56+
cert: 'some-file-path',
57+
key: 'some-file-path',
5858
},
5959
wns: {
6060
client_id: 'client id',

test/send/cert.pem

Lines changed: 0 additions & 23 deletions
This file was deleted.

test/send/key.pem

Lines changed: 0 additions & 28 deletions
This file was deleted.

test/send/sendAPN.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
/* eslint-env mocha */
2-
import path from 'path';
32
import chai from 'chai';
43
import sinon from 'sinon';
54
import sinonChai from 'sinon-chai';
65
import dirtyChai from 'dirty-chai';
7-
import { readFileSync } from 'fs';
86

97
import apn from '@parse/node-apn';
108
import PN from '../../src';
119
import APN from '../../src/sendAPN';
10+
1211
import {
1312
sendOkMethodGCM,
1413
testPushSuccess,
1514
testPushError,
1615
testPushException,
1716
} from '../util';
1817

18+
// Mock apn certificate loading to prevent file access
19+
before(() => {
20+
// Stub out the Provider constructor to avoid certificate loading
21+
sinon.stub(apn, 'Provider', function mockProvider(options) {
22+
this.client = { config: options };
23+
});
24+
// Ensure prototype methods exist for tests to stub
25+
if (!apn.Provider.prototype.send) {
26+
apn.Provider.prototype.send = () => {};
27+
}
28+
if (!apn.Provider.prototype.shutdown) {
29+
apn.Provider.prototype.shutdown = () => {};
30+
}
31+
});
32+
after(() => {
33+
sinon.restore();
34+
});
35+
1936
const { expect } = chai;
2037
chai.use(dirtyChai);
2138
chai.use(sinonChai);
@@ -36,12 +53,10 @@ const data = {
3653
};
3754
const experienceId = '43e798c-4dff073-2b58a85a-27c5d9fdf59-b69';
3855
const apnOptions = {
39-
cert: path.resolve('./test/send/cert.pem'),
40-
key: path.resolve('./test/send/key.pem'),
56+
cert: 'DUMMY_CERT',
57+
key: 'DUMMY_KEY',
4158
};
42-
const pn = new PN({
43-
apn: apnOptions,
44-
});
59+
let pn;
4560
const fErr = new Error('Forced error');
4661
const errStatusCode = '410';
4762

@@ -59,12 +74,12 @@ function sendOkMethod() {
5974
apn.Provider.prototype,
6075
'send',
6176
function sendAPN(message, _regIds) {
62-
// TODO: validate other props? What about token?
77+
// Validate config uses dummy cert/key
6378
expect(this.client.config)
6479
.to.be.an('object')
6580
.includes.keys(['cert', 'key']);
66-
expect(this.client.config.cert).to.eql(readFileSync(apnOptions.cert));
67-
expect(this.client.config.key).to.eql(readFileSync(apnOptions.key));
81+
expect(this.client.config.cert).to.eql('DUMMY_CERT');
82+
expect(this.client.config.key).to.eql('DUMMY_KEY');
6883

6984
expect(_regIds).to.be.instanceOf(Array);
7085
_regIds.forEach((regId) => expect(regIds).to.include(regId));
@@ -131,6 +146,12 @@ function sendThrowExceptionMethod() {
131146
}
132147

133148
describe('push-notifications-apn', () => {
149+
before(() => {
150+
pn = new PN({
151+
apn: apnOptions,
152+
});
153+
});
154+
134155
describe('send push notifications successfully', () => {
135156
before(() => {
136157
sendMethod = sendOkMethod();
@@ -718,10 +739,12 @@ describe('push-notifications-apn', () => {
718739
});
719740

720741
describe('shutdown', () => {
721-
const connectionStub = sinon.stub(apn.Provider.prototype, 'shutdown');
722-
const apnInstance = new APN(apnOptions);
742+
let connectionStub;
743+
let apnInstance;
723744

724745
before(() => {
746+
connectionStub = sinon.stub(apn.Provider.prototype, 'shutdown');
747+
apnInstance = new APN(apnOptions);
725748
apnInstance.shutdown();
726749
});
727750

0 commit comments

Comments
 (0)