aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-23 16:14:33 +0100
committerChocobozzz <me@florianbigard.com>2022-03-24 09:40:46 +0100
commita219c9100b3ce8774d454497d46be87465bf664e (patch)
treecaa869e47919a9e23cc86dcece1100e239683b8c /server/tests
parent7e98a7df7d04e19ba67163a86c7b876d78d76839 (diff)
downloadPeerTube-a219c9100b3ce8774d454497d46be87465bf664e.tar.gz
PeerTube-a219c9100b3ce8774d454497d46be87465bf664e.tar.zst
PeerTube-a219c9100b3ce8774d454497d46be87465bf664e.zip
Refactor AP context builder
Diffstat (limited to 'server/tests')
-rw-r--r--server/tests/api/activitypub/helpers.ts6
-rw-r--r--server/tests/api/activitypub/security.ts23
-rw-r--r--server/tests/shared/requests.ts2
3 files changed, 15 insertions, 16 deletions
diff --git a/server/tests/api/activitypub/helpers.ts b/server/tests/api/activitypub/helpers.ts
index e516cf49e..bc1de35d1 100644
--- a/server/tests/api/activitypub/helpers.ts
+++ b/server/tests/api/activitypub/helpers.ts
@@ -3,10 +3,10 @@
3import 'mocha' 3import 'mocha'
4import { expect } from 'chai' 4import { expect } from 'chai'
5import { cloneDeep } from 'lodash' 5import { cloneDeep } from 'lodash'
6import { signAndContextify } from '@server/lib/activitypub/send'
6import { buildRequestStub } from '@server/tests/shared' 7import { buildRequestStub } from '@server/tests/shared'
7import { buildAbsoluteFixturePath } from '@shared/core-utils' 8import { buildAbsoluteFixturePath } from '@shared/core-utils'
8import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../../../helpers/peertube-crypto' 9import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../../../helpers/peertube-crypto'
9import { buildSignedActivity } from '../../../lib/activitypub/activity'
10 10
11describe('Test activity pub helpers', function () { 11describe('Test activity pub helpers', function () {
12 describe('When checking the Linked Signature', function () { 12 describe('When checking the Linked Signature', function () {
@@ -46,7 +46,7 @@ describe('Test activity pub helpers', function () {
46 const body = require(buildAbsoluteFixturePath('./ap-json/peertube/announce-without-context.json')) 46 const body = require(buildAbsoluteFixturePath('./ap-json/peertube/announce-without-context.json'))
47 47
48 const actorSignature = { url: 'http://localhost:9002/accounts/peertube', privateKey: keys.privateKey } 48 const actorSignature = { url: 'http://localhost:9002/accounts/peertube', privateKey: keys.privateKey }
49 const signedBody = await buildSignedActivity(actorSignature as any, body) 49 const signedBody = await signAndContextify(actorSignature as any, body, 'Announce')
50 50
51 const fromActor = { publicKey: keys.publicKey, url: 'http://localhost:9002/accounts/peertube' } 51 const fromActor = { publicKey: keys.publicKey, url: 'http://localhost:9002/accounts/peertube' }
52 const result = await isJsonLDSignatureVerified(fromActor as any, signedBody) 52 const result = await isJsonLDSignatureVerified(fromActor as any, signedBody)
@@ -59,7 +59,7 @@ describe('Test activity pub helpers', function () {
59 const body = require(buildAbsoluteFixturePath('./ap-json/peertube/announce-without-context.json')) 59 const body = require(buildAbsoluteFixturePath('./ap-json/peertube/announce-without-context.json'))
60 60
61 const actorSignature = { url: 'http://localhost:9002/accounts/peertube', privateKey: keys.privateKey } 61 const actorSignature = { url: 'http://localhost:9002/accounts/peertube', privateKey: keys.privateKey }
62 const signedBody = await buildSignedActivity(actorSignature as any, body) 62 const signedBody = await signAndContextify(actorSignature as any, body, 'Announce')
63 63
64 const fromActor = { publicKey: keys.publicKey, url: 'http://localhost:9002/accounts/peertube' } 64 const fromActor = { publicKey: keys.publicKey, url: 'http://localhost:9002/accounts/peertube' }
65 const result = await isJsonLDSignatureVerified(fromActor as any, signedBody) 65 const result = await isJsonLDSignatureVerified(fromActor as any, signedBody)
diff --git a/server/tests/api/activitypub/security.ts b/server/tests/api/activitypub/security.ts
index da9880d7d..a070517b8 100644
--- a/server/tests/api/activitypub/security.ts
+++ b/server/tests/api/activitypub/security.ts
@@ -4,9 +4,8 @@ import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { buildDigest } from '@server/helpers/peertube-crypto' 5import { buildDigest } from '@server/helpers/peertube-crypto'
6import { HTTP_SIGNATURE } from '@server/initializers/constants' 6import { HTTP_SIGNATURE } from '@server/initializers/constants'
7import { buildSignedActivity } from '@server/lib/activitypub/activity'
8import { activityPubContextify } from '@server/lib/activitypub/context' 7import { activityPubContextify } from '@server/lib/activitypub/context'
9import { buildGlobalHeaders } from '@server/lib/job-queue/handlers/utils/activitypub-http-utils' 8import { buildGlobalHeaders, signAndContextify } from '@server/lib/activitypub/send'
10import { makeFollowRequest, makePOSTAPRequest } from '@server/tests/shared' 9import { makeFollowRequest, makePOSTAPRequest } from '@server/tests/shared'
11import { buildAbsoluteFixturePath, wait } from '@shared/core-utils' 10import { buildAbsoluteFixturePath, wait } from '@shared/core-utils'
12import { HttpStatusCode } from '@shared/models' 11import { HttpStatusCode } from '@shared/models'
@@ -81,7 +80,7 @@ describe('Test ActivityPub security', function () {
81 describe('When checking HTTP signature', function () { 80 describe('When checking HTTP signature', function () {
82 81
83 it('Should fail with an invalid digest', async function () { 82 it('Should fail with an invalid digest', async function () {
84 const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) 83 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
85 const headers = { 84 const headers = {
86 Digest: buildDigest({ hello: 'coucou' }) 85 Digest: buildDigest({ hello: 'coucou' })
87 } 86 }
@@ -95,7 +94,7 @@ describe('Test ActivityPub security', function () {
95 }) 94 })
96 95
97 it('Should fail with an invalid date', async function () { 96 it('Should fail with an invalid date', async function () {
98 const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) 97 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
99 const headers = buildGlobalHeaders(body) 98 const headers = buildGlobalHeaders(body)
100 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT' 99 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
101 100
@@ -111,7 +110,7 @@ describe('Test ActivityPub security', function () {
111 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey) 110 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
112 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey) 111 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
113 112
114 const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) 113 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
115 const headers = buildGlobalHeaders(body) 114 const headers = buildGlobalHeaders(body)
116 115
117 try { 116 try {
@@ -126,7 +125,7 @@ describe('Test ActivityPub security', function () {
126 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey) 125 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
127 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey) 126 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
128 127
129 const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) 128 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
130 const headers = buildGlobalHeaders(body) 129 const headers = buildGlobalHeaders(body)
131 130
132 const signatureOptions = baseHttpSignature() 131 const signatureOptions = baseHttpSignature()
@@ -149,7 +148,7 @@ describe('Test ActivityPub security', function () {
149 }) 148 })
150 149
151 it('Should succeed with a valid HTTP signature', async function () { 150 it('Should succeed with a valid HTTP signature', async function () {
152 const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) 151 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
153 const headers = buildGlobalHeaders(body) 152 const headers = buildGlobalHeaders(body)
154 153
155 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers) 154 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
@@ -168,7 +167,7 @@ describe('Test ActivityPub security', function () {
168 await killallServers([ servers[1] ]) 167 await killallServers([ servers[1] ])
169 await servers[1].run() 168 await servers[1].run()
170 169
171 const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) 170 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
172 const headers = buildGlobalHeaders(body) 171 const headers = buildGlobalHeaders(body)
173 172
174 try { 173 try {
@@ -204,7 +203,7 @@ describe('Test ActivityPub security', function () {
204 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube' 203 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
205 204
206 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' } 205 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
207 const signedBody = await buildSignedActivity(signer, body) 206 const signedBody = await signAndContextify(signer, body, 'Announce')
208 207
209 const headers = buildGlobalHeaders(signedBody) 208 const headers = buildGlobalHeaders(signedBody)
210 209
@@ -226,7 +225,7 @@ describe('Test ActivityPub security', function () {
226 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube' 225 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
227 226
228 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' } 227 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
229 const signedBody = await buildSignedActivity(signer, body) 228 const signedBody = await signAndContextify(signer, body, 'Announce')
230 229
231 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube' 230 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
232 231
@@ -247,7 +246,7 @@ describe('Test ActivityPub security', function () {
247 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube' 246 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
248 247
249 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' } 248 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
250 const signedBody = await buildSignedActivity(signer, body) 249 const signedBody = await signAndContextify(signer, body, 'Announce')
251 250
252 const headers = buildGlobalHeaders(signedBody) 251 const headers = buildGlobalHeaders(signedBody)
253 252
@@ -269,7 +268,7 @@ describe('Test ActivityPub security', function () {
269 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube' 268 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
270 269
271 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' } 270 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
272 const signedBody = await buildSignedActivity(signer, body) 271 const signedBody = await signAndContextify(signer, body, 'Announce')
273 272
274 const headers = buildGlobalHeaders(signedBody) 273 const headers = buildGlobalHeaders(signedBody)
275 274
diff --git a/server/tests/shared/requests.ts b/server/tests/shared/requests.ts
index d7aedf82f..57120caca 100644
--- a/server/tests/shared/requests.ts
+++ b/server/tests/shared/requests.ts
@@ -22,7 +22,7 @@ export async function makeFollowRequest (to: { url: string }, by: { url: string,
22 object: to.url 22 object: to.url
23 } 23 }
24 24
25 const body = activityPubContextify(follow) 25 const body = activityPubContextify(follow, 'Follow')
26 26
27 const httpSignature = { 27 const httpSignature = {
28 algorithm: HTTP_SIGNATURE.ALGORITHM, 28 algorithm: HTTP_SIGNATURE.ALGORITHM,