@apolitical/testing 0.0.3 → 1.0.1

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 CHANGED
@@ -5,6 +5,20 @@ 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.1] - 2022-01-20
9
+ ### Fixed
10
+ - Pipeline node version
11
+
12
+ ## [1.0.0] - 2022-01-19
13
+ ### Changed
14
+ - Updated Dependencies
15
+ - Upgraded Node.js version
16
+ - Revamped GitLab pipelines
17
+
18
+ ## [0.0.4] - 2021-09-03
19
+ ### Changed
20
+ - Pass in cookies to a new request helper
21
+
8
22
  ## [0.0.3] - 2021-09-03
9
23
  ### Fixed
10
24
  - Stub 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 `loggedIn`, `loggedOut`, `loggedInAdmin` and `loggedInMyself`. The `loggedInMyself` agent can also be updated with custome token payload with the use of the `assignTokenToMyself` function.
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",
3
+ "version": "1.0.1",
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": "5.0.1",
34
- "jsrsasign": "10.4.0",
35
- "supertest": "6.1.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.1",
39
- "audit-ci": "4.1.0",
40
- "husky": "7.0.2",
41
- "jest": "27.1.0",
42
- "lint-staged": "11.1.2"
32
+ "@apolitical/eslint-config": "1.0.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.1"
43
38
  },
44
39
  "engines": {
45
- "node": ">=12.20.1"
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",
@@ -15,11 +15,12 @@ module.exports =
15
15
  } = config;
16
16
 
17
17
  // Assign token to agent
18
- function assignToken(agent, customPayload = {}, isAdmin) {
18
+ function assignToken(agent, customPayload = {}, isAdmin, customCookies = '') {
19
19
  const role = isAdmin ? ADMIN : PUBLIC_SERVANT;
20
20
  const payload = Object.assign({}, { role }, customPayload);
21
21
  const token = jwtHelper.encode(sessionSecret, payload);
22
- agent.set('Cookie', `${COOKIE_KEY}=${token}`);
22
+ const cookies = `${COOKIE_KEY}=${token}${customCookies}`;
23
+ agent.set('Cookie', cookies);
23
24
  return agent;
24
25
  }
25
26
 
@@ -34,6 +35,8 @@ module.exports =
34
35
  loggedInAdmin: assignToken(supertest.agent(app), {}, true),
35
36
  loggedInMyself,
36
37
  assignTokenToMyself: (payload, isAdmin) => assignToken(loggedInMyself, payload, isAdmin),
38
+ assignTokenToMyselfWithCookies: (payload, isAdmin, cookies) =>
39
+ assignToken(loggedInMyself, payload, isAdmin, cookies),
37
40
  };
38
41
  }
39
42
 
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const { resolve } = require('./src/container');
3
+ const { resolve } = require('./container');
4
4
 
5
5
  const requestHelper = resolve('requestHelper');
6
6
  const stubHelper = resolve('stubHelper');
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,6 +0,0 @@
1
- #!/bin/sh
2
- . "$(dirname "$0")/_/husky.sh"
3
-
4
- yarn lint-format
5
-
6
- yarn test
@@ -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;
@@ -1,76 +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 function', () => {
66
- const { assignTokenToMyself } = instance.generateAgents(app);
67
- expect(typeof assignTokenToMyself).toBe('function');
68
- });
69
-
70
- test('should throw an error when no express app is defined', () => {
71
- expect(() => {
72
- instance.generateAgents();
73
- }).toThrow('Cannot generate agents without express app');
74
- });
75
- });
76
- });
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- const sign = jest.fn();
4
-
5
- const stubs = { sign };
6
-
7
- module.exports = { stubs, jsrsasign: { jws: { JWS: { sign } } } };
@@ -1,8 +0,0 @@
1
- 'use strict';
2
-
3
- const agent = jest.fn();
4
- const set = jest.fn();
5
-
6
- const stubs = { agent, set };
7
-
8
- module.exports = { stubs, supertest: { agent } };