-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Group membership count off after suspending users in groups (#1670)
* fix: Clear group memberships when suspending a user Refactor to command * test * test
- Loading branch information
Showing
4 changed files
with
104 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// @flow | ||
import { ValidationError } from "../errors"; | ||
import { User, Event, GroupUser } from "../models"; | ||
import { sequelize } from "../sequelize"; | ||
|
||
export default async function userSuspender({ | ||
user, | ||
actorId, | ||
ip, | ||
}: { | ||
user: User, | ||
actorId: string, | ||
ip: string, | ||
}): Promise<void> { | ||
if (user.id === actorId) { | ||
throw new ValidationError("Unable to suspend the current user"); | ||
} | ||
|
||
let transaction; | ||
try { | ||
transaction = await sequelize.transaction(); | ||
|
||
await user.update({ | ||
suspendedById: actorId, | ||
suspendedAt: new Date(), | ||
}); | ||
|
||
await GroupUser.destroy({ where: { userId: user.id } }); | ||
} catch (err) { | ||
if (transaction) { | ||
await transaction.rollback(); | ||
} | ||
throw err; | ||
} | ||
|
||
await Event.create({ | ||
name: "users.suspend", | ||
actorId, | ||
userId: user.id, | ||
teamId: user.teamId, | ||
data: { name: user.name }, | ||
ip, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-disable flowtype/require-valid-file-annotation */ | ||
import { GroupUser } from "../models"; | ||
import { buildGroup, buildUser } from "../test/factories"; | ||
import { flushdb } from "../test/support"; | ||
import userSuspender from "./userSuspender"; | ||
|
||
beforeEach(() => flushdb()); | ||
|
||
describe("userSuspender", () => { | ||
const ip = "127.0.0.1"; | ||
|
||
it("should not suspend self", async () => { | ||
const user = await buildUser(); | ||
let error; | ||
|
||
try { | ||
await userSuspender({ | ||
actorId: user.id, | ||
user, | ||
ip, | ||
}); | ||
} catch (err) { | ||
error = err; | ||
} | ||
|
||
expect(error.message).toEqual("Unable to suspend the current user"); | ||
}); | ||
|
||
it("should suspend the user", async () => { | ||
const admin = await buildUser({ isAdmin: true }); | ||
const user = await buildUser({ teamId: admin.teamId }); | ||
await userSuspender({ | ||
actorId: admin.id, | ||
user, | ||
ip, | ||
}); | ||
expect(user.suspendedAt).toBeTruthy(); | ||
expect(user.suspendedById).toEqual(admin.id); | ||
}); | ||
|
||
it("should remove group memberships", async () => { | ||
const admin = await buildUser({ isAdmin: true }); | ||
const user = await buildUser({ teamId: admin.teamId }); | ||
const group = await buildGroup({ teamId: user.teamId }); | ||
|
||
await group.addUser(user, { through: { createdById: user.id } }); | ||
|
||
await userSuspender({ | ||
actorId: admin.id, | ||
user, | ||
ip, | ||
}); | ||
expect(user.suspendedAt).toBeTruthy(); | ||
expect(user.suspendedById).toEqual(admin.id); | ||
expect(await GroupUser.count()).toEqual(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters