3 const fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
4 const assert = require('assert');
5 const persistence = require('gitter-web-persistence');
6 const User = persistence.User;
7 const Identity = persistence.Identity;
8 const TroupeUser = persistence.TroupeUser;
9 const OAuthAccessToken = persistence.OAuthAccessToken;
10 const userRemovalService = require('../lib/user-removal-service');
12 describe('user-removal-service', function() {
13 var fixture = fixtureLoader.setupEach({
19 providerKey: 'abcd1234'
22 oAuthAccessToken1: { user: 'user1', client: 'oAuthClient1' },
24 troupe1: { users: ['userWithRooms1'] },
25 troupe2: { users: ['userWithRooms1'] },
26 troupe3: { users: ['userWithRooms1'] },
27 troupe4: { users: ['userWithRooms1'] },
28 troupe5: { users: ['userWithRooms1'] },
29 troupe6: { users: ['userWithRooms1'] },
30 troupe7: { users: ['userWithRooms1'] },
31 troupe8: { users: ['userWithRooms1'] },
34 users: ['userWithRooms1']
38 users: ['userWithRooms1']
42 users: ['userWithRooms1']
46 describe('#removeByUsername', () => {
47 it('should mark user as removed', async () => {
48 await userRemovalService
49 .removeByUsername(fixture.user1.username)
50 .then(() => User.findOne({ _id: fixture.user1._id }))
52 assert.strictEqual(user.state, 'REMOVED');
56 it('should remove and convert to ghost user when ghost option is passed', async () => {
57 assert.strictEqual(fixture.user1.identities.length, 1);
59 await userRemovalService.removeByUsername(fixture.user1.username, { ghost: true });
61 const user = await User.findOne({ _id: fixture.user1._id });
62 const identities = await Identity.find({ userId: fixture.user1._id });
64 assert.strictEqual(user.state, 'REMOVED');
65 assert.strictEqual(user.username, `ghost~${fixture.user1._id}`);
66 assert.strictEqual(user.displayName, 'Ghost');
68 assert.strictEqual(user.identities.length, 0);
69 assert.strictEqual(identities.length, 0);
72 it('should remove room membership', async () => {
73 const roomMembershipBefore = await TroupeUser.find({ userId: fixture.userWithRooms1._id });
74 assert.strictEqual(roomMembershipBefore.length, 11);
76 await userRemovalService.removeByUsername(fixture.userWithRooms1.username);
78 const roomMembershipAfter = await TroupeUser.find({ userId: fixture.userWithRooms1._id });
79 assert.strictEqual(roomMembershipAfter.length, 0);
82 it('should remove access tokens', async () => {
83 const accessTokensBefore = await OAuthAccessToken.find({
84 userId: fixture.user1._id,
85 clientId: fixture.oAuthClient1._id
87 assert.strictEqual(accessTokensBefore.length, 1);
89 await userRemovalService.removeByUsername(fixture.user1.username);
91 const accessTokensAfter = await OAuthAccessToken.find({
92 userId: fixture.user1._id,
93 clientId: fixture.oAuthClient1._id
95 assert.strictEqual(accessTokensAfter.length, 0);