Skip to content

Commit

Permalink
test challengerequest event instead of private props
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinse12 committed Oct 29, 2024
1 parent d9df43d commit 492c679
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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
});

Expand All @@ -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 () => {
Expand All @@ -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({
Expand All @@ -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);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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
});

Expand All @@ -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
Expand All @@ -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 () => {
Expand All @@ -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);
});
});

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,26 @@ 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 () => {
const post = await generateMockPost(subplebbitAddress, plebbit);
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);
});
Expand Down Expand Up @@ -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);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,57 @@ 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 () => {
it(`Publishing with extra prop for author should fail if it's a reserved field`, async () => {
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);
const extraProps = { extraProp: "1234" };
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);
});
});
});
Expand Down
Loading

0 comments on commit 492c679

Please sign in to comment.