From fa9d0e5790a6d7ba9d4490bc016e0f9afde49ec8 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 3 Mar 2021 16:15:53 +0100 Subject: [PATCH 1/7] Added basic jest packages and scripts ref #4 --- package.json | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a872e2b..3c3c920 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,10 @@ "licenses:export": "license-exporter --markdown", "release": "release-it --only-version", "translations:sort": "node ./scripts/sort_translations.js", - "test:generate_env": "ts-node ./scripts/create_testenv.ts" + "test": "jest", + "test:generate_env": "ts-node ./scripts/create_testenv.ts", + "test:ci": "npm run test:generate_env && npm run test:ci:run", + "test:ci:run": "start-server-and-test dev http://localhost:4010/api/docs/openapi.json test" }, "repository": { "type": "git", @@ -58,13 +61,16 @@ "devDependencies": { "@odit/license-exporter": "^0.0.10", "@types/express": "^4.17.11", + "@types/jest": "^26.0.20", "@types/node": "^14.14.22", "@types/nodemailer": "^6.4.0", "cp-cli": "^2.0.0", + "jest": "^26.6.3", "nodemon": "^2.0.7", "release-it": "^14.2.2", "rimraf": "^3.0.2", "start-server-and-test": "^1.12.0", + "ts-jest": "^26.5.2", "ts-node": "^9.1.1", "typescript": "^4.1.3" }, @@ -81,4 +87,4 @@ "publish": false } } -} \ No newline at end of file +} From ec939e6c1142e825cdb21555e03d9e026e7cdf3c Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 3 Mar 2021 16:17:05 +0100 Subject: [PATCH 2/7] Added jest config and first tests --- jest.config.js | 4 ++++ src/tests/api_docs.spec.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 jest.config.js create mode 100644 src/tests/api_docs.spec.ts diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..91a2d2c --- /dev/null +++ b/jest.config.js @@ -0,0 +1,4 @@ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', +}; \ No newline at end of file diff --git a/src/tests/api_docs.spec.ts b/src/tests/api_docs.spec.ts new file mode 100644 index 0000000..994a4e8 --- /dev/null +++ b/src/tests/api_docs.spec.ts @@ -0,0 +1,34 @@ +import axios from 'axios'; +import { config } from '../config'; +const base = "http://localhost:" + config.internal_port + +describe('GET /api/docs/openapi.json', () => { + it('OpenAPI Spec is availdable 200', async () => { + const res = await axios.get(base + '/api/docs/openapi.json'); + expect(res.status).toEqual(200); + }); +}); +describe('GET /api/docs/swagger.json', () => { + it('OpenAPI Spec is availdable 200', async () => { + const res = await axios.get(base + '/api/docs/swagger.json'); + expect(res.status).toEqual(200); + }); +}); +describe('GET /api/docs/swaggerui', () => { + it('swaggerui is availdable 200', async () => { + const res = await axios.get(base + '/api/docs/swaggerui'); + expect(res.status).toEqual(200); + }); +}); +describe('GET /api/docs/redoc', () => { + it('redoc is availdable 200', async () => { + const res = await axios.get(base + '/api/docs/redoc'); + expect(res.status).toEqual(200); + }); +}); +describe('GET /api/docs/rapidoc', () => { + it('rapidoc is availdable 200', async () => { + const res = await axios.get(base + '/api/docs/rapidoc'); + expect(res.status).toEqual(200); + }); +}); From f6a53cc3e47308e84cde13640cb3f614642fd4ca Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 3 Mar 2021 16:21:11 +0100 Subject: [PATCH 3/7] Updated api doc tests to listen on the right endpoint ref #4 --- package.json | 3 ++- src/tests/api_docs.spec.ts | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 3c3c920..89e659a 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "test": "jest", "test:generate_env": "ts-node ./scripts/create_testenv.ts", "test:ci": "npm run test:generate_env && npm run test:ci:run", - "test:ci:run": "start-server-and-test dev http://localhost:4010/api/docs/openapi.json test" + "test:ci:run": "start-server-and-test dev http://localhost:4010/docs/openapi.json test" }, "repository": { "type": "git", @@ -64,6 +64,7 @@ "@types/jest": "^26.0.20", "@types/node": "^14.14.22", "@types/nodemailer": "^6.4.0", + "axios": "^0.21.1", "cp-cli": "^2.0.0", "jest": "^26.6.3", "nodemon": "^2.0.7", diff --git a/src/tests/api_docs.spec.ts b/src/tests/api_docs.spec.ts index 994a4e8..9e9444a 100644 --- a/src/tests/api_docs.spec.ts +++ b/src/tests/api_docs.spec.ts @@ -2,33 +2,33 @@ import axios from 'axios'; import { config } from '../config'; const base = "http://localhost:" + config.internal_port -describe('GET /api/docs/openapi.json', () => { +describe('GET /docs/openapi.json', () => { it('OpenAPI Spec is availdable 200', async () => { - const res = await axios.get(base + '/api/docs/openapi.json'); + const res = await axios.get(base + '/docs/openapi.json'); expect(res.status).toEqual(200); }); }); -describe('GET /api/docs/swagger.json', () => { +describe('GET /docs/swagger.json', () => { it('OpenAPI Spec is availdable 200', async () => { - const res = await axios.get(base + '/api/docs/swagger.json'); + const res = await axios.get(base + '/docs/swagger.json'); expect(res.status).toEqual(200); }); }); -describe('GET /api/docs/swaggerui', () => { +describe('GET /docs/swaggerui', () => { it('swaggerui is availdable 200', async () => { - const res = await axios.get(base + '/api/docs/swaggerui'); + const res = await axios.get(base + '/docs/swaggerui'); expect(res.status).toEqual(200); }); }); -describe('GET /api/docs/redoc', () => { +describe('GET /docs/redoc', () => { it('redoc is availdable 200', async () => { - const res = await axios.get(base + '/api/docs/redoc'); + const res = await axios.get(base + '/docs/redoc'); expect(res.status).toEqual(200); }); }); -describe('GET /api/docs/rapidoc', () => { +describe('GET /docs/rapidoc', () => { it('rapidoc is availdable 200', async () => { - const res = await axios.get(base + '/api/docs/rapidoc'); + const res = await axios.get(base + '/docs/rapidoc'); expect(res.status).toEqual(200); }); }); From 38cffc20490fe10b4e4bc4d88a8377562cd1545a Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 3 Mar 2021 16:27:56 +0100 Subject: [PATCH 4/7] Added test watch mode ref #4 --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 89e659a..5c44558 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "release": "release-it --only-version", "translations:sort": "node ./scripts/sort_translations.js", "test": "jest", + "test:watch": "jest --watchAll", "test:generate_env": "ts-node ./scripts/create_testenv.ts", "test:ci": "npm run test:generate_env && npm run test:ci:run", "test:ci:run": "start-server-and-test dev http://localhost:4010/docs/openapi.json test" @@ -88,4 +89,4 @@ "publish": false } } -} +} \ No newline at end of file From bf9652dcfed3667f9d2e8cfd933963935a621a88 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 3 Mar 2021 16:31:07 +0100 Subject: [PATCH 5/7] Added test mail tests ref #4 --- src/tests/test_mail.spec.ts | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/tests/test_mail.spec.ts diff --git a/src/tests/test_mail.spec.ts b/src/tests/test_mail.spec.ts new file mode 100644 index 0000000..ff3dac6 --- /dev/null +++ b/src/tests/test_mail.spec.ts @@ -0,0 +1,44 @@ +import axios from 'axios'; +import { config } from '../config'; +const base = "http://localhost:" + config.internal_port + +const axios_config = { + validateStatus: undefined +}; + +describe('POST /test without auth', () => { + it('Post without auth should return 401', async () => { + const res = await axios.post(base + '/test', null, axios_config); + expect(res.status).toEqual(401); + }); +}); + +describe('POST /test with auth', () => { + it('Post with auth and no locale should return 200', async () => { + const res = await axios.post(base + '/test?key=' + config.api_key, null, axios_config); + expect(res.status).toEqual(200); + expect(res.data).toEqual({ + success: true, + message: "Sent!", + locale: "en" + }) + }); + it('Post with auth and locale=en should return 200', async () => { + const res = await axios.post(base + '/test?locale=en&key=' + config.api_key, null, axios_config); + expect(res.status).toEqual(200); + expect(res.data).toEqual({ + success: true, + message: "Sent!", + locale: "en" + }) + }); + it('Post with auth and locale=de should return 200', async () => { + const res = await axios.post(base + '/test?locale=de&key=' + config.api_key, null, axios_config); + expect(res.status).toEqual(200); + expect(res.data).toEqual({ + success: true, + message: "Sent!", + locale: "de" + }) + }); +}); \ No newline at end of file From 46242509637e05e6b258b406d0a2a76e5db9ca03 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 3 Mar 2021 16:37:52 +0100 Subject: [PATCH 6/7] Added reset mail valid tests ref #4 --- src/tests/pw_reset_mail.spec.ts | 72 +++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/tests/pw_reset_mail.spec.ts diff --git a/src/tests/pw_reset_mail.spec.ts b/src/tests/pw_reset_mail.spec.ts new file mode 100644 index 0000000..d43f3a3 --- /dev/null +++ b/src/tests/pw_reset_mail.spec.ts @@ -0,0 +1,72 @@ +import axios from 'axios'; +import { config } from '../config'; +const base = "http://localhost:" + config.internal_port + +const axios_config = { + validateStatus: undefined +}; + +describe('POST /reset without auth', () => { + it('Post without auth should return 401', async () => { + const res = await axios.post(base + '/reset', null, axios_config); + expect(res.status).toEqual(401); + }); +}); + +describe('POST /reset with auth but wrong body', () => { + it('Post with auth but no body should return 400', async () => { + const res = await axios.post(base + '/reset?key=' + config.api_key, null, axios_config); + expect(res.status).toEqual(400); + }); + it('Post with auth but no mail should return 400', async () => { + const res = await axios.post(base + '/reset?key=' + config.api_key, { resetKey: "test" }, axios_config); + expect(res.status).toEqual(400); + }); + it('Post with auth but no reset key should return 400', async () => { + const res = await axios.post(base + '/reset?key=' + config.api_key, { address: "test@dev.lauf-fuer-kaya.de" }, axios_config); + expect(res.status).toEqual(400); + }); + it('Post with auth but invalid mail should return 400', async () => { + const res = await axios.post(base + '/reset?key=' + config.api_key, { resetKey: "test", address: "testdev.l.de" }, axios_config); + expect(res.status).toEqual(400); + }); +}); + +describe('POST /reset with auth and vaild body', () => { + it('Post with auth, body and no locale should return 200', async () => { + const res = await axios.post(base + '/reset?key=' + config.api_key, { + resetKey: "test", + address: "test@dev.lauf-fuer-kaya.de" + }, axios_config); + expect(res.status).toEqual(200); + expect(res.data).toEqual({ + success: true, + message: "Sent!", + locale: "en" + }) + }); + it('Post with auth, body and locale=en should return 200', async () => { + const res = await axios.post(base + '/reset?locale=en&key=' + config.api_key, { + resetKey: "test", + address: "test@dev.lauf-fuer-kaya.de" + }, axios_config); + expect(res.status).toEqual(200); + expect(res.data).toEqual({ + success: true, + message: "Sent!", + locale: "en" + }) + }); + it('Post with auth, body and locale=de should return 200', async () => { + const res = await axios.post(base + '/reset?locale=de&key=' + config.api_key, { + resetKey: "test", + address: "test@dev.lauf-fuer-kaya.de" + }, axios_config); + expect(res.status).toEqual(200); + expect(res.data).toEqual({ + success: true, + message: "Sent!", + locale: "de" + }) + }); +}); \ No newline at end of file From 3501a4bef1b6ec73ea0be643e1b4e95ee9874ff9 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 3 Mar 2021 16:39:54 +0100 Subject: [PATCH 7/7] Added drone pipeline for automagic tests ref #4 --- .drone.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/.drone.yml b/.drone.yml index 9a2a300..fb4006f 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,3 +1,23 @@ +--- +kind: pipeline +name: tests:node_latest +clone: + disable: true +steps: + - name: checkout pr + image: alpine/git + commands: + - git clone $DRONE_REMOTE_URL . + - git checkout $DRONE_SOURCE_BRANCH + - name: run tests + image: node:latest + commands: + - yarn + - yarn test:ci +trigger: + event: + - pull_request + --- kind: pipeline type: docker