mirror of
https://github.com/brmlab/wekan-mailer.git
synced 2025-06-08 09:54:10 +02:00
Implement email configuration
This commit is contained in:
parent
ac4e666d53
commit
53cf554c7d
5 changed files with 204 additions and 6 deletions
|
@ -8,6 +8,12 @@ services:
|
|||
environment:
|
||||
- WEKAN_URL=http://localhost:3000
|
||||
- WEKAN_USER=someuser
|
||||
- WEKAN_PASSWORD=somepass
|
||||
- WEKAN_PASS=somepass
|
||||
- WEKAN_TARGET_BOARD=someId
|
||||
- WEKAN_TARGET_LIST=someListId
|
||||
- MAIL_PROTOCOL=imap
|
||||
- MAIL_URL=imap.server.somewhere
|
||||
- MAIL_PORT=993
|
||||
- MAIL_USER=mailuser
|
||||
- MAIL_PASS=mailpass
|
||||
- MAIL_FOLDER=inbox
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package cz.brmlab.wm;
|
||||
|
||||
import cz.brmlab.wm.utils.exceptions.BrmException;
|
||||
import cz.brmlab.wm.config.MailConfiguration;
|
||||
import cz.brmlab.wm.config.WekanConfiguration;
|
||||
import cz.brmlab.wm.utils.exceptions.BrmException;
|
||||
import cz.brmlab.wm.wekan.pojo.card.PostCardResponse;
|
||||
import cz.brmlab.wm.wekan.rest.CardPost;
|
||||
import cz.brmlab.wm.wekan.rest.LoginPost;
|
||||
|
@ -28,6 +29,7 @@ public class Application implements CommandLineRunner {
|
|||
public void run(String... args) {
|
||||
try {
|
||||
WekanConfiguration wekanConfiguration = new WekanConfiguration();
|
||||
MailConfiguration mailConfiguration = new MailConfiguration();
|
||||
|
||||
LoginPost loginPost = new LoginPost(wekanConfiguration);
|
||||
loginPost.login();
|
||||
|
@ -37,6 +39,7 @@ public class Application implements CommandLineRunner {
|
|||
|
||||
} catch (BrmException ex) {
|
||||
log.error("Error {} encountered, shutting down!", ex.getExitCode());
|
||||
log.error("Error message is: {}", ex.getMessage());
|
||||
System.exit(ex.getExitCode().getCode());
|
||||
}
|
||||
}
|
||||
|
|
72
src/main/java/cz/brmlab/wm/config/MailConfiguration.java
Normal file
72
src/main/java/cz/brmlab/wm/config/MailConfiguration.java
Normal file
|
@ -0,0 +1,72 @@
|
|||
package cz.brmlab.wm.config;
|
||||
|
||||
import cz.brmlab.wm.utils.exceptions.BrmException;
|
||||
import cz.brmlab.wm.utils.exceptions.ExitCode;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class MailConfiguration implements EnvConfig {
|
||||
|
||||
//ENV variables for mail
|
||||
private static final String MAIL_PROTOCOL = "MAIL_PROTOCOL";
|
||||
private static final String MAIL_URL = "MAIL_URL";
|
||||
private static final String MAIL_PORT = "MAIL_PORT";
|
||||
private static final String MAIL_USER = "MAIL_USER";
|
||||
private static final String MAIL_PASS = "MAIL_PASS";
|
||||
private static final String MAIL_FOLDER = "MAIL_FOLDER";
|
||||
|
||||
//List of mail ENV vars
|
||||
private static final List<String> properties = new ArrayList<>(Arrays.asList(MAIL_PROTOCOL, MAIL_URL, MAIL_PORT, MAIL_USER, MAIL_PASS, MAIL_FOLDER));
|
||||
|
||||
@Getter
|
||||
private String mailProtocol;
|
||||
|
||||
@Getter
|
||||
private String mailUrl;
|
||||
|
||||
@Getter
|
||||
private String mailUser;
|
||||
|
||||
@Getter
|
||||
private String mailPassword;
|
||||
|
||||
@Getter
|
||||
private String mailFolder;
|
||||
|
||||
@Getter
|
||||
private String mailPort;
|
||||
|
||||
/**
|
||||
* Configuration for mail. Taken from the container ENV variables.
|
||||
*
|
||||
* @throws BrmException if some of the properties is missing in ENV variables.
|
||||
*/
|
||||
public MailConfiguration() throws BrmException {
|
||||
log.trace("{}() - start.", this.getClass().getSimpleName());
|
||||
|
||||
checkProps(properties);
|
||||
|
||||
this.mailProtocol = System.getenv(MAIL_PROTOCOL);
|
||||
this.mailUrl = System.getenv(MAIL_URL);
|
||||
this.mailPort = System.getenv(MAIL_PORT);
|
||||
this.mailUser = System.getenv(MAIL_USER);
|
||||
this.mailPassword = System.getenv(MAIL_PASS);
|
||||
this.mailFolder = System.getenv(MAIL_FOLDER);
|
||||
|
||||
checkProtocol();
|
||||
|
||||
log.info("Mail config loaded successfully.");
|
||||
}
|
||||
|
||||
private void checkProtocol() throws BrmException {
|
||||
if (!this.mailProtocol.equalsIgnoreCase("imap")){
|
||||
throw new BrmException(ExitCode.UNSUPPORTED_PROTOCOL.getReason(), ExitCode.UNSUPPORTED_PROTOCOL);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -5,6 +5,7 @@ import lombok.Getter;
|
|||
public enum ExitCode {
|
||||
|
||||
CONFIGURATION_MISSING(10, "Missing configuration property: "),
|
||||
UNSUPPORTED_PROTOCOL(15, "Unsupported email protocol specified! IMAP only is currently supported."),
|
||||
POST_ERROR(20, "Failed POST request, RC: ");
|
||||
|
||||
@Getter
|
||||
|
|
116
src/test/java/cz/brmlab/wm/config/MailConfigurationTest.java
Normal file
116
src/test/java/cz/brmlab/wm/config/MailConfigurationTest.java
Normal file
|
@ -0,0 +1,116 @@
|
|||
package cz.brmlab.wm.config;
|
||||
|
||||
import cz.brmlab.wm.utils.exceptions.BrmException;
|
||||
import cz.brmlab.wm.utils.exceptions.ExitCode;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.contrib.java.lang.system.EnvironmentVariables;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class MailConfigurationTest {
|
||||
|
||||
@Rule
|
||||
public final EnvironmentVariables environmentVariables
|
||||
= new EnvironmentVariables();
|
||||
|
||||
private static final String MAIL_PROTOCOL = "MAIL_PROTOCOL";
|
||||
private static final String MAIL_PROTOCOL_VALUE = "imap";
|
||||
|
||||
private static final String MAIL_URL = "MAIL_URL";
|
||||
private static final String MAIL_URL_VALUE = "mail.test.url";
|
||||
|
||||
private static final String MAIL_PORT = "MAIL_PORT";
|
||||
private static final String MAIL_PORT_VALUE = "993";
|
||||
|
||||
private static final String MAIL_USER = "MAIL_USER";
|
||||
private static final String MAIL_USER_VALUE = "someuser";
|
||||
|
||||
private static final String MAIL_PASS = "MAIL_PASS";
|
||||
private static final String MAIL_PASS_VALUE = "somepass";
|
||||
|
||||
private static final String MAIL_FOLDER = "MAIL_FOLDER";
|
||||
private static final String MAIL_FOLDER_VALUE = "inbox";
|
||||
|
||||
|
||||
@Before
|
||||
public void setEnvironmentVariables() {
|
||||
environmentVariables.set(MAIL_PROTOCOL, MAIL_PROTOCOL_VALUE);
|
||||
environmentVariables.set(MAIL_URL, MAIL_URL_VALUE);
|
||||
environmentVariables.set(MAIL_PORT, MAIL_PORT_VALUE);
|
||||
environmentVariables.set(MAIL_USER, MAIL_USER_VALUE);
|
||||
environmentVariables.set(MAIL_PASS, MAIL_PASS_VALUE);
|
||||
environmentVariables.set(MAIL_FOLDER, MAIL_FOLDER_VALUE);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanEnvVars() {
|
||||
environmentVariables.clear(MAIL_PROTOCOL, MAIL_URL, MAIL_USER, MAIL_PASS, MAIL_FOLDER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configurationOk() {
|
||||
|
||||
MailConfiguration configuration = null;
|
||||
try {
|
||||
configuration = new MailConfiguration();
|
||||
} catch (BrmException e) {
|
||||
fail("OK configuration should not throw an error!");
|
||||
}
|
||||
assertEquals(MAIL_PROTOCOL_VALUE, configuration.getMailProtocol());
|
||||
assertEquals(MAIL_URL_VALUE, configuration.getMailUrl());
|
||||
assertEquals(MAIL_USER_VALUE, configuration.getMailUser());
|
||||
assertEquals(MAIL_PASS_VALUE, configuration.getMailPassword());
|
||||
assertEquals(MAIL_FOLDER_VALUE, configuration.getMailFolder());
|
||||
assertEquals(MAIL_PORT_VALUE, configuration.getMailPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allConfigurationMissing() {
|
||||
cleanEnvVars();
|
||||
MailConfiguration configuration = null;
|
||||
try {
|
||||
configuration = new MailConfiguration();
|
||||
fail("Missing whole configuration should throw an error!");
|
||||
} catch (BrmException ignored) {
|
||||
|
||||
}
|
||||
|
||||
assertNull(null, configuration);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingOneProp() {
|
||||
|
||||
environmentVariables.clear(MAIL_PASS);
|
||||
|
||||
MailConfiguration configuration = null;
|
||||
try {
|
||||
configuration = new MailConfiguration();
|
||||
fail("Missing one property in configuration should throw an error!");
|
||||
} catch (BrmException ex) {
|
||||
assertEquals(ExitCode.CONFIGURATION_MISSING.getReason() + MAIL_PASS, ex.getMessage());
|
||||
}
|
||||
|
||||
assertNull(null, configuration);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unsupportedProtocol() {
|
||||
environmentVariables.set(MAIL_PROTOCOL, "POP3");
|
||||
|
||||
MailConfiguration configuration = null;
|
||||
try {
|
||||
configuration = new MailConfiguration();
|
||||
fail("Unsupported protocol should throw an error!");
|
||||
} catch (BrmException ex) {
|
||||
assertEquals(ExitCode.UNSUPPORTED_PROTOCOL.getReason(), ex.getMessage());
|
||||
}
|
||||
|
||||
assertNull(null, configuration);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue