gitea源码

wiki_test.go 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/http"
  6. "net/url"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "testing"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/util"
  13. "code.gitea.io/gitea/tests"
  14. "github.com/PuerkitoBio/goquery"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func assertFileExist(t *testing.T, p string) {
  18. exist, err := util.IsExist(p)
  19. assert.NoError(t, err)
  20. assert.True(t, exist)
  21. }
  22. func assertFileEqual(t *testing.T, p string, content []byte) {
  23. bs, err := os.ReadFile(p)
  24. assert.NoError(t, err)
  25. assert.Equal(t, content, bs)
  26. }
  27. func TestRepoCloneWiki(t *testing.T) {
  28. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  29. defer tests.PrepareTestEnv(t)()
  30. dstPath := t.TempDir()
  31. r := u.String() + "user2/repo1.wiki.git"
  32. u, _ = url.Parse(r)
  33. u.User = url.UserPassword("user2", userPassword)
  34. t.Run("Clone", func(t *testing.T) {
  35. assert.NoError(t, git.Clone(t.Context(), u.String(), dstPath, git.CloneRepoOptions{}))
  36. assertFileEqual(t, filepath.Join(dstPath, "Home.md"), []byte("# Home page\n\nThis is the home page!\n"))
  37. assertFileExist(t, filepath.Join(dstPath, "Page-With-Image.md"))
  38. assertFileExist(t, filepath.Join(dstPath, "Page-With-Spaced-Name.md"))
  39. assertFileExist(t, filepath.Join(dstPath, "images"))
  40. assertFileExist(t, filepath.Join(dstPath, "files/Non-Renderable-File.zip"))
  41. assertFileExist(t, filepath.Join(dstPath, "jpeg.jpg"))
  42. })
  43. })
  44. }
  45. func Test_RepoWikiPages(t *testing.T) {
  46. defer tests.PrepareTestEnv(t)()
  47. url := "/user2/repo1/wiki/?action=_pages"
  48. req := NewRequest(t, "GET", url)
  49. resp := MakeRequest(t, req, http.StatusOK)
  50. doc := NewHTMLParser(t, resp.Body)
  51. expectedPagePaths := []string{
  52. "Home", "Page-With-Image", "Page-With-Spaced-Name", "Unescaped-File",
  53. }
  54. doc.Find("tr").Each(func(i int, s *goquery.Selection) {
  55. firstAnchor := s.Find("a").First()
  56. href, _ := firstAnchor.Attr("href")
  57. pagePath := strings.TrimPrefix(href, "/user2/repo1/wiki/")
  58. assert.Equal(t, expectedPagePaths[i], pagePath)
  59. })
  60. }