|
4 | 4 | package mailer
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "strings" |
7 | 8 | "testing"
|
8 | 9 | "time"
|
9 | 10 |
|
@@ -36,3 +37,78 @@ func TestGenerateMessageID(t *testing.T) {
|
36 | 37 | gm = m.ToMessage()
|
37 | 38 | assert. Equal( t, "<[email protected]>", gm. GetHeader( "Message-ID")[ 0])
|
38 | 39 | }
|
| 40 | + |
| 41 | +func TestToMessage(t *testing.T) { |
| 42 | + oldConf := *setting.MailService |
| 43 | + defer func() { |
| 44 | + setting.MailService = &oldConf |
| 45 | + }() |
| 46 | + setting. MailService. From = "[email protected]" |
| 47 | + |
| 48 | + m1 := Message{ |
| 49 | + Info: "info", |
| 50 | + |
| 51 | + FromDisplayName: "Test Gitea", |
| 52 | + |
| 53 | + Subject: "Issue X Closed", |
| 54 | + Body: "Some Issue got closed by Y-Man", |
| 55 | + } |
| 56 | + |
| 57 | + buf := &strings.Builder{} |
| 58 | + _, err := m1.ToMessage().WriteTo(buf) |
| 59 | + assert.NoError(t, err) |
| 60 | + header, _ := extractMailHeaderAndContent(t, buf.String()) |
| 61 | + assert.EqualValues(t, map[string]string{ |
| 62 | + "Content-Type": "multipart/alternative;", |
| 63 | + "Date": "Mon, 01 Jan 0001 00:00:00 +0000", |
| 64 | + "From": "\"Test Gitea\" <[email protected]>", |
| 65 | + "Message-ID": "<autogen--6795364578871-69c000786adc60dc@localhost>", |
| 66 | + "Mime-Version": "1.0", |
| 67 | + "Subject": "Issue X Closed", |
| 68 | + |
| 69 | + "X-Auto-Response-Suppress": "All", |
| 70 | + }, header) |
| 71 | + |
| 72 | + setting.MailService.OverrideHeader = map[string][]string{ |
| 73 | + "Message-ID": {""}, // delete message id |
| 74 | + "Auto-Submitted": {"auto-generated"}, // suppress auto replay |
| 75 | + } |
| 76 | + |
| 77 | + buf = &strings.Builder{} |
| 78 | + _, err = m1.ToMessage().WriteTo(buf) |
| 79 | + assert.NoError(t, err) |
| 80 | + header, _ = extractMailHeaderAndContent(t, buf.String()) |
| 81 | + assert.EqualValues(t, map[string]string{ |
| 82 | + "Content-Type": "multipart/alternative;", |
| 83 | + "Date": "Mon, 01 Jan 0001 00:00:00 +0000", |
| 84 | + "From": "\"Test Gitea\" <[email protected]>", |
| 85 | + "Message-ID": "", |
| 86 | + "Mime-Version": "1.0", |
| 87 | + "Subject": "Issue X Closed", |
| 88 | + |
| 89 | + "X-Auto-Response-Suppress": "All", |
| 90 | + "Auto-Submitted": "auto-generated", |
| 91 | + }, header) |
| 92 | +} |
| 93 | + |
| 94 | +func extractMailHeaderAndContent(t *testing.T, mail string) (map[string]string, string) { |
| 95 | + header := make(map[string]string) |
| 96 | + |
| 97 | + parts := strings.SplitN(mail, "boundary=", 2) |
| 98 | + if !assert.Len(t, parts, 2) { |
| 99 | + return nil, "" |
| 100 | + } |
| 101 | + content := strings.TrimSpace("boundary=" + parts[1]) |
| 102 | + |
| 103 | + hParts := strings.Split(parts[0], "\n") |
| 104 | + |
| 105 | + for _, hPart := range hParts { |
| 106 | + parts := strings.SplitN(hPart, ":", 2) |
| 107 | + hk := strings.TrimSpace(parts[0]) |
| 108 | + if hk != "" { |
| 109 | + header[hk] = strings.TrimSpace(parts[1]) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return header, content |
| 114 | +} |
0 commit comments