gitea源码

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