Add API tests for purge user and purge post (#4183)

* Add API tests for purge user and purge post

* prettier

* fix test

* ci

---------

Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
This commit is contained in:
Nutomic 2023-11-22 16:15:06 +01:00 committed by GitHub
parent 58258414cc
commit cafeb14f1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 112 additions and 44 deletions

View File

@ -16,6 +16,7 @@ fi
./api_tests/pict-rs \ ./api_tests/pict-rs \
run -a 0.0.0.0:8080 \ run -a 0.0.0.0:8080 \
--danger-dummy-mode \ --danger-dummy-mode \
--api-key "my-pictrs-key" \
filesystem -p /tmp/pictrs/files \ filesystem -p /tmp/pictrs/files \
sled -p /tmp/pictrs/sled-repo 2>&1 & sled -p /tmp/pictrs/sled-repo 2>&1 &

View File

@ -230,10 +230,7 @@ test.skip("Remove a comment from admin and community on the same instance", asyn
}); });
test("Remove a comment from admin and community on different instance", async () => { test("Remove a comment from admin and community on different instance", async () => {
let alpha_user = await registerUser(alpha); let newAlphaApi = await registerUser(alpha, alphaUrl);
let newAlphaApi = new LemmyHttp(alphaUrl, {
headers: { Authorization: `Bearer ${alpha_user.jwt ?? ""}` },
});
// New alpha user creates a community, post, and comment. // New alpha user creates a community, post, and comment.
let newCommunity = await createCommunity(newAlphaApi); let newCommunity = await createCommunity(newAlphaApi);

View File

@ -251,10 +251,7 @@ test("Admin actions in remote community are not federated to origin", async () =
test("moderator view", async () => { test("moderator view", async () => {
// register a new user with their own community on alpha and post to it // register a new user with their own community on alpha and post to it
let registerUserRes = await registerUser(alpha); let otherUser = await registerUser(alpha, alphaUrl);
let otherUser = new LemmyHttp(alphaUrl, {
headers: { Authorization: `Bearer ${registerUserRes.jwt ?? ""}` },
});
let otherCommunity = (await createCommunity(otherUser)).community_view; let otherCommunity = (await createCommunity(otherUser)).community_view;
expect(otherCommunity.community.name).toBeDefined(); expect(otherCommunity.community.name).toBeDefined();

View File

@ -1,6 +1,5 @@
jest.setTimeout(120000); jest.setTimeout(120000);
import { LemmyHttp } from "lemmy-js-client";
import { import {
alpha, alpha,
setupLogins, setupLogins,
@ -10,8 +9,8 @@ import {
getSite, getSite,
waitUntil, waitUntil,
beta, beta,
registerUser,
betaUrl, betaUrl,
registerUser,
} from "./shared"; } from "./shared";
beforeAll(setupLogins); beforeAll(setupLogins);
@ -21,11 +20,7 @@ afterAll(() => {
}); });
test("Follow local community", async () => { test("Follow local community", async () => {
let userRes = await registerUser(beta); let user = await registerUser(beta, betaUrl);
expect(userRes.jwt).toBeDefined();
let user = new LemmyHttp(betaUrl, {
headers: { Authorization: `Bearer ${userRes.jwt ?? ""}` },
});
let community = (await resolveBetaCommunity(user)).community!; let community = (await resolveBetaCommunity(user)).community!;
expect(community.counts.subscribers).toBe(1); expect(community.counts.subscribers).toBe(1);

View File

@ -1,7 +1,23 @@
jest.setTimeout(120000); jest.setTimeout(120000);
import { UploadImage, DeleteImage } from "lemmy-js-client"; import {
import { alpha, setupLogins, unfollowRemotes } from "./shared"; UploadImage,
DeleteImage,
PurgePerson,
PurgePost,
} from "lemmy-js-client";
import {
alpha,
alphaUrl,
beta,
betaUrl,
createPost,
getSite,
registerUser,
resolveBetaCommunity,
setupLogins,
unfollowRemotes,
} from "./shared";
import fs = require("fs"); import fs = require("fs");
const downloadFileSync = require("download-file-sync"); const downloadFileSync = require("download-file-sync");
@ -18,7 +34,6 @@ test("Upload image and delete it", async () => {
image: upload_image, image: upload_image,
}; };
const upload = await alpha.uploadImage(upload_form); const upload = await alpha.uploadImage(upload_form);
console.log(upload);
expect(upload.files![0].file).toBeDefined(); expect(upload.files![0].file).toBeDefined();
expect(upload.files![0].delete_token).toBeDefined(); expect(upload.files![0].delete_token).toBeDefined();
expect(upload.url).toBeDefined(); expect(upload.url).toBeDefined();
@ -41,4 +56,71 @@ test("Upload image and delete it", async () => {
expect(content2).toBe(""); expect(content2).toBe("");
}); });
// TODO: add tests for image purging test("Purge user, uploaded image removed", async () => {
let user = await registerUser(alpha, alphaUrl);
// upload test image
const upload_image = fs.readFileSync("test.png");
const upload_form: UploadImage = {
image: upload_image,
};
const upload = await user.uploadImage(upload_form);
expect(upload.files![0].file).toBeDefined();
expect(upload.files![0].delete_token).toBeDefined();
expect(upload.url).toBeDefined();
expect(upload.delete_url).toBeDefined();
// ensure that image download is working. theres probably a better way to do this
const content = downloadFileSync(upload.url);
expect(content.length).toBeGreaterThan(0);
// purge user
let site = await getSite(user);
const purge_form: PurgePerson = {
person_id: site.my_user!.local_user_view.person.id,
};
const delete_ = await alpha.purgePerson(purge_form);
expect(delete_.success).toBe(true);
// ensure that image is deleted
const content2 = downloadFileSync(upload.url);
expect(content2).toBe("");
});
test("Purge post, linked image removed", async () => {
let user = await registerUser(beta, betaUrl);
// upload test image
const upload_image = fs.readFileSync("test.png");
const upload_form: UploadImage = {
image: upload_image,
};
const upload = await user.uploadImage(upload_form);
expect(upload.files![0].file).toBeDefined();
expect(upload.files![0].delete_token).toBeDefined();
expect(upload.url).toBeDefined();
expect(upload.delete_url).toBeDefined();
// ensure that image download is working. theres probably a better way to do this
const content = downloadFileSync(upload.url);
expect(content.length).toBeGreaterThan(0);
let community = await resolveBetaCommunity(user);
let post = await createPost(
user,
community.community!.community.id,
upload.url,
);
expect(post.post_view.post.url).toBe(upload.url);
// purge post
const purge_form: PurgePost = {
post_id: post.post_view.post.id,
};
const delete_ = await beta.purgePost(purge_form);
expect(delete_.success).toBe(true);
// ensure that image is deleted
const content2 = downloadFileSync(upload.url);
expect(content2).toBe("");
});

View File

@ -390,11 +390,7 @@ test("Enforce site ban for federated user", async () => {
throw "Missing beta community"; throw "Missing beta community";
} }
// create a test user // create a test user
let alphaUserJwt = await registerUser(alpha); let alpha_user = await registerUser(alpha, alphaUrl);
expect(alphaUserJwt).toBeDefined();
let alpha_user = new LemmyHttp(alphaUrl, {
headers: { Authorization: `Bearer ${alphaUserJwt.jwt ?? ""}` },
});
let alphaUserPerson = (await getSite(alpha_user)).my_user?.local_user_view let alphaUserPerson = (await getSite(alpha_user)).my_user?.local_user_view
.person; .person;
let alphaUserActorId = alphaUserPerson?.actor_id; let alphaUserActorId = alphaUserPerson?.actor_id;

View File

@ -196,12 +196,11 @@ export async function setupLogins() {
export async function createPost( export async function createPost(
api: LemmyHttp, api: LemmyHttp,
community_id: number, community_id: number,
// use example.com for consistent title and embed description
url: string = "https://example.com/",
): Promise<PostResponse> { ): Promise<PostResponse> {
let name = randomString(5); let name = randomString(5);
let body = randomString(10); let body = randomString(10);
// switch from google.com to example.com for consistent title (embed_title and embed_description)
// google switches description when a google doodle appears
let url = "https://example.com/";
let form: CreatePost = { let form: CreatePost = {
name, name,
url, url,
@ -615,15 +614,22 @@ export async function deletePrivateMessage(
export async function registerUser( export async function registerUser(
api: LemmyHttp, api: LemmyHttp,
url: string,
username: string = randomString(5), username: string = randomString(5),
): Promise<LoginResponse> { ): Promise<LemmyHttp> {
let form: Register = { let form: Register = {
username, username,
password, password,
password_verify: password, password_verify: password,
show_nsfw: true, show_nsfw: true,
}; };
return api.register(form); let login_response = await api.register(form);
expect(login_response.jwt).toBeDefined();
let lemmy_http = new LemmyHttp(url, {
headers: { Authorization: `Bearer ${login_response.jwt ?? ""}` },
});
return lemmy_http;
} }
export async function loginUser( export async function loginUser(

View File

@ -37,11 +37,7 @@ function assertUserFederation(userOne?: PersonView, userTwo?: PersonView) {
} }
test("Create user", async () => { test("Create user", async () => {
let userRes = await registerUser(alpha); let user = await registerUser(alpha, alphaUrl);
expect(userRes.jwt).toBeDefined();
let user = new LemmyHttp(alphaUrl, {
headers: { Authorization: `Bearer ${userRes.jwt ?? ""}` },
});
let site = await getSite(user); let site = await getSite(user);
expect(site.my_user).toBeDefined(); expect(site.my_user).toBeDefined();
@ -68,11 +64,7 @@ test("Set some user settings, check that they are federated", async () => {
}); });
test("Delete user", async () => { test("Delete user", async () => {
let userRes = await registerUser(alpha); let user = await registerUser(alpha, alphaUrl);
expect(userRes.jwt).toBeDefined();
let user = new LemmyHttp(alphaUrl, {
headers: { Authorization: `Bearer ${userRes.jwt ?? ""}` },
});
// make a local post and comment // make a local post and comment
let alphaCommunity = (await resolveCommunity(user, "!main@lemmy-alpha:8541")) let alphaCommunity = (await resolveCommunity(user, "!main@lemmy-alpha:8541"))
@ -133,11 +125,7 @@ test("Requests with invalid auth should be treated as unauthenticated", async ()
}); });
test("Create user with Arabic name", async () => { test("Create user with Arabic name", async () => {
let userRes = await registerUser(alpha, "تجريب"); let user = await registerUser(alpha, alphaUrl, "تجريب");
expect(userRes.jwt).toBeDefined();
let user = new LemmyHttp(alphaUrl, {
headers: { Authorization: `Bearer ${userRes.jwt ?? ""}` },
});
let site = await getSite(user); let site = await getSite(user);
expect(site.my_user).toBeDefined(); expect(site.my_user).toBeDefined();

View File

@ -10,4 +10,7 @@
database: { database: {
host: postgres_alpha host: postgres_alpha
} }
pictrs: {
api_key: "my-pictrs-key"
}
} }

View File

@ -10,4 +10,7 @@
database: { database: {
host: postgres_beta host: postgres_beta
} }
pictrs: {
api_key: "my-pictrs-key"
}
} }