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.

lfs_view_test.go 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/http"
  6. "testing"
  7. "code.gitea.io/gitea/tests"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. // check that files stored in LFS render properly in the web UI
  11. func TestLFSRender(t *testing.T) {
  12. defer tests.PrepareTestEnv(t)()
  13. session := loginUser(t, "user2")
  14. // check that a markup file is flagged with "Stored in Git LFS" and shows its text
  15. t.Run("Markup", func(t *testing.T) {
  16. defer tests.PrintCurrentTest(t)()
  17. req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/CONTRIBUTING.md")
  18. resp := session.MakeRequest(t, req, http.StatusOK)
  19. doc := NewHTMLParser(t, resp.Body).doc
  20. fileInfo := doc.Find("div.file-info-entry").First().Text()
  21. assert.Contains(t, fileInfo, "Stored with Git LFS")
  22. content := doc.Find("div.file-view").Text()
  23. assert.Contains(t, content, "Testing documents in LFS")
  24. })
  25. // check that an image is flagged with "Stored in Git LFS" and renders inline
  26. t.Run("Image", func(t *testing.T) {
  27. defer tests.PrintCurrentTest(t)()
  28. req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/jpeg.jpg")
  29. resp := session.MakeRequest(t, req, http.StatusOK)
  30. doc := NewHTMLParser(t, resp.Body).doc
  31. fileInfo := doc.Find("div.file-info-entry").First().Text()
  32. assert.Contains(t, fileInfo, "Stored with Git LFS")
  33. src, exists := doc.Find(".file-view img").Attr("src")
  34. assert.True(t, exists, "The image should be in an <img> tag")
  35. assert.Equal(t, "/user2/lfs/media/branch/master/jpeg.jpg", src, "The image should use the /media link because it's in LFS")
  36. })
  37. // check that a binary file is flagged with "Stored in Git LFS" and renders a /media/ link instead of a /raw/ link
  38. t.Run("Binary", func(t *testing.T) {
  39. defer tests.PrintCurrentTest(t)()
  40. req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/crypt.bin")
  41. resp := session.MakeRequest(t, req, http.StatusOK)
  42. doc := NewHTMLParser(t, resp.Body).doc
  43. fileInfo := doc.Find("div.file-info-entry").First().Text()
  44. assert.Contains(t, fileInfo, "Stored with Git LFS")
  45. rawLink, exists := doc.Find("div.file-view > div.view-raw > a").Attr("href")
  46. assert.True(t, exists, "Download link should render instead of content because this is a binary file")
  47. assert.Equal(t, "/user2/lfs/media/branch/master/crypt.bin", rawLink, "The download link should use the proper /media link because it's in LFS")
  48. })
  49. // check that a directory with a README file shows its text
  50. t.Run("Readme", func(t *testing.T) {
  51. defer tests.PrintCurrentTest(t)()
  52. req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/subdir")
  53. resp := session.MakeRequest(t, req, http.StatusOK)
  54. doc := NewHTMLParser(t, resp.Body).doc
  55. content := doc.Find("div.file-view").Text()
  56. assert.Contains(t, content, "Testing READMEs in LFS")
  57. })
  58. }