lemmy/api_tests/src/community.spec.ts

300 lines
10 KiB
TypeScript
Raw Normal View History

jest.setTimeout(120000);
import { CommunityView } from "lemmy-js-client/dist/types/CommunityView";
import {
alpha,
beta,
gamma,
setupLogins,
resolveCommunity,
createCommunity,
deleteCommunity,
removeCommunity,
getCommunity,
followCommunity,
banPersonFromCommunity,
resolvePerson,
getSite,
createPost,
getPost,
resolvePost,
registerUser,
API,
getPosts,
getCommunityByName,
} from "./shared";
beforeAll(async () => {
await setupLogins();
});
function assertCommunityFederation(
communityOne?: CommunityView,
communityTwo?: CommunityView,
2020-12-20 21:16:57 +00:00
) {
expect(communityOne?.community.actor_id).toBe(
communityTwo?.community.actor_id,
);
expect(communityOne?.community.name).toBe(communityTwo?.community.name);
expect(communityOne?.community.title).toBe(communityTwo?.community.title);
expect(communityOne?.community.description).toBe(
communityTwo?.community.description,
);
expect(communityOne?.community.icon).toBe(communityTwo?.community.icon);
expect(communityOne?.community.banner).toBe(communityTwo?.community.banner);
expect(communityOne?.community.published).toBe(
communityTwo?.community.published,
);
expect(communityOne?.community.nsfw).toBe(communityTwo?.community.nsfw);
expect(communityOne?.community.removed).toBe(communityTwo?.community.removed);
expect(communityOne?.community.deleted).toBe(communityTwo?.community.deleted);
}
test("Create community", async () => {
let communityRes = await createCommunity(alpha);
2020-12-20 21:16:57 +00:00
expect(communityRes.community_view.community.name).toBeDefined();
// A dupe check
2020-12-20 21:16:57 +00:00
let prevName = communityRes.community_view.community.name;
await expect(createCommunity(alpha, prevName)).rejects.toBe(
"community_already_exists",
);
// Cache the community on beta, make sure it has the other fields
let searchShort = `!${prevName}@lemmy-alpha:8541`;
let betaCommunity = (await resolveCommunity(beta, searchShort)).community;
assertCommunityFederation(betaCommunity, communityRes.community_view);
});
test("Delete community", async () => {
let communityRes = await createCommunity(beta);
// Cache the community on Alpha
2020-12-20 21:16:57 +00:00
let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community;
if (!alphaCommunity) {
throw "Missing alpha community";
}
assertCommunityFederation(alphaCommunity, communityRes.community_view);
// Follow the community from alpha
let follow = await followCommunity(alpha, true, alphaCommunity.community.id);
// Make sure the follow response went through
expect(follow.community_view.community.local).toBe(false);
let deleteCommunityRes = await deleteCommunity(
beta,
true,
communityRes.community_view.community.id,
);
2020-12-20 21:16:57 +00:00
expect(deleteCommunityRes.community_view.community.deleted).toBe(true);
expect(deleteCommunityRes.community_view.community.title).toBe(
communityRes.community_view.community.title,
);
// Make sure it got deleted on A
2020-12-20 21:16:57 +00:00
let communityOnAlphaDeleted = await getCommunity(
alpha,
alphaCommunity.community.id,
2020-12-20 21:16:57 +00:00
);
expect(communityOnAlphaDeleted.community_view.community.deleted).toBe(true);
// Undelete
let undeleteCommunityRes = await deleteCommunity(
beta,
false,
communityRes.community_view.community.id,
);
2020-12-20 21:16:57 +00:00
expect(undeleteCommunityRes.community_view.community.deleted).toBe(false);
// Make sure it got undeleted on A
2020-12-20 21:16:57 +00:00
let communityOnAlphaUnDeleted = await getCommunity(
alpha,
alphaCommunity.community.id,
2020-12-20 21:16:57 +00:00
);
expect(communityOnAlphaUnDeleted.community_view.community.deleted).toBe(
false,
2020-12-20 21:16:57 +00:00
);
});
test("Remove community", async () => {
let communityRes = await createCommunity(beta);
// Cache the community on Alpha
2020-12-20 21:16:57 +00:00
let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community;
if (!alphaCommunity) {
throw "Missing alpha community";
}
assertCommunityFederation(alphaCommunity, communityRes.community_view);
// Follow the community from alpha
let follow = await followCommunity(alpha, true, alphaCommunity.community.id);
// Make sure the follow response went through
expect(follow.community_view.community.local).toBe(false);
let removeCommunityRes = await removeCommunity(
beta,
true,
communityRes.community_view.community.id,
);
2020-12-20 21:16:57 +00:00
expect(removeCommunityRes.community_view.community.removed).toBe(true);
expect(removeCommunityRes.community_view.community.title).toBe(
communityRes.community_view.community.title,
);
// Make sure it got Removed on A
2020-12-20 21:16:57 +00:00
let communityOnAlphaRemoved = await getCommunity(
alpha,
alphaCommunity.community.id,
2020-12-20 21:16:57 +00:00
);
expect(communityOnAlphaRemoved.community_view.community.removed).toBe(true);
// unremove
let unremoveCommunityRes = await removeCommunity(
beta,
false,
communityRes.community_view.community.id,
);
2020-12-20 21:16:57 +00:00
expect(unremoveCommunityRes.community_view.community.removed).toBe(false);
// Make sure it got undeleted on A
2020-12-20 21:16:57 +00:00
let communityOnAlphaUnRemoved = await getCommunity(
alpha,
alphaCommunity.community.id,
2020-12-20 21:16:57 +00:00
);
expect(communityOnAlphaUnRemoved.community_view.community.removed).toBe(
false,
2020-12-20 21:16:57 +00:00
);
});
test("Search for beta community", async () => {
let communityRes = await createCommunity(beta);
2020-12-20 21:16:57 +00:00
expect(communityRes.community_view.community.name).toBeDefined();
2020-12-20 21:16:57 +00:00
let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community;
assertCommunityFederation(alphaCommunity, communityRes.community_view);
});
test("Admin actions in remote community are not federated to origin", async () => {
// create a community on alpha
let communityRes = (await createCommunity(alpha)).community_view;
expect(communityRes.community.name).toBeDefined();
// gamma follows community and posts in it
let gammaCommunity = (
await resolveCommunity(gamma, communityRes.community.actor_id)
).community;
if (!gammaCommunity) {
throw "Missing gamma community";
}
await followCommunity(gamma, true, gammaCommunity.community.id);
gammaCommunity = (
await resolveCommunity(gamma, communityRes.community.actor_id)
).community;
if (!gammaCommunity) {
throw "Missing gamma community";
}
expect(gammaCommunity.subscribed).toBe("Subscribed");
let gammaPost = (await createPost(gamma, gammaCommunity.community.id))
.post_view;
expect(gammaPost.post.id).toBeDefined();
expect(gammaPost.creator_banned_from_community).toBe(false);
// admin of beta decides to ban gamma from community
let betaCommunity = (
await resolveCommunity(beta, communityRes.community.actor_id)
).community;
if (!betaCommunity) {
throw "Missing beta community";
}
let bannedUserInfo1 = (await getSite(gamma)).my_user?.local_user_view.person;
if (!bannedUserInfo1) {
throw "Missing banned user 1";
}
let bannedUserInfo2 = (await resolvePerson(beta, bannedUserInfo1.actor_id))
.person;
if (!bannedUserInfo2) {
throw "Missing banned user 2";
}
let banRes = await banPersonFromCommunity(
beta,
bannedUserInfo2.person.id,
betaCommunity.community.id,
true,
true,
);
expect(banRes.banned).toBe(true);
// ban doesnt federate to community's origin instance alpha
let alphaPost = (await resolvePost(alpha, gammaPost.post)).post;
expect(alphaPost?.creator_banned_from_community).toBe(false);
// and neither to gamma
let gammaPost2 = await getPost(gamma, gammaPost.post.id);
expect(gammaPost2.post_view.creator_banned_from_community).toBe(false);
});
test("moderator view", async () => {
// register a new user with their own community on alpha and post to it
let otherUser: API = {
auth: (await registerUser(alpha)).jwt ?? "",
client: alpha.client,
};
expect(otherUser.auth).not.toBe("");
let otherCommunity = (await createCommunity(otherUser)).community_view;
expect(otherCommunity.community.name).toBeDefined();
let otherPost = (await createPost(otherUser, otherCommunity.community.id))
.post_view;
expect(otherPost.post.id).toBeDefined();
// create a community and post on alpha
let alphaCommunity = (await createCommunity(alpha)).community_view;
expect(alphaCommunity.community.name).toBeDefined();
let alphaPost = (await createPost(alpha, alphaCommunity.community.id))
.post_view;
expect(alphaPost.post.id).toBeDefined();
// other user also posts on alpha's community
let otherAlphaPost = (
await createPost(otherUser, alphaCommunity.community.id)
).post_view;
expect(otherAlphaPost.post.id).toBeDefined();
// alpha lists posts on home page, should contain all posts that were made
let posts = (await getPosts(alpha)).posts;
expect(posts).toBeDefined();
let postIds = posts.map(post => post.post.id);
expect(postIds).toContain(otherPost.post.id);
expect(postIds).toContain(alphaPost.post.id);
expect(postIds).toContain(otherAlphaPost.post.id);
// in moderator view, alpha should not see otherPost, wich was posted on a community alpha doesn't moderate
posts = (await getPosts(alpha, true)).posts;
expect(posts).toBeDefined();
postIds = posts.map(post => post.post.id);
expect(postIds).not.toContain(otherPost.post.id);
expect(postIds).toContain(alphaPost.post.id);
expect(postIds).toContain(otherAlphaPost.post.id);
});
test("Get community for different casing on domain", async () => {
let communityRes = await createCommunity(alpha);
expect(communityRes.community_view.community.name).toBeDefined();
// A dupe check
let prevName = communityRes.community_view.community.name;
await expect(createCommunity(alpha, prevName)).rejects.toBe(
"community_already_exists",
);
// Cache the community on beta, make sure it has the other fields
let communityName = `${communityRes.community_view.community.name}@LEMMY-ALPHA:8541`;
let betaCommunity = (await getCommunityByName(beta, communityName))
.community_view;
assertCommunityFederation(betaCommunity, communityRes.community_view);
});