owl-blogs/renderer_test.go

326 lines
8.5 KiB
Go
Raw Normal View History

2022-08-03 14:55:48 +00:00
package owl_test
2022-07-21 17:02:37 +00:00
import (
2022-08-03 14:55:48 +00:00
"h4kor/owl-blogs"
2022-07-24 18:29:31 +00:00
"os"
"path"
2022-07-21 17:02:37 +00:00
"strings"
"testing"
2022-09-01 20:01:36 +00:00
"time"
2022-07-21 17:02:37 +00:00
)
func TestCanRenderPost(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-10-13 19:03:16 +00:00
result, err := owl.RenderPost(post)
if err != nil {
t.Error("Error rendering post: " + err.Error())
return
}
if !strings.Contains(result, "<h1 class=\"p-name\">testpost</h1>") {
2022-07-21 17:02:37 +00:00
t.Error("Post title not rendered as h1. Got: " + result)
}
}
2022-07-21 17:44:07 +00:00
func TestRenderTwitterHandle(t *testing.T) {
user := getTestUser()
config, _ := user.Config()
config.TwitterHandle = "testhandle"
user.SetConfig(config)
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-10-13 19:03:16 +00:00
result, err := owl.RenderPost(post)
if err != nil {
t.Error("Error rendering post: " + err.Error())
return
}
if !strings.Contains(result, "href=\"https://twitter.com/testhandle\" rel=\"me\"") {
t.Error("Twitter handle not rendered. Got: " + result)
}
}
2022-09-08 19:28:05 +00:00
func TestRenderGitHubHandle(t *testing.T) {
user := getTestUser()
config, _ := user.Config()
config.GitHubHandle = "testhandle"
user.SetConfig(config)
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-10-13 19:03:16 +00:00
result, err := owl.RenderPost(post)
2022-09-08 19:28:05 +00:00
if err != nil {
t.Error("Error rendering post: " + err.Error())
return
}
if !strings.Contains(result, "href=\"https://github.com/testhandle\" rel=\"me\"") {
t.Error("GitHub handle not rendered. Got: " + result)
}
}
func TestRenderPostHEntry(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-10-13 19:03:16 +00:00
result, _ := owl.RenderPost(post)
if !strings.Contains(result, "class=\"h-entry\"") {
t.Error("h-entry container not rendered. Got: " + result)
}
if !strings.Contains(result, "class=\"p-name\"") {
t.Error("p-name not rendered. Got: " + result)
}
if !strings.Contains(result, "class=\"e-content\"") {
t.Error("e-content not rendered. Got: " + result)
}
}
2022-07-21 17:44:07 +00:00
func TestRendererUsesBaseTemplate(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-10-13 19:03:16 +00:00
result, err := owl.RenderPost(post)
if err != nil {
t.Error("Error rendering post: " + err.Error())
return
}
2022-07-23 07:11:25 +00:00
if !strings.Contains(result, "<html") {
2022-07-21 17:44:07 +00:00
t.Error("Base template not used. Got: " + result)
}
}
2022-07-23 15:19:47 +00:00
func TestCanRenderIndexPage(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost1", false)
user.CreateNewPost("testpost2", false)
2022-08-03 14:55:48 +00:00
result, _ := owl.RenderIndexPage(user)
2022-07-23 15:19:47 +00:00
if !strings.Contains(result, "testpost1") {
2022-07-24 14:30:49 +00:00
t.Error("Post title not rendered. Got: " + result)
2022-07-23 15:19:47 +00:00
}
if !strings.Contains(result, "testpost2") {
2022-07-24 14:30:49 +00:00
t.Error("Post title not rendered. Got: " + result)
2022-07-23 15:19:47 +00:00
}
}
2022-07-24 18:29:31 +00:00
func TestIndexPageContainsHFeedContainer(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost1", false)
result, _ := owl.RenderIndexPage(user)
if !strings.Contains(result, "<div class=\"h-feed\">") {
t.Error("h-feed container not rendered. Got: " + result)
}
}
func TestIndexPageContainsHEntryAndUUrl(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost1", false)
result, _ := owl.RenderIndexPage(user)
if !strings.Contains(result, "class=\"h-entry\"") {
t.Error("h-entry container not rendered. Got: " + result)
}
if !strings.Contains(result, "class=\"u-url\"") {
t.Error("u-url not rendered. Got: " + result)
}
}
2022-07-24 18:29:31 +00:00
func TestRenderIndexPageWithBrokenBaseTemplate(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost1", false)
user.CreateNewPost("testpost2", false)
2022-07-24 18:29:31 +00:00
os.WriteFile(path.Join(user.Dir(), "meta/base.html"), []byte("{{content}}"), 0644)
2022-08-03 14:55:48 +00:00
_, err := owl.RenderIndexPage(user)
2022-07-24 18:29:31 +00:00
if err == nil {
t.Error("Expected error rendering index page, got nil")
}
}
func TestRenderUserList(t *testing.T) {
repo := getTestRepo(owl.RepoConfig{})
2022-07-24 18:29:31 +00:00
repo.CreateUser("user1")
repo.CreateUser("user2")
2022-08-03 14:55:48 +00:00
result, err := owl.RenderUserList(repo)
2022-07-24 18:29:31 +00:00
if err != nil {
t.Error("Error rendering user list: " + err.Error())
}
if !strings.Contains(result, "user1") {
t.Error("Post title not rendered. Got: " + result)
}
if !strings.Contains(result, "user2") {
t.Error("Post title not rendered. Got: " + result)
}
}
2022-07-27 19:53:56 +00:00
func TestRendersHeaderTitle(t *testing.T) {
user := getTestUser()
2022-08-03 14:55:48 +00:00
user.SetConfig(owl.UserConfig{
2022-07-27 19:53:56 +00:00
Title: "Test Title",
SubTitle: "Test SubTitle",
HeaderColor: "#ff1337",
})
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-07-27 19:53:56 +00:00
2022-10-13 19:03:16 +00:00
result, _ := owl.RenderPost(post)
2022-07-27 19:53:56 +00:00
if !strings.Contains(result, "Test Title") {
t.Error("Header title not rendered. Got: " + result)
}
if !strings.Contains(result, "Test SubTitle") {
t.Error("Header subtitle not rendered. Got: " + result)
}
if !strings.Contains(result, "#ff1337") {
t.Error("Header color not rendered. Got: " + result)
}
}
2022-08-31 18:20:16 +00:00
func TestRenderPostIncludesRelToWebMention(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-08-31 18:20:16 +00:00
2022-10-13 19:03:16 +00:00
result, _ := owl.RenderPost(post)
2022-08-31 18:20:16 +00:00
if !strings.Contains(result, "rel=\"webmention\"") {
t.Error("webmention rel not rendered. Got: " + result)
}
if !strings.Contains(result, "href=\""+user.WebmentionUrl()+"\"") {
t.Error("webmention href not rendered. Got: " + result)
}
}
2022-09-01 20:01:36 +00:00
func TestRenderPostAddsLinksToApprovedWebmention(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-09-04 13:03:16 +00:00
webmention := owl.WebmentionIn{
2022-09-01 20:01:36 +00:00
Source: "http://example.com/source3",
Title: "Test Title",
ApprovalStatus: "approved",
RetrievedAt: time.Now().Add(time.Hour * -2),
}
post.PersistIncomingWebmention(webmention)
2022-09-04 13:03:16 +00:00
webmention = owl.WebmentionIn{
2022-09-01 20:01:36 +00:00
Source: "http://example.com/source4",
ApprovalStatus: "rejected",
RetrievedAt: time.Now().Add(time.Hour * -3),
}
post.PersistIncomingWebmention(webmention)
2022-09-01 20:01:36 +00:00
2022-10-13 19:03:16 +00:00
result, _ := owl.RenderPost(post)
2022-09-01 20:01:36 +00:00
if !strings.Contains(result, "http://example.com/source3") {
t.Error("webmention not rendered. Got: " + result)
}
if !strings.Contains(result, "Test Title") {
t.Error("webmention title not rendered. Got: " + result)
}
if strings.Contains(result, "http://example.com/source4") {
t.Error("unapproved webmention rendered. Got: " + result)
}
}
func TestRenderPostNotMentioningWebmentionsIfNoAvail(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-10-13 19:03:16 +00:00
result, _ := owl.RenderPost(post)
2022-09-01 20:01:36 +00:00
if strings.Contains(result, "Webmention") {
t.Error("Webmention mentioned. Got: " + result)
}
}
2022-09-06 18:00:12 +00:00
func TestRenderIncludesFullUrl(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-10-13 19:03:16 +00:00
result, _ := owl.RenderPost(post)
2022-09-06 18:00:12 +00:00
if !strings.Contains(result, "class=\"u-url\"") {
t.Error("u-url not rendered. Got: " + result)
}
if !strings.Contains(result, post.FullUrl()) {
t.Error("Full url not rendered. Got: " + result)
t.Error("Expected: " + post.FullUrl())
}
}
2022-09-10 13:22:18 +00:00
func TestAddAvatarIfExist(t *testing.T) {
user := getTestUser()
os.WriteFile(path.Join(user.MediaDir(), "avatar.png"), []byte("test"), 0644)
result, _ := owl.RenderIndexPage(user)
if !strings.Contains(result, "avatar.png") {
t.Error("Avatar not rendered. Got: " + result)
}
}
2022-10-07 17:51:13 +00:00
func TestAuthorNameInPost(t *testing.T) {
user := getTestUser()
user.SetConfig(owl.UserConfig{
Title: "Test Title",
SubTitle: "Test SubTitle",
HeaderColor: "#ff1337",
AuthorName: "Test Author",
})
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-10-07 17:51:13 +00:00
2022-10-13 19:03:16 +00:00
result, _ := owl.RenderPost(post)
2022-10-07 17:51:13 +00:00
if !strings.Contains(result, "Test Author") {
t.Error("Author Name not included. Got: " + result)
}
}
2022-10-10 18:59:06 +00:00
func TestRenderReplyWithoutText(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-10-10 18:59:06 +00:00
content := "---\n"
content += "title: test\n"
content += "date: Wed, 17 Aug 2022 10:50:02 +0000\n"
content += "reply: \n"
content += " url: https://example.com/post\n"
content += "---\n"
content += "\n"
content += "Hi \n"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
2022-10-13 19:03:16 +00:00
result, _ := owl.RenderPost(post)
2022-10-10 18:59:06 +00:00
if !strings.Contains(result, "https://example.com/post") {
t.Error("Reply url not rendered. Got: " + result)
}
}
func TestRenderReplyWithText(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-10-10 18:59:06 +00:00
content := "---\n"
content += "title: test\n"
content += "date: Wed, 17 Aug 2022 10:50:02 +0000\n"
content += "reply: \n"
content += " url: https://example.com/post\n"
content += " text: \"This is a reply\"\n"
content += "---\n"
content += "Hi \n"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
2022-10-13 19:03:16 +00:00
result, _ := owl.RenderPost(post)
2022-10-10 18:59:06 +00:00
if !strings.Contains(result, "https://example.com/post") {
t.Error("Reply url not rendered. Got: " + result)
}
if !strings.Contains(result, "This is a reply") {
t.Error("Reply text not rendered. Got: " + result)
}
}