@jskit-ai/users-core 0.2.48 → 0.2.49
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/users-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.49",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
"./locales/en.json": "./locales/en.json"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@jskit-ai/resource-core": "0.1.
|
|
22
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
23
|
-
"@jskit-ai/storage-runtime": "0.1.
|
|
24
|
-
"@jskit-ai/uploads-runtime": "0.2.
|
|
21
|
+
"@jskit-ai/resource-core": "0.1.218",
|
|
22
|
+
"@jskit-ai/resource-crud-core": "0.1.220",
|
|
23
|
+
"@jskit-ai/storage-runtime": "0.1.273",
|
|
24
|
+
"@jskit-ai/uploads-runtime": "0.2.27",
|
|
25
25
|
"json-rest-schema": "^1.0.20"
|
|
26
26
|
},
|
|
27
27
|
"description": "Users/account runtime plus HTTP routes for account features.",
|
|
@@ -196,10 +196,10 @@
|
|
|
196
196
|
}
|
|
197
197
|
},
|
|
198
198
|
"peerDependencies": {
|
|
199
|
-
"@jskit-ai/auth-core": "0.2.
|
|
200
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
201
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
202
|
-
"@jskit-ai/kernel": "0.2.
|
|
203
|
-
"@jskit-ai/resource-runtime": "0.2.
|
|
199
|
+
"@jskit-ai/auth-core": "0.2.30",
|
|
200
|
+
"@jskit-ai/database-runtime": "0.1.276",
|
|
201
|
+
"@jskit-ai/http-runtime": "0.2.27",
|
|
202
|
+
"@jskit-ai/kernel": "0.2.27",
|
|
203
|
+
"@jskit-ai/resource-runtime": "0.2.37"
|
|
204
204
|
}
|
|
205
205
|
}
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
"./shared": "./src/shared/index.js"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@jskit-ai/crud-core": "0.2.
|
|
14
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
15
|
-
"@jskit-ai/users-core": "0.2.
|
|
13
|
+
"@jskit-ai/crud-core": "0.2.41",
|
|
14
|
+
"@jskit-ai/resource-crud-core": "0.1.220",
|
|
15
|
+
"@jskit-ai/users-core": "0.2.49"
|
|
16
16
|
},
|
|
17
17
|
"jskit": {
|
|
18
18
|
"kind": "runtime",
|
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
"./shared": "./src/shared/index.js"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@jskit-ai/crud-core": "0.2.
|
|
14
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
15
|
-
"@jskit-ai/users-core": "0.2.
|
|
16
|
-
"@jskit-ai/workspaces-core": "0.2.
|
|
13
|
+
"@jskit-ai/crud-core": "0.2.41",
|
|
14
|
+
"@jskit-ai/resource-crud-core": "0.1.220",
|
|
15
|
+
"@jskit-ai/users-core": "0.2.49",
|
|
16
|
+
"@jskit-ai/workspaces-core": "0.2.49"
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"@jskit-ai/kernel": "0.2.
|
|
20
|
-
"@jskit-ai/resource-runtime": "0.2.
|
|
19
|
+
"@jskit-ai/kernel": "0.2.27",
|
|
20
|
+
"@jskit-ai/resource-runtime": "0.2.37"
|
|
21
21
|
},
|
|
22
22
|
"jskit": {
|
|
23
23
|
"kind": "runtime",
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
2
3
|
import test from "node:test";
|
|
3
4
|
import Fastify from "fastify";
|
|
4
5
|
import { createActionCatalogue } from "@jskit-ai/kernel/server/actions";
|
|
6
|
+
import { createCapabilityRuntime, defineProvider } from "@jskit-ai/kernel/shared/capabilities";
|
|
7
|
+
import { StorageProvider } from "@jskit-ai/storage-runtime/server/providers/StorageProvider";
|
|
5
8
|
import { readSingleMultipartFile } from "@jskit-ai/uploads-runtime/server/multipart/readSingleMultipartFile";
|
|
6
9
|
import { registerMultipartSupport } from "@jskit-ai/uploads-runtime/server/multipart/registerMultipartSupport";
|
|
7
10
|
import { createRouter } from "../../kernel/server/http/lib/router.js";
|
|
8
11
|
import { registerRoutes } from "../../kernel/server/http/lib/routeRegistration.js";
|
|
9
12
|
import { registerApiErrorHandler } from "../../kernel/server/http/errorHandling.js";
|
|
13
|
+
import { startS3StandIn } from "../../storage-runtime/test/support/s3StandIn.js";
|
|
10
14
|
import { createAccountProfileActions } from "../src/server/accountProfile/accountProfileActions.js";
|
|
11
15
|
import { registerAccountProfileRoutes } from "../src/server/accountProfile/accountProfileRoutes.js";
|
|
12
16
|
import { createService as createAccountProfileService } from "../src/server/accountProfile/accountProfileService.js";
|
|
@@ -15,11 +19,18 @@ import { createService as createAvatarStorageService } from "../src/server/accou
|
|
|
15
19
|
import { DEFAULT_USER_SETTINGS } from "../src/shared/settings.js";
|
|
16
20
|
|
|
17
21
|
const PNG_BYTES = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d]);
|
|
22
|
+
const S3_BUCKET = Object.freeze({
|
|
23
|
+
bucket: "jskit-files",
|
|
24
|
+
region: "eu-west-1",
|
|
25
|
+
accessKeyId: "AKIASTANDIN",
|
|
26
|
+
secretAccessKey: `stand-in-secret-${randomUUID()}`
|
|
27
|
+
});
|
|
18
28
|
|
|
19
29
|
// The account profile as an application serves it: users-core's profile, avatar and avatar storage services, its
|
|
20
|
-
// actions and its routes on Fastify with multipart support, with the profile
|
|
21
|
-
// memory, and the signed-in user 7.
|
|
22
|
-
|
|
30
|
+
// actions and its routes on Fastify with multipart support, with the profile and the settings in memory,
|
|
31
|
+
// runtime.storage in memory unless the test gives one, and the signed-in user 7. The app logs to `logStream` when
|
|
32
|
+
// one is given.
|
|
33
|
+
async function profileApp(t, { storage, logStream } = {}) {
|
|
23
34
|
const storedFiles = new Map();
|
|
24
35
|
let profile = { id: 7, displayName: "Ada", email: "ada@example.test", avatarStorageKey: null, avatarVersion: null };
|
|
25
36
|
const userProfilesRepository = {
|
|
@@ -29,12 +40,16 @@ async function profileApp(t) {
|
|
|
29
40
|
async updateAvatarById(_userId, { avatarStorageKey, avatarVersion }) {
|
|
30
41
|
profile = { ...profile, avatarStorageKey, avatarVersion };
|
|
31
42
|
return { ...profile };
|
|
43
|
+
},
|
|
44
|
+
async clearAvatarById() {
|
|
45
|
+
profile = { ...profile, avatarStorageKey: null, avatarVersion: null };
|
|
46
|
+
return { ...profile };
|
|
32
47
|
}
|
|
33
48
|
};
|
|
34
49
|
const avatarService = createAvatarService({
|
|
35
50
|
userProfilesRepository,
|
|
36
51
|
avatarStorageService: createAvatarStorageService({
|
|
37
|
-
storage: {
|
|
52
|
+
storage: storage ?? {
|
|
38
53
|
async getItemRaw(key) {
|
|
39
54
|
return storedFiles.get(key) ?? null;
|
|
40
55
|
},
|
|
@@ -65,18 +80,58 @@ async function profileApp(t) {
|
|
|
65
80
|
const router = createRouter();
|
|
66
81
|
registerAccountProfileRoutes(router, { accountProfileService, authService, uploads: { readSingleMultipartFile } });
|
|
67
82
|
|
|
68
|
-
const app = Fastify();
|
|
83
|
+
const app = Fastify(logStream ? { logger: { level: "info", stream: logStream } } : {});
|
|
69
84
|
t.after(() => app.close());
|
|
85
|
+
// Routes that run no action read the signed-in user from the request, as authentication sets it.
|
|
86
|
+
app.decorateRequest("user", null);
|
|
87
|
+
app.addHook("onRequest", async (request) => {
|
|
88
|
+
request.user = { id: "7" };
|
|
89
|
+
});
|
|
70
90
|
await registerMultipartSupport(app);
|
|
71
91
|
registerApiErrorHandler(app);
|
|
72
92
|
registerRoutes(app, { routes: router.list(), actions });
|
|
73
93
|
return { app, storedFiles };
|
|
74
94
|
}
|
|
75
95
|
|
|
76
|
-
|
|
77
|
-
|
|
96
|
+
// runtime.storage as an application's features receive it, from StorageProvider on JSKIT_STORAGE_DRIVER=s3 with
|
|
97
|
+
// these settings.
|
|
98
|
+
async function s3RuntimeStorage(t, settings) {
|
|
99
|
+
let storage;
|
|
100
|
+
const consumer = defineProvider({
|
|
101
|
+
id: "test.storage.consumer",
|
|
102
|
+
requires: { value: "runtime.storage" },
|
|
103
|
+
setup({ value }) {
|
|
104
|
+
storage = value;
|
|
105
|
+
return {};
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
const runtime = createCapabilityRuntime({
|
|
109
|
+
inputs: {
|
|
110
|
+
"runtime.app-root": process.cwd(),
|
|
111
|
+
"runtime.env": {
|
|
112
|
+
JSKIT_STORAGE_DRIVER: "s3",
|
|
113
|
+
JSKIT_STORAGE_S3_BUCKET: settings.bucket,
|
|
114
|
+
JSKIT_STORAGE_S3_REGION: settings.region,
|
|
115
|
+
JSKIT_STORAGE_S3_ENDPOINT: settings.endpoint,
|
|
116
|
+
JSKIT_STORAGE_S3_ACCESS_KEY_ID: settings.accessKeyId,
|
|
117
|
+
JSKIT_STORAGE_S3_SECRET_ACCESS_KEY: settings.secretAccessKey
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
providers: [StorageProvider, consumer]
|
|
121
|
+
});
|
|
122
|
+
await runtime.start();
|
|
123
|
+
t.after(() => runtime.shutdown());
|
|
124
|
+
return storage;
|
|
125
|
+
}
|
|
78
126
|
|
|
79
|
-
|
|
127
|
+
async function startBucket(t, credentials) {
|
|
128
|
+
const bucket = await startS3StandIn(credentials);
|
|
129
|
+
t.after(() => bucket.close());
|
|
130
|
+
return bucket;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function avatarUpload() {
|
|
134
|
+
return {
|
|
80
135
|
method: "POST",
|
|
81
136
|
url: "/api/settings/profile/avatar",
|
|
82
137
|
headers: { "content-type": "multipart/form-data; boundary=jskit-avatar" },
|
|
@@ -87,7 +142,13 @@ test("saving a profile picture answers 200 with the account's plain settings", a
|
|
|
87
142
|
PNG_BYTES,
|
|
88
143
|
Buffer.from("\r\n--jskit-avatar--\r\n")
|
|
89
144
|
])
|
|
90
|
-
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
test("saving a profile picture answers 200 with the account's plain settings", async (t) => {
|
|
149
|
+
const { app, storedFiles } = await profileApp(t);
|
|
150
|
+
|
|
151
|
+
const response = await app.inject(avatarUpload());
|
|
91
152
|
|
|
92
153
|
// Before, the route sent the action's JSON:API result where its schema and the upload read the plain settings:
|
|
93
154
|
// the picture was saved, and serializing the answer failed with a 500.
|
|
@@ -100,3 +161,44 @@ test("saving a profile picture answers 200 with the account's plain settings", a
|
|
|
100
161
|
assert.match(settings.profile.avatar.effectiveUrl, /^\/api\/settings\/profile\/avatar\?v=\d+$/u);
|
|
101
162
|
assert.equal(settings.preferences.avatarSize, DEFAULT_USER_SETTINGS.avatarSize);
|
|
102
163
|
});
|
|
164
|
+
|
|
165
|
+
test("on JSKIT_STORAGE_DRIVER=s3 a profile picture is saved to, served from and removed from the bucket", async (t) => {
|
|
166
|
+
const bucket = await startBucket(t, S3_BUCKET);
|
|
167
|
+
const storage = await s3RuntimeStorage(t, { ...S3_BUCKET, endpoint: bucket.endpoint });
|
|
168
|
+
const { app } = await profileApp(t, { storage });
|
|
169
|
+
|
|
170
|
+
const uploaded = await app.inject(avatarUpload());
|
|
171
|
+
assert.equal(uploaded.statusCode, 200, uploaded.body);
|
|
172
|
+
assert.equal(uploaded.json().profile.avatar.hasUploadedAvatar, true);
|
|
173
|
+
assert.deepEqual([...bucket.objects.keys()], ["users/avatars/7/avatar"]);
|
|
174
|
+
assert.deepEqual(bucket.objects.get("users/avatars/7/avatar"), PNG_BYTES);
|
|
175
|
+
|
|
176
|
+
const served = await app.inject({ method: "GET", url: "/api/settings/profile/avatar" });
|
|
177
|
+
assert.equal(served.statusCode, 200, served.body);
|
|
178
|
+
assert.equal(served.headers["content-type"], "image/png");
|
|
179
|
+
assert.deepEqual(served.rawPayload, PNG_BYTES);
|
|
180
|
+
|
|
181
|
+
const removed = await app.inject({ method: "DELETE", url: "/api/settings/profile/avatar" });
|
|
182
|
+
assert.equal(removed.statusCode, 200, removed.body);
|
|
183
|
+
assert.equal(bucket.objects.size, 0);
|
|
184
|
+
assert.equal((await app.inject({ method: "GET", url: "/api/settings/profile/avatar" })).statusCode, 404);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("a bucket that refuses a profile picture logs S3's answer and never the storage secret", async (t) => {
|
|
188
|
+
const bucket = await startBucket(t, { ...S3_BUCKET, secretAccessKey: "the-bucket-holds-another-secret" });
|
|
189
|
+
const storage = await s3RuntimeStorage(t, { ...S3_BUCKET, endpoint: bucket.endpoint });
|
|
190
|
+
const logLines = [];
|
|
191
|
+
const { app } = await profileApp(t, { storage, logStream: { write: (line) => logLines.push(line) } });
|
|
192
|
+
// An application that logs its storage at startup, as a diagnostic.
|
|
193
|
+
app.log.info({ mounts: storage.getMounts() }, "storage mounts");
|
|
194
|
+
|
|
195
|
+
const response = await app.inject(avatarUpload());
|
|
196
|
+
|
|
197
|
+
assert.equal(response.statusCode, 500, response.body);
|
|
198
|
+
assert.equal(bucket.objects.size, 0);
|
|
199
|
+
const log = logLines.join("");
|
|
200
|
+
assert.match(log, /storage mounts/u);
|
|
201
|
+
assert.match(log, /SignatureDoesNotMatch/u);
|
|
202
|
+
assert.equal(log.includes(S3_BUCKET.secretAccessKey), false);
|
|
203
|
+
assert.equal(response.body.includes(S3_BUCKET.secretAccessKey), false);
|
|
204
|
+
});
|