owl-blogs/renderer_test.go

399 lines
12 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-11-06 13:17:14 +00:00
"h4kor/owl-blogs/test/assertions"
2022-07-24 18:29:31 +00:00
"os"
"path"
2022-07-21 17:02:37 +00:00
"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)
assertions.AssertNoError(t, err, "Error rendering post")
assertions.AssertContains(t, result, "testpost")
2022-07-21 17:02:37 +00:00
}
2022-07-21 17:44:07 +00:00
2022-11-01 20:35:47 +00:00
func TestRenderOneMe(t *testing.T) {
user := getTestUser()
2022-11-03 19:25:53 +00:00
config := user.Config()
2022-11-01 20:35:47 +00:00
config.Me = append(config.Me, owl.UserMe{
Name: "Twitter",
Url: "https://twitter.com/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)
assertions.AssertNoError(t, err, "Error rendering post")
assertions.AssertContains(t, result, "href=\"https://twitter.com/testhandle\" rel=\"me\"")
}
2022-11-01 20:35:47 +00:00
func TestRenderTwoMe(t *testing.T) {
2022-09-08 19:28:05 +00:00
user := getTestUser()
2022-11-03 19:25:53 +00:00
config := user.Config()
2022-11-01 20:35:47 +00:00
config.Me = append(config.Me, owl.UserMe{
Name: "Twitter",
Url: "https://twitter.com/testhandle",
})
config.Me = append(config.Me, owl.UserMe{
Name: "Github",
Url: "https://github.com/testhandle",
})
2022-09-08 19:28:05 +00:00
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
assertions.AssertNoError(t, err, "Error rendering post")
assertions.AssertContains(t, result, "href=\"https://twitter.com/testhandle\" rel=\"me\"")
assertions.AssertContains(t, result, "href=\"https://github.com/testhandle\" rel=\"me\"")
2022-09-08 19:28:05 +00:00
}
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)
assertions.AssertContains(t, result, "class=\"h-entry\"")
assertions.AssertContains(t, result, "class=\"p-name\"")
assertions.AssertContains(t, result, "class=\"e-content\"")
}
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)
assertions.AssertNoError(t, err, "Error rendering post")
assertions.AssertContains(t, result, "<html")
2022-07-21 17:44:07 +00:00
}
2022-07-23 15:19:47 +00:00
2022-12-01 18:10:08 +00:00
func TestCanRenderPostList(t *testing.T) {
user := getTestUser()
user.CreateNewPost("testpost1", false)
user.CreateNewPost("testpost2", false)
result, _ := owl.RenderPostList(user, &owl.PostList{
Id: "testlist",
Title: "Test List",
Include: []string{
"article",
},
})
assertions.AssertContains(t, result, "testpost1")
assertions.AssertContains(t, result, "testpost2")
}
func TestCanRenderPostListNotIncludeOther(t *testing.T) {
user := getTestUser()
user.CreateNewPostFull(owl.PostMeta{Title: "testpost1", Type: "article"}, "testpost1")
user.CreateNewPostFull(owl.PostMeta{Title: "testpost2", Type: "note"}, "testpost2")
result, _ := owl.RenderPostList(user, &owl.PostList{
Id: "testlist",
Title: "Test List",
Include: []string{
"article",
},
})
assertions.AssertContains(t, result, "testpost1")
assertions.AssertNotContains(t, result, "testpost2")
}
func TestCanRenderPostListNotIncludeMultiple(t *testing.T) {
user := getTestUser()
user.CreateNewPostFull(owl.PostMeta{Title: "testpost1", Type: "article"}, "testpost1")
user.CreateNewPostFull(owl.PostMeta{Title: "testpost2", Type: "note"}, "testpost2")
user.CreateNewPostFull(owl.PostMeta{Title: "testpost3", Type: "recipe"}, "testpost3")
result, _ := owl.RenderPostList(user, &owl.PostList{
Id: "testlist",
Title: "Test List",
Include: []string{
"article", "recipe",
},
})
assertions.AssertContains(t, result, "testpost1")
assertions.AssertNotContains(t, result, "testpost2")
assertions.AssertContains(t, result, "testpost3")
}
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)
assertions.AssertContains(t, result, "testpost1")
assertions.AssertContains(t, result, "testpost2")
2022-07-23 15:19:47 +00:00
}
2022-07-24 18:29:31 +00:00
2022-11-19 15:02:41 +00:00
func TestCanRenderIndexPageNoTitle(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPostFull(owl.PostMeta{}, "hi")
result, _ := owl.RenderIndexPage(user)
assertions.AssertContains(t, result, post.Id())
assertions.AssertContains(t, result, "Note: ")
}
func TestIndexPageContainsHFeedContainer(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost1", false)
result, _ := owl.RenderIndexPage(user)
assertions.AssertContains(t, result, "<div class=\"h-feed\">")
}
func TestIndexPageContainsHEntryAndUUrl(t *testing.T) {
user := getTestUser()
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost1", false)
result, _ := owl.RenderIndexPage(user)
assertions.AssertContains(t, result, "class=\"h-entry\"")
assertions.AssertContains(t, result, "class=\"u-url\"")
}
2022-11-29 20:22:39 +00:00
func TestIndexPageDoesNotContainsArticle(t *testing.T) {
user := getTestUser()
user.CreateNewPostFull(owl.PostMeta{Type: "article"}, "hi")
result, _ := owl.RenderIndexPage(user)
assertions.AssertContains(t, result, "class=\"h-entry\"")
assertions.AssertContains(t, result, "class=\"u-url\"")
}
func TestIndexPageDoesNotContainsReply(t *testing.T) {
user := getTestUser()
user.CreateNewPostFull(owl.PostMeta{Type: "reply", Reply: owl.Reply{Url: "https://example.com/post"}}, "hi")
result, _ := owl.RenderIndexPage(user)
assertions.AssertContains(t, result, "class=\"h-entry\"")
assertions.AssertContains(t, result, "class=\"u-url\"")
}
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-11-03 18:23:13 +00:00
assertions.AssertError(t, err, "Expected error rendering index page")
2022-07-24 18:29:31 +00:00
}
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)
assertions.AssertNoError(t, err, "Error rendering user list")
assertions.AssertContains(t, result, "user1")
assertions.AssertContains(t, result, "user2")
2022-07-24 18:29:31 +00:00
}
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)
assertions.AssertContains(t, result, "Test Title")
assertions.AssertContains(t, result, "Test SubTitle")
assertions.AssertContains(t, result, "#ff1337")
2022-07-27 19:53:56 +00:00
}
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)
assertions.AssertContains(t, result, "rel=\"webmention\"")
2022-08-31 18:20:16 +00:00
assertions.AssertContains(t, result, "href=\""+user.WebmentionUrl()+"\"")
2022-08-31 18:20:16 +00:00
}
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)
assertions.AssertContains(t, result, "http://example.com/source3")
assertions.AssertContains(t, result, "Test Title")
2022-11-03 18:23:13 +00:00
assertions.AssertNotContains(t, result, "http://example.com/source4")
2022-09-01 20:01:36 +00:00
}
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
2022-11-03 18:23:13 +00:00
assertions.AssertNotContains(t, result, "Webmention")
2022-09-01 20:01:36 +00:00
}
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
assertions.AssertContains(t, result, "class=\"u-url\"")
assertions.AssertContains(t, result, post.FullUrl())
2022-09-06 18:00:12 +00:00
}
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)
assertions.AssertContains(t, result, "avatar.png")
2022-09-10 13:22:18 +00:00
}
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)
assertions.AssertContains(t, result, "Test Author")
2022-10-07 17:51:13 +00:00
}
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)
assertions.AssertContains(t, result, "https://example.com/post")
2022-10-10 18:59:06 +00:00
}
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)
assertions.AssertContains(t, result, "https://example.com/post")
2022-10-10 18:59:06 +00:00
assertions.AssertContains(t, result, "This is a reply")
2022-10-10 18:59:06 +00:00
}
2022-10-19 19:14:31 +00:00
func TestOpenGraphTags(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost", false)
content := "---\n"
content += "title: The Rock\n"
content += "description: Dwayne Johnson\n"
content += "date: Wed, 17 Aug 2022 10:50:02 +0000\n"
content += "---\n"
content += "\n"
content += "Hi \n"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
2022-10-19 19:14:31 +00:00
post, _ = user.GetPost(post.Id())
result, _ := owl.RenderPost(post)
assertions.AssertContains(t, result, "<meta property=\"og:title\" content=\"The Rock\" />")
assertions.AssertContains(t, result, "<meta property=\"og:description\" content=\"Dwayne Johnson\" />")
assertions.AssertContains(t, result, "<meta property=\"og:type\" content=\"article\" />")
assertions.AssertContains(t, result, "<meta property=\"og:url\" content=\""+post.FullUrl()+"\" />")
2022-10-19 19:14:31 +00:00
}
2022-11-01 20:14:03 +00:00
func TestAddFaviconIfExist(t *testing.T) {
user := getTestUser()
os.WriteFile(path.Join(user.MediaDir(), "favicon.png"), []byte("test"), 0644)
result, _ := owl.RenderIndexPage(user)
assertions.AssertContains(t, result, "favicon.png")
2022-11-01 20:14:03 +00:00
}
2022-11-03 20:22:55 +00:00
func TestRenderUserAuth(t *testing.T) {
user := getTestUser()
user.ResetPassword("test")
2022-11-04 20:53:14 +00:00
result, err := owl.RenderUserAuthPage(owl.AuthRequestData{
User: user,
})
2022-11-03 20:22:55 +00:00
assertions.AssertNoError(t, err, "Error rendering user auth page")
assertions.AssertContains(t, result, "<form")
}
2022-11-04 20:53:14 +00:00
func TestRenderUserAuthIncludesClientId(t *testing.T) {
user := getTestUser()
user.ResetPassword("test")
result, err := owl.RenderUserAuthPage(owl.AuthRequestData{
User: user,
ClientId: "https://example.com/",
})
assertions.AssertNoError(t, err, "Error rendering user auth page")
assertions.AssertContains(t, result, "https://example.com/")
}
func TestRenderUserAuthHiddenFields(t *testing.T) {
user := getTestUser()
user.ResetPassword("test")
result, err := owl.RenderUserAuthPage(owl.AuthRequestData{
User: user,
ClientId: "https://example.com/",
RedirectUri: "https://example.com/redirect",
ResponseType: "code",
State: "teststate",
})
assertions.AssertNoError(t, err, "Error rendering user auth page")
assertions.AssertContains(t, result, "name=\"client_id\" value=\"https://example.com/\"")
assertions.AssertContains(t, result, "name=\"redirect_uri\" value=\"https://example.com/redirect\"")
assertions.AssertContains(t, result, "name=\"response_type\" value=\"code\"")
assertions.AssertContains(t, result, "name=\"state\" value=\"teststate\"")
}