code.gitea.io/gitea@v1.22.3/modules/templates/util_render_test.go (about)

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