From dff54d5e390e460767fe2bbbdd8dad66cb48fc83 Mon Sep 17 00:00:00 2001 From: phiresky Date: Mon, 18 Sep 2023 19:58:20 +0000 Subject: [PATCH] tweak tests more --- api_tests/src/comment.spec.ts | 25 +++++++++++++-------- api_tests/src/community.spec.ts | 2 +- api_tests/src/post.spec.ts | 39 +++++++++++++++++++-------------- api_tests/src/shared.ts | 3 ++- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/api_tests/src/comment.spec.ts b/api_tests/src/comment.spec.ts index 36bf5809e..3c8578d80 100644 --- a/api_tests/src/comment.spec.ts +++ b/api_tests/src/comment.spec.ts @@ -342,6 +342,8 @@ test("Federated comment like", async () => { }); test("Reply to a comment from another instance, get notification", async () => { + await alpha.client.markAllAsRead({ auth: alpha.auth }); + let betaCommunity = (await resolveBetaCommunity(alpha)).community; if (!betaCommunity) { throw "Missing beta community"; @@ -404,16 +406,20 @@ test("Reply to a comment from another instance, get notification", async () => { // check inbox of replies on alpha, fetching read/unread both let alphaRepliesRes = await getReplies(alpha); - expect(alphaRepliesRes.replies.length).toBe(1); - expect(alphaRepliesRes.replies[0].comment.content).toBeDefined(); - expect(alphaRepliesRes.replies[0].community.local).toBe(false); - expect(alphaRepliesRes.replies[0].creator.local).toBe(false); - expect(alphaRepliesRes.replies[0].counts.score).toBe(1); + const alphaReply = alphaRepliesRes.replies.find( + r => r.comment.id === alphaComment.comment.id, + ); + expect(alphaReply).toBeDefined(); + if (!alphaReply) throw Error(); + expect(alphaReply.comment.content).toBeDefined(); + expect(alphaReply.community.local).toBe(false); + expect(alphaReply.creator.local).toBe(false); + expect(alphaReply.counts.score).toBe(1); // ToDo: interesting alphaRepliesRes.replies[0].comment_reply.id is 1, meaning? how did that come about? - expect(alphaRepliesRes.replies[0].comment.id).toBe(alphaComment.comment.id); + expect(alphaReply.comment.id).toBe(alphaComment.comment.id); // this is a new notification, getReplies fetch was for read/unread both, confirm it is unread. - expect(alphaRepliesRes.replies[0].comment_reply.read).toBe(false); - assertCommentFederation(alphaRepliesRes.replies[0], replyRes.comment_view); + expect(alphaReply.comment_reply.read).toBe(false); + assertCommentFederation(alphaReply, replyRes.comment_view); }); test("Mention beta from alpha", async () => { @@ -494,7 +500,8 @@ test("A and G subscribe to B (center) A posts, G mentions B, it gets announced t expect(alphaPost.post_view.community.local).toBe(true); // Make sure gamma sees it - let gammaPost = (await resolvePost(gamma, alphaPost.post_view.post)).post; + let gammaPost = (await resolvePost(gamma, alphaPost.post_view.post, false))! + .post; if (!gammaPost) { throw "Missing gamma post"; diff --git a/api_tests/src/community.spec.ts b/api_tests/src/community.spec.ts index c078cd9b0..a6ecd1ab8 100644 --- a/api_tests/src/community.spec.ts +++ b/api_tests/src/community.spec.ts @@ -237,7 +237,7 @@ test("Admin actions in remote community are not federated to origin", async () = expect(banRes.banned).toBe(true); // ban doesnt federate to community's origin instance alpha - let alphaPost = (await resolvePost(alpha, gammaPost.post)).post; + let alphaPost = (await resolvePost(alpha, gammaPost.post, false)).post; expect(alphaPost?.creator_banned_from_community).toBe(false); // and neither to gamma diff --git a/api_tests/src/post.spec.ts b/api_tests/src/post.spec.ts index 89dfaa35f..1edabdcc8 100644 --- a/api_tests/src/post.spec.ts +++ b/api_tests/src/post.spec.ts @@ -83,10 +83,10 @@ test("Create a post", async () => { // Make sure that post is liked on beta const res = await waitUntil( - () => resolvePost(beta, postRes.post_view.post), - res => res.post?.counts.score === 1, + () => resolvePost(beta, postRes.post_view.post).catch(e => null), + res => res?.post?.counts.score === 1, ); - let betaPost = res.post; + let betaPost = res?.post; expect(betaPost).toBeDefined(); expect(betaPost?.community.local).toBe(true); @@ -177,7 +177,7 @@ test("Sticky a post", async () => { } let postRes = await createPost(alpha, betaCommunity.community.id); - let betaPost1 = (await resolvePost(beta, postRes.post_view.post)).post; + let betaPost1 = (await resolvePost(beta, postRes.post_view.post, false)).post; if (!betaPost1) { throw "Missing beta post1"; } @@ -201,7 +201,8 @@ test("Sticky a post", async () => { expect(betaPost2?.post.featured_community).toBe(false); // Make sure that gamma cannot sticky the post on beta - let gammaPost = (await resolvePost(gamma, postRes.post_view.post)).post; + let gammaPost = (await resolvePost(gamma, postRes.post_view.post, false)) + .post; if (!gammaPost) { throw "Missing gamma post"; } @@ -320,7 +321,8 @@ test("Remove a post from admin and community on different instance", async () => } let postRes = await createPost(gamma, gammaCommunity.id); - let alphaPost = (await resolvePost(alpha, postRes.post_view.post)).post; + let alphaPost = (await resolvePost(alpha, postRes.post_view.post, false)) + .post; if (!alphaPost) { throw "Missing alpha post"; } @@ -329,7 +331,7 @@ test("Remove a post from admin and community on different instance", async () => expect(removedPost.post_view.post.name).toBe(postRes.post_view.post.name); // Make sure lemmy beta sees post is NOT removed - let betaPost = (await resolvePost(beta, postRes.post_view.post)).post; + let betaPost = (await resolvePost(beta, postRes.post_view.post, false)).post; if (!betaPost) { throw "Missing beta post"; } @@ -533,7 +535,7 @@ test("A and G subscribe to B (center) A posts, it gets announced to G", async () let postRes = await createPost(alpha, betaCommunity.community.id); expect(postRes.post_view.post).toBeDefined(); - let betaPost = (await resolvePost(gamma, postRes.post_view.post)).post; + let betaPost = (await resolvePost(gamma, postRes.post_view.post, false)).post; expect(betaPost?.post.name).toBeDefined(); }); @@ -546,7 +548,8 @@ test("Report a post", async () => { let postRes = await createPost(beta, betaCommunity.community.id); expect(postRes.post_view.post).toBeDefined(); - let alphaPost = (await resolvePost(alpha, postRes.post_view.post)).post; + let alphaPost = (await resolvePost(alpha, postRes.post_view.post, false)) + .post; if (!alphaPost) { throw "Missing alpha post"; } @@ -554,12 +557,16 @@ test("Report a post", async () => { await reportPost(alpha, alphaPost.post.id, randomString(10)) ).post_report_view.post_report; - let betaReport = ( - await waitUntil( - () => listPostReports(beta), - res => !!res.post_reports[0], - ) - ).post_reports[0].post_report; + let betaReport = (await waitUntil( + () => + listPostReports(beta).then(p => + p.post_reports.find( + r => + r.post_report.original_post_name === alphaReport.original_post_name, + ), + ), + res => !!res, + ))!.post_report; expect(betaReport).toBeDefined(); expect(betaReport.resolved).toBe(false); expect(betaReport.original_post_name).toBe(alphaReport.original_post_name); @@ -588,7 +595,7 @@ test("Sanitize HTML", async () => { "<script>alert('xss');</script> hello &"'", ); - let alphaPost = (await resolvePost(alpha, post.post_view.post)).post; + let alphaPost = (await resolvePost(alpha, post.post_view.post, false)).post; // second escaping over federation, avoid double escape of & expect(alphaPost?.post.body).toBe( "<script>alert('xss');</script> hello &"'", diff --git a/api_tests/src/shared.ts b/api_tests/src/shared.ts index df5d299c6..b3f9f637f 100644 --- a/api_tests/src/shared.ts +++ b/api_tests/src/shared.ts @@ -296,10 +296,11 @@ export async function lockPost( export async function resolvePost( api: API, post: Post, + localOnly = true, ): Promise { let form: ResolveObject = { q: post.ap_id, - auth: api.auth, + auth: localOnly ? null : api.auth, }; return api.client.resolveObject(form); }