Skip to content

Commit

Permalink
Implement deleting accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
akmatchev committed Feb 28, 2024
1 parent 41ce7e7 commit d5985ac
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/api/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, CurrentUser, Get, JsonController, Param, Params, Post } from 'routing-controllers';
import { Body, CurrentUser, Get, JsonController, Param, Params, Post, Delete} from 'routing-controllers';

import { UserModel } from '../../models/UserModel';
import { UserService } from '../../services/UserService';
Expand Down Expand Up @@ -58,4 +58,9 @@ export class UserController {
async unblockUser(@Body() blockUserRequest: BlockUserRequest, @CurrentUser() user: UserModel): Promise<GetUserResponse> {
return { user: await this.userService.unblockUser(user, blockUserRequest) }
}

@Delete('id/:id/')
async deleteUser(@Params() params: UuidParam, @CurrentUser() user: UserModel): Promise<GetUserResponse> {
return { user: await this.userService.deleteUser(user, params) };
}
}
12 changes: 12 additions & 0 deletions src/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,16 @@ export class UserService {
return userRepository.unblockUser(user, blocked);
});
}

public async deleteUser(user: UserModel, params: UuidParam): Promise<UserModel> {
return this.transactions.readWrite(async (transactionalEntityManager) => {
const userRepository = Repositories.user(transactionalEntityManager);
const userToDelete = await userRepository.getUserById(params.id);
if (!userToDelete) throw new NotFoundError('User not found!');
if (user.id !== userToDelete.id && !user.admin) {
throw new UnauthorizedError('User does not have permission to delete other users');
}
return userRepository.deleteUser(userToDelete);
});
}
}
60 changes: 60 additions & 0 deletions src/tests/UserTest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,64 @@ describe('user tests', () => {
expect(error.message).toBe('User is not blocked!');
}
});

test('delete users - user deletes themselves', async () => {
const admin = UserFactory.fake();
const user = UserFactory.fakeTemplate();
admin.admin = true;

await new DataFactory()
.createUsers(user)
.write();

const preDeleteUserResponse = await userController.getUsers(admin);
expect(preDeleteUserResponse.users).toHaveLength(1);

const getUserResponse = await userController.getUserById(uuidParam);
if (getUserResponse.user != undefined) {
getUserResponse.user.stars = Number(getUserResponse.user.stars);
}
expect(getUserResponse.user).toEqual(expectedUser);

const deleteUserResponse = await userController.deleteUser(uuidParam, user);
if (deleteUserResponse.user != undefined) {
deleteUserResponse.user.stars = Number(deleteUserResponse.user.stars);
}
const getUsersResponse = await userController.getUsers(admin);
expect(getUsersResponse.users).toHaveLength(0);
});

test('delete users - user deletes another user', async () => {
const admin = UserFactory.fake();
const user = UserFactory.fakeTemplate();
admin.admin = true;

await new DataFactory()
.createUsers(admin, user)
.write();

const preDeleteUserResponse = await userController.getUsers(admin);
expect(preDeleteUserResponse.users).toHaveLength(2);

const deleteUserResponse = await userController.deleteUser(uuidParam, admin);
if (deleteUserResponse.user != undefined) {
deleteUserResponse.user.stars = Number(deleteUserResponse.user.stars);
}
const getUsersResponse = await userController.getUsers(admin);
expect(getUsersResponse.users).toHaveLength(1);
});

test('delete users - user that is not an admin tries to delete another user', async () => {
const [user1, user2] = UserFactory.create(2);

await new DataFactory()
.createUsers(user1, user2)
.write();

try {
await userController.deleteUser({id: user2.id}, user1);
} catch (error) {
expect(error.message).toBe('User does not have permission to delete other users');
}
});
});

0 comments on commit d5985ac

Please sign in to comment.