From d102de1b38f2877463529c3b27bd35ffef4fd8bf Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 21 Apr 2023 15:00:01 +0200 Subject: Add runner server tests --- server/tests/api/activitypub/cleaner.ts | 33 +++++++++++------- server/tests/api/activitypub/fetch.ts | 9 +++-- server/tests/api/activitypub/refresher.ts | 14 +++++--- server/tests/api/activitypub/security.ts | 58 +++++++++++++++++-------------- 4 files changed, 70 insertions(+), 44 deletions(-) (limited to 'server/tests/api/activitypub') diff --git a/server/tests/api/activitypub/cleaner.ts b/server/tests/api/activitypub/cleaner.ts index 1c1495022..d67175e20 100644 --- a/server/tests/api/activitypub/cleaner.ts +++ b/server/tests/api/activitypub/cleaner.ts @@ -1,6 +1,7 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import { expect } from 'chai' +import { SQLCommand } from '@server/tests/shared' import { wait } from '@shared/core-utils' import { cleanupTests, @@ -13,6 +14,8 @@ import { describe('Test AP cleaner', function () { let servers: PeerTubeServer[] = [] + const sqlCommands: SQLCommand[] = [] + let videoUUID1: string let videoUUID2: string let videoUUID3: string @@ -56,6 +59,8 @@ describe('Test AP cleaner', function () { await server.videos.rate({ id: uuid, rating: 'like' }) await server.comments.createThread({ videoId: uuid, text: 'comment' }) } + + sqlCommands.push(new SQLCommand(server)) } await waitJobs(servers) @@ -75,9 +80,9 @@ describe('Test AP cleaner', function () { it('Should destroy server 3 internal likes and correctly clean them', async function () { this.timeout(20000) - await servers[2].sql.deleteAll('accountVideoRate') + await sqlCommands[2].deleteAll('accountVideoRate') for (const uuid of videoUUIDs) { - await servers[2].sql.setVideoField(uuid, 'likes', '0') + await sqlCommands[2].setVideoField(uuid, 'likes', '0') } await wait(5000) @@ -121,10 +126,10 @@ describe('Test AP cleaner', function () { it('Should destroy server 3 internal dislikes and correctly clean them', async function () { this.timeout(20000) - await servers[2].sql.deleteAll('accountVideoRate') + await sqlCommands[2].deleteAll('accountVideoRate') for (const uuid of videoUUIDs) { - await servers[2].sql.setVideoField(uuid, 'dislikes', '0') + await sqlCommands[2].setVideoField(uuid, 'dislikes', '0') } await wait(5000) @@ -148,15 +153,15 @@ describe('Test AP cleaner', function () { it('Should destroy server 3 internal shares and correctly clean them', async function () { this.timeout(20000) - const preCount = await servers[0].sql.getVideoShareCount() + const preCount = await sqlCommands[0].getVideoShareCount() expect(preCount).to.equal(6) - await servers[2].sql.deleteAll('videoShare') + await sqlCommands[2].deleteAll('videoShare') await wait(5000) await waitJobs(servers) // Still 6 because we don't have remote shares on local videos - const postCount = await servers[0].sql.getVideoShareCount() + const postCount = await sqlCommands[0].getVideoShareCount() expect(postCount).to.equal(6) }) @@ -168,7 +173,7 @@ describe('Test AP cleaner', function () { expect(total).to.equal(3) } - await servers[2].sql.deleteAll('videoComment') + await sqlCommands[2].deleteAll('videoComment') await wait(5000) await waitJobs(servers) @@ -185,7 +190,7 @@ describe('Test AP cleaner', function () { async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') { const query = `SELECT "videoId", "accountVideoRate".url FROM "accountVideoRate" ` + `INNER JOIN video ON "accountVideoRate"."videoId" = video.id AND remote IS ${remote} WHERE "accountVideoRate"."url" LIKE '${like}'` - const res = await servers[0].sql.selectQuery<{ url: string }>(query) + const res = await sqlCommands[0].selectQuery<{ url: string }>(query) for (const rate of res) { const matcher = new RegExp(`^${ofServerUrl}/accounts/root/dislikes/\\d+${urlSuffix}$`) @@ -214,7 +219,7 @@ describe('Test AP cleaner', function () { { const query = `UPDATE "accountVideoRate" SET url = url || 'stan'` - await servers[1].sql.updateQuery(query) + await sqlCommands[1].updateQuery(query) await wait(5000) await waitJobs(servers) @@ -231,7 +236,7 @@ describe('Test AP cleaner', function () { const query = `SELECT "videoId", "videoComment".url, uuid as "videoUUID" FROM "videoComment" ` + `INNER JOIN video ON "videoComment"."videoId" = video.id AND remote IS ${remote} WHERE "videoComment"."url" LIKE '${like}'` - const res = await servers[0].sql.selectQuery<{ url: string, videoUUID: string }>(query) + const res = await sqlCommands[0].selectQuery<{ url: string, videoUUID: string }>(query) for (const comment of res) { const matcher = new RegExp(`${ofServerUrl}/videos/watch/${comment.videoUUID}/comments/\\d+${urlSuffix}`) @@ -257,7 +262,7 @@ describe('Test AP cleaner', function () { { const query = `UPDATE "videoComment" SET url = url || 'kyle'` - await servers[1].sql.updateQuery(query) + await sqlCommands[1].updateQuery(query) await wait(5000) await waitJobs(servers) @@ -328,6 +333,10 @@ describe('Test AP cleaner', function () { }) after(async function () { + for (const sql of sqlCommands) { + await sql.cleanup() + } + await cleanupTests(servers) }) }) diff --git a/server/tests/api/activitypub/fetch.ts b/server/tests/api/activitypub/fetch.ts index f0caea507..3899a6a49 100644 --- a/server/tests/api/activitypub/fetch.ts +++ b/server/tests/api/activitypub/fetch.ts @@ -1,6 +1,7 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import { expect } from 'chai' +import { SQLCommand } from '@server/tests/shared' import { cleanupTests, createMultipleServers, @@ -12,6 +13,7 @@ import { describe('Test ActivityPub fetcher', function () { let servers: PeerTubeServer[] + let sqlCommandServer1: SQLCommand // --------------------------------------------------------------- @@ -34,15 +36,17 @@ describe('Test ActivityPub fetcher', function () { const { uuid } = await servers[0].videos.upload({ attributes: { name: 'bad video root' } }) await servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'video user' } }) + sqlCommandServer1 = new SQLCommand(servers[0]) + { const to = servers[0].url + '/accounts/user1' const value = servers[1].url + '/accounts/user1' - await servers[0].sql.setActorField(to, 'url', value) + await sqlCommandServer1.setActorField(to, 'url', value) } { const value = servers[2].url + '/videos/watch/' + uuid - await servers[0].sql.setVideoField(uuid, 'url', value) + await sqlCommandServer1.setVideoField(uuid, 'url', value) } }) @@ -72,6 +76,7 @@ describe('Test ActivityPub fetcher', function () { after(async function () { this.timeout(20000) + await sqlCommandServer1.cleanup() await cleanupTests(servers) }) }) diff --git a/server/tests/api/activitypub/refresher.ts b/server/tests/api/activitypub/refresher.ts index 4fb22f512..6c48b7ac8 100644 --- a/server/tests/api/activitypub/refresher.ts +++ b/server/tests/api/activitypub/refresher.ts @@ -1,5 +1,6 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ +import { SQLCommand } from '@server/tests/shared' import { wait } from '@shared/core-utils' import { HttpStatusCode, VideoPlaylistPrivacy } from '@shared/models' import { @@ -15,6 +16,7 @@ import { describe('Test AP refresher', function () { let servers: PeerTubeServer[] = [] + let sqlCommandServer2: SQLCommand let videoUUID1: string let videoUUID2: string let videoUUID3: string @@ -61,6 +63,8 @@ describe('Test AP refresher', function () { } await doubleFollow(servers[0], servers[1]) + + sqlCommandServer2 = new SQLCommand(servers[1]) }) describe('Videos refresher', function () { @@ -71,7 +75,7 @@ describe('Test AP refresher', function () { await wait(10000) // Change UUID so the remote server returns a 404 - await servers[1].sql.setVideoField(videoUUID1, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b174f') + await sqlCommandServer2.setVideoField(videoUUID1, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b174f') await servers[0].videos.get({ id: videoUUID1 }) await servers[0].videos.get({ id: videoUUID2 }) @@ -87,7 +91,7 @@ describe('Test AP refresher', function () { await killallServers([ servers[1] ]) - await servers[1].sql.setVideoField(videoUUID3, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b174e') + await sqlCommandServer2.setVideoField(videoUUID3, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b174e') // Video will need a refresh await wait(10000) @@ -113,7 +117,7 @@ describe('Test AP refresher', function () { // Change actor name so the remote server returns a 404 const to = servers[1].url + '/accounts/user2' - await servers[1].sql.setActorField(to, 'preferredUsername', 'toto') + await sqlCommandServer2.setActorField(to, 'preferredUsername', 'toto') await command.get({ accountName: 'user1@' + servers[1].host }) await command.get({ accountName: 'user2@' + servers[1].host }) @@ -133,7 +137,7 @@ describe('Test AP refresher', function () { await wait(10000) // Change UUID so the remote server returns a 404 - await servers[1].sql.setPlaylistField(playlistUUID2, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b178e') + await sqlCommandServer2.setPlaylistField(playlistUUID2, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b178e') await servers[0].playlists.get({ playlistId: playlistUUID1 }) await servers[0].playlists.get({ playlistId: playlistUUID2 }) @@ -148,6 +152,8 @@ describe('Test AP refresher', function () { after(async function () { this.timeout(10000) + await sqlCommandServer2.cleanup() + await cleanupTests(servers) }) }) diff --git a/server/tests/api/activitypub/security.ts b/server/tests/api/activitypub/security.ts index c6f171633..d6a07b87f 100644 --- a/server/tests/api/activitypub/security.ts +++ b/server/tests/api/activitypub/security.ts @@ -5,26 +5,26 @@ import { buildDigest } from '@server/helpers/peertube-crypto' import { ACTIVITY_PUB, HTTP_SIGNATURE } from '@server/initializers/constants' import { activityPubContextify } from '@server/lib/activitypub/context' import { buildGlobalHeaders, signAndContextify } from '@server/lib/activitypub/send' -import { makePOSTAPRequest } from '@server/tests/shared' +import { makePOSTAPRequest, SQLCommand } from '@server/tests/shared' import { buildAbsoluteFixturePath, wait } from '@shared/core-utils' import { HttpStatusCode } from '@shared/models' import { cleanupTests, createMultipleServers, killallServers, PeerTubeServer } from '@shared/server-commands' -function setKeysOfServer (onServer: PeerTubeServer, ofServer: PeerTubeServer, publicKey: string, privateKey: string) { - const url = ofServer.url + '/accounts/peertube' +function setKeysOfServer (onServer: SQLCommand, ofServerUrl: string, publicKey: string, privateKey: string) { + const url = ofServerUrl + '/accounts/peertube' return Promise.all([ - onServer.sql.setActorField(url, 'publicKey', publicKey), - onServer.sql.setActorField(url, 'privateKey', privateKey) + onServer.setActorField(url, 'publicKey', publicKey), + onServer.setActorField(url, 'privateKey', privateKey) ]) } -function setUpdatedAtOfServer (onServer: PeerTubeServer, ofServer: PeerTubeServer, updatedAt: string) { - const url = ofServer.url + '/accounts/peertube' +function setUpdatedAtOfServer (onServer: SQLCommand, ofServerUrl: string, updatedAt: string) { + const url = ofServerUrl + '/accounts/peertube' return Promise.all([ - onServer.sql.setActorField(url, 'createdAt', updatedAt), - onServer.sql.setActorField(url, 'updatedAt', updatedAt) + onServer.setActorField(url, 'createdAt', updatedAt), + onServer.setActorField(url, 'updatedAt', updatedAt) ]) } @@ -71,6 +71,8 @@ async function makeFollowRequest (to: { url: string }, by: { url: string, privat describe('Test ActivityPub security', function () { let servers: PeerTubeServer[] + let sqlCommands: SQLCommand[] + let url: string const keys = require(buildAbsoluteFixturePath('./ap-json/peertube/keys.json')) @@ -90,10 +92,12 @@ describe('Test ActivityPub security', function () { servers = await createMultipleServers(3) + sqlCommands = servers.map(s => new SQLCommand(s)) + url = servers[0].url + '/inbox' - await setKeysOfServer(servers[0], servers[1], keys.publicKey, null) - await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey) + await setKeysOfServer(sqlCommands[0], servers[1].url, keys.publicKey, null) + await setKeysOfServer(sqlCommands[1], servers[1].url, keys.publicKey, keys.privateKey) const to = { url: servers[0].url + '/accounts/peertube' } const by = { url: servers[1].url + '/accounts/peertube', privateKey: keys.privateKey } @@ -130,8 +134,8 @@ describe('Test ActivityPub security', function () { }) it('Should fail with bad keys', async function () { - await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey) - await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey) + await setKeysOfServer(sqlCommands[0], servers[1].url, invalidKeys.publicKey, invalidKeys.privateKey) + await setKeysOfServer(sqlCommands[1], servers[1].url, invalidKeys.publicKey, invalidKeys.privateKey) const body = await activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce') const headers = buildGlobalHeaders(body) @@ -145,8 +149,8 @@ describe('Test ActivityPub security', function () { }) it('Should reject requests without appropriate signed headers', async function () { - await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey) - await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey) + await setKeysOfServer(sqlCommands[0], servers[1].url, keys.publicKey, keys.privateKey) + await setKeysOfServer(sqlCommands[1], servers[1].url, keys.publicKey, keys.privateKey) const body = await activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce') const headers = buildGlobalHeaders(body) @@ -194,8 +198,8 @@ describe('Test ActivityPub security', function () { // Update keys of server 2 to invalid keys // Server 1 should refresh the actor and fail - await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey) - await setUpdatedAtOfServer(servers[0], servers[1], '2015-07-17 22:00:00+00') + await setKeysOfServer(sqlCommands[1], servers[1].url, invalidKeys.publicKey, invalidKeys.privateKey) + await setUpdatedAtOfServer(sqlCommands[0], servers[1].url, '2015-07-17 22:00:00+00') // Invalid peertube actor cache await killallServers([ servers[1] ]) @@ -218,9 +222,9 @@ describe('Test ActivityPub security', function () { before(async function () { this.timeout(10000) - await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey) - await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey) - await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey) + await setKeysOfServer(sqlCommands[0], servers[1].url, keys.publicKey, keys.privateKey) + await setKeysOfServer(sqlCommands[1], servers[1].url, keys.publicKey, keys.privateKey) + await setKeysOfServer(sqlCommands[2], servers[2].url, keys.publicKey, keys.privateKey) const to = { url: servers[0].url + '/accounts/peertube' } const by = { url: servers[2].url + '/accounts/peertube', privateKey: keys.privateKey } @@ -230,8 +234,8 @@ describe('Test ActivityPub security', function () { it('Should fail with bad keys', async function () { this.timeout(10000) - await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey) - await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey) + await setKeysOfServer(sqlCommands[0], servers[2].url, invalidKeys.publicKey, invalidKeys.privateKey) + await setKeysOfServer(sqlCommands[2], servers[2].url, invalidKeys.publicKey, invalidKeys.privateKey) const body = getAnnounceWithoutContext(servers[1]) body.actor = servers[2].url + '/accounts/peertube' @@ -252,8 +256,8 @@ describe('Test ActivityPub security', function () { it('Should fail with an altered body', async function () { this.timeout(10000) - await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey) - await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey) + await setKeysOfServer(sqlCommands[0], servers[2].url, keys.publicKey, keys.privateKey) + await setKeysOfServer(sqlCommands[0], servers[2].url, keys.publicKey, keys.privateKey) const body = getAnnounceWithoutContext(servers[1]) body.actor = servers[2].url + '/accounts/peertube' @@ -296,7 +300,7 @@ describe('Test ActivityPub security', function () { // Update keys of server 3 to invalid keys // Server 1 should refresh the actor and fail - await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey) + await setKeysOfServer(sqlCommands[2], servers[2].url, invalidKeys.publicKey, invalidKeys.privateKey) const body = getAnnounceWithoutContext(servers[1]) body.actor = servers[2].url + '/accounts/peertube' @@ -316,7 +320,9 @@ describe('Test ActivityPub security', function () { }) after(async function () { - this.timeout(10000) + for (const sql of sqlCommands) { + await sql.cleanup() + } await cleanupTests(servers) }) -- cgit v1.2.3