Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [CO-1463] Delete account log exception when DeleteUserUseCase th… #595

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
package com.zimbra.cs.service.admin;

import com.zextras.carbonio.message_broker.MessageBrokerClient;
import com.zextras.carbonio.message_broker.config.enums.Service;
import com.zextras.carbonio.message_broker.events.services.mailbox.UserDeleted;
import com.zextras.mailbox.account.usecase.DeleteUserUseCase;
import com.zextras.mailbox.client.ServiceDiscoverHttpClient;
import com.zimbra.common.account.Key.AccountBy;
import com.zimbra.common.service.ServiceException;
import com.zimbra.common.soap.AdminConstants;
Expand All @@ -26,10 +24,6 @@
import com.zimbra.soap.admin.message.DeleteAccountRequest;
import com.zimbra.soap.admin.message.DeleteAccountResponse;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -96,7 +90,9 @@ public Element handle(Element request, Map<String, Object> context) throws Servi
* To prevent this race condition, put the account in "maintenance" mode
* so mail delivery and any user action is blocked.
*/
deleteUserUseCase.delete(account.getId());
deleteUserUseCase.delete(account.getId()).getOrElseThrow(ex -> ServiceException.FAILURE("Delete account "
+ account.getMail() + " has an error: "
+ ex.getMessage(), ex));
publishAccountDeletedEvent(account);

ZimbraLog.security.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class DeleteAccountTest {
* to start the SoapServlet with {@link AdminService}. The reason is some code calls
* System.exit(1) presumably, so the VM exits and maven fails.
*
* @throws Exception
*/
@BeforeAll
static void setUp() throws Exception {
Expand Down Expand Up @@ -225,7 +224,7 @@ private static Stream<Arguments> getHappyPathCases() throws ServiceException {
.get());
}

private Element doDeleteAccount(Account caller, String toDeleteId) throws ServiceException {
private void doDeleteAccount(Account caller, String toDeleteId) throws ServiceException {
Map<String, Object> context = new HashMap<String, Object>();
ZimbraSoapContext zsc =
new ZimbraSoapContext(
Expand All @@ -234,7 +233,7 @@ private Element doDeleteAccount(Account caller, String toDeleteId) throws Servic
SoapProtocol.Soap12,
SoapProtocol.Soap12);
context.put(SoapEngine.ZIMBRA_CONTEXT, zsc);
return deleteAccount.handle(
deleteAccount.handle(
JaxbUtil.jaxbToElement(new DeleteAccountRequest(toDeleteId)), context);
}

Expand Down Expand Up @@ -301,4 +300,34 @@ void shouldGetPermissionDenied(Account caller, Account toDelete) throws ServiceE
Assertions.assertEquals(ServiceException.PERM_DENIED, serviceException.getCode());
Assertions.assertNotNull(provisioning.getAccountById(toDeleteId));
}

@ParameterizedTest
@MethodSource("getHappyPathCases")
void shouldDeleteUserThrowsException(Account caller, Account toDelete) throws Exception {
Mockito.when(mockMessageBrokerClient.publish(any(UserDeleted.class))).thenReturn(true);
DeleteUserUseCase deleteUserUseCase = Mockito.mock(DeleteUserUseCase.class);

final String toDeleteId = toDelete.getId();
Map<String, Object> context = new HashMap<String, Object>();
ZimbraSoapContext zsc =
new ZimbraSoapContext(
AuthProvider.getAuthToken(caller),
caller.getId(),
SoapProtocol.Soap12,
SoapProtocol.Soap12);
context.put(SoapEngine.ZIMBRA_CONTEXT, zsc);
DeleteAccount deleteAccountHandler =
new DeleteAccount(
deleteUserUseCase,
mockMessageBrokerClient);
Mockito.when(deleteUserUseCase.delete(toDeleteId)).thenReturn(Try.failure(new RuntimeException("message")));
DeleteAccountRequest deleteAccountRequest = new DeleteAccountRequest(toDeleteId);
Element request = JaxbUtil.jaxbToElement(deleteAccountRequest);
final ServiceException serviceException =
Assertions.assertThrows(ServiceException.class, () -> deleteAccountHandler.handle(request, context));
Assertions.assertEquals("service.FAILURE", serviceException.getCode());
Assertions.assertTrue(serviceException.getMessage().startsWith("system failure: Delete account "));
Assertions.assertTrue(serviceException.getMessage().endsWith("has an error: message"));
}

}