Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

util_render_test.go 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package templates
  4. import (
  5. "context"
  6. "html/template"
  7. "os"
  8. "strings"
  9. "testing"
  10. "code.gitea.io/gitea/models/issues"
  11. "code.gitea.io/gitea/models/unittest"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/markup"
  15. "code.gitea.io/gitea/modules/translation"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func testInput() string {
  19. s := ` space @mention-user<SPACE><SPACE>
  20. /just/a/path.bin
  21. https://example.com/file.bin
  22. [local link](file.bin)
  23. [remote link](https://example.com)
  24. [[local link|file.bin]]
  25. [[remote link|https://example.com]]
  26. ![local image](image.jpg)
  27. ![remote image](https://example.com/image.jpg)
  28. [[local image|image.jpg]]
  29. [[remote link|https://example.com/image.jpg]]
  30. https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
  31. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
  32. https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
  33. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
  34. :+1:
  35. mail@domain.com
  36. @mention-user test
  37. #123
  38. space<SPACE><SPACE>
  39. `
  40. return strings.ReplaceAll(s, "<SPACE>", " ")
  41. }
  42. var testMetas = map[string]string{
  43. "user": "user13",
  44. "repo": "repo11",
  45. "repoPath": "../../tests/gitea-repositories-meta/user13/repo11.git/",
  46. "mode": "comment",
  47. }
  48. func TestMain(m *testing.M) {
  49. unittest.InitSettings()
  50. if err := git.InitSimple(context.Background()); err != nil {
  51. log.Fatal("git init failed, err: %v", err)
  52. }
  53. markup.Init(&markup.ProcessorHelper{
  54. IsUsernameMentionable: func(ctx context.Context, username string) bool {
  55. return username == "mention-user"
  56. },
  57. })
  58. os.Exit(m.Run())
  59. }
  60. func TestRenderCommitBody(t *testing.T) {
  61. type args struct {
  62. ctx context.Context
  63. msg string
  64. metas map[string]string
  65. }
  66. tests := []struct {
  67. name string
  68. args args
  69. want template.HTML
  70. }{
  71. {
  72. name: "multiple lines",
  73. args: args{
  74. ctx: context.Background(),
  75. msg: "first line\nsecond line",
  76. },
  77. want: "second line",
  78. },
  79. {
  80. name: "multiple lines with leading newlines",
  81. args: args{
  82. ctx: context.Background(),
  83. msg: "\n\n\n\nfirst line\nsecond line",
  84. },
  85. want: "second line",
  86. },
  87. {
  88. name: "multiple lines with trailing newlines",
  89. args: args{
  90. ctx: context.Background(),
  91. msg: "first line\nsecond line\n\n\n",
  92. },
  93. want: "second line",
  94. },
  95. }
  96. for _, tt := range tests {
  97. t.Run(tt.name, func(t *testing.T) {
  98. assert.Equalf(t, tt.want, RenderCommitBody(tt.args.ctx, tt.args.msg, tt.args.metas), "RenderCommitBody(%v, %v, %v)", tt.args.ctx, tt.args.msg, tt.args.metas)
  99. })
  100. }
  101. expected := `/just/a/path.bin
  102. <a href="https://example.com/file.bin" class="link">https://example.com/file.bin</a>
  103. [local link](file.bin)
  104. [remote link](<a href="https://example.com" class="link">https://example.com</a>)
  105. [[local link|file.bin]]
  106. [[remote link|<a href="https://example.com" class="link">https://example.com</a>]]
  107. ![local image](image.jpg)
  108. ![remote image](<a href="https://example.com/image.jpg" class="link">https://example.com/image.jpg</a>)
  109. [[local image|image.jpg]]
  110. [[remote link|<a href="https://example.com/image.jpg" class="link">https://example.com/image.jpg</a>]]
  111. <a href="https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash" class="compare"><code class="nohighlight">88fc37a3c0...12fc37a3c0 (hash)</code></a>
  112. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
  113. <a href="https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb" class="commit"><code class="nohighlight">88fc37a3c0</code></a>
  114. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
  115. <span class="emoji" aria-label="thumbs up">👍</span>
  116. <a href="mailto:mail@domain.com" class="mailto">mail@domain.com</a>
  117. <a href="/mention-user" class="mention">@mention-user</a> test
  118. <a href="/user13/repo11/issues/123" class="ref-issue">#123</a>
  119. space`
  120. assert.EqualValues(t, expected, RenderCommitBody(context.Background(), testInput(), testMetas))
  121. }
  122. func TestRenderCommitMessage(t *testing.T) {
  123. expected := `space <a href="/mention-user" class="mention">@mention-user</a> `
  124. assert.EqualValues(t, expected, RenderCommitMessage(context.Background(), testInput(), testMetas))
  125. }
  126. func TestRenderCommitMessageLinkSubject(t *testing.T) {
  127. expected := `<a href="https://example.com/link" class="default-link muted">space </a><a href="/mention-user" class="mention">@mention-user</a>`
  128. assert.EqualValues(t, expected, RenderCommitMessageLinkSubject(context.Background(), testInput(), "https://example.com/link", testMetas))
  129. }
  130. func TestRenderIssueTitle(t *testing.T) {
  131. expected := ` space @mention-user<SPACE><SPACE>
  132. /just/a/path.bin
  133. https://example.com/file.bin
  134. [local link](file.bin)
  135. [remote link](https://example.com)
  136. [[local link|file.bin]]
  137. [[remote link|https://example.com]]
  138. ![local image](image.jpg)
  139. ![remote image](https://example.com/image.jpg)
  140. [[local image|image.jpg]]
  141. [[remote link|https://example.com/image.jpg]]
  142. https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
  143. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
  144. https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
  145. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
  146. <span class="emoji" aria-label="thumbs up">👍</span>
  147. mail@domain.com
  148. @mention-user test
  149. <a href="/user13/repo11/issues/123" class="ref-issue">#123</a>
  150. space<SPACE><SPACE>
  151. `
  152. expected = strings.ReplaceAll(expected, "<SPACE>", " ")
  153. assert.EqualValues(t, expected, RenderIssueTitle(context.Background(), testInput(), testMetas))
  154. }
  155. func TestRenderMarkdownToHtml(t *testing.T) {
  156. expected := `<p>space <a href="/mention-user" rel="nofollow">@mention-user</a><br/>
  157. /just/a/path.bin
  158. <a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a>
  159. <a href="/file.bin" rel="nofollow">local link</a>
  160. <a href="https://example.com" rel="nofollow">remote link</a>
  161. <a href="/src/file.bin" rel="nofollow">local link</a>
  162. <a href="https://example.com" rel="nofollow">remote link</a>
  163. <a href="/image.jpg" target="_blank" rel="nofollow noopener"><img src="/image.jpg" alt="local image"/></a>
  164. <a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image"/></a>
  165. <a href="/image.jpg" rel="nofollow"><img src="/image.jpg" title="local image" alt="local image"/></a>
  166. <a href="https://example.com/image.jpg" rel="nofollow"><img src="https://example.com/image.jpg" title="remote link" alt="remote link"/></a>
  167. <a href="https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash" rel="nofollow"><code>88fc37a3c0...12fc37a3c0 (hash)</code></a>
  168. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
  169. <a href="https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb" rel="nofollow"><code>88fc37a3c0</code></a>
  170. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
  171. <span class="emoji" aria-label="thumbs up">👍</span>
  172. <a href="mailto:mail@domain.com" rel="nofollow">mail@domain.com</a>
  173. <a href="/mention-user" rel="nofollow">@mention-user</a> test
  174. #123
  175. space</p>
  176. `
  177. assert.EqualValues(t, expected, RenderMarkdownToHtml(context.Background(), testInput()))
  178. }
  179. func TestRenderLabels(t *testing.T) {
  180. ctx := context.Background()
  181. locale := &translation.MockLocale{}
  182. label := &issues.Label{ID: 123, Name: "label-name", Color: "label-color"}
  183. issue := &issues.Issue{}
  184. expected := `/owner/repo/issues?labels=123`
  185. assert.Contains(t, RenderLabels(ctx, locale, []*issues.Label{label}, "/owner/repo", issue), expected)
  186. label = &issues.Label{ID: 123, Name: "label-name", Color: "label-color"}
  187. issue = &issues.Issue{IsPull: true}
  188. expected = `/owner/repo/pulls?labels=123`
  189. assert.Contains(t, RenderLabels(ctx, locale, []*issues.Label{label}, "/owner/repo", issue), expected)
  190. }