express-generator-typescript 1.10.2 → 2.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.
Files changed (120) hide show
  1. package/README.md +37 -27
  2. package/bin/cli.js +2 -15
  3. package/lib/cli.js +1 -15
  4. package/lib/express-generator-typescript.js +11 -21
  5. package/lib/project-files/.eslintrc.json +16 -2
  6. package/lib/project-files/build.ts +2 -2
  7. package/lib/project-files/package.json +15 -5
  8. package/lib/project-files/spec/nodemon.json +1 -1
  9. package/lib/{auth-proj → project-files}/spec/support/login-agent.ts +6 -8
  10. package/lib/{auth-proj → project-files}/spec/tests/auth.spec.ts +18 -32
  11. package/lib/project-files/spec/tests/users.spec.ts +31 -22
  12. package/lib/project-files/spec/types/supertest/index.d.ts +14 -0
  13. package/lib/project-files/src/models/user-model.ts +17 -2
  14. package/lib/project-files/src/pre-start/env/development.env +9 -0
  15. package/lib/project-files/src/pre-start/env/production.env +10 -0
  16. package/lib/project-files/src/pre-start/env/test.env +9 -0
  17. package/lib/{auth-proj → project-files}/src/public/scripts/http.js +0 -0
  18. package/lib/{auth-proj → project-files}/src/public/scripts/login.js +0 -0
  19. package/lib/{auth-proj → project-files}/src/public/scripts/users.js +0 -0
  20. package/lib/{auth-proj → project-files}/src/public/stylesheets/login.css +0 -0
  21. package/lib/{auth-proj → project-files}/src/public/stylesheets/users.css +0 -0
  22. package/lib/project-files/src/repos/mock-orm.ts +2 -2
  23. package/lib/project-files/src/repos/user-repo.ts +1 -1
  24. package/lib/project-files/src/routes/api.ts +9 -6
  25. package/lib/{auth-proj → project-files}/src/routes/auth-router.ts +15 -7
  26. package/lib/{auth-proj → project-files}/src/routes/middleware.ts +7 -8
  27. package/lib/project-files/src/routes/user-router.ts +12 -16
  28. package/lib/project-files/src/server.ts +82 -73
  29. package/lib/{auth-proj → project-files}/src/services/auth-service.ts +0 -0
  30. package/lib/project-files/src/services/user-service.ts +8 -8
  31. package/lib/project-files/src/shared/env-vars.ts +18 -0
  32. package/lib/project-files/src/shared/errors.ts +11 -0
  33. package/lib/project-files/src/types/express/index.d.ts +27 -5
  34. package/lib/{auth-proj → project-files}/src/util/jwt-util.ts +3 -8
  35. package/lib/{auth-proj → project-files}/src/views/login.html +0 -0
  36. package/lib/{auth-proj → project-files}/src/views/users.html +0 -0
  37. package/lib/project-files/tsconfig.json +3 -0
  38. package/package.json +1 -1
  39. package/chat-screenshot.png +0 -0
  40. package/lib/auth-proj/.eslintignore +0 -1
  41. package/lib/auth-proj/.eslintrc.json +0 -27
  42. package/lib/auth-proj/build.ts +0 -64
  43. package/lib/auth-proj/gitignore +0 -3
  44. package/lib/auth-proj/package.json +0 -73
  45. package/lib/auth-proj/spec/index.ts +0 -57
  46. package/lib/auth-proj/spec/loadEnv.ts +0 -10
  47. package/lib/auth-proj/spec/nodemon.json +0 -6
  48. package/lib/auth-proj/spec/support/jasmine.json +0 -11
  49. package/lib/auth-proj/spec/tests/users.spec.ts +0 -283
  50. package/lib/auth-proj/src/index.ts +0 -14
  51. package/lib/auth-proj/src/models/user-model.ts +0 -56
  52. package/lib/auth-proj/src/pre-start/env/development.env +0 -23
  53. package/lib/auth-proj/src/pre-start/env/production.env +0 -24
  54. package/lib/auth-proj/src/pre-start/env/test.env +0 -23
  55. package/lib/auth-proj/src/pre-start/index.ts +0 -28
  56. package/lib/auth-proj/src/repos/database.json +0 -1
  57. package/lib/auth-proj/src/repos/mock-orm.ts +0 -41
  58. package/lib/auth-proj/src/repos/user-repo.ts +0 -88
  59. package/lib/auth-proj/src/routes/api.ts +0 -17
  60. package/lib/auth-proj/src/routes/user-router.ts +0 -78
  61. package/lib/auth-proj/src/server.ts +0 -82
  62. package/lib/auth-proj/src/services/user-service.ts +0 -52
  63. package/lib/auth-proj/src/shared/env-vars.ts +0 -20
  64. package/lib/auth-proj/src/shared/errors.ts +0 -45
  65. package/lib/auth-proj/src/shared/functions.ts +0 -18
  66. package/lib/auth-proj/src/types/express/index.d.ts +0 -23
  67. package/lib/auth-proj/tsconfig.json +0 -100
  68. package/lib/auth-proj/tsconfig.prod.json +0 -11
  69. package/lib/project-files/src/public/scripts/index.js +0 -183
  70. package/lib/project-files/src/public/stylesheets/style.css +0 -33
  71. package/lib/project-files/src/views/index.html +0 -31
  72. package/lib/socket-io/.eslintignore +0 -1
  73. package/lib/socket-io/.eslintrc.json +0 -27
  74. package/lib/socket-io/build.ts +0 -64
  75. package/lib/socket-io/gitignore +0 -3
  76. package/lib/socket-io/package.json +0 -75
  77. package/lib/socket-io/spec/index.ts +0 -57
  78. package/lib/socket-io/spec/loadEnv.ts +0 -10
  79. package/lib/socket-io/spec/nodemon.json +0 -6
  80. package/lib/socket-io/spec/support/jasmine.json +0 -11
  81. package/lib/socket-io/spec/support/login-agent.ts +0 -49
  82. package/lib/socket-io/spec/tests/auth.spec.ts +0 -159
  83. package/lib/socket-io/spec/tests/chat.spec.ts +0 -150
  84. package/lib/socket-io/spec/tests/users.spec.ts +0 -283
  85. package/lib/socket-io/src/index.ts +0 -14
  86. package/lib/socket-io/src/models/user-model.ts +0 -56
  87. package/lib/socket-io/src/pre-start/env/development.env +0 -23
  88. package/lib/socket-io/src/pre-start/env/production.env +0 -24
  89. package/lib/socket-io/src/pre-start/env/test.env +0 -23
  90. package/lib/socket-io/src/pre-start/index.ts +0 -28
  91. package/lib/socket-io/src/public/scripts/chat.js +0 -87
  92. package/lib/socket-io/src/public/scripts/http.js +0 -24
  93. package/lib/socket-io/src/public/scripts/index.js +0 -16
  94. package/lib/socket-io/src/public/scripts/login.js +0 -17
  95. package/lib/socket-io/src/public/scripts/users.js +0 -148
  96. package/lib/socket-io/src/public/stylesheets/chat.css +0 -43
  97. package/lib/socket-io/src/public/stylesheets/index.css +0 -13
  98. package/lib/socket-io/src/public/stylesheets/login.css +0 -20
  99. package/lib/socket-io/src/public/stylesheets/users.css +0 -40
  100. package/lib/socket-io/src/repos/database.json +0 -38
  101. package/lib/socket-io/src/repos/mock-orm.ts +0 -41
  102. package/lib/socket-io/src/repos/user-repo.ts +0 -88
  103. package/lib/socket-io/src/routes/api.ts +0 -19
  104. package/lib/socket-io/src/routes/auth-router.ts +0 -67
  105. package/lib/socket-io/src/routes/chat-router.ts +0 -88
  106. package/lib/socket-io/src/routes/middleware.ts +0 -55
  107. package/lib/socket-io/src/routes/user-router.ts +0 -76
  108. package/lib/socket-io/src/server.ts +0 -100
  109. package/lib/socket-io/src/services/auth-service.ts +0 -38
  110. package/lib/socket-io/src/services/user-service.ts +0 -52
  111. package/lib/socket-io/src/shared/env-vars.ts +0 -20
  112. package/lib/socket-io/src/shared/errors.ts +0 -56
  113. package/lib/socket-io/src/shared/functions.ts +0 -18
  114. package/lib/socket-io/src/types/express/index.d.ts +0 -23
  115. package/lib/socket-io/src/util/jwt-util.ts +0 -52
  116. package/lib/socket-io/src/views/chat.html +0 -51
  117. package/lib/socket-io/src/views/login.html +0 -35
  118. package/lib/socket-io/src/views/users.html +0 -47
  119. package/lib/socket-io/tsconfig.json +0 -100
  120. package/lib/socket-io/tsconfig.prod.json +0 -11
@@ -1,27 +0,0 @@
1
- {
2
- "parser": "@typescript-eslint/parser",
3
- "plugins": [
4
- "@typescript-eslint"
5
- ],
6
- "extends": [
7
- "eslint:recommended",
8
- "plugin:@typescript-eslint/recommended",
9
- "plugin:@typescript-eslint/recommended-requiring-type-checking"
10
- ],
11
- "parserOptions": {
12
- "project": "./tsconfig.json"
13
- },
14
- "rules": {
15
- "max-len": [
16
- "error",
17
- {
18
- "code": 100
19
- }
20
- ],
21
- "no-console": 1,
22
- "no-extra-boolean-cast": 0,
23
- "semi": 1,
24
- "@typescript-eslint/no-misused-promises": 0,
25
- "@typescript-eslint/no-floating-promises": 0
26
- }
27
- }
@@ -1,64 +0,0 @@
1
- /**
2
- * Remove old files, copy front-end ones.
3
- */
4
-
5
- import fs from 'fs-extra';
6
- import logger from 'jet-logger';
7
- import childProcess from 'child_process';
8
-
9
-
10
- // Start
11
- (async () => {
12
- try {
13
- // Remove current build
14
- await remove('./dist/');
15
- // Copy front-end files
16
- await copy('./src/public', './dist/public');
17
- await copy('./src/views', './dist/views');
18
- // Copy production env file
19
- await copy('./src/pre-start/env/production.env', './dist/pre-start/env/production.env');
20
- // Copy back-end files
21
- await exec('tsc --build tsconfig.prod.json', './');
22
- } catch (err) {
23
- logger.err(err);
24
- }
25
- })();
26
-
27
- /**
28
- * Remove file
29
- */
30
- function remove(loc: string): Promise<void> {
31
- return new Promise((res, rej) => {
32
- return fs.remove(loc, (err) => {
33
- return (!!err ? rej(err) : res());
34
- });
35
- });
36
- }
37
-
38
- /**
39
- * Copy file.
40
- */
41
- function copy(src: string, dest: string): Promise<void> {
42
- return new Promise((res, rej) => {
43
- return fs.copy(src, dest, (err) => {
44
- return (!!err ? rej(err) : res());
45
- });
46
- });
47
- }
48
-
49
- /**
50
- * Do command line command.
51
- */
52
- function exec(cmd: string, loc: string): Promise<void> {
53
- return new Promise((res, rej) => {
54
- return childProcess.exec(cmd, {cwd: loc}, (err, stdout, stderr) => {
55
- if (!!stdout) {
56
- logger.info(stdout);
57
- }
58
- if (!!stderr) {
59
- logger.warn(stderr);
60
- }
61
- return (!!err ? rej(err) : res());
62
- });
63
- });
64
- }
@@ -1,3 +0,0 @@
1
- **/node_modules
2
- dist/
3
- **/**/*.log
@@ -1,73 +0,0 @@
1
- {
2
- "name": "with-auth",
3
- "version": "0.0.0",
4
- "scripts": {
5
- "build": "npx ts-node build.ts",
6
- "lint": "eslint . --ext .ts",
7
- "start": "node -r module-alias/register ./dist --env=production",
8
- "dev": "nodemon",
9
- "test": "nodemon --config ./spec/nodemon.json",
10
- "test:no-reloading": "npx ts-node --files -r tsconfig-paths/register ./spec"
11
- },
12
- "nodemonConfig": {
13
- "watch": [
14
- "src"
15
- ],
16
- "ext": "ts, html",
17
- "ignore": [
18
- "src/public"
19
- ],
20
- "exec": "./node_modules/.bin/ts-node --files -r tsconfig-paths/register ./src"
21
- },
22
- "_moduleAliases": {
23
- "@services": "dist/services",
24
- "@repos": "dist/repos",
25
- "@entities": "dist/entities",
26
- "@shared": "dist/shared",
27
- "@server": "dist/server",
28
- "@routes": "dist/routes",
29
- "@util": "dist/util"
30
- },
31
- "dependencies": {
32
- "bcrypt": "^5.0.1",
33
- "command-line-args": "^5.2.1",
34
- "cookie-parser": "^1.4.6",
35
- "dotenv": "^16.0.2",
36
- "express": "^4.18.1",
37
- "express-async-errors": "^3.1.1",
38
- "helmet": "^6.0.0",
39
- "http-status-codes": "^2.2.0",
40
- "jet-logger": "^1.2.3",
41
- "jsonfile": "^6.1.0",
42
- "jsonwebtoken": "^8.5.1",
43
- "module-alias": "^2.2.2",
44
- "morgan": "^1.10.0",
45
- "randomstring": "^1.2.2"
46
- },
47
- "devDependencies": {
48
- "@types/bcrypt": "^5.0.0",
49
- "@types/command-line-args": "^5.2.0",
50
- "@types/cookie-parser": "^1.4.3",
51
- "@types/express": "^4.17.13",
52
- "@types/find": "^0.2.1",
53
- "@types/fs-extra": "^9.0.13",
54
- "@types/jasmine": "^4.3.0",
55
- "@types/jsonfile": "^6.1.0",
56
- "@types/jsonwebtoken": "^8.5.9",
57
- "@types/morgan": "^1.9.3",
58
- "@types/node": "^18.7.14",
59
- "@types/randomstring": "^1.1.8",
60
- "@types/supertest": "^2.0.12",
61
- "@typescript-eslint/eslint-plugin": "^5.36.1",
62
- "@typescript-eslint/parser": "^5.36.1",
63
- "eslint": "^8.23.0",
64
- "find": "^0.3.0",
65
- "fs-extra": "^10.1.0",
66
- "jasmine": "^4.3.0",
67
- "nodemon": "^2.0.19",
68
- "supertest": "^6.2.4",
69
- "ts-node": "^10.9.1",
70
- "tsconfig-paths": "^4.1.0",
71
- "typescript": "^4.8.2"
72
- }
73
- }
@@ -1,57 +0,0 @@
1
- import './loadEnv';
2
- import find from 'find';
3
- import Jasmine from 'jasmine';
4
- import commandLineArgs from 'command-line-args';
5
- import logger from 'jet-logger';
6
-
7
-
8
- // Setup command line options
9
- const options = commandLineArgs([
10
- {
11
- name: 'testFile',
12
- alias: 'f',
13
- type: String,
14
- },
15
- ]);
16
-
17
- // Init Jasmine
18
- const jasmine = new Jasmine();
19
- jasmine.exitOnCompletion = false;
20
-
21
- // Set location of test files
22
- jasmine.loadConfig({
23
- random: true,
24
- spec_dir: 'spec',
25
- spec_files: [
26
- './tests/**/*.spec.ts',
27
- ],
28
- stopSpecOnExpectationFailure: false,
29
- });
30
-
31
- // Run all or a single unit-test
32
- let execResp: Promise<jasmine.JasmineDoneInfo> | undefined;
33
- if (options.testFile) {
34
- const testFile = options.testFile as string;
35
- find.file(testFile + '.spec.ts', './spec', (files: string[]) => {
36
- if (files.length === 1) {
37
- jasmine.specFiles = [files[0]];
38
- jasmine.execute();
39
- } else {
40
- logger.err('Test file not found!');
41
- }
42
- });
43
- } else {
44
- execResp = jasmine.execute();
45
- }
46
-
47
- // Wait for tests to finish
48
- (async () => {
49
- if (!!execResp) {
50
- const info = await execResp;
51
- if (info.overallStatus === 'passed') {
52
- logger.info('All tests have passed :)');
53
- } else {
54
- logger.err('At least one test has failed :(');
55
- }
56
- }
57
- })();
@@ -1,10 +0,0 @@
1
- import dotenv from 'dotenv';
2
-
3
-
4
- const result2 = dotenv.config({
5
- path: `./src/pre-start/env/test.env`,
6
- });
7
-
8
- if (result2.error) {
9
- throw result2.error;
10
- }
@@ -1,6 +0,0 @@
1
- {
2
- "watch": ["spec"],
3
- "ext": "spec.ts",
4
- "ignore": ["spec/support"],
5
- "exec": "./node_modules/.bin/ts-node --files -r tsconfig-paths/register ./spec"
6
- }
@@ -1,11 +0,0 @@
1
- {
2
- "spec_dir": "spec",
3
- "spec_files": [
4
- "**/*[sS]pec.ts"
5
- ],
6
- "helpers": [
7
- "helpers/**/*.js"
8
- ],
9
- "stopSpecOnExpectationFailure": false,
10
- "random": true
11
- }
@@ -1,283 +0,0 @@
1
- import supertest from 'supertest';
2
- import StatusCodes from 'http-status-codes';
3
- import { SuperTest, Test, Response } from 'supertest';
4
-
5
- import app from '@server';
6
- import userRepo from '@repos/user-repo';
7
- import User, { IUser } from '@models/user-model';
8
- import { pErr } from '@shared/functions';
9
- import { p as userPaths } from '@routes/user-router';
10
- import loginAgent from '../support/login-agent';
11
- import { ParamMissingError, UserNotFoundError } from '@shared/errors';
12
-
13
-
14
- // **** Variables **** //
15
-
16
- // Misc
17
- const usersPath = '/api/users',
18
- getUsersPath = `${usersPath}${userPaths.get}`,
19
- addUsersPath = `${usersPath}${userPaths.add}`,
20
- updateUserPath = `${usersPath}${userPaths.update}`,
21
- deleteUserPath = `${usersPath}${userPaths.delete}`,
22
- { BAD_REQUEST, CREATED, OK } = StatusCodes;
23
-
24
- // Dummy users for GET req
25
- const dummyGetAllUsers = [
26
- User.new('Sean Maxwell', 'sean.maxwell@gmail.com'),
27
- User.new('John Smith', 'john.smith@gmail.com'),
28
- User.new('Gordan Freeman', 'gordan.freeman@gmail.com'),
29
- ] as const;
30
-
31
- // Dummy update user
32
- const dummyUserData = {
33
- user: User.new('Gordan Freeman', 'gordan.freeman@gmail.com'),
34
- } as const;
35
-
36
- // Test messages
37
- const msgs = {
38
- getUsersSuccess: `should return a JSON object with all the users and a status code of
39
- "${OK}" if the request was successful.`,
40
- getUsersBad: `should return a JSON object containing an error message and a status code of
41
- "${BAD_REQUEST}" if the request was unsuccessful.`,
42
- addUserSuccess: `should return a status code of "${CREATED}" if the request was successful.`,
43
- addUserFailedMissingParam: `should return a JSON object with an error message of
44
- "${ParamMissingError.Msg}" and a status code of "${BAD_REQUEST}" if the user param was
45
- missing.`,
46
- addUserFallbackErr: `should return a JSON object with an error message and a status code
47
- of "${BAD_REQUEST}" if the request was unsuccessful.`,
48
- updateSuccess: `should return a status code of "${OK}" if the request was successful.`,
49
- updateParamMissing: `should return a JSON object with an error message of
50
- "${ParamMissingError.Msg}" and a status code of "${BAD_REQUEST}" if the user param was
51
- missing.`,
52
- updateUserNotFound: `should return a JSON object with the error message of
53
- ${UserNotFoundError.Msg} and a status code of "${UserNotFoundError.HttpStatus}" if the id
54
- was not found.`,
55
- updateFallbackErr: `should return a JSON object with an error message and a status code of
56
- "${BAD_REQUEST}" if the request was unsuccessful.`,
57
- deleteSuccessful: `should return a status code of "${OK}" if the request was successful.`,
58
- deleteUserNotFound: `should return a JSON object with the error message of
59
- ${UserNotFoundError.Msg} and a status code of "${UserNotFoundError.HttpStatus}" if the id was
60
- not found.`,
61
- deleteFallbackErr: `should return a JSON object with an error message and a status code of
62
- "${BAD_REQUEST}" if the request was unsuccessful.`,
63
- };
64
-
65
-
66
- // **** Types **** //
67
-
68
- type TReqBody = string | object | undefined;
69
-
70
-
71
- // **** Tests **** //
72
-
73
- describe('user-router', () => {
74
-
75
- let agent: SuperTest<Test>;
76
- let jwtCookie: string;
77
-
78
-
79
- // Run before all tests
80
- beforeAll((done) => {
81
- agent = supertest.agent(app);
82
- loginAgent.login(agent, (cookie: string) => {
83
- jwtCookie = cookie;
84
- done();
85
- });
86
- });
87
-
88
- // Test get users
89
- describe(`"GET:${getUsersPath}"`, () => {
90
-
91
- const callApi = () =>
92
- agent.get(getUsersPath)
93
- .set('Cookie', jwtCookie);
94
-
95
- // Get all users
96
- it(msgs.getUsersSuccess, (done) => {
97
- const ret = Promise.resolve([...dummyGetAllUsers]);
98
- spyOn(userRepo, 'getAll').and.returnValue(ret);
99
- // Call API
100
- callApi().end((err: Error, res: Response) => {
101
- pErr(err);
102
- expect(res.status).toBe(OK);
103
- // Caste instance-objects to 'User' objects
104
- const respUsers = res.body.users;
105
- const retUsers: IUser[] = respUsers.map((user: IUser) => User.copy(user));
106
- expect(retUsers).toEqual(dummyGetAllUsers);
107
- expect(res.body.error).toBeUndefined();
108
- done();
109
- });
110
- });
111
-
112
- // Get all users bad
113
- it(msgs.getUsersBad, (done) => {
114
- const errMsg = 'Could not fetch users.';
115
- spyOn(userRepo, 'getAll').and.throwError(errMsg);
116
- // Call API
117
- callApi()
118
- .end((err: Error, res: Response) => {
119
- pErr(err);
120
- expect(res.status).toBe(BAD_REQUEST);
121
- expect(res.body.error).toBe(errMsg);
122
- done();
123
- });
124
- });
125
- });
126
-
127
- // Test add user
128
- describe(`"POST:${addUsersPath}"`, () => {
129
-
130
- // Consts
131
- const callApi = (reqBody: TReqBody) => {
132
- return agent.post(addUsersPath)
133
- .set('Cookie', jwtCookie)
134
- .type('form').send(reqBody);
135
- };
136
-
137
- // Test add user success
138
- it(msgs.addUserSuccess, (done) => {
139
- spyOn(userRepo, 'add').and.returnValue(Promise.resolve());
140
- callApi(dummyUserData)
141
- .end((err: Error, res: Response) => {
142
- pErr(err);
143
- expect(res.status).toBe(CREATED);
144
- expect(res.body.error).toBeUndefined();
145
- done();
146
- });
147
- });
148
-
149
- // Test add user failed due to missing param
150
- it(msgs.addUserFailedMissingParam, (done) => {
151
- callApi({})
152
- .end((err: Error, res: Response) => {
153
- pErr(err);
154
- expect(res.status).toBe(BAD_REQUEST);
155
- expect(res.body.error).toBe(ParamMissingError.Msg);
156
- done();
157
- });
158
- });
159
-
160
- // Default error
161
- it(msgs.addUserFallbackErr, (done) => {
162
- const errMsg = 'Could not add user.';
163
- spyOn(userRepo, 'add').and.throwError(errMsg);
164
- // Call API
165
- callApi(dummyUserData)
166
- .end((err: Error, res: Response) => {
167
- pErr(err);
168
- expect(res.status).toBe(BAD_REQUEST);
169
- expect(res.body.error).toBe(errMsg);
170
- done();
171
- });
172
- });
173
- });
174
-
175
- // Test update users
176
- describe(`"PUT:${updateUserPath}"`, () => {
177
-
178
- const callApi = (reqBody: TReqBody) =>
179
- agent.put(updateUserPath)
180
- .set('Cookie', jwtCookie)
181
- .type('form').send(reqBody);
182
-
183
- // Test updating a user success
184
- it(msgs.updateSuccess, (done) => {
185
- spyOn(userRepo, 'update').and.returnValue(Promise.resolve());
186
- spyOn(userRepo, 'persists').and.returnValue(Promise.resolve(true));
187
- // Call api
188
- callApi(dummyUserData)
189
- .end((err: Error, res: Response) => {
190
- pErr(err);
191
- expect(res.status).toBe(OK);
192
- expect(res.body.error).toBeUndefined();
193
- done();
194
- });
195
- });
196
-
197
- // Test updating a user failed because a param was missing
198
- it(msgs.updateParamMissing, (done) => {
199
- callApi({})
200
- .end((err: Error, res: Response) => {
201
- pErr(err);
202
- expect(res.status).toBe(BAD_REQUEST);
203
- expect(res.body.error).toBe(ParamMissingError.Msg);
204
- done();
205
- });
206
- });
207
-
208
- // Update user not found
209
- it(msgs.updateUserNotFound, (done) => {
210
- callApi(dummyUserData)
211
- .end((err: Error, res: Response) => {
212
- pErr(err);
213
- expect(res.status).toBe(UserNotFoundError.HttpStatus);
214
- expect(res.body.error).toBe(UserNotFoundError.Msg);
215
- done();
216
- });
217
- });
218
-
219
- // Update fallback error
220
- it(msgs.updateFallbackErr, (done) => {
221
- const updateErrMsg = 'Could not update user.';
222
- spyOn(userRepo, 'persists').and.returnValue(Promise.resolve(true));
223
- spyOn(userRepo, 'update').and.throwError(updateErrMsg);
224
- // Call API
225
- callApi(dummyUserData)
226
- .end((err: Error, res: Response) => {
227
- pErr(err);
228
- expect(res.status).toBe(BAD_REQUEST);
229
- expect(res.body.error).toBe(updateErrMsg);
230
- done();
231
- });
232
- });
233
- });
234
-
235
- // Test delete user
236
- describe(`"DELETE:${deleteUserPath}"`, () => {
237
-
238
- const callApi = (id: number) => {
239
- const path = deleteUserPath.replace(':id', id.toString());
240
- return agent.delete(path).set('Cookie', jwtCookie);
241
- };
242
-
243
- // Delete user successful
244
- it(msgs.deleteSuccessful, (done) => {
245
- spyOn(userRepo, 'delete').and.returnValue(Promise.resolve());
246
- spyOn(userRepo, 'persists').and.returnValue(Promise.resolve(true));
247
- // Call api
248
- callApi(5)
249
- .end((err: Error, res: Response) => {
250
- pErr(err);
251
- expect(res.status).toBe(OK);
252
- expect(res.body.error).toBeUndefined();
253
- done();
254
- });
255
- });
256
-
257
- // Delete, user not found error
258
- it(msgs.deleteUserNotFound, (done) => {
259
- callApi(-1)
260
- .end((err: Error, res: Response) => {
261
- pErr(err);
262
- expect(res.status).toBe(UserNotFoundError.HttpStatus);
263
- expect(res.body.error).toBe(UserNotFoundError.Msg);
264
- done();
265
- });
266
- });
267
-
268
- // Delete user fallback error
269
- it(msgs.deleteFallbackErr, (done) => {
270
- spyOn(userRepo, 'persists').and.returnValue(Promise.resolve(true));
271
- const deleteErrMsg = 'Could not delete user.';
272
- spyOn(userRepo, 'delete').and.throwError(deleteErrMsg);
273
- // Call API
274
- callApi(1)
275
- .end((err: Error, res: Response) => {
276
- pErr(err);
277
- expect(res.status).toBe(BAD_REQUEST);
278
- expect(res.body.error).toBe(deleteErrMsg);
279
- done();
280
- });
281
- });
282
- });
283
- });
@@ -1,14 +0,0 @@
1
- import './pre-start'; // Must be the first import
2
- import logger from 'jet-logger';
3
-
4
- import envVars from '@shared/env-vars';
5
- import server from './server';
6
-
7
-
8
- // Constants
9
- const serverStartMsg = 'Express server started on port: ';
10
-
11
- // Start server
12
- server.listen(envVars.port, () => {
13
- logger.info(serverStartMsg + envVars.port.toString());
14
- });
@@ -1,56 +0,0 @@
1
- // **** Types **** //
2
-
3
- export enum UserRoles {
4
- Standard,
5
- Admin,
6
- }
7
-
8
- export interface IUser {
9
- id: number;
10
- name: string;
11
- email: string;
12
- pwdHash: string;
13
- role: UserRoles;
14
- }
15
-
16
-
17
- // **** Functions **** //
18
-
19
- /**
20
- * Get a new User object.
21
- */
22
- function _new(
23
- name: string,
24
- email: string,
25
- role?: UserRoles,
26
- pwdHash?: string,
27
- ): IUser {
28
- return {
29
- id: -1,
30
- email,
31
- name,
32
- role: (role ?? UserRoles.Standard),
33
- pwdHash: (pwdHash ?? ''),
34
- };
35
- }
36
-
37
- /**
38
- * Copy a user object.
39
- */
40
- function copy(user: IUser): IUser {
41
- return {
42
- id: user.id,
43
- email: user.email,
44
- name: user.name,
45
- role: user.role,
46
- pwdHash: user.pwdHash,
47
- };
48
- }
49
-
50
-
51
- // **** Export default **** //
52
-
53
- export default {
54
- new: _new,
55
- copy,
56
- };
@@ -1,23 +0,0 @@
1
- ## Environment ##
2
- NODE_ENV=development
3
-
4
-
5
- ## Server ##
6
- PORT=3000
7
- HOST=localhost
8
-
9
-
10
- ## Setup jet-logger ##
11
- JET_LOGGER_MODE=CONSOLE
12
- JET_LOGGER_FILEPATH=jet-logger.log
13
- JET_LOGGER_TIMESTAMP=TRUE
14
- JET_LOGGER_FORMAT=LINE
15
-
16
- ## Authentication ##
17
- COOKIE_DOMAIN=localhost
18
- COOKIE_PATH=/
19
- SECURE_COOKIE=false
20
- JWT_SECRET=xxxxxxxxxxxxxx
21
- COOKIE_SECRET=xxxxxxxxxxxxxx
22
- # expires in 3 days
23
- COOKIE_EXP=259200000
@@ -1,24 +0,0 @@
1
- ## Environment ##
2
- NODE_ENV=production
3
-
4
-
5
- ## Server ##
6
- PORT=8081
7
- HOST=localhost
8
-
9
-
10
- ## Setup jet-logger ##
11
- JET_LOGGER_MODE=FILE
12
- JET_LOGGER_FILEPATH=jet-logger.log
13
- JET_LOGGER_TIMESTAMP=TRUE
14
- JET_LOGGER_FORMAT=LINE
15
-
16
- ## Authentication ##
17
- # SECURE_COOKIE 'false' here for demo-ing. But ideally should be true.
18
- COOKIE_DOMAIN=localhost
19
- COOKIE_PATH=/
20
- SECURE_COOKIE=false
21
- JWT_SECRET=xxxxxxxxxxxxxx
22
- COOKIE_SECRET=xxxxxxxxxxxxxx
23
- # expires in 3 days
24
- COOKIE_EXP=259200000
@@ -1,23 +0,0 @@
1
- ## Environment ##
2
- NODE_ENV=test
3
-
4
-
5
- ## Server ##
6
- PORT=4000
7
- HOST=localhost
8
-
9
-
10
- ## Setup jet-logger ##
11
- JET_LOGGER_MODE=CONSOLE
12
- JET_LOGGER_FILEPATH=jet-logger.log
13
- JET_LOGGER_TIMESTAMP=TRUE
14
- JET_LOGGER_FORMAT=LINE
15
-
16
- ## Authentication ##
17
- COOKIE_DOMAIN=localhost
18
- COOKIE_PATH=/
19
- SECURE_COOKIE=false
20
- JWT_SECRET=xxxxxxxxxxxxxx
21
- COOKIE_SECRET=xxxxxxxxxxxxxx
22
- # expires in 3 days
23
- COOKIE_EXP=259200000