You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by "bernardodemarco (via GitHub)" <gi...@apache.org> on 2024/07/23 12:26:53 UTC
Re: [PR] Fix `deleteAccount` API to prevent deletion of the caller [cloudstack]
bernardodemarco commented on code in PR #8743:
URL: https://github.com/apache/cloudstack/pull/8743#discussion_r1687972014
##########
server/src/test/java/com/cloud/user/AccountManagerImplTest.java:
##########
@@ -198,6 +198,38 @@ public void deleteUserAccountCleanup() {
Mockito.verify(_accountDao, Mockito.atLeastOnce()).markForCleanup(Mockito.eq(42l));
}
+ @Test (expected = InvalidParameterValueException.class)
+ public void deleteUserAccountTestIfAccountIdIsEqualToCallerIdShouldThrowException() {
+ try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
+ CallContext callContextMock = Mockito.mock(CallContext.class);
+ callContextMocked.when(CallContext::current).thenReturn(callContextMock);
+ long accountId = 1L;
+
+ Mockito.doReturn(accountVoMock).when(callContextMock).getCallingAccount();
+ Mockito.doReturn(accountVoMock).when(_accountDao).findById(Mockito.anyLong());
+ Mockito.doReturn(domainVoMock).when(_domainDao).findById(Mockito.anyLong());
+ Mockito.doReturn(1L).when(accountVoMock).getId();
+
+ accountManagerImpl.deleteUserAccount(accountId);
+ }
+ }
+ @Test
Review Comment:
```suggestion
}
@Test
```
##########
server/src/test/java/com/cloud/user/AccountManagerImplTest.java:
##########
@@ -198,6 +198,38 @@ public void deleteUserAccountCleanup() {
Mockito.verify(_accountDao, Mockito.atLeastOnce()).markForCleanup(Mockito.eq(42l));
}
+ @Test (expected = InvalidParameterValueException.class)
+ public void deleteUserAccountTestIfAccountIdIsEqualToCallerIdShouldThrowException() {
+ try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
+ CallContext callContextMock = Mockito.mock(CallContext.class);
+ callContextMocked.when(CallContext::current).thenReturn(callContextMock);
+ long accountId = 1L;
+
+ Mockito.doReturn(accountVoMock).when(callContextMock).getCallingAccount();
+ Mockito.doReturn(accountVoMock).when(_accountDao).findById(Mockito.anyLong());
+ Mockito.doReturn(domainVoMock).when(_domainDao).findById(Mockito.anyLong());
+ Mockito.doReturn(1L).when(accountVoMock).getId();
+
+ accountManagerImpl.deleteUserAccount(accountId);
+ }
+ }
+ @Test
+ public void deleteUserAccountTestIfAccountIdIsNotEqualToCallerAccountIdShouldNotThrowException() {
+ try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)){
+ CallContext callContextMock = Mockito.mock(CallContext.class);
+ callContextMocked.when((CallContext::current)).thenReturn(callContextMock);
Review Comment:
```suggestion
callContextMocked.when(CallContext::current).thenReturn(callContextMock);
```
##########
server/src/test/java/com/cloud/user/AccountManagerImplTest.java:
##########
@@ -198,6 +198,38 @@ public void deleteUserAccountCleanup() {
Mockito.verify(_accountDao, Mockito.atLeastOnce()).markForCleanup(Mockito.eq(42l));
}
+ @Test (expected = InvalidParameterValueException.class)
+ public void deleteUserAccountTestIfAccountIdIsEqualToCallerIdShouldThrowException() {
+ try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
+ CallContext callContextMock = Mockito.mock(CallContext.class);
+ callContextMocked.when(CallContext::current).thenReturn(callContextMock);
+ long accountId = 1L;
+
+ Mockito.doReturn(accountVoMock).when(callContextMock).getCallingAccount();
+ Mockito.doReturn(accountVoMock).when(_accountDao).findById(Mockito.anyLong());
+ Mockito.doReturn(domainVoMock).when(_domainDao).findById(Mockito.anyLong());
+ Mockito.doReturn(1L).when(accountVoMock).getId();
+
+ accountManagerImpl.deleteUserAccount(accountId);
+ }
+ }
+ @Test
+ public void deleteUserAccountTestIfAccountIdIsNotEqualToCallerAccountIdShouldNotThrowException() {
+ try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)){
Review Comment:
```suggestion
try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org