Skip to content

Commit

Permalink
delete level on user delete testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sspenst committed May 16, 2024
1 parent 6129ede commit 35ee50a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pages/api/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ export default withAuth({
GraphModel.deleteMany({ $or: [{ source: req.user._id }, { target: req.user._id }] }, { session: session }),
// delete in keyvaluemodel where key contains userId
KeyValueModel.deleteMany({ key: { $regex: `.*${req.user._id}.*` } }, { session: session }),
// delete draft levels
LevelModel.updateMany({ userId: req.user._id, isDraft: true }, { $set: { isDeleted: true } }, { session: session }),
NotificationModel.deleteMany({ $or: [
{ source: req.user._id },
{ target: req.user._id },
Expand Down
55 changes: 54 additions & 1 deletion tests/pages/api/user/user-delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import TestId from '@root/constants/testId';
import dbConnect, { dbDisconnect } from '@root/lib/dbConnect';
import { getTokenCookieValue } from '@root/lib/getTokenCookie';
import { NextApiRequestWithAuth } from '@root/lib/withAuth';
import { CommentModel, UserModel } from '@root/models/mongoose';
import { CommentModel, LevelModel, UserModel } from '@root/models/mongoose';
import { cancelSubscription, stripe } from '@root/pages/api/subscription';
import { enableFetchMocks } from 'jest-fetch-mock';
import mongoose, { Types } from 'mongoose';
Expand Down Expand Up @@ -240,4 +240,57 @@ describe('pages/api/collection/index.ts', () => {
},
});
});
test('Deleting levels', async () => {
// draft levels should be marked as deleted
// published levels should be moved to archive

const levels = await LevelModel.find({ userId: TestId.USER }, {}, { sort: { _id: 1 } });

expect(levels.length).toBe(4);
expect(levels[0].isDraft).toBe(false);
expect(levels[1].isDraft).toBe(true);
expect(levels[2].isDraft).toBe(false);
expect(levels[3].isDeleted).toBe(true);

const archiveLevels = await LevelModel.find({ userId: TestId.ARCHIVE }, {}, { sort: { _id: 1 } });

expect(archiveLevels.length).toBe(0);

await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
method: 'DELETE',
cookies: {
token: cookie,
},
headers: {
'content-type': 'application/json',
},
} as unknown as NextApiRequestWithAuth;

await modifyUserHandler(req, res);
},
test: async ({ fetch }) => {
const res = await fetch();
const response = await res.json();

expect(response.error).toBeUndefined();
expect(response.updated).toBe(true);
expect(res.status).toBe(200);

const userLevels = await LevelModel.find({ userId: TestId.USER }, {}, { sort: { _id: 1 } });

expect(userLevels.length).toBe(2);
expect(userLevels[0].isDraft).toBeTruthy();
expect(userLevels[0].isDeleted).toBeTruthy();
expect(userLevels[1].isDeleted).toBeTruthy();

const archiveLevels = await LevelModel.find({ userId: TestId.ARCHIVE }, {}, { sort: { _id: 1 } });

expect(archiveLevels.length).toBe(2);
expect(archiveLevels[0].slug).toBe('archive/test-level-1');
expect(archiveLevels[1].slug).toBe('archive/x');
},
});
});
});

0 comments on commit 35ee50a

Please sign in to comment.