From 70936659dafe20871d0c7e50d42319bbb48bb93b Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Wed, 3 Aug 2022 18:03:10 +0200 Subject: [PATCH] limit posts to single depth directory --- directories.go | 5 +++++ user.go | 10 ++++++---- user_test.go | 44 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/directories.go b/directories.go index 4a88a1c..09f6fb9 100644 --- a/directories.go +++ b/directories.go @@ -18,6 +18,11 @@ func listDir(path string) []string { return files } +func fileExists(path string) bool { + _, err := os.Stat(path) + return err == nil +} + // recursive list of all files in a directory func walkDir(path string) []string { files := make([]string, 0) diff --git a/user.go b/user.go index 7d39e5f..ed6fec0 100644 --- a/user.go +++ b/user.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "os" "path" - "strings" "time" "gopkg.in/yaml.v2" @@ -47,11 +46,14 @@ func (user User) Name() string { } func (user User) Posts() ([]string, error) { - postFiles := walkDir(path.Join(user.Dir(), "public")) + postFiles := listDir(path.Join(user.Dir(), "public")) posts := make([]string, 0) for _, id := range postFiles { - if strings.HasSuffix(id, "/index.md") { - posts = append(posts, id[:len(id)-9]) + // if is a directory and has index.md, add to posts + if dirExists(path.Join(user.Dir(), "public", id)) { + if fileExists(path.Join(user.Dir(), "public", id, "index.md")) { + posts = append(posts, id) + } } } return posts, nil diff --git a/user_test.go b/user_test.go index bb8bbaf..d61f039 100644 --- a/user_test.go +++ b/user_test.go @@ -69,7 +69,40 @@ func TestCanListUserPosts(t *testing.T) { } } -func TestCanListUserPostsWithSubdirectories(t *testing.T) { +func TestCannotListUserPostsInSubdirectories(t *testing.T) { + // Create a new user + repo, _ := owl.CreateRepository(testRepoName()) + user, _ := repo.CreateUser(randomUserName()) + // Create a new post + user.CreateNewPost("testpost") + os.Mkdir(path.Join(user.PostDir(), "foo"), 0755) + os.Mkdir(path.Join(user.PostDir(), "foo/bar"), 0755) + content := "" + content += "---\n" + content += "title: test\n" + content += "---\n" + content += "\n" + content += "Write your post here.\n" + + os.WriteFile(path.Join(user.PostDir(), "foo/index.md"), []byte(content), 0644) + os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644) + posts, _ := user.Posts() + if !contains(posts, "foo") { + t.Error("Does not contain post: foo. Found:") + for _, p := range posts { + t.Error("\t" + p) + } + } + + if contains(posts, "foo/bar") { + t.Error("Invalid post found: foo/bar. Found:") + for _, p := range posts { + t.Error("\t" + p) + } + } +} + +func TestCannotListUserPostsWithoutIndexMd(t *testing.T) { // Create a new user repo, _ := owl.CreateRepository(testRepoName()) user, _ := repo.CreateUser(randomUserName()) @@ -87,14 +120,7 @@ func TestCanListUserPostsWithSubdirectories(t *testing.T) { os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644) posts, _ := user.Posts() if contains(posts, "foo") { - t.Error("Contains non-post name: foo") - for _, p := range posts { - t.Error("\t" + p) - } - } - - if !contains(posts, "foo/bar") { - t.Error("Post not found. Found: ") + t.Error("Contains invalid post: foo. Found:") for _, p := range posts { t.Error("\t" + p) }