Files
outline/server/routes/api/installation/installation.test.ts
codegen-sh[bot]andTom Moor 11cff77162 Add installation.create API endpoint (#9324)
* Add installation.create API endpoint

- Add new endpoint that accepts teamName, userName, userEmail
- Use accountProvisioner to create team and user
- Only allow when no existing teams exist (unauthenticated)
- Add comprehensive tests for success, failure, and validation cases
- Add schema validation with Zod
- Include rate limiting (5 per hour)
- Follow existing API patterns and conventions

* Remove changes to .env.test

* fix

* Centralize validation

* test

* test

* test

---------

Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com>
Co-authored-by: Tom Moor <tom@getoutline.com>
2025-05-29 22:52:49 -04:00

83 lines
2.4 KiB
TypeScript

import { faker } from "@faker-js/faker";
import { sequelize } from "@server/storage/database";
import { buildUser, buildTeam } from "@server/test/factories";
import { getTestServer, setSelfHosted } from "@server/test/support";
setSelfHosted();
const server = getTestServer();
describe("installation.create", () => {
// Skipped in CI because tests run in parallel and this requires a clean database state.
it.skip("should create a team when no teams exist", async () => {
await sequelize.query(
"TRUNCATE TABLE teams, users, team_domains, user_authentications RESTART IDENTITY CASCADE"
);
const res = await server.post("/api/installation.create", {
body: {
teamName: faker.company.name(),
userName: faker.person.fullName(),
userEmail: faker.internet.email().toLowerCase(),
},
redirect: "manual",
});
expect(res.status).toEqual(302);
expect(res.headers.get("location")).not.toBeNull();
});
it("should fail when teams already exist", async () => {
await buildTeam();
const res = await server.post("/api/installation.create", {
body: {
teamName: faker.company.name(),
userName: faker.person.fullName(),
userEmail: faker.internet.email().toLowerCase(),
},
});
expect(res.status).toEqual(400);
const body = await res.json();
expect(body.message).toContain("Installation already has existing teams");
});
it("should validate required fields", async () => {
const res = await server.post("/api/installation.create", {
body: {
teamName: "",
userName: "",
userEmail: "invalid-email",
},
});
expect(res.status).toEqual(400);
});
});
describe("installation.info", () => {
it("should require authentication", async () => {
const res = await server.post("/api/installation.info", {
body: {},
});
expect(res.status).toEqual(401);
});
it("should return installation information", async () => {
const user = await buildUser();
const res = await server.post("/api/installation.info", {
body: {
token: user.getJwtToken(),
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data).not.toBeFalsy();
expect(body.data.version).not.toBeFalsy();
expect(body.data.latestVersion).not.toBeFalsy();
expect(typeof body.data.versionsBehind).toBe("number");
expect(body.policies).not.toBeFalsy();
});
});