mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-01 14:59:06 +00:00
fc037b4b82
closes #13585 fixes #9067 fixes #2386 ref #6226 ref #6219 fixes #745 This PR adds support to process incoming emails to perform actions. Currently I added handling of replies and unsubscribing from issues/pulls. In contrast to #13585 the IMAP IDLE command is used instead of polling which results (in my opinion 😉) in cleaner code. Procedure: - When sending an issue/pull reply email, a token is generated which is present in the Reply-To and References header. - IMAP IDLE waits until a new email arrives - The token tells which action should be performed A possible signature and/or reply gets stripped from the content. I added a new service to the drone pipeline to test the receiving of incoming mails. If we keep this in, we may test our outgoing emails too in future. Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
138 lines
3.4 KiB
Go
138 lines
3.4 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package incoming
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/jhillyerd/enmime"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestIsAutomaticReply(t *testing.T) {
|
|
cases := []struct {
|
|
Headers map[string]string
|
|
Expected bool
|
|
}{
|
|
{
|
|
Headers: map[string]string{},
|
|
Expected: false,
|
|
},
|
|
{
|
|
Headers: map[string]string{
|
|
"Auto-Submitted": "no",
|
|
},
|
|
Expected: false,
|
|
},
|
|
{
|
|
Headers: map[string]string{
|
|
"Auto-Submitted": "yes",
|
|
},
|
|
Expected: true,
|
|
},
|
|
{
|
|
Headers: map[string]string{
|
|
"X-Autoreply": "no",
|
|
},
|
|
Expected: false,
|
|
},
|
|
{
|
|
Headers: map[string]string{
|
|
"X-Autoreply": "yes",
|
|
},
|
|
Expected: true,
|
|
},
|
|
{
|
|
Headers: map[string]string{
|
|
"X-Autorespond": "yes",
|
|
},
|
|
Expected: true,
|
|
},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
b := enmime.Builder().
|
|
From("Dummy", "dummy@gitea.io").
|
|
To("Dummy", "dummy@gitea.io")
|
|
for k, v := range c.Headers {
|
|
b = b.Header(k, v)
|
|
}
|
|
root, err := b.Build()
|
|
assert.NoError(t, err)
|
|
env, err := enmime.EnvelopeFromPart(root)
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, c.Expected, isAutomaticReply(env))
|
|
}
|
|
}
|
|
|
|
func TestGetContentFromMailReader(t *testing.T) {
|
|
mailString := "Content-Type: multipart/mixed; boundary=message-boundary\r\n" +
|
|
"\r\n" +
|
|
"--message-boundary\r\n" +
|
|
"Content-Type: multipart/alternative; boundary=text-boundary\r\n" +
|
|
"\r\n" +
|
|
"--text-boundary\r\n" +
|
|
"Content-Type: text/plain\r\n" +
|
|
"Content-Disposition: inline\r\n" +
|
|
"\r\n" +
|
|
"mail content\r\n" +
|
|
"--text-boundary--\r\n" +
|
|
"--message-boundary\r\n" +
|
|
"Content-Type: text/plain\r\n" +
|
|
"Content-Disposition: attachment; filename=attachment.txt\r\n" +
|
|
"\r\n" +
|
|
"attachment content\r\n" +
|
|
"--message-boundary--\r\n"
|
|
|
|
env, err := enmime.ReadEnvelope(strings.NewReader(mailString))
|
|
assert.NoError(t, err)
|
|
content := getContentFromMailReader(env)
|
|
assert.Equal(t, "mail content", content.Content)
|
|
assert.Len(t, content.Attachments, 1)
|
|
assert.Equal(t, "attachment.txt", content.Attachments[0].Name)
|
|
assert.Equal(t, []byte("attachment content"), content.Attachments[0].Content)
|
|
|
|
mailString = "Content-Type: multipart/mixed; boundary=message-boundary\r\n" +
|
|
"\r\n" +
|
|
"--message-boundary\r\n" +
|
|
"Content-Type: multipart/alternative; boundary=text-boundary\r\n" +
|
|
"\r\n" +
|
|
"--text-boundary\r\n" +
|
|
"Content-Type: text/html\r\n" +
|
|
"Content-Disposition: inline\r\n" +
|
|
"\r\n" +
|
|
"<p>mail content</p>\r\n" +
|
|
"--text-boundary--\r\n" +
|
|
"--message-boundary--\r\n"
|
|
|
|
env, err = enmime.ReadEnvelope(strings.NewReader(mailString))
|
|
assert.NoError(t, err)
|
|
content = getContentFromMailReader(env)
|
|
assert.Equal(t, "mail content", content.Content)
|
|
assert.Empty(t, content.Attachments)
|
|
|
|
mailString = "Content-Type: multipart/mixed; boundary=message-boundary\r\n" +
|
|
"\r\n" +
|
|
"--message-boundary\r\n" +
|
|
"Content-Type: multipart/alternative; boundary=text-boundary\r\n" +
|
|
"\r\n" +
|
|
"--text-boundary\r\n" +
|
|
"Content-Type: text/plain\r\n" +
|
|
"Content-Disposition: inline\r\n" +
|
|
"\r\n" +
|
|
"mail content without signature\r\n" +
|
|
"--\r\n" +
|
|
"signature\r\n" +
|
|
"--text-boundary--\r\n" +
|
|
"--message-boundary--\r\n"
|
|
|
|
env, err = enmime.ReadEnvelope(strings.NewReader(mailString))
|
|
assert.NoError(t, err)
|
|
content = getContentFromMailReader(env)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "mail content without signature", content.Content)
|
|
assert.Empty(t, content.Attachments)
|
|
}
|