private void configure() { when(configuration.getSmtpHost()).thenReturn("localhost"); when(configuration.getSmtpPort()).thenReturn(smtpServer.getServer().getPort()); when(configuration.getFrom()).thenReturn("server@nowhere"); when(configuration.getFromName()).thenReturn("SonarQube from NoWhere"); when(configuration.getPrefix()).thenReturn("[SONARQUBE]"); when(configuration.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org"); }
@Test public void shouldSendTestEmailWithSTARTTLS() { smtpServer.getServer().setEnableTLS(true); smtpServer.getServer().setRequireTLS(true); configure(); when(configuration.getSecureConnection()).thenReturn("STARTTLS"); try { underTest.sendTestEmail("user@nowhere", "Test Message from SonarQube", "This is a test message from SonarQube."); fail("An SSL exception was expected a a proof that STARTTLS is enabled"); } catch (EmailException e) { // We don't have a SSL certificate so we are expecting a SSL error assertThat(e.getCause().getMessage()).isEqualTo("Could not convert socket to TLS"); } }
/** * Check if the server is running or not. * * @return {@link Boolean} * * @see org.unitils.mail.SmtpServer#isRunning() */ @Override public Boolean isRunning() { return wiser.getServer().isRunning(); }
@Override protected void before() throws Throwable { if (!mailServer.getServer().isRunning()) { LOGGER.info("Starting mail server {} ", mailServer.hashCode()); mailServer.start(); } }
@Override protected void after() { if (mailServer.getServer().isRunning()) { LOGGER.info("Stopping mail server {} ", mailServer.hashCode()); mailServer.stop(); } }
private void configureServer(int port, String host) { mailServer.setPort(port); try { mailServer.getServer().setBindAddress(InetAddress.getByName(host)); } catch (UnknownHostException e) { //if not a valid binding address then fall back to host name as listen address LOGGER.info("Invalid binding address, using it as hostname instead", e); mailServer.setHostname(host); } } }
/** * Checks if an email server is running at the address stored in the * <code>fakeMailServer</code>. * * @param fakeMailServer * The server from which the address is picked up. * @return <code>true</code> if the server claims to be running * @since 1.1 */ protected boolean isMailServerStopped(final Wiser fakeMailServer) { return !fakeMailServer.getServer().isRunning(); }
/** * @param port : the port where the server should be started on. */ public SmtpServerImpl(int port) { this.port = port; wiser = new Wiser(); wiser.setPort(port); wiser.start(); wiser.getServer().isRunning(); }
@After public void tearDown() { // clear any authentication handler factory WISER.getServer().setAuthenticationHandlerFactory(null); }
private void setupServerAuth() { WISER.getServer().setAuthenticationHandlerFactory(new EasyAuthenticationHandlerFactory(new UsernamePasswordValidator() { public void login(String username, String password) throws LoginFailedException { if (!username.equals(password)) { throw new LoginFailedException("username=" + username + ", password=" + password); } } })); } }
@Test public void sendHtmlWithBadAuthentication() throws Exception { // Add authentication to Wiser SMTP server wiser.getServer().setAuthenticationHandlerFactory(new TestAuthHandlerFactory()); // Input String testMethodName = Thread.currentThread().getStackTrace()[1].getMethodName(); String toAddress = "mags@bennetstore.com"; String fromAddress = "rgivens@kty.us.gov"; checkBadAuthentication(toAddress, fromAddress, testMethodName, authUsername, "bad password"); checkBadAuthentication(toAddress, fromAddress, testMethodName, "badUserName", authPassword); }
handlerFactory.setAcceptAllFrom(true); wiser.getServer().setMessageHandlerFactory(handlerFactory); wiser.start();
@Test public void sendHtmlWithAuthentication() throws Exception { // Add authentication to Wiser SMTP server wiser.getServer().setAuthenticationHandlerFactory(new TestAuthHandlerFactory()); // Input String testMethodName = Thread.currentThread().getStackTrace()[1].getMethodName(); String toAddress = "rgivens@kty.us.gov"; String fromAddress = "whawkins@kty.us.gov"; // Setup email WorkItemImpl workItem = createEmailWorkItem(toAddress, fromAddress, testMethodName); Connection connection = new Connection(emailHost, emailPort, authUsername, authPassword); sendAndCheckThatMessagesAreSent(workItem, connection); }
protected void startSMTP(String factory) { wiser = new Wiser(); wiser.setPort(1587); wiser.getServer().setAuthenticationHandlerFactory(new AuthenticationHandlerFactory() { /* * AUTH PLAIN handler which returns success on any string */ @Override public List<String> getAuthenticationMechanisms() { return Arrays.asList("PLAIN"); } @Override public AuthenticationHandler create() { return new AuthenticationHandler() { @Override public String auth(final String clientInput) throws RejectException { log.info(clientInput); return null; } @Override public Object getIdentity() { return "username"; } }; } }); Security.setProperty("ssl.SocketFactory.provider", factory); wiser.getServer().setEnableTLS(true); wiser.start(); }
@Test public void useEmailWorkItemHandlerWithAuthentication() throws Exception { // Add authentication to Wiser SMTP server wiser.getServer().setAuthenticationHandlerFactory(new TestAuthHandlerFactory()); // Input String testMethodName = Thread.currentThread().getStackTrace()[1].getMethodName(); String toAddress = "rgivens@yahoo.com"; String fromAddress = "rgivens@kty.us.gov"; EmailWorkItemHandler handler = new EmailWorkItemHandler(); handler.setConnection( emailHost, emailPort, authUsername, authPassword ); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter( "To", toAddress ); workItem.setParameter( "From", fromAddress ); workItem.setParameter( "Reply-To", fromAddress ); workItem.setParameter( "Subject", "Test mail for " + testMethodName ); workItem.setParameter( "Body", "Don't forget to check on Boyd later today." ); WorkItemManager manager = new DefaultWorkItemManager(null); handler.executeWorkItem( workItem, manager ); List<WiserMessage> messages = wiser.getMessages(); assertEquals(1, messages.size()); for (WiserMessage wiserMessage : messages ) { MimeMessage message = wiserMessage.getMimeMessage(); assertEquals(workItem.getParameter("Subject"), message.getSubject()); assertTrue(Arrays.equals(InternetAddress.parse(toAddress), message.getRecipients(RecipientType.TO))); } }
@Test public void sendHtmlWithAuthenticationAndAttachments() throws Exception { wiser.getServer().setAuthenticationHandlerFactory(new TestAuthHandlerFactory());