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

test: [CO-822] add test for schedule agent #350

Merged
merged 12 commits into from
Oct 5, 2023
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ build

target/
**/common-passwords.txt
/store/opt/
4 changes: 4 additions & 0 deletions common/src/main/java/com/zimbra/common/localconfig/LC.java
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,10 @@ public final class LC {
@Supported
public static final KnownKey zimbra_remote_cmd_channel_timeout_min = KnownKey.newKey(10);

/** Location of mailbox create db script */
public static final KnownKey mailboxd_create_db_file =
KnownKey.newKey("${mailboxd_directory}/../db/create_database.sql");

static {
// Automatically set the key name with the variable name.
for (Field field : LC.class.getFields()) {
Expand Down
318 changes: 318 additions & 0 deletions store/db/create_database.sql

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion store/src/main/java/com/zimbra/cs/db/DbMailbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public static void createMailboxDatabase(DbConnection conn, int mailboxId, int g
throws ServiceException {
ZimbraLog.mailbox.debug("createMailboxDatabase(" + mailboxId + ")");

File file = new File(LC.mailboxd_directory.value() + "/../db/create_database.sql");
File file = new File(LC.mailboxd_create_db_file.value());

boolean succeeded = false;
PreparedStatement stmt = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: 2023 Zextras <https://www.zextras.com>
//
// SPDX-License-Identifier: AGPL-3.0-only

package com.zextras.mailbox.util;

import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
import com.unboundid.ldap.listener.InMemoryListenerConfig;
import com.unboundid.ldap.sdk.Modification;
import com.unboundid.ldap.sdk.ModificationType;
import com.zimbra.common.localconfig.LC;

public class InMemoryLdapServerTestUtil {
keshavbhatt marked this conversation as resolved.
Show resolved Hide resolved
This conversation was marked as resolved.
Show resolved Hide resolved

public static InMemoryDirectoryServer createInMemoryDirectoryServer(int ldapPort)
throws Exception {
InMemoryDirectoryServerConfig ldapServerConfig =
new InMemoryDirectoryServerConfig(
"dc=com",
"cn=config",
"cn=defaultExternal,cn=cos,cn=zimbra",
"cn=default,cn=cos,cn=zimbra",
"cn=config,cn=zimbra",
"cn=zimbra");
ldapServerConfig.addAdditionalBindCredentials("cn=config", LC.ldap_root_password.value());
ldapServerConfig.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", ldapPort));
ldapServerConfig.setSchema(null);
ldapServerConfig.setGenerateOperationalAttributes(true);

InMemoryDirectoryServer inMemoryLdapServer = new InMemoryDirectoryServer(ldapServerConfig);
inMemoryLdapServer.importFromLDIF(true, "./build/ldap/config/cn=config.ldif");
inMemoryLdapServer.importFromLDIF(false, "./build/ldap/zimbra_globalconfig.ldif");
inMemoryLdapServer.importFromLDIF(false, "./build/ldap/zimbra_defaultcos.ldif");
inMemoryLdapServer.importFromLDIF(false, "./build/ldap/zimbra_defaultexternalcos.ldif");
inMemoryLdapServer.importFromLDIF(false, "./build/ldap/carbonio.ldif");
inMemoryLdapServer.startListening();
// update password for admin
inMemoryLdapServer
.getConnection()
.modify(
"uid=zimbra,cn=admins,cn=zimbra",
new Modification(
ModificationType.REPLACE, "userPassword", LC.zimbra_ldap_password.value()));
return inMemoryLdapServer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-FileCopyrightText: 2023 Zextras <https://www.zextras.com>
//
// SPDX-License-Identifier: AGPL-3.0-only

package com.zextras.mailbox.util;

import java.util.Map;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

/** Test utility class to create a {@link Server} instance with custom port and servlets. */
public class JettyServerFactory {
This conversation was marked as resolved.
Show resolved Hide resolved

/**
* @param port listening port of the server
* @param servlets {@link Map} of servlets with key path of servlet, value {@link ServletHolder}
* @return {@link Server}
* @throws Exception
*/
public static Server create(int port, Map<String, ServletHolder> servlets) throws Exception {
Server server = new Server();
frisonisland marked this conversation as resolved.
Show resolved Hide resolved
ServerConnector connector = new ServerConnector(server);
connector.setPort(port);
ServletContextHandler servletHandler = new ServletContextHandler();
servlets.forEach((path, servlet) -> servletHandler.addServlet(servlet, path));
server.setHandler(servletHandler);
server.setConnectors(new Connector[] {connector});
return server;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// SPDX-FileCopyrightText: 2023 Zextras <https://www.zextras.com>
//
// SPDX-License-Identifier: AGPL-3.0-only

package com.zextras.mailbox.util;

import static com.zimbra.cs.account.Provisioning.SERVICE_MAILCLIENT;

import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import com.zimbra.common.account.ZAttrProvisioning;
import com.zimbra.common.localconfig.LC;
import com.zimbra.common.service.ServiceException;
import com.zimbra.cs.account.Account;
import com.zimbra.cs.account.Provisioning;
import com.zimbra.cs.account.accesscontrol.RightManager;
import com.zimbra.cs.db.DbPool;
import com.zimbra.cs.db.HSQLDB;
import com.zimbra.cs.mailbox.ScheduledTaskManager;
import com.zimbra.cs.redolog.RedoLogProvider;
import com.zimbra.cs.store.StoreManager;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

public class MailboxTestUtil {
frisonisland marked this conversation as resolved.
Show resolved Hide resolved

public static final int LDAP_PORT = 1389;
public static final String SERVER_NAME = "localhost";
public static final String DEFAULT_DOMAIN = "test.com";
private static InMemoryDirectoryServer inMemoryDirectoryServer;

public static void setUp() throws Exception {
System.setProperty("java.library.path", "../native/target");
System.setProperty(
"zimbra.config",
Objects.requireNonNull(
com.zimbra.cs.mailbox.MailboxTestUtil.class.getResource(
"/localconfig-api-test.xml"))
.getFile());

inMemoryDirectoryServer = InMemoryLdapServerTestUtil.createInMemoryDirectoryServer(LDAP_PORT);
inMemoryDirectoryServer.startListening();

LC.ldap_port.setDefault(LDAP_PORT);
LC.zimbra_class_database.setDefault(HSQLDB.class.getName());

DbPool.startup();
HSQLDB.createDatabase("");

Provisioning provisioning = Provisioning.getInstance(Provisioning.CacheMode.OFF);
provisioning.createServer(
SERVER_NAME,
new HashMap<>(Map.of(ZAttrProvisioning.A_zimbraServiceEnabled, SERVICE_MAILCLIENT)));
provisioning.createDomain(DEFAULT_DOMAIN, new HashMap<>());
RedoLogProvider.getInstance().startup();
RightManager.getInstance();
StoreManager.getInstance().startup();
ScheduledTaskManager.startup();
}

public static void tearDown() throws ServiceException {
keshavbhatt marked this conversation as resolved.
Show resolved Hide resolved
inMemoryDirectoryServer.clear();
RedoLogProvider.getInstance().shutdown();
inMemoryDirectoryServer.shutDown(true);
}

/**
* Creates a basic account for domain {@link #DEFAULT_DOMAIN} and {@link #SERVER_NAME} You can
* alter the account properties later if you need to make it an admin.
*
* @return created account
* @throws ServiceException
*/
public static Account createAccountDefaultDomain(Map<String, Object> extraAttrs)
throws ServiceException {
final HashMap<String, Object> attrs =
new HashMap<>(Map.of(Provisioning.A_zimbraMailHost, MailboxTestUtil.SERVER_NAME));
attrs.putAll(extraAttrs);
return Provisioning.getInstance()
.createAccount(UUID.randomUUID() + "@" + MailboxTestUtil.DEFAULT_DOMAIN, "password", attrs);
}
}
134 changes: 97 additions & 37 deletions store/src/test/java/com/zimbra/cs/dav/service/DavServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@

package com.zimbra.cs.dav.service;

import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.ServerSetup;
import com.zextras.mailbox.util.MailboxTestUtil;
import com.zimbra.common.account.ZAttrProvisioning;
import com.zimbra.common.service.ServiceException;
import com.zimbra.common.util.ZimbraCookie;
import com.zimbra.cs.account.Account;
import com.zimbra.cs.account.AuthToken;
import com.zimbra.cs.account.AuthTokenException;
import com.zimbra.cs.account.Provisioning;
import com.zimbra.cs.mailbox.MailboxTestUtil;
import com.zimbra.cs.mailbox.ScheduledTaskManager;
import com.zimbra.cs.db.DbPool;
import com.zimbra.cs.mailclient.smtp.SmtpConfig;
import com.zimbra.cs.service.AuthProvider;
import com.zimbra.cs.service.AuthProviderException;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
Expand All @@ -30,59 +39,96 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class DavServletTest {

private static Provisioning provisioning;
private static Account organizer;
public String testName;
public Server server;
private static final int PORT = 8090;
private static final String CALENDAR_UID = "95a5527e-df0a-4df2-b64a-7eee8e647efe";
private static final String DAV_BASE_PATH = "/dav";
private static GreenMail greenMail;
private static Account organizer;

private static Server server;
private static Provisioning provisioning;

@BeforeAll
public static void init() throws Exception {
MailboxTestUtil.initServer();
ScheduledTaskManager.startup();
public static void setUp() throws Exception {
MailboxTestUtil.setUp();
greenMail =
new GreenMail(
new ServerSetup[] {
new ServerSetup(
SmtpConfig.DEFAULT_PORT, SmtpConfig.DEFAULT_HOST, ServerSetup.PROTOCOL_SMTP)
});
greenMail.start();
provisioning = Provisioning.getInstance();
server = JettyServerFactory.createDefault();
server.start();
organizer =
provisioning.createAccount(
"organizer@" + MailboxTestUtil.DEFAULT_DOMAIN,
"password",
new HashMap<>(Map.of(ZAttrProvisioning.A_zimbraMailHost, MailboxTestUtil.SERVER_NAME)));
organizer.addAlias("alias@" + MailboxTestUtil.DEFAULT_DOMAIN);
provisioning.createAccount(
"attendee1@" + MailboxTestUtil.DEFAULT_DOMAIN,
"password",
new HashMap<>(Map.of(ZAttrProvisioning.A_zimbraMailHost, MailboxTestUtil.SERVER_NAME)));
provisioning.createAccount(
"attendee2@" + MailboxTestUtil.DEFAULT_DOMAIN,
"password",
new HashMap<>(Map.of(ZAttrProvisioning.A_zimbraMailHost, MailboxTestUtil.SERVER_NAME)));
provisioning.createAccount(
"attendee3@" + MailboxTestUtil.DEFAULT_DOMAIN,
"password",
new HashMap<>(Map.of(ZAttrProvisioning.A_zimbraMailHost, MailboxTestUtil.SERVER_NAME)));
}

private static class JettyServerFactory {

public static Server createDefault() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(PORT);
ServletContextHandler servletHandler = new ServletContextHandler();
servletHandler.addServlet(DavServlet.class, DAV_BASE_PATH + "/*");
server.setHandler(servletHandler);
server.setConnectors(new Connector[] {connector});
return server;
}
@AfterAll
public static void tearDown() throws Exception {
server.stop();
greenMail.stop();
DbPool.shutdown();
}

@AfterEach
public void tearDown() {
try {
MailboxTestUtil.clearData();
server.stop();
} catch (Exception e) {
e.printStackTrace();
}
private HttpResponse executeDavRequest(Account organizer)
throws AuthProviderException, AuthTokenException, IOException {
final AuthToken authToken = AuthProvider.getAuthToken(organizer);
String url =
"http://localhost:"
+ PORT
+ DAV_BASE_PATH
+ "/home/"
+ URLEncoder.encode(organizer.getName(), StandardCharsets.UTF_8)
+ "/Calendar/95a5527e-df0a-4df2-b64a-7eee8e647efe.ics";
BasicCookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie =
new BasicClientCookie(ZimbraCookie.authTokenCookieName(false), authToken.getEncoded());
cookie.setDomain("localhost");
cookie.setPath("/");
cookieStore.addCookie(cookie);
HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
HttpPut request = new HttpPut(url);
request.setEntity(
new InputStreamEntity(
Objects.requireNonNull(
this.getClass().getResourceAsStream("Invite_ScheduleAgent_Client.ics"))));
request.setHeader(HttpHeaders.CONTENT_TYPE, "text/calendar; charset=utf-8");
return client.execute(request);
}

@BeforeEach
public void setUp() throws Exception {
server = JettyServerFactory.createDefault();
server.start();
provisioning = Provisioning.getInstance();
organizer = provisioning.createAccount("test@test.com", "password", new HashMap<>());
@Test
void shouldNotSendNotificationWhenScheduleAgentClient()
throws IOException, ServiceException, AuthTokenException {
final Account organizer = provisioning.getAccount("alias@test.com");
final HttpResponse response = executeDavRequest(organizer);

Assertions.assertEquals(HttpStatus.SC_CREATED, response.getStatusLine().getStatusCode());
Assertions.assertEquals(0, greenMail.getReceivedMessages().length);
}

private HttpResponse createAppointmentWithCalDAV() throws Exception {
Expand Down Expand Up @@ -171,4 +217,18 @@ void shouldDeleteAppointmentUsingCalDAV() throws Exception {
Assertions.assertEquals(
HttpStatus.SC_NOT_FOUND, getAppointmentWithCalDAV().getStatusLine().getStatusCode());
}

private static class JettyServerFactory {
frisonisland marked this conversation as resolved.
Show resolved Hide resolved

public static Server createDefault() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(PORT);
ServletContextHandler servletHandler = new ServletContextHandler();
servletHandler.addServlet(DavServlet.class, DAV_BASE_PATH + "/*");
server.setHandler(servletHandler);
server.setConnectors(new Connector[] {connector});
return server;
}
}
}
Loading