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.
216 lines
6.0 KiB
216 lines
6.0 KiB
7 years ago
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package repo
|
||
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
"testing"
|
||
|
|
||
|
"code.gitea.io/gitea/models"
|
||
6 years ago
|
"code.gitea.io/gitea/modules/git"
|
||
7 years ago
|
"code.gitea.io/gitea/modules/test"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/web"
|
||
4 years ago
|
"code.gitea.io/gitea/services/forms"
|
||
5 years ago
|
wiki_service "code.gitea.io/gitea/services/wiki"
|
||
7 years ago
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
const content = "Wiki contents for unit tests"
|
||
|
const message = "Wiki commit message for unit tests"
|
||
|
|
||
7 years ago
|
func wikiEntry(t *testing.T, repo *models.Repository, wikiName string) *git.TreeEntry {
|
||
|
wikiRepo, err := git.OpenRepository(repo.WikiPath())
|
||
|
assert.NoError(t, err)
|
||
5 years ago
|
defer wikiRepo.Close()
|
||
7 years ago
|
commit, err := wikiRepo.GetBranchCommit("master")
|
||
|
assert.NoError(t, err)
|
||
|
entries, err := commit.ListEntries()
|
||
|
assert.NoError(t, err)
|
||
|
for _, entry := range entries {
|
||
5 years ago
|
if entry.Name() == wiki_service.NameToFilename(wikiName) {
|
||
7 years ago
|
return entry
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
7 years ago
|
}
|
||
|
|
||
|
func wikiContent(t *testing.T, repo *models.Repository, wikiName string) string {
|
||
7 years ago
|
entry := wikiEntry(t, repo, wikiName)
|
||
|
if !assert.NotNil(t, entry) {
|
||
|
return ""
|
||
|
}
|
||
6 years ago
|
reader, err := entry.Blob().DataAsync()
|
||
7 years ago
|
assert.NoError(t, err)
|
||
6 years ago
|
defer reader.Close()
|
||
7 years ago
|
bytes, err := ioutil.ReadAll(reader)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
|
return string(bytes)
|
||
|
}
|
||
|
|
||
|
func assertWikiExists(t *testing.T, repo *models.Repository, wikiName string) {
|
||
7 years ago
|
assert.NotNil(t, wikiEntry(t, repo, wikiName))
|
||
7 years ago
|
}
|
||
|
|
||
|
func assertWikiNotExists(t *testing.T, repo *models.Repository, wikiName string) {
|
||
7 years ago
|
assert.Nil(t, wikiEntry(t, repo, wikiName))
|
||
7 years ago
|
}
|
||
|
|
||
|
func assertPagesMetas(t *testing.T, expectedNames []string, metas interface{}) {
|
||
|
pageMetas, ok := metas.([]PageMeta)
|
||
|
if !assert.True(t, ok) {
|
||
|
return
|
||
|
}
|
||
4 years ago
|
if !assert.Len(t, pageMetas, len(expectedNames)) {
|
||
7 years ago
|
return
|
||
|
}
|
||
|
for i, pageMeta := range pageMetas {
|
||
|
assert.EqualValues(t, expectedNames[i], pageMeta.Name)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestWiki(t *testing.T) {
|
||
|
models.PrepareTestEnv(t)
|
||
|
|
||
|
ctx := test.MockContext(t, "user2/repo1/wiki/_pages")
|
||
|
ctx.SetParams(":page", "Home")
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
|
Wiki(ctx)
|
||
|
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
||
|
assert.EqualValues(t, "Home", ctx.Data["Title"])
|
||
6 years ago
|
assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name"}, ctx.Data["Pages"])
|
||
7 years ago
|
}
|
||
|
|
||
|
func TestWikiPages(t *testing.T) {
|
||
|
models.PrepareTestEnv(t)
|
||
|
|
||
|
ctx := test.MockContext(t, "user2/repo1/wiki/_pages")
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
|
WikiPages(ctx)
|
||
|
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
||
6 years ago
|
assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name"}, ctx.Data["Pages"])
|
||
7 years ago
|
}
|
||
|
|
||
|
func TestNewWiki(t *testing.T) {
|
||
|
models.PrepareTestEnv(t)
|
||
|
|
||
|
ctx := test.MockContext(t, "user2/repo1/wiki/_new")
|
||
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
|
NewWiki(ctx)
|
||
|
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
||
|
assert.EqualValues(t, ctx.Tr("repo.wiki.new_page"), ctx.Data["Title"])
|
||
|
}
|
||
|
|
||
|
func TestNewWikiPost(t *testing.T) {
|
||
|
for _, title := range []string{
|
||
|
"New page",
|
||
|
"&&&&",
|
||
|
} {
|
||
|
models.PrepareTestEnv(t)
|
||
|
|
||
|
ctx := test.MockContext(t, "user2/repo1/wiki/_new")
|
||
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
4 years ago
|
web.SetForm(ctx, &forms.NewWikiForm{
|
||
7 years ago
|
Title: title,
|
||
|
Content: content,
|
||
|
Message: message,
|
||
|
})
|
||
4 years ago
|
NewWikiPost(ctx)
|
||
7 years ago
|
assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
|
||
|
assertWikiExists(t, ctx.Repo.Repository, title)
|
||
|
assert.Equal(t, wikiContent(t, ctx.Repo.Repository, title), content)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestNewWikiPost_ReservedName(t *testing.T) {
|
||
|
models.PrepareTestEnv(t)
|
||
|
|
||
|
ctx := test.MockContext(t, "user2/repo1/wiki/_new")
|
||
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
4 years ago
|
web.SetForm(ctx, &forms.NewWikiForm{
|
||
7 years ago
|
Title: "_edit",
|
||
|
Content: content,
|
||
|
Message: message,
|
||
|
})
|
||
4 years ago
|
NewWikiPost(ctx)
|
||
7 years ago
|
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
||
|
assert.EqualValues(t, ctx.Tr("repo.wiki.reserved_page"), ctx.Flash.ErrorMsg)
|
||
|
assertWikiNotExists(t, ctx.Repo.Repository, "_edit")
|
||
|
}
|
||
|
|
||
|
func TestEditWiki(t *testing.T) {
|
||
|
models.PrepareTestEnv(t)
|
||
|
|
||
|
ctx := test.MockContext(t, "user2/repo1/wiki/_edit/Home")
|
||
|
ctx.SetParams(":page", "Home")
|
||
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
|
EditWiki(ctx)
|
||
|
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
||
|
assert.EqualValues(t, "Home", ctx.Data["Title"])
|
||
|
assert.Equal(t, wikiContent(t, ctx.Repo.Repository, "Home"), ctx.Data["content"])
|
||
|
}
|
||
|
|
||
|
func TestEditWikiPost(t *testing.T) {
|
||
|
for _, title := range []string{
|
||
|
"Home",
|
||
|
"New/<page>",
|
||
|
} {
|
||
|
models.PrepareTestEnv(t)
|
||
|
ctx := test.MockContext(t, "user2/repo1/wiki/_new/Home")
|
||
|
ctx.SetParams(":page", "Home")
|
||
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
4 years ago
|
web.SetForm(ctx, &forms.NewWikiForm{
|
||
7 years ago
|
Title: title,
|
||
|
Content: content,
|
||
|
Message: message,
|
||
|
})
|
||
4 years ago
|
EditWikiPost(ctx)
|
||
7 years ago
|
assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
|
||
|
assertWikiExists(t, ctx.Repo.Repository, title)
|
||
|
assert.Equal(t, wikiContent(t, ctx.Repo.Repository, title), content)
|
||
|
if title != "Home" {
|
||
|
assertWikiNotExists(t, ctx.Repo.Repository, "Home")
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestDeleteWikiPagePost(t *testing.T) {
|
||
|
models.PrepareTestEnv(t)
|
||
|
|
||
|
ctx := test.MockContext(t, "user2/repo1/wiki/Home/delete")
|
||
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
|
DeleteWikiPagePost(ctx)
|
||
|
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
||
|
assertWikiNotExists(t, ctx.Repo.Repository, "Home")
|
||
|
}
|
||
6 years ago
|
|
||
|
func TestWikiRaw(t *testing.T) {
|
||
|
for filepath, filetype := range map[string]string{
|
||
|
"jpeg.jpg": "image/jpeg",
|
||
5 years ago
|
"images/jpeg.jpg": "image/jpeg",
|
||
6 years ago
|
"Page With Spaced Name": "text/plain; charset=utf-8",
|
||
|
"Page-With-Spaced-Name": "text/plain; charset=utf-8",
|
||
|
"Page With Spaced Name.md": "text/plain; charset=utf-8",
|
||
|
"Page-With-Spaced-Name.md": "text/plain; charset=utf-8",
|
||
|
} {
|
||
|
models.PrepareTestEnv(t)
|
||
|
|
||
|
ctx := test.MockContext(t, "user2/repo1/wiki/raw/"+filepath)
|
||
|
ctx.SetParams("*", filepath)
|
||
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
|
WikiRaw(ctx)
|
||
|
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
||
|
assert.EqualValues(t, filetype, ctx.Resp.Header().Get("Content-Type"))
|
||
|
}
|
||
|
}
|