GithubHelp home page GithubHelp logo

gpc / greenmail Goto Github PK

View Code? Open in Web Editor NEW
13.0 6.0 27.0 273 KB

Adds an in memory SMTP server to grails apps for testing email sending

Home Page: http://grails.org/plugin/greenmail

License: Apache License 2.0

Groovy 95.80% Shell 4.20%

greenmail's Introduction

Greenmail Plugin for Grails 5

This is a fork of grails greenmail plugin for grails 5.

INSTALL

Add a dependency for the plugin in build.gradle:

dependencies {
    testImplementation 'io.github.gpc:greenmail:5.0.0'
    
}

If you need to type your MimeMessages you also need to depend on:

    testImplementation 'jakarta.mail:jakarta.mail-api:1.6.7'

USAGE

Provides a wrapper around GreenMail and provides a view that displays sent messages - useful for testing application in the development or test environments.

The plugin assumes that you have some sort of Java mail provider installed (for instance the Grails mail plugin). You need to define an SMTP port for the mock Greenmail SMTP server to start with. Using the Grails Mail plugin, this is as simple as defining the grails.mail.port property in application.yml, like this (see the first line in the development and test blocks):

You can completely disable the plugin by using the config setting grails.plugin.greenmail.disabled = true.

If you need to change the default listening port (3025) you can use the grails.plugin.greenmail.ports.smtp configuration variable.

Example configuration:

--- # Mail and GreenMail configurations
environments:
    development:
        grails:
            mail:
                port: 3025 # Use default GreenMail port
    test:
        grails:
            plugin:
                greenmail:
                    ports:
                        smtp: 2525 # Specify GreenMail port
            mail:
                port: "${grails.plugin.greenmail.ports.smtp}"
    production:
        grails:
            plugin:
                greenmail:
                    disabled: true # Will not run the GreenMail plugin
            mail: # For your production SMTP server. See mail plugin for configuration options
                server: smtp.example.com
                port: 25

Usage in Integration Tests

The plugin can be used to capture email messages during integration tests. For example:

import com.icegreen.greenmail.util.GreenMailUtil
import grails.plugin.greenmail.GreenMail
import grails.plugins.mail.MailService
import grails.testing.mixin.integration.Integration
import spock.lang.Specification

import javax.mail.internet.MimeMessage

@Integration
class GreenmailExampleSpec extends Specification {

    MailService mailService
    GreenMail greenMail

    void cleanup() {
        greenMail.deleteAllMessages()
    }

    void "send a test mail"() {
        given:
        Map mail = [message: 'hello world', from: '[email protected]', to: '[email protected]', subject: 'subject']

        when:
        mailService.sendMail {
            to mail.to
            from mail.from
            subject mail.subject
            body mail.message
        }

        then:
        greenMail.receivedMessages.length == 1

        with(greenMail.receivedMessages[0]) { MimeMessage message ->
            GreenMailUtil.getBody(message) == mail.message
            GreenMailUtil.getAddressList(message.from) == mail.from
            message.subject == mail.subject
        }
    }

}

Test example

The above code snippets can be found here: https://github.com/sbglasius/greenmail-example

Additional methods

grails.plugin.greenmail.GreenMail extends from com.icegreen.greenmail.util.GreenMail and adds a few extra methods. See GreenMail.groovy for reference.

Usage in running application

The plugin provides a controller and view to show messages that are sent from the application. Simply browse to http://localhost:8080/greenmail, and it will show a list of messages sent. You can click on the show link to view the raw message.

Roadmap

This is a fully functional plugin, though there are some features that I think would be worth adding. Contributions and patches are welcome!

  • Messages sent by the Grails Mail plugin have duplicate TO: fields in the raw message and in the address list, for instance if the recipient is [email protected], then that email address is listed twice when you retrieve the address list for RecipientType.TO (e.g. GreenMailUtil.getAddressList(message.getRecipients(javax.mail.Message.RecipientType.TO))
  • Ability to view HTML email messages as they appear in a mail client rather than as RAW message.

greenmail's People

Contributors

alvarosanchez avatar antonevane avatar bkoehm avatar bluesliverx avatar graemerocher avatar kwongpan avatar ldaley avatar mjhugo avatar ragufronz avatar sbglasius avatar yohannrub avatar zacharyklein avatar zyro23 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

greenmail's Issues

Choose free port

It would be nice to search for available port. It is especially useful for running concurrent tests

Plugin not available on Bintray

The plugin fails to install (I followed the installation instructions). Seems like the plugin is not published on Bintray?

Not working in integration and functional tests together

I have the Geb plugin for functional test and the remote-control plugin to execute code in the functional test phase.

I have a mail integration test and also use greenmail for functional test (check the account signin email)

I can run it whitout errors independently test-app functional: and test-app integration: but when running with test-app the functional test greenmail receives no email.

Greenmail should be disabled by default

Now there is option greenmail.disable and if user forgot to set it to true greenmail will be started.
It looks not good for me because of security and unexpected behavior.
For example dumpster plugin uses option dumbster.enable
Maybe it will be better to deprecate greenmail.disable and introduce greenmail.enable instead?

greenmail bean is null

Greenmail been is null and can't be autowired.
I've fixed this problem and will send pull request later. But I'm not sure why this error happens, so I want to describe it.
It looks like something changed in Spring beans DSL.

This code not work in Grails 2.2.2 and 2.2.3

    def doWithSpring = {
            ...
            greenMail(GreenMail, [smtp] as ServerSetup[]) { 
                it.initMethod = 'start'
                it.destroyMethod = 'stop'
            }
        }
    }

As I know Groovy lang variable it is referenced to first argument of closure. Maybe now there is more args, or something else, but now it not working.
If you define closure arguments directly all works as expected:

    def doWithSpring = {
            ...
            greenMail(GreenMail, [smtp] as ServerSetup[]) { bean ->
                bean.initMethod = 'start'
                bean.destroyMethod = 'stop'
            }
        }
    }

Same problem is in dumbster plugin.

greenMail#stop() fails; should call super

grails.plugin.greenmail.GreenMail is not in the same package as com.icegreen.greenmail.util.GreenMail.. thus stop() fails to find the super's services field at runtime as (being a Java class) it has package protection on the field.

It should call super akin to the start() method.

greenmail.disable property doesn't work propely

I set grails.plugin.greenmail.disabled=true .
I'm getting NPE exception when I try to access http://localhost:8080/greenmail/list

Caused by: java.lang.NullPointerException: Cannot invoke method getReceivedMessages() on null object
	at com.piragua.greenmail.GreenmailController$_list_closure1$_closure4.doCall(GreenmailController.groovy:30)
	at org.grails.plugins.web.api.MimeTypesApiSupport.getResponseForFormat(MimeTypesApiSupport.groovy:142)
	at org.grails.plugins.web.api.MimeTypesApiSupport.resolveAllFormat(MimeTypesApiSupport.groovy:103)
	at org.grails.plugins.web.api.MimeTypesApiSupport.withFormatInternal(MimeTypesApiSupport.groovy:52)
	at org.grails.plugins.web.api.MimeTypesApiSupport.withFormat(MimeTypesApiSupport.groovy:44)
	at grails.artefact.Controller$Trait$Helper.withFormat(Controller.groovy:92)
	at com.piragua.greenmail.GreenmailController.list(GreenmailController.groovy:28)
	... 38 common frames omitted

Enable all Greenmail services, not only SMTP

Greenmail supports mocking of SMTP not only it. It also supports SMTPS, POP3, POP3S, IMAP, IMAPS. See sources of com.icegreen.greenmail.util.ServerSetupTest
But nor of this protocols can't be enabled via config.

java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory

Adding this plugin to my grails app gave me this exception on application startup under tomcat 7, this is caused by old slf4j-api-1.3.1 lib. Pleas upgrade to latest greenmail 1.4.0 where slf4j was updated from 1.3.1 to 1.7.7.

SEVERE: Error configuring application listener of class org.codehaus.groovy.grails.web.util.Log4jConfigListener
java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
at org.slf4j.LoggerFactory.(LoggerFactory.java:57)
at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155)
at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:131)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685)
at org.codehaus.groovy.grails.web.util.Log4jConfigListener.(Log4jConfigListener.java:43)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:374)
at java.lang.Class.newInstance(Class.java:327)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:125)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4715)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:958)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1599)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)

Injecting Greenmail into a Spock specification doesn't work

Environment: Grails 2.0.3, grails-greenmail 1.3.2

@stepwise
class NewSubscriberSpec extends GebReportingSpec {
def greenMail
def "check if green mail was injected"() {
expect:
greenMail != null // always fails
}
}
BuildConfig.groovy
dependencies:
test "org.grails.plugins:greenmail:1.3.2" // tried compile as well
Config.groovy
test {
greenmail.disabled = false
grails.mail.port = com.icegreen.greenmail.util.ServerSetupTest.SMTP.port
grails.serverURL = "http://localhost:8080/"
}
I have tried to specify greenmai.port as a String, this results in runtime exception, so doWithSpring gets executed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.