@furystack/rest-service 7.0.11 → 7.0.13
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/esm/actions/error-action.spec.d.ts +2 -0
- package/esm/actions/error-action.spec.d.ts.map +1 -0
- package/esm/actions/error-action.spec.js +47 -0
- package/esm/actions/error-action.spec.js.map +1 -0
- package/esm/actions/get-current-user.spec.d.ts +2 -0
- package/esm/actions/get-current-user.spec.d.ts.map +1 -0
- package/esm/actions/get-current-user.spec.js +19 -0
- package/esm/actions/get-current-user.spec.js.map +1 -0
- package/esm/actions/is-authenticated.spec.d.ts +2 -0
- package/esm/actions/is-authenticated.spec.d.ts.map +1 -0
- package/esm/actions/is-authenticated.spec.js +18 -0
- package/esm/actions/is-authenticated.spec.js.map +1 -0
- package/esm/actions/login-action.spec.d.ts +2 -0
- package/esm/actions/login-action.spec.d.ts.map +1 -0
- package/esm/actions/login-action.spec.js +34 -0
- package/esm/actions/login-action.spec.js.map +1 -0
- package/esm/actions/logout-action.spec.d.ts +2 -0
- package/esm/actions/logout-action.spec.d.ts.map +1 -0
- package/esm/actions/logout-action.spec.js +22 -0
- package/esm/actions/logout-action.spec.js.map +1 -0
- package/esm/actions/not-found-action.spec.d.ts +2 -0
- package/esm/actions/not-found-action.spec.d.ts.map +1 -0
- package/esm/actions/not-found-action.spec.js +16 -0
- package/esm/actions/not-found-action.spec.js.map +1 -0
- package/esm/add-cors-header.spec.d.ts +2 -0
- package/esm/add-cors-header.spec.d.ts.map +1 -0
- package/esm/add-cors-header.spec.js +98 -0
- package/esm/add-cors-header.spec.js.map +1 -0
- package/esm/api-manager.js +4 -4
- package/esm/api-manager.js.map +1 -1
- package/esm/authenticate.spec.d.ts +2 -0
- package/esm/authenticate.spec.d.ts.map +1 -0
- package/esm/authenticate.spec.js +58 -0
- package/esm/authenticate.spec.js.map +1 -0
- package/esm/authorize.spec.d.ts +2 -0
- package/esm/authorize.spec.d.ts.map +1 -0
- package/esm/authorize.spec.js +54 -0
- package/esm/authorize.spec.js.map +1 -0
- package/esm/endpoint-generators/create-delete-endpoint.spec.d.ts +2 -0
- package/esm/endpoint-generators/create-delete-endpoint.spec.d.ts.map +1 -0
- package/esm/endpoint-generators/create-delete-endpoint.spec.js +34 -0
- package/esm/endpoint-generators/create-delete-endpoint.spec.js.map +1 -0
- package/esm/endpoint-generators/create-get-collection-endpoint.spec.d.ts +2 -0
- package/esm/endpoint-generators/create-get-collection-endpoint.spec.d.ts.map +1 -0
- package/esm/endpoint-generators/create-get-collection-endpoint.spec.js +156 -0
- package/esm/endpoint-generators/create-get-collection-endpoint.spec.js.map +1 -0
- package/esm/endpoint-generators/create-get-entity-endpoint.spec.d.ts +2 -0
- package/esm/endpoint-generators/create-get-entity-endpoint.spec.d.ts.map +1 -0
- package/esm/endpoint-generators/create-get-entity-endpoint.spec.js +74 -0
- package/esm/endpoint-generators/create-get-entity-endpoint.spec.js.map +1 -0
- package/esm/endpoint-generators/create-patch-endpoint.spec.d.ts +2 -0
- package/esm/endpoint-generators/create-patch-endpoint.spec.d.ts.map +1 -0
- package/esm/endpoint-generators/create-patch-endpoint.spec.js +37 -0
- package/esm/endpoint-generators/create-patch-endpoint.spec.js.map +1 -0
- package/esm/endpoint-generators/create-post-endpoint.spec.d.ts +2 -0
- package/esm/endpoint-generators/create-post-endpoint.spec.d.ts.map +1 -0
- package/esm/endpoint-generators/create-post-endpoint.spec.js +35 -0
- package/esm/endpoint-generators/create-post-endpoint.spec.js.map +1 -0
- package/esm/endpoint-generators/utils.js +2 -0
- package/esm/endpoint-generators/utils.js.map +1 -1
- package/esm/helpers.spec.d.ts +2 -0
- package/esm/helpers.spec.d.ts.map +1 -0
- package/esm/helpers.spec.js +34 -0
- package/esm/helpers.spec.js.map +1 -0
- package/esm/http-authentication-settings.js +7 -8
- package/esm/http-authentication-settings.js.map +1 -1
- package/esm/http-user-context.js +24 -21
- package/esm/http-user-context.js.map +1 -1
- package/esm/http-user-context.spec.d.ts +3 -0
- package/esm/http-user-context.spec.d.ts.map +1 -0
- package/esm/http-user-context.spec.js +269 -0
- package/esm/http-user-context.spec.js.map +1 -0
- package/esm/incoming-message-extensions.spec.d.ts +2 -0
- package/esm/incoming-message-extensions.spec.d.ts.map +1 -0
- package/esm/incoming-message-extensions.spec.js +38 -0
- package/esm/incoming-message-extensions.spec.js.map +1 -0
- package/esm/mime-types.spec.d.ts +2 -0
- package/esm/mime-types.spec.d.ts.map +1 -0
- package/esm/mime-types.spec.js +14 -0
- package/esm/mime-types.spec.js.map +1 -0
- package/esm/models/default-session.js +8 -0
- package/esm/models/default-session.js.map +1 -1
- package/esm/rest-service.integration.spec.d.ts +2 -0
- package/esm/rest-service.integration.spec.d.ts.map +1 -0
- package/esm/rest-service.integration.spec.js +156 -0
- package/esm/rest-service.integration.spec.js.map +1 -0
- package/esm/rest.integration.test.d.ts +89 -0
- package/esm/rest.integration.test.d.ts.map +1 -0
- package/esm/rest.integration.test.js +154 -0
- package/esm/rest.integration.test.js.map +1 -0
- package/esm/schema-validator/schema-validation-error.js +1 -0
- package/esm/schema-validator/schema-validation-error.js.map +1 -1
- package/esm/schema-validator/schema-validator.d.ts +1 -2
- package/esm/schema-validator/schema-validator.d.ts.map +1 -1
- package/esm/schema-validator/schema-validator.js +3 -2
- package/esm/schema-validator/schema-validator.js.map +1 -1
- package/esm/schema-validator/schema-validator.test.d.ts +2 -0
- package/esm/schema-validator/schema-validator.test.d.ts.map +1 -0
- package/esm/schema-validator/schema-validator.test.js +61 -0
- package/esm/schema-validator/schema-validator.test.js.map +1 -0
- package/esm/server-manager.js +12 -12
- package/esm/server-manager.js.map +1 -1
- package/esm/server-response-extensions.spec.d.ts +2 -0
- package/esm/server-response-extensions.spec.d.ts.map +1 -0
- package/esm/server-response-extensions.spec.js +52 -0
- package/esm/server-response-extensions.spec.js.map +1 -0
- package/esm/static-server-manager.js +26 -26
- package/esm/static-server-manager.js.map +1 -1
- package/esm/static-server-manager.spec.d.ts +2 -0
- package/esm/static-server-manager.spec.d.ts.map +1 -0
- package/esm/static-server-manager.spec.js +216 -0
- package/esm/static-server-manager.spec.js.map +1 -0
- package/esm/utils.js +2 -1
- package/esm/utils.js.map +1 -1
- package/esm/validate.integration.spec.d.ts +2 -0
- package/esm/validate.integration.spec.d.ts.map +1 -0
- package/esm/validate.integration.spec.js +211 -0
- package/esm/validate.integration.spec.js.map +1 -0
- package/package.json +11 -11
- package/src/actions/error-action.spec.ts +1 -1
- package/src/actions/get-current-user.spec.ts +1 -1
- package/src/actions/is-authenticated.spec.ts +1 -1
- package/src/actions/login-action.spec.ts +2 -2
- package/src/actions/logout-action.spec.ts +2 -2
- package/src/actions/not-found-action.spec.ts +1 -1
- package/src/add-cors-header.spec.ts +1 -1
- package/src/authenticate.spec.ts +3 -3
- package/src/authorize.spec.ts +2 -2
- package/src/endpoint-generators/create-delete-endpoint.spec.ts +3 -3
- package/src/endpoint-generators/create-get-collection-endpoint.spec.ts +3 -3
- package/src/endpoint-generators/create-get-entity-endpoint.spec.ts +3 -3
- package/src/endpoint-generators/create-patch-endpoint.spec.ts +3 -3
- package/src/endpoint-generators/create-post-endpoint.spec.ts +3 -3
- package/src/helpers.spec.ts +4 -4
- package/src/http-user-context.spec.ts +3 -3
- package/src/mime-types.spec.ts +1 -1
- package/src/rest-service.integration.spec.ts +4 -4
- package/src/rest.integration.test.ts +2 -3
- package/src/schema-validator/schema-validator.test.ts +4 -4
- package/src/schema-validator/schema-validator.ts +6 -5
- package/src/server-response-extensions.spec.ts +1 -1
- package/src/static-server-manager.spec.ts +15 -15
- package/src/validate.integration.spec.ts +4 -7
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { usingAsync } from '@furystack/utils';
|
|
2
|
+
import { Injector } from '@furystack/inject';
|
|
3
|
+
import { User, StoreManager, InMemoryStore, addStore } from '@furystack/core';
|
|
4
|
+
import { DefaultSession } from './models/default-session.js';
|
|
5
|
+
import { HttpUserContext } from './http-user-context.js';
|
|
6
|
+
import { PasswordAuthenticator, PasswordCredential, UnauthenticatedError } from '@furystack/security';
|
|
7
|
+
import { useHttpAuthentication } from './helpers.js';
|
|
8
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
9
|
+
export const prepareInjector = async (i) => {
|
|
10
|
+
addStore(i, new InMemoryStore({ model: User, primaryKey: 'username' }))
|
|
11
|
+
.addStore(new InMemoryStore({ model: DefaultSession, primaryKey: 'sessionId' }))
|
|
12
|
+
.addStore(new InMemoryStore({ model: PasswordCredential, primaryKey: 'userName' }));
|
|
13
|
+
useHttpAuthentication(i);
|
|
14
|
+
};
|
|
15
|
+
const setupUser = async (i, userName, password) => {
|
|
16
|
+
const sm = i.getInstance(StoreManager);
|
|
17
|
+
const pw = i.getInstance(PasswordAuthenticator);
|
|
18
|
+
const hasher = pw.getHasher();
|
|
19
|
+
const cred = await hasher.createCredential(userName, password);
|
|
20
|
+
await sm.getStoreFor(PasswordCredential, 'userName').add(cred);
|
|
21
|
+
await sm.getStoreFor(User, 'username').add({ username: userName, roles: [] });
|
|
22
|
+
};
|
|
23
|
+
describe('HttpUserContext', () => {
|
|
24
|
+
const request = { headers: {} };
|
|
25
|
+
const response = {};
|
|
26
|
+
const testUser = { username: 'testUser', roles: ['grantedRole1', 'grantedRole2'] };
|
|
27
|
+
it('Should be constructed with the extension method', async () => {
|
|
28
|
+
await usingAsync(new Injector(), async (i) => {
|
|
29
|
+
await prepareInjector(i);
|
|
30
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
31
|
+
expect(ctx).toBeInstanceOf(HttpUserContext);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
describe('isAuthenticated', () => {
|
|
35
|
+
it('Should return true for authenticated users', async () => {
|
|
36
|
+
await usingAsync(new Injector(), async (i) => {
|
|
37
|
+
await prepareInjector(i);
|
|
38
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
39
|
+
ctx.getCurrentUser = vi.fn(async () => testUser);
|
|
40
|
+
const value = await ctx.isAuthenticated(request);
|
|
41
|
+
expect(value).toBe(true);
|
|
42
|
+
expect(ctx.getCurrentUser).toBeCalled();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
it('Should return false for unauthenticated users', async () => {
|
|
46
|
+
await usingAsync(new Injector(), async (i) => {
|
|
47
|
+
await prepareInjector(i);
|
|
48
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
49
|
+
ctx.getCurrentUser = vi.fn(async () => {
|
|
50
|
+
throw Error(':(');
|
|
51
|
+
});
|
|
52
|
+
await expect(ctx.isAuthenticated(request)).resolves.toEqual(false);
|
|
53
|
+
expect(ctx.getCurrentUser).toBeCalled();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
describe('isAuthorized', () => {
|
|
58
|
+
it('Should return true if all roles are authorized', async () => {
|
|
59
|
+
await usingAsync(new Injector(), async (i) => {
|
|
60
|
+
await prepareInjector(i);
|
|
61
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
62
|
+
ctx.getCurrentUser = vi.fn(async () => testUser);
|
|
63
|
+
const value = await ctx.isAuthorized(request, 'grantedRole1', 'grantedRole2');
|
|
64
|
+
expect(value).toBe(true);
|
|
65
|
+
expect(ctx.getCurrentUser).toBeCalled();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
it('Should return false if not all roles are authorized', async () => {
|
|
69
|
+
await usingAsync(new Injector(), async (i) => {
|
|
70
|
+
await prepareInjector(i);
|
|
71
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
72
|
+
ctx.getCurrentUser = vi.fn(async () => testUser);
|
|
73
|
+
const value = await ctx.isAuthorized(request, 'grantedRole1', 'nonGrantedRole2');
|
|
74
|
+
expect(value).toBe(false);
|
|
75
|
+
expect(ctx.getCurrentUser).toBeCalled();
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
describe('authenticateUser', () => {
|
|
80
|
+
it('Should fail when the store is empty', async () => {
|
|
81
|
+
await usingAsync(new Injector(), async (i) => {
|
|
82
|
+
await prepareInjector(i);
|
|
83
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
84
|
+
await expect(ctx.authenticateUser('user', 'password')).rejects.toThrowError(UnauthenticatedError);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
it('Should fail when the password not equals', async () => {
|
|
88
|
+
await usingAsync(new Injector(), async (i) => {
|
|
89
|
+
await prepareInjector(i);
|
|
90
|
+
await setupUser(i, 'user', 'pass123');
|
|
91
|
+
await expect(i.getInstance(HttpUserContext).authenticateUser('user', 'pass321')).rejects.toThrowError(UnauthenticatedError);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
it('Should fail when the username not equals', async () => {
|
|
95
|
+
await usingAsync(new Injector(), async (i) => {
|
|
96
|
+
await prepareInjector(i);
|
|
97
|
+
await setupUser(i, 'otherUser', 'pass123');
|
|
98
|
+
expect(i.getInstance(HttpUserContext).authenticateUser('user', 'pass123')).rejects.toThrowError(UnauthenticatedError);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
it('Should fail when password not provided', async () => {
|
|
102
|
+
await usingAsync(new Injector(), async (i) => {
|
|
103
|
+
await prepareInjector(i);
|
|
104
|
+
await setupUser(i, 'user', 'pass123');
|
|
105
|
+
await expect(i.getInstance(HttpUserContext).authenticateUser('user', '')).rejects.toThrowError(UnauthenticatedError);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
it('Should fail when the user is not in the user store', async () => {
|
|
109
|
+
await usingAsync(new Injector(), async (i) => {
|
|
110
|
+
await prepareInjector(i);
|
|
111
|
+
await setupUser(i, 'user', 'pass123');
|
|
112
|
+
await i.getInstance(StoreManager).getStoreFor(User, 'username').remove('user');
|
|
113
|
+
await expect(i.getInstance(HttpUserContext).authenticateUser('user', 'pass123')).rejects.toThrowError(UnauthenticatedError);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
it('Should return the user when the username and password matches', async () => {
|
|
117
|
+
await usingAsync(new Injector(), async (i) => {
|
|
118
|
+
await prepareInjector(i);
|
|
119
|
+
await setupUser(i, 'user', 'pass123');
|
|
120
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
121
|
+
const value = await ctx.authenticateUser('user', 'pass123');
|
|
122
|
+
expect(value).toEqual({ username: 'user', roles: [] });
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
describe('getSessionIdFromRequest', () => {
|
|
127
|
+
it('Should return null if no headers present', async () => {
|
|
128
|
+
await usingAsync(new Injector(), async (i) => {
|
|
129
|
+
await prepareInjector(i);
|
|
130
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
131
|
+
const sid = ctx.getSessionIdFromRequest(request);
|
|
132
|
+
expect(sid).toBeNull();
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
it('Should return null if no session ID cookie present', async () => {
|
|
136
|
+
await usingAsync(new Injector(), async (i) => {
|
|
137
|
+
await prepareInjector(i);
|
|
138
|
+
const requestWithCookie = { ...request, cookie: 'a=2;b=3;c=4;' };
|
|
139
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
140
|
+
const sid = ctx.getSessionIdFromRequest(requestWithCookie);
|
|
141
|
+
expect(sid).toBeNull();
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
it('Should return the Session ID value if session ID cookie present', async () => {
|
|
145
|
+
await usingAsync(new Injector(), async (i) => {
|
|
146
|
+
await prepareInjector(i);
|
|
147
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
148
|
+
const requestWithAuthCookie = {
|
|
149
|
+
...request,
|
|
150
|
+
headers: { cookie: `a=2;b=3;${ctx.authentication.cookieName}=666;c=4;` },
|
|
151
|
+
};
|
|
152
|
+
const sid = ctx.getSessionIdFromRequest(requestWithAuthCookie);
|
|
153
|
+
expect(sid).toBe('666');
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
describe('authenticateRequest', () => {
|
|
158
|
+
it('Should try to authenticate with Basic, if enabled', async () => {
|
|
159
|
+
await usingAsync(new Injector(), async (i) => {
|
|
160
|
+
await prepareInjector(i);
|
|
161
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
162
|
+
ctx.authenticateUser = vi.fn(async () => testUser);
|
|
163
|
+
const result = await ctx.authenticateRequest({
|
|
164
|
+
headers: { authorization: `Basic dGVzdHVzZXI6cGFzc3dvcmQ=` },
|
|
165
|
+
});
|
|
166
|
+
expect(ctx.authenticateUser).toBeCalledWith('testuser', 'password');
|
|
167
|
+
expect(result).toBe(testUser);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
it('Should NOT try to authenticate with Basic, if disabled', async () => {
|
|
171
|
+
await usingAsync(new Injector(), async (i) => {
|
|
172
|
+
await prepareInjector(i);
|
|
173
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
174
|
+
ctx.authentication.enableBasicAuth = false;
|
|
175
|
+
ctx.authenticateUser = vi.fn(async () => testUser);
|
|
176
|
+
await expect(ctx.authenticateRequest({
|
|
177
|
+
headers: { authorization: `Basic dGVzdHVzZXI6cGFzc3dvcmQ=` },
|
|
178
|
+
})).rejects.toThrowError(UnauthenticatedError);
|
|
179
|
+
expect(ctx.authenticateUser).not.toBeCalled();
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
it('Should fail with no session in the store', async () => {
|
|
183
|
+
await usingAsync(new Injector(), async (i) => {
|
|
184
|
+
await prepareInjector(i);
|
|
185
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
186
|
+
await expect(ctx.authenticateRequest({
|
|
187
|
+
headers: { cookie: `${ctx.authentication.cookieName}=666;a=3` },
|
|
188
|
+
})).rejects.toThrowError(UnauthenticatedError);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
it('Should fail with valid session Id but no user', async () => {
|
|
192
|
+
await usingAsync(new Injector(), async (i) => {
|
|
193
|
+
await prepareInjector(i);
|
|
194
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
195
|
+
ctx.authentication
|
|
196
|
+
.getSessionStore(i.getInstance(StoreManager))
|
|
197
|
+
.add({ sessionId: '666', username: testUser.username });
|
|
198
|
+
await expect(ctx.authenticateRequest({
|
|
199
|
+
headers: { cookie: `${ctx.authentication.cookieName}=666;a=3` },
|
|
200
|
+
})).rejects.toThrowError(UnauthenticatedError);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
it('Should authenticate with cookie, if the session IDs matches', async () => {
|
|
204
|
+
await usingAsync(new Injector(), async (i) => {
|
|
205
|
+
await prepareInjector(i);
|
|
206
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
207
|
+
ctx.authentication
|
|
208
|
+
.getSessionStore(i.getInstance(StoreManager))
|
|
209
|
+
.add({ sessionId: '666', username: testUser.username });
|
|
210
|
+
ctx.authentication.getUserStore(i.getInstance(StoreManager)).add({ ...testUser });
|
|
211
|
+
const result = await ctx.authenticateRequest({
|
|
212
|
+
headers: { cookie: `${ctx.authentication.cookieName}=666;a=3` },
|
|
213
|
+
});
|
|
214
|
+
expect(result).toEqual(testUser);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
describe('getCurrentUser', () => {
|
|
219
|
+
it('Should return the current user from authenticateRequest() once per request', async () => {
|
|
220
|
+
await usingAsync(new Injector(), async (i) => {
|
|
221
|
+
await prepareInjector(i);
|
|
222
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
223
|
+
ctx.authenticateRequest = vi.fn(async () => testUser);
|
|
224
|
+
const result = await ctx.getCurrentUser(request);
|
|
225
|
+
const result2 = await ctx.getCurrentUser(request);
|
|
226
|
+
expect(ctx.authenticateRequest).toBeCalledTimes(1);
|
|
227
|
+
expect(result).toBe(testUser);
|
|
228
|
+
expect(result2).toBe(testUser);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
describe('cookieLogin', () => {
|
|
233
|
+
it('Should return the current user from authenticateRequest() once per request', async () => {
|
|
234
|
+
await usingAsync(new Injector(), async (i) => {
|
|
235
|
+
await prepareInjector(i);
|
|
236
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
237
|
+
const setHeader = vi.fn();
|
|
238
|
+
ctx.getSessionStore().add = vi.fn(async () => {
|
|
239
|
+
return {};
|
|
240
|
+
});
|
|
241
|
+
const authResult = await ctx.cookieLogin(testUser, { setHeader });
|
|
242
|
+
expect(authResult).toBe(testUser);
|
|
243
|
+
expect(setHeader).toBeCalled();
|
|
244
|
+
expect(ctx.getSessionStore().add).toBeCalled();
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
describe('cookieLogout', () => {
|
|
249
|
+
it('Should invalidate the current session id cookie', async () => {
|
|
250
|
+
await usingAsync(new Injector(), async (i) => {
|
|
251
|
+
await prepareInjector(i);
|
|
252
|
+
const ctx = i.getInstance(HttpUserContext);
|
|
253
|
+
const setHeader = vi.fn();
|
|
254
|
+
ctx.getSessionStore().add = vi.fn(async () => {
|
|
255
|
+
return {};
|
|
256
|
+
});
|
|
257
|
+
ctx.authenticateRequest = vi.fn(async () => testUser);
|
|
258
|
+
ctx.getSessionStore().remove = vi.fn(async () => undefined);
|
|
259
|
+
ctx.getSessionIdFromRequest = () => 'example-session-id';
|
|
260
|
+
response.setHeader = vi.fn(() => response);
|
|
261
|
+
await ctx.cookieLogin(testUser, { setHeader });
|
|
262
|
+
await ctx.cookieLogout(request, response);
|
|
263
|
+
expect(response.setHeader).toBeCalledWith('Set-Cookie', 'fss=; Path=/; HttpOnly');
|
|
264
|
+
expect(ctx.getSessionStore().remove).toBeCalled();
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
//# sourceMappingURL=http-user-context.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-user-context.spec.js","sourceRoot":"","sources":["../src/http-user-context.spec.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA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|
+
import { IncomingMessage } from 'http';
|
|
2
|
+
import './incoming-message-extensions';
|
|
3
|
+
import { Socket } from 'net';
|
|
4
|
+
import { describe, it, expect } from 'vitest';
|
|
5
|
+
describe('IncomingMessage extensions', () => {
|
|
6
|
+
describe('readPostBody', () => {
|
|
7
|
+
it('Should be extended', () => {
|
|
8
|
+
const socket = new Socket();
|
|
9
|
+
const msg = new IncomingMessage(socket);
|
|
10
|
+
expect(typeof msg.readPostBody).toBe('function');
|
|
11
|
+
});
|
|
12
|
+
it('Should read the raw post body', async () => {
|
|
13
|
+
const exampleValue = { value: Math.random().toString() };
|
|
14
|
+
const socket = new Socket();
|
|
15
|
+
const msg = new IncomingMessage(socket);
|
|
16
|
+
setTimeout(() => {
|
|
17
|
+
msg.read = () => JSON.stringify(exampleValue);
|
|
18
|
+
msg.emit('readable');
|
|
19
|
+
msg.emit('end');
|
|
20
|
+
}, 10);
|
|
21
|
+
const result = await msg.readPostBodyRaw();
|
|
22
|
+
expect(result).toEqual(JSON.stringify(exampleValue));
|
|
23
|
+
});
|
|
24
|
+
it('Should read the post body', async () => {
|
|
25
|
+
const exampleValue = { value: Math.random().toString() };
|
|
26
|
+
const socket = new Socket();
|
|
27
|
+
const msg = new IncomingMessage(socket);
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
msg.read = () => JSON.stringify(exampleValue);
|
|
30
|
+
msg.emit('readable');
|
|
31
|
+
msg.emit('end');
|
|
32
|
+
}, 10);
|
|
33
|
+
const result = await msg.readPostBody();
|
|
34
|
+
expect(result).toEqual(exampleValue);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
//# sourceMappingURL=incoming-message-extensions.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"incoming-message-extensions.spec.js","sourceRoot":"","sources":["../src/incoming-message-extensions.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,MAAM,CAAA;AACtC,OAAO,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AI,EAAE;YACzC,MAAM,YAAY,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAA;YACxD,MAAM,MAAM,GAAG,IAAI,MAAM,EAAE,CAAA;YAC3B,MAAM,GAAG,GAAG,IAAI,eAAe,CAAC,MAAM,CAAC,CAAA;YACvC,UAAU,CAAC,GAAG,EAAE;gBACd,GAAG,CAAC,IAAI,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAA;gBAC7C,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;gBACpB,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YACjB,CAAC,EAAE,EAAE,CAAC,CAAA;YAEN,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,YAAY,EAAE,CAAA;YACvC,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAA;QACtC,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mime-types.spec.d.ts","sourceRoot":"","sources":["../src/mime-types.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { getMimeForFile } from './mime-types.js';
|
|
2
|
+
import { describe, it, expect } from 'vitest';
|
|
3
|
+
describe('MIME Types', () => {
|
|
4
|
+
it('Should return a default fallback', () => {
|
|
5
|
+
expect(getMimeForFile('test.alma')).toBe('application/octet-stream');
|
|
6
|
+
});
|
|
7
|
+
it('Should return a value from a supported list', () => {
|
|
8
|
+
expect(getMimeForFile('asd/123/test.mp4')).toBe('video/mp4');
|
|
9
|
+
});
|
|
10
|
+
it('Should return a value from a wildcarded entry list', () => {
|
|
11
|
+
expect(getMimeForFile('asd/123/test.asdxml')).toBe('text/xml');
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
//# sourceMappingURL=mime-types.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mime-types.spec.js","sourceRoot":"","sources":["../src/mime-types.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC7C,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE;IAC1B,EAAE,CAAC,kCAAkC,EAAE,GAAG,EAAE;QAC1C,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAA;IACtE,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,6CAA6C,EAAE,GAAG,EAAE;QACrD,MAAM,CAAC,cAAc,CAAC,kBAAkB,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAA;IAC9D,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,oDAAoD,EAAE,GAAG,EAAE;QAC5D,MAAM,CAAC,cAAc,CAAC,qBAAqB,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;IAChE,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -2,5 +2,13 @@
|
|
|
2
2
|
* Model class that defines the default session data
|
|
3
3
|
*/
|
|
4
4
|
export class DefaultSession {
|
|
5
|
+
/**
|
|
6
|
+
* The generated session identifier
|
|
7
|
+
*/
|
|
8
|
+
sessionId;
|
|
9
|
+
/**
|
|
10
|
+
* The user's login name for the session
|
|
11
|
+
*/
|
|
12
|
+
username;
|
|
5
13
|
}
|
|
6
14
|
//# sourceMappingURL=default-session.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"default-session.js","sourceRoot":"","sources":["../../src/models/default-session.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,cAAc;
|
|
1
|
+
{"version":3,"file":"default-session.js","sourceRoot":"","sources":["../../src/models/default-session.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,cAAc;IACzB;;OAEG;IACI,SAAS,CAAS;IAEzB;;OAEG;IACI,QAAQ,CAAS;CACzB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rest-service.integration.spec.d.ts","sourceRoot":"","sources":["../src/rest-service.integration.spec.ts"],"names":[],"mappings":"AACA,OAAO,WAAW,CAAA"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { Injector } from '@furystack/inject';
|
|
2
|
+
import './helpers';
|
|
3
|
+
import { usingAsync, PathHelper } from '@furystack/utils';
|
|
4
|
+
import { GetCurrentUser, IsAuthenticated, LoginAction, LogoutAction } from './actions/index.js';
|
|
5
|
+
import { User, InMemoryStore, addStore } from '@furystack/core';
|
|
6
|
+
import { DefaultSession } from './models/default-session.js';
|
|
7
|
+
import { JsonResult } from './request-action-implementation.js';
|
|
8
|
+
import { useHttpAuthentication, useRestService } from './helpers.js';
|
|
9
|
+
import { describe, it, expect } from 'vitest';
|
|
10
|
+
import { serializeValue } from '@furystack/rest';
|
|
11
|
+
class UserWithPassword extends User {
|
|
12
|
+
password;
|
|
13
|
+
}
|
|
14
|
+
const portGenerator = function* () {
|
|
15
|
+
const initialPort = 19090;
|
|
16
|
+
let port = initialPort;
|
|
17
|
+
while (true) {
|
|
18
|
+
yield port++;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const getPort = () => portGenerator().next().value;
|
|
22
|
+
const prepareInjector = async (i) => {
|
|
23
|
+
const port = getPort();
|
|
24
|
+
const hostName = 'localhost';
|
|
25
|
+
const root = 'test-api';
|
|
26
|
+
addStore(i, new InMemoryStore({ model: User, primaryKey: 'username' })).addStore(new InMemoryStore({ model: DefaultSession, primaryKey: 'sessionId' }));
|
|
27
|
+
useHttpAuthentication(i, {
|
|
28
|
+
getUserStore: (sm) => sm.getStoreFor(UserWithPassword, 'username'),
|
|
29
|
+
getSessionStore: (sm) => sm.getStoreFor(DefaultSession, 'sessionId'),
|
|
30
|
+
});
|
|
31
|
+
await useRestService({
|
|
32
|
+
injector: i,
|
|
33
|
+
root,
|
|
34
|
+
port: getPort(),
|
|
35
|
+
hostName,
|
|
36
|
+
cors: {
|
|
37
|
+
credentials: true,
|
|
38
|
+
origins: ['http://localhost:8080'],
|
|
39
|
+
headers: ['cache', 'content-type'],
|
|
40
|
+
},
|
|
41
|
+
api: {
|
|
42
|
+
GET: {
|
|
43
|
+
'/currentUser': GetCurrentUser,
|
|
44
|
+
'/isAuthenticated': IsAuthenticated,
|
|
45
|
+
'/testQuery': async (options) => JsonResult({ param1Value: options.getQuery().param1 }),
|
|
46
|
+
'/testUrlParams/:urlParam': async (options) => JsonResult({ urlParamValue: options.getUrlParams().urlParam }),
|
|
47
|
+
},
|
|
48
|
+
POST: {
|
|
49
|
+
'/login': LoginAction,
|
|
50
|
+
'/logout': LogoutAction,
|
|
51
|
+
'/testPostBody': async (options) => {
|
|
52
|
+
const body = await options.getBody();
|
|
53
|
+
const body2 = await options.getBody();
|
|
54
|
+
return JsonResult({ bodyValue: body.value, body2Value: body2.value });
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
apiUrl: `http://${hostName}:${port}/${root}`,
|
|
61
|
+
port,
|
|
62
|
+
hostName,
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
describe('@furystack/rest-service inregration tests', () => {
|
|
66
|
+
it('Should be started and disposed', async () => {
|
|
67
|
+
await usingAsync(new Injector(), async (i) => {
|
|
68
|
+
await prepareInjector(i);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
it('Should respond with 404 when a route is not found', async () => {
|
|
72
|
+
await usingAsync(new Injector(), async (i) => {
|
|
73
|
+
const { apiUrl } = await prepareInjector(i);
|
|
74
|
+
const result = await fetch(PathHelper.joinPaths(apiUrl, 'some-route-that-does-not-exists'));
|
|
75
|
+
expect(result.ok).toBe(false);
|
|
76
|
+
expect(result.status).toBe(404);
|
|
77
|
+
const responseText = await result.json();
|
|
78
|
+
expect(responseText).toEqual({ error: 'Content not found' });
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
it('Should respond with 401 for unauthorized request errors', async () => {
|
|
82
|
+
await usingAsync(new Injector(), async (i) => {
|
|
83
|
+
const { apiUrl } = await prepareInjector(i);
|
|
84
|
+
const result = await fetch(PathHelper.joinPaths(apiUrl, 'currentUser'));
|
|
85
|
+
expect(result.ok).toBe(false);
|
|
86
|
+
expect(result.status).toBe(401);
|
|
87
|
+
const responseText = await result.json();
|
|
88
|
+
expect(responseText).toEqual({ error: 'unauthorized' });
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
it('Should respond with 401 for unauthorized request errors', async () => {
|
|
92
|
+
await usingAsync(new Injector(), async (i) => {
|
|
93
|
+
const { apiUrl } = await prepareInjector(i);
|
|
94
|
+
const result = await fetch(PathHelper.joinPaths(apiUrl, 'currentUser'));
|
|
95
|
+
expect(result.ok).toBe(false);
|
|
96
|
+
expect(result.status).toBe(401);
|
|
97
|
+
const responseText = await result.json();
|
|
98
|
+
expect(responseText).toEqual({ error: 'unauthorized' });
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
it('Should respond with the correct result body', async () => {
|
|
102
|
+
await usingAsync(new Injector(), async (i) => {
|
|
103
|
+
const { apiUrl } = await prepareInjector(i);
|
|
104
|
+
const response = await fetch(PathHelper.joinPaths(apiUrl, 'isAuthenticated'));
|
|
105
|
+
expect(response.status).toBe(200);
|
|
106
|
+
const result = await response.json();
|
|
107
|
+
expect(result).toEqual({ isAuthenticated: false });
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
it('Should be able to read query parameters', async () => {
|
|
111
|
+
await usingAsync(new Injector(), async (i) => {
|
|
112
|
+
const { apiUrl } = await prepareInjector(i);
|
|
113
|
+
const response = await fetch(PathHelper.joinPaths(apiUrl, `testQuery?param1=${serializeValue('foo')}`));
|
|
114
|
+
expect(response.status).toBe(200);
|
|
115
|
+
const result = await response.json();
|
|
116
|
+
expect(result).toEqual({ param1Value: 'foo' });
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
it('Should be able to read url parameters', async () => {
|
|
120
|
+
await usingAsync(new Injector(), async (i) => {
|
|
121
|
+
const { apiUrl } = await prepareInjector(i);
|
|
122
|
+
const response = await fetch(PathHelper.joinPaths(apiUrl, 'testUrlParams/bar'));
|
|
123
|
+
expect(response.status).toBe(200);
|
|
124
|
+
const result = await response.json();
|
|
125
|
+
expect(result).toEqual({ urlParamValue: 'bar' });
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
it('Should be able to read post body', async () => {
|
|
129
|
+
await usingAsync(new Injector(), async (i) => {
|
|
130
|
+
const { apiUrl } = await prepareInjector(i);
|
|
131
|
+
const response = await fetch(PathHelper.joinPaths(apiUrl, 'testPostBody'), {
|
|
132
|
+
method: 'POST',
|
|
133
|
+
body: JSON.stringify({ value: 'baz' }),
|
|
134
|
+
});
|
|
135
|
+
expect(response.status).toBe(200);
|
|
136
|
+
const result = await response.json();
|
|
137
|
+
expect(result).toEqual({ bodyValue: 'baz', body2Value: 'baz' });
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
it('Should respond with OK to OPTIONS requests', async () => {
|
|
141
|
+
await usingAsync(new Injector(), async (i) => {
|
|
142
|
+
const { apiUrl } = await prepareInjector(i);
|
|
143
|
+
const response = await fetch(PathHelper.joinPaths(apiUrl, 'testPostBody'), {
|
|
144
|
+
method: 'OPTIONS',
|
|
145
|
+
});
|
|
146
|
+
expect(response.status).toBe(200);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
it('Should reject requests outside of the API Root', async () => {
|
|
150
|
+
await usingAsync(new Injector(), async (i) => {
|
|
151
|
+
const { hostName, port } = await prepareInjector(i);
|
|
152
|
+
await expect(fetch(PathHelper.joinPaths(`http://${hostName}:${port}`, 'not-my-api-root'))).rejects.toThrowError('fetch failed');
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
//# sourceMappingURL=rest-service.integration.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rest-service.integration.spec.js","sourceRoot":"","sources":["../src/rest-service.integration.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC5C,OAAO,WAAW,CAAA;AAClB,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AACzD,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAE/F,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,oCAAoC,CAAA;AAC/D,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AACpE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAEhD,MAAM,gBAAiB,SAAQ,IAAI;IACjC,QAAQ,CAAS;CAClB;AAgBD,MAAM,aAAa,GAAG,QAAQ,CAAC;IAC7B,MAAM,WAAW,GAAG,KAAK,CAAA;IACzB,IAAI,IAAI,GAAG,WAAW,CAAA;IACtB,OAAO,IAAI,EAAE;QACX,MAAM,IAAI,EAAE,CAAA;KACb;AACH,CAAC,CAAA;AAED,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,aAAa,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAA;AAElD,MAAM,eAAe,GAAG,KAAK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