owl-blogs/cmd/owl/web/single_user_test.go

133 lines
3.8 KiB
Go
Raw Normal View History

2022-09-05 18:34:24 +00:00
package web_test
2022-08-03 17:41:13 +00:00
import (
2022-08-05 20:04:03 +00:00
owl "h4kor/owl-blogs"
2022-09-05 18:34:24 +00:00
main "h4kor/owl-blogs/cmd/owl/web"
2022-11-06 13:17:14 +00:00
"h4kor/owl-blogs/test/assertions"
2022-08-03 17:41:13 +00:00
"net/http"
"net/http/httptest"
"os"
"path"
"testing"
)
2022-08-05 20:04:03 +00:00
func getSingleUserTestRepo() (owl.Repository, owl.User) {
repo, _ := owl.CreateRepository(testRepoName(), owl.RepoConfig{SingleUser: "test-1"})
2022-08-05 20:04:03 +00:00
user, _ := repo.CreateUser("test-1")
return repo, user
2022-08-03 17:41:13 +00:00
}
2022-08-05 20:04:03 +00:00
func TestSingleUserUserIndexHandler(t *testing.T) {
repo, user := getSingleUserTestRepo()
2022-10-13 18:33:00 +00:00
user.CreateNewPost("post-1", false)
2022-08-03 17:41:13 +00:00
// Create Request and Response
req, err := http.NewRequest("GET", user.UrlPath(), nil)
assertions.AssertNoError(t, err, "Error creating request")
2022-08-03 17:41:13 +00:00
rr := httptest.NewRecorder()
2022-08-06 18:22:09 +00:00
router := main.SingleUserRouter(&repo)
2022-08-03 17:41:13 +00:00
router.ServeHTTP(rr, req)
assertions.AssertStatus(t, rr, http.StatusOK)
2022-08-03 17:41:13 +00:00
// Check the response body contains names of users
assertions.AssertContains(t, rr.Body.String(), "post-1")
2022-08-03 17:41:13 +00:00
}
2022-08-05 20:04:03 +00:00
func TestSingleUserPostHandler(t *testing.T) {
repo, user := getSingleUserTestRepo()
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("post-1", false)
2022-08-03 17:41:13 +00:00
// Create Request and Response
req, err := http.NewRequest("GET", post.UrlPath(), nil)
assertions.AssertNoError(t, err, "Error creating request")
2022-08-03 17:41:13 +00:00
rr := httptest.NewRecorder()
2022-08-06 18:22:09 +00:00
router := main.SingleUserRouter(&repo)
2022-08-03 17:41:13 +00:00
router.ServeHTTP(rr, req)
assertions.AssertStatus(t, rr, http.StatusOK)
2022-08-03 17:41:13 +00:00
}
2022-08-05 20:04:03 +00:00
func TestSingleUserPostMediaHandler(t *testing.T) {
repo, user := getSingleUserTestRepo()
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("post-1", false)
2022-08-03 17:41:13 +00:00
// Create test media file
path := path.Join(post.MediaDir(), "data.txt")
err := os.WriteFile(path, []byte("test"), 0644)
assertions.AssertNoError(t, err, "Error creating request")
2022-08-03 17:41:13 +00:00
// Create Request and Response
req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil)
assertions.AssertNoError(t, err, "Error creating request")
2022-08-03 17:41:13 +00:00
rr := httptest.NewRecorder()
2022-08-06 18:22:09 +00:00
router := main.SingleUserRouter(&repo)
2022-08-03 17:41:13 +00:00
router.ServeHTTP(rr, req)
assertions.AssertStatus(t, rr, http.StatusOK)
2022-08-03 17:41:13 +00:00
// Check the response body contains data of media file
2022-11-03 18:23:13 +00:00
assertions.Assert(t, rr.Body.String() == "test", "Media file data not returned")
2022-08-03 17:41:13 +00:00
}
2022-08-22 06:01:44 +00:00
func TestHasNoDraftsInList(t *testing.T) {
repo, user := getSingleUserTestRepo()
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("post-1", false)
2022-08-22 06:01:44 +00:00
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 += "<https://nesslabs.com/time-anxiety>\n"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
// Create Request and Response
req, err := http.NewRequest("GET", "/", nil)
assertions.AssertNoError(t, err, "Error creating request")
2022-08-22 06:01:44 +00:00
rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req)
// Check if title is in the response body
2022-11-03 18:23:13 +00:00
assertions.AssertNotContains(t, rr.Body.String(), "Articles September 2019")
2022-08-22 06:01:44 +00:00
}
2022-12-01 18:10:08 +00:00
func TestSingleUserUserPostListHandler(t *testing.T) {
repo, user := getSingleUserTestRepo()
user.CreateNewPostFull(owl.PostMeta{
Title: "post-1",
Type: "article",
}, "hi")
user.CreateNewPostFull(owl.PostMeta{
Title: "post-2",
Type: "note",
}, "hi")
list := owl.PostList{
Title: "list-1",
Id: "list-1",
Include: []string{"article"},
}
user.AddPostList(list)
// Create Request and Response
req, err := http.NewRequest("GET", user.ListUrl(list), nil)
assertions.AssertNoError(t, err, "Error creating request")
rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req)
assertions.AssertStatus(t, rr, http.StatusOK)
// Check the response body contains names of users
assertions.AssertContains(t, rr.Body.String(), "post-1")
assertions.AssertNotContains(t, rr.Body.String(), "post-2")
}