diff --git a/models/fixtures/repo_unit.yml b/models/fixtures/repo_unit.yml index bb8715a202..dd188fe39e 100644 --- a/models/fixtures/repo_unit.yml +++ b/models/fixtures/repo_unit.yml @@ -608,6 +608,20 @@ type: 1 created_unix: 946684810 +# BEGIN Forgejo [GITEA] Fix generated source URL on rendered files +- + id: 1091 + repo_id: 49 + type: 1 + created_unix: 946684810 + +- + id: 1092 + repo_id: 49 + type: 2 + created_unix: 946684810 +# END Forgejo [GITEA] Fix generated source URL on rendered files + - id: 91 repo_id: 58 diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 96339c9699..c7d5b276d2 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -116,10 +116,10 @@
{{end}} {{end}} diff --git a/tests/integration/repo_test.go b/tests/integration/repo_test.go index 2fb1a37d31..9ace3ca30c 100644 --- a/tests/integration/repo_test.go +++ b/tests/integration/repo_test.go @@ -408,3 +408,39 @@ func TestMarkDownReadmeImageSubfolder(t *testing.T) { assert.True(t, exists, "Image not found in markdown file") assert.Equal(t, "/user2/repo1/media/branch/sub-home-md-img-check/docs/test-fake-img.jpg", src) } + +func TestGeneratedSourceLink(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + t.Run("Rendered file", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + req := NewRequest(t, "GET", "/user2/repo1/src/branch/master/README.md?display=source") + resp := MakeRequest(t, req, http.StatusOK) + doc := NewHTMLParser(t, resp.Body) + + dataURL, exists := doc.doc.Find(".copy-line-permalink").Attr("data-url") + assert.True(t, exists) + assert.Equal(t, "/user2/repo1/src/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d/README.md?display=source", dataURL) + + dataURL, exists = doc.doc.Find(".ref-in-new-issue").Attr("data-url-param-body-link") + assert.True(t, exists) + assert.Equal(t, "/user2/repo1/src/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d/README.md?display=source", dataURL) + }) + + t.Run("Non-Rendered file", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + session := loginUser(t, "user27") + req := NewRequest(t, "GET", "/user27/repo49/src/branch/master/test/test.txt") + resp := session.MakeRequest(t, req, http.StatusOK) + doc := NewHTMLParser(t, resp.Body) + + dataURL, exists := doc.doc.Find(".copy-line-permalink").Attr("data-url") + assert.True(t, exists) + assert.Equal(t, "/user27/repo49/src/commit/aacbdfe9e1c4b47f60abe81849045fa4e96f1d75/test/test.txt", dataURL) + + dataURL, exists = doc.doc.Find(".ref-in-new-issue").Attr("data-url-param-body-link") + assert.True(t, exists) + assert.Equal(t, "/user27/repo49/src/commit/aacbdfe9e1c4b47f60abe81849045fa4e96f1d75/test/test.txt", dataURL) + }) +}