code.gitea.io/gitea@v1.21.7/services/mailer/incoming/incoming_test.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package incoming 5 6 import ( 7 "strings" 8 "testing" 9 10 "github.com/jhillyerd/enmime" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestIsAutomaticReply(t *testing.T) { 15 cases := []struct { 16 Headers map[string]string 17 Expected bool 18 }{ 19 { 20 Headers: map[string]string{}, 21 Expected: false, 22 }, 23 { 24 Headers: map[string]string{ 25 "Auto-Submitted": "no", 26 }, 27 Expected: false, 28 }, 29 { 30 Headers: map[string]string{ 31 "Auto-Submitted": "yes", 32 }, 33 Expected: true, 34 }, 35 { 36 Headers: map[string]string{ 37 "X-Autoreply": "no", 38 }, 39 Expected: false, 40 }, 41 { 42 Headers: map[string]string{ 43 "X-Autoreply": "yes", 44 }, 45 Expected: true, 46 }, 47 { 48 Headers: map[string]string{ 49 "X-Autorespond": "yes", 50 }, 51 Expected: true, 52 }, 53 } 54 55 for _, c := range cases { 56 b := enmime.Builder(). 57 From("Dummy", "dummy@gitea.io"). 58 To("Dummy", "dummy@gitea.io") 59 for k, v := range c.Headers { 60 b = b.Header(k, v) 61 } 62 root, err := b.Build() 63 assert.NoError(t, err) 64 env, err := enmime.EnvelopeFromPart(root) 65 assert.NoError(t, err) 66 67 assert.Equal(t, c.Expected, isAutomaticReply(env)) 68 } 69 } 70 71 func TestGetContentFromMailReader(t *testing.T) { 72 mailString := "Content-Type: multipart/mixed; boundary=message-boundary\r\n" + 73 "\r\n" + 74 "--message-boundary\r\n" + 75 "Content-Type: multipart/alternative; boundary=text-boundary\r\n" + 76 "\r\n" + 77 "--text-boundary\r\n" + 78 "Content-Type: text/plain\r\n" + 79 "Content-Disposition: inline\r\n" + 80 "\r\n" + 81 "mail content\r\n" + 82 "--text-boundary--\r\n" + 83 "--message-boundary\r\n" + 84 "Content-Type: text/plain\r\n" + 85 "Content-Disposition: attachment; filename=attachment.txt\r\n" + 86 "\r\n" + 87 "attachment content\r\n" + 88 "--message-boundary--\r\n" 89 90 env, err := enmime.ReadEnvelope(strings.NewReader(mailString)) 91 assert.NoError(t, err) 92 content := getContentFromMailReader(env) 93 assert.Equal(t, "mail content", content.Content) 94 assert.Len(t, content.Attachments, 1) 95 assert.Equal(t, "attachment.txt", content.Attachments[0].Name) 96 assert.Equal(t, []byte("attachment content"), content.Attachments[0].Content) 97 98 mailString = "Content-Type: multipart/mixed; boundary=message-boundary\r\n" + 99 "\r\n" + 100 "--message-boundary\r\n" + 101 "Content-Type: multipart/alternative; boundary=text-boundary\r\n" + 102 "\r\n" + 103 "--text-boundary\r\n" + 104 "Content-Type: text/html\r\n" + 105 "Content-Disposition: inline\r\n" + 106 "\r\n" + 107 "<p>mail content</p>\r\n" + 108 "--text-boundary--\r\n" + 109 "--message-boundary--\r\n" 110 111 env, err = enmime.ReadEnvelope(strings.NewReader(mailString)) 112 assert.NoError(t, err) 113 content = getContentFromMailReader(env) 114 assert.Equal(t, "mail content", content.Content) 115 assert.Empty(t, content.Attachments) 116 117 mailString = "Content-Type: multipart/mixed; boundary=message-boundary\r\n" + 118 "\r\n" + 119 "--message-boundary\r\n" + 120 "Content-Type: multipart/alternative; boundary=text-boundary\r\n" + 121 "\r\n" + 122 "--text-boundary\r\n" + 123 "Content-Type: text/plain\r\n" + 124 "Content-Disposition: inline\r\n" + 125 "\r\n" + 126 "mail content without signature\r\n" + 127 "--\r\n" + 128 "signature\r\n" + 129 "--text-boundary--\r\n" + 130 "--message-boundary--\r\n" 131 132 env, err = enmime.ReadEnvelope(strings.NewReader(mailString)) 133 assert.NoError(t, err) 134 content = getContentFromMailReader(env) 135 assert.NoError(t, err) 136 assert.Equal(t, "mail content without signature", content.Content) 137 assert.Empty(t, content.Attachments) 138 }