summaryrefslogtreecommitdiffstats
path: root/models/wiki_test.go
diff options
context:
space:
mode:
authorEthan Koenig <ethantkoenig@gmail.com>2017-11-28 01:43:51 -0800
committerLunny Xiao <xiaolunwen@gmail.com>2017-11-28 17:43:51 +0800
commitb7ebaf6d2078cbf4de00d0782be8bc1b1de644bb (patch)
tree5ad8ef6f9738883e89559112e965b9a0bf303476 /models/wiki_test.go
parent6a58e3f9fcb8b9f30345e2f8a5812bda72c6baf5 (diff)
downloadgitea-b7ebaf6d2078cbf4de00d0782be8bc1b1de644bb.tar.gz
gitea-b7ebaf6d2078cbf4de00d0782be8bc1b1de644bb.zip
Various wiki bug fixes (#2996)
* Update macaron * Various wiki bug fixes
Diffstat (limited to 'models/wiki_test.go')
-rw-r--r--models/wiki_test.go148
1 files changed, 135 insertions, 13 deletions
diff --git a/models/wiki_test.go b/models/wiki_test.go
index ed914d1e55..e80c6cbb8d 100644
--- a/models/wiki_test.go
+++ b/models/wiki_test.go
@@ -5,24 +5,91 @@
package models
import (
+ "path"
"path/filepath"
"testing"
"code.gitea.io/gitea/modules/setting"
+ "github.com/Unknwon/com"
"github.com/stretchr/testify/assert"
)
-func TestToWikiPageURL(t *testing.T) {
- assert.Equal(t, "wiki-name", ToWikiPageURL("wiki-name"))
- assert.Equal(t, "wiki-name-with-many-spaces", ToWikiPageURL("wiki name with many spaces"))
+func TestNormalizeWikiName(t *testing.T) {
+ type test struct {
+ Expected string
+ WikiName string
+ }
+ for _, test := range []test{
+ {"wiki name", "wiki name"},
+ {"wiki name", "wiki-name"},
+ {"name with/slash", "name with/slash"},
+ {"name with%percent", "name-with%percent"},
+ {"%2F", "%2F"},
+ } {
+ assert.Equal(t, test.Expected, NormalizeWikiName(test.WikiName))
+ }
}
-func TestToWikiPageName(t *testing.T) {
- assert.Equal(t, "wiki name", ToWikiPageName("wiki name"))
- assert.Equal(t, "wiki name", ToWikiPageName("wiki-name"))
- assert.Equal(t, "wiki name", ToWikiPageName("wiki\tname"))
- assert.Equal(t, "wiki name", ToWikiPageName("./.././wiki/name"))
+func TestWikiNameToFilename(t *testing.T) {
+ type test struct {
+ Expected string
+ WikiName string
+ }
+ for _, test := range []test{
+ {"wiki-name.md", "wiki name"},
+ {"wiki-name.md", "wiki-name"},
+ {"name-with%2Fslash.md", "name with/slash"},
+ {"name-with%25percent.md", "name with%percent"},
+ } {
+ assert.Equal(t, test.Expected, WikiNameToFilename(test.WikiName))
+ }
+}
+
+func TestWikiNameToSubURL(t *testing.T) {
+ type test struct {
+ Expected string
+ WikiName string
+ }
+ for _, test := range []test{
+ {"wiki-name", "wiki name"},
+ {"wiki-name", "wiki-name"},
+ {"name-with%2Fslash", "name with/slash"},
+ {"name-with%25percent", "name with%percent"},
+ } {
+ assert.Equal(t, test.Expected, WikiNameToSubURL(test.WikiName))
+ }
+}
+
+func TestWikiFilenameToName(t *testing.T) {
+ type test struct {
+ Expected string
+ Filename string
+ }
+ for _, test := range []test{
+ {"hello world", "hello-world.md"},
+ {"symbols/?*", "symbols%2F%3F%2A.md"},
+ } {
+ name, err := WikiFilenameToName(test.Filename)
+ assert.NoError(t, err)
+ assert.Equal(t, test.Expected, name)
+ }
+}
+
+func TestWikiNameToFilenameToName(t *testing.T) {
+ // converting from wiki name to filename, then back to wiki name should
+ // return the original (normalized) name
+ for _, name := range []string{
+ "wiki-name",
+ "wiki name",
+ "wiki name with/slash",
+ "$$$%%%^^&&!@#$(),.<>",
+ } {
+ filename := WikiNameToFilename(name)
+ resultName, err := WikiFilenameToName(filename)
+ assert.NoError(t, err)
+ assert.Equal(t, NormalizeWikiName(name), resultName)
+ }
}
func TestRepository_WikiCloneLink(t *testing.T) {
@@ -47,17 +114,72 @@ func TestRepository_WikiPath(t *testing.T) {
assert.Equal(t, expected, repo.WikiPath())
}
-// TODO TestRepository_HasWiki
+func TestRepository_HasWiki(t *testing.T) {
+ prepareTestEnv(t)
+ repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
+ assert.True(t, repo1.HasWiki())
+ repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
+ assert.False(t, repo2.HasWiki())
+}
+
+func TestRepository_InitWiki(t *testing.T) {
+ prepareTestEnv(t)
+ // repo1 already has a wiki
+ repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
+ assert.NoError(t, repo1.InitWiki())
-// TODO TestRepository_InitWiki
+ // repo2 does not already have a wiki
+ repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
+ assert.NoError(t, repo2.InitWiki())
+ assert.True(t, repo2.HasWiki())
+}
func TestRepository_LocalWikiPath(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ prepareTestEnv(t)
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
expected := filepath.Join(setting.AppDataPath, "tmp/local-wiki/1")
assert.Equal(t, expected, repo.LocalWikiPath())
}
-// TODO TestRepository_UpdateLocalWiki
+func TestRepository_AddWikiPage(t *testing.T) {
+ const wikiContent = "This is the wiki content"
+ const commitMsg = "Commit message"
+ repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
+ doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
+ for _, wikiName := range []string{
+ "Another page",
+ "Here's a <tag> and a/slash",
+ } {
+ prepareTestEnv(t)
+ assert.NoError(t, repo.AddWikiPage(doer, wikiName, wikiContent, commitMsg))
+ expectedPath := path.Join(repo.LocalWikiPath(), WikiNameToFilename(wikiName))
+ assert.True(t, com.IsExist(expectedPath))
+ }
+}
+
+func TestRepository_EditWikiPage(t *testing.T) {
+ const newWikiContent = "This is the new content"
+ const commitMsg = "Commit message"
+ repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
+ doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
+ for _, newWikiName := range []string{
+ "New home",
+ "New/name/with/slashes",
+ } {
+ prepareTestEnv(t)
+ assert.NoError(t, repo.EditWikiPage(doer, "Home", newWikiName, newWikiContent, commitMsg))
+ newPath := path.Join(repo.LocalWikiPath(), WikiNameToFilename(newWikiName))
+ assert.True(t, com.IsExist(newPath))
+ oldPath := path.Join(repo.LocalWikiPath(), "Home.md")
+ assert.False(t, com.IsExist(oldPath))
+ }
+}
-// TODO ... (all remaining untested functions)
+func TestRepository_DeleteWikiPage(t *testing.T) {
+ prepareTestEnv(t)
+ repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
+ doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
+ assert.NoError(t, repo.DeleteWikiPage(doer, "Home"))
+ wikiPath := path.Join(repo.LocalWikiPath(), "Home.md")
+ assert.False(t, com.IsExist(wikiPath))
+}