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