-
-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Philipp Heckel
committed
Dec 28, 2021
1 parent
7cfe909
commit 113053a
Showing
3 changed files
with
213 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/emersion/go-smtp" | ||
"github.com/stretchr/testify/require" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestSmtpBackend_Multipart(t *testing.T) { | ||
email := `MIME-Version: 1.0 | ||
Date: Tue, 28 Dec 2021 00:30:10 +0100 | ||
Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com> | ||
Subject: and one more | ||
From: Phil <[email protected]> | ||
To: [email protected] | ||
Content-Type: multipart/alternative; boundary="000000000000f3320b05d42915c9" | ||
--000000000000f3320b05d42915c9 | ||
Content-Type: text/plain; charset="UTF-8" | ||
what's up | ||
--000000000000f3320b05d42915c9 | ||
Content-Type: text/html; charset="UTF-8" | ||
<div dir="ltr">what's up<br clear="all"><div><br></div></div> | ||
--000000000000f3320b05d42915c9--` | ||
_, backend := newTestBackend(t, func(m *message) error { | ||
require.Equal(t, "mytopic", m.Topic) | ||
require.Equal(t, "and one more", m.Title) | ||
require.Equal(t, "what's up\n", m.Message) | ||
return nil | ||
}) | ||
session, _ := backend.AnonymousLogin(nil) | ||
require.Nil(t, session.Mail("[email protected]", smtp.MailOptions{})) | ||
require.Nil(t, session.Rcpt("[email protected]")) | ||
require.Nil(t, session.Data(strings.NewReader(email))) | ||
} | ||
|
||
func TestSmtpBackend_Plaintext(t *testing.T) { | ||
email := `Date: Tue, 28 Dec 2021 00:30:10 +0100 | ||
Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com> | ||
Subject: and one more | ||
From: Phil <[email protected]> | ||
To: [email protected] | ||
Content-Type: text/plain; charset="UTF-8" | ||
what's up | ||
` | ||
conf, backend := newTestBackend(t, func(m *message) error { | ||
require.Equal(t, "mytopic", m.Topic) | ||
require.Equal(t, "and one more", m.Title) | ||
require.Equal(t, "what's up\n", m.Message) | ||
return nil | ||
}) | ||
conf.SMTPServerAddrPrefix = "" | ||
session, _ := backend.AnonymousLogin(nil) | ||
require.Nil(t, session.Mail("[email protected]", smtp.MailOptions{})) | ||
require.Nil(t, session.Rcpt("[email protected]")) | ||
require.Nil(t, session.Data(strings.NewReader(email))) | ||
} | ||
|
||
func TestSmtpBackend_Plaintext_EncodedSubject(t *testing.T) { | ||
email := `Date: Tue, 28 Dec 2021 00:30:10 +0100 | ||
Subject: =?UTF-8?B?VGhyZWUgc2FudGFzIPCfjoXwn46F8J+OhQ==?= | ||
From: Phil <[email protected]> | ||
To: [email protected] | ||
Content-Type: text/plain; charset="UTF-8" | ||
what's up | ||
` | ||
_, backend := newTestBackend(t, func(m *message) error { | ||
require.Equal(t, "Three santas 🎅🎅🎅", m.Title) | ||
return nil | ||
}) | ||
session, _ := backend.AnonymousLogin(nil) | ||
require.Nil(t, session.Mail("[email protected]", smtp.MailOptions{})) | ||
require.Nil(t, session.Rcpt("[email protected]")) | ||
require.Nil(t, session.Data(strings.NewReader(email))) | ||
} | ||
|
||
func TestSmtpBackend_Plaintext_TooLongTruncate(t *testing.T) { | ||
email := `Date: Tue, 28 Dec 2021 00:30:10 +0100 | ||
Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com> | ||
Subject: and one more | ||
From: Phil <[email protected]> | ||
To: [email protected] | ||
Content-Type: text/plain; charset="UTF-8" | ||
you know this is a string. | ||
it's a long string. | ||
it's supposed to be longer than the max message length | ||
which is 512 bytes, | ||
which some people say is too short | ||
but it kinda makes sense when you look at what it looks like one a phone | ||
heck this wasn't even half of it so far. | ||
so i'm gonna fill the rest of this with AAAAAAAAAAAAAAAAAAAAAAAAAAA | ||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | ||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa | ||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | ||
and with BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB | ||
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB | ||
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB | ||
that should do it | ||
` | ||
conf, backend := newTestBackend(t, func(m *message) error { | ||
expected := `you know this is a string. | ||
it's a long string. | ||
it's supposed to be longer than the max message length | ||
which is 512 bytes, | ||
which some people say is too short | ||
but it kinda makes sense when you look at what it looks like one a phone | ||
heck this wasn't even half of it so far. | ||
so i'm gonna fill the rest of this with AAAAAAAAAAAAAAAAAAAAAAAAAAA | ||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | ||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa | ||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | ||
and with ` | ||
require.Equal(t, expected, m.Message) | ||
return nil | ||
}) | ||
conf.SMTPServerAddrPrefix = "" | ||
session, _ := backend.AnonymousLogin(nil) | ||
require.Nil(t, session.Mail("[email protected]", smtp.MailOptions{})) | ||
require.Nil(t, session.Rcpt("[email protected]")) | ||
require.Nil(t, session.Data(strings.NewReader(email))) | ||
} | ||
|
||
func TestSmtpBackend_Unsupported(t *testing.T) { | ||
email := `Date: Tue, 28 Dec 2021 00:30:10 +0100 | ||
Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com> | ||
Subject: and one more | ||
From: Phil <[email protected]> | ||
To: [email protected] | ||
Content-Type: text/SOMETHINGELSE | ||
what's up | ||
` | ||
conf, backend := newTestBackend(t, func(m *message) error { | ||
return nil | ||
}) | ||
conf.SMTPServerAddrPrefix = "" | ||
session, _ := backend.Login(nil, "user", "pass") | ||
require.Nil(t, session.Mail("[email protected]", smtp.MailOptions{})) | ||
require.Nil(t, session.Rcpt("[email protected]")) | ||
require.Equal(t, errUnsupportedContentType, session.Data(strings.NewReader(email))) | ||
} | ||
|
||
func newTestBackend(t *testing.T, sub subscriber) (*Config, *smtpBackend) { | ||
conf := newTestConfig(t) | ||
conf.SMTPServerListen = ":25" | ||
conf.SMTPServerDomain = "ntfy.sh" | ||
conf.SMTPServerAddrPrefix = "ntfy-" | ||
backend := newMailBackend(conf, sub) | ||
return conf, backend | ||
} |