Skip to content

Commit 316c454

Browse files
committed
add unit tests
1 parent 71b7b81 commit 316c454

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

plugins/api/discovery/src/main/java/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class ApiDiscoveryServiceImpl extends ComponentLifecycleBase implements A
6969
List<APIChecker> _apiAccessCheckers = null;
7070
List<PluggableService> _services = null;
7171
protected static Map<String, ApiDiscoveryResponse> s_apiNameDiscoveryResponseMap = null;
72+
public static final List<String> APIS_ALLOWED_FOR_PASSWORD_CHANGE = Arrays.asList("login", "logout", "updateUser", "listUsers", "listApis");
7273

7374
@Inject
7475
AccountService accountService;
@@ -287,7 +288,7 @@ public ListResponse<? extends BaseResponse> listApis(User user, String name) {
287288
UserAccount userAccount = accountService.getUserAccountById(user.getId());
288289
Map<String, String> userAccDetails = userAccount.getDetails();
289290
if (MapUtils.isNotEmpty(userAccDetails) && "true".equalsIgnoreCase(userAccDetails.get(UserDetailVO.PasswordChangeRequired))) {
290-
apisAllowed = Arrays.asList("login", "logout", "updateUser", "listUsers", "listApis");
291+
apisAllowed = APIS_ALLOWED_FOR_PASSWORD_CHANGE;
291292
} else {
292293
if (role.getRoleType() == RoleType.Admin && role.getId() == RoleType.Admin.getId()) {
293294
logger.info(String.format("Account [%s] is Root Admin, all APIs are allowed.",

plugins/api/discovery/src/test/java/org/apache/cloudstack/discovery/ApiDiscoveryTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.cloud.user.AccountVO;
2323
import com.cloud.user.User;
2424
import com.cloud.user.UserAccount;
25+
import com.cloud.user.UserAccountVO;
2526
import com.cloud.user.UserVO;
2627

2728
import org.apache.cloudstack.acl.APIChecker;
@@ -30,6 +31,8 @@
3031
import org.apache.cloudstack.acl.RoleType;
3132
import org.apache.cloudstack.acl.RoleVO;
3233
import org.apache.cloudstack.api.response.ApiDiscoveryResponse;
34+
import org.apache.cloudstack.api.response.ListResponse;
35+
import org.junit.Assert;
3336
import org.junit.Before;
3437
import org.junit.Test;
3538
import org.junit.runner.RunWith;
@@ -40,9 +43,12 @@
4043
import org.mockito.junit.MockitoJUnitRunner;
4144

4245
import java.util.Arrays;
46+
import java.util.HashMap;
4347
import java.util.List;
4448
import java.util.Map;
4549

50+
import static org.apache.cloudstack.resourcedetail.UserDetailVO.PasswordChangeRequired;
51+
import static org.apache.cloudstack.resourcedetail.UserDetailVO.Setup2FADetail;
4652
import static org.mockito.ArgumentMatchers.any;
4753
import static org.mockito.ArgumentMatchers.anyList;
4854
import static org.mockito.ArgumentMatchers.anyLong;
@@ -138,4 +144,29 @@ public void listApisTestGetsApisAllowedToUserOnUserRole() throws PermissionDenie
138144

139145
Mockito.verify(apiCheckerMock, Mockito.times(1)).getApisAllowedToUser(any(Role.class), any(User.class), anyList());
140146
}
147+
148+
@Test
149+
public void listApisForUserWithoutEnforcedPwdChange() throws PermissionDeniedException {
150+
RoleVO userRoleVO = new RoleVO(4L, "name", RoleType.User, "description");
151+
Map<String, String> userDetails = new HashMap<>();
152+
userDetails.put(Setup2FADetail, UserAccountVO.Setup2FAstatus.ENABLED.name());
153+
Mockito.when(mockUserAccount.getDetails()).thenReturn(userDetails);
154+
Mockito.when(accountServiceMock.getAccount(Mockito.anyLong())).thenReturn(getNormalAccount());
155+
Mockito.when(roleServiceMock.findRole(Mockito.anyLong())).thenReturn(userRoleVO);
156+
discoveryServiceSpy.listApis(getTestUser(), null);
157+
Mockito.verify(apiCheckerMock, Mockito.times(1)).getApisAllowedToUser(any(Role.class), any(User.class), anyList());
158+
}
159+
160+
@Test
161+
public void listApisForUserEnforcedPwdChange() throws PermissionDeniedException {
162+
RoleVO userRoleVO = new RoleVO(4L, "name", RoleType.User, "description");
163+
Map<String, String> userDetails = new HashMap<>();
164+
userDetails.put(PasswordChangeRequired, "true");
165+
Mockito.when(mockUserAccount.getDetails()).thenReturn(userDetails);
166+
Mockito.when(accountServiceMock.getAccount(Mockito.anyLong())).thenReturn(getNormalAccount());
167+
Mockito.when(roleServiceMock.findRole(Mockito.anyLong())).thenReturn(userRoleVO);
168+
Mockito.when(apiNameDiscoveryResponseMapMock.get(Mockito.anyString())).thenReturn(Mockito.mock(ApiDiscoveryResponse.class));
169+
ListResponse<ApiDiscoveryResponse> response = (ListResponse<ApiDiscoveryResponse>) discoveryServiceSpy.listApis(getTestUser(), null);
170+
Assert.assertEquals(5, response.getResponses().size());
171+
}
141172
}

server/src/test/java/com/cloud/user/AccountManagerImplTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,19 @@ public void updateUserTestTimeZoneAndEmailNotNull() {
432432
prepareMockAndExecuteUpdateUserTest(1);
433433
}
434434

435+
@Test
436+
public void updateUserTestPwdChange() {
437+
Mockito.doReturn(true).when(UpdateUserCmdMock).isPasswordChangeRequired();
438+
Mockito.when(userVoMock.getAccountId()).thenReturn(10L);
439+
Mockito.doReturn(accountMock).when(accountManagerImpl).getAccount(10L);
440+
Mockito.when(accountMock.getAccountId()).thenReturn(10L);
441+
Mockito.doReturn(false).when(accountManagerImpl).isRootAdmin(10L);
442+
Mockito.lenient().when(accountManagerImpl.getRoleType(Mockito.eq(accountMock))).thenReturn(RoleType.User);
443+
Mockito.when(callingUser.getAccountId()).thenReturn(1L);
444+
Mockito.doReturn(true).when(accountManagerImpl).isRootAdmin(1L);
445+
prepareMockAndExecuteUpdateUserTest(0);
446+
}
447+
435448
private void prepareMockAndExecuteUpdateUserTest(int numberOfExpectedCallsForSetEmailAndSetTimeZone) {
436449
Mockito.doReturn("password").when(UpdateUserCmdMock).getPassword();
437450
Mockito.doReturn("newpassword").when(UpdateUserCmdMock).getCurrentPassword();

0 commit comments

Comments
 (0)