Skip to content

Commit 15339f2

Browse files
feat: documentation (#10)
1 parent 33eb440 commit 15339f2

24 files changed

+146
-39
lines changed

app/internal/adapters/auth/jwt.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/**
2-
* @module Auth/JWT
3-
*/
1+
/** @module Auth */
42

53
import jsonwebtoken from 'jsonwebtoken';
64

@@ -27,6 +25,11 @@ const generateToken = (adminID, expiresIn, secretKey) =>
2725
*/
2826
const verifyToken = (token, secretKey) => jsonwebtoken.verify(token, secretKey);
2927

28+
/**
29+
* @namespace JWT
30+
* @memberof module:Auth
31+
* @constant
32+
*/
3033
export const JWT = {
3134
generateToken,
3235
verifyToken,

app/internal/adapters/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { AuthRoute, MainRoute, UsersRoute } from './routes';
33

44
/**
55
* Adapters module containing various adapters.
6+
* @namespace
67
* @property {object} Auth - Authentication adapter.
78
* @property {object} Routes - Routes adapter.
89
*/

app/internal/adapters/routes/auth.routes.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
/**
2-
* @module Routes/Auth
3-
*/
4-
51
import express from 'express';
62
import { AuthController } from '../../core/controllers';
73

84
/**
95
* Set up routes related to authentication.
6+
* @namespace Auth
107
* @function AuthRoute
118
* @param {express.Application} app - The Express application.
9+
* @memberof module:Routes
1210
*/
1311
export const AuthRoute = (app) => {
1412
const router = express.Router();

app/internal/adapters/routes/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @module Routes */
12
export * from './main.routes';
23
export * from './users.routes';
34
export * from './auth.routes';

app/internal/adapters/routes/main.routes.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
/**
2-
* @module Routes/Main
3-
*/
4-
51
import express from 'express';
62
import { MainController } from '../../core/controllers';
73

84
/**
95
* Set up main routes.
6+
* @namespace Main
107
* @function MainRoute
118
* @param {express.Application} app - The Express application.
9+
* @memberof module:Routes
1210
*/
1311
export const MainRoute = (app) => {
1412
const router = express.Router();

app/internal/adapters/routes/users.routes.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
/**
2-
* @module Routes/Users
3-
*/
4-
51
import express from 'express';
62
import { UsersController } from '../../core/controllers';
73

84
/**
95
* Set up routes related to Users CRUD operations
6+
* @namespace Users
107
* @function UsersRoute
118
* @param {express.Application} app - The Express application.
9+
* @memberof module:Routes
1210
*/
1311
export const UsersRoute = (app) => {
1412
const router = express.Router();

app/internal/core/controllers/auth.controller.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
/**
2-
* @module Controllers/Auth
3-
*/
4-
1+
/** @module Controllers */
52
import { constants } from 'http2';
63
import { AuthServices } from '../services';
74

@@ -25,6 +22,11 @@ const getToken = (req, res, next) => {
2522
}
2623
};
2724

25+
/**
26+
* @namespace AuthController
27+
* @memberof module:Controllers
28+
* @constant
29+
*/
2830
export const AuthController = {
2931
getToken,
3032
};

app/internal/core/controllers/main.controller.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/**
2-
* @module Controllers/Main
3-
*/
1+
/** @module Controllers */
42

53
import { constants } from 'http2';
64
import { MainServices } from '../services';
@@ -21,6 +19,10 @@ const healthCheck = (req, res, next) => {
2119
}
2220
};
2321

22+
/**
23+
* @namespace MainController
24+
* @constant
25+
*/
2426
export const MainController = {
2527
healthCheck,
2628
};

app/internal/core/controllers/users.controller.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
* @module Controllers/Users
3-
*/
41
import { constants } from 'http2';
52
import { UserServices } from '../services';
63

@@ -68,6 +65,11 @@ const drop = async (req, res, next) => {
6865
}
6966
};
7067

68+
/**
69+
* @namespace UsersController
70+
* @memberof module:Controllers
71+
* @constant
72+
*/
7173
export const UsersController = {
7274
getById,
7375
create,
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from './logs';
2-
export * from './error';
3-
export * from './configurations';
4-
export * from './auth';
1+
export * from './logs.middleware';
2+
export * from './error.middleware';
3+
export * from './configurations.middleware';
4+
export * from './auth.middleware';

app/internal/core/models/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './users';
1+
export * from './users.model';

app/internal/core/providers/config.provider.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/**
2-
* @module Providers/Config
3-
*/
1+
/** @module Providers */
42

53
/**
64
* Default configuration object.
@@ -12,7 +10,10 @@
1210

1311
/**
1412
* Default configuration for the application.
13+
* @namespace defaultConfig
14+
* @memberof module:Providers
1515
* @type {DefaultConfig}
16+
* @constant
1617
*/
1718
export const defaultConfig = {
1819
auth: {

jsdoc.json

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"sort": true,
2424
"collapse": true,
2525
"typedefs": true,
26+
"sectionOrder": [
27+
"Modules",
28+
"Namespaces"
29+
],
2630
"menu": {
2731
"Github repo": {
2832
"href": "https://github.com/wizeline/CA-Microservices-NodeJS",

test/functional/controllers/auth.controller.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { constants } from 'http2';
22
import { AuthController } from '../../../app/internal/core/controllers';
33

44
describe('AuthController', () => {
5-
it('Create token is requested', () => {
5+
it('should create token is requested', () => {
66
const req = {
77
body: {
88
adminId: 123,

test/functional/controllers/main.controller.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { constants } from 'http2';
22
import { MainController } from '../../../app/internal/core/controllers';
33

44
describe('MainController', () => {
5-
it('health check is requested', () => {
5+
it('should request health check', () => {
66
const req = {
77
body: {
88
admin_id: 123,

test/functional/controllers/users.controller.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { constants } from 'http2';
22
import { UsersController } from '../../../app/internal/core/controllers';
33

44
describe('UsersController GetByID', () => {
5-
it('Get by ID is requested', () => {
5+
it('should get by ID', () => {
66
const res = { json: jest.fn((x) => x), status: jest.fn((x) => x) };
77
const callback = () => null;
88

@@ -12,7 +12,7 @@ describe('UsersController GetByID', () => {
1212
});
1313

1414
describe('UsersController Create', () => {
15-
it('Create is requested', () => {
15+
it('should request create', () => {
1616
const res = { json: jest.fn((x) => x), status: jest.fn((x) => x) };
1717
const callback = () => null;
1818

@@ -22,7 +22,7 @@ describe('UsersController Create', () => {
2222
});
2323

2424
describe('UsersController Update', () => {
25-
it('Update is requested', () => {
25+
it('should request an update', () => {
2626
const res = { json: jest.fn((x) => x), status: jest.fn((x) => x) };
2727
const callback = () => null;
2828

@@ -32,7 +32,7 @@ describe('UsersController Update', () => {
3232
});
3333

3434
describe('UsersController Delete', () => {
35-
it('Delete is requested', () => {
35+
it('should request a delete', () => {
3636
const res = { json: jest.fn((x) => x), status: jest.fn((x) => x) };
3737
const callback = () => null;
3838

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Adapters } from '../../../app/internal/adapters';
2+
import { Auth } from '../../../app/internal/core/middlewares';
3+
4+
const { JWT } = Adapters.Auth;
5+
6+
describe('AuthMiddleware', () => {
7+
const req = {
8+
headers: {
9+
authorization: 'Bearer valid_token',
10+
},
11+
path: '/users/some-route',
12+
defaultConfig: {
13+
auth: {
14+
secretKey: 'your_secret_key',
15+
},
16+
},
17+
};
18+
19+
const res = {};
20+
const next = jest.fn();
21+
22+
it('should call next if token is valid and path is in tokenPaths', () => {
23+
jest.spyOn(JWT, 'verifyToken').mockReturnValueOnce(true);
24+
Auth(req, res, next);
25+
expect(next).toHaveBeenCalled();
26+
});
27+
28+
it('should call next if token is not provided', () => {
29+
req.headers.authorization = undefined;
30+
Auth(req, res, next);
31+
expect(next).toHaveBeenCalled();
32+
});
33+
34+
it('should call next if token is "undefined"', () => {
35+
req.headers.authorization = 'Bearer undefined';
36+
Auth(req, res, next);
37+
expect(next).toHaveBeenCalled();
38+
});
39+
40+
it('should call next if path is not in tokenPaths', () => {
41+
req.path = '/some-other-route';
42+
Auth(req, res, next);
43+
expect(next).toHaveBeenCalled();
44+
});
45+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { constants } from 'http2';
2+
import { HandlerError } from '../../../app/internal/core/middlewares';
3+
4+
describe('HandlerErrorMiddleware', () => {
5+
it('should send a JSON response with the error message and set status code', () => {
6+
const errorMessage = 'This is an error message';
7+
const errorStatus = constants.HTTP_STATUS_BAD_REQUEST;
8+
const res = {
9+
json: jest.fn(),
10+
status: jest.fn(),
11+
};
12+
const next = jest.fn();
13+
14+
HandlerError(
15+
{ message: errorMessage, status: errorStatus },
16+
null,
17+
res,
18+
next,
19+
);
20+
21+
expect(res.json).toHaveBeenCalledWith({ error: errorMessage });
22+
expect(res.status).toHaveBeenCalledWith(errorStatus);
23+
expect(next).not.toHaveBeenCalled(); // Ensure next function is not called
24+
});
25+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Logger } from '../../../app/internal/core/middlewares';
2+
3+
describe('LoggerMiddleware', () => {
4+
let req;
5+
let res;
6+
let next;
7+
8+
beforeEach(() => {
9+
req = {
10+
path: '/test',
11+
};
12+
res = {};
13+
next = jest.fn();
14+
});
15+
16+
it('should attach a logger to the request object', () => {
17+
Logger(req, res, next);
18+
expect(req.logger).toBeDefined();
19+
expect(req.logger.info).toBeDefined();
20+
});
21+
22+
it('should call the next function', () => {
23+
Logger(req, res, next);
24+
25+
expect(next).toHaveBeenCalled();
26+
});
27+
});

0 commit comments

Comments
 (0)