@vida-global/core 1.1.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 +9 -0
- package/index.js +17 -0
- package/lib/active_record/README.md +205 -0
- package/lib/active_record/baseRecord.js +112 -0
- package/lib/active_record/db/connection.js +128 -0
- package/lib/active_record/db/connectionConfiguration.js +114 -0
- package/lib/active_record/db/importSchema.js +4 -0
- package/lib/active_record/db/migration.js +132 -0
- package/lib/active_record/db/migrationTemplate.js +8 -0
- package/lib/active_record/db/migrationVersion.js +68 -0
- package/lib/active_record/db/migrator.js +169 -0
- package/lib/active_record/db/queryInterface.js +47 -0
- package/lib/active_record/db/schema.js +113 -0
- package/lib/active_record/index.js +6 -0
- package/lib/active_record/utils.js +43 -0
- package/lib/http/README.md +32 -0
- package/lib/http/client.js +129 -0
- package/lib/http/error.js +34 -0
- package/lib/logger/README.md +2 -0
- package/lib/logger/index.js +16 -0
- package/lib/release/develop.js +27 -0
- package/lib/release/git.js +86 -0
- package/lib/release/increment.js +56 -0
- package/lib/release/index.js +10 -0
- package/lib/release/release.js +30 -0
- package/lib/release/utils.js +44 -0
- package/lib/server/README.md +37 -0
- package/lib/server/index.js +9 -0
- package/lib/server/server.js +359 -0
- package/lib/server/serverController.js +344 -0
- package/lib/server/systemController.js +23 -0
- package/package.json +37 -0
- package/scripts/active_record/migrate.js +30 -0
- package/scripts/release.js +62 -0
- package/test/active_record/baseRecord.test.js +179 -0
- package/test/active_record/db/connection.test.js +221 -0
- package/test/active_record/db/connectionConfiguration.test.js +184 -0
- package/test/active_record/db/migrator.test.js +266 -0
- package/test/active_record/db/queryInterface.test.js +66 -0
- package/test/http/client.test.js +271 -0
- package/test/http/error.test.js +71 -0
- package/test/release/develop.test.js +57 -0
- package/test/release/git.test.js +189 -0
- package/test/release/increment.test.js +145 -0
- package/test/release/release.test.js +72 -0
- package/test/release/utils.test.js +148 -0
- package/test/server/helpers/controllers/barController.js +9 -0
- package/test/server/helpers/controllers/fooController.js +48 -0
- package/test/server/helpers/controllers/sub/bazController.js +10 -0
- package/test/server/helpers/server.js +14 -0
- package/test/server/server.test.js +188 -0
- package/test/server/serverController.test.js +251 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
const { ValidationError,
|
|
2
|
+
VidaServerController } = require('../../lib/server');
|
|
3
|
+
const { BarController } = require('./helpers/controllers/barController');
|
|
4
|
+
const { FooController } = require('./helpers/controllers/fooController');
|
|
5
|
+
const { camelize } = require('inflection');
|
|
6
|
+
const TestHelpers = require('@vida-global/test-helpers');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
describe('VidaServerController', () => {
|
|
10
|
+
describe('VidaServerController.actionNames', () => {
|
|
11
|
+
it ('returns actions based on method names', () => {
|
|
12
|
+
const names = FooController.actionNames;
|
|
13
|
+
const expected = ['postIndex',
|
|
14
|
+
'getFooBar',
|
|
15
|
+
'getBazBan',
|
|
16
|
+
'postFooBar',
|
|
17
|
+
'putFooBar',
|
|
18
|
+
'deleteFooBar',
|
|
19
|
+
'patchFooBar'];
|
|
20
|
+
expect(names).toEqual(expected);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
describe('VidaServerController._constructAction', () => {
|
|
26
|
+
it ('returns an object with path, method, and action', () => {
|
|
27
|
+
const expected = {
|
|
28
|
+
method: 'GET',
|
|
29
|
+
path: '/foo/fooBar',
|
|
30
|
+
action: 'getFooBar'
|
|
31
|
+
}
|
|
32
|
+
expect(FooController._constructAction('getFooBar', '')).toEqual(expected);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it ('uses a blank path for index actions', () => {
|
|
36
|
+
const expected = {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
path: '/foo',
|
|
39
|
+
action: 'postIndex'
|
|
40
|
+
}
|
|
41
|
+
expect(FooController._constructAction('postIndex', '')).toEqual(expected);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it ('uses a custom path when defined', () => {
|
|
45
|
+
const expected = {
|
|
46
|
+
method: 'GET',
|
|
47
|
+
path: '/baz/:id/ban',
|
|
48
|
+
action: 'getBazBan'
|
|
49
|
+
}
|
|
50
|
+
expect(FooController._constructAction('getBazBan', '')).toEqual(expected);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
describe('VidaServerController.validateParameters', () => {
|
|
56
|
+
const paramName1 = TestHelpers.Faker.Text.randomString();
|
|
57
|
+
const paramName2 = TestHelpers.Faker.Text.randomString();
|
|
58
|
+
const value1 = TestHelpers.Faker.Text.randomString();
|
|
59
|
+
const value2 = TestHelpers.Faker.Text.randomString();
|
|
60
|
+
const params = {[paramName1]: value1, [paramName2]: value2};
|
|
61
|
+
const validationType1 = TestHelpers.Faker.Text.randomString();
|
|
62
|
+
const validationType2 = TestHelpers.Faker.Text.randomString();
|
|
63
|
+
const options1 = [TestHelpers.Faker.Text.randomString()];
|
|
64
|
+
const options2 = [TestHelpers.Faker.Text.randomString()];
|
|
65
|
+
const error1 = TestHelpers.Faker.Text.randomString()
|
|
66
|
+
const error2 = TestHelpers.Faker.Text.randomString()
|
|
67
|
+
|
|
68
|
+
let controller;
|
|
69
|
+
let response;
|
|
70
|
+
let validator1;
|
|
71
|
+
let validator2;
|
|
72
|
+
beforeEach(() => {
|
|
73
|
+
response = {json: jest.fn(), statusCode: 200};
|
|
74
|
+
controller = new FooController({params}, response);
|
|
75
|
+
controller.render = jest.fn();
|
|
76
|
+
validator1 = jest.fn();
|
|
77
|
+
validator2 = jest.fn();
|
|
78
|
+
controller[`validate${camelize(validationType1)}`] = validator1;
|
|
79
|
+
controller[`validate${camelize(validationType2)}`] = validator2;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const validations = {
|
|
83
|
+
[paramName1]: {[validationType1]: options1, [validationType2]: options2},
|
|
84
|
+
[paramName2]: {[validationType1]: options2}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
it ('runs validation checkers based on inputs', () => {
|
|
88
|
+
controller.validateParameters(validations);
|
|
89
|
+
expect(validator1).toBeCalledTimes(2);
|
|
90
|
+
expect(validator1).toBeCalledWith(value1, options1);
|
|
91
|
+
expect(validator1).toBeCalledWith(value2, options2);
|
|
92
|
+
|
|
93
|
+
expect(validator2).toBeCalledTimes(1);
|
|
94
|
+
expect(validator2).toBeCalledWith(value1, options2);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it ('throws a `ValidationError` when there\'s an error', async () => {
|
|
98
|
+
validator1.mockImplementation(() => error1);
|
|
99
|
+
await expect(async () => {
|
|
100
|
+
await controller.validateParameters(validations);
|
|
101
|
+
}).rejects.toThrow(ValidationError);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it ('throws a `ValidationError` when there are multiple errors', async () => {
|
|
105
|
+
validator1.mockImplementation(() => error1);
|
|
106
|
+
validator2.mockImplementation(() => error2);
|
|
107
|
+
|
|
108
|
+
await expect(async () => {
|
|
109
|
+
await controller.validateParameters(validations);
|
|
110
|
+
}).rejects.toThrow(ValidationError);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it ('sets nothing when there is no error', () => {
|
|
114
|
+
controller.validateParameters(validations);
|
|
115
|
+
expect(controller.render).toHaveBeenCalledTimes(0);
|
|
116
|
+
expect(response.statusCode).toEqual(200);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
describe('VidaServerController.validatePresence', () => {
|
|
122
|
+
const controller = new FooController({}, {});
|
|
123
|
+
it ('returns undefined when there is a value', () => {
|
|
124
|
+
const result = controller.validatePresence(TestHelpers.Faker.Text.randomString());
|
|
125
|
+
expect(result).toBe(undefined);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it ('returns an error when there is no value', () => {
|
|
129
|
+
const result = controller.validatePresence();
|
|
130
|
+
expect(result).toEqual('required');
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
describe('VidaServerController.validateIsInteger', () => {
|
|
136
|
+
const controller = new FooController({}, {});
|
|
137
|
+
it ('returns undefined when there is an integer', () => {
|
|
138
|
+
const val = Math.ceil(Math.random() * 100);
|
|
139
|
+
const result = controller.validateIsInteger(val)
|
|
140
|
+
expect(result).toEqual(undefined);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it ('returns undefined when there is an integer string', () => {
|
|
144
|
+
const val = Math.ceil(Math.random() * 100).toString();
|
|
145
|
+
const result = controller.validateIsInteger(val)
|
|
146
|
+
expect(result).toEqual(undefined);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it ('returns an error when there is a non integer number', () => {
|
|
150
|
+
const val = Math.random() * 100;
|
|
151
|
+
const result = controller.validateIsInteger(val)
|
|
152
|
+
expect(result).toEqual('must be an integer');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it ('returns an error when there is a non integer string', () => {
|
|
156
|
+
// starts with a number because parseFloat converts a string to a float if it starts with a number
|
|
157
|
+
const val = '1' + TestHelpers.Faker.Text.randomString();
|
|
158
|
+
const result = controller.validateIsInteger(val)
|
|
159
|
+
expect(result).toEqual('must be an integer');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it ('returns an error when the number is less than the gte option', () => {
|
|
163
|
+
const result = controller.validateIsInteger(1, {gte: 2});
|
|
164
|
+
expect(result).toEqual('must be greater than or equal to 2');
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it ('returns undefined when the number is greater than the gte option', () => {
|
|
168
|
+
const result = controller.validateIsInteger(3, {gte: 2});
|
|
169
|
+
expect(result).toEqual(undefined);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it ('returns undefined when the number is equal to the gte option', () => {
|
|
173
|
+
const result = controller.validateIsInteger(2, {gte: 2});
|
|
174
|
+
expect(result).toEqual(undefined);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
describe('VidaServerController.validateIsDateTime', () => {
|
|
180
|
+
const controller = new FooController({}, {});
|
|
181
|
+
it ('returns undefined when passed a string of format YYYY-MM-DD', () => {
|
|
182
|
+
const result = controller.validateIsDateTime('2025-12-17');
|
|
183
|
+
expect(result).toBe(undefined);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it ('returns undefined when passed a string of format MM/DD/YY', () => {
|
|
187
|
+
const result = controller.validateIsDateTime('12/17/25');
|
|
188
|
+
expect(result).toBe(undefined);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it ('returns undefined when passed a string of format Month name DD, YYY', () => {
|
|
192
|
+
const result = controller.validateIsDateTime('December 17, 2025');
|
|
193
|
+
expect(result).toBe(undefined);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it ('returns undefined when passed a string of format YYYY-MM-DD HH:MM:SS', () => {
|
|
197
|
+
const result = controller.validateIsDateTime('2025-12-17 01:05:00');
|
|
198
|
+
expect(result).toBe(undefined);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it ('returns undefined when passed a string of format MM/DD/YY', () => {
|
|
202
|
+
const result = controller.validateIsDateTime('12/17/25 01:05:00');
|
|
203
|
+
expect(result).toBe(undefined);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it ('returns undefined when passed a string of format Month name DD, YYY', () => {
|
|
207
|
+
const result = controller.validateIsDateTime('December 17, 2025 01:05:00');
|
|
208
|
+
expect(result).toBe(undefined);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it ('returns an error when passed an invalid date', () => {
|
|
212
|
+
const result = controller.validateIsDateTime('foo');
|
|
213
|
+
expect(result).toEqual('invalid date');
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
describe('VidaServerController.validateIsEnum', () => {
|
|
219
|
+
const controller = new FooController({}, {});
|
|
220
|
+
const enums = [Math.random(), Math.random(), Math.random()];
|
|
221
|
+
const error = TestHelpers.Faker.Text.randomString();
|
|
222
|
+
const options = { enums, error }
|
|
223
|
+
it ('returns undefined when passed a valid value', () => {
|
|
224
|
+
const result = controller.validateIsEnum(enums[2], options);
|
|
225
|
+
expect(result).toBe(undefined);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it ('returns an error when passed an invalid date', () => {
|
|
229
|
+
const result = controller.validateIsEnum('foo', options);
|
|
230
|
+
expect(result).toBe(error);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
describe('VidaServerController.validateFunction', () => {
|
|
236
|
+
const controller = new FooController({}, {});
|
|
237
|
+
const correctValue = Math.random();
|
|
238
|
+
const error = TestHelpers.Faker.Text.randomString();
|
|
239
|
+
const validator = (value) => value == correctValue ? undefined : error;
|
|
240
|
+
|
|
241
|
+
it ('returns undefiend when passed a valid value', () => {
|
|
242
|
+
const result = controller.validateFunction(correctValue, validator);
|
|
243
|
+
expect(result).toBe(undefined);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it ('returns undefiend when passed a valid value', () => {
|
|
247
|
+
const result = controller.validateFunction(Math.random(), validator);
|
|
248
|
+
expect(result).toBe(error);
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
});
|