backend-test-tools 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +51 -0
- package/.mocharc.js +9 -0
- package/.prettierrc +7 -0
- package/README.md +271 -0
- package/babel.config.json +33 -0
- package/dist/factory/factory.d.ts +11 -0
- package/dist/factory/factory.js +39 -0
- package/dist/factory/factory.js.map +1 -0
- package/dist/factory/factory.types.d.ts +38 -0
- package/dist/factory/factory.types.js +2 -0
- package/dist/factory/factory.types.js.map +1 -0
- package/dist/factory/index.d.ts +2 -0
- package/dist/factory/index.js +28 -0
- package/dist/factory/index.js.map +1 -0
- package/dist/factory/workers/connection.d.ts +3 -0
- package/dist/factory/workers/connection.js +25 -0
- package/dist/factory/workers/connection.js.map +1 -0
- package/dist/factory/workers/connection.types.d.ts +28 -0
- package/dist/factory/workers/connection.types.js +2 -0
- package/dist/factory/workers/connection.types.js.map +1 -0
- package/dist/factory/workers/index.d.ts +6 -0
- package/dist/factory/workers/index.js +72 -0
- package/dist/factory/workers/index.js.map +1 -0
- package/dist/factory/workers/organization.d.ts +3 -0
- package/dist/factory/workers/organization.js +36 -0
- package/dist/factory/workers/organization.js.map +1 -0
- package/dist/factory/workers/organization.types.d.ts +51 -0
- package/dist/factory/workers/organization.types.js +2 -0
- package/dist/factory/workers/organization.types.js.map +1 -0
- package/dist/factory/workers/user.d.ts +5 -0
- package/dist/factory/workers/user.js +60 -0
- package/dist/factory/workers/user.js.map +1 -0
- package/dist/factory/workers/user.types.d.ts +76 -0
- package/dist/factory/workers/user.types.js +2 -0
- package/dist/factory/workers/user.types.js.map +1 -0
- package/dist/generators/generateAccessToken.d.ts +3 -0
- package/dist/generators/generateAccessToken.js +27 -0
- package/dist/generators/generateAccessToken.js.map +1 -0
- package/dist/generators/generateChatId.d.ts +2 -0
- package/dist/generators/generateChatId.js +13 -0
- package/dist/generators/generateChatId.js.map +1 -0
- package/dist/generators/generateEcdsaKeyPair.d.ts +2 -0
- package/dist/generators/generateEcdsaKeyPair.js +31 -0
- package/dist/generators/generateEcdsaKeyPair.js.map +1 -0
- package/dist/generators/generateImageBuffer.d.ts +3 -0
- package/dist/generators/generateImageBuffer.js +23 -0
- package/dist/generators/generateImageBuffer.js.map +1 -0
- package/dist/generators/generateImageBuffer.types.d.ts +7 -0
- package/dist/generators/generateImageBuffer.types.js +2 -0
- package/dist/generators/generateImageBuffer.types.js.map +1 -0
- package/dist/generators/generateKeyPair.d.ts +6 -0
- package/dist/generators/generateKeyPair.js +36 -0
- package/dist/generators/generateKeyPair.js.map +1 -0
- package/dist/generators/generateKeyPair.types.d.ts +9 -0
- package/dist/generators/generateKeyPair.types.js +2 -0
- package/dist/generators/generateKeyPair.types.js.map +1 -0
- package/dist/generators/generateLocale.d.ts +5 -0
- package/dist/generators/generateLocale.js +16 -0
- package/dist/generators/generateLocale.js.map +1 -0
- package/dist/generators/generateRegId.d.ts +4 -0
- package/dist/generators/generateRegId.js +14 -0
- package/dist/generators/generateRegId.js.map +1 -0
- package/dist/generators/generateRsaKeyPair.d.ts +2 -0
- package/dist/generators/generateRsaKeyPair.js +22 -0
- package/dist/generators/generateRsaKeyPair.js.map +1 -0
- package/dist/generators/generateUserCrypto.d.ts +15 -0
- package/dist/generators/generateUserCrypto.js +33 -0
- package/dist/generators/generateUserCrypto.js.map +1 -0
- package/dist/generators/index.d.ts +9 -0
- package/dist/generators/index.js +105 -0
- package/dist/generators/index.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +50 -0
- package/dist/index.js.map +1 -0
- package/dist/stubs/index.d.ts +1 -0
- package/dist/stubs/index.js +17 -0
- package/dist/stubs/index.js.map +1 -0
- package/dist/stubs/server-engine/Leader.types.d.ts +8 -0
- package/dist/stubs/server-engine/Leader.types.js +2 -0
- package/dist/stubs/server-engine/Leader.types.js.map +1 -0
- package/dist/stubs/server-engine/Localizator.d.ts +3 -0
- package/dist/stubs/server-engine/Localizator.js +34 -0
- package/dist/stubs/server-engine/Localizator.js.map +1 -0
- package/dist/stubs/server-engine/Localizator.types.d.ts +22 -0
- package/dist/stubs/server-engine/Localizator.types.js +2 -0
- package/dist/stubs/server-engine/Localizator.types.js.map +1 -0
- package/dist/stubs/server-engine/PubSub.d.ts +3 -0
- package/dist/stubs/server-engine/PubSub.js +24 -0
- package/dist/stubs/server-engine/PubSub.js.map +1 -0
- package/dist/stubs/server-engine/PubSub.types.d.ts +14 -0
- package/dist/stubs/server-engine/PubSub.types.js +2 -0
- package/dist/stubs/server-engine/PubSub.types.js.map +1 -0
- package/dist/stubs/server-engine/PushNotification.d.ts +3 -0
- package/dist/stubs/server-engine/PushNotification.js +18 -0
- package/dist/stubs/server-engine/PushNotification.js.map +1 -0
- package/dist/stubs/server-engine/PushNotification.types.d.ts +8 -0
- package/dist/stubs/server-engine/PushNotification.types.js +2 -0
- package/dist/stubs/server-engine/PushNotification.types.js.map +1 -0
- package/dist/stubs/server-engine/index.d.ts +4 -0
- package/dist/stubs/server-engine/index.js +50 -0
- package/dist/stubs/server-engine/index.js.map +1 -0
- package/dist/stubs/server-engine/sendPush.d.ts +3 -0
- package/dist/stubs/server-engine/sendPush.js +15 -0
- package/dist/stubs/server-engine/sendPush.js.map +1 -0
- package/dist/utils/generateCerts/generateCerts.const.d.ts +5 -0
- package/dist/utils/generateCerts/generateCerts.const.js +35 -0
- package/dist/utils/generateCerts/generateCerts.const.js.map +1 -0
- package/dist/utils/generateCerts/generateCerts.d.ts +9 -0
- package/dist/utils/generateCerts/generateCerts.js +108 -0
- package/dist/utils/generateCerts/generateCerts.js.map +1 -0
- package/dist/utils/generateCerts/generateCerts.types.d.ts +19 -0
- package/dist/utils/generateCerts/generateCerts.types.js +2 -0
- package/dist/utils/generateCerts/generateCerts.types.js.map +1 -0
- package/dist/utils/generateCerts/index.d.ts +1 -0
- package/dist/utils/generateCerts/index.js +17 -0
- package/dist/utils/generateCerts/index.js.map +1 -0
- package/dist/utils/getAdminApiBearer.d.ts +2 -0
- package/dist/utils/getAdminApiBearer.js +19 -0
- package/dist/utils/getAdminApiBearer.js.map +1 -0
- package/dist/utils/getBearer.d.ts +3 -0
- package/dist/utils/getBearer.js +12 -0
- package/dist/utils/getBearer.js.map +1 -0
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.js +72 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/jwtRequest.d.ts +9 -0
- package/dist/utils/jwtRequest.js +29 -0
- package/dist/utils/jwtRequest.js.map +1 -0
- package/dist/utils/staticRequest.d.ts +10 -0
- package/dist/utils/staticRequest.js +24 -0
- package/dist/utils/staticRequest.js.map +1 -0
- package/dist/utils/tlsRequest.d.ts +6 -0
- package/dist/utils/tlsRequest.js +44 -0
- package/dist/utils/tlsRequest.js.map +1 -0
- package/dist/utils/tlsRequest.types.d.ts +9 -0
- package/dist/utils/tlsRequest.types.js +2 -0
- package/dist/utils/tlsRequest.types.js.map +1 -0
- package/eslint.config.mjs +11 -0
- package/jsconfig.json +14 -0
- package/package.json +83 -0
- package/src/factory/factory.ts +51 -0
- package/src/factory/factory.types.ts +50 -0
- package/src/factory/index.ts +2 -0
- package/src/factory/workers/connection.ts +22 -0
- package/src/factory/workers/connection.types.ts +29 -0
- package/src/factory/workers/index.ts +6 -0
- package/src/factory/workers/organization.ts +35 -0
- package/src/factory/workers/organization.types.ts +53 -0
- package/src/factory/workers/user.ts +57 -0
- package/src/factory/workers/user.types.ts +77 -0
- package/src/generators/generateAccessToken.ts +24 -0
- package/src/generators/generateChatId.ts +6 -0
- package/src/generators/generateEcdsaKeyPair.ts +16 -0
- package/src/generators/generateImageBuffer.ts +19 -0
- package/src/generators/generateImageBuffer.types.ts +7 -0
- package/src/generators/generateKeyPair.ts +24 -0
- package/src/generators/generateKeyPair.types.ts +9 -0
- package/src/generators/generateLocale.ts +28 -0
- package/src/generators/generateRegId.ts +9 -0
- package/src/generators/generateRsaKeyPair.ts +12 -0
- package/src/generators/generateUserCrypto.ts +24 -0
- package/src/generators/index.ts +9 -0
- package/src/index.ts +4 -0
- package/src/stubs/index.ts +1 -0
- package/src/stubs/server-engine/Leader.types.ts +9 -0
- package/src/stubs/server-engine/Localizator.ts +25 -0
- package/src/stubs/server-engine/Localizator.types.ts +24 -0
- package/src/stubs/server-engine/PubSub.ts +13 -0
- package/src/stubs/server-engine/PubSub.types.ts +15 -0
- package/src/stubs/server-engine/PushNotification.ts +11 -0
- package/src/stubs/server-engine/PushNotification.types.ts +9 -0
- package/src/stubs/server-engine/index.ts +4 -0
- package/src/stubs/server-engine/sendPush.ts +7 -0
- package/src/utils/generateCerts/generateCerts.const.ts +48 -0
- package/src/utils/generateCerts/generateCerts.ts +108 -0
- package/src/utils/generateCerts/generateCerts.types.ts +21 -0
- package/src/utils/generateCerts/index.ts +1 -0
- package/src/utils/getAdminApiBearer.ts +17 -0
- package/src/utils/getBearer.ts +11 -0
- package/src/utils/index.ts +6 -0
- package/src/utils/jwtRequest.ts +25 -0
- package/src/utils/staticRequest.ts +22 -0
- package/src/utils/tlsRequest.ts +41 -0
- package/src/utils/tlsRequest.types.ts +9 -0
- package/test/env.ts +10 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.json +30 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {PubSub} from '@gwcdata/node-server-engine';
|
|
2
|
+
import {stub} from 'sinon';
|
|
3
|
+
import {PubSubStub} from './PubSub.types';
|
|
4
|
+
|
|
5
|
+
/** Stub the PubSub service */
|
|
6
|
+
export function stubPubSub(): PubSubStub {
|
|
7
|
+
const init = stub(PubSub, 'init').resolves();
|
|
8
|
+
const shutdown = stub(PubSub, 'shutdown').resolves();
|
|
9
|
+
const addPublisher = stub(PubSub, 'addPublisher').returns();
|
|
10
|
+
const addSubscriber = stub(PubSub, 'addSubscriber').returns();
|
|
11
|
+
const publish = stub(PubSub, 'publish').resolves();
|
|
12
|
+
return {init, shutdown, addPublisher, addSubscriber, publish};
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {SinonStub} from 'sinon';
|
|
2
|
+
|
|
3
|
+
/** Stubbed properties of the Pubsub entity */
|
|
4
|
+
export interface PubSubStub {
|
|
5
|
+
/** Stub of the init method */
|
|
6
|
+
init: SinonStub;
|
|
7
|
+
/** Stub of the shutdown method */
|
|
8
|
+
shutdown: SinonStub;
|
|
9
|
+
/** Stub of the addPublisher method */
|
|
10
|
+
addPublisher: SinonStub;
|
|
11
|
+
/** Stub of the addSubscriber method */
|
|
12
|
+
addSubscriber: SinonStub;
|
|
13
|
+
/** Stub of the publish method */
|
|
14
|
+
publish: SinonStub;
|
|
15
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {PushNotification} from '@gwcdata/node-server-engine';
|
|
2
|
+
import {stub} from 'sinon';
|
|
3
|
+
import {PushNotificationStub} from './PushNotification.types';
|
|
4
|
+
|
|
5
|
+
/** Stub the PushNotification service */
|
|
6
|
+
export function stubPushNotification(): PushNotificationStub {
|
|
7
|
+
const init = stub(PushNotification, 'init').resolves();
|
|
8
|
+
const sendPush = stub(PushNotification, 'sendPush').resolves();
|
|
9
|
+
|
|
10
|
+
return {init, sendPush};
|
|
11
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {pki} from 'node-forge';
|
|
2
|
+
|
|
3
|
+
export const baseAttributes: Array<pki.CertificateField> = [
|
|
4
|
+
{
|
|
5
|
+
name: 'countryName',
|
|
6
|
+
value: 'TW',
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
shortName: 'ST',
|
|
10
|
+
value: 'Taipei',
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
name: 'localityName',
|
|
14
|
+
value: 'Taipei',
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: 'organizationName',
|
|
18
|
+
value: 'Self',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
shortName: 'OU',
|
|
22
|
+
value: 'Self',
|
|
23
|
+
},
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
export const caAttributes: Array<pki.CertificateField> = [
|
|
27
|
+
{
|
|
28
|
+
name: 'commonName',
|
|
29
|
+
value: 'root',
|
|
30
|
+
},
|
|
31
|
+
...baseAttributes,
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
export const serverAttributes: Array<pki.CertificateField> = [
|
|
35
|
+
{
|
|
36
|
+
name: 'commonName',
|
|
37
|
+
value: 'server',
|
|
38
|
+
},
|
|
39
|
+
...baseAttributes,
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
export const clientAttributes: Array<pki.CertificateField> = [
|
|
43
|
+
{
|
|
44
|
+
name: 'commonName',
|
|
45
|
+
value: 'client',
|
|
46
|
+
},
|
|
47
|
+
...baseAttributes,
|
|
48
|
+
];
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import {pki, md} from 'node-forge';
|
|
4
|
+
import {caAttributes, clientAttributes, serverAttributes} from './generateCerts.const';
|
|
5
|
+
import {generateCertsOptions, GeneratedCertificate} from './generateCerts.types';
|
|
6
|
+
|
|
7
|
+
let serialNumber = 1;
|
|
8
|
+
|
|
9
|
+
const dir = path.resolve('certs');
|
|
10
|
+
|
|
11
|
+
/** Generate a certificate and a private key */
|
|
12
|
+
export function generateCerts(
|
|
13
|
+
attributes: Array<pki.CertificateField>,
|
|
14
|
+
options: generateCertsOptions = {},
|
|
15
|
+
): GeneratedCertificate {
|
|
16
|
+
const keys = pki.rsa.generateKeyPair(2048);
|
|
17
|
+
const cert = pki.createCertificate();
|
|
18
|
+
cert.publicKey = keys.publicKey;
|
|
19
|
+
const serial = `00${serialNumber.toString()}`;
|
|
20
|
+
cert.serialNumber = serial.substr(serial.length - 2);
|
|
21
|
+
serialNumber++;
|
|
22
|
+
cert.validity.notBefore = new Date();
|
|
23
|
+
cert.validity.notAfter = new Date();
|
|
24
|
+
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
|
|
25
|
+
cert.setSubject(attributes);
|
|
26
|
+
cert.setIssuer(options.issuer ?? attributes);
|
|
27
|
+
cert.setExtensions(getExtensions(options));
|
|
28
|
+
cert.sign(options.key ?? keys.privateKey, md.sha256.create());
|
|
29
|
+
return {
|
|
30
|
+
cert: pki.certificateToPem(cert),
|
|
31
|
+
privateKey: keys.privateKey,
|
|
32
|
+
privateKeyPem: pki.privateKeyToPem(keys.privateKey),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Generate a CA certificate as well as a client and a server private key and certificate
|
|
38
|
+
* These will be stored in the $ROOT/certs directoryA
|
|
39
|
+
*/
|
|
40
|
+
export function generatePkiEnvironment(): void {
|
|
41
|
+
const ca = generateCerts(caAttributes, {ca: true});
|
|
42
|
+
const client = generateCerts(clientAttributes, {key: ca.privateKey, issuer: caAttributes});
|
|
43
|
+
const server = generateCerts(serverAttributes, {key: ca.privateKey, issuer: caAttributes});
|
|
44
|
+
|
|
45
|
+
// Create directory if it does not exist
|
|
46
|
+
if (!fs.existsSync(dir)) {
|
|
47
|
+
fs.mkdirSync(dir);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
fs.writeFileSync(path.resolve(dir, 'ca.crt'), ca.cert);
|
|
51
|
+
fs.writeFileSync(path.resolve(dir, 'ca.key'), ca.privateKeyPem);
|
|
52
|
+
fs.writeFileSync(path.resolve(dir, 'client.crt'), client.cert);
|
|
53
|
+
fs.writeFileSync(path.resolve(dir, 'client.key'), client.privateKeyPem);
|
|
54
|
+
fs.writeFileSync(path.resolve(dir, 'server.crt'), server.cert);
|
|
55
|
+
fs.writeFileSync(path.resolve(dir, 'server.key'), server.privateKeyPem);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Get the best fitting extensions for the certificate to generate */
|
|
59
|
+
function getExtensions(options: generateCertsOptions = {}): Array<unknown> {
|
|
60
|
+
return [
|
|
61
|
+
{
|
|
62
|
+
name: 'basicConstraints',
|
|
63
|
+
cA: options.ca === true,
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
name: 'keyUsage',
|
|
67
|
+
keyCertSign: true,
|
|
68
|
+
digitalSignature: true,
|
|
69
|
+
nonRepudiation: true,
|
|
70
|
+
keyEncipherment: true,
|
|
71
|
+
dataEncipherment: true,
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
name: 'extKeyUsage',
|
|
75
|
+
serverAuth: true,
|
|
76
|
+
clientAuth: true,
|
|
77
|
+
codeSigning: true,
|
|
78
|
+
emailProtection: true,
|
|
79
|
+
timeStamping: true,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: 'nsCertType',
|
|
83
|
+
client: true,
|
|
84
|
+
server: true,
|
|
85
|
+
email: true,
|
|
86
|
+
objsign: true,
|
|
87
|
+
sslCA: true,
|
|
88
|
+
emailCA: true,
|
|
89
|
+
objCA: true,
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: 'subjectAltName',
|
|
93
|
+
altNames: [
|
|
94
|
+
{
|
|
95
|
+
type: 2,
|
|
96
|
+
value: 'localhost',
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
type: 7,
|
|
100
|
+
ip: '127.0.0.1',
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
name: 'subjectKeyIdentifier',
|
|
106
|
+
},
|
|
107
|
+
];
|
|
108
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {pki} from 'node-forge';
|
|
2
|
+
|
|
3
|
+
/** Options ot generate fake certificates */
|
|
4
|
+
export interface generateCertsOptions {
|
|
5
|
+
/** Indicates that we should generate a CA certificate */
|
|
6
|
+
ca?: boolean;
|
|
7
|
+
/** Certificate issue information */
|
|
8
|
+
issuer?: Array<pki.CertificateField>;
|
|
9
|
+
/** Private key to use to sign the certificate */
|
|
10
|
+
key?: pki.PrivateKey;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Fake certificate generated */
|
|
14
|
+
export interface GeneratedCertificate {
|
|
15
|
+
/** PEM certificate */
|
|
16
|
+
cert: string;
|
|
17
|
+
/** Private key to use with the certificate */
|
|
18
|
+
privateKey: pki.PrivateKey;
|
|
19
|
+
/** PEM encoding of the private key */
|
|
20
|
+
privateKeyPem: string;
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './generateCerts';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { faker } from '@faker-js/faker';
|
|
2
|
+
import {generateAccessToken} from 'generators';
|
|
3
|
+
|
|
4
|
+
/** Generate Admin api Bearer Token authorization header */
|
|
5
|
+
export function getAdminApiBearer(email: string, payload = {}): string {
|
|
6
|
+
return `Bearer ${generateAccessToken(
|
|
7
|
+
email,
|
|
8
|
+
{
|
|
9
|
+
audience: process.env.ADMIN_API_ACCESS_TOKEN_AUDIENCE,
|
|
10
|
+
issuer: process.env.ADMIN_API_PUBLIC_URL,
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
per: [faker.lorem.word(), faker.lorem.word()],
|
|
14
|
+
...payload,
|
|
15
|
+
},
|
|
16
|
+
)}`;
|
|
17
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {SignOptions} from 'jsonwebtoken';
|
|
2
|
+
import {generateAccessToken} from 'generators';
|
|
3
|
+
|
|
4
|
+
/** Generate Bearer Token authorization header */
|
|
5
|
+
export function getBearer(
|
|
6
|
+
userId: string,
|
|
7
|
+
options: SignOptions = {},
|
|
8
|
+
payload: string | object | Buffer = {},
|
|
9
|
+
): string {
|
|
10
|
+
return `Bearer ${generateAccessToken(userId, options, payload)}`;
|
|
11
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
6
|
+
// @ts-nocheck
|
|
7
|
+
import supertest from 'supertest';
|
|
8
|
+
import {getBearer} from './getBearer';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Get a pre-configured generator for an express instance that uses JWT authentication
|
|
12
|
+
* @param {Object} app Express app
|
|
13
|
+
* @param {String} method Method in which the requests will be made
|
|
14
|
+
* @param {String} endpoint PAth of the endpoint where the requests are to be made
|
|
15
|
+
* @return {Function} - Supertest instance with JWT Authorization
|
|
16
|
+
*/
|
|
17
|
+
export function jwtRequest(app, method, endpoint) {
|
|
18
|
+
/**
|
|
19
|
+
* @param {Object} user User making the request
|
|
20
|
+
* @return {Object} Pre-configured supertest instance
|
|
21
|
+
*/
|
|
22
|
+
return async function request(userId: string): supertest.Request {
|
|
23
|
+
return supertest(app)[method](endpoint).set('Authorization', getBearer(userId));
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2
|
+
// @ts-nocheck
|
|
3
|
+
import { Application } from 'express';
|
|
4
|
+
import supertest from 'supertest';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get a pre-configured generator for an express instance that uses STATIC client key authentication
|
|
8
|
+
* @param app Express app
|
|
9
|
+
* @param method Method in which the requests will be made
|
|
10
|
+
* @param endpoint PAth of the endpoint where the requests are to be made
|
|
11
|
+
* @return - Supertest instance with STATIC Authorization
|
|
12
|
+
*/
|
|
13
|
+
export function staticRequest(
|
|
14
|
+
app: Application,
|
|
15
|
+
method: string,
|
|
16
|
+
endpoint: string
|
|
17
|
+
) {
|
|
18
|
+
return async function request(): supertest.Request {
|
|
19
|
+
return supertest(app) [method](endpoint)
|
|
20
|
+
.set('Authorization', `Bearer ${process.env.STATIC_TOKEN as string}`);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import {agent, Request} from 'supertest';
|
|
3
|
+
import {tlsRequestOptions} from './tlsRequest.types';
|
|
4
|
+
|
|
5
|
+
const customAgent = agent(`https://localhost:${process.env.PORT as string}`);
|
|
6
|
+
|
|
7
|
+
let settings: tlsRequestOptions;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Wrapper for supertest that adds TLS encryption
|
|
11
|
+
*/
|
|
12
|
+
export async function tlsRequest(options: tlsRequestOptions = {}): Promise<Request> {
|
|
13
|
+
if (!settings) loadDefaultSettings();
|
|
14
|
+
const ca = options.ca ? fs.readFileSync(options.ca, 'utf-8') : settings.ca;
|
|
15
|
+
const key = options.key ? fs.readFileSync(options.key, 'utf-8') : settings.key;
|
|
16
|
+
const cert = options.cert ? fs.readFileSync(options.cert, 'utf-8') : settings.cert;
|
|
17
|
+
if (!ca || !key || !cert) throw new Error('Missing some TLS configuration');
|
|
18
|
+
return customAgent.ca(ca).key(key).cert(cert);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Load TLS settings from env variables */
|
|
22
|
+
function loadDefaultSettings(): void {
|
|
23
|
+
const {
|
|
24
|
+
TLS_REQUEST_KEY,
|
|
25
|
+
TLS_REQUEST_CERT,
|
|
26
|
+
TLS_REQUEST_CA,
|
|
27
|
+
TLS_SERVER_KEY,
|
|
28
|
+
TLS_SERVER_CERT,
|
|
29
|
+
TLS_CA,
|
|
30
|
+
} = process.env;
|
|
31
|
+
|
|
32
|
+
if (!(TLS_REQUEST_KEY || TLS_SERVER_KEY)) {
|
|
33
|
+
throw new Error('TLS Request: no TLS setting specified');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
settings = {
|
|
37
|
+
key: fs.readFileSync((TLS_REQUEST_KEY ?? TLS_SERVER_KEY) as string, 'utf-8'),
|
|
38
|
+
cert: fs.readFileSync((TLS_REQUEST_CERT ?? TLS_SERVER_CERT) as string, 'utf-8'),
|
|
39
|
+
ca: fs.readFileSync((TLS_REQUEST_CA ?? TLS_CA) as string, 'utf-8'),
|
|
40
|
+
};
|
|
41
|
+
}
|
package/test/env.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"baseUrl": "src",
|
|
4
|
+
"paths": {
|
|
5
|
+
"test": ["../test"],
|
|
6
|
+
"test/*": ["../test/*"]
|
|
7
|
+
},
|
|
8
|
+
"target": "ES6",
|
|
9
|
+
"lib": ["es2023"],
|
|
10
|
+
"module": "commonjs",
|
|
11
|
+
"moduleResolution": "node",
|
|
12
|
+
"outDir": "./dist",
|
|
13
|
+
"noImplicitAny": true,
|
|
14
|
+
"noImplicitThis": true,
|
|
15
|
+
"strictNullChecks": true,
|
|
16
|
+
"allowJs": true,
|
|
17
|
+
"skipLibCheck": true,
|
|
18
|
+
"esModuleInterop": true,
|
|
19
|
+
"allowSyntheticDefaultImports": true,
|
|
20
|
+
"strict": true,
|
|
21
|
+
"forceConsistentCasingInFileNames": true,
|
|
22
|
+
"noFallthroughCasesInSwitch": true,
|
|
23
|
+
"sourceMap": true,
|
|
24
|
+
"resolveJsonModule": true,
|
|
25
|
+
"isolatedModules": true,
|
|
26
|
+
"declaration": true,
|
|
27
|
+
"emitDeclarationOnly": true
|
|
28
|
+
},
|
|
29
|
+
"include": ["src", "test"]
|
|
30
|
+
}
|