2021-03-08 13:59:20 +01:00
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const fsp = require('fs').promises;
|
|
|
|
const { rmdir } = require('./../utils')
|
|
|
|
|
|
|
|
jest.mock('got');
|
|
|
|
const md5 = require('md5')
|
|
|
|
const { receive } = require('../../src/webmention/receive')
|
2021-03-08 16:15:22 +01:00
|
|
|
const dumpdir = 'data/brainbaking.com'
|
2021-03-08 13:59:20 +01:00
|
|
|
const MockDate = require('mockdate')
|
|
|
|
|
|
|
|
describe("receive webmention process tests happy path", () => {
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
if(fs.existsSync(dumpdir)) {
|
|
|
|
rmdir(dumpdir)
|
|
|
|
}
|
2021-03-08 17:21:49 +01:00
|
|
|
fs.mkdirSync(dumpdir, {
|
|
|
|
recursive: true
|
|
|
|
})
|
2021-03-08 13:59:20 +01:00
|
|
|
|
|
|
|
MockDate.set('2020-01-01')
|
|
|
|
})
|
|
|
|
|
2021-03-08 16:15:22 +01:00
|
|
|
function asFilename(body) {
|
|
|
|
return `${dumpdir}/` + md5(`source=${body.source},target=${body.target}`)
|
|
|
|
}
|
|
|
|
|
2021-03-09 16:30:53 +01:00
|
|
|
test("receive a brid.gy webmention like", async () => {
|
|
|
|
const body = {
|
|
|
|
source: "https://brainbaking.com/valid-bridgy-like.html",
|
|
|
|
// wrapped in a a class="u-like-of" tag
|
|
|
|
target: "https://brainbaking.com/valid-indieweb-target.html"
|
|
|
|
}
|
|
|
|
await receive(body)
|
|
|
|
|
|
|
|
const result = await fsp.readFile(`${asFilename(body)}.json`, 'utf-8')
|
|
|
|
const data = JSON.parse(result)
|
|
|
|
|
|
|
|
expect(data).toEqual({
|
|
|
|
author: {
|
|
|
|
name: "Stampeding Longhorn",
|
|
|
|
picture: "https://cdn.social.linux.pizza/v1/AUTH_91eb37814936490c95da7b85993cc2ff/sociallinuxpizza/accounts/avatars/000/185/996/original/9e36da0c093cfc9b.png"
|
|
|
|
},
|
|
|
|
url: "https://chat.brainbaking.com/notice/A4nx1rFwKUJYSe4TqK#favorited-by-A4nwg4LYyh4WgrJOXg",
|
|
|
|
name: "",
|
|
|
|
type: "like",
|
|
|
|
source: body.source,
|
|
|
|
target: body.target,
|
|
|
|
// no dates in bridgy-to-mastodon likes...
|
|
|
|
published: "2020-01-01T01:00:00"
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-03-08 20:13:48 +01:00
|
|
|
test("receive a brid.gy webmention that has a url and photo without value", async () => {
|
|
|
|
const body = {
|
|
|
|
source: "https://brainbaking.com/valid-bridgy-source.html",
|
|
|
|
target: "https://brainbaking.com/valid-indieweb-target.html"
|
|
|
|
}
|
|
|
|
await receive(body)
|
|
|
|
|
|
|
|
const result = await fsp.readFile(`${asFilename(body)}.json`, 'utf-8')
|
|
|
|
const data = JSON.parse(result)
|
|
|
|
|
|
|
|
expect(data).toEqual({
|
|
|
|
author: {
|
|
|
|
name: "Stampeding Longhorn",
|
|
|
|
picture: "https://cdn.social.linux.pizza/v1/AUTH_91eb37814936490c95da7b85993cc2ff/sociallinuxpizza/accounts/avatars/000/185/996/original/9e36da0c093cfc9b.png"
|
|
|
|
},
|
|
|
|
url: "https://social.linux.pizza/@StampedingLonghorn/105821099684887793",
|
|
|
|
content: "@wouter The cat pictures are awesome. for jest tests!",
|
2021-03-09 16:18:03 +01:00
|
|
|
name: "@wouter The cat pictures are awesome. for jest tests!",
|
2021-03-09 16:30:53 +01:00
|
|
|
type: "mention",
|
2021-03-08 20:13:48 +01:00
|
|
|
source: body.source,
|
|
|
|
target: body.target,
|
|
|
|
published: "2021-03-02T16:17:18.000Z"
|
|
|
|
})
|
|
|
|
})
|
2021-03-08 13:59:20 +01:00
|
|
|
test("receive saves a JSON file of indieweb-metadata if all is valid", async () => {
|
2021-03-08 16:15:22 +01:00
|
|
|
const body = {
|
|
|
|
source: "https://brainbaking.com/valid-indieweb-source.html",
|
|
|
|
target: "https://brainbaking.com/valid-indieweb-target.html"
|
|
|
|
}
|
|
|
|
await receive(body)
|
2021-03-08 13:59:20 +01:00
|
|
|
|
2021-03-08 16:15:22 +01:00
|
|
|
const result = await fsp.readFile(`${asFilename(body)}.json`, 'utf-8')
|
2021-03-08 13:59:20 +01:00
|
|
|
const data = JSON.parse(result)
|
|
|
|
|
|
|
|
expect(data).toEqual({
|
|
|
|
author: {
|
|
|
|
name: "Wouter Groeneveld",
|
|
|
|
picture: "https://brainbaking.com//img/avatar.jpg"
|
|
|
|
},
|
2021-03-08 20:13:48 +01:00
|
|
|
url: "https://brainbaking.com/notes/2021/03/06h12m41s48/",
|
|
|
|
content: "This is cool, I just found out about valid indieweb target - so cool",
|
2021-03-09 16:18:03 +01:00
|
|
|
name: "I just learned about https://www.inklestudios.com/...",
|
2021-03-09 16:30:53 +01:00
|
|
|
type: "mention",
|
2021-03-08 16:15:22 +01:00
|
|
|
source: body.source,
|
|
|
|
target: body.target,
|
2021-03-08 13:59:20 +01:00
|
|
|
published: "2021-03-06T12:41:00"
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-03-08 16:15:22 +01:00
|
|
|
test("receive saves a JSON file of indieweb-metadata with summary as content if present", async () => {
|
|
|
|
const body = {
|
|
|
|
source: "https://brainbaking.com/valid-indieweb-source-with-summary.html",
|
|
|
|
target: "https://brainbaking.com/valid-indieweb-target.html"
|
|
|
|
}
|
|
|
|
await receive(body)
|
|
|
|
|
|
|
|
const result = await fsp.readFile(`${asFilename(body)}.json`, 'utf-8')
|
|
|
|
const data = JSON.parse(result)
|
|
|
|
|
|
|
|
expect(data).toEqual({
|
|
|
|
author: {
|
|
|
|
name: "Wouter Groeneveld",
|
|
|
|
picture: "https://brainbaking.com//img/avatar.jpg"
|
|
|
|
},
|
2021-03-09 16:18:03 +01:00
|
|
|
url: "https://brainbaking.com/notes/2021/03/06h12m41s48/",
|
|
|
|
name: "I just learned about https://www.inklestudios.com/...",
|
2021-03-08 16:15:22 +01:00
|
|
|
content: "This is cool, this is a summary!",
|
2021-03-09 16:30:53 +01:00
|
|
|
type: "mention",
|
2021-03-08 16:15:22 +01:00
|
|
|
source: body.source,
|
|
|
|
target: body.target,
|
|
|
|
published: "2021-03-06T12:41:00"
|
2021-03-08 13:59:20 +01:00
|
|
|
})
|
2021-03-08 16:15:22 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test("receive saves a JSON file of non-indieweb-data such as title if all is valid", async () => {
|
|
|
|
const body = {
|
|
|
|
source: "https://brainbaking.com/valid-nonindieweb-source.html",
|
|
|
|
target: "https://brainbaking.com/valid-indieweb-target.html"
|
|
|
|
}
|
|
|
|
await receive(body)
|
2021-03-08 13:59:20 +01:00
|
|
|
|
2021-03-08 16:15:22 +01:00
|
|
|
const result = await fsp.readFile(`${asFilename(body)}.json`, 'utf-8')
|
2021-03-08 13:59:20 +01:00
|
|
|
const data = JSON.parse(result)
|
|
|
|
|
|
|
|
expect(data).toEqual({
|
|
|
|
author: {
|
2021-03-08 16:15:22 +01:00
|
|
|
name: "https://brainbaking.com/valid-nonindieweb-source.html",
|
2021-03-08 13:59:20 +01:00
|
|
|
},
|
|
|
|
content: "Diablo 2 Twenty Years Later: A Retrospective | Jefklaks Codex",
|
2021-03-09 16:18:03 +01:00
|
|
|
name: "Diablo 2 Twenty Years Later: A Retrospective | Jefklaks Codex",
|
2021-03-09 16:30:53 +01:00
|
|
|
type: "mention",
|
2021-03-09 16:18:03 +01:00
|
|
|
url: body.source,
|
2021-03-08 16:15:22 +01:00
|
|
|
source: body.source,
|
|
|
|
target: body.target,
|
2021-03-08 13:59:20 +01:00
|
|
|
published: "2020-01-01T01:00:00"
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test("receive a target that does not point to the source does nothing", async () => {
|
2021-03-08 16:15:22 +01:00
|
|
|
const body = {
|
|
|
|
source: "https://brainbaking.com/valid-indieweb-source.html",
|
|
|
|
target: "https://brainbaking.com/valid-indieweb-source.html"
|
|
|
|
}
|
|
|
|
await receive(body)
|
|
|
|
|
|
|
|
const data = fs.readdirSync(dumpdir)
|
|
|
|
expect(data.length).toBe(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
test("receive a source that does not exist should also delete older webmention files", async () => {
|
|
|
|
const body = {
|
|
|
|
source: "https://wubanga2001.boom/lolz",
|
|
|
|
target: "https://brainbaking.com/valid-indieweb-source.html"
|
|
|
|
}
|
|
|
|
|
|
|
|
await fsp.writeFile(`${asFilename(body)}.json`, JSON.stringify({ lolz: "aha" }), 'utf-8')
|
|
|
|
await receive(body)
|
2021-03-08 13:59:20 +01:00
|
|
|
|
|
|
|
const data = fs.readdirSync(dumpdir)
|
|
|
|
expect(data.length).toBe(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|