@apolitical/testing 0.0.4 → 1.0.0-beta.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/CHANGELOG.md +6 -0
- package/README.md +18 -1
- package/package.json +21 -21
- package/{index.js → src/index.js} +1 -1
- package/.gitlab-ci.yml +0 -22
- package/.husky/pre-commit +0 -6
- package/test/__snapshots__/request.spec.js.snap +0 -42
- package/test/container.js +0 -31
- package/test/request.spec.js +0 -77
- package/test/stubs/jsrsasign.stub.js +0 -7
- package/test/stubs/supertest.stub.js +0 -8
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.0.0] - 2022-01-19
|
|
9
|
+
### Changed
|
|
10
|
+
- Updated Dependencies
|
|
11
|
+
- Upgraded Node.js version
|
|
12
|
+
- Revamped GitLab pipelines
|
|
13
|
+
|
|
8
14
|
## [0.0.4] - 2021-09-03
|
|
9
15
|
### Changed
|
|
10
16
|
- Pass in cookies to a new request helper
|
package/README.md
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
# Apolitical Testing
|
|
2
2
|
|
|
3
3
|
Node.js module to encapsulate Jest testing helpers (backend)
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
Requires the following to run:
|
|
8
|
+
|
|
9
|
+
- [node.js][node] 16.13.2+
|
|
10
|
+
- [yarn][yarn]
|
|
11
|
+
|
|
12
|
+
[node]: https://nodejs.org/en/download/
|
|
13
|
+
[yarn]: https://classic.yarnpkg.com/en/docs/install
|
|
14
|
+
|
|
4
15
|
## Installation
|
|
5
16
|
|
|
6
17
|
Install with `yarn`:
|
|
@@ -29,4 +40,10 @@ Then, you can use your express app to generate supertests agents:
|
|
|
29
40
|
const agents = instance.generateAgents(app);
|
|
30
41
|
```
|
|
31
42
|
|
|
32
|
-
The agents are
|
|
43
|
+
The agents are:
|
|
44
|
+
- `loggedIn`
|
|
45
|
+
- `loggedOut`
|
|
46
|
+
- `loggedInAdmin`
|
|
47
|
+
- `loggedInMyself`
|
|
48
|
+
|
|
49
|
+
The `loggedInMyself` agent can also be updated with custome token payload with the use of the `assignTokenToMyself` function.
|
package/package.json
CHANGED
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apolitical/testing",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "1.0.0-beta.0",
|
|
4
4
|
"description": "Node.js module to encapsulate Jest testing helpers (backend)",
|
|
5
5
|
"author": "Apolitical Group Limited <engineering@apolitical.co>",
|
|
6
|
-
"contributors": [
|
|
7
|
-
"Anouska Hopkins <anouska.hopkins@apolitical.co>",
|
|
8
|
-
"Antonio Cordasco <antonio.cordasco@apolitical.co>",
|
|
9
|
-
"Dorothy Yau <dorothy.yau@apolitical.co>",
|
|
10
|
-
"Laura Hanna-White <laura.hanna-white@apolitical.co>",
|
|
11
|
-
"Fatimat Gbajabiamila <fatimat.gbajabiamila@apolitical.co>",
|
|
12
|
-
"Renzo Rozza <renzo.rozza@apolitical.co>",
|
|
13
|
-
"Rihards Jukna <rihards.jukna@apolitical.co>"
|
|
14
|
-
],
|
|
15
6
|
"license": "MIT",
|
|
16
|
-
"main": "index.js",
|
|
7
|
+
"main": "src/index.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"src"
|
|
10
|
+
],
|
|
17
11
|
"scripts": {
|
|
18
12
|
"test": "jest --bail --runInBand",
|
|
19
13
|
"integration-test": "jest --bail --runInBand",
|
|
@@ -30,19 +24,20 @@
|
|
|
30
24
|
"Node Modules"
|
|
31
25
|
],
|
|
32
26
|
"dependencies": {
|
|
33
|
-
"awilix": "
|
|
34
|
-
"jsrsasign": "10.
|
|
35
|
-
"supertest": "6.
|
|
27
|
+
"awilix": "6.0.0",
|
|
28
|
+
"jsrsasign": "10.5.1",
|
|
29
|
+
"supertest": "6.2.2"
|
|
36
30
|
},
|
|
37
31
|
"devDependencies": {
|
|
38
|
-
"@apolitical/eslint-config": "0.0.
|
|
39
|
-
"audit-ci": "
|
|
40
|
-
"husky": "7.0.
|
|
41
|
-
"jest": "27.
|
|
42
|
-
"
|
|
32
|
+
"@apolitical/eslint-config": "1.0.0-beta.0",
|
|
33
|
+
"audit-ci": "5.1.2",
|
|
34
|
+
"husky": "7.0.4",
|
|
35
|
+
"jest": "27.4.7",
|
|
36
|
+
"jest-junit": "13.0.0",
|
|
37
|
+
"lint-staged": "12.2.0"
|
|
43
38
|
},
|
|
44
39
|
"engines": {
|
|
45
|
-
"node": ">=
|
|
40
|
+
"node": ">=16.13.2"
|
|
46
41
|
},
|
|
47
42
|
"eslintConfig": {
|
|
48
43
|
"extends": "@apolitical/eslint-config/base.config"
|
|
@@ -67,7 +62,12 @@
|
|
|
67
62
|
"testEnvironment": "node",
|
|
68
63
|
"testTimeout": 60000,
|
|
69
64
|
"maxConcurrency": 1,
|
|
70
|
-
"maxWorkers": 1
|
|
65
|
+
"maxWorkers": 1,
|
|
66
|
+
"reporters": [
|
|
67
|
+
"default",
|
|
68
|
+
"jest-junit"
|
|
69
|
+
],
|
|
70
|
+
"testResultsProcessor": "jest-junit"
|
|
71
71
|
},
|
|
72
72
|
"lint-staged": {
|
|
73
73
|
"*.js": "eslint --cache --fix --ignore-path .gitignore",
|
package/.gitlab-ci.yml
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# External jobs
|
|
2
|
-
include:
|
|
3
|
-
- project: 'apolitical/templates/gitlab-pipelines'
|
|
4
|
-
ref: master
|
|
5
|
-
file: 'Node-Testing.gitlab-ci.yml'
|
|
6
|
-
|
|
7
|
-
stages:
|
|
8
|
-
- cache
|
|
9
|
-
- test
|
|
10
|
-
- publish
|
|
11
|
-
|
|
12
|
-
publish-module:
|
|
13
|
-
stage: publish
|
|
14
|
-
image: node:12.20.1-alpine3.11
|
|
15
|
-
extends:
|
|
16
|
-
- .use-cache
|
|
17
|
-
only:
|
|
18
|
-
- tags
|
|
19
|
-
- /^v\d+\.\d+\.\d+$/
|
|
20
|
-
script:
|
|
21
|
-
- echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > ~/.npmrc
|
|
22
|
-
- yarn publish --access=public
|
package/.husky/pre-commit
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
-
|
|
3
|
-
exports[`Request Helper Generate Agents should encode logged in and logged in admin tokens 1`] = `
|
|
4
|
-
Array [
|
|
5
|
-
Array [
|
|
6
|
-
"HS256",
|
|
7
|
-
Object {
|
|
8
|
-
"alg": "HS256",
|
|
9
|
-
"typ": "JWT",
|
|
10
|
-
},
|
|
11
|
-
Object {
|
|
12
|
-
"admin": true,
|
|
13
|
-
"email": "apolitical.testing@email.com",
|
|
14
|
-
"exp": 2592000,
|
|
15
|
-
"iss": "apolitical-testing",
|
|
16
|
-
"name": "Apolitical Testing",
|
|
17
|
-
"role": "public-servant",
|
|
18
|
-
"slug": "apolitical-testing",
|
|
19
|
-
"sub": "login",
|
|
20
|
-
},
|
|
21
|
-
"some-session-secret",
|
|
22
|
-
],
|
|
23
|
-
Array [
|
|
24
|
-
"HS256",
|
|
25
|
-
Object {
|
|
26
|
-
"alg": "HS256",
|
|
27
|
-
"typ": "JWT",
|
|
28
|
-
},
|
|
29
|
-
Object {
|
|
30
|
-
"admin": true,
|
|
31
|
-
"email": "apolitical.testing@email.com",
|
|
32
|
-
"exp": 2592000,
|
|
33
|
-
"iss": "apolitical-testing",
|
|
34
|
-
"name": "Apolitical Testing",
|
|
35
|
-
"role": "administrator",
|
|
36
|
-
"slug": "apolitical-testing",
|
|
37
|
-
"sub": "login",
|
|
38
|
-
},
|
|
39
|
-
"some-session-secret",
|
|
40
|
-
],
|
|
41
|
-
]
|
|
42
|
-
`;
|
package/test/container.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// External Modules
|
|
4
|
-
const { createContainer, asValue, asFunction } = require('awilix');
|
|
5
|
-
// Configuration
|
|
6
|
-
const config = require('../src/config');
|
|
7
|
-
// Helpers
|
|
8
|
-
const jwtHelper = require('../src/helpers/jwt.helper');
|
|
9
|
-
const requestHelper = require('../src/helpers/request.helper');
|
|
10
|
-
// Stubs
|
|
11
|
-
const { stubs: jsrsasignStubs, jsrsasign } = require('./stubs/jsrsasign.stub');
|
|
12
|
-
const { stubs: supertestStubs, supertest } = require('./stubs/supertest.stub');
|
|
13
|
-
|
|
14
|
-
const container = createContainer();
|
|
15
|
-
container.register({
|
|
16
|
-
// External Modules
|
|
17
|
-
jsrsasign: asValue(jsrsasign),
|
|
18
|
-
supertest: asValue(supertest),
|
|
19
|
-
// Configuration
|
|
20
|
-
config: asValue(config),
|
|
21
|
-
// Helpers
|
|
22
|
-
jwtHelper: asFunction(jwtHelper).singleton(),
|
|
23
|
-
requestHelper: asFunction(requestHelper).singleton(),
|
|
24
|
-
// Stubs
|
|
25
|
-
stubs: asValue({
|
|
26
|
-
...jsrsasignStubs,
|
|
27
|
-
...supertestStubs,
|
|
28
|
-
}),
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
module.exports = container;
|
package/test/request.spec.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { resolve } = require('./container');
|
|
4
|
-
// Helpers
|
|
5
|
-
const requestHelper = resolve('requestHelper');
|
|
6
|
-
// Stubs
|
|
7
|
-
const { agent, set, sign } = resolve('stubs');
|
|
8
|
-
|
|
9
|
-
describe('Request Helper', () => {
|
|
10
|
-
const sessionSecret = 'some-session-secret';
|
|
11
|
-
|
|
12
|
-
describe('General', () => {
|
|
13
|
-
test('should be exposed as function', () => {
|
|
14
|
-
expect(typeof requestHelper).toBe('function');
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test('should throw an error when no session secret is defined', () => {
|
|
18
|
-
expect(() => {
|
|
19
|
-
requestHelper();
|
|
20
|
-
}).toThrow('Cannot create request helper without session secret');
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test('should return genearte agents function', () => {
|
|
24
|
-
const instance = requestHelper({ sessionSecret });
|
|
25
|
-
expect(typeof instance.generateAgents).toBe('function');
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
describe('Generate Agents', () => {
|
|
30
|
-
const app = {};
|
|
31
|
-
let instance = null;
|
|
32
|
-
|
|
33
|
-
beforeEach(() => {
|
|
34
|
-
// Mock agent
|
|
35
|
-
agent.mockReturnValue({ set });
|
|
36
|
-
|
|
37
|
-
// Fixed expiration
|
|
38
|
-
Date.now = jest.fn();
|
|
39
|
-
Date.now.mockReturnValue(0);
|
|
40
|
-
|
|
41
|
-
// Create request instance
|
|
42
|
-
instance = requestHelper({ sessionSecret });
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test('should encode logged in and logged in admin tokens', () => {
|
|
46
|
-
instance.generateAgents(app);
|
|
47
|
-
expect(sign).toHaveBeenCalledTimes(2);
|
|
48
|
-
expect(set).toHaveBeenCalledTimes(2);
|
|
49
|
-
expect(sign.mock.calls).toMatchSnapshot();
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test('should create agents', () => {
|
|
53
|
-
instance.generateAgents(app);
|
|
54
|
-
expect(agent).toHaveBeenCalledTimes(4);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test('should expose agents', () => {
|
|
58
|
-
const { loggedIn, loggedOut, loggedInAdmin, loggedInMyself } = instance.generateAgents(app);
|
|
59
|
-
expect(typeof loggedIn).not.toBeUndefined();
|
|
60
|
-
expect(typeof loggedOut).not.toBeUndefined();
|
|
61
|
-
expect(typeof loggedInAdmin).not.toBeUndefined();
|
|
62
|
-
expect(typeof loggedInMyself).not.toBeUndefined();
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
test('should expose assign token to myself functions', () => {
|
|
66
|
-
const { assignTokenToMyself, assignTokenToMyselfWithCookies } = instance.generateAgents(app);
|
|
67
|
-
expect(typeof assignTokenToMyself).toBe('function');
|
|
68
|
-
expect(typeof assignTokenToMyselfWithCookies).toBe('function');
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test('should throw an error when no express app is defined', () => {
|
|
72
|
-
expect(() => {
|
|
73
|
-
instance.generateAgents();
|
|
74
|
-
}).toThrow('Cannot generate agents without express app');
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
});
|