Don't allow access of draft posts. Resolved #8

This commit is contained in:
Niko Abeler 2022-08-20 22:46:52 +02:00
parent 2fa453a408
commit 2e0332618b
2 changed files with 47 additions and 0 deletions

View File

@ -88,11 +88,20 @@ func postHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request,
return
}
post, err := user.GetPost(postId)
if err != nil {
println("Error getting post: ", err.Error())
notFoundHandler(repo)(w, r)
return
}
_, meta := post.MarkdownData()
if meta.Draft {
println("Post is a draft")
notFoundHandler(repo)(w, r)
return
}
html, err := owl.RenderPost(post)
if err != nil {
println("Error rendering post: ", err.Error())

38
cmd/owl-web/post_test.go Normal file
View File

@ -0,0 +1,38 @@
package main_test
import (
main "h4kor/owl-blogs/cmd/owl-web"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestPostHandlerReturns404OnDrafts(t *testing.T) {
repo := getTestRepo()
user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1")
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)
// Create Request and Response
req, err := http.NewRequest("GET", post.UrlPath(), nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router := main.Router(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusNotFound {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusNotFound)
}
}