snapshot of https://github.com/ueberdosis/hocuspocus.git @ cf3cfa355b4d3bb61c6b6c80071f71783e32470e (2026-09-15)
Some checks failed
build / lint (22) (push) Failing after 17s
build / test (22) (push) Failing after 8s
build / lint (23) (push) Failing after 2s
build / build (22) (push) Has been skipped
build / build (23) (push) Has been skipped
build / test (23) (push) Failing after 4s
docsearch / docsearch (push) Failing after 1s
Some checks failed
build / lint (22) (push) Failing after 17s
build / test (22) (push) Failing after 8s
build / lint (23) (push) Failing after 2s
build / build (22) (push) Has been skipped
build / build (23) (push) Has been skipped
build / test (23) (push) Failing after 4s
docsearch / docsearch (push) Failing after 1s
This commit is contained in:
90
tests/provider/onAuthenticationFailedRetry.ts
Normal file
90
tests/provider/onAuthenticationFailedRetry.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import test from "ava";
|
||||
import {
|
||||
newHocuspocus,
|
||||
newHocuspocusProvider,
|
||||
newHocuspocusProviderWebsocket,
|
||||
sleep,
|
||||
} from "../utils/index.ts";
|
||||
|
||||
test("provider retries auth with token function after initial failure", async (t) => {
|
||||
const docName = "superSecretDoc";
|
||||
const requiredToken = "SUPER-SECRET-TOKEN";
|
||||
|
||||
const server = await newHocuspocus(t, {
|
||||
async onAuthenticate({ token, documentName }) {
|
||||
if (documentName !== docName) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
if (token !== requiredToken) {
|
||||
throw new Error();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const socket = newHocuspocusProviderWebsocket(t, server);
|
||||
|
||||
let tokenCallCount = 0;
|
||||
|
||||
const provider = newHocuspocusProvider(t, server, {
|
||||
websocketProvider: socket,
|
||||
name: docName,
|
||||
token: () => {
|
||||
tokenCallCount++;
|
||||
return tokenCallCount === 1 ? "wrongToken" : requiredToken;
|
||||
},
|
||||
onAuthenticationFailed() {
|
||||
provider.sendToken();
|
||||
provider.startSync();
|
||||
},
|
||||
});
|
||||
|
||||
await sleep(2000);
|
||||
|
||||
t.is(tokenCallCount, 2);
|
||||
t.is(provider.isAuthenticated, true);
|
||||
});
|
||||
|
||||
test("second provider with same doc name succeeds after first fails auth", async (t) => {
|
||||
const docName = "superSecretDoc";
|
||||
const requiredToken = "SUPER-SECRET-TOKEN";
|
||||
|
||||
const server = await newHocuspocus(t, {
|
||||
async onAuthenticate({ token, documentName }) {
|
||||
if (documentName !== docName) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
if (token !== requiredToken) {
|
||||
throw new Error();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const socket = newHocuspocusProviderWebsocket(t, server);
|
||||
|
||||
const providerFail = newHocuspocusProvider(t, server, {
|
||||
websocketProvider: socket,
|
||||
token: "wrongToken",
|
||||
name: docName,
|
||||
onAuthenticated() {
|
||||
t.fail("providerFail should not authenticate");
|
||||
},
|
||||
});
|
||||
|
||||
await sleep(1000);
|
||||
|
||||
const providerOK = newHocuspocusProvider(t, server, {
|
||||
websocketProvider: socket,
|
||||
token: requiredToken,
|
||||
name: docName,
|
||||
onAuthenticationFailed() {
|
||||
t.fail("providerOK should not fail auth");
|
||||
},
|
||||
});
|
||||
|
||||
await sleep(1000);
|
||||
|
||||
t.is(providerFail.isAuthenticated, false);
|
||||
t.is(providerOK.isAuthenticated, true);
|
||||
});
|
||||
Reference in New Issue
Block a user