You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
15 KiB

  1. package markdown_test
  2. import (
  3. "bytes"
  4. "testing"
  5. . "github.com/go-gitea/gitea/modules/markdown"
  6. "github.com/go-gitea/gitea/modules/setting"
  7. "github.com/russross/blackfriday"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestMarkdown(t *testing.T) {
  11. Convey("Rendering an issue mention", t, func() {
  12. var (
  13. urlPrefix = "/prefix"
  14. metas map[string]string = nil
  15. )
  16. setting.AppSubUrlDepth = 0
  17. Convey("To the internal issue tracker", func() {
  18. Convey("It should not render anything when there are no mentions", func() {
  19. testCases := []string{
  20. "",
  21. "this is a test",
  22. "test 123 123 1234",
  23. "#",
  24. "# # #",
  25. "# 123",
  26. "#abcd",
  27. "##1234",
  28. "test#1234",
  29. "#1234test",
  30. " test #1234test",
  31. }
  32. for i := 0; i < len(testCases); i++ {
  33. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i])
  34. }
  35. })
  36. Convey("It should render freestanding mentions", func() {
  37. testCases := []string{
  38. "#1234 test", "<a href=\"/prefix/issues/1234\">#1234</a> test",
  39. "test #1234 issue", "test <a href=\"/prefix/issues/1234\">#1234</a> issue",
  40. "test issue #1234", "test issue <a href=\"/prefix/issues/1234\">#1234</a>",
  41. "#5 test", "<a href=\"/prefix/issues/5\">#5</a> test",
  42. "test #5 issue", "test <a href=\"/prefix/issues/5\">#5</a> issue",
  43. "test issue #5", "test issue <a href=\"/prefix/issues/5\">#5</a>",
  44. }
  45. for i := 0; i < len(testCases); i += 2 {
  46. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  47. }
  48. })
  49. Convey("It should not render issue mention without leading space", func() {
  50. input := []byte("test#54321 issue")
  51. expected := "test#54321 issue"
  52. So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected)
  53. })
  54. Convey("It should not render issue mention without trailing space", func() {
  55. input := []byte("test #54321issue")
  56. expected := "test #54321issue"
  57. So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected)
  58. })
  59. Convey("It should render issue mention in parentheses", func() {
  60. testCases := []string{
  61. "(#54321 issue)", "(<a href=\"/prefix/issues/54321\">#54321</a> issue)",
  62. "test (#54321) issue", "test (<a href=\"/prefix/issues/54321\">#54321</a>) issue",
  63. "test (#54321 extra) issue", "test (<a href=\"/prefix/issues/54321\">#54321</a> extra) issue",
  64. "test (#54321 issue)", "test (<a href=\"/prefix/issues/54321\">#54321</a> issue)",
  65. "test (#54321)", "test (<a href=\"/prefix/issues/54321\">#54321</a>)",
  66. }
  67. for i := 0; i < len(testCases); i += 2 {
  68. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  69. }
  70. })
  71. Convey("It should render multiple issue mentions in the same line", func() {
  72. testCases := []string{
  73. "#54321 #1243", "<a href=\"/prefix/issues/54321\">#54321</a> <a href=\"/prefix/issues/1243\">#1243</a>",
  74. "test #54321 #1243", "test <a href=\"/prefix/issues/54321\">#54321</a> <a href=\"/prefix/issues/1243\">#1243</a>",
  75. "(#54321 #1243)", "(<a href=\"/prefix/issues/54321\">#54321</a> <a href=\"/prefix/issues/1243\">#1243</a>)",
  76. "(#54321)(#1243)", "(<a href=\"/prefix/issues/54321\">#54321</a>)(<a href=\"/prefix/issues/1243\">#1243</a>)",
  77. "text #54321 test #1243 issue", "text <a href=\"/prefix/issues/54321\">#54321</a> test <a href=\"/prefix/issues/1243\">#1243</a> issue",
  78. "#1 (#4321) test", "<a href=\"/prefix/issues/1\">#1</a> (<a href=\"/prefix/issues/4321\">#4321</a>) test",
  79. }
  80. for i := 0; i < len(testCases); i += 2 {
  81. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  82. }
  83. })
  84. })
  85. Convey("To an external issue tracker with numeric style", func() {
  86. metas = make(map[string]string)
  87. metas["format"] = "https://someurl.com/{user}/{repo}/{index}"
  88. metas["user"] = "someuser"
  89. metas["repo"] = "somerepo"
  90. metas["style"] = ISSUE_NAME_STYLE_NUMERIC
  91. Convey("should not render anything when there are no mentions", func() {
  92. testCases := []string{
  93. "this is a test",
  94. "test 123 123 1234",
  95. "#",
  96. "# # #",
  97. "# 123",
  98. "#abcd",
  99. }
  100. for i := 0; i < len(testCases); i++ {
  101. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i])
  102. }
  103. })
  104. Convey("It should render freestanding issue mentions", func() {
  105. testCases := []string{
  106. "#1234 test", "<a href=\"https://someurl.com/someuser/somerepo/1234\">#1234</a> test",
  107. "test #1234 issue", "test <a href=\"https://someurl.com/someuser/somerepo/1234\">#1234</a> issue",
  108. "test issue #1234", "test issue <a href=\"https://someurl.com/someuser/somerepo/1234\">#1234</a>",
  109. "#5 test", "<a href=\"https://someurl.com/someuser/somerepo/5\">#5</a> test",
  110. "test #5 issue", "test <a href=\"https://someurl.com/someuser/somerepo/5\">#5</a> issue",
  111. "test issue #5", "test issue <a href=\"https://someurl.com/someuser/somerepo/5\">#5</a>",
  112. }
  113. for i := 0; i < len(testCases); i += 2 {
  114. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  115. }
  116. })
  117. Convey("It should not render issue mention without leading space", func() {
  118. input := []byte("test#54321 issue")
  119. expected := "test#54321 issue"
  120. So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected)
  121. })
  122. Convey("It should not render issue mention without trailing space", func() {
  123. input := []byte("test #54321issue")
  124. expected := "test #54321issue"
  125. So(string(RenderIssueIndexPattern(input, urlPrefix, metas)), ShouldEqual, expected)
  126. })
  127. Convey("It should render issue mention in parentheses", func() {
  128. testCases := []string{
  129. "(#54321 issue)", "(<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> issue)",
  130. "test (#54321) issue", "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a>) issue",
  131. "test (#54321 extra) issue", "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> extra) issue",
  132. "test (#54321 issue)", "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> issue)",
  133. "test (#54321)", "test (<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a>)",
  134. }
  135. for i := 0; i < len(testCases); i += 2 {
  136. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  137. }
  138. })
  139. Convey("It should render multiple issue mentions in the same line", func() {
  140. testCases := []string{
  141. "#54321 #1243", "<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>",
  142. "test #54321 #1243", "test <a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>",
  143. "(#54321 #1243)", "(<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>)",
  144. "(#54321)(#1243)", "(<a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a>)(<a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a>)",
  145. "text #54321 test #1243 issue", "text <a href=\"https://someurl.com/someuser/somerepo/54321\">#54321</a> test <a href=\"https://someurl.com/someuser/somerepo/1243\">#1243</a> issue",
  146. "#1 (#4321) test", "<a href=\"https://someurl.com/someuser/somerepo/1\">#1</a> (<a href=\"https://someurl.com/someuser/somerepo/4321\">#4321</a>) test",
  147. }
  148. for i := 0; i < len(testCases); i += 2 {
  149. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  150. }
  151. })
  152. })
  153. Convey("To an external issue tracker with alphanumeric style", func() {
  154. metas = make(map[string]string)
  155. metas["format"] = "https://someurl.com/{user}/{repo}/?b={index}"
  156. metas["user"] = "someuser"
  157. metas["repo"] = "somerepo"
  158. metas["style"] = ISSUE_NAME_STYLE_ALPHANUMERIC
  159. Convey("It should not render anything when there are no mentions", func() {
  160. testCases := []string{
  161. "",
  162. "this is a test",
  163. "test 123 123 1234",
  164. "#",
  165. "##1234",
  166. "# 123",
  167. "#abcd",
  168. "test #123",
  169. "abc-1234", // issue prefix must be capital
  170. "ABc-1234", // issue prefix must be _all_ capital
  171. "ABCDEFGHIJK-1234", // the limit is 10 characters in the prefix
  172. "ABC1234", // dash is required
  173. "test ABC- test", // number is required
  174. "test -1234 test", // prefix is required
  175. "testABC-123 test", // leading space is required
  176. "test ABC-123test", // trailing space is required
  177. "ABC-0123", // no leading zero
  178. }
  179. for i := 0; i < len(testCases); i += 2 {
  180. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i])
  181. }
  182. })
  183. Convey("It should render freestanding issue mention", func() {
  184. testCases := []string{
  185. "OTT-1234 test", "<a href=\"https://someurl.com/someuser/somerepo/?b=OTT-1234\">OTT-1234</a> test",
  186. "test T-12 issue", "test <a href=\"https://someurl.com/someuser/somerepo/?b=T-12\">T-12</a> issue",
  187. "test issue ABCDEFGHIJ-1234567890", "test issue <a href=\"https://someurl.com/someuser/somerepo/?b=ABCDEFGHIJ-1234567890\">ABCDEFGHIJ-1234567890</a>",
  188. "A-1 test", "<a href=\"https://someurl.com/someuser/somerepo/?b=A-1\">A-1</a> test",
  189. "test ZED-1 issue", "test <a href=\"https://someurl.com/someuser/somerepo/?b=ZED-1\">ZED-1</a> issue",
  190. "test issue DEED-7154", "test issue <a href=\"https://someurl.com/someuser/somerepo/?b=DEED-7154\">DEED-7154</a>",
  191. }
  192. for i := 0; i < len(testCases); i += 2 {
  193. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  194. }
  195. })
  196. Convey("It should render issue mention in parentheses", func() {
  197. testCases := []string{
  198. "(ABG-124 issue)", "(<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> issue)",
  199. "test (ABG-124) issue", "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>) issue",
  200. "test (ABG-124 extra) issue", "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> extra) issue",
  201. "test (ABG-124 issue)", "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> issue)",
  202. "test (ABG-124)", "test (<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>)",
  203. }
  204. for i := 0; i < len(testCases); i += 2 {
  205. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  206. }
  207. })
  208. Convey("It should render multiple issue mentions in the same line", func() {
  209. testCases := []string{
  210. "ABG-124 OTT-4321", "<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> <a href=\"https://someurl.com/someuser/somerepo/?b=OTT-4321\">OTT-4321</a>",
  211. "test ABG-124 OTT-4321", "test <a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> <a href=\"https://someurl.com/someuser/somerepo/?b=OTT-4321\">OTT-4321</a>",
  212. "(ABG-124 OTT-4321)", "(<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> <a href=\"https://someurl.com/someuser/somerepo/?b=OTT-4321\">OTT-4321</a>)",
  213. "(ABG-124)(OTT-4321)", "(<a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a>)(<a href=\"https://someurl.com/someuser/somerepo/?b=OTT-4321\">OTT-4321</a>)",
  214. "text ABG-124 test OTT-4321 issue", "text <a href=\"https://someurl.com/someuser/somerepo/?b=ABG-124\">ABG-124</a> test <a href=\"https://someurl.com/someuser/somerepo/?b=OTT-4321\">OTT-4321</a> issue",
  215. "A-1 (RRE-345) test", "<a href=\"https://someurl.com/someuser/somerepo/?b=A-1\">A-1</a> (<a href=\"https://someurl.com/someuser/somerepo/?b=RRE-345\">RRE-345</a>) test",
  216. }
  217. for i := 0; i < len(testCases); i += 2 {
  218. So(string(RenderIssueIndexPattern([]byte(testCases[i]), urlPrefix, metas)), ShouldEqual, testCases[i+1])
  219. }
  220. })
  221. })
  222. })
  223. Convey("Rendering an issue URL", t, func() {
  224. setting.AppUrl = "http://localhost:3000/"
  225. htmlFlags := 0
  226. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  227. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  228. renderer := &Renderer{
  229. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  230. }
  231. buffer := new(bytes.Buffer)
  232. Convey("To the internal issue tracker", func() {
  233. Convey("It should render valid issue URLs", func() {
  234. testCases := []string{
  235. "http://localhost:3000/user/repo/issues/3333", "<a href=\"http://localhost:3000/user/repo/issues/3333\">#3333</a>",
  236. }
  237. for i := 0; i < len(testCases); i += 2 {
  238. renderer.AutoLink(buffer, []byte(testCases[i]), blackfriday.LINK_TYPE_NORMAL)
  239. line, _ := buffer.ReadString(0)
  240. So(line, ShouldEqual, testCases[i+1])
  241. }
  242. })
  243. Convey("It should render but not change non-issue URLs", func() {
  244. testCases := []string{
  245. "http://1111/2222/ssss-issues/3333?param=blah&blahh=333", "<a href=\"http://1111/2222/ssss-issues/3333?param=blah&amp;blahh=333\">http://1111/2222/ssss-issues/3333?param=blah&amp;blahh=333</a>",
  246. "http://test.com/issues/33333", "<a href=\"http://test.com/issues/33333\">http://test.com/issues/33333</a>",
  247. "http://test.com/issues/3", "<a href=\"http://test.com/issues/3\">http://test.com/issues/3</a>",
  248. "http://issues/333", "<a href=\"http://issues/333\">http://issues/333</a>",
  249. "https://issues/333", "<a href=\"https://issues/333\">https://issues/333</a>",
  250. "http://tissues/0", "<a href=\"http://tissues/0\">http://tissues/0</a>",
  251. }
  252. for i := 0; i < len(testCases); i += 2 {
  253. renderer.AutoLink(buffer, []byte(testCases[i]), blackfriday.LINK_TYPE_NORMAL)
  254. line, _ := buffer.ReadString(0)
  255. So(line, ShouldEqual, testCases[i+1])
  256. }
  257. })
  258. })
  259. })
  260. Convey("Rendering a commit URL", t, func() {
  261. setting.AppUrl = "http://localhost:3000/"
  262. htmlFlags := 0
  263. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  264. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  265. renderer := &Renderer{
  266. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  267. }
  268. buffer := new(bytes.Buffer)
  269. Convey("To the internal issue tracker", func() {
  270. Convey("It should correctly convert URLs", func() {
  271. testCases := []string{
  272. "http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae", " <code><a href=\"http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae\">d8a994ef24</a></code>",
  273. "http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2", " <code><a href=\"http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2\">d8a994ef24</a></code>",
  274. "https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2", "<a href=\"https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2\">https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2</a>",
  275. "https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae", "<a href=\"https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae\">https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae</a>",
  276. }
  277. for i := 0; i < len(testCases); i += 2 {
  278. renderer.AutoLink(buffer, []byte(testCases[i]), blackfriday.LINK_TYPE_NORMAL)
  279. line, _ := buffer.ReadString(0)
  280. So(line, ShouldEqual, testCases[i+1])
  281. }
  282. })
  283. })
  284. })
  285. }