Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route tests #43

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .c8rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"frontend",
"coverage",
"test",
"**/@types"
"**/@types",
"server.js"
]
}
2 changes: 1 addition & 1 deletion lib/createRespondingActivity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Activity } from "./Activity.js";
import { Activity } from "toot-sweet/models";
import log from "./logger.js";
import { randomUUID } from "node:crypto";

Expand Down
88 changes: 88 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import * as Sentry from "@sentry/node";
import { ProfilingIntegration } from "@sentry/profiling-node";
import * as Tracing from "@sentry/tracing";
import createExpress from "express";
import helmet from "helmet";
import {
bodyParser,
errorHandler,
ipCheckMiddleware,
logActivities,
logRequestMiddleware,
notFoundHandler,
requestLogger
} from "toot-sweet/middleware";
import { apiRouter, nodeinfoRouter, peopleRouter, wellKnownRouter } from "toot-sweet/routes";
import CreateCompression from "compression";

export const app = createExpress();




Sentry.init({
dsn: "https://[email protected]/4504277664661504",
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
profilesSampleRate: 1.0, // Profiling sample rate is relative to tracesSampleRate
tracesSampler: samplingContext => {
if (samplingContext.request?.url === "/robots.txt") {
return 0
} else {
return 1.0
}
},
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
new Tracing.Integrations.Express({ app }),
// Add profiling integration to list of integrations
new ProfilingIntegration(),
],
});

// RequestHandler creates a separate execution context using domains, so that every
// transaction/span/breadcrumb is attached to its own Hub instance
app.use(Sentry.Handlers.requestHandler());
// TracingHandler creates a trace for every incoming request
app.use(Sentry.Handlers.tracingHandler());

app.disable("x-powered-by");
drazisil marked this conversation as resolved.
Show resolved Hide resolved

app.use(ipCheckMiddleware);

apiRouter.use(CreateCompression())

app.use(helmet());
drazisil marked this conversation as resolved.
Show resolved Hide resolved
drazisil marked this conversation as resolved.
Show resolved Hide resolved
drazisil marked this conversation as resolved.
Show resolved Hide resolved
drazisil marked this conversation as resolved.
Show resolved Hide resolved
drazisil marked this conversation as resolved.
Show resolved Hide resolved
drazisil marked this conversation as resolved.
Show resolved Hide resolved

app.use(logRequestMiddleware);

app.use(bodyParser);

app.use(logActivities);

app.use("/api", apiRouter);

app.use(requestLogger);

app.use("/.well-known", wellKnownRouter);

app.use("/nodeinfo", nodeinfoRouter);

app.use("/people", peopleRouter);

// This needs to redirect to /people
app.use("/users", peopleRouter);

// static files
app.use(createExpress.static("./build"));

// custom 404
app.use(notFoundHandler);

// custom error handler
app.use(errorHandler);

13 changes: 7 additions & 6 deletions lib/json404.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
*
* @param {import("node:http").ServerResponse} response
* @param {import("express").Response} res
* @param {string} [reason]
*/
export function json404(response, reason='Not found') {
response.statusCode = 404;
response.setHeader('content-type', 'application/json');
return response.end(JSON.stringify({
export function json404(res, reason='Not found') {
res.statusCode = 404;
res.setHeader('content-type', 'application/json');
res.json({
code: 404,
reason
}));
});
res.flushHeaders()
}
3 changes: 2 additions & 1 deletion lib/middleware/errorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ log.error(logLine);
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.get("sentry") + "\n");
res.send(res.get("sentry") + "\n")
res.flushHeaders();

}
3 changes: 2 additions & 1 deletion lib/middleware/ipCheckMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const grouper = Grouper.getGrouper();
export function ipCheckMiddleware(request, response, next) {
const blockedIPsGroup = grouper.findGroup("blockedIPs");
if (blockedIPsGroup.findId (request.socket.remoteAddress ?? "")) {
return request.socket.end();
request.socket.end();
return
}
next();
}
2 changes: 1 addition & 1 deletion lib/middleware/logActivities.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Activity } from "../Activity.js";
import { Activity } from "toot-sweet/models";
import { Grouper } from "../Grouper.js";

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Activity.js → lib/models/Activity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActivityStreamObject } from "./ActivityStreamObject.js";
import { ActivityStreamObject } from "../ActivityStreamObject.js";
import Avj from "ajv";

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/models/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Activity } from "./Activity.js";
import { Person } from "./Person.js";

export { Person };
export { Person, Activity };
6 changes: 3 additions & 3 deletions lib/routes/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { Grouper } from "../Grouper.js"

const router = express.Router()

router.get("/getAll", (request, response) => {
router.get("/getAll", (request, res) => {
const groups = Grouper.getGrouper()

response.setHeader('content-type', 'application/json')
res.setHeader('content-type', 'application/json')

const allItems = groups.getAll()

return response.end(JSON.stringify(allItems))
res.json(allItems)
})


Expand Down
6 changes: 5 additions & 1 deletion lib/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { apiRouter } from "./api.js";
import { nodeinfoRouter } from "./nodeinfo.js";
import { peopleRouter } from "./people.js";
import { peopleRouter, getStatus, getPerson, getCollection, handlePOSTToCollection } from "./people.js";
import { wellKnownRouter } from "./wellknown.js";

export {
apiRouter,
nodeinfoRouter,
peopleRouter,
wellKnownRouter,
getStatus,
getPerson,
getCollection,
handlePOSTToCollection
};

6 changes: 3 additions & 3 deletions lib/routes/logsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { Queue } from "../Queue.js"

const router = express.Router()

router.get("/getAll", (request, response) => {
router.get("/getAll", (request, res) => {
const logQueue = Queue.getQueue()

response.setHeader('content-type', 'application/json')
res.setHeader('content-type', 'application/json')

const allItems = logQueue.getAll()

return response.end(JSON.stringify(allItems))
res.json(allItems)
})


Expand Down
9 changes: 5 additions & 4 deletions lib/routes/nodeinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ router.get("/2.1", getNodeinfo)
*
* @author Drazi Crendraven
* @param {Request} request
* @param {Response} response
* @param {Response} res
*/
function getNodeinfo(request, response) {
function getNodeinfo(request, res) {
const nodeInfo = JSON.stringify({
"version": "2.1",
"software": {
Expand All @@ -45,9 +45,10 @@ function getNodeinfo(request, response) {
"metadata": {}
})

response.setHeader('content-type', 'application/json');
res.setHeader('content-type', 'application/json');

return response.end(nodeInfo)
res.json(nodeInfo)
res.flushHeaders()
}

export { router as nodeinfoRouter }
Loading