|
23 | 23 | import java.net.UnknownHostException; |
24 | 24 | import java.util.ArrayList; |
25 | 25 | import java.util.Arrays; |
| 26 | +import java.util.Collections; |
26 | 27 | import java.util.HashMap; |
27 | 28 | import java.util.List; |
28 | 29 | import java.util.Map; |
@@ -1605,4 +1606,104 @@ public void testcheckCallerApiPermissionsForUserOperationsNotAllowedApis() { |
1605 | 1606 |
|
1606 | 1607 | accountManagerImpl.checkCallerApiPermissionsForUserOrAccountOperations(accountMock); |
1607 | 1608 | } |
| 1609 | + |
| 1610 | + @Test(expected = InvalidParameterValueException.class) |
| 1611 | + public void testPasswordChangeRequiredWithSamlThrowsException() { |
| 1612 | + accountManagerImpl.createUser( |
| 1613 | + "user", "pass", "fn", "ln", "[email protected]", |
| 1614 | + "UTC", "acct", 1L, null, |
| 1615 | + User.Source.SAML2, true |
| 1616 | + ); |
| 1617 | + } |
| 1618 | + |
| 1619 | + @Test(expected = InvalidParameterValueException.class) |
| 1620 | + public void testPasswordChangeRequiredWithLdapSourceThrows() { |
| 1621 | + accountManagerImpl.createUser( |
| 1622 | + "user", "pass", "fn", "ln", "[email protected]", |
| 1623 | + "UTC", "acct", 1L, null, |
| 1624 | + User.Source.LDAP, true); |
| 1625 | + } |
| 1626 | + |
| 1627 | + @Test(expected = CloudRuntimeException.class) |
| 1628 | + public void testDomainNotFound() { |
| 1629 | + Mockito.when(_domainMgr.getDomain(1L)).thenReturn(null); |
| 1630 | + accountManagerImpl.createUser( |
| 1631 | + "user", "pass", "fn", "ln", "[email protected]", |
| 1632 | + "UTC", "acct", 1L, null, |
| 1633 | + User.Source.UNKNOWN, false); |
| 1634 | + } |
| 1635 | + |
| 1636 | + @Test(expected = CloudRuntimeException.class) |
| 1637 | + public void testCreateUserInactiveDomain() { |
| 1638 | + Mockito.when(domainVoMock.getState()).thenReturn(Domain.State.Inactive); |
| 1639 | + Mockito.when(_domainMgr.getDomain(Mockito.anyLong())).thenReturn(domainVoMock); |
| 1640 | + accountManagerImpl.createUser( |
| 1641 | + "user", "pass", "fn", "ln", "[email protected]", |
| 1642 | + "UTC", "acct", 1L, null, |
| 1643 | + User.Source.NATIVE, false); |
| 1644 | + } |
| 1645 | + |
| 1646 | + @Test(expected = InvalidParameterValueException.class) |
| 1647 | + public void testCreateUserCheckAccess() { |
| 1648 | + Mockito.when(domainVoMock.getState()).thenReturn(Domain.State.Active); |
| 1649 | + Mockito.when(_domainMgr.getDomain(Mockito.anyLong())).thenReturn(domainVoMock); |
| 1650 | + Mockito.doNothing().when(accountManagerImpl).checkAccess(Mockito.any(Account.class), Mockito.any(Domain.class)); |
| 1651 | + accountManagerImpl.createUser( |
| 1652 | + "user", "pass", "fn", "ln", "[email protected]", |
| 1653 | + "UTC", "acct", 1L, null, |
| 1654 | + User.Source.NATIVE, false); |
| 1655 | + } |
| 1656 | + |
| 1657 | + @Test(expected = InvalidParameterValueException.class) |
| 1658 | + public void testCreateUserMissingOrProjectAccount() { |
| 1659 | + Mockito.when(domainVoMock.getState()).thenReturn(Domain.State.Active); |
| 1660 | + Mockito.when(_accountDao.findEnabledAccount(Mockito.anyString(), Mockito.anyLong())).thenReturn(accountMock); |
| 1661 | + Mockito.when(accountMock.getType()).thenReturn(Account.Type.PROJECT); |
| 1662 | + Mockito.when(_domainMgr.getDomain(Mockito.anyLong())).thenReturn(domainVoMock); |
| 1663 | + Mockito.doNothing().when(accountManagerImpl).checkAccess(Mockito.any(Account.class), Mockito.any(Domain.class)); |
| 1664 | + accountManagerImpl.createUser( |
| 1665 | + "user", "pass", "fn", "ln", "[email protected]", |
| 1666 | + "UTC", "acct", 1L, null, |
| 1667 | + User.Source.NATIVE, false); |
| 1668 | + } |
| 1669 | + |
| 1670 | + @Test |
| 1671 | + public void testCreateUserSuccess() { |
| 1672 | + Account rootAdminAccount = Mockito.mock(Account.class); |
| 1673 | + Mockito.when(rootAdminAccount.getId()).thenReturn(1L); |
| 1674 | + Mockito.when(accountManagerImpl.isRootAdmin(1L)).thenReturn(true); |
| 1675 | + User callingUser = Mockito.mock(User.class); |
| 1676 | + CallContext.register(callingUser, rootAdminAccount); |
| 1677 | + |
| 1678 | + String newPassword = "newPassword"; |
| 1679 | + configureUserMockAuthenticators(newPassword); |
| 1680 | + Mockito.doNothing().when(accountManagerImpl).checkAccess(any(Account.class), any(Domain.class)); |
| 1681 | + Mockito.doReturn(accountMock).when(accountManagerImpl).getAccount(Mockito.anyLong()); |
| 1682 | + Mockito.doNothing().when(passwordPolicyMock).verifyIfPasswordCompliesWithPasswordPolicies(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong()); |
| 1683 | + Mockito.when(_domainMgr.getDomain(Mockito.anyLong())).thenReturn(domainVoMock); |
| 1684 | + Mockito.when(domainVoMock.getState()).thenReturn(Domain.State.Active); |
| 1685 | + |
| 1686 | + Mockito.when(_accountDao.findEnabledAccount(Mockito.anyString(), Mockito.anyLong())).thenReturn(accountMock); |
| 1687 | + Mockito.when(accountMock.getId()).thenReturn(10L); |
| 1688 | + Mockito.when(accountMock.getType()).thenReturn(Account.Type.NORMAL); |
| 1689 | + |
| 1690 | + Mockito.when(userAccountDaoMock.validateUsernameInDomain(Mockito.anyString(), Mockito.anyLong())).thenReturn(true); |
| 1691 | + Mockito.when(userDaoMock.findUsersByName(Mockito.anyString())).thenReturn(Collections.emptyList()); |
| 1692 | + UserVO createdUser = new UserVO(); |
| 1693 | + String userMockUUID = "userMockUUID"; |
| 1694 | + createdUser.setUuid(userMockUUID); |
| 1695 | + Mockito.when(userDaoMock.persist(Mockito.any(UserVO.class))).thenReturn(createdUser); |
| 1696 | + UserVO userResultVO = accountManagerImpl.createUser( |
| 1697 | + "user", newPassword, "fn", "ln", "[email protected]", |
| 1698 | + "UTC", "acct", 1L, null, |
| 1699 | + User.Source.NATIVE, false |
| 1700 | + ); |
| 1701 | + Assert.assertNotNull(userResultVO); |
| 1702 | + UserVO userResultPasswordChangeVO = accountManagerImpl.createUser( |
| 1703 | + "user", newPassword, "fn", "ln", "[email protected]", |
| 1704 | + "UTC", "acct", 1L, null, |
| 1705 | + User.Source.NATIVE, true |
| 1706 | + ); |
| 1707 | + Assert.assertNotNull(userResultVO); |
| 1708 | + } |
1608 | 1709 | } |
0 commit comments