From 492c6797f9c200f9857c5924bec2f3eb3f38f0b8 Mon Sep 17 00:00:00 2001 From: Rinse Date: Tue, 29 Oct 2024 14:54:48 +0000 Subject: [PATCH] test challengerequest event instead of private props --- ...backward.compatibility.commentedit.test.js | 23 +++++-- .../backward.compatibility.test.js | 29 ++++++--- .../backward.compatibility.comment.test.js | 25 ++++++-- ...ckward.compatibility.commentupdate.test.js | 5 +- .../comment/publish.verification.test.js | 4 +- .../vote/backward.compatibility.vote.test.js | 25 ++++++-- .../signatures/pubsub.messages.test.js | 63 ++++++++----------- 7 files changed, 111 insertions(+), 63 deletions(-) diff --git a/test/node-and-browser/publications/comment-edit/backward.compatibility.commentedit.test.js b/test/node-and-browser/publications/comment-edit/backward.compatibility.commentedit.test.js index 692a8933..5d948e41 100644 --- a/test/node-and-browser/publications/comment-edit/backward.compatibility.commentedit.test.js +++ b/test/node-and-browser/publications/comment-edit/backward.compatibility.commentedit.test.js @@ -44,12 +44,14 @@ getRemotePlebbitConfigs().map((config) => { }); await setExtraPropOnCommentEditAndSign(commentEdit, { extraProp: "1234" }, false); + const challengeRequestPromise = new Promise((resolve) => commentEdit.once("challengerequest", resolve)); await publishWithExpectedResult( commentEdit, false, messages.ERR_COMMENT_EDIT_RECORD_INCLUDES_FIELD_NOT_IN_SIGNED_PROPERTY_NAMES ); - expect(commentEdit._publishedChallengeRequests[0].commentEdit.extraProp).to.equal("1234"); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.commentEdit.extraProp).to.equal("1234"); }); it(`publishing commentEdit.extraProp should succeed as an author edit if it's included in commentEdit.signature.signedPropertyNames`, async () => { @@ -61,13 +63,16 @@ getRemotePlebbitConfigs().map((config) => { }); await setExtraPropOnCommentEditAndSign(commentEdit, { extraProp: "1234" }, true); + const challengeRequestPromise = new Promise((resolve) => commentEdit.once("challengerequest", resolve)); + await publishWithExpectedResult(commentEdit, true); await new Promise((resolve) => commentToEdit.once("update", resolve)); // if commentToEdit emits update that means the signature of update.edit is correct expect(commentToEdit.content).to.equal(commentEdit.content); // should process only content since it's the known field expect(commentToEdit.extraProp).to.be.undefined; - expect(commentEdit._publishedChallengeRequests[0].commentEdit.extraProp).to.equal("1234"); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.commentEdit.extraProp).to.equal("1234"); await plebbit.createCommentEdit(JSON.parse(JSON.stringify(commentEdit))); // Just to test if create will throw because of extra prop }); @@ -80,8 +85,10 @@ getRemotePlebbitConfigs().map((config) => { }); await setExtraPropOnCommentEditAndSign(commentEdit, { insertedAt: "1234" }, true); + const challengeRequestPromise = new Promise((resolve) => commentEdit.once("challengerequest", resolve)); await publishWithExpectedResult(commentEdit, false, messages.ERR_COMMENT_EDIT_HAS_RESERVED_FIELD); - expect(commentEdit._publishedChallengeRequests[0].commentEdit.insertedAt).to.equal("1234"); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.commentEdit.insertedAt).to.equal("1234"); }); describe(`Publishing CommentEdit with extra props in author field - ${config.name}`, async () => { @@ -99,8 +106,11 @@ getRemotePlebbitConfigs().map((config) => { true ); + const challengeRequestPromise = new Promise((resolve) => commentEdit.once("challengerequest", resolve)); + await publishWithExpectedResult(commentEdit, false, messages.ERR_PUBLICATION_AUTHOR_HAS_RESERVED_FIELD); - expect(commentEdit._publishedChallengeRequests[0].commentEdit.author.subplebbit).to.equal("random"); + const challengerequest = await challengeRequestPromise; + expect(challengerequest.commentEdit.author.subplebbit).to.equal("random"); }); it(`Publishing with extra prop for author should succeed`, async () => { const commentEdit = await plebbit.createCommentEdit({ @@ -118,8 +128,11 @@ getRemotePlebbitConfigs().map((config) => { await plebbit.createCommentEdit(JSON.parse(JSON.stringify(commentEdit))); // Just to test if create will throw because of extra prop + const challengeRequestPromise = new Promise((resolve) => commentEdit.once("challengerequest", resolve)); + await publishWithExpectedResult(commentEdit, true); - expect(commentEdit._publishedChallengeRequests[0].commentEdit.author.extraProp).to.equal(extraProps.extraProp); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.commentEdit.author.extraProp).to.equal(extraProps.extraProp); }); }); }); diff --git a/test/node-and-browser/publications/comment-moderation/backward.compatibility.test.js b/test/node-and-browser/publications/comment-moderation/backward.compatibility.test.js index 74d24208..74013c86 100644 --- a/test/node-and-browser/publications/comment-moderation/backward.compatibility.test.js +++ b/test/node-and-browser/publications/comment-moderation/backward.compatibility.test.js @@ -44,12 +44,14 @@ getRemotePlebbitConfigs().map((config) => { }); await setExtraPropOnCommentModerationAndSign(commentModeration, { extraProp: "1234" }, false); + const challengeRequestPromise = new Promise((resolve) => commentModeration.once("challengerequest", resolve)); await publishWithExpectedResult( commentModeration, false, messages.ERR_COMMENT_MODERATION_RECORD_INCLUDES_FIELD_NOT_IN_SIGNED_PROPERTY_NAMES ); - expect(commentModeration._publishedChallengeRequests[0].commentModeration.extraProp).to.equal("1234"); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.commentModeration.extraProp).to.equal("1234"); }); it(`publishing commentModeration.extraProp should succeed if it's included in commentModeration.signature.signedPropertyNames`, async () => { @@ -61,12 +63,15 @@ getRemotePlebbitConfigs().map((config) => { }); await setExtraPropOnCommentModerationAndSign(commentModeration, { extraProp: "1234" }, true); + const challengeRequestPromise = new Promise((resolve) => commentModeration.once("challengerequest", resolve)); + await publishWithExpectedResult(commentModeration, true); await new Promise((resolve) => commentToMod.once("update", resolve)); expect(commentToMod.removed).to.be.true; // should process only removed since it's the known field to the sub expect(commentToMod.extraProp).to.be.undefined; - expect(commentModeration._publishedChallengeRequests[0].commentModeration.extraProp).to.equal("1234"); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.commentModeration.extraProp).to.equal("1234"); await plebbit.createCommentModeration(JSON.parse(JSON.stringify(commentModeration))); // Just to test if create will throw because of extra prop }); @@ -80,9 +85,12 @@ getRemotePlebbitConfigs().map((config) => { await setExtraPropOnCommentModerationAndSign(commentModeration, { commentModeration: { extraProp: "1234" } }, true); + const challengeRequestPromise = new Promise((resolve) => commentModeration.once("challengerequest", resolve)); + await publishWithExpectedResult(commentModeration, true); - expect(commentModeration._publishedChallengeRequests[0].commentModeration.commentModeration.extraProp).to.equal("1234"); - expect(commentModeration._publishedChallengeRequests[0].commentModeration.commentModeration.locked).to.be.true; + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.commentModeration.commentModeration.extraProp).to.equal("1234"); + expect(challengeRequest.commentModeration.commentModeration.locked).to.be.true; await new Promise((resolve) => commentToMod.once("update", resolve)); // if commentToEdit emits update that means the signature of update.edit is correct @@ -101,8 +109,10 @@ getRemotePlebbitConfigs().map((config) => { }); await setExtraPropOnCommentModerationAndSign(commentModeration, { insertedAt: "1234" }, true); + const challengeRequestPromise = new Promise((resolve) => commentModeration.once("challengerequest", resolve)); await publishWithExpectedResult(commentModeration, false, messages.ERR_COMMENT_MODERATION_HAS_RESERVED_FIELD); - expect(commentModeration._publishedChallengeRequests[0].commentModeration.insertedAt).to.equal("1234"); + const challengerequest = await challengeRequestPromise; + expect(challengerequest.commentModeration.insertedAt).to.equal("1234"); }); describe(`Publishing CommentModeration with extra props in commentModerationPublication.author field - ${config.name}`, async () => { @@ -119,8 +129,10 @@ getRemotePlebbitConfigs().map((config) => { await plebbit.createCommentModeration(JSON.parse(JSON.stringify(commentModeration))); // Just to test if create will throw because of extra prop + const challengeRequestPromise = new Promise((resolve) => commentModeration.once("challengerequest", resolve)); await publishWithExpectedResult(commentModeration, true); - expect(commentModeration._publishedChallengeRequests[0].commentModeration.author.extraProp).to.equal(extraProps.extraProp); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.commentModeration.author.extraProp).to.equal(extraProps.extraProp); }); }); @@ -138,8 +150,11 @@ getRemotePlebbitConfigs().map((config) => { await plebbit.createCommentModeration(JSON.parse(JSON.stringify(commentModeration))); // Just to test if create will throw because of extra prop + const challengeRequestPromise = new Promise((resolve) => commentModeration.once("challengerequest", resolve)); + await publishWithExpectedResult(commentModeration, true); - expect(commentModeration._publishedChallengeRequests[0].commentModeration.author.extraProp).to.equal(extraProps.extraProp); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.commentModeration.author.extraProp).to.equal(extraProps.extraProp); await commentToModWithAuthor.update(); await resolveWhenConditionIsTrue(commentToModWithAuthor, () => commentToModWithAuthor.removed === true); diff --git a/test/node-and-browser/publications/comment/backward.compatibility.comment.test.js b/test/node-and-browser/publications/comment/backward.compatibility.comment.test.js index c0b2d210..14f8f145 100644 --- a/test/node-and-browser/publications/comment/backward.compatibility.comment.test.js +++ b/test/node-and-browser/publications/comment/backward.compatibility.comment.test.js @@ -59,8 +59,11 @@ getRemotePlebbitConfigs().map((config) => { const extraProps = { cid: "1234" }; await setExtraPropOnCommentAndSign(post, extraProps, true); + const challengeRequestPromise = new Promise((resolve) => post.once("challengerequest", resolve)); + await publishWithExpectedResult(post, false, messages.ERR_COMMENT_HAS_RESERVED_FIELD); - expect(post._publishedChallengeRequests[0].comment.cid).to.equal(extraProps.cid); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.comment.cid).to.equal(extraProps.cid); }); it(`A CommentPubsub with an extra field included in signature.signedPropertyNames will be accepted`, async () => { @@ -68,10 +71,14 @@ getRemotePlebbitConfigs().map((config) => { const extraProps = { extraProp: "1234" }; await setExtraPropOnCommentAndSign(post, extraProps, true); - let challengeVerification; - post.once("challengeverification", (_verification) => (challengeVerification = _verification)); + const challengeVerificationPromise = new Promise((resolve) => post.once("challengeverification", resolve)); + + const challengeRequestPromise = new Promise((resolve) => post.once("challengerequest", resolve)); + await publishWithExpectedResult(post, true); - expect(post._publishedChallengeRequests[0].comment.extraProp).to.equal(extraProps.extraProp); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.comment.extraProp).to.equal(extraProps.extraProp); + const challengeVerification = await challengeVerificationPromise; expect(challengeVerification.comment.extraProp).to.equal(extraProps.extraProp); expect(post.extraProp).to.equal(extraProps.extraProp); }); @@ -128,16 +135,22 @@ getRemotePlebbitConfigs().map((config) => { const post = await generateMockPost(subplebbitAddress, plebbit); await setExtraPropOnCommentAndSign(post, { author: { ...post._pubsubMsgToPublish.author, subplebbit: "random" } }, true); + const challengeRequestPromise = new Promise((resolve) => post.once("challengerequest", resolve)); + await publishWithExpectedResult(post, false, messages.ERR_PUBLICATION_AUTHOR_HAS_RESERVED_FIELD); - expect(post._publishedChallengeRequests[0].comment.author.subplebbit).to.equal("random"); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.comment.author.subplebbit).to.equal("random"); }); it(`Publishing with extra prop for author should succeed`, async () => { const post = await generateMockPost(subplebbitAddress, plebbit); const extraProps = { extraProp: "1234" }; await setExtraPropOnCommentAndSign(post, { author: { ...post._pubsubMsgToPublish.author, ...extraProps } }, true); + const challengeRequestPromise = new Promise((resolve) => post.once("challengerequest", resolve)); + await publishWithExpectedResult(post, true); - expect(post._publishedChallengeRequests[0].comment.author.extraProp).to.equal(extraProps.extraProp); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.comment.author.extraProp).to.equal(extraProps.extraProp); expect(post.author.extraProp).to.equal(extraProps.extraProp); }); }); diff --git a/test/node-and-browser/publications/comment/backward.compatibility.commentupdate.test.js b/test/node-and-browser/publications/comment/backward.compatibility.commentupdate.test.js index aadfbd69..aaba08a5 100644 --- a/test/node-and-browser/publications/comment/backward.compatibility.commentupdate.test.js +++ b/test/node-and-browser/publications/comment/backward.compatibility.commentupdate.test.js @@ -71,13 +71,12 @@ getRemotePlebbitConfigs().map((config) => { mockPostToFetchSpecificCommentUpdateCid(postToUpdate, invalidCommentUpdateCid); // should emit an error because we did not include extraProp in signedPropertyNames - let error; - postToUpdate.once("error", (_err) => (error = _err)); + const errorPromise = new Promise((resolve) => postToUpdate.once("error", resolve)); await postToUpdate.update(); - await new Promise((resolve) => setTimeout(resolve, plebbit.updateInterval * 2)); + const error = await errorPromise; await postToUpdate.stop(); diff --git a/test/node-and-browser/publications/comment/publish.verification.test.js b/test/node-and-browser/publications/comment/publish.verification.test.js index 82d6ae31..6f8437ae 100644 --- a/test/node-and-browser/publications/comment/publish.verification.test.js +++ b/test/node-and-browser/publications/comment/publish.verification.test.js @@ -150,8 +150,10 @@ describe("Subplebbit rejection of incorrect values of fields", async () => { const reply = await generateMockComment(post, plebbit, false); await setExtraPropOnCommentAndSign(reply, { postCid: undefined }, true); expect(reply.postCid).to.be.undefined; + const challengerequestPromise = new Promise((resolve) => reply.once("challengerequest", resolve)); await publishWithExpectedResult(reply, false, messages.ERR_REPLY_HAS_NOT_DEFINED_POST_CID); - expect(reply._publishedChallengeRequests[0].comment.postCid).to.be.undefined; + const challengeRequest = await challengerequestPromise; + expect(challengeRequest.comment.postCid).to.be.undefined; }); }); diff --git a/test/node-and-browser/publications/vote/backward.compatibility.vote.test.js b/test/node-and-browser/publications/vote/backward.compatibility.vote.test.js index 775371a8..97f62a7e 100644 --- a/test/node-and-browser/publications/vote/backward.compatibility.vote.test.js +++ b/test/node-and-browser/publications/vote/backward.compatibility.vote.test.js @@ -34,24 +34,33 @@ getRemotePlebbitConfigs().map((config) => { await setExtraPropOnVoteAndSign(vote, { extraProp: "1234" }, false); // will include extra prop in request.vote, but not in signedPropertyNames await plebbit.createVote(JSON.parse(JSON.stringify(vote))); // attempt to create just to see if createVote will throw due to extra prop + const challengeRequestPromise = new Promise((resolve) => vote.once("challengerequest", resolve)); + await publishWithExpectedResult(vote, false, messages.ERR_VOTE_RECORD_INCLUDES_FIELD_NOT_IN_SIGNED_PROPERTY_NAMES); - expect(vote._publishedChallengeRequests[0].vote.extraProp).to.equal("1234"); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.vote.extraProp).to.equal("1234"); }); it(`publishing vote.extraProp should succeed if it's included in vote.signature.signedPropertyNames`, async () => { const vote = await generateMockVote(commentToVoteOn, 1, plebbit); await setExtraPropOnVoteAndSign(vote, { extraProp: "1234" }, true); // will include extra prop in request.vote, and signedPropertyNames + const challengeRequestPromise = new Promise((resolve) => vote.once("challengerequest", resolve)); + await publishWithExpectedResult(vote, true); - expect(vote._publishedChallengeRequests[0].vote.extraProp).to.equal("1234"); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.vote.extraProp).to.equal("1234"); }); it(`Publishing vote.reservedField should be rejected`, async () => { const vote = await generateMockVote(commentToVoteOn, 1, plebbit); await setExtraPropOnVoteAndSign(vote, { insertedAt: "1234" }, true); + const challengeRequestPromise = new Promise((resolve) => vote.once("challengerequest", resolve)); + await publishWithExpectedResult(vote, false, messages.ERR_VOTE_HAS_RESERVED_FIELD); - expect(vote._publishedChallengeRequests[0].vote.insertedAt).to.equal("1234"); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.vote.insertedAt).to.equal("1234"); }); describe(`Publishing vote with extra props in author field - ${config.name}`, async () => { @@ -59,8 +68,11 @@ getRemotePlebbitConfigs().map((config) => { const vote = await generateMockVote(commentToVoteOn, 1, plebbit); await setExtraPropOnVoteAndSign(vote, { author: { ...vote._pubsubMsgToPublish.author, subplebbit: "random" } }, true); + const challengeRequestPromise = new Promise((resolve) => vote.once("challengerequest", resolve)); + await publishWithExpectedResult(vote, false, messages.ERR_PUBLICATION_AUTHOR_HAS_RESERVED_FIELD); - expect(vote._publishedChallengeRequests[0].vote.author.subplebbit).to.equal("random"); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.vote.author.subplebbit).to.equal("random"); }); it(`Publishing with extra prop for author should succeed`, async () => { const vote = await generateMockVote(commentToVoteOn, 1, plebbit); @@ -68,8 +80,11 @@ getRemotePlebbitConfigs().map((config) => { await setExtraPropOnVoteAndSign(vote, { author: { ...vote._pubsubMsgToPublish.author, ...extraProps } }, true); await plebbit.createVote(JSON.parse(JSON.stringify(vote))); // attempt to create just to see if createVote will throw due to extra prop + const challengeRequestPromise = new Promise((resolve) => vote.once("challengerequest", resolve)); + await publishWithExpectedResult(vote, true); - expect(vote._publishedChallengeRequests[0].vote.author.extraProp).to.equal(extraProps.extraProp); + const challengeRequest = await challengeRequestPromise; + expect(challengeRequest.vote.author.extraProp).to.equal(extraProps.extraProp); }); }); }); diff --git a/test/node-and-browser/signatures/pubsub.messages.test.js b/test/node-and-browser/signatures/pubsub.messages.test.js index b4e6ba7b..7a3c5e13 100644 --- a/test/node-and-browser/signatures/pubsub.messages.test.js +++ b/test/node-and-browser/signatures/pubsub.messages.test.js @@ -57,13 +57,10 @@ describeSkipIfRpc("challengerequest", async () => { it(`challenge request with outdated timestamp is invalidated`, async () => { const comment = await generateMockPost(signers[0].address, plebbit, false, { signer: signers[5] }); + const challengeRequestPromise = new Promise((resolve) => comment.once("challengerequest", resolve)); await comment.publish(); - expect(comment._publishedChallengeRequests).to.be.a("array"); - const challengeRequestToEdit = remeda.omit(remeda.clone(comment._publishedChallengeRequests[0]), [ - "comment", - "challengeAnswers", - "challengeCommentCids" - ]); + const challengeRequest = await challengeRequestPromise; + const challengeRequestToEdit = remeda.omit(remeda.clone(challengeRequest), ["comment", "challengeAnswers", "challengeCommentCids"]); challengeRequestToEdit.timestamp = timestamp() - 6 * 60; // Should be invalidated now const signer = Object.values(comment._challengeIdToPubsubSigner)[0]; challengeRequestToEdit.signature = await signChallengeRequest(challengeRequestToEdit, signer); @@ -73,14 +70,11 @@ describeSkipIfRpc("challengerequest", async () => { it(`Valid live ChallengeRequest gets validated correctly`, async () => { const comment = await generateMockPost(signers[0].address, plebbit, false, { signer: signers[5] }); + const challengeRequestPromise = new Promise((resolve) => comment.once("challengerequest", resolve)); + await comment.publish(); - // comment._publishedChallengeRequests (ChallengeRequest[]) should be defined now - expect(comment._publishedChallengeRequests).to.be.a("array"); - const requestToValidate = remeda.omit(comment._publishedChallengeRequests[0], [ - "comment", - "challengeAnswers", - "challengeCommentCids" - ]); + const challengeRequest = await challengeRequestPromise; + const requestToValidate = remeda.omit(challengeRequest, ["comment", "challengeAnswers", "challengeCommentCids"]); const verificaiton = await verifyChallengeRequest(requestToValidate); expect(verificaiton).to.deep.equal({ valid: true }); }); @@ -90,13 +84,12 @@ describeSkipIfRpc("challengerequest", async () => { const originalPublish = comment._clientsManager.pubsubPublishOnProvider.bind(comment._clientsManager); comment._clientsManager.pubsubPublishOnProvider = () => undefined; - await comment.publish(); // comment._publishedChallengeRequests should be defined now, although it hasn't been published + const challengeRequestPromise = new Promise((resolve) => comment.once("challengerequest", resolve)); - const challengeRequestToModify = remeda.omit(comment._publishedChallengeRequests[0], [ - "comment", - "challengeCommentCids", - "challengeAnswers" - ]); + await comment.publish(); + const challengeRequest = await challengeRequestPromise; + + const challengeRequestToModify = remeda.omit(challengeRequest, ["comment", "challengeCommentCids", "challengeAnswers"]); const pubsubSigner = Object.values(comment._challengeIdToPubsubSigner)[0]; challengeRequestToModify.encrypted = await encryptEd25519AesGcm( JSON.stringify(comment.toJSONPubsubRequestToEncrypt()), @@ -121,14 +114,15 @@ describeSkipIfRpc("challengerequest", async () => { const originalPublish = comment._clientsManager.pubsubPublishOnProvider.bind(comment._clientsManager); comment._clientsManager.pubsubPublishOnProvider = () => undefined; + const challengeRequestPromise = new Promise((resolve) => comment.once("challengerequest", resolve)); + try { - await comment.publish(); // comment._publishedChallengeRequests should be defined now, although it hasn't been published + await comment.publish(); } catch {} + const challengeRequest = await challengeRequestPromise; comment._clientsManager.pubsubPublishOnProvider = originalPublish; - expect(comment._publishedChallengeRequests).to.be.a("array"); - const commentObjToEncrypt = JSON.parse(JSON.stringify(comment.toJSONPubsubRequestToEncrypt())); expect( @@ -142,11 +136,7 @@ describeSkipIfRpc("challengerequest", async () => { reason: messages.ERR_SIGNATURE_IS_INVALID }); - const challengeRequestToModify = remeda.omit(comment._publishedChallengeRequests[0], [ - "comment", - "challengeCommentCids", - "challengeAnswers" - ]); + const challengeRequestToModify = remeda.omit(challengeRequest, ["comment", "challengeCommentCids", "challengeAnswers"]); const pubsubSigner = Object.values(comment._challengeIdToPubsubSigner)[0]; challengeRequestToModify.encrypted = await encryptEd25519AesGcm( @@ -170,12 +160,12 @@ describeSkipIfRpc("challengerequest", async () => { it(`Sub ignores a challenge request with invalid pubsubMessage.signature`, async () => { // This test case also includes challengeRequestId not being derived from signer since it's caught by verifyChallengeRequest const comment = await generateMockPost(signers[0].address, plebbit, false, { signer: signers[6] }); + const challengeRequestPromise = new Promise((resolve) => comment.once("challengerequest", resolve)); await Promise.all([new Promise((resolve) => comment.once("challengeverification", resolve)), comment.publish()]); - // comment._publishedChallengeRequests (ChallengeRequest) should be defined now - expect(comment._publishedChallengeRequests).to.be.a("array"); - const requestWithInvalidSignature = remeda.omit(remeda.clone(comment._publishedChallengeRequests[0]), [ + const challengeRequest = await challengeRequestPromise; + const requestWithInvalidSignature = remeda.omit(remeda.clone(challengeRequest), [ "comment", "challengeCommentCids", "challengeAnswers" @@ -279,14 +269,18 @@ describeSkipIfRpc("challengeanswer", async () => { const comment = await generateMockPost(mathCliSubplebbitAddress, plebbit, false, { signer: signers[6] }); comment.removeAllListeners(); + const challengeRequestPromise = new Promise((resolve) => comment.once("challengerequest", resolve)); + const challengePromise = new Promise((resolve) => comment.once("challenge", resolve)); + await comment.publish(); - await new Promise((resolve) => comment.once("challenge", resolve)); + const challengeRequest = await challengeRequestPromise; + const challenge = await challengePromise; await comment._plebbit._clientsManager.pubsubUnsubscribe(comment._subplebbit.pubsubTopic, comment.handleChallengeExchange); const toSignAnswer = { type: "CHALLENGEANSWER", - challengeRequestId: comment._publishedChallengeRequests[0].challengeRequestId, + challengeRequestId: challengeRequest.challengeRequestId, encrypted: JSON.stringify([2]), userAgent: PlebbitJsVersion.USER_AGENT, protocolVersion: PlebbitJsVersion.PROTOCOL_VERSION, @@ -311,10 +305,7 @@ describeSkipIfRpc("challengeanswer", async () => { const subMethod = (pubsubMsg) => { const msgParsed = cborgDecode(pubsubMsg["data"]); - if ( - msgParsed.type === "CHALLENGEVERIFICATION" && - msgParsed.challengeRequestId === comment._publishedChallengeRequests[0].challengeRequestId - ) { + if (msgParsed.type === "CHALLENGEVERIFICATION" && msgParsed.challengeRequestId === challengeRequest.challengeRequestId) { assert.fail("Subplebbit should ignore a challenge answer with invalid signature"); } };