user.Posts does not list drafts. #8
This commit is contained in:
parent
e176729358
commit
2fa453a408
1
post.go
1
post.go
|
@ -21,6 +21,7 @@ type PostMeta struct {
|
|||
Title string `yaml:"title"`
|
||||
Aliases []string `yaml:"aliases"`
|
||||
Date string `yaml:"date"`
|
||||
Draft bool `yaml:"draft"`
|
||||
}
|
||||
|
||||
func (post Post) Id() string {
|
||||
|
|
18
post_test.go
18
post_test.go
|
@ -1,6 +1,7 @@
|
|||
package owl_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
)
|
||||
|
@ -66,3 +67,20 @@ func TestPostUrlMediaPathWithSubDir(t *testing.T) {
|
|||
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")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
8
user.go
8
user.go
|
@ -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 {
|
||||
post Post
|
||||
date time.Time
|
||||
|
|
21
user_test.go
21
user_test.go
|
@ -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) {
|
||||
user := getTestUser()
|
||||
// Create a new post
|
||||
|
|
Loading…
Reference in New Issue