2023-06-21 11:14:34 +02:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"html/template"
|
2024-01-15 09:49:24 +01:00
|
|
|
"os"
|
2024-04-13 12:05:33 +02:00
|
|
|
"strings"
|
2023-06-21 11:14:34 +02:00
|
|
|
"testing"
|
|
|
|
|
2024-04-13 12:05:33 +02:00
|
|
|
"code.gitea.io/gitea/models/issues"
|
2024-01-15 09:49:24 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2024-04-13 12:05:33 +02:00
|
|
|
"code.gitea.io/gitea/modules/translation"
|
2024-01-15 09:49:24 +01:00
|
|
|
|
2023-06-21 11:14:34 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-04-13 12:05:33 +02:00
|
|
|
func testInput() string {
|
|
|
|
s := ` space @mention-user<SPACE><SPACE>
|
2024-01-15 09:49:24 +01:00
|
|
|
/just/a/path.bin
|
|
|
|
https://example.com/file.bin
|
|
|
|
[local link](file.bin)
|
|
|
|
[remote link](https://example.com)
|
|
|
|
[[local link|file.bin]]
|
|
|
|
[[remote link|https://example.com]]
|
|
|
|
![local image](image.jpg)
|
|
|
|
![remote image](https://example.com/image.jpg)
|
|
|
|
[[local image|image.jpg]]
|
|
|
|
[[remote link|https://example.com/image.jpg]]
|
|
|
|
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
|
|
|
|
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
|
|
|
|
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
|
|
|
|
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
|
|
|
|
:+1:
|
|
|
|
mail@domain.com
|
|
|
|
@mention-user test
|
|
|
|
#123
|
2024-04-13 12:05:33 +02:00
|
|
|
space<SPACE><SPACE>
|
2024-01-15 09:49:24 +01:00
|
|
|
`
|
2024-04-13 12:05:33 +02:00
|
|
|
return strings.ReplaceAll(s, "<SPACE>", " ")
|
|
|
|
}
|
2024-01-15 09:49:24 +01:00
|
|
|
|
|
|
|
var testMetas = map[string]string{
|
|
|
|
"user": "user13",
|
|
|
|
"repo": "repo11",
|
|
|
|
"repoPath": "../../tests/gitea-repositories-meta/user13/repo11.git/",
|
|
|
|
"mode": "comment",
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
unittest.InitSettings()
|
|
|
|
if err := git.InitSimple(context.Background()); err != nil {
|
|
|
|
log.Fatal("git init failed, err: %v", err)
|
|
|
|
}
|
|
|
|
markup.Init(&markup.ProcessorHelper{
|
|
|
|
IsUsernameMentionable: func(ctx context.Context, username string) bool {
|
|
|
|
return username == "mention-user"
|
|
|
|
},
|
|
|
|
})
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:04:26 +01:00
|
|
|
func newTestRenderUtils() *RenderUtils {
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = context.WithValue(ctx, translation.ContextKey, &translation.MockLocale{})
|
|
|
|
return NewRenderUtils(ctx)
|
|
|
|
}
|
|
|
|
|
2023-06-21 11:14:34 +02:00
|
|
|
func TestRenderCommitBody(t *testing.T) {
|
|
|
|
type args struct {
|
2024-01-15 09:49:24 +01:00
|
|
|
msg string
|
|
|
|
metas map[string]string
|
2023-06-21 11:14:34 +02:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want template.HTML
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "multiple lines",
|
|
|
|
args: args{
|
|
|
|
msg: "first line\nsecond line",
|
|
|
|
},
|
|
|
|
want: "second line",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple lines with leading newlines",
|
|
|
|
args: args{
|
|
|
|
msg: "\n\n\n\nfirst line\nsecond line",
|
|
|
|
},
|
|
|
|
want: "second line",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple lines with trailing newlines",
|
|
|
|
args: args{
|
|
|
|
msg: "first line\nsecond line\n\n\n",
|
|
|
|
},
|
|
|
|
want: "second line",
|
|
|
|
},
|
|
|
|
}
|
2024-11-05 07:04:26 +01:00
|
|
|
ut := newTestRenderUtils()
|
2023-06-21 11:14:34 +02:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-11-05 07:04:26 +01:00
|
|
|
assert.Equalf(t, tt.want, ut.RenderCommitBody(tt.args.msg, tt.args.metas), "RenderCommitBody(%v, %v)", tt.args.msg, tt.args.metas)
|
2023-06-21 11:14:34 +02:00
|
|
|
})
|
|
|
|
}
|
2024-01-15 09:49:24 +01:00
|
|
|
|
|
|
|
expected := `/just/a/path.bin
|
|
|
|
<a href="https://example.com/file.bin" class="link">https://example.com/file.bin</a>
|
|
|
|
[local link](file.bin)
|
|
|
|
[remote link](<a href="https://example.com" class="link">https://example.com</a>)
|
|
|
|
[[local link|file.bin]]
|
|
|
|
[[remote link|<a href="https://example.com" class="link">https://example.com</a>]]
|
|
|
|
![local image](image.jpg)
|
|
|
|
![remote image](<a href="https://example.com/image.jpg" class="link">https://example.com/image.jpg</a>)
|
|
|
|
[[local image|image.jpg]]
|
|
|
|
[[remote link|<a href="https://example.com/image.jpg" class="link">https://example.com/image.jpg</a>]]
|
|
|
|
<a href="https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash" class="compare"><code class="nohighlight">88fc37a3c0...12fc37a3c0 (hash)</code></a>
|
|
|
|
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
|
|
|
|
<a href="https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb" class="commit"><code class="nohighlight">88fc37a3c0</code></a>
|
|
|
|
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
|
|
|
|
<span class="emoji" aria-label="thumbs up">👍</span>
|
|
|
|
<a href="mailto:mail@domain.com" class="mailto">mail@domain.com</a>
|
2024-03-13 11:34:58 +01:00
|
|
|
<a href="/mention-user" class="mention">@mention-user</a> test
|
|
|
|
<a href="/user13/repo11/issues/123" class="ref-issue">#123</a>
|
2024-01-15 09:49:24 +01:00
|
|
|
space`
|
|
|
|
|
2024-11-05 07:04:26 +01:00
|
|
|
assert.EqualValues(t, expected, newTestRenderUtils().RenderCommitBody(testInput(), testMetas))
|
2024-01-15 09:49:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderCommitMessage(t *testing.T) {
|
2024-03-13 11:34:58 +01:00
|
|
|
expected := `space <a href="/mention-user" class="mention">@mention-user</a> `
|
2024-01-15 09:49:24 +01:00
|
|
|
|
2024-11-05 07:04:26 +01:00
|
|
|
assert.EqualValues(t, expected, newTestRenderUtils().RenderCommitMessage(testInput(), testMetas))
|
2024-01-15 09:49:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderCommitMessageLinkSubject(t *testing.T) {
|
2024-03-13 11:34:58 +01:00
|
|
|
expected := `<a href="https://example.com/link" class="default-link muted">space </a><a href="/mention-user" class="mention">@mention-user</a>`
|
2024-01-15 09:49:24 +01:00
|
|
|
|
2024-11-05 07:04:26 +01:00
|
|
|
assert.EqualValues(t, expected, newTestRenderUtils().RenderCommitMessageLinkSubject(testInput(), "https://example.com/link", testMetas))
|
2024-01-15 09:49:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderIssueTitle(t *testing.T) {
|
2024-04-13 12:05:33 +02:00
|
|
|
expected := ` space @mention-user<SPACE><SPACE>
|
2024-01-15 09:49:24 +01:00
|
|
|
/just/a/path.bin
|
|
|
|
https://example.com/file.bin
|
|
|
|
[local link](file.bin)
|
|
|
|
[remote link](https://example.com)
|
|
|
|
[[local link|file.bin]]
|
|
|
|
[[remote link|https://example.com]]
|
|
|
|
![local image](image.jpg)
|
|
|
|
![remote image](https://example.com/image.jpg)
|
|
|
|
[[local image|image.jpg]]
|
|
|
|
[[remote link|https://example.com/image.jpg]]
|
|
|
|
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
|
|
|
|
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
|
|
|
|
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
|
|
|
|
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
|
|
|
|
<span class="emoji" aria-label="thumbs up">👍</span>
|
|
|
|
mail@domain.com
|
|
|
|
@mention-user test
|
2024-03-13 11:34:58 +01:00
|
|
|
<a href="/user13/repo11/issues/123" class="ref-issue">#123</a>
|
2024-04-13 12:05:33 +02:00
|
|
|
space<SPACE><SPACE>
|
2024-01-15 09:49:24 +01:00
|
|
|
`
|
2024-04-13 12:05:33 +02:00
|
|
|
expected = strings.ReplaceAll(expected, "<SPACE>", " ")
|
2024-11-05 07:04:26 +01:00
|
|
|
assert.EqualValues(t, expected, newTestRenderUtils().RenderIssueTitle(testInput(), testMetas))
|
2024-01-15 09:49:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderMarkdownToHtml(t *testing.T) {
|
2024-03-13 11:34:58 +01:00
|
|
|
expected := `<p>space <a href="/mention-user" rel="nofollow">@mention-user</a><br/>
|
2024-01-15 09:49:24 +01:00
|
|
|
/just/a/path.bin
|
|
|
|
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a>
|
|
|
|
<a href="/file.bin" rel="nofollow">local link</a>
|
|
|
|
<a href="https://example.com" rel="nofollow">remote link</a>
|
2024-06-18 05:09:20 +02:00
|
|
|
<a href="/file.bin" rel="nofollow">local link</a>
|
2024-01-15 09:49:24 +01:00
|
|
|
<a href="https://example.com" rel="nofollow">remote link</a>
|
|
|
|
<a href="/image.jpg" target="_blank" rel="nofollow noopener"><img src="/image.jpg" alt="local image"/></a>
|
|
|
|
<a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image"/></a>
|
|
|
|
<a href="/image.jpg" rel="nofollow"><img src="/image.jpg" title="local image" alt="local image"/></a>
|
|
|
|
<a href="https://example.com/image.jpg" rel="nofollow"><img src="https://example.com/image.jpg" title="remote link" alt="remote link"/></a>
|
|
|
|
<a href="https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash" rel="nofollow"><code>88fc37a3c0...12fc37a3c0 (hash)</code></a>
|
|
|
|
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
|
|
|
|
<a href="https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb" rel="nofollow"><code>88fc37a3c0</code></a>
|
|
|
|
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
|
|
|
|
<span class="emoji" aria-label="thumbs up">👍</span>
|
|
|
|
<a href="mailto:mail@domain.com" rel="nofollow">mail@domain.com</a>
|
2024-03-13 11:34:58 +01:00
|
|
|
<a href="/mention-user" rel="nofollow">@mention-user</a> test
|
2024-01-15 09:49:24 +01:00
|
|
|
#123
|
|
|
|
space</p>
|
|
|
|
`
|
2024-11-05 07:04:26 +01:00
|
|
|
assert.Equal(t, expected, string(newTestRenderUtils().MarkdownToHtml(testInput())))
|
2024-04-13 12:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderLabels(t *testing.T) {
|
2024-11-05 07:04:26 +01:00
|
|
|
ut := newTestRenderUtils()
|
2024-04-13 12:05:33 +02:00
|
|
|
label := &issues.Label{ID: 123, Name: "label-name", Color: "label-color"}
|
|
|
|
issue := &issues.Issue{}
|
|
|
|
expected := `/owner/repo/issues?labels=123`
|
2024-11-05 07:04:26 +01:00
|
|
|
assert.Contains(t, ut.RenderLabels([]*issues.Label{label}, "/owner/repo", issue), expected)
|
2024-04-13 12:05:33 +02:00
|
|
|
|
|
|
|
label = &issues.Label{ID: 123, Name: "label-name", Color: "label-color"}
|
|
|
|
issue = &issues.Issue{IsPull: true}
|
|
|
|
expected = `/owner/repo/pulls?labels=123`
|
2024-11-05 07:04:26 +01:00
|
|
|
assert.Contains(t, ut.RenderLabels([]*issues.Label{label}, "/owner/repo", issue), expected)
|
2023-06-21 11:14:34 +02:00
|
|
|
}
|
2024-05-02 03:00:46 +02:00
|
|
|
|
|
|
|
func TestUserMention(t *testing.T) {
|
2024-11-05 07:04:26 +01:00
|
|
|
rendered := newTestRenderUtils().MarkdownToHtml("@no-such-user @mention-user @mention-user")
|
2024-05-02 03:00:46 +02:00
|
|
|
assert.EqualValues(t, `<p>@no-such-user <a href="/mention-user" rel="nofollow">@mention-user</a> <a href="/mention-user" rel="nofollow">@mention-user</a></p>`, strings.TrimSpace(string(rendered)))
|
|
|
|
}
|