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