@adaptivestone/framework 5.0.0-alpha.10 → 5.0.0-alpha.12

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.
@@ -1,143 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
-
3
- const userEmail = 'testing@test.com';
4
- const userPassword = 'SuperNiceSecret123$';
5
-
6
- let globalUser;
7
-
8
- describe('user model', () => {
9
- it('can create user', async () => {
10
- expect.assertions(1);
11
- globalUser = await global.server.app.getModel('User').create({
12
- email: userEmail,
13
- password: userPassword,
14
- name: {
15
- nick: 'nickname',
16
- },
17
- });
18
- expect(globalUser.id).toBeDefined();
19
- });
20
-
21
- it('passwords should be hashed', async () => {
22
- expect.assertions(1);
23
-
24
- const user = await global.server.app.getModel('User').findOne({
25
- email: userEmail,
26
- });
27
- expect(user.password).not.toBe(userPassword);
28
- });
29
-
30
- it('passwords should not be changed on other fields save', async () => {
31
- expect.assertions(1);
32
- const user = await global.server.app.getModel('User').findOne({
33
- email: userEmail,
34
- });
35
- const psw = user.password;
36
- user.email = 'rrrr';
37
- await user.save();
38
- user.email = userEmail;
39
- await user.save();
40
-
41
- expect(user.password).toBe(psw);
42
- });
43
-
44
- describe('getUserByEmailAndPassword', () => {
45
- it('should WORK with valid creds', async () => {
46
- expect.assertions(1);
47
- const userModel = await global.server.app.getModel('User');
48
- const user = await userModel.getUserByEmailAndPassword(
49
- userEmail,
50
- userPassword,
51
- );
52
- expect(user.id).toBe(globalUser.id);
53
- });
54
-
55
- it('should NOT with INvalid creds', async () => {
56
- expect.assertions(1);
57
- const userModel = await global.server.app.getModel('User');
58
- const user = await userModel.getUserByEmailAndPassword(
59
- userEmail,
60
- 'wrongPassword',
61
- );
62
- expect(user).toBeFalsy();
63
- });
64
-
65
- it('should NOT with wrong email', async () => {
66
- expect.assertions(1);
67
- const userModel = await global.server.app.getModel('User');
68
- const user = await userModel.getUserByEmailAndPassword(
69
- 'not@exists.com',
70
- userPassword,
71
- );
72
- expect(user).toBeFalsy();
73
- });
74
- });
75
-
76
- describe('getUserByToken', () => {
77
- it('should NOT work for non valid token', async () => {
78
- expect.assertions(1);
79
-
80
- const user = await global.server.app
81
- .getModel('User')
82
- .getUserByToken('fake one');
83
- expect(user).toBeFalsy();
84
- });
85
-
86
- it('should work for VALID token', async () => {
87
- expect.assertions(1);
88
- const token = await globalUser.generateToken();
89
- const user = await global.server.app
90
- .getModel('User')
91
- .getUserByToken(token.token);
92
- expect(user.id).toBe(globalUser.id);
93
- });
94
- });
95
-
96
- describe('getUserByVerificationToken', () => {
97
- it('should NOT work for non valid token', async () => {
98
- expect.assertions(1);
99
-
100
- await expect(
101
- global.server.app
102
- .getModel('User')
103
- .getUserByVerificationToken('fake one'),
104
- ).rejects.toStrictEqual(new Error('User not exists'));
105
- });
106
-
107
- it('should work for VALID token', async () => {
108
- expect.assertions(1);
109
- const token = await global.server.app
110
- .getModel('User')
111
- .generateUserVerificationToken(globalUser);
112
-
113
- const user = await global.server.app
114
- .getModel('User')
115
- .getUserByVerificationToken(token.token);
116
- expect(user.id).toBe(globalUser.id);
117
- });
118
- });
119
-
120
- describe('getUserByPasswordRecoveryToken', () => {
121
- it('should NOT work for non valid token', async () => {
122
- expect.assertions(1);
123
-
124
- await expect(
125
- global.server.app
126
- .getModel('User')
127
- .getUserByPasswordRecoveryToken('fake one'),
128
- ).rejects.toStrictEqual(new Error('User not exists'));
129
- });
130
-
131
- it('should work for VALID token', async () => {
132
- expect.assertions(1);
133
- const token = await global.server.app
134
- .getModel('User')
135
- .generateUserPasswordRecoveryToken(globalUser);
136
-
137
- const user = await global.server.app
138
- .getModel('User')
139
- .getUserByPasswordRecoveryToken(token.token);
140
- expect(user.id).toBe(globalUser.id);
141
- });
142
- });
143
- });
@@ -1,18 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import SomeController from '../controllers/test/SomeController.js';
3
- import AbstractController from './AbstractController.js';
4
-
5
- describe('abstract controller methods', () => {
6
- it('can get routes', async () => {
7
- expect.assertions(2);
8
-
9
- const controller = new AbstractController(global.server.app);
10
- const childController = new SomeController(global.server.app);
11
-
12
- const { routes } = controller;
13
- const { routes: childRoutes } = childController;
14
-
15
- expect(routes).toStrictEqual({});
16
- expect(childRoutes).toBeDefined();
17
- });
18
- });
@@ -1,81 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { setTimeout } from 'node:timers/promises';
3
-
4
- describe('cache', () => {
5
- const time = Date.now();
6
-
7
- it('can get set values', async () => {
8
- expect.assertions(2);
9
-
10
- const { cache } = global.server.app;
11
-
12
- const res = await cache.getSetValue('TEST_TIME', () => time);
13
- expect(res).toStrictEqual(time);
14
-
15
- const res2 = await cache.getSetValue('TEST_TIME', () => '123');
16
- expect(res2).toStrictEqual(time);
17
- });
18
-
19
- it('can delete values', async () => {
20
- expect.assertions(1);
21
- const { cache } = global.server.app;
22
-
23
- await cache.removeKey('TEST_TIME');
24
-
25
- const res2 = await cache.getSetValue('TEST_TIME', () => '123');
26
- expect(res2).toBe('123');
27
- });
28
-
29
- it('can works with big int', async () => {
30
- expect.assertions(2);
31
- const { cache } = global.server.app;
32
-
33
- const res = await cache.getSetValue('BIN_INT', () => 1n);
34
- expect(res).toBe(1n);
35
-
36
- const res2 = await cache.getSetValue('BIN_INT', () => '1111');
37
- expect(res2).toBe(1n);
38
- });
39
-
40
- it('can execute only one request per time', async () => {
41
- expect.assertions(3);
42
- const { cache } = global.server.app;
43
- let counter = 0;
44
-
45
- const f = async () => {
46
- await setTimeout(10);
47
- counter += 1;
48
- return 1;
49
- };
50
-
51
- const [res, res1] = await Promise.all([
52
- cache.getSetValue('T', f),
53
- cache.getSetValue('T', f),
54
- ]);
55
-
56
- expect(counter).toBe(1);
57
-
58
- expect(res).toBe(1);
59
- expect(res1).toBe(1);
60
- });
61
-
62
- it('can handle problems on onNotFound', async () => {
63
- expect.assertions(1);
64
- const getAsyncThrow = async () => {
65
- throw new Error('err');
66
- };
67
- let err;
68
-
69
- const { cache } = global.server.app;
70
-
71
- try {
72
- await Promise.all([
73
- cache.getSetValue('THROW', getAsyncThrow),
74
- cache.getSetValue('THROW', getAsyncThrow),
75
- ]);
76
- } catch (e) {
77
- err = e;
78
- }
79
- expect(err.message).toBe('err');
80
- });
81
- });
@@ -1,57 +0,0 @@
1
- import { beforeAll, describe, it, expect } from 'vitest';
2
- import Auth from './Auth.js';
3
-
4
- describe('atuh middleware methods', () => {
5
- let middleware;
6
- beforeAll(() => {
7
- middleware = new Auth(global.server.app);
8
- });
9
- it('have description fields', async () => {
10
- expect.assertions(1);
11
- expect(middleware.constructor.description).toBeDefined();
12
- });
13
-
14
- it('middleware pass when user presented', async () => {
15
- expect.assertions(1);
16
- let isCalled = false;
17
- const nextFunction = () => {
18
- isCalled = true;
19
- };
20
- const req = {
21
- appInfo: {
22
- user: true,
23
- },
24
- };
25
- await middleware.middleware(req, {}, nextFunction);
26
- expect(isCalled).toBeTruthy();
27
- });
28
-
29
- it('middleware NOT pass when user NOT presented', async () => {
30
- expect.assertions(3);
31
- let isCalled = false;
32
- let status;
33
- let isSend;
34
- const nextFunction = () => {
35
- isCalled = true;
36
- };
37
- const req = {
38
- appInfo: {}, // no user
39
- };
40
- await middleware.middleware(
41
- req,
42
- {
43
- status(statusCode) {
44
- status = statusCode;
45
- return this;
46
- },
47
- json() {
48
- isSend = true;
49
- },
50
- },
51
- nextFunction,
52
- );
53
- expect(isCalled).toBeFalsy();
54
- expect(status).toBe(401);
55
- expect(isSend).toBeTruthy();
56
- });
57
- });
@@ -1,147 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import Cors from './Cors.js';
3
-
4
- describe('cors middleware methods', () => {
5
- it('have description fields', async () => {
6
- expect.assertions(1);
7
- const middleware = new Cors(global.server.app, { origins: ['something'] });
8
- expect(middleware.constructor.description).toBeDefined();
9
- });
10
-
11
- it('should throw without origns', async () => {
12
- expect.assertions(1);
13
- expect(() => new Cors(global.server.app)).toThrow();
14
- });
15
-
16
- it('should throw with empty options', async () => {
17
- expect.assertions(1);
18
- expect(() => new Cors(global.server.app, {})).toThrow();
19
- });
20
-
21
- it('should throw with empty origins', async () => {
22
- expect.assertions(1);
23
- expect(() => new Cors(global.server.app, { origins: [] })).toThrow();
24
- });
25
-
26
- it('should throw with empty origins not array', async () => {
27
- expect.assertions(1);
28
- expect(() => new Cors(global.server.app, { origins: 'origins' })).toThrow();
29
- });
30
-
31
- it('non options should be different', async () => {
32
- expect.assertions(2);
33
- let isCalled = false;
34
- const nextFunction = () => {
35
- isCalled = true;
36
- };
37
- const map = new Map();
38
- const req = {
39
- method: 'GET',
40
-
41
- headers: { origin: 'https://localhost' },
42
- };
43
- const res = {
44
- set: (key, val) => {
45
- map.set(key, val);
46
- },
47
- };
48
- const middleware = new Cors(global.server.app, {
49
- origins: ['https://localhost'],
50
- });
51
-
52
- await middleware.middleware(req, res, nextFunction);
53
- expect(isCalled).toBeTruthy();
54
- expect(map.get('Vary')).toBe('Origin');
55
- });
56
-
57
- it('host the not match origin', async () => {
58
- expect.assertions(1);
59
- let isCalled = false;
60
- const nextFunction = () => {
61
- isCalled = true;
62
- };
63
- const req = {
64
- method: 'OPTIONS',
65
- headers: { origin: 'http://anotherDomain.com' },
66
- };
67
- const middleware = new Cors(global.server.app, {
68
- origins: ['https://localhost'],
69
- });
70
-
71
- await middleware.middleware(req, {}, nextFunction);
72
- expect(isCalled).toBeTruthy();
73
- });
74
-
75
- it('string domain match', async () => {
76
- expect.assertions(5);
77
- let isEndCalled = false;
78
- const map = new Map();
79
- const req = {
80
- method: 'OPTIONS',
81
- headers: {
82
- origin: 'https://localhost',
83
- 'access-control-request-headers': 'someAccessControlRequestHeaders',
84
- },
85
- };
86
- const res = {
87
- set: (key, val) => {
88
- map.set(key, val);
89
- },
90
- status: () => {},
91
- end: () => {
92
- isEndCalled = true;
93
- },
94
- };
95
- const middleware = new Cors(global.server.app, {
96
- origins: ['https://localhost'],
97
- });
98
-
99
- await middleware.middleware(req, res);
100
- expect(isEndCalled).toBeTruthy();
101
- expect(map.get('Vary')).toBe('Origin, Access-Control-Request-Headers');
102
- expect(map.get('Access-Control-Allow-Headers')).toBe(
103
- 'someAccessControlRequestHeaders',
104
- );
105
- expect(map.get('Access-Control-Allow-Origin')).toBe('https://localhost');
106
- expect(map.get('Access-Control-Allow-Methods')).toBe(
107
- 'GET,HEAD,PUT,PATCH,POST,DELETE',
108
- );
109
- });
110
-
111
- it('regexp domain match', async () => {
112
- expect.assertions(5);
113
- let isEndCalled = false;
114
- const map = new Map();
115
- const req = {
116
- method: 'OPTIONS',
117
- headers: {
118
- origin: 'https://localhost',
119
- 'access-control-request-headers': 'someAccessControlRequestHeaders',
120
- },
121
- };
122
- const res = {
123
- set: (key, val) => {
124
- map.set(key, val);
125
- },
126
- status: () => {},
127
-
128
- end: () => {
129
- isEndCalled = true;
130
- },
131
- };
132
- const middleware = new Cors(global.server.app, {
133
- origins: [/./],
134
- });
135
-
136
- await middleware.middleware(req, res);
137
- expect(isEndCalled).toBeTruthy();
138
- expect(map.get('Vary')).toBe('Origin, Access-Control-Request-Headers');
139
- expect(map.get('Access-Control-Allow-Headers')).toBe(
140
- 'someAccessControlRequestHeaders',
141
- );
142
- expect(map.get('Access-Control-Allow-Origin')).toBe('https://localhost');
143
- expect(map.get('Access-Control-Allow-Methods')).toBe(
144
- 'GET,HEAD,PUT,PATCH,POST,DELETE',
145
- );
146
- });
147
- });
@@ -1,108 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import GetUserByToken from './GetUserByToken.js';
3
-
4
- describe('getUserByToken middleware methods', () => {
5
- it('have description fields', async () => {
6
- expect.assertions(1);
7
- const middleware = new GetUserByToken(global.server.app);
8
- expect(middleware.constructor.description).toBeDefined();
9
- });
10
-
11
- it('have description usedAuthParameters', async () => {
12
- expect.assertions(2);
13
- const middleware = new GetUserByToken(global.server.app);
14
- const params = middleware.usedAuthParameters;
15
- expect(params).toHaveLength(1);
16
- expect(params[0].name).toBe('Authorization');
17
- });
18
-
19
- it('should not called twice', async () => {
20
- expect.assertions(1);
21
- const middleware = new GetUserByToken(global.server.app);
22
- let isCalled = false;
23
- const nextFunction = () => {
24
- isCalled = true;
25
- };
26
- const req = {
27
- appInfo: {
28
- user: {},
29
- },
30
- };
31
- await middleware.middleware(req, {}, nextFunction);
32
- expect(isCalled).toBeTruthy();
33
- });
34
-
35
- it('should not getuser without token', async () => {
36
- expect.assertions(1);
37
- const middleware = new GetUserByToken(global.server.app);
38
- let isCalled = false;
39
- const nextFunction = () => {
40
- isCalled = true;
41
- };
42
- const req = {
43
- appInfo: {},
44
- body: {},
45
- get: () => {},
46
- };
47
-
48
- await middleware.middleware(req, {}, nextFunction);
49
- expect(isCalled).toBeTruthy();
50
- });
51
-
52
- it('should not getuser with a wrong token', async () => {
53
- expect.assertions(2);
54
- const middleware = new GetUserByToken(global.server.app);
55
- let isCalled = false;
56
- const nextFunction = () => {
57
- isCalled = true;
58
- };
59
- const req = {
60
- appInfo: {},
61
- body: {
62
- token: 'fake',
63
- },
64
- get: () => {},
65
- };
66
- await middleware.middleware(req, {}, nextFunction);
67
- expect(isCalled).toBeTruthy();
68
- expect(req.appInfo.user).toBeUndefined();
69
- });
70
-
71
- it('should not getuser with a good token in body', async () => {
72
- expect.assertions(2);
73
- const middleware = new GetUserByToken(global.server.app);
74
- let isCalled = false;
75
- const nextFunction = () => {
76
- isCalled = true;
77
- };
78
- const req = {
79
- appInfo: {},
80
- body: {
81
- token: global.authToken.token,
82
- },
83
- get: () => {},
84
- };
85
-
86
- await middleware.middleware(req, {}, nextFunction);
87
- expect(isCalled).toBeTruthy();
88
- expect(req.appInfo.user).toBeDefined();
89
- });
90
-
91
- it('should not getuser with a good token in header', async () => {
92
- expect.assertions(2);
93
- const middleware = new GetUserByToken(global.server.app);
94
- let isCalled = false;
95
- const nextFunction = () => {
96
- isCalled = true;
97
- };
98
- const req = {
99
- appInfo: {},
100
- body: {},
101
- get: () => global.authToken.token,
102
- };
103
-
104
- await middleware.middleware(req, {}, nextFunction);
105
- expect(isCalled).toBeTruthy();
106
- expect(req.appInfo.user).toBeDefined();
107
- });
108
- });
@@ -1,96 +0,0 @@
1
- import { beforeAll, describe, it, expect } from 'vitest';
2
- import I18n from './I18n.js';
3
-
4
- describe('i18n middleware methods', () => {
5
- let middleware;
6
- beforeAll(() => {
7
- middleware = new I18n(global.server.app);
8
- });
9
- it('have description fields', async () => {
10
- expect.assertions(1);
11
- expect(middleware.constructor.description).toBeDefined();
12
- });
13
-
14
- it('detectors should works correctly', async () => {
15
- expect.assertions(6);
16
- const request = {
17
- get: () => 'en',
18
- query: {
19
- [middleware.lookupQuerystring]: 'es',
20
- },
21
- };
22
- let lang = await middleware.detectLang(request);
23
- expect(lang).toBe('en');
24
-
25
- request.appInfo = {
26
- user: {
27
- locale: 'be',
28
- },
29
- };
30
- lang = await middleware.detectLang(request);
31
- expect(lang).toBe('en');
32
- request.get = () => null;
33
- lang = await middleware.detectLang(request);
34
- expect(lang).toBe('es');
35
-
36
- delete request.query;
37
- lang = await middleware.detectLang(request);
38
- expect(lang).toBe('be');
39
-
40
- request.query = {
41
- [middleware.lookupQuerystring]: 'en-GB',
42
- };
43
- lang = await middleware.detectLang(request);
44
- expect(lang).toBe('en');
45
-
46
- lang = await middleware.detectLang(request, false);
47
- expect(lang).toBe('en-GB');
48
- });
49
-
50
- it('middleware that works', async () => {
51
- expect.assertions(6);
52
- let isCalled = false;
53
- const nextFunction = () => {
54
- isCalled = true;
55
- };
56
- const req = {
57
- get: () => 'en',
58
- appInfo: {},
59
- };
60
- await middleware.middleware(req, {}, nextFunction);
61
- expect(isCalled).toBeTruthy();
62
- expect(req.appInfo.i18n).toBeDefined();
63
- expect(req.appInfo.i18n.language).toBe('en');
64
- expect(req.appInfo.i18n.t('aaaaa')).toBe('aaaaa');
65
- expect(req.i18n.t('aaaaa')).toBe('aaaaa'); // proxy test
66
-
67
- const req2 = {
68
- get: () => 'fakeLang',
69
- appInfo: {},
70
- };
71
-
72
- await middleware.middleware(req2, {}, nextFunction);
73
- expect(req2.appInfo.i18n.language).toBe('en');
74
- });
75
-
76
- it('middleware disabled', async () => {
77
- expect.assertions(4);
78
- global.server.app.updateConfig('i18n', { enabled: false });
79
- middleware = new I18n(global.server.app);
80
-
81
- let isCalled = false;
82
- const nextFunction = () => {
83
- isCalled = true;
84
- };
85
- const req = {
86
- get: () => 'en',
87
- appInfo: {},
88
- };
89
- await middleware.middleware(req, {}, nextFunction);
90
- expect(isCalled).toBeTruthy();
91
- expect(req.appInfo.i18n).toBeDefined();
92
- expect(req.appInfo.i18n.t('aaaaa')).toBe('aaaaa');
93
- expect(req.i18n.t('aaaaa')).toBe('aaaaa'); // proxy test
94
- global.server.app.updateConfig('i18n', { enabled: true });
95
- });
96
- });