1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-13 01:36:18 +00:00

#180: IMAP inbox synching (api)

This commit is contained in:
Benjamin Gamard
2018-02-27 14:58:37 +01:00
parent f379b4e5ab
commit 3720a881a4
14 changed files with 525 additions and 10 deletions

View File

@@ -127,6 +127,12 @@
<artifactId>subethasmtp-wiser</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

@@ -1,3 +1,3 @@
api.current_version=${project.version}
api.min_version=1.0
db.version=16
db.version=17

View File

@@ -8,6 +8,7 @@ import com.sismics.docs.core.dao.jpa.DocumentDao;
import com.sismics.docs.core.dao.jpa.FileDao;
import com.sismics.docs.core.dao.jpa.UserDao;
import com.sismics.docs.core.event.RebuildIndexAsyncEvent;
import com.sismics.docs.core.model.context.AppContext;
import com.sismics.docs.core.model.jpa.Config;
import com.sismics.docs.core.model.jpa.File;
import com.sismics.docs.core.model.jpa.User;
@@ -174,10 +175,10 @@ public class AppResource extends BaseResource {
* @apiName GetAppConfigSmtp
* @apiGroup App
* @apiSuccess {String} hostname SMTP hostname
* @apiSuccess {String} port
* @apiSuccess {String} username
* @apiSuccess {String} password
* @apiSuccess {String} from
* @apiSuccess {String} port SMTP port
* @apiSuccess {String} username SMTP username
* @apiSuccess {String} password SMTP password
* @apiSuccess {String} from From address
* @apiError (client) ForbiddenError Access denied
* @apiPermission admin
* @apiVersion 1.5.0
@@ -295,6 +296,161 @@ public class AppResource extends BaseResource {
return Response.ok().build();
}
/**
* Get the inbox configuration.
*
* @api {get} /app/config_inbox Get the inbox scanning configuration
* @apiName GetAppConfigInbox
* @apiGroup App
* @apiSuccess {Boolean} enabled True if the inbox scanning is enabled
* @apiSuccess {String} hostname IMAP hostname
* @apiSuccess {String} port IMAP port
* @apiSuccess {String} username IMAP username
* @apiSuccess {String} password IMAP password
* @apiSuccess {String} tag Tag for created documents
* @apiError (client) ForbiddenError Access denied
* @apiPermission admin
* @apiVersion 1.5.0
*
* @return Response
*/
@GET
@Path("config_inbox")
public Response getConfigInbox() {
if (!authenticate()) {
throw new ForbiddenClientException();
}
checkBaseFunction(BaseFunction.ADMIN);
ConfigDao configDao = new ConfigDao();
Boolean enabled = ConfigUtil.getConfigBooleanValue(ConfigType.INBOX_ENABLED);
Config hostnameConfig = configDao.getById(ConfigType.INBOX_HOSTNAME);
Config portConfig = configDao.getById(ConfigType.INBOX_PORT);
Config usernameConfig = configDao.getById(ConfigType.INBOX_USERNAME);
Config passwordConfig = configDao.getById(ConfigType.INBOX_PASSWORD);
Config tagConfig = configDao.getById(ConfigType.INBOX_TAG);
JsonObjectBuilder response = Json.createObjectBuilder();
response.add("enabled", enabled);
if (hostnameConfig == null) {
response.addNull("hostname");
} else {
response.add("hostname", hostnameConfig.getValue());
}
if (portConfig == null) {
response.addNull("port");
} else {
response.add("port", Integer.valueOf(portConfig.getValue()));
}
if (usernameConfig == null) {
response.addNull("username");
} else {
response.add("username", usernameConfig.getValue());
}
if (passwordConfig == null) {
response.addNull("password");
} else {
response.add("password", passwordConfig.getValue());
}
if (tagConfig == null) {
response.addNull("tag");
} else {
response.add("tag", tagConfig.getValue());
}
return Response.ok().entity(response.build()).build();
}
/**
* Configure the inbox.
*
* @api {post} /app/config_inbox Configure the inbox scanning
* @apiName PostAppConfigInbox
* @apiGroup App
* @apiParam {Boolean} enabled True if the inbox scanning is enabled
* @apiParam {String} hostname IMAP hostname
* @apiParam {Integer} port IMAP port
* @apiParam {String} username IMAP username
* @apiParam {String} password IMAP password
* @apiParam {String} tag Tag for created documents
* @apiError (client) ForbiddenError Access denied
* @apiError (client) ValidationError Validation error
* @apiPermission admin
* @apiVersion 1.5.0
*
* @param enabled True if the inbox scanning is enabled
* @param hostname IMAP hostname
* @param portStr IMAP port
* @param username IMAP username
* @param password IMAP password
* @param tag Tag for created documents
* @return Response
*/
@POST
@Path("config_inbox")
public Response configInbox(@FormParam("enabled") Boolean enabled,
@FormParam("hostname") String hostname,
@FormParam("port") String portStr,
@FormParam("username") String username,
@FormParam("password") String password,
@FormParam("tag") String tag) {
if (!authenticate()) {
throw new ForbiddenClientException();
}
checkBaseFunction(BaseFunction.ADMIN);
ValidationUtil.validateRequired(enabled, "enabled");
if (!Strings.isNullOrEmpty(portStr)) {
ValidationUtil.validateInteger(portStr, "port");
}
// Just update the changed configuration
ConfigDao configDao = new ConfigDao();
configDao.update(ConfigType.INBOX_ENABLED, enabled.toString());
if (!Strings.isNullOrEmpty(hostname)) {
configDao.update(ConfigType.INBOX_HOSTNAME, hostname);
}
if (!Strings.isNullOrEmpty(portStr)) {
configDao.update(ConfigType.INBOX_PORT, portStr);
}
if (!Strings.isNullOrEmpty(username)) {
configDao.update(ConfigType.INBOX_USERNAME, username);
}
if (!Strings.isNullOrEmpty(password)) {
configDao.update(ConfigType.INBOX_PASSWORD, password);
}
if (!Strings.isNullOrEmpty(tag)) {
configDao.update(ConfigType.INBOX_TAG, tag);
}
return Response.ok().build();
}
/**
* Test the inbox.
*
* @api {post} /app/test_inbox Test the inbox scanning
* @apiName PostAppTestInbox
* @apiGroup App
* @apiSuccess {Number} Number of unread emails in the inbox
* @apiError (client) ForbiddenError Access denied
* @apiPermission admin
* @apiVersion 1.5.0
*
* @return Response
*/
@POST
@Path("test_inbox")
public Response testInbox() {
if (!authenticate()) {
throw new ForbiddenClientException();
}
checkBaseFunction(BaseFunction.ADMIN);
return Response.ok().entity(Json.createObjectBuilder()
.add("count", AppContext.getInstance().getInboxService().testInbox())
.build()).build();
}
/**
* Retrieve the application logs.
*

View File

@@ -1,3 +1,3 @@
api.current_version=${project.version}
api.min_version=1.0
db.version=16
db.version=17

View File

@@ -1,3 +1,3 @@
api.current_version=${project.version}
api.min_version=1.0
db.version=16
db.version=17

View File

@@ -1,5 +1,9 @@
package com.sismics.docs.rest;
import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetup;
import com.sismics.docs.core.model.context.AppContext;
import com.sismics.util.filter.TokenBasedSecurityFilter;
import org.junit.Assert;
import org.junit.Test;
@@ -218,4 +222,92 @@ public class TestAppResource extends BaseJerseyTest {
Assert.assertTrue(json.isNull("password"));
Assert.assertEquals("contact@sismics.com", json.getString("from"));
}
/**
* Test inbox scanning.
*/
@Test
public void testInbox() {
// Login admin
String adminToken = clientUtil.login("admin", "admin", false);
// Create a tag
JsonObject json = target().path("/tag").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.put(Entity.form(new Form()
.param("name", "Inbox")
.param("color", "#ff0000")), JsonObject.class);
String tagInboxId = json.getString("id");
// Get inbox configuration
json = target().path("/app/config_inbox").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get(JsonObject.class);
Assert.assertFalse(json.getBoolean("enabled"));
Assert.assertTrue(json.isNull("hostname"));
Assert.assertTrue(json.isNull("port"));
Assert.assertTrue(json.isNull("username"));
Assert.assertTrue(json.isNull("password"));
Assert.assertTrue(json.isNull("tag"));
// Change inbox configuration
target().path("/app/config_inbox").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.post(Entity.form(new Form()
.param("enabled", "true")
.param("hostname", "localhost")
.param("port", "143")
.param("username", "test@sismics.com")
.param("password", "12345678")
.param("tag", tagInboxId)
), JsonObject.class);
// Get inbox configuration
json = target().path("/app/config_inbox").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get(JsonObject.class);
Assert.assertTrue(json.getBoolean("enabled"));
Assert.assertEquals("localhost", json.getString("hostname"));
Assert.assertEquals(143, json.getInt("port"));
Assert.assertEquals("test@sismics.com", json.getString("username"));
Assert.assertEquals("12345678", json.getString("password"));
Assert.assertEquals(tagInboxId, json.getString("tag"));
GreenMail greenMail = new GreenMail(new ServerSetup[] { ServerSetup.SMTP, ServerSetup.IMAP });
greenMail.setUser("test@sismics.com", "12345678");
greenMail.start();
// Test the inbox
json = target().path("/app/test_inbox").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.post(Entity.form(new Form()), JsonObject.class);
Assert.assertEquals(0, json.getJsonNumber("count").intValue());
// Send an email
GreenMailUtil.sendTextEmail("test@sismics.com", "test@sismicsdocs.com", "Test email 1", "Test content 1", ServerSetup.SMTP);
// Trigger an inbox sync
AppContext.getInstance().getInboxService().syncInbox();
// Search for added documents
json = target().path("/document/list")
.queryParam("search", "tag:Inbox")
.request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get(JsonObject.class);
Assert.assertEquals(1, json.getJsonArray("documents").size());
// Trigger an inbox sync
AppContext.getInstance().getInboxService().syncInbox();
// Search for added documents
json = target().path("/document/list")
.queryParam("search", "tag:Inbox")
.request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get(JsonObject.class);
Assert.assertEquals(1, json.getJsonArray("documents").size());
greenMail.stop();
}
}