user.Posts does not list drafts. #8

This commit is contained in:
Niko Abeler 2022-08-20 22:35:51 +02:00
parent e176729358
commit 2fa453a408
4 changed files with 48 additions and 0 deletions

View File

@ -21,6 +21,7 @@ type PostMeta struct {
Title string `yaml:"title"` Title string `yaml:"title"`
Aliases []string `yaml:"aliases"` Aliases []string `yaml:"aliases"`
Date string `yaml:"date"` Date string `yaml:"date"`
Draft bool `yaml:"draft"`
} }
func (post Post) Id() string { func (post Post) Id() string {

View File

@ -1,6 +1,7 @@
package owl_test package owl_test
import ( import (
"os"
"path" "path"
"testing" "testing"
) )
@ -66,3 +67,20 @@ func TestPostUrlMediaPathWithSubDir(t *testing.T) {
t.Error(" Got: " + post.UrlPath()) t.Error(" Got: " + post.UrlPath())
} }
} }
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.MarkdownData()
if !meta.Draft {
t.Error("Draft should be true")
}
}

View File

@ -65,6 +65,14 @@ func (user User) Posts() ([]Post, error) {
} }
} }
// remove drafts
for i, post := range posts {
_, meta := post.MarkdownData()
if meta.Draft {
posts = append(posts[:i], posts[i+1:]...)
}
}
type PostWithDate struct { type PostWithDate struct {
post Post post Post
date time.Time date time.Time

View File

@ -147,6 +147,27 @@ func TestCannotListUserPostsWithoutIndexMd(t *testing.T) {
} }
} }
func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) {
// Create a new user
repo, _ := owl.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName())
// Create a new post
post, _ := user.CreateNewPost("testpost")
content := ""
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)
posts, _ := user.Posts()
if len(posts) != 0 {
t.Error("Found draft post")
}
}
func TestCanLoadPost(t *testing.T) { func TestCanLoadPost(t *testing.T) {
user := getTestUser() user := getTestUser()
// Create a new post // Create a new post