eintopf.info@v0.13.16/service/email/email_test.go (about)

     1  // Copyright (C) 2024 The Eintopf authors
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  
    16  package email_test
    17  
    18  import (
    19  	"os"
    20  	"testing"
    21  
    22  	"eintopf.info/service/email"
    23  	"eintopf.info/test"
    24  )
    25  
    26  func TestEmailSend(t *testing.T) {
    27  	if ci, _ := os.LookupEnv("CI"); ci != "" {
    28  		t.Skip("does not work on ci, because it needs a docker client")
    29  	}
    30  
    31  	pool := test.InitDockerPool(t)
    32  	smtpHost, client := test.RunMailHog(t, pool)
    33  
    34  	emailService := email.NewService(email.Config{
    35  		SMTPHost:     smtpHost,
    36  		SMTPPassword: "",
    37  		Mail:         "eintopf@example.com",
    38  		DisableTLS:   true,
    39  	})
    40  
    41  	err := emailService.Send("foo@example.com", "test message", "a test message")
    42  	if err != nil {
    43  		t.Error(err)
    44  	}
    45  
    46  	client.WantLastEmailToBe(t, "eintopf@example.com", "foo@example.com", "test message", "a test message")
    47  }
    48  
    49  func TestEmailAllowList(t *testing.T) {
    50  	if ci, _ := os.LookupEnv("CI"); ci != "" {
    51  		t.Skip("does not work on ci, because it needs a docker client")
    52  	}
    53  
    54  	pool := test.InitDockerPool(t)
    55  	smtpHost, client := test.RunMailHog(t, pool)
    56  
    57  	emailService := email.NewService(email.Config{
    58  		SMTPHost:     smtpHost,
    59  		SMTPPassword: "",
    60  		Mail:         "eintopf@example.com",
    61  		DisableTLS:   true,
    62  		AllowList:    []string{"foo@example.com"},
    63  	})
    64  
    65  	err := emailService.Send("bar@example.com", "test message", "a test message")
    66  	if err == nil || err.Error() != email.ErrDisallowedEmail.Error() {
    67  		t.Error("email should be disallowed")
    68  	}
    69  	err = emailService.Send("foo@example.com", "test message", "a test message")
    70  	if err != nil {
    71  		t.Error(err)
    72  	}
    73  	client.WantLastEmailToBe(t, "eintopf@example.com", "foo@example.com", "test message", "a test message")
    74  }