express-generator-typescript 1.10.1 → 2.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/README.md +37 -25
- package/bin/cli.js +2 -15
- package/lib/cli.js +1 -15
- package/lib/express-generator-typescript.js +15 -23
- package/lib/project-files/.eslintrc.json +15 -2
- package/lib/project-files/build.ts +2 -2
- package/lib/project-files/package.json +15 -5
- package/lib/project-files/spec/nodemon.json +1 -1
- package/lib/{auth-proj → project-files}/spec/support/login-agent.ts +0 -0
- package/lib/{auth-proj → project-files}/spec/tests/auth.spec.ts +18 -32
- package/lib/project-files/spec/tests/users.spec.ts +31 -22
- package/lib/project-files/spec/types/supertest/index.d.ts +14 -0
- package/lib/project-files/src/models/user-model.ts +17 -2
- package/lib/project-files/src/pre-start/env/development.env +9 -0
- package/lib/project-files/src/pre-start/env/production.env +10 -0
- package/lib/project-files/src/pre-start/env/test.env +9 -0
- package/lib/{auth-proj → project-files}/src/public/scripts/http.js +0 -0
- package/lib/{auth-proj → project-files}/src/public/scripts/login.js +0 -0
- package/lib/{auth-proj → project-files}/src/public/scripts/users.js +0 -0
- package/lib/{auth-proj → project-files}/src/public/stylesheets/login.css +0 -0
- package/lib/{auth-proj → project-files}/src/public/stylesheets/users.css +0 -0
- package/lib/project-files/src/repos/user-repo.ts +7 -7
- package/lib/project-files/src/routes/api.ts +9 -6
- package/lib/{auth-proj → project-files}/src/routes/auth-router.ts +11 -4
- package/lib/{auth-proj → project-files}/src/routes/middleware.ts +7 -8
- package/lib/project-files/src/routes/user-router.ts +12 -16
- package/lib/project-files/src/server.ts +82 -73
- package/lib/{auth-proj → project-files}/src/services/auth-service.ts +0 -0
- package/lib/project-files/src/services/user-service.ts +8 -8
- package/lib/project-files/src/shared/env-vars.ts +18 -0
- package/lib/project-files/src/shared/errors.ts +11 -0
- package/lib/project-files/src/types/express/index.d.ts +27 -5
- package/lib/{auth-proj → project-files}/src/util/jwt-util.ts +4 -9
- package/lib/{auth-proj → project-files}/src/views/login.html +0 -0
- package/lib/{auth-proj → project-files}/src/views/users.html +0 -0
- package/lib/project-files/tsconfig.json +3 -0
- package/package.json +1 -1
- package/chat-screenshot.png +0 -0
- package/lib/auth-proj/.eslintignore +0 -1
- package/lib/auth-proj/.eslintrc.json +0 -27
- package/lib/auth-proj/build.ts +0 -64
- package/lib/auth-proj/gitignore +0 -3
- package/lib/auth-proj/package.json +0 -73
- package/lib/auth-proj/spec/index.ts +0 -57
- package/lib/auth-proj/spec/loadEnv.ts +0 -10
- package/lib/auth-proj/spec/nodemon.json +0 -6
- package/lib/auth-proj/spec/support/jasmine.json +0 -11
- package/lib/auth-proj/spec/tests/users.spec.ts +0 -283
- package/lib/auth-proj/src/index.ts +0 -14
- package/lib/auth-proj/src/models/user-model.ts +0 -56
- package/lib/auth-proj/src/pre-start/env/development.env +0 -23
- package/lib/auth-proj/src/pre-start/env/production.env +0 -24
- package/lib/auth-proj/src/pre-start/env/test.env +0 -23
- package/lib/auth-proj/src/pre-start/index.ts +0 -28
- package/lib/auth-proj/src/repos/database.json +0 -1
- package/lib/auth-proj/src/repos/mock-orm.ts +0 -41
- package/lib/auth-proj/src/repos/user-repo.ts +0 -88
- package/lib/auth-proj/src/routes/api.ts +0 -17
- package/lib/auth-proj/src/routes/user-router.ts +0 -78
- package/lib/auth-proj/src/server.ts +0 -82
- package/lib/auth-proj/src/services/user-service.ts +0 -52
- package/lib/auth-proj/src/shared/env-vars.ts +0 -20
- package/lib/auth-proj/src/shared/errors.ts +0 -45
- package/lib/auth-proj/src/shared/functions.ts +0 -18
- package/lib/auth-proj/src/types/express/index.d.ts +0 -23
- package/lib/auth-proj/tsconfig.json +0 -100
- package/lib/auth-proj/tsconfig.prod.json +0 -11
- package/lib/project-files/src/public/scripts/index.js +0 -183
- package/lib/project-files/src/public/stylesheets/style.css +0 -33
- package/lib/project-files/src/views/index.html +0 -31
- package/lib/socket-io/.eslintignore +0 -1
- package/lib/socket-io/.eslintrc.json +0 -27
- package/lib/socket-io/build.ts +0 -64
- package/lib/socket-io/gitignore +0 -3
- package/lib/socket-io/package.json +0 -75
- package/lib/socket-io/spec/index.ts +0 -57
- package/lib/socket-io/spec/loadEnv.ts +0 -10
- package/lib/socket-io/spec/nodemon.json +0 -6
- package/lib/socket-io/spec/support/jasmine.json +0 -11
- package/lib/socket-io/spec/support/login-agent.ts +0 -49
- package/lib/socket-io/spec/tests/auth.spec.ts +0 -159
- package/lib/socket-io/spec/tests/chat.spec.ts +0 -150
- package/lib/socket-io/spec/tests/users.spec.ts +0 -283
- package/lib/socket-io/src/index.ts +0 -14
- package/lib/socket-io/src/models/user-model.ts +0 -56
- package/lib/socket-io/src/pre-start/env/development.env +0 -23
- package/lib/socket-io/src/pre-start/env/production.env +0 -24
- package/lib/socket-io/src/pre-start/env/test.env +0 -23
- package/lib/socket-io/src/pre-start/index.ts +0 -28
- package/lib/socket-io/src/public/scripts/chat.js +0 -87
- package/lib/socket-io/src/public/scripts/http.js +0 -24
- package/lib/socket-io/src/public/scripts/index.js +0 -16
- package/lib/socket-io/src/public/scripts/login.js +0 -17
- package/lib/socket-io/src/public/scripts/users.js +0 -148
- package/lib/socket-io/src/public/stylesheets/chat.css +0 -43
- package/lib/socket-io/src/public/stylesheets/index.css +0 -13
- package/lib/socket-io/src/public/stylesheets/login.css +0 -20
- package/lib/socket-io/src/public/stylesheets/users.css +0 -40
- package/lib/socket-io/src/repos/database.json +0 -38
- package/lib/socket-io/src/repos/mock-orm.ts +0 -41
- package/lib/socket-io/src/repos/user-repo.ts +0 -88
- package/lib/socket-io/src/routes/api.ts +0 -19
- package/lib/socket-io/src/routes/auth-router.ts +0 -67
- package/lib/socket-io/src/routes/chat-router.ts +0 -88
- package/lib/socket-io/src/routes/middleware.ts +0 -55
- package/lib/socket-io/src/routes/user-router.ts +0 -76
- package/lib/socket-io/src/server.ts +0 -100
- package/lib/socket-io/src/services/auth-service.ts +0 -38
- package/lib/socket-io/src/services/user-service.ts +0 -52
- package/lib/socket-io/src/shared/env-vars.ts +0 -20
- package/lib/socket-io/src/shared/errors.ts +0 -56
- package/lib/socket-io/src/shared/functions.ts +0 -18
- package/lib/socket-io/src/types/express/index.d.ts +0 -23
- package/lib/socket-io/src/util/jwt-util.ts +0 -52
- package/lib/socket-io/src/views/chat.html +0 -51
- package/lib/socket-io/src/views/login.html +0 -35
- package/lib/socket-io/src/views/users.html +0 -47
- package/lib/socket-io/tsconfig.json +0 -100
- package/lib/socket-io/tsconfig.prod.json +0 -11
package/lib/auth-proj/build.ts
DELETED
|
@@ -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
|
-
}
|
package/lib/auth-proj/gitignore
DELETED
|
@@ -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,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
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pre-start is where we want to place things that must run BEFORE the express server is started.
|
|
3
|
-
* This is useful for environment variables, command-line arguments, and cron-jobs.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import path from 'path';
|
|
7
|
-
import dotenv from 'dotenv';
|
|
8
|
-
import commandLineArgs from 'command-line-args';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// Setup command line options
|
|
12
|
-
const options = commandLineArgs([
|
|
13
|
-
{
|
|
14
|
-
name: 'env',
|
|
15
|
-
alias: 'e',
|
|
16
|
-
defaultValue: 'development',
|
|
17
|
-
type: String,
|
|
18
|
-
},
|
|
19
|
-
]);
|
|
20
|
-
|
|
21
|
-
// Set the env file
|
|
22
|
-
const result2 = dotenv.config({
|
|
23
|
-
path: path.join(__dirname, `env/${String(options.env)}.env`),
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
if (result2.error) {
|
|
27
|
-
throw result2.error;
|
|
28
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"users":[{"name":"Sean Maxwell","email":"sean.maxwell@gmail.com","pwdHash":"$2b$12$1mE2OI9hMS/rgH9Mi0s85OM2V5gzm7aF3gJIWH1y0S1MqVBueyjsy","role":1,"id":159123164363},{"name":"Gordan Freeman","email":"gordan.freeman@halflife.com","pwdHash":"$2b$12$1mE2OI9hMS/rgH9Mi0s85OM2V5gzm7aF3gJIWH1y0S1MqVBueyjsy","role":0,"id":906524522143},{"name":"John Smith","email":"jsmith@yahoo.com","pwdHash":"$2b$12$1mE2OI9hMS/rgH9Mi0s85OM2V5gzm7aF3gJIWH1y0S1MqVBueyjsy","role":0,"id":357437875835}]}
|