gitea源码

lfs_view_test.go 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "testing"
  9. "code.gitea.io/gitea/models/db"
  10. "code.gitea.io/gitea/models/git"
  11. repo_model "code.gitea.io/gitea/models/repo"
  12. "code.gitea.io/gitea/models/unittest"
  13. user_model "code.gitea.io/gitea/models/user"
  14. "code.gitea.io/gitea/modules/lfs"
  15. api "code.gitea.io/gitea/modules/structs"
  16. "code.gitea.io/gitea/tests"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. )
  20. // check that files stored in LFS render properly in the web UI
  21. func TestLFSRender(t *testing.T) {
  22. defer tests.PrepareTestEnv(t)()
  23. session := loginUser(t, "user2")
  24. // check that a markup file is flagged with "Stored in Git LFS" and shows its text
  25. t.Run("Markup", func(t *testing.T) {
  26. defer tests.PrintCurrentTest(t)()
  27. req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/CONTRIBUTING.md")
  28. resp := session.MakeRequest(t, req, http.StatusOK)
  29. doc := NewHTMLParser(t, resp.Body).doc
  30. fileInfo := doc.Find("div.file-info-entry").First().Text()
  31. assert.Contains(t, fileInfo, "LFS")
  32. content := doc.Find("div.file-view").Text()
  33. assert.Contains(t, content, "Testing documents in LFS")
  34. })
  35. // check that an image is flagged with "Stored in Git LFS" and renders inline
  36. t.Run("Image", func(t *testing.T) {
  37. defer tests.PrintCurrentTest(t)()
  38. req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/jpeg.jpg")
  39. resp := session.MakeRequest(t, req, http.StatusOK)
  40. doc := NewHTMLParser(t, resp.Body).doc
  41. fileInfo := doc.Find("div.file-info-entry").First().Text()
  42. assert.Contains(t, fileInfo, "LFS")
  43. src, exists := doc.Find(".file-view img").Attr("src")
  44. assert.True(t, exists, "The image should be in an <img> tag")
  45. assert.Equal(t, "/user2/lfs/media/branch/master/jpeg.jpg", src, "The image should use the /media link because it's in LFS")
  46. })
  47. // check that a binary file is flagged with "Stored in Git LFS" and renders a /media/ link instead of a /raw/ link
  48. t.Run("Binary", func(t *testing.T) {
  49. defer tests.PrintCurrentTest(t)()
  50. req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/crypt.bin")
  51. resp := session.MakeRequest(t, req, http.StatusOK)
  52. doc := NewHTMLParser(t, resp.Body)
  53. fileInfo := doc.Find("div.file-info-entry").First().Text()
  54. assert.Contains(t, fileInfo, "LFS")
  55. // find new file view container
  56. fileViewContainer := doc.Find("[data-global-init=initRepoFileView]")
  57. assert.Equal(t, "/user2/lfs/media/branch/master/crypt.bin", fileViewContainer.AttrOr("data-raw-file-link", ""))
  58. AssertHTMLElement(t, doc, ".view-raw > .file-view-render-container > .file-view-raw-prompt", 1)
  59. })
  60. // check that a directory with a README file shows its text
  61. t.Run("Readme", func(t *testing.T) {
  62. defer tests.PrintCurrentTest(t)()
  63. req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/subdir")
  64. resp := session.MakeRequest(t, req, http.StatusOK)
  65. doc := NewHTMLParser(t, resp.Body).doc
  66. content := doc.Find("div.file-view").Text()
  67. assert.Contains(t, content, "Testing READMEs in LFS")
  68. })
  69. // check that an invalid lfs entry defaults to plaintext
  70. t.Run("Invalid", func(t *testing.T) {
  71. defer tests.PrintCurrentTest(t)()
  72. // the LFS exists
  73. req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/CONTRIBUTING.md")
  74. resp := session.MakeRequest(t, req, http.StatusOK)
  75. content := NewHTMLParser(t, resp.Body).Find("div.file-view").Text()
  76. assert.Contains(t, content, "Testing documents in LFS")
  77. // then make it disappear
  78. assert.NoError(t, db.TruncateBeans(t.Context(), &git.LFSMetaObject{}))
  79. req = NewRequest(t, "GET", "/user2/lfs/src/branch/master/CONTRIBUTING.md")
  80. resp = session.MakeRequest(t, req, http.StatusOK)
  81. content = NewHTMLParser(t, resp.Body).Find("div.file-view").Text()
  82. assert.Contains(t, content, "oid sha256:7b6b2c88dba9f760a1a58469b67fee2b698ef7e9399c4ca4f34a14ccbe39f623")
  83. })
  84. }
  85. // TestLFSLockView tests the LFS lock view on settings page of repositories
  86. func TestLFSLockView(t *testing.T) {
  87. defer tests.PrepareTestEnv(t)()
  88. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // in org 3
  89. repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) // own by org 3
  90. session := loginUser(t, user2.Name)
  91. // create a lock
  92. lockPath := "test_lfs_lock_view.zip"
  93. lockID := ""
  94. {
  95. req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks", repo3.FullName()), map[string]string{"path": lockPath})
  96. req.Header.Set("Accept", lfs.AcceptHeader)
  97. req.Header.Set("Content-Type", lfs.MediaType)
  98. resp := session.MakeRequest(t, req, http.StatusCreated)
  99. lockResp := &api.LFSLockResponse{}
  100. DecodeJSON(t, resp, lockResp)
  101. lockID = lockResp.Lock.ID
  102. }
  103. defer func() {
  104. // release the lock
  105. req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks/%s/unlock", repo3.FullName(), lockID), map[string]string{})
  106. req.Header.Set("Accept", lfs.AcceptHeader)
  107. req.Header.Set("Content-Type", lfs.MediaType)
  108. session.MakeRequest(t, req, http.StatusOK)
  109. }()
  110. t.Run("owner name", func(t *testing.T) {
  111. defer tests.PrintCurrentTest(t)()
  112. // make sure the display names are different, or the test is meaningless
  113. require.NoError(t, repo3.LoadOwner(t.Context()))
  114. require.NotEqual(t, user2.DisplayName(), repo3.Owner.DisplayName())
  115. req := NewRequest(t, "GET", fmt.Sprintf("/%s/settings/lfs/locks", repo3.FullName()))
  116. resp := session.MakeRequest(t, req, http.StatusOK)
  117. doc := NewHTMLParser(t, resp.Body).doc
  118. tr := doc.Find("table#lfs-files-locks-table tbody tr")
  119. require.Equal(t, 1, tr.Length())
  120. td := tr.First().Find("td")
  121. require.Equal(t, 4, td.Length())
  122. // path
  123. assert.Equal(t, lockPath, strings.TrimSpace(td.Eq(0).Text()))
  124. // owner name
  125. assert.Equal(t, user2.DisplayName(), strings.TrimSpace(td.Eq(1).Text()))
  126. })
  127. }