From 709f850a73bac75784c62b6b9a4264cda0cb241e Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Mon, 22 Aug 2022 08:01:44 +0200 Subject: [PATCH] tests for drafts --- cmd/owl-web/single_user_test.go | 35 +++++++++++++++++++++++++++++++++ user_test.go | 28 ++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/cmd/owl-web/single_user_test.go b/cmd/owl-web/single_user_test.go index a0582cf..e657a41 100644 --- a/cmd/owl-web/single_user_test.go +++ b/cmd/owl-web/single_user_test.go @@ -96,3 +96,38 @@ func TestSingleUserPostMediaHandler(t *testing.T) { t.Error(rr.Body.String()) } } + +func TestHasNoDraftsInList(t *testing.T) { + repo, user := getSingleUserTestRepo() + post, _ := user.CreateNewPost("post-1") + content := "" + content += "---\n" + content += "title: Articles September 2019\n" + content += "author: h4kor\n" + content += "type: post\n" + content += "date: -001-11-30T00:00:00+00:00\n" + content += "draft: true\n" + content += "url: /?p=426\n" + content += "categories:\n" + content += " - Uncategorised\n" + content += "\n" + content += "---\n" + content += "\n" + + os.WriteFile(post.ContentFile(), []byte(content), 0644) + + // Create Request and Response + req, err := http.NewRequest("GET", "/", nil) + if err != nil { + t.Fatal(err) + } + rr := httptest.NewRecorder() + router := main.SingleUserRouter(&repo) + router.ServeHTTP(rr, req) + + // Check if title is in the response body + if strings.Contains(rr.Body.String(), "Articles September 2019") { + t.Error("Articles September 2019 listed on index page. Got: ") + t.Error(rr.Body.String()) + } +} diff --git a/user_test.go b/user_test.go index c3ccb3a..f8f03f9 100644 --- a/user_test.go +++ b/user_test.go @@ -168,6 +168,34 @@ func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) { } } +func TestListUsersDraftsExcludedRealWorld(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: Articles September 2019\n" + content += "author: h4kor\n" + content += "type: post\n" + content += "date: -001-11-30T00:00:00+00:00\n" + content += "draft: true\n" + content += "url: /?p=426\n" + content += "categories:\n" + content += " - Uncategorised\n" + content += "\n" + content += "---\n" + content += "\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