feat: allow ad-hoc creation of new teams (#3964)
Co-authored-by: Tom Moor <tom@getoutline.com>
This commit is contained in:
@@ -73,21 +73,20 @@ describe("#authenticationProviders.update", () => {
|
||||
const user = await buildAdmin({
|
||||
teamId: team.id,
|
||||
});
|
||||
await team.$create("authenticationProvider", {
|
||||
const googleProvider = await team.$create("authenticationProvider", {
|
||||
name: "google",
|
||||
providerId: uuidv4(),
|
||||
});
|
||||
const authenticationProviders = await team.$get("authenticationProviders");
|
||||
const res = await server.post("/api/authenticationProviders.update", {
|
||||
body: {
|
||||
id: authenticationProviders[0].id,
|
||||
id: googleProvider.id,
|
||||
isEnabled: false,
|
||||
token: user.getJwtToken(),
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.name).toBe("slack");
|
||||
expect(body.data.name).toBe("google");
|
||||
expect(body.data.isEnabled).toBe(false);
|
||||
expect(body.data.isConnected).toBe(true);
|
||||
});
|
||||
|
||||
@@ -30,6 +30,7 @@ router.post("authenticationProviders.update", auth(), async (ctx) => {
|
||||
assertPresent(isEnabled, "isEnabled is required");
|
||||
const { user } = ctx.state;
|
||||
const authenticationProvider = await AuthenticationProvider.findByPk(id);
|
||||
|
||||
authorize(user, "update", authenticationProvider);
|
||||
const enabled = !!isEnabled;
|
||||
|
||||
|
||||
@@ -1,9 +1,41 @@
|
||||
import env from "@server/env";
|
||||
import { TeamDomain } from "@server/models";
|
||||
import { buildAdmin, buildCollection, buildTeam } from "@server/test/factories";
|
||||
import { seed, getTestServer } from "@server/test/support";
|
||||
|
||||
const server = getTestServer();
|
||||
|
||||
describe("teams.create", () => {
|
||||
it("creates a team", async () => {
|
||||
env.DEPLOYMENT = "hosted";
|
||||
const team = await buildTeam();
|
||||
const user = await buildAdmin({ teamId: team.id });
|
||||
const res = await server.post("/api/teams.create", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
name: "new workspace",
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.team.name).toEqual("new workspace");
|
||||
expect(body.data.team.subdomain).toEqual("new-workspace");
|
||||
});
|
||||
|
||||
it("requires a cloud hosted deployment", async () => {
|
||||
env.DEPLOYMENT = "self-hosted";
|
||||
const team = await buildTeam();
|
||||
const user = await buildAdmin({ teamId: team.id });
|
||||
const res = await server.post("/api/teams.create", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
name: "new workspace",
|
||||
},
|
||||
});
|
||||
expect(res.status).toEqual(500);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#team.update", () => {
|
||||
it("should update team details", async () => {
|
||||
const { admin } = await seed();
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import invariant from "invariant";
|
||||
import Router from "koa-router";
|
||||
import { RateLimiterStrategy } from "@server/RateLimiter";
|
||||
import teamCreator from "@server/commands/teamCreator";
|
||||
import teamUpdater from "@server/commands/teamUpdater";
|
||||
import { sequelize } from "@server/database/sequelize";
|
||||
import auth from "@server/middlewares/authentication";
|
||||
import { rateLimiter } from "@server/middlewares/rateLimiter";
|
||||
import { Team, TeamDomain } from "@server/models";
|
||||
import { Event, Team, TeamDomain, User } from "@server/models";
|
||||
import { authorize } from "@server/policies";
|
||||
import { presentTeam, presentPolicies } from "@server/presenters";
|
||||
import { assertUuid } from "@server/validation";
|
||||
@@ -69,4 +72,83 @@ router.post(
|
||||
}
|
||||
);
|
||||
|
||||
router.post(
|
||||
"teams.create",
|
||||
auth(),
|
||||
rateLimiter(RateLimiterStrategy.FivePerHour),
|
||||
async (ctx) => {
|
||||
const { user } = ctx.state;
|
||||
const { name } = ctx.body;
|
||||
|
||||
const existingTeam = await Team.scope(
|
||||
"withAuthenticationProviders"
|
||||
).findByPk(user.teamId, {
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
|
||||
authorize(user, "createTeam", existingTeam);
|
||||
|
||||
const authenticationProviders = existingTeam.authenticationProviders.map(
|
||||
(provider) => {
|
||||
return {
|
||||
name: provider.name,
|
||||
providerId: provider.providerId,
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
invariant(
|
||||
authenticationProviders?.length,
|
||||
"Team must have at least one authentication provider"
|
||||
);
|
||||
|
||||
const [team, newUser] = await sequelize.transaction(async (transaction) => {
|
||||
const team = await teamCreator({
|
||||
name,
|
||||
subdomain: name,
|
||||
authenticationProviders,
|
||||
ip: ctx.ip,
|
||||
transaction,
|
||||
});
|
||||
|
||||
const newUser = await User.create(
|
||||
{
|
||||
teamId: team.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
isAdmin: true,
|
||||
avatarUrl: user.avatarUrl,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
|
||||
await Event.create(
|
||||
{
|
||||
name: "users.create",
|
||||
actorId: user.id,
|
||||
userId: newUser.id,
|
||||
teamId: newUser.teamId,
|
||||
data: {
|
||||
name: newUser.name,
|
||||
},
|
||||
ip: ctx.ip,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
|
||||
return [team, newUser];
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
success: true,
|
||||
data: {
|
||||
team: presentTeam(team),
|
||||
transferUrl: `${
|
||||
team.url
|
||||
}/auth/redirect?token=${newUser?.getTransferToken()}`,
|
||||
},
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user