owl-blogs/post_test.go

166 lines
4.3 KiB
Go
Raw Normal View History

2022-08-03 14:55:48 +00:00
package owl_test
2022-07-21 17:44:07 +00:00
2022-08-01 17:50:29 +00:00
import (
2022-08-20 20:35:51 +00:00
"os"
2022-08-01 17:50:29 +00:00
"path"
2022-08-21 09:31:48 +00:00
"strings"
2022-08-01 17:50:29 +00:00
"testing"
)
2022-07-21 17:44:07 +00:00
func TestCanGetPostTitle(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost")
result := post.Title()
if result != "testpost" {
t.Error("Wrong Title. Got: " + result)
}
}
2022-08-01 17:50:29 +00:00
func TestMediaDir(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost")
result := post.MediaDir()
if result != path.Join(post.Dir(), "media") {
t.Error("Wrong MediaDir. Got: " + result)
}
}
2022-08-03 17:41:13 +00:00
func TestPostUrlPath(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost")
expected := "/user/" + user.Name() + "/posts/" + post.Id() + "/"
if !(post.UrlPath() == expected) {
t.Error("Wrong url path")
t.Error("Expected: " + expected)
t.Error(" Got: " + post.UrlPath())
}
}
2022-08-13 16:47:27 +00:00
func TestPostFullUrl(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost")
expected := "http://localhost:8080/user/" + user.Name() + "/posts/" + post.Id() + "/"
if !(post.FullUrl() == expected) {
t.Error("Wrong url path")
t.Error("Expected: " + expected)
t.Error(" Got: " + post.FullUrl())
}
}
2022-08-03 17:41:13 +00:00
func TestPostUrlMediaPath(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost")
expected := "/user/" + user.Name() + "/posts/" + post.Id() + "/media/data.png"
if !(post.UrlMediaPath("data.png") == expected) {
t.Error("Wrong url path")
t.Error("Expected: " + expected)
t.Error(" Got: " + post.UrlPath())
}
}
func TestPostUrlMediaPathWithSubDir(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost")
expected := "/user/" + user.Name() + "/posts/" + post.Id() + "/media/foo/data.png"
if !(post.UrlMediaPath("foo/data.png") == expected) {
t.Error("Wrong url path")
t.Error("Expected: " + expected)
t.Error(" Got: " + post.UrlPath())
}
}
2022-08-20 20:35:51 +00:00
func TestDraftInMetaData(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost")
content := "---\n"
content += "title: test\n"
content += "draft: true\n"
content += "---\n"
content += "\n"
content += "Write your post here.\n"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
meta := post.Meta()
2022-08-20 20:35:51 +00:00
if !meta.Draft {
t.Error("Draft should be true")
}
}
2022-08-21 09:31:48 +00:00
func TestNoRawHTMLIfDisallowedByRepo(t *testing.T) {
repo := getTestRepo()
user, _ := repo.CreateUser("testuser")
post, _ := user.CreateNewPost("testpost")
content := "---\n"
content += "title: test\n"
content += "draft: true\n"
content += "---\n"
content += "\n"
content += "<script>alert('foo')</script>\n"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
html := post.RenderedContent()
2022-08-21 09:31:48 +00:00
html_str := html.String()
if strings.Contains(html_str, "<script>") {
t.Error("HTML should not be allowed")
}
}
func TestRawHTMLIfAllowedByRepo(t *testing.T) {
repo := getTestRepo()
repo.SetAllowRawHtml(true)
user, _ := repo.CreateUser("testuser")
post, _ := user.CreateNewPost("testpost")
content := "---\n"
content += "title: test\n"
content += "draft: true\n"
content += "---\n"
content += "\n"
content += "<script>alert('foo')</script>\n"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
html := post.RenderedContent()
2022-08-21 09:31:48 +00:00
html_str := html.String()
if !strings.Contains(html_str, "<script>") {
t.Error("HTML should be allowed")
}
}
func TestLoadMeta(t *testing.T) {
repo := getTestRepo()
repo.SetAllowRawHtml(true)
user, _ := repo.CreateUser("testuser")
post, _ := user.CreateNewPost("testpost")
content := "---\n"
content += "title: test\n"
content += "draft: true\n"
content += "date: Wed, 17 Aug 2022 10:50:02 +0000\n"
content += "aliases:\n"
content += " - foo/bar/\n"
content += "---\n"
content += "\n"
content += "<script>alert('foo')</script>\n"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
err := post.LoadMeta()
if err != nil {
t.Errorf("Got Error: %v", err)
}
if post.Meta().Title != "test" {
t.Errorf("Expected title: %s, got %s", "test", post.Meta().Title)
}
if len(post.Meta().Aliases) != 1 || post.Meta().Aliases[0] != "foo/bar/" {
t.Errorf("Expected title: %v, got %v", []string{"foo/bar/"}, post.Meta().Aliases)
}
if post.Meta().Date != "Wed, 17 Aug 2022 10:50:02 +0000" {
t.Errorf("Expected title: %s, got %s", "Wed, 17 Aug 2022 10:50:02 +0000", post.Meta().Title)
}
if post.Meta().Draft != true {
t.Errorf("Expected title: %v, got %v", true, post.Meta().Draft)
}
}