Files
Atay-Makhzan/tests/integration/wiki_test.go
T

119 lines
3.6 KiB
Go
Raw Normal View History

2021-04-17 05:39:21 +08:00
// Copyright 2021 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2021-04-17 05:39:21 +08:00
2022-09-02 15:18:23 -04:00
package integration
2021-04-17 05:39:21 +08:00
import (
2024-12-05 23:39:50 -08:00
"net/http"
2021-04-17 05:39:21 +08:00
"net/url"
"os"
2021-04-17 05:39:21 +08:00
"path/filepath"
2024-12-05 23:39:50 -08:00
"strings"
2021-04-17 05:39:21 +08:00
"testing"
2025-11-02 00:52:59 -07:00
auth_model "code.gitea.io/gitea/models/auth"
2021-04-17 05:39:21 +08:00
"code.gitea.io/gitea/modules/git"
2025-11-02 00:52:59 -07:00
api "code.gitea.io/gitea/modules/structs"
2021-04-17 05:39:21 +08:00
"code.gitea.io/gitea/modules/util"
2022-09-02 15:18:23 -04:00
"code.gitea.io/gitea/tests"
2024-12-05 23:39:50 -08:00
"github.com/PuerkitoBio/goquery"
2021-04-17 05:39:21 +08:00
"github.com/stretchr/testify/assert"
)
func assertFileExist(t *testing.T, p string) {
exist, err := util.IsExist(p)
assert.NoError(t, err)
assert.True(t, exist)
}
func assertFileEqual(t *testing.T, p string, content []byte) {
bs, err := os.ReadFile(p)
2021-04-17 05:39:21 +08:00
assert.NoError(t, err)
2025-03-31 07:53:48 +02:00
assert.Equal(t, content, bs)
2021-04-17 05:39:21 +08:00
}
func TestRepoCloneWiki(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
2022-09-02 15:18:23 -04:00
defer tests.PrepareTestEnv(t)()
2021-04-17 05:39:21 +08:00
dstPath := t.TempDir()
2021-04-17 05:39:21 +08:00
2025-04-01 12:14:01 +02:00
r := u.String() + "user2/repo1.wiki.git"
2021-04-17 05:39:21 +08:00
u, _ = url.Parse(r)
u.User = url.UserPassword("user2", userPassword)
t.Run("Clone", func(t *testing.T) {
2025-08-28 00:31:21 +08:00
assert.NoError(t, git.Clone(t.Context(), u.String(), dstPath, git.CloneRepoOptions{}))
2021-04-17 05:39:21 +08:00
assertFileEqual(t, filepath.Join(dstPath, "Home.md"), []byte("# Home page\n\nThis is the home page!\n"))
assertFileExist(t, filepath.Join(dstPath, "Page-With-Image.md"))
assertFileExist(t, filepath.Join(dstPath, "Page-With-Spaced-Name.md"))
assertFileExist(t, filepath.Join(dstPath, "images"))
assertFileExist(t, filepath.Join(dstPath, "files/Non-Renderable-File.zip"))
2021-04-17 05:39:21 +08:00
assertFileExist(t, filepath.Join(dstPath, "jpeg.jpg"))
})
})
}
2024-12-05 23:39:50 -08:00
func Test_RepoWikiPages(t *testing.T) {
defer tests.PrepareTestEnv(t)()
url := "/user2/repo1/wiki/?action=_pages"
req := NewRequest(t, "GET", url)
resp := MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body)
expectedPagePaths := []string{
"Home", "Page-With-Image", "Page-With-Spaced-Name", "Unescaped-File",
}
doc.Find("tr").Each(func(i int, s *goquery.Selection) {
firstAnchor := s.Find("a").First()
href, _ := firstAnchor.Attr("href")
pagePath := strings.TrimPrefix(href, "/user2/repo1/wiki/")
2025-03-31 07:53:48 +02:00
assert.Equal(t, expectedPagePaths[i], pagePath)
2024-12-05 23:39:50 -08:00
})
}
2025-11-02 00:52:59 -07:00
func Test_WikiClone(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
username := "user2"
reponame := "repo1"
wikiPath := username + "/" + reponame + ".wiki.git"
keyname := "my-testing-key"
baseAPITestContext := NewAPITestContext(t, username, "repo1", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
u.Path = wikiPath
t.Run("Clone HTTP", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
dstLocalPath := t.TempDir()
assert.NoError(t, git.Clone(t.Context(), u.String(), dstLocalPath, git.CloneRepoOptions{}))
content, err := os.ReadFile(filepath.Join(dstLocalPath, "Home.md"))
assert.NoError(t, err)
assert.Equal(t, "# Home page\n\nThis is the home page!\n", string(content))
})
t.Run("Clone SSH", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
dstLocalPath := t.TempDir()
sshURL := createSSHUrl(wikiPath, u)
withKeyFile(t, keyname, func(keyFile string) {
var keyID int64
t.Run("CreateUserKey", doAPICreateUserKey(baseAPITestContext, "test-key", keyFile, func(t *testing.T, key api.PublicKey) {
keyID = key.ID
}))
assert.NotZero(t, keyID)
// Setup clone folder
assert.NoError(t, git.Clone(t.Context(), sshURL.String(), dstLocalPath, git.CloneRepoOptions{}))
content, err := os.ReadFile(filepath.Join(dstLocalPath, "Home.md"))
assert.NoError(t, err)
assert.Equal(t, "# Home page\n\nThis is the home page!\n", string(content))
})
})
})
}