Add ability to quickly create test users in development (#3764)
This commit is contained in:
58
server/routes/api/developer.ts
Normal file
58
server/routes/api/developer.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Context, Next } from "koa";
|
||||
import Router from "koa-router";
|
||||
import randomstring from "randomstring";
|
||||
import userInviter, { Invite } from "@server/commands/userInviter";
|
||||
import env from "@server/env";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import auth from "@server/middlewares/authentication";
|
||||
import { presentUser } from "@server/presenters";
|
||||
|
||||
const router = new Router();
|
||||
|
||||
function dev() {
|
||||
return async function checkDevelopmentMiddleware(ctx: Context, next: Next) {
|
||||
if (env.ENVIRONMENT !== "development") {
|
||||
throw new Error("Attempted to access development route in production");
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
}
|
||||
|
||||
router.post("developer.create_test_users", dev(), auth(), async (ctx) => {
|
||||
const { count = 10 } = ctx.body;
|
||||
const { user } = ctx.state;
|
||||
const invites = Array(count)
|
||||
.fill(0)
|
||||
.map(() => {
|
||||
const rando = randomstring.generate(10);
|
||||
|
||||
return {
|
||||
email: `${rando}@example.com`,
|
||||
name: `${rando.slice(0, 5)} Tester`,
|
||||
role: "member",
|
||||
} as Invite;
|
||||
});
|
||||
|
||||
Logger.info("utils", `Creating ${count} test users`, invites);
|
||||
|
||||
// Generate a bunch of invites
|
||||
const response = await userInviter({
|
||||
user,
|
||||
invites,
|
||||
ip: ctx.request.ip,
|
||||
});
|
||||
|
||||
// Convert from invites to active users by marking as active
|
||||
await Promise.all(
|
||||
response.users.map((user) => user.updateActiveAt(ctx.request.ip, true))
|
||||
);
|
||||
|
||||
ctx.body = {
|
||||
data: {
|
||||
users: response.users.map((user) => presentUser(user)),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -1,6 +1,7 @@
|
||||
import Koa from "koa";
|
||||
import bodyParser from "koa-body";
|
||||
import Router from "koa-router";
|
||||
import env from "@server/env";
|
||||
import { NotFoundError } from "@server/errors";
|
||||
import errorHandling from "@server/middlewares/errorHandling";
|
||||
import methodOverride from "@server/middlewares/methodOverride";
|
||||
@@ -10,6 +11,7 @@ import auth from "./auth";
|
||||
import authenticationProviders from "./authenticationProviders";
|
||||
import collections from "./collections";
|
||||
import utils from "./cron";
|
||||
import developer from "./developer";
|
||||
import documents from "./documents";
|
||||
import events from "./events";
|
||||
import fileOperationsRoute from "./fileOperations";
|
||||
@@ -70,6 +72,10 @@ router.use("/", groups.routes());
|
||||
router.use("/", fileOperationsRoute.routes());
|
||||
router.use("/", webhookSubscriptions.routes());
|
||||
|
||||
if (env.ENVIRONMENT === "development") {
|
||||
router.use("/", developer.routes());
|
||||
}
|
||||
|
||||
router.post("*", (ctx) => {
|
||||
ctx.throw(NotFoundError("Endpoint not found"));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user