@ -18,12 +18,12 @@ import {
resolveBetaCommunity ,
createComment ,
deletePost ,
delay ,
removePost ,
getPost ,
unfollowRemotes ,
resolvePerson ,
banPersonFromSite ,
searchPostLocal ,
followCommunity ,
banPersonFromCommunity ,
reportPost ,
@ -37,10 +37,9 @@ import {
waitForPost ,
alphaUrl ,
loginUser ,
createCommunity ,
} from "./shared" ;
import { PostView } from "lemmy-js-client/dist/types/PostView" ;
import { EditSite, ResolveObject } from "lemmy-js-client" ;
import { ResolveObject } from "lemmy-js-client" ;
let betaCommunity : CommunityView | undefined ;
@ -48,22 +47,14 @@ beforeAll(async () => {
await setupLogins ( ) ;
betaCommunity = ( await resolveBetaCommunity ( alpha ) ) . community ;
expect ( betaCommunity ) . toBeDefined ( ) ;
await unfollows ( ) ;
} ) ;
afterAll ( unfollows ) ;
async function assertPostFederation ( postOne : PostView , postTwo : PostView ) {
// Link metadata is generated in background task and may not be ready yet at this time,
// so wait for it explicitly. For removed posts we cant refetch anything.
postOne = await waitForPost ( beta , postOne . post , res = > {
return res === null || res ? . post . embed_title !== null ;
} ) ;
postTwo = await waitForPost (
beta ,
postTwo . post ,
res = > res === null || res ? . post . embed_title !== null ,
) ;
afterAll ( ( ) = > {
unfollows ( ) ;
} ) ;
function assertPostFederation ( postOne? : PostView , postTwo? : PostView ) {
expect ( postOne ? . post . ap_id ) . toBe ( postTwo ? . post . ap_id ) ;
expect ( postOne ? . post . name ) . toBe ( postTwo ? . post . name ) ;
expect ( postOne ? . post . body ) . toBe ( postTwo ? . post . body ) ;
@ -81,13 +72,6 @@ async function assertPostFederation(postOne: PostView, postTwo: PostView) {
}
test ( "Create a post" , async ( ) = > {
// Setup some allowlists and blocklists
const editSiteForm : EditSite = { } ;
editSiteForm . allowed_instances = [ ] ;
editSiteForm . blocked_instances = [ "lemmy-alpha" ] ;
await epsilon . editSite ( editSiteForm ) ;
if ( ! betaCommunity ) {
throw "Missing beta community" ;
}
@ -114,7 +98,7 @@ test("Create a post", async () => {
expect ( betaPost ? . community . local ) . toBe ( true ) ;
expect ( betaPost ? . creator . local ) . toBe ( false ) ;
expect ( betaPost ? . counts . score ) . toBe ( 1 ) ;
await assertPostFederation ( betaPost , postRes . post_view ) ;
assertPostFederation ( betaPost , postRes . post_view ) ;
// Delta only follows beta, so it should not see an alpha ap_id
await expect (
@ -125,12 +109,6 @@ test("Create a post", async () => {
await expect (
resolvePost ( epsilon , postRes . post_view . post ) ,
) . rejects . toStrictEqual ( Error ( "couldnt_find_object" ) ) ;
// remove added allow/blocklists
editSiteForm . allowed_instances = [ ] ;
editSiteForm . blocked_instances = [ ] ;
await delta . editSite ( editSiteForm ) ;
await epsilon . editSite ( editSiteForm ) ;
} ) ;
test ( "Create a post in a non-existent community" , async ( ) = > {
@ -162,7 +140,7 @@ test("Unlike a post", async () => {
expect ( betaPost ? . community . local ) . toBe ( true ) ;
expect ( betaPost ? . creator . local ) . toBe ( false ) ;
expect ( betaPost ? . counts . score ) . toBe ( 0 ) ;
await assertPostFederation ( betaPost , postRes . post_view ) ;
assertPostFederation ( betaPost , postRes . post_view ) ;
} ) ;
test ( "Update a post" , async ( ) = > {
@ -183,7 +161,7 @@ test("Update a post", async () => {
expect ( betaPost . community . local ) . toBe ( true ) ;
expect ( betaPost . creator . local ) . toBe ( false ) ;
expect ( betaPost . post . name ) . toBe ( updatedName ) ;
await assertPostFederation ( betaPost , updatedPost . post_view ) ;
assertPostFederation ( betaPost , updatedPost . post_view ) ;
// Make sure lemmy beta cannot update the post
await expect ( editPost ( beta , betaPost . post ) ) . rejects . toStrictEqual (
@ -225,35 +203,12 @@ test("Sticky a post", async () => {
if ( ! gammaPost ) {
throw "Missing gamma post" ;
}
// This has been failing occasionally
await featurePost ( gamma , true , gammaPost . post ) ;
let gammaTrySticky = await featurePost ( gamma , true , gammaPost . post ) ;
let betaPost3 = ( await resolvePost ( beta , postRes . post_view . post ) ) . post ;
// expect(gammaTrySticky.post_view.post.featured_community).toBe(true);
expect ( gammaTrySticky . post_view . post . featured_community ) . toBe ( true ) ;
expect ( betaPost3 ? . post . featured_community ) . toBe ( false ) ;
} ) ;
test ( "Collection of featured posts gets federated" , async ( ) = > {
// create a new community and feature a post
let community = await createCommunity ( alpha ) ;
let post = await createPost ( alpha , community . community_view . community . id ) ;
let featuredPost = await featurePost ( alpha , true , post . post_view . post ) ;
expect ( featuredPost . post_view . post . featured_community ) . toBe ( true ) ;
// fetch the community, ensure that post is also fetched and marked as featured
let betaCommunity = await resolveCommunity (
beta ,
community . community_view . community . actor_id ,
) ;
expect ( betaCommunity ) . toBeDefined ( ) ;
const betaPost = await waitForPost (
beta ,
post . post_view . post ,
post = > post ? . post . featured_community === true ,
) ;
expect ( betaPost ) . toBeDefined ( ) ;
} ) ;
test ( "Lock a post" , async ( ) = > {
if ( ! betaCommunity ) {
throw "Missing beta community" ;
@ -277,10 +232,8 @@ test("Lock a post", async () => {
post = > ! ! post && post . post . locked ,
) ;
// Try to make a new comment there, on alpha. For this we need to create a normal
// user account because admins/mods can comment in locked posts.
let user = await registerUser ( alpha , alphaUrl ) ;
await expect ( createComment ( user , alphaPost1 . post . id ) ) . rejects . toStrictEqual (
// Try to make a new comment there, on alpha
await expect ( createComment ( alpha , alphaPost1 . post . id ) ) . rejects . toStrictEqual (
Error ( "locked" ) ,
) ;
@ -299,7 +252,7 @@ test("Lock a post", async () => {
expect ( alphaPost2 . post . locked ) . toBe ( false ) ;
// Try to create a new comment, on alpha
let commentAlpha = await createComment ( user , alphaPost1 . post . id ) ;
let commentAlpha = await createComment ( alpha , alphaPost1 . post . id ) ;
expect ( commentAlpha ) . toBeDefined ( ) ;
} ) ;
@ -334,7 +287,7 @@ test("Delete a post", async () => {
throw "Missing beta post 2" ;
}
expect ( betaPost2 . post . deleted ) . toBe ( false ) ;
await assertPostFederation ( betaPost2 , undeletedPost . post_view ) ;
assertPostFederation ( betaPost2 , undeletedPost . post_view ) ;
// Make sure lemmy beta cannot delete the post
await expect ( deletePost ( beta , true , betaPost2 . post ) ) . rejects . toStrictEqual (
@ -377,7 +330,7 @@ test("Remove a post from admin and community on different instance", async () =>
// Make sure lemmy beta sees post is undeleted
let betaPost2 = ( await resolvePost ( beta , postRes . post_view . post ) ) . post ;
expect ( betaPost2 ? . post . removed ) . toBe ( false ) ;
await assertPostFederation ( betaPost2 ! , undeletedPost . post_view ) ;
assertPostFederation ( betaPost2 , undeletedPost . post_view ) ;
} ) ;
test ( "Remove a post from admin and community on same instance" , async ( ) = > {
@ -408,7 +361,7 @@ test("Remove a post from admin and community on same instance", async () => {
p = > p ? . post_view . post . removed ? ? false ,
) ;
expect ( alphaPost ? . post_view . post . removed ) . toBe ( true ) ;
await assertPostFederation ( alphaPost . post_view , removePostRes . post_view ) ;
assertPostFederation ( alphaPost . post_view , removePostRes . post_view ) ;
// Undelete
let undeletedPost = await removePost ( beta , false , betaPost . post ) ;
@ -421,7 +374,7 @@ test("Remove a post from admin and community on same instance", async () => {
p = > ! ! p && ! p . post . removed ,
) ;
expect ( alphaPost2 . post . removed ) . toBe ( false ) ;
await assertPostFederation ( alphaPost2 , undeletedPost . post_view ) ;
assertPostFederation ( alphaPost2 , undeletedPost . post_view ) ;
await unfollowRemotes ( alpha ) ;
} ) ;
@ -437,34 +390,30 @@ test("Search for a post", async () => {
expect ( betaPost ? . post . name ) . toBeDefined ( ) ;
} ) ;
test ( "Enforce site ban f ederation for local user", async ( ) = > {
test ( "Enforce site ban f or federated user", async ( ) = > {
if ( ! betaCommunity ) {
throw "Missing beta community" ;
}
// create a test user
let alpha UserHttp = await registerUser ( alpha , alphaUrl ) ;
let alphaUserPerson = ( await getSite ( alpha UserHttp ) ) . my_user ? . local_user_view
let alpha _user = await registerUser ( alpha , alphaUrl ) ;
let alphaUserPerson = ( await getSite ( alpha _user ) ) . my_user ? . local_user_view
. person ;
let alphaUserActorId = alphaUserPerson ? . actor_id ;
if ( ! alphaUserActorId ) {
throw "Missing alpha user actor id" ;
}
expect ( alphaUserActorId ) . toBeDefined ( ) ;
await followBeta ( alphaUserHttp ) ;
let alphaPerson = ( await resolvePerson ( alphaUserHttp , alphaUserActorId ! ) )
. person ;
let alphaPerson = ( await resolvePerson ( alpha_user , alphaUserActorId ! ) ) . person ;
if ( ! alphaPerson ) {
throw "Missing alpha person" ;
}
expect ( alphaPerson ) . toBeDefined ( ) ;
// alpha makes post in beta community, it federates to beta instance
let postRes1 = await createPost ( alpha UserHttp , betaCommunity . community . id ) ;
let postRes1 = await createPost ( alpha _user , betaCommunity . community . id ) ;
let searchBeta1 = await waitForPost ( beta , postRes1 . post_view . post ) ;
// ban alpha from its own instance
// ban alpha from its instance
let banAlpha = await banPersonFromSite (
alpha ,
alphaPerson . person . id ,
@ -481,11 +430,10 @@ test("Enforce site ban federation for local user", async () => {
expect ( alphaUserOnBeta1 . person ? . person . banned ) . toBe ( true ) ;
// existing alpha post should be removed on beta
let betaBanRes = await waitUntil (
await waitUntil (
( ) = > getPost ( beta , searchBeta1 . post . id ) ,
s = > s . post_view . post . removed ,
) ;
expect ( betaBanRes . post_view . post . removed ) . toBe ( true ) ;
// Unban alpha
let unBanAlpha = await banPersonFromSite (
@ -501,84 +449,21 @@ test("Enforce site ban federation for local user", async () => {
throw "Missing alpha person" ;
}
let newAlphaUserJwt = await loginUser ( alpha , alphaUserPerson . name ) ;
alpha UserHttp . setHeaders ( {
alpha _user . setHeaders ( {
Authorization : "Bearer " + newAlphaUserJwt . jwt ? ? "" ,
} ) ;
// alpha makes new post in beta community, it federates
let postRes2 = await createPost ( alpha UserHttp , betaCommunity ! . community . id ) ;
let postRes2 = await createPost ( alpha _user , betaCommunity ! . community . id ) ;
await waitForPost ( beta , postRes2 . post_view . post ) ;
await unfollowRemotes ( alpha ) ;
} ) ;
test ( "Enforce site ban federation for federated user" , async ( ) = > {
if ( ! betaCommunity ) {
throw "Missing beta community" ;
}
// create a test user
let alphaUserHttp = await registerUser ( alpha , alphaUrl ) ;
let alphaUserPerson = ( await getSite ( alphaUserHttp ) ) . my_user ? . local_user_view
. person ;
let alphaUserActorId = alphaUserPerson ? . actor_id ;
if ( ! alphaUserActorId ) {
throw "Missing alpha user actor id" ;
}
expect ( alphaUserActorId ) . toBeDefined ( ) ;
await followBeta ( alphaUserHttp ) ;
let alphaUserOnBeta2 = await resolvePerson ( beta , alphaUserActorId ! ) ;
expect ( alphaUserOnBeta2 . person ? . person . banned ) . toBe ( false ) ;
if ( ! alphaUserOnBeta2 . person ) {
throw "Missing alpha person" ;
}
// alpha makes post in beta community, it federates to beta instance
let postRes1 = await createPost ( alphaUserHttp , betaCommunity . community . id ) ;
let searchBeta1 = await waitForPost ( beta , postRes1 . post_view . post ) ;
expect ( searchBeta1 . post ) . toBeDefined ( ) ;
// Now ban and remove their data from beta
let banAlphaOnBeta = await banPersonFromSite (
beta ,
alphaUserOnBeta2 . person . person . id ,
true ,
true ,
) ;
expect ( banAlphaOnBeta . banned ) . toBe ( true ) ;
// The beta site ban should NOT be federated to alpha
let alphaPerson2 = ( await getSite ( alphaUserHttp ) ) . my_user ! . local_user_view
. person ;
expect ( alphaPerson2 . banned ) . toBe ( false ) ;
// existing alpha post should be removed on beta
let betaBanRes = await waitUntil (
( ) = > getPost ( beta , searchBeta1 . post . id ) ,
s = > s . post_view . post . removed ,
) ;
expect ( betaBanRes . post_view . post . removed ) . toBe ( true ) ;
// existing alpha's post to the beta community should be removed on alpha
let alphaPostAfterRemoveOnBeta = await waitUntil (
( ) = > getPost ( alpha , postRes1 . post_view . post . id ) ,
s = > s . post_view . post . removed ,
) ;
expect ( betaBanRes . post_view . post . removed ) . toBe ( true ) ;
expect ( alphaPostAfterRemoveOnBeta . post_view . post . removed ) . toBe ( true ) ;
expect (
alphaPostAfterRemoveOnBeta . post_view . creator_banned_from_community ,
) . toBe ( true ) ;
await unfollowRemotes ( alpha ) ;
} ) ;
test ("Enforce community ban for federated user" , async ( ) = > {
test . skip ( "Enforce community ban for federated user" , async ( ) = > {
if ( ! betaCommunity ) {
throw "Missing beta community" ;
}
await followBeta ( alpha ) ;
let alphaShortname = ` @lemmy_alpha@lemmy-alpha:8541 ` ;
let alphaPerson = ( await resolvePerson ( beta , alphaShortname ) ) . person ;
if ( ! alphaPerson ) {
@ -588,46 +473,38 @@ test("Enforce community ban for federated user", async () => {
// make a post in beta, it goes through
let postRes1 = await createPost ( alpha , betaCommunity . community . id ) ;
let searchBeta1 = await waitForPost ( beta , postRes1 . post_view . post ) ;
expect ( searchBeta1 . post ) . toBeDefined ( ) ;
let searchBeta1 = await searchPostLocal ( beta , postRes1 . post_view . post ) ;
expect ( searchBeta1 . post s[ 0 ] ) . toBeDefined ( ) ;
// ban alpha from beta community
let banAlpha = await banPersonFromCommunity (
beta ,
alphaPerson . person . id ,
searchBeta1 . community . id ,
2 ,
true ,
true ,
) ;
expect ( banAlpha . banned ) . toBe ( true ) ;
// ensure that the post by alpha got removed
let removePostRes = await waitUntil (
( ) = > getPost ( alpha , postRes1 . post_view . post . id ) ,
s = > s . post_view . post . removed ,
await expect ( getPost ( alpha , searchBeta1 . posts [ 0 ] . post . id ) ) . rejects . toBe (
Error ( "unknown" ) ,
) ;
expect ( removePostRes . post_view . post . removed ) . toBe ( true ) ;
expect ( removePostRes . post_view . creator_banned_from_community ) . toBe ( true ) ;
expect ( removePostRes . community_view . banned_from_community ) . toBe ( true ) ;
// Alpha tries to make post on beta, but it fails because of ban
await expect (
createPost ( alpha , betaCommunity . community . id ) ,
) .rejects . toStrictEqual ( Error ( "banned_from_community" ) ) ;
await expect ( createPost ( alpha , betaCommunity . community . id ) ) . rejects . toBe (
Error ( "banned_from_community" ) ,
) ;
// Unban alpha
let unBanAlpha = await banPersonFromCommunity (
beta ,
alphaPerson . person . id ,
searchBeta1 . community . id ,
2 ,
false ,
false ,
) ;
expect ( unBanAlpha . banned ) . toBe ( false ) ;
// Need to re-follow the community
await followBeta ( alpha ) ;
let postRes3 = await createPost ( alpha , betaCommunity . community . id ) ;
expect ( postRes3 . post_view . post ) . toBeDefined ( ) ;
expect ( postRes3 . post_view . community . local ) . toBe ( false ) ;
@ -635,86 +512,57 @@ test("Enforce community ban for federated user", async () => {
expect ( postRes3 . post_view . counts . score ) . toBe ( 1 ) ;
// Make sure that post makes it to beta community
let postRes4 = await waitForPost ( beta , postRes3 . post_view . post ) ;
expect ( postRes4 . post ) . toBeDefined ( ) ;
expect ( postRes4 . creator_banned_from_community ) . toBe ( false ) ;
await unfollowRemotes ( alpha ) ;
let searchBeta2 = await searchPostLocal ( beta , postRes3 . post_view . post ) ;
expect ( searchBeta2 . posts [ 0 ] ) . toBeDefined ( ) ;
} ) ;
test ( "A and G subscribe to B (center) A posts, it gets announced to G" , async ( ) = > {
if ( ! betaCommunity ) {
throw "Missing beta community" ;
}
await followBeta ( alpha ) ;
let postRes = await createPost ( alpha , betaCommunity . community . id ) ;
expect ( postRes . post_view . post ) . toBeDefined ( ) ;
let betaPost = ( await resolvePost ( gamma , postRes . post_view . post ) ) . post ;
expect ( betaPost ? . post . name ) . toBeDefined ( ) ;
await unfollowRemotes ( alpha ) ;
} ) ;
test ( "Report a post" , async ( ) = > {
// Create post from alpha
let alphaCommunity = ( await resolveBetaCommunity ( alpha ) ) . community ! ;
await followBeta ( alpha ) ;
let postRes = await createPost ( alpha , alphaCommunity . community . id ) ;
// Note, this is a different one from the setup
let betaCommunity = ( await resolveBetaCommunity ( beta ) ) . community ;
if ( ! betaCommunity ) {
throw "Missing beta community" ;
}
let postRes = await createPost ( beta , betaCommunity . community . id ) ;
expect ( postRes . post_view . post ) . toBeDefined ( ) ;
let alphaPost = ( await resolvePost ( alpha , postRes . post_view . post ) ) . post ;
if ( ! alphaPost ) {
throw "Missing alpha post" ;
}
// Send report from gamma
let gammaPost = ( await resolvePost ( gamma , alphaPost . post ) ) . post ! ;
let gammaReport = (
await reportPost ( gamma , gammaPost . post . id , randomString ( 10 ) )
let alphaReport = (
await reportPost ( alpha , alphaPost . post . id , randomString ( 10 ) )
) . post_report_view . post_report ;
expect ( gammaReport ) . toBeDefined ( ) ;
// Report was federated to community instance
let betaReport = ( await waitUntil (
( ) = >
listPostReports ( beta ) . then ( p = >
p . post_reports . find (
r = >
r . post_report . original_post_name === gamm aReport. original_post_name ,
r . post_report . original_post_name === alph aReport. original_post_name ,
) ,
) ,
res = > ! ! res ,
) ) ! . post_report ;
expect ( betaReport ) . toBeDefined ( ) ;
expect ( betaReport . resolved ) . toBe ( false ) ;
expect ( betaReport . original_post_name ) . toBe ( gammaReport . original_post_name ) ;
//expect(betaReport.original_post_url).toBe(gammaReport.original_post_url);
expect ( betaReport . original_post_body ) . toBe ( gammaReport . original_post_body ) ;
expect ( betaReport . reason ) . toBe ( gammaReport . reason ) ;
await unfollowRemotes ( alpha ) ;
// Report was federated to poster's instance
let alphaReport = ( await waitUntil (
( ) = >
listPostReports ( alpha ) . then ( p = >
p . post_reports . find (
r = >
r . post_report . original_post_name === gammaReport . original_post_name ,
) ,
) ,
res = > ! ! res ,
) ) ! . post_report ;
expect ( alphaReport ) . toBeDefined ( ) ;
expect ( alphaReport . resolved ) . toBe ( false ) ;
expect ( alphaReport . original_post_name ) . toBe ( gammaReport . original_post_name ) ;
//expect(alphaReport.original_post_url).toBe(gammaReport.original_post_url);
expect ( alphaReport . original_post_body ) . toBe ( gammaReport . original_post_body ) ;
expect ( alphaReport . reason ) . toBe ( gammaReport . reason ) ;
expect ( betaReport . original_post_name ) . toBe ( alphaReport . original_post_name ) ;
expect ( betaReport . original_post_url ) . toBe ( alphaReport . original_post_url ) ;
expect ( betaReport . original_post_body ) . toBe ( alphaReport . original_post_body ) ;
expect ( betaReport . reason ) . toBe ( alphaReport . reason ) ;
} ) ;
test ( "Fetch post via redirect" , async ( ) = > {
await followBeta ( alpha ) ;
let alphaPost = await createPost ( alpha , betaCommunity ! . community . id ) ;
expect ( alphaPost . post_view . post ) . toBeDefined ( ) ;
// Make sure that post is liked on beta
@ -735,47 +583,4 @@ test("Fetch post via redirect", async () => {
let gammaPost = await gamma . resolveObject ( form ) ;
expect ( gammaPost ) . toBeDefined ( ) ;
expect ( gammaPost . post ? . post . ap_id ) . toBe ( alphaPost . post_view . post . ap_id ) ;
await unfollowRemotes ( alpha ) ;
} ) ;
test ( "Block post that contains banned URL" , async ( ) = > {
let editSiteForm : EditSite = {
blocked_urls : [ "https://evil.com/" ] ,
} ;
await epsilon . editSite ( editSiteForm ) ;
await delay ( ) ;
if ( ! betaCommunity ) {
throw "Missing beta community" ;
}
expect (
createPost ( epsilon , betaCommunity . community . id , "https://evil.com" ) ,
) . rejects . toStrictEqual ( Error ( "blocked_url" ) ) ;
// Later tests need this to be empty
editSiteForm . blocked_urls = [ ] ;
await epsilon . editSite ( editSiteForm ) ;
} ) ;
test ( "Fetch post with redirect" , async ( ) = > {
let alphaPost = await createPost ( alpha , betaCommunity ! . community . id ) ;
expect ( alphaPost . post_view . post ) . toBeDefined ( ) ;
// beta fetches from alpha as usual
let betaPost = await resolvePost ( beta , alphaPost . post_view . post ) ;
expect ( betaPost . post ) . toBeDefined ( ) ;
// gamma fetches from beta, and gets redirected to alpha
let gammaPost = await resolvePost ( gamma , betaPost . post ! . post ) ;
expect ( gammaPost . post ) . toBeDefined ( ) ;
// fetch remote object from local url, which redirects to the original url
let form : ResolveObject = {
q : ` http://lemmy-gamma:8561/post/ ${ gammaPost . post ! . post . id } ` ,
} ;
let gammaPost2 = await gamma . resolveObject ( form ) ;
expect ( gammaPost2 . post ) . toBeDefined ( ) ;
} ) ;