Skip to content

Commit

Permalink
refactor(syndicator-mastodon): use masto.js
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Jul 15, 2023
1 parent 8df0565 commit b68087a
Show file tree
Hide file tree
Showing 15 changed files with 545 additions and 459 deletions.
42 changes: 39 additions & 3 deletions helpers/mock-agent/endpoint-syndicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,55 @@ export function mockClient() {
agent.disableNetConnect();
agent.enableNetConnect(/(?:127\.0\.0\.1:\d{5})/);

const origin = "https://store.example";
const statusId = "1234567890987654321";
const storeOrigin = "https://store.example";
const syndicatorOrigin = "https://mastodon.example";
const syndicatorResponseOptions = {
headers: { "Content-type": "application/json" },
};

// Create file on content store
agent
.get(origin)
.get(storeOrigin)
.intercept({ path: /\/user\/.*\.(md|jpg)/, method: "PUT" })
.reply(201);

// Update file on content store
agent
.get(origin)
.get(storeOrigin)
.intercept({ path: /\/user\/.*\.(md|jpg)/, method: "PATCH" })
.reply(201);

// Get instance information from syndication target
agent
.get(syndicatorOrigin)
.intercept({
path: `/api/v1/instance`,
})
.reply(
200,
{
uri: syndicatorOrigin,
urls: { streaming_api: "https://streaming.mastodon.example" },
version: "4.1.2",
},
syndicatorResponseOptions
)
.persist();

// Post status to syndication target
agent
.get(syndicatorOrigin)
.intercept({ path: `/api/v1/statuses`, method: "POST" })
.reply(
200,
{
id: statusId,
url: `https://mastodon.example/@username/${statusId}`,
},
syndicatorResponseOptions
)
.persist();

return agent;
}
126 changes: 126 additions & 0 deletions helpers/mock-agent/syndicator-mastodon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { MockAgent } from "undici";
import { getFixture } from "@indiekit-test/fixtures";

/**
* @returns {import("undici").MockAgent} Undici MockAgent
* @see {@link https://undici.nodejs.org/#/docs/api/MockAgent}
*/
export function mockClient() {
const agent = new MockAgent();
agent.disableNetConnect();

const id = "1234567890987654321";
const instanceOrigin = "https://mastodon.example";
const instanceResponse = {
uri: instanceOrigin,
urls: { streaming_api: "https://streaming.mastodon.example" },
version: "4.1.2",
};
const statusResponse = {
id,
url: `https://mastodon.example/@username/${id}`,
};
const responseOptions = {
headers: { "Content-type": "application/json" },
};
const websiteOrigin = "https://website.example";

// Instance information
agent
.get(instanceOrigin)
.intercept({
path: `/api/v1/instance`,
headers: { authorization: "Bearer token" },
})
.reply(200, instanceResponse, responseOptions)
.persist();

// Instance information (Unauthorized, invalid access token)
agent
.get(instanceOrigin)
.intercept({
path: `/api/v1/instance`,
headers: {
authorization: "Bearer invalid",
},
})
.reply(401)
.persist();

// Instance information (Unauthorized, no access token provided)
agent
.get(instanceOrigin)
.intercept({
path: `/api/v1/instance`,
})
.reply(401)
.persist();

// Post favourite
agent
.get(instanceOrigin)
.intercept({ path: `/api/v1/statuses/${id}/favourite`, method: "POST" })
.reply(200, statusResponse, responseOptions)
.persist();

// Post reblog
agent
.get(instanceOrigin)
.intercept({ path: `/api/v1/statuses/${id}/reblog`, method: "POST" })
.reply(200, statusResponse, responseOptions)
.persist();

// Post status
agent
.get(instanceOrigin)
.intercept({ path: `/api/v1/statuses`, method: "POST" })
.reply(200, statusResponse, responseOptions)
.persist();

// Media information
agent
.get(instanceOrigin)
.intercept({ path: "/api/v1/media/1" })
.reply(
200,
{ id: 1, url: `https://mastodon.example/1.jpg` },
responseOptions
)
.persist();

// Upload media
agent
.get(instanceOrigin)
.intercept({
path: `/api/v2/media`,
method: "POST",
})
.reply(202, { id: 1, type: "image" }, responseOptions)
.persist();

// Upload media (Not Found)
agent
.get(instanceOrigin)
.intercept({
path: `/api/v2/media`,
method: "POST",
})
.reply(404, { error: "Record not found" }, responseOptions)
.persist();

// Get media file
agent
.get(websiteOrigin)
.intercept({ path: "/photo1.jpg" })
.reply(200, getFixture("file-types/photo.jpg", false))
.persist();

// Get media file (Not Found)
agent
.get(websiteOrigin)
.intercept({ path: "/404.jpg" })
.reply(404, {})
.persist();

return agent;
}
Loading

0 comments on commit b68087a

Please sign in to comment.