This repository was archived by the owner on Feb 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathopenProjectApi.test.ts
More file actions
253 lines (211 loc) · 8.13 KB
/
openProjectApi.test.ts
File metadata and controls
253 lines (211 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { Document, onAuthenticatePayload, onLoadDocumentPayload, onStoreDocumentPayload } from "@hocuspocus/server";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import * as Y from "yjs";
import { OpenProjectApi, createEditor } from "../../src/extensions/openProjectApi";
describe("OpenProjectApi", () => {
let fetchMock: any;
beforeEach(() => {
fetchMock = vi.fn();
vi.stubGlobal('fetch', fetchMock);
});
afterEach(() => {
vi.unstubAllGlobals();
});
describe("onAuthenticate", () => {
test("when the token is not present throw an error", async () => {
await expect(() =>
new OpenProjectApi().onAuthenticate({
token: null,
} as unknown as onAuthenticatePayload)
).rejects.toThrowError("Unauthorized: Token missing.");
});
test("when the token is invalid throw an error", async () => {
await expect(() =>
new OpenProjectApi().onAuthenticate({
// Invalid token, generated with a different secret
token: "5Sm4blMLhP8PFS67xw==--br8L/7YDX3rbTLpT--HHEi+SnNdmHmH90N3mHY9A==",
} as unknown as onAuthenticatePayload)
).rejects.toThrowError("Unsupported state or unable to authenticate data");
});
test("when the resourceUrl has invalid format throw an error", async () => {
fetchMock.mockResolvedValueOnce({ throws: new TypeError("is not a valid URL") });
await expect(() =>
new OpenProjectApi().onAuthenticate({
token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==",
documentName: "not a valid url",
} as unknown as onAuthenticatePayload)
).rejects.toThrowError("Unauthorized: Invalid token or document access denied.");
});
test("when the server does not authorize the request throw an error", async () => {
fetchMock.mockResolvedValueOnce({
ok: false,
status: 401,
});
await expect(() =>
new OpenProjectApi().onAuthenticate({
token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==",
documentName: "https://test.api/api/v3/documents/121",
} as unknown as onAuthenticatePayload)
).rejects.toThrowError("Unauthorized: Invalid token or document access denied.");
expect(fetchMock).toHaveBeenCalledWith(
"https://test.api/api/v3/documents/121",
{
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer valid_token",
},
}
);
});
test("when the token is valid set the context", async () => {
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
json: () => Promise.resolve({}),
});
const data = {
context: {},
connectionConfig: {},
token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==",
documentName: "https://test.api/api/v3/documents/121",
} as unknown as onAuthenticatePayload;
await new OpenProjectApi().onAuthenticate(data);
expect(data.context.resourceUrl).toEqual("https://test.api/api/v3/documents/121");
expect(data.context.token).toEqual("valid_token");
expect(data.documentName).toEqual("https://test.api/api/v3/documents/121");
});
test("when there is no update link, setup the connection as readonly", async () => {
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
json: () => Promise.resolve({
_links: {
self: { href: "/api/v3/documents/121" }
}
}),
});
const data = {
context: {},
connectionConfig: {},
token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==",
documentName: "https://test.api/api/v3/documents/121",
} as unknown as onAuthenticatePayload;
await new OpenProjectApi().onAuthenticate(data);
expect(data.connectionConfig.readOnly).toBe(true);
expect(data.context.readonly).toBe(true);
});
test("when there is an update link, setup the connection as writable", async () => {
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
json: () => Promise.resolve({
title: "TheDocName",
_links: {
self: { href: "/api/v3/documents/121" },
update: { href: "/api/v3/documents/121" }
}
}),
});
const data = {
context: {},
connectionConfig: {},
token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==",
documentName: "https://test.api/api/v3/documents/121",
} as unknown as onAuthenticatePayload;
await new OpenProjectApi().onAuthenticate(data);
expect(data.connectionConfig.readOnly).toBeUndefined();
expect(data.context.readonly).toBeUndefined();
});
});
describe("onLoadDocument", () => {
test("should fetch document content and apply update to YDoc", async () => {
// Create a valid YJS update by encoding state from a document with content
const sourceDoc = new Y.Doc();
const text = sourceDoc.getText('content');
text.insert(0, 'test content');
const base64Update = Buffer.from(Y.encodeStateAsUpdate(sourceDoc)).toString('base64');
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
json: () => Promise.resolve({ contentBinary: base64Update }),
});
const targetDoc = new Y.Doc();
const data = {
context: { token: "superValidToken", resourceUrl: "https://test.api/api/v3/documents/121" },
document: targetDoc,
} as onLoadDocumentPayload;
const api = new OpenProjectApi();
await api.onLoadDocument(data);
expect(fetchMock).toHaveBeenCalledWith(
"https://test.api/api/v3/documents/121",
{
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer superValidToken",
},
}
);
// Verify the document was updated with the content
const updatedContent = targetDoc.getText('content').toString();
expect(updatedContent).toBe('test content');
});
test("should return early when response is not successful", async () => {
fetchMock.mockResolvedValueOnce({
ok: false,
status: 404,
});
const data = {
context: { token: "superValidToken", resourceUrl: "https://test.api/api/v3/documents/121" },
document: new Y.Doc(),
} as onLoadDocumentPayload;
const initialContent = data.document.getText('content').toString();
const api = new OpenProjectApi();
await api.onLoadDocument(data);
expect(fetchMock).toHaveBeenCalled();
const updatedContent = data.document.getText('content').toString();
expect(updatedContent).toBe(initialContent);
});
});
describe("onStoreDocument", () => {
test("should store document content successfully", async () => {
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
});
const editor = createEditor();
const blocks = [
{
type: "paragraph",
content: "test document content"
}
];
const document = new Y.Doc();
const fragment = document.getXmlFragment('document-store');
// @ts-expect-error BlockNote types are complicated
editor.blocksToYXmlFragment(blocks, fragment);
const data = {
context: {
token: "superValidToken",
resourceUrl: "https://test.api/api/v3/documents/121",
readonly: false,
},
document: { ...document, connections: [] } as unknown as Document,
} as onStoreDocumentPayload;
const api = new OpenProjectApi();
await api.onStoreDocument(data);
expect(fetchMock).toHaveBeenCalledWith(
"https://test.api/api/v3/documents/121",
expect.objectContaining({
method: "PATCH",
headers: {
"Content-Type": "application/json",
"Authorization": expect.stringContaining("Bearer"),
},
body: expect.stringContaining("content_binary"),
})
);
});
});
});