Search in sources :

Example 1 with LdapUser

use of org.apache.cloudstack.ldap.LdapUser in project cloudstack by apache.

the class LdapCreateAccountCmdTest method failedCreationDueToANullResponseFromCloudstackAccountCreator.

@Test(expected = ServerApiException.class)
public void failedCreationDueToANullResponseFromCloudstackAccountCreator() throws Exception {
    // We have an LdapManager, AccountService and LdapCreateAccountCmd
    LdapUser mrMurphy = new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,ou=engineering,dc=cloudstack,dc=org", "engineering", false, null);
    when(ldapManager.getUser(nullable(String.class), isNull())).thenReturn(mrMurphy).thenReturn(mrMurphy);
    ldapCreateAccountCmd.execute();
    fail("An exception should have been thrown: " + ServerApiException.class);
}
Also used : LdapUser(org.apache.cloudstack.ldap.LdapUser) ServerApiException(org.apache.cloudstack.api.ServerApiException) Test(org.junit.Test)

Example 2 with LdapUser

use of org.apache.cloudstack.ldap.LdapUser in project cloudstack by apache.

the class LdapListUsersCmdTest method mockResponseCreation.

private void mockResponseCreation() throws NoLdapUserMatchingQueryException {
    List<LdapUser> users = new ArrayList();
    LdapUser murphy = new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", "mythical", false, null);
    LdapUser bob = new LdapUser("bob", "bob@test.com", "Robert", "Young", "cn=bob,ou=engineering,dc=cloudstack,dc=org", LOCAL_DOMAIN_NAME, false, null);
    LdapUser abhi = new LdapUser("abhi", "abhi@test.com", "Abhi", "YoungOrOld", "cn=abhi,ou=engineering,dc=cloudstack,dc=org", LOCAL_DOMAIN_NAME, false, null);
    users.add(murphy);
    users.add(bob);
    users.add(abhi);
    doReturn(users).when(ldapManager).getUsers(any());
    LdapUserResponse response = new LdapUserResponse("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null);
    doReturn(response).when(ldapManager).createLdapUserResponse(murphy);
    LdapUserResponse bobResponse = new LdapUserResponse("bob", "bob@test.com", "Robert", "Young", "cn=bob,ou=engineering,dc=cloudstack,dc=org", LOCAL_DOMAIN_NAME);
    doReturn(bobResponse).when(ldapManager).createLdapUserResponse(bob);
    LdapUserResponse abhiResponse = new LdapUserResponse("abhi", "abhi@test.com", "Abhi", "YoungOrOld", "cn=abhi,ou=engineering,dc=cloudstack,dc=org", LOCAL_DOMAIN_NAME);
    doReturn(abhiResponse).when(ldapManager).createLdapUserResponse(abhi);
}
Also used : LdapUser(org.apache.cloudstack.ldap.LdapUser) ArrayList(java.util.ArrayList) LdapUserResponse(org.apache.cloudstack.api.response.LdapUserResponse)

Example 3 with LdapUser

use of org.apache.cloudstack.ldap.LdapUser in project cloudstack by apache.

the class LdapListUsersCmdTest method isNotACloudstackUser.

/**
 * given: "We have an LdapUser and not a matching CloudstackUser"
 *  when: "isACloudstackUser is executed"
 *  then: "The result is false"
 */
@Test
public void isNotACloudstackUser() {
    doReturn(new ListResponse<UserResponse>()).when(queryService).searchForUsers(nullable(Long.class), nullable(Boolean.class));
    LdapUser ldapUser = new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null, false, null);
    boolean result = ldapListUsersCmd.isACloudstackUser(ldapUser);
    assertFalse(result);
}
Also used : UserResponse(org.apache.cloudstack.api.response.UserResponse) LdapUserResponse(org.apache.cloudstack.api.response.LdapUserResponse) LdapUser(org.apache.cloudstack.ldap.LdapUser) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with LdapUser

use of org.apache.cloudstack.ldap.LdapUser in project cloudstack by apache.

the class LinkDomainToLdapCmdTest method execute.

@Test
public void execute() throws Exception {
    // test with valid params and with admin who doesnt exist in cloudstack
    Long domainId = 1L;
    String type = "GROUP";
    String ldapDomain = "CN=test,DC=ccp,DC=Citrix,DC=com";
    short accountType = Account.ACCOUNT_TYPE_DOMAIN_ADMIN;
    String username = "admin";
    long accountId = 24;
    setHiddenField(linkDomainToLdapCmd, "ldapDomain", ldapDomain);
    setHiddenField(linkDomainToLdapCmd, "admin", username);
    setHiddenField(linkDomainToLdapCmd, "type", type);
    setHiddenField(linkDomainToLdapCmd, "domainId", domainId);
    setHiddenField(linkDomainToLdapCmd, "accountType", accountType);
    LinkDomainToLdapResponse response = new LinkDomainToLdapResponse(domainId.toString(), type, ldapDomain, (short) accountType);
    when(ldapManager.linkDomainToLdap(linkDomainToLdapCmd)).thenReturn(response);
    when(ldapManager.getUser(username, type, ldapDomain, 1L)).thenReturn(new LdapUser(username, "admin@ccp.citrix.com", "Admin", "Admin", ldapDomain, "ccp", false, null));
    when(accountService.getActiveAccountByName(username, domainId)).thenReturn(null);
    UserAccountVO userAccount = new UserAccountVO();
    userAccount.setAccountId(24);
    when(accountService.createUserAccount(eq(username), eq(""), eq("Admin"), eq("Admin"), eq("admin@ccp.citrix.com"), isNull(String.class), eq(username), eq(Account.ACCOUNT_TYPE_DOMAIN_ADMIN), eq(RoleType.DomainAdmin.getId()), eq(domainId), isNull(String.class), (java.util.Map<String, String>) isNull(), anyString(), anyString(), eq(User.Source.LDAP))).thenReturn(userAccount);
    linkDomainToLdapCmd.execute();
    LinkDomainToLdapResponse result = (LinkDomainToLdapResponse) linkDomainToLdapCmd.getResponseObject();
    assertEquals("objectName", "LinkDomainToLdap", result.getObjectName());
    assertEquals("commandName", linkDomainToLdapCmd.getCommandName(), result.getResponseName());
    assertEquals("domainId", domainId.toString(), result.getDomainId());
    assertEquals("type", type, result.getType());
    assertEquals("name", ldapDomain, result.getLdapDomain());
    assertEquals("accountId", String.valueOf(accountId), result.getAdminId());
}
Also used : UserAccountVO(com.cloud.user.UserAccountVO) LdapUser(org.apache.cloudstack.ldap.LdapUser) LinkDomainToLdapResponse(org.apache.cloudstack.api.response.LinkDomainToLdapResponse) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 5 with LdapUser

use of org.apache.cloudstack.ldap.LdapUser in project cloudstack by apache.

the class LinkAccountToLdapCmd method execute.

@Override
public void execute() throws ServerApiException {
    try {
        LinkAccountToLdapResponse response = _ldapManager.linkAccountToLdap(this);
        if (admin != null) {
            LdapUser ldapUser = null;
            try {
                ldapUser = _ldapManager.getUser(admin, type, ldapDomain, domainId);
            } catch (NoLdapUserMatchingQueryException e) {
                LOGGER.debug("no ldap user matching username " + admin + " in the given group/ou", e);
            }
            if (ldapUser != null && !ldapUser.isDisabled()) {
                Account account = _accountService.getActiveAccountByName(admin, domainId);
                if (account == null) {
                    try {
                        UserAccount userAccount = _accountService.createUserAccount(admin, "", ldapUser.getFirstname(), ldapUser.getLastname(), ldapUser.getEmail(), null, admin, Account.ACCOUNT_TYPE_DOMAIN_ADMIN, RoleType.DomainAdmin.getId(), domainId, null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), User.Source.LDAP);
                        response.setAdminId(String.valueOf(userAccount.getAccountId()));
                        LOGGER.info("created an account with name " + admin + " in the given domain " + domainId);
                    } catch (Exception e) {
                        LOGGER.info("an exception occurred while creating account with name " + admin + " in domain " + domainId, e);
                    }
                } else {
                    LOGGER.debug("an account with name " + admin + " already exists in the domain " + domainId);
                }
            } else {
                LOGGER.debug("ldap user with username " + admin + " is disabled in the given group/ou");
            }
        }
        response.setObjectName(APINAME);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } catch (final InvalidParameterValueException e) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.toString());
    }
}
Also used : Account(com.cloud.user.Account) UserAccount(com.cloud.user.UserAccount) NoLdapUserMatchingQueryException(org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException) LdapUser(org.apache.cloudstack.ldap.LdapUser) ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) LinkAccountToLdapResponse(org.apache.cloudstack.api.response.LinkAccountToLdapResponse) UserAccount(com.cloud.user.UserAccount) ServerApiException(org.apache.cloudstack.api.ServerApiException) NoLdapUserMatchingQueryException(org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException)

Aggregations

LdapUser (org.apache.cloudstack.ldap.LdapUser)14 LdapUserResponse (org.apache.cloudstack.api.response.LdapUserResponse)7 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 ServerApiException (org.apache.cloudstack.api.ServerApiException)5 NoLdapUserMatchingQueryException (org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException)4 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)3 UserAccount (com.cloud.user.UserAccount)3 Domain (com.cloud.domain.Domain)2 Account (com.cloud.user.Account)2 UserAccountVO (com.cloud.user.UserAccountVO)2 LinkAccountToLdapResponse (org.apache.cloudstack.api.response.LinkAccountToLdapResponse)2 LinkDomainToLdapResponse (org.apache.cloudstack.api.response.LinkDomainToLdapResponse)2 ListResponse (org.apache.cloudstack.api.response.ListResponse)2 Matchers.anyString (org.mockito.Matchers.anyString)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 DomainVO (com.cloud.domain.DomainVO)1 AccountResponse (org.apache.cloudstack.api.response.AccountResponse)1 UserResponse (org.apache.cloudstack.api.response.UserResponse)1 CallContext (org.apache.cloudstack.context.CallContext)1