@seidor-cloud-produtos/orbit-backend-lib 2.0.110 → 2.0.112
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/dist/clean-arch/application/cache/cache.spec.d.ts +1 -0
- package/dist/clean-arch/application/cache/cache.spec.js +178 -0
- package/dist/clean-arch/application/consistency-event-dispatcher/consistency-event-dispatcher.d.ts +1 -1
- package/dist/clean-arch/application/logger/logger.spec.d.ts +1 -0
- package/dist/clean-arch/application/logger/logger.spec.js +204 -0
- package/dist/clean-arch/domain/entities/entity.spec.d.ts +1 -0
- package/dist/clean-arch/domain/entities/entity.spec.js +130 -0
- package/dist/clean-arch/domain/events/consistency-event/consistency-event.spec.d.ts +1 -0
- package/dist/clean-arch/domain/events/consistency-event/consistency-event.spec.js +91 -0
- package/dist/clean-arch/domain/events/consistency-event/events-enum.d.ts +16 -0
- package/dist/clean-arch/domain/events/consistency-event/events-enum.js +16 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/authorization/authorization-created.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/authorization/authorization-created.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/authorization/authorization-entity-event.d.ts +11 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/authorization/authorization-entity-event.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/authorization/authorization-updated.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/authorization/authorization-updated.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/external-provider/external-provider-created.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/external-provider/external-provider-created.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/external-provider/external-provider-entity-event.d.ts +14 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/external-provider/external-provider-entity-event.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/role/role-created.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/role/role-created.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/role/role-entity-event.d.ts +13 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/role/role-entity-event.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/role/role-updated.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/role/role-updated.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant/tenant-created.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant/tenant-created.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant/tenant-entity-event.d.ts +14 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant/tenant-entity-event.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant/tenant-updated.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant/tenant-updated.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant-role/tenant-role-entity-event.d.ts +8 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant-role/tenant-role-entity-event.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant-role/tenant-role-updated.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant-role/tenant-role-updated.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant-user/tenant-user-entity-event.d.ts +10 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant-user/tenant-user-entity-event.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant-user/tenant-user-updated.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/tenant-user/tenant-user-updated.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/user/user-forgot-password.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/user/user-forgot-password.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/user-tenant-role/user-tenant-role-entity-event.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/user-tenant-role/user-tenant-role-entity-event.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/user-tenant-role/user-tenant-roles-created.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/user-tenant-role/user-tenant-roles-created.js +2 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/user-tenant-role/user-tenant-roles-updated.d.ts +9 -0
- package/dist/clean-arch/domain/events/consistency-event/types/portal-admin/user-tenant-role/user-tenant-roles-updated.js +2 -0
- package/dist/clean-arch/domain/value-objects/password.spec.d.ts +1 -0
- package/dist/clean-arch/domain/value-objects/password.spec.js +90 -0
- package/dist/clean-arch/infra/adapters/http-adapters.spec.d.ts +1 -0
- package/dist/clean-arch/infra/adapters/http-adapters.spec.js +318 -0
- package/dist/clean-arch/infra/authorizations/authorizer.spec.js +43 -0
- package/dist/clean-arch/infra/cache/cache-clients.spec.d.ts +1 -0
- package/dist/clean-arch/infra/cache/cache-clients.spec.js +161 -0
- package/dist/clean-arch/infra/cache/clients/node-cache.js +3 -2
- package/dist/clean-arch/infra/consistency-event-dispatcher/exchanges-enum.d.ts +16 -0
- package/dist/clean-arch/infra/consistency-event-dispatcher/exchanges-enum.js +18 -0
- package/dist/clean-arch/infra/http/controller.spec.js +170 -51
- package/dist/clean-arch/infra/http/helpers.spec.d.ts +1 -0
- package/dist/clean-arch/infra/http/helpers.spec.js +136 -0
- package/dist/clean-arch/infra/http/net.spec.d.ts +1 -0
- package/dist/clean-arch/infra/http/net.spec.js +46 -0
- package/dist/clean-arch/infra/logger/logger-orbit.d.ts +2 -0
- package/dist/clean-arch/infra/logger/logger-orbit.spec.d.ts +1 -0
- package/dist/clean-arch/infra/logger/logger-orbit.spec.js +122 -0
- package/dist/clean-arch/infra/orbit-http-client/orbit-axios-client/orbit-axios-client.spec.d.ts +1 -0
- package/dist/clean-arch/infra/orbit-http-client/orbit-axios-client/orbit-axios-client.spec.js +143 -0
- package/dist/clean-arch/infra/orbit-notification-client/discord-client.spec.d.ts +1 -0
- package/dist/clean-arch/infra/orbit-notification-client/discord-client.spec.js +58 -0
- package/dist/clean-arch/infra/queue/queue-utils.spec.d.ts +1 -0
- package/dist/clean-arch/infra/queue/queue-utils.spec.js +170 -0
- package/dist/clean-arch/infra/queue/rabbitmq/amqp-lib.js +4 -4
- package/dist/clean-arch/infra/queue/rabbitmq/amqp-lib.spec.js +466 -89
- package/dist/clean-arch/infra/queue/rabbitmq/types.d.ts +1 -1
- package/dist/clean-arch/infra/scaledjob/amqp/runner.d.ts +0 -3
- package/dist/clean-arch/infra/scaledjob/amqp/runner.js +5 -6
- package/dist/clean-arch/infra/scaledjob/amqp/runner.spec.js +74 -84
- package/dist/clean-arch/infra/scaledjob/sqs/runner.spec.d.ts +1 -0
- package/dist/clean-arch/infra/scaledjob/sqs/runner.spec.js +150 -0
- package/dist/clean-arch/infra/tracing/start-tracing.d.ts +10 -0
- package/dist/clean-arch/infra/tracing/start-tracing.js +78 -0
- package/dist/clean-arch/infra/validations/zod/validation-zod.spec.js +23 -0
- package/dist/clean-arch/shared/pagination/get-take-and-skip.spec.d.ts +1 -0
- package/dist/clean-arch/shared/pagination/get-take-and-skip.spec.js +16 -0
- package/dist/coverage/block-navigation.d.ts +1 -0
- package/dist/coverage/block-navigation.js +72 -0
- package/dist/coverage/prettify.d.ts +0 -0
- package/dist/coverage/prettify.js +1009 -0
- package/dist/coverage/sorter.d.ts +1 -0
- package/dist/coverage/sorter.js +178 -0
- package/dist/frameworks/express/api-gateway/mapping-model.spec.js +58 -0
- package/dist/frameworks/express/authorizations/authorization-express.spec.js +9 -0
- package/dist/frameworks/express/middleware-express.spec.d.ts +1 -0
- package/dist/frameworks/express/middleware-express.spec.js +51 -0
- package/dist/frameworks/nest/nest.spec.d.ts +1 -0
- package/dist/frameworks/nest/nest.spec.js +122 -0
- package/dist/infra/http/api-gateway/mapping-model.spec.js +42 -0
- package/package.json +5 -1
|
@@ -1,60 +1,179 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
|
+
const api_error_1 = require("../../../infra/http/errors/api-error");
|
|
5
|
+
const application_error_1 = tslib_1.__importDefault(require("../../domain/errors/application-error"));
|
|
4
6
|
const conflict_error_1 = tslib_1.__importDefault(require("../../domain/errors/conflict-error"));
|
|
7
|
+
const domain_error_1 = tslib_1.__importDefault(require("../../domain/errors/domain-error"));
|
|
8
|
+
const validation_error_1 = tslib_1.__importDefault(require("../../domain/errors/validation-error"));
|
|
9
|
+
const not_allowed_1 = tslib_1.__importDefault(require("../authorizations/not-allowed"));
|
|
10
|
+
const infra_error_1 = tslib_1.__importDefault(require("../errors/infra-error"));
|
|
5
11
|
const controller_1 = tslib_1.__importDefault(require("./controller"));
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
try {
|
|
10
|
-
throw new conflict_error_1.default({ id: 'ID' });
|
|
11
|
-
}
|
|
12
|
-
catch (err) {
|
|
13
|
-
return this.throw(err);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
12
|
+
class TestController extends controller_1.default {
|
|
13
|
+
async handle() {
|
|
14
|
+
return this.success({ ok: true });
|
|
16
15
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
code: 409,
|
|
23
|
-
message: 'Entity already exists.',
|
|
24
|
-
items: [
|
|
25
|
-
{
|
|
26
|
-
id: 'ID',
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
},
|
|
30
|
-
}));
|
|
31
|
-
});
|
|
32
|
-
test('conflict error with array of objects', async () => {
|
|
33
|
-
class ControllerError extends controller_1.default {
|
|
34
|
-
async handle() {
|
|
35
|
-
try {
|
|
36
|
-
throw new conflict_error_1.default([{ id: 'ID 1' }, { id: 'ID 2' }]);
|
|
37
|
-
}
|
|
38
|
-
catch (err) {
|
|
39
|
-
return this.throw(err);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
16
|
+
successResponse(dto) {
|
|
17
|
+
return this.success(dto);
|
|
18
|
+
}
|
|
19
|
+
noContentResponse() {
|
|
20
|
+
return this.noContent();
|
|
42
21
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
22
|
+
createdResponse(dto) {
|
|
23
|
+
return this.created(dto);
|
|
24
|
+
}
|
|
25
|
+
paginatedResponse(dto) {
|
|
26
|
+
return this.paginated(dto);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
describe('Controller', () => {
|
|
30
|
+
it('should build success responses', () => {
|
|
31
|
+
const controller = new TestController();
|
|
32
|
+
expect(controller.successResponse({ id: '1' })).toEqual({
|
|
33
|
+
code: 200,
|
|
34
|
+
data: { data: { id: '1' } },
|
|
35
|
+
});
|
|
36
|
+
expect(controller.noContentResponse()).toEqual({
|
|
37
|
+
code: 204,
|
|
38
|
+
data: undefined,
|
|
39
|
+
});
|
|
40
|
+
expect(controller.createdResponse({ id: '1' })).toEqual({
|
|
41
|
+
code: 201,
|
|
42
|
+
data: { data: { id: '1' } },
|
|
43
|
+
});
|
|
44
|
+
expect(controller.createdResponse()).toEqual({
|
|
45
|
+
code: 201,
|
|
46
|
+
data: undefined,
|
|
47
|
+
});
|
|
48
|
+
expect(controller.paginatedResponse({ items: [1, 2] })).toEqual({
|
|
49
|
+
code: 200,
|
|
50
|
+
data: { items: [1, 2] },
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
it('should return conflict error response with one object', () => {
|
|
54
|
+
const controller = new TestController();
|
|
55
|
+
expect(controller.throw(new conflict_error_1.default({ id: 'ID' }))).toEqual({
|
|
56
|
+
code: 409,
|
|
57
|
+
data: {
|
|
58
|
+
code: 409,
|
|
59
|
+
message: 'Entity already exists.',
|
|
60
|
+
items: [{ id: 'ID' }],
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
it('should return conflict error response with array of objects', () => {
|
|
65
|
+
const controller = new TestController();
|
|
66
|
+
expect(controller.throw(new conflict_error_1.default([{ id: 'ID 1' }, { id: 'ID 2' }]))).toEqual({
|
|
48
67
|
code: 409,
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
68
|
+
data: {
|
|
69
|
+
code: 409,
|
|
70
|
+
message: 'Entity already exists.',
|
|
71
|
+
items: [{ id: 'ID 1' }, { id: 'ID 2' }],
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
it.each([
|
|
76
|
+
[new domain_error_1.default('domain failed', 422), 422, 'domain failed'],
|
|
77
|
+
[
|
|
78
|
+
new application_error_1.default('application failed', 401),
|
|
79
|
+
401,
|
|
80
|
+
'application failed',
|
|
81
|
+
],
|
|
82
|
+
[new infra_error_1.default('infra failed', 503), 503, 'infra failed'],
|
|
83
|
+
])('should map known error types with code and message', (error, code, message) => {
|
|
84
|
+
const controller = new TestController();
|
|
85
|
+
expect(controller.throw(error)).toEqual({
|
|
86
|
+
code,
|
|
87
|
+
data: {
|
|
88
|
+
code,
|
|
89
|
+
message,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
it('should map api errors', () => {
|
|
94
|
+
const controller = new TestController();
|
|
95
|
+
const error = new api_error_1.ApiError({
|
|
96
|
+
status: 401,
|
|
97
|
+
body: {
|
|
98
|
+
code: 401,
|
|
99
|
+
message: 'Unauthorized',
|
|
100
|
+
timestamp: new Date('2024-01-01T00:00:00.000Z'),
|
|
101
|
+
errors: [
|
|
102
|
+
{
|
|
103
|
+
code_error: '401',
|
|
104
|
+
location: 'authorization',
|
|
105
|
+
msg: 'Unauthorized',
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
},
|
|
109
|
+
headers: {},
|
|
110
|
+
});
|
|
111
|
+
expect(controller.throw(error)).toEqual({
|
|
112
|
+
code: 401,
|
|
113
|
+
data: error.error,
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
it('should map validation errors', () => {
|
|
117
|
+
const controller = new TestController();
|
|
118
|
+
const error = new validation_error_1.default([
|
|
119
|
+
{
|
|
120
|
+
property: 'email',
|
|
121
|
+
message: 'Required field',
|
|
122
|
+
},
|
|
123
|
+
]);
|
|
124
|
+
expect(controller.throw(error)).toEqual({
|
|
125
|
+
code: 400,
|
|
126
|
+
data: {
|
|
127
|
+
code: 400,
|
|
128
|
+
message: 'Validation Error',
|
|
129
|
+
errors: [
|
|
130
|
+
{
|
|
131
|
+
property: 'email',
|
|
132
|
+
message: 'Required field',
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
it('should map not allowed errors', () => {
|
|
139
|
+
const controller = new TestController();
|
|
140
|
+
const error = new not_allowed_1.default({
|
|
141
|
+
status: 403,
|
|
142
|
+
body: {
|
|
143
|
+
code: 403,
|
|
144
|
+
message: 'Forbidden',
|
|
145
|
+
timestamp: new Date('2024-02-02T00:00:00.000Z'),
|
|
146
|
+
errors: [
|
|
147
|
+
{
|
|
148
|
+
code_error: '403',
|
|
149
|
+
location: 'authorization',
|
|
150
|
+
msg: 'Forbidden',
|
|
151
|
+
},
|
|
152
|
+
],
|
|
153
|
+
},
|
|
154
|
+
headers: {
|
|
155
|
+
'content-type': 'application/json',
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
expect(controller.throw(error)).toEqual({
|
|
159
|
+
code: 403,
|
|
160
|
+
data: error.error,
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
it('should map unexpected errors to internal server error', () => {
|
|
164
|
+
const controller = new TestController();
|
|
165
|
+
const unexpectedError = new Error('boom');
|
|
166
|
+
const consoleError = vi
|
|
167
|
+
.spyOn(console, 'error')
|
|
168
|
+
.mockImplementation(() => undefined);
|
|
169
|
+
expect(controller.throw(unexpectedError)).toEqual({
|
|
170
|
+
code: 500,
|
|
171
|
+
data: {
|
|
172
|
+
code: 500,
|
|
173
|
+
message: 'Server failed. Contact the administrator!',
|
|
174
|
+
reason: unexpectedError,
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
expect(consoleError).toHaveBeenCalledWith(unexpectedError);
|
|
178
|
+
});
|
|
60
179
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const health_connections_1 = tslib_1.__importDefault(require("./health-connections"));
|
|
5
|
+
const handle_authorizer_headers_1 = require("./handle-authorizer-headers");
|
|
6
|
+
const handle_bool_filter_1 = tslib_1.__importDefault(require("./handle-bool-filter"));
|
|
7
|
+
const handle_is_active_query_1 = tslib_1.__importDefault(require("./handle-is-active-query"));
|
|
8
|
+
const handle_sort_param_query_1 = require("./handle-sort-param-query");
|
|
9
|
+
const handle_user_headers_1 = require("./handle-user-headers");
|
|
10
|
+
const filter_1 = require("../../../infra/http/compression/filter");
|
|
11
|
+
describe('HTTP helpers', () => {
|
|
12
|
+
it('should convert boolean filters', () => {
|
|
13
|
+
expect((0, handle_bool_filter_1.default)()).toBeUndefined();
|
|
14
|
+
expect((0, handle_bool_filter_1.default)('true')).toBe(true);
|
|
15
|
+
expect((0, handle_bool_filter_1.default)('false')).toBe(false);
|
|
16
|
+
expect((0, handle_is_active_query_1.default)()).toBeUndefined();
|
|
17
|
+
expect((0, handle_is_active_query_1.default)('true')).toBe(true);
|
|
18
|
+
expect((0, handle_is_active_query_1.default)('false')).toBe(false);
|
|
19
|
+
});
|
|
20
|
+
it('should map sort params to entity props', () => {
|
|
21
|
+
expect((0, handle_sort_param_query_1.handleSortParamQuery)('created_at')).toBe('createdAt');
|
|
22
|
+
expect((0, handle_sort_param_query_1.handleSortParamQuery)('updated_at')).toBe('updatedAt');
|
|
23
|
+
expect((0, handle_sort_param_query_1.handleSortParamQuery)('is_main')).toBe('isMain');
|
|
24
|
+
expect((0, handle_sort_param_query_1.handleSortParamQuery)('other')).toBe('createdAt');
|
|
25
|
+
});
|
|
26
|
+
it('should normalize authorizer headers and legacy headers', () => {
|
|
27
|
+
expect((0, handle_authorizer_headers_1.handleAuthorizerHeaders)({
|
|
28
|
+
username: 'John',
|
|
29
|
+
useremail: 'john@example.com',
|
|
30
|
+
userid: 'user-id',
|
|
31
|
+
['app-id']: 'app-id',
|
|
32
|
+
['app-name']: 'Portal',
|
|
33
|
+
tenantid: 'tenant-id',
|
|
34
|
+
['x-trace-id']: 'trace-id',
|
|
35
|
+
})).toEqual({
|
|
36
|
+
userName: 'John',
|
|
37
|
+
userEmail: 'john@example.com',
|
|
38
|
+
appName: 'Portal',
|
|
39
|
+
entityId: 'user-id',
|
|
40
|
+
tenantId: 'tenant-id',
|
|
41
|
+
traceId: 'trace-id',
|
|
42
|
+
});
|
|
43
|
+
expect((0, handle_authorizer_headers_1.handleAuthorizerHeaders)({
|
|
44
|
+
username: 'John',
|
|
45
|
+
useremail: 'john@example.com',
|
|
46
|
+
['app-id']: 'app-id',
|
|
47
|
+
tenantid: 'tenant-id',
|
|
48
|
+
['x-trace-id']: 'trace-id',
|
|
49
|
+
})).toEqual({
|
|
50
|
+
userName: 'John',
|
|
51
|
+
userEmail: 'john@example.com',
|
|
52
|
+
appName: undefined,
|
|
53
|
+
entityId: 'app-id',
|
|
54
|
+
tenantId: 'tenant-id',
|
|
55
|
+
traceId: 'trace-id',
|
|
56
|
+
});
|
|
57
|
+
expect((0, handle_authorizer_headers_1.handleAuthorizerHeaders)({
|
|
58
|
+
username: 'John',
|
|
59
|
+
useremail: 'john@example.com',
|
|
60
|
+
tenantid: 'tenant-id',
|
|
61
|
+
['x-trace-id']: 'trace-id',
|
|
62
|
+
})).toEqual({
|
|
63
|
+
userName: 'John',
|
|
64
|
+
userEmail: 'john@example.com',
|
|
65
|
+
appName: undefined,
|
|
66
|
+
entityId: 'john@example.com',
|
|
67
|
+
tenantId: 'tenant-id',
|
|
68
|
+
traceId: 'trace-id',
|
|
69
|
+
});
|
|
70
|
+
expect((0, handle_authorizer_headers_1.authorizerToLegacyDefaultHeaders)({
|
|
71
|
+
userName: 'John',
|
|
72
|
+
userEmail: 'john@example.com',
|
|
73
|
+
appName: 'Portal',
|
|
74
|
+
tenantId: 'tenant-id',
|
|
75
|
+
entityId: 'user-id',
|
|
76
|
+
traceId: 'trace-id',
|
|
77
|
+
})).toEqual({
|
|
78
|
+
createdByEmail: 'john@example.com',
|
|
79
|
+
createdByName: 'Portal',
|
|
80
|
+
updatedByEmail: 'john@example.com',
|
|
81
|
+
updatedByName: 'Portal',
|
|
82
|
+
});
|
|
83
|
+
expect((0, handle_authorizer_headers_1.authorizerToLegacyDefaultHeaders)({
|
|
84
|
+
userName: 'John',
|
|
85
|
+
userEmail: 'john@example.com',
|
|
86
|
+
tenantId: 'tenant-id',
|
|
87
|
+
entityId: 'user-id',
|
|
88
|
+
})).toEqual({
|
|
89
|
+
createdByEmail: 'john@example.com',
|
|
90
|
+
createdByName: 'John',
|
|
91
|
+
updatedByEmail: 'john@example.com',
|
|
92
|
+
updatedByName: 'John',
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
it('should normalize user headers', () => {
|
|
96
|
+
expect((0, handle_user_headers_1.handleUserHeaders)({
|
|
97
|
+
username: 'John',
|
|
98
|
+
useremail: 'john@example.com',
|
|
99
|
+
userid: 'user-id',
|
|
100
|
+
['app-id']: 'app-id',
|
|
101
|
+
tenantid: 'tenant-id',
|
|
102
|
+
})).toEqual({
|
|
103
|
+
createdByName: 'John',
|
|
104
|
+
createdByEmail: 'john@example.com',
|
|
105
|
+
updatedByEmail: 'john@example.com',
|
|
106
|
+
updatedByName: 'John',
|
|
107
|
+
entityId: 'user-id',
|
|
108
|
+
tenantId: 'tenant-id',
|
|
109
|
+
});
|
|
110
|
+
expect((0, handle_user_headers_1.handleUserHeaders)({
|
|
111
|
+
username: 'John',
|
|
112
|
+
useremail: 'john@example.com',
|
|
113
|
+
['app-id']: 'app-id',
|
|
114
|
+
tenantid: 'tenant-id',
|
|
115
|
+
})).toEqual({
|
|
116
|
+
createdByName: 'John',
|
|
117
|
+
createdByEmail: 'john@example.com',
|
|
118
|
+
updatedByEmail: 'john@example.com',
|
|
119
|
+
updatedByName: 'John',
|
|
120
|
+
entityId: 'app-id',
|
|
121
|
+
tenantId: 'tenant-id',
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
it('should delegate the health check to the database dependency', async () => {
|
|
125
|
+
const checkDB = {
|
|
126
|
+
dbIsOnline: vi.fn().mockResolvedValue(true),
|
|
127
|
+
};
|
|
128
|
+
const health = new health_connections_1.default(checkDB);
|
|
129
|
+
await expect(health.check()).resolves.toEqual({ dbIsOnline: true });
|
|
130
|
+
expect(checkDB.dbIsOnline).toHaveBeenCalledTimes(1);
|
|
131
|
+
});
|
|
132
|
+
it('should define if the payload must be compressed', () => {
|
|
133
|
+
expect((0, filter_1.isToCompress)({ 'accept-encoding': 'identity' })).toBe(true);
|
|
134
|
+
expect((0, filter_1.isToCompress)({ 'accept-encoding': 'gzip' })).toBe(false);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const vitest_1 = require("vitest");
|
|
5
|
+
vitest_1.vi.mock('axios', () => ({
|
|
6
|
+
default: {
|
|
7
|
+
get: vitest_1.vi.fn(),
|
|
8
|
+
},
|
|
9
|
+
}));
|
|
10
|
+
const axios_1 = tslib_1.__importDefault(require("axios"));
|
|
11
|
+
const net_1 = require("./net");
|
|
12
|
+
describe('Net', () => {
|
|
13
|
+
it('should return the current IP when the request succeeds', async () => {
|
|
14
|
+
vitest_1.vi.mocked(axios_1.default.get).mockResolvedValue({
|
|
15
|
+
data: '127.0.0.1\n',
|
|
16
|
+
});
|
|
17
|
+
const net = new net_1.Net();
|
|
18
|
+
await expect(net.getCurrentIP()).resolves.toBe('127.0.0.1');
|
|
19
|
+
expect(axios_1.default.get).toHaveBeenCalledWith('https://checkip.amazonaws.com/', {
|
|
20
|
+
timeout: 1000,
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
it('should return invalid when the request fails', async () => {
|
|
24
|
+
const consoleLog = vitest_1.vi
|
|
25
|
+
.spyOn(console, 'log')
|
|
26
|
+
.mockImplementation(() => undefined);
|
|
27
|
+
vitest_1.vi.mocked(axios_1.default.get).mockRejectedValue(new Error('fail'));
|
|
28
|
+
const net = new net_1.Net();
|
|
29
|
+
await expect(net.getCurrentIP()).resolves.toBe('invalid');
|
|
30
|
+
expect(consoleLog).toHaveBeenCalledWith(expect.any(Error));
|
|
31
|
+
});
|
|
32
|
+
it('should validate if the current IP is in the whitelist', async () => {
|
|
33
|
+
const net = new net_1.Net();
|
|
34
|
+
vitest_1.vi.spyOn(net, 'getCurrentIP').mockResolvedValue('127.0.0.1');
|
|
35
|
+
await expect(net.isCurrentIPInIPWhiteList(['127.0.0.1', '10.0.0.1'])).resolves.toBe(true);
|
|
36
|
+
await expect(net.isCurrentIPInIPWhiteList(['10.0.0.1', '10.0.0.2'])).resolves.toBe(false);
|
|
37
|
+
});
|
|
38
|
+
it('should ignore invalid IPs when configured', async () => {
|
|
39
|
+
const net = new net_1.Net();
|
|
40
|
+
vitest_1.vi.spyOn(net, 'getCurrentIP').mockResolvedValue('invalid');
|
|
41
|
+
await expect(net.isCurrentIPInIPWhiteList(['10.0.0.1'], true)).resolves.toBe(true);
|
|
42
|
+
await expect(net.isCurrentIPInIPWhiteList(['10.0.0.1'], false)).resolves.toBe(false);
|
|
43
|
+
expect(net.isIP('127.0.0.1')).toBe(4);
|
|
44
|
+
expect(net.isIP('invalid')).toBe(0);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const vitest_1 = require("vitest");
|
|
5
|
+
const logger_1 = require("../../application/logger");
|
|
6
|
+
const log_error_1 = tslib_1.__importDefault(require("../../domain/errors/log-error"));
|
|
7
|
+
const types_1 = require("../environment/types");
|
|
8
|
+
const logger_orbit_1 = tslib_1.__importStar(require("./logger-orbit"));
|
|
9
|
+
const createQueue = () => ({
|
|
10
|
+
on: vitest_1.vi.fn().mockResolvedValue(undefined),
|
|
11
|
+
publish: vitest_1.vi.fn().mockResolvedValue(undefined),
|
|
12
|
+
});
|
|
13
|
+
(0, vitest_1.describe)('LoggerOrbit', () => {
|
|
14
|
+
(0, vitest_1.afterEach)(() => {
|
|
15
|
+
vitest_1.vi.useRealTimers();
|
|
16
|
+
vitest_1.vi.restoreAllMocks();
|
|
17
|
+
logger_orbit_1.AuditOrbitLoggerGateway.instance = undefined;
|
|
18
|
+
});
|
|
19
|
+
(0, vitest_1.it)('should publish business logs even when the cached level would block them', async () => {
|
|
20
|
+
const queue = createQueue();
|
|
21
|
+
const cache = {
|
|
22
|
+
get: vitest_1.vi.fn().mockResolvedValue(types_1.LOG_LEVEL.error),
|
|
23
|
+
getKeys: vitest_1.vi.fn().mockResolvedValue(['LOG_LEVEL:*:*']),
|
|
24
|
+
set: vitest_1.vi.fn(),
|
|
25
|
+
};
|
|
26
|
+
const logger = new logger_orbit_1.LoggerOrbit(queue, {
|
|
27
|
+
cache: cache,
|
|
28
|
+
params: {
|
|
29
|
+
actor: 'system',
|
|
30
|
+
type: 'log',
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
await logger.log({
|
|
34
|
+
level: types_1.LOG_LEVEL.info,
|
|
35
|
+
category: logger_1.CategoryEnum.BUSINESS,
|
|
36
|
+
actorType: logger_1.ActorEnum.SYSTEM,
|
|
37
|
+
expirationHours: 1,
|
|
38
|
+
}, { id: 'business-log' });
|
|
39
|
+
await logger.log({
|
|
40
|
+
level: types_1.LOG_LEVEL.info,
|
|
41
|
+
category: logger_1.CategoryEnum.TECHNICAL,
|
|
42
|
+
actorType: logger_1.ActorEnum.SYSTEM,
|
|
43
|
+
expirationHours: 1,
|
|
44
|
+
}, { id: 'technical-log' });
|
|
45
|
+
(0, vitest_1.expect)(queue.publish).toHaveBeenCalledTimes(1);
|
|
46
|
+
(0, vitest_1.expect)(queue.publish).toHaveBeenCalledWith('log-service.direct', vitest_1.expect.any(logger_orbit_1.default));
|
|
47
|
+
});
|
|
48
|
+
(0, vitest_1.it)('should build default props and publish payloads with expiration dates', async () => {
|
|
49
|
+
vitest_1.vi.useFakeTimers();
|
|
50
|
+
vitest_1.vi.setSystemTime(new Date('2024-01-01T00:00:00.000Z'));
|
|
51
|
+
const queue = createQueue();
|
|
52
|
+
const logger = new logger_orbit_1.LoggerOrbit(queue, {
|
|
53
|
+
params: {
|
|
54
|
+
actor: 'system',
|
|
55
|
+
type: 'log',
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
const props = logger['buildDefaultProps']({
|
|
59
|
+
actor: 'system',
|
|
60
|
+
type: 'log',
|
|
61
|
+
});
|
|
62
|
+
await logger.register(props, { id: '1' });
|
|
63
|
+
const publishedEvent = queue.publish.mock.calls[0][1];
|
|
64
|
+
(0, vitest_1.expect)(props).toEqual(vitest_1.expect.objectContaining({
|
|
65
|
+
actor: 'system',
|
|
66
|
+
type: 'log',
|
|
67
|
+
level: types_1.LOG_LEVEL.info,
|
|
68
|
+
actorType: logger_1.ActorEnum.SYSTEM,
|
|
69
|
+
category: logger_1.CategoryEnum.TECHNICAL,
|
|
70
|
+
expirationHours: 672,
|
|
71
|
+
}));
|
|
72
|
+
(0, vitest_1.expect)(publishedEvent.name).toBe('log-service.log.created');
|
|
73
|
+
(0, vitest_1.expect)(publishedEvent.eventDate).toEqual(new Date('2024-01-01T00:00:00.000Z'));
|
|
74
|
+
(0, vitest_1.expect)(publishedEvent.input).toEqual(vitest_1.expect.objectContaining({
|
|
75
|
+
actor: 'system',
|
|
76
|
+
type: 'log',
|
|
77
|
+
level: types_1.LOG_LEVEL.info,
|
|
78
|
+
actorType: logger_1.ActorEnum.SYSTEM,
|
|
79
|
+
category: logger_1.CategoryEnum.TECHNICAL,
|
|
80
|
+
details: { id: '1' },
|
|
81
|
+
expirationDate: new Date('2024-01-29T00:00:00.000Z'),
|
|
82
|
+
}));
|
|
83
|
+
});
|
|
84
|
+
(0, vitest_1.it)('should execute retry strategies and throw LogError when configured', async () => {
|
|
85
|
+
const queue = createQueue();
|
|
86
|
+
const retryStrategy = vitest_1.vi.fn().mockResolvedValue(undefined);
|
|
87
|
+
const logger = new logger_orbit_1.LoggerOrbit(queue, {
|
|
88
|
+
options: {
|
|
89
|
+
retryStrategy,
|
|
90
|
+
},
|
|
91
|
+
params: {
|
|
92
|
+
actor: 'system',
|
|
93
|
+
type: 'log',
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
queue.publish.mockRejectedValueOnce(new Error('publish failed'));
|
|
97
|
+
await logger.register(logger['buildDefaultProps']({
|
|
98
|
+
actor: 'system',
|
|
99
|
+
type: 'log',
|
|
100
|
+
}), { id: '2' });
|
|
101
|
+
(0, vitest_1.expect)(retryStrategy).toHaveBeenCalledWith({ id: '2' }, vitest_1.expect.any(Object));
|
|
102
|
+
const failingLogger = new logger_orbit_1.LoggerOrbit(queue, {
|
|
103
|
+
options: {
|
|
104
|
+
throwOnError: true,
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
queue.publish.mockRejectedValueOnce(new Error('publish failed'));
|
|
108
|
+
await (0, vitest_1.expect)(failingLogger.register(failingLogger['buildDefaultProps'](), {
|
|
109
|
+
id: '3',
|
|
110
|
+
})).rejects.toThrow(log_error_1.default);
|
|
111
|
+
});
|
|
112
|
+
(0, vitest_1.it)('should reuse the singleton and apply default Orbit logger options', () => {
|
|
113
|
+
const queue = createQueue();
|
|
114
|
+
const first = logger_orbit_1.AuditOrbitLoggerGateway.getInstance(queue);
|
|
115
|
+
const second = logger_orbit_1.AuditOrbitLoggerGateway.getInstance(queue);
|
|
116
|
+
(0, vitest_1.expect)(first).toBe(second);
|
|
117
|
+
(0, vitest_1.expect)(first.options).toEqual({
|
|
118
|
+
throwOnError: false,
|
|
119
|
+
lastArgIsParams: true,
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
});
|
package/dist/clean-arch/infra/orbit-http-client/orbit-axios-client/orbit-axios-client.spec.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|