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 \
run -a 0.0.0.0:8080 \
--danger-dummy-mode \
--api-key "my-pictrs-key" \
filesystem -p /tmp/pictrs/files \
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 () => {
let alpha_user = await registerUser(alpha);
let newAlphaApi = new LemmyHttp(alphaUrl, {
headers: { Authorization: `Bearer ${alpha_user.jwt ?? ""}` },
});
let newAlphaApi = await registerUser(alpha, alphaUrl);
// New alpha user creates a community, post, and comment.
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 () => {
// register a new user with their own community on alpha and post to it
let registerUserRes = await registerUser(alpha);
let otherUser = new LemmyHttp(alphaUrl, {
headers: { Authorization: `Bearer ${registerUserRes.jwt ?? ""}` },
});
let otherUser = await registerUser(alpha, alphaUrl);
let otherCommunity = (await createCommunity(otherUser)).community_view;
expect(otherCommunity.community.name).toBeDefined();

View file

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

View file

@ -1,7 +1,23 @@
jest.setTimeout(120000);
import { UploadImage, DeleteImage } from "lemmy-js-client";
import { alpha, setupLogins, unfollowRemotes } from "./shared";
import {
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");
const downloadFileSync = require("download-file-sync");
@ -18,7 +34,6 @@ test("Upload image and delete it", async () => {
image: upload_image,
};
const upload = await alpha.uploadImage(upload_form);
console.log(upload);
expect(upload.files![0].file).toBeDefined();
expect(upload.files![0].delete_token).toBeDefined();
expect(upload.url).toBeDefined();
@ -41,4 +56,71 @@ test("Upload image and delete it", async () => {
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";
}
// create a test user
let alphaUserJwt = await registerUser(alpha);
expect(alphaUserJwt).toBeDefined();
let alpha_user = new LemmyHttp(alphaUrl, {
headers: { Authorization: `Bearer ${alphaUserJwt.jwt ?? ""}` },
});
let alpha_user = await registerUser(alpha, alphaUrl);
let alphaUserPerson = (await getSite(alpha_user)).my_user?.local_user_view
.person;
let alphaUserActorId = alphaUserPerson?.actor_id;

View file

@ -196,12 +196,11 @@ export async function setupLogins() {
export async function createPost(
api: LemmyHttp,
community_id: number,
// use example.com for consistent title and embed description
url: string = "https://example.com/",
): Promise<PostResponse> {
let name = randomString(5);
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 = {
name,
url,
@ -615,15 +614,22 @@ export async function deletePrivateMessage(
export async function registerUser(
api: LemmyHttp,
url: string,
username: string = randomString(5),
): Promise<LoginResponse> {
): Promise<LemmyHttp> {
let form: Register = {
username,
password,
password_verify: password,
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(

View file

@ -37,11 +37,7 @@ function assertUserFederation(userOne?: PersonView, userTwo?: PersonView) {
}
test("Create user", async () => {
let userRes = await registerUser(alpha);
expect(userRes.jwt).toBeDefined();
let user = new LemmyHttp(alphaUrl, {
headers: { Authorization: `Bearer ${userRes.jwt ?? ""}` },
});
let user = await registerUser(alpha, alphaUrl);
let site = await getSite(user);
expect(site.my_user).toBeDefined();
@ -68,11 +64,7 @@ test("Set some user settings, check that they are federated", async () => {
});
test("Delete user", async () => {
let userRes = await registerUser(alpha);
expect(userRes.jwt).toBeDefined();
let user = new LemmyHttp(alphaUrl, {
headers: { Authorization: `Bearer ${userRes.jwt ?? ""}` },
});
let user = await registerUser(alpha, alphaUrl);
// make a local post and comment
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 () => {
let userRes = await registerUser(alpha, "تجريب");
expect(userRes.jwt).toBeDefined();
let user = new LemmyHttp(alphaUrl, {
headers: { Authorization: `Bearer ${userRes.jwt ?? ""}` },
});
let user = await registerUser(alpha, alphaUrl, "تجريب");
let site = await getSite(user);
expect(site.my_user).toBeDefined();

View file

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

View file

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