@@ -1,136 +0,0 @@
|
||||
import {
|
||||
User,
|
||||
Team,
|
||||
UserAuthentication,
|
||||
AuthenticationProvider,
|
||||
} from "@server/models";
|
||||
import { flushdb } from "@server/test/support";
|
||||
import script from "./20210226232041-migrate-authentication";
|
||||
|
||||
beforeEach(() => flushdb());
|
||||
describe("#work", () => {
|
||||
it("should create authentication record for users", async () => {
|
||||
const team = await Team.create({
|
||||
name: `Test`,
|
||||
slackId: "T123",
|
||||
});
|
||||
const user = await User.create({
|
||||
email: `test@example.com`,
|
||||
name: `Test`,
|
||||
serviceId: "U123",
|
||||
teamId: team.id,
|
||||
});
|
||||
await script();
|
||||
const authProvider = await AuthenticationProvider.findOne({
|
||||
where: {
|
||||
providerId: "T123",
|
||||
},
|
||||
});
|
||||
const auth = await UserAuthentication.findOne({
|
||||
where: {
|
||||
providerId: "U123",
|
||||
},
|
||||
});
|
||||
expect(authProvider.name).toEqual("slack");
|
||||
expect(auth.userId).toEqual(user.id);
|
||||
});
|
||||
|
||||
it("should create authentication record for deleted users", async () => {
|
||||
const team = await Team.create({
|
||||
name: `Test`,
|
||||
googleId: "domain.com",
|
||||
});
|
||||
const user = await User.create({
|
||||
email: `test1@example.com`,
|
||||
name: `Test`,
|
||||
service: "google",
|
||||
serviceId: "123456789",
|
||||
teamId: team.id,
|
||||
deletedAt: new Date(),
|
||||
});
|
||||
await script();
|
||||
const authProvider = await AuthenticationProvider.findOne({
|
||||
where: {
|
||||
providerId: "domain.com",
|
||||
},
|
||||
});
|
||||
const auth = await UserAuthentication.findOne({
|
||||
where: {
|
||||
providerId: "123456789",
|
||||
},
|
||||
});
|
||||
expect(authProvider.name).toEqual("google");
|
||||
expect(auth.userId).toEqual(user.id);
|
||||
});
|
||||
|
||||
it("should create authentication record for suspended users", async () => {
|
||||
const team = await Team.create({
|
||||
name: `Test`,
|
||||
googleId: "example.com",
|
||||
});
|
||||
const user = await User.create({
|
||||
email: `test1@example.com`,
|
||||
name: `Test`,
|
||||
service: "google",
|
||||
serviceId: "123456789",
|
||||
teamId: team.id,
|
||||
suspendedAt: new Date(),
|
||||
});
|
||||
await script();
|
||||
const authProvider = await AuthenticationProvider.findOne({
|
||||
where: {
|
||||
providerId: "example.com",
|
||||
},
|
||||
});
|
||||
const auth = await UserAuthentication.findOne({
|
||||
where: {
|
||||
providerId: "123456789",
|
||||
},
|
||||
});
|
||||
expect(authProvider.name).toEqual("google");
|
||||
expect(auth.userId).toEqual(user.id);
|
||||
});
|
||||
|
||||
it("should create correct authentication record when team has both slackId and googleId", async () => {
|
||||
const team = await Team.create({
|
||||
name: `Test`,
|
||||
slackId: "T456",
|
||||
googleId: "example.com",
|
||||
});
|
||||
const user = await User.create({
|
||||
email: `test1@example.com`,
|
||||
name: `Test`,
|
||||
service: "slack",
|
||||
serviceId: "U456",
|
||||
teamId: team.id,
|
||||
});
|
||||
await script();
|
||||
const authProvider = await AuthenticationProvider.findOne({
|
||||
where: {
|
||||
providerId: "T456",
|
||||
},
|
||||
});
|
||||
const auth = await UserAuthentication.findOne({
|
||||
where: {
|
||||
providerId: "U456",
|
||||
},
|
||||
});
|
||||
expect(authProvider.name).toEqual("slack");
|
||||
expect(auth.userId).toEqual(user.id);
|
||||
});
|
||||
|
||||
it("should skip invited users", async () => {
|
||||
const team = await Team.create({
|
||||
name: `Test`,
|
||||
slackId: "T789",
|
||||
});
|
||||
await User.create({
|
||||
email: `test2@example.com`,
|
||||
name: `Test`,
|
||||
teamId: team.id,
|
||||
});
|
||||
await script();
|
||||
const count = await UserAuthentication.count();
|
||||
expect(count).toEqual(0);
|
||||
});
|
||||
});
|
||||
@@ -1,104 +0,0 @@
|
||||
import "./bootstrap";
|
||||
import Logger from "@server/logging/logger";
|
||||
import {
|
||||
Team,
|
||||
User,
|
||||
AuthenticationProvider,
|
||||
UserAuthentication,
|
||||
} from "@server/models";
|
||||
import { Op } from "../sequelize";
|
||||
|
||||
const cache = {};
|
||||
const page = 0;
|
||||
const limit = 100;
|
||||
|
||||
export default async function main(exit = false) {
|
||||
// @ts-expect-error ts-migrate(7024) FIXME: Function implicitly has return type 'any' because ... Remove this comment to see the full error message
|
||||
const work = async (page: number) => {
|
||||
Logger.info("database", "Starting authentication migration");
|
||||
const users = await User.findAll({
|
||||
limit,
|
||||
offset: page * limit,
|
||||
paranoid: false,
|
||||
order: [["createdAt", "ASC"]],
|
||||
where: {
|
||||
serviceId: {
|
||||
[Op.ne]: "email",
|
||||
},
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: Team,
|
||||
as: "team",
|
||||
required: true,
|
||||
paranoid: false,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
for (const user of users) {
|
||||
const provider = user.service;
|
||||
const providerId = user.team[`${provider}Id`];
|
||||
|
||||
if (!providerId) {
|
||||
Logger.info(
|
||||
"database",
|
||||
`User ${user.id} has serviceId ${user.serviceId}, but team ${provider}Id missing`
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (providerId.startsWith("transferred")) {
|
||||
Logger.info(
|
||||
"database",
|
||||
`skipping previously transferred ${user.team.name} (${user.team.id})`
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
let authenticationProviderId = cache[providerId];
|
||||
|
||||
if (!authenticationProviderId) {
|
||||
const [
|
||||
authenticationProvider,
|
||||
] = await AuthenticationProvider.findOrCreate({
|
||||
where: {
|
||||
name: provider,
|
||||
providerId,
|
||||
teamId: user.teamId,
|
||||
},
|
||||
});
|
||||
cache[providerId] = authenticationProviderId =
|
||||
authenticationProvider.id;
|
||||
}
|
||||
|
||||
try {
|
||||
await UserAuthentication.create({
|
||||
authenticationProviderId,
|
||||
providerId: user.serviceId,
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
});
|
||||
} catch (err) {
|
||||
Logger.info(
|
||||
"database",
|
||||
`serviceId ${user.serviceId} exists, for user ${user.id}`
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return users.length === limit ? work(page + 1) : undefined;
|
||||
};
|
||||
|
||||
await work(page);
|
||||
|
||||
if (exit) {
|
||||
Logger.info("database", "Migration complete");
|
||||
process.exit(0);
|
||||
}
|
||||
} // In the test suite we import the script rather than run via node CLI
|
||||
|
||||
if (process.env.NODE_ENV !== "test") {
|
||||
main(true);
|
||||
}
|
||||
@@ -4,17 +4,18 @@ import { flushdb } from "@server/test/support";
|
||||
import script from "./20210716000000-backfill-revisions";
|
||||
|
||||
beforeEach(() => flushdb());
|
||||
|
||||
describe("#work", () => {
|
||||
it("should create events for revisions", async () => {
|
||||
const document = await buildDocument();
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
await script();
|
||||
const event = await Event.findOne();
|
||||
expect(event.name).toEqual("revisions.create");
|
||||
expect(event.modelId).toEqual(revision.id);
|
||||
expect(event.documentId).toEqual(document.id);
|
||||
expect(event.teamId).toEqual(document.teamId);
|
||||
expect(event.createdAt).toEqual(revision.createdAt);
|
||||
expect(event!.name).toEqual("revisions.create");
|
||||
expect(event!.modelId).toEqual(revision.id);
|
||||
expect(event!.documentId).toEqual(document.id);
|
||||
expect(event!.teamId).toEqual(document.teamId);
|
||||
expect(event!.createdAt).toEqual(revision.createdAt);
|
||||
});
|
||||
|
||||
it("should create events for revisions of deleted documents", async () => {
|
||||
@@ -23,11 +24,11 @@ describe("#work", () => {
|
||||
await document.destroy();
|
||||
await script();
|
||||
const event = await Event.findOne();
|
||||
expect(event.name).toEqual("revisions.create");
|
||||
expect(event.modelId).toEqual(revision.id);
|
||||
expect(event.documentId).toEqual(document.id);
|
||||
expect(event.teamId).toEqual(document.teamId);
|
||||
expect(event.createdAt).toEqual(revision.createdAt);
|
||||
expect(event!.name).toEqual("revisions.create");
|
||||
expect(event!.modelId).toEqual(revision.id);
|
||||
expect(event!.documentId).toEqual(document.id);
|
||||
expect(event!.teamId).toEqual(document.teamId);
|
||||
expect(event!.createdAt).toEqual(revision.createdAt);
|
||||
});
|
||||
|
||||
it("should be idempotent", async () => {
|
||||
|
||||
Reference in New Issue
Block a user