2022-08-03 14:55:48 +00:00
|
|
|
package owl_test
|
2022-07-21 17:44:07 +00:00
|
|
|
|
2022-08-01 17:50:29 +00:00
|
|
|
import (
|
2022-09-01 19:34:33 +00:00
|
|
|
"h4kor/owl-blogs"
|
2022-11-06 13:17:14 +00:00
|
|
|
"h4kor/owl-blogs/test/assertions"
|
|
|
|
"h4kor/owl-blogs/test/mocks"
|
2022-08-20 20:35:51 +00:00
|
|
|
"os"
|
2022-08-01 17:50:29 +00:00
|
|
|
"path"
|
2022-09-09 19:14:49 +00:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
2022-08-01 17:50:29 +00:00
|
|
|
"testing"
|
2022-09-01 19:53:06 +00:00
|
|
|
"time"
|
2022-08-01 17:50:29 +00:00
|
|
|
)
|
2022-07-21 17:44:07 +00:00
|
|
|
|
|
|
|
func TestCanGetPostTitle(t *testing.T) {
|
|
|
|
user := getTestUser()
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-07-21 17:44:07 +00:00
|
|
|
result := post.Title()
|
2022-11-02 21:12:58 +00:00
|
|
|
assertions.AssertEqual(t, result, "testpost")
|
2022-07-21 17:44:07 +00:00
|
|
|
}
|
2022-08-01 17:50:29 +00:00
|
|
|
|
|
|
|
func TestMediaDir(t *testing.T) {
|
|
|
|
user := getTestUser()
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-08-01 17:50:29 +00:00
|
|
|
result := post.MediaDir()
|
2022-11-02 21:12:58 +00:00
|
|
|
assertions.AssertEqual(t, result, path.Join(post.Dir(), "media"))
|
2022-08-01 17:50:29 +00:00
|
|
|
}
|
2022-08-03 17:41:13 +00:00
|
|
|
|
|
|
|
func TestPostUrlPath(t *testing.T) {
|
|
|
|
user := getTestUser()
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-08-03 17:41:13 +00:00
|
|
|
expected := "/user/" + user.Name() + "/posts/" + post.Id() + "/"
|
2022-11-02 21:12:58 +00:00
|
|
|
assertions.AssertEqual(t, post.UrlPath(), expected)
|
2022-08-03 17:41:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 16:47:27 +00:00
|
|
|
func TestPostFullUrl(t *testing.T) {
|
|
|
|
user := getTestUser()
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-08-13 16:47:27 +00:00
|
|
|
expected := "http://localhost:8080/user/" + user.Name() + "/posts/" + post.Id() + "/"
|
2022-11-02 21:12:58 +00:00
|
|
|
assertions.AssertEqual(t, post.FullUrl(), expected)
|
2022-08-13 16:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 17:41:13 +00:00
|
|
|
func TestPostUrlMediaPath(t *testing.T) {
|
|
|
|
user := getTestUser()
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-08-03 17:41:13 +00:00
|
|
|
expected := "/user/" + user.Name() + "/posts/" + post.Id() + "/media/data.png"
|
2022-11-02 21:12:58 +00:00
|
|
|
assertions.AssertEqual(t, post.UrlMediaPath("data.png"), expected)
|
2022-08-03 17:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostUrlMediaPathWithSubDir(t *testing.T) {
|
|
|
|
user := getTestUser()
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-08-03 17:41:13 +00:00
|
|
|
expected := "/user/" + user.Name() + "/posts/" + post.Id() + "/media/foo/data.png"
|
2022-11-02 21:12:58 +00:00
|
|
|
assertions.AssertEqual(t, post.UrlMediaPath("foo/data.png"), expected)
|
2022-08-03 17:41:13 +00:00
|
|
|
}
|
2022-08-20 20:35:51 +00:00
|
|
|
|
|
|
|
func TestDraftInMetaData(t *testing.T) {
|
|
|
|
user := getTestUser()
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-08-20 20:35:51 +00:00
|
|
|
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)
|
2022-08-22 19:15:36 +00:00
|
|
|
meta := post.Meta()
|
2022-11-02 21:12:58 +00:00
|
|
|
assertions.AssertEqual(t, meta.Draft, true)
|
2022-08-20 20:35:51 +00:00
|
|
|
}
|
2022-08-21 09:31:48 +00:00
|
|
|
|
|
|
|
func TestNoRawHTMLIfDisallowedByRepo(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-08-21 09:31:48 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-08-21 09:31:48 +00:00
|
|
|
content := "---\n"
|
|
|
|
content += "title: test\n"
|
|
|
|
content += "draft: true\n"
|
|
|
|
content += "---\n"
|
|
|
|
content += "\n"
|
|
|
|
content += "<script>alert('foo')</script>\n"
|
|
|
|
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
2022-08-22 19:15:36 +00:00
|
|
|
html := post.RenderedContent()
|
2022-12-01 18:37:13 +00:00
|
|
|
assertions.AssertNotContains(t, html, "<script>")
|
2022-08-21 09:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRawHTMLIfAllowedByRepo(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{AllowRawHtml: true})
|
2022-08-21 09:31:48 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-08-21 09:31:48 +00:00
|
|
|
content := "---\n"
|
|
|
|
content += "title: test\n"
|
|
|
|
content += "draft: true\n"
|
|
|
|
content += "---\n"
|
|
|
|
content += "\n"
|
|
|
|
content += "<script>alert('foo')</script>\n"
|
|
|
|
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
2022-08-22 19:15:36 +00:00
|
|
|
html := post.RenderedContent()
|
2022-12-01 18:37:13 +00:00
|
|
|
assertions.AssertContains(t, html, "<script>")
|
2022-08-21 09:31:48 +00:00
|
|
|
}
|
2022-08-22 19:15:36 +00:00
|
|
|
|
|
|
|
func TestLoadMeta(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{AllowRawHtml: true})
|
2022-08-22 19:15:36 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-08-22 19:15:36 +00:00
|
|
|
|
|
|
|
content := "---\n"
|
|
|
|
content += "title: test\n"
|
|
|
|
content += "draft: true\n"
|
|
|
|
content += "date: Wed, 17 Aug 2022 10:50:02 +0000\n"
|
|
|
|
content += "aliases:\n"
|
|
|
|
content += " - foo/bar/\n"
|
|
|
|
content += "---\n"
|
|
|
|
content += "\n"
|
|
|
|
content += "<script>alert('foo')</script>\n"
|
|
|
|
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
|
|
|
|
|
|
|
err := post.LoadMeta()
|
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error loading meta")
|
2022-08-22 19:15:36 +00:00
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertEqual(t, post.Meta().Title, "test")
|
|
|
|
assertions.AssertLen(t, post.Meta().Aliases, 1)
|
|
|
|
assertions.AssertEqual(t, post.Meta().Draft, true)
|
|
|
|
assertions.AssertEqual(t, post.Meta().Date.Format(time.RFC1123Z), "Wed, 17 Aug 2022 10:50:02 +0000")
|
|
|
|
assertions.AssertEqual(t, post.Meta().Draft, true)
|
2022-08-22 19:15:36 +00:00
|
|
|
}
|
2022-08-24 20:11:39 +00:00
|
|
|
|
2022-08-27 21:01:07 +00:00
|
|
|
///
|
|
|
|
/// Webmention
|
|
|
|
///
|
|
|
|
|
2022-09-07 20:06:59 +00:00
|
|
|
func TestPersistIncomingWebmention(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-09-01 19:34:33 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
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 19:34:33 +00:00
|
|
|
Source: "http://example.com/source",
|
|
|
|
}
|
2022-09-07 20:06:59 +00:00
|
|
|
err := post.PersistIncomingWebmention(webmention)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error persisting webmention")
|
2022-09-07 20:06:59 +00:00
|
|
|
mentions := post.IncomingWebmentions()
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, mentions, 1)
|
|
|
|
assertions.AssertEqual(t, mentions[0].Source, webmention.Source)
|
2022-09-01 19:34:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:06:59 +00:00
|
|
|
func TestAddIncomingWebmentionCreatesFile(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-11-06 13:17:14 +00:00
|
|
|
repo.HttpClient = &mocks.MockHttpClient{}
|
|
|
|
repo.Parser = &mocks.MockHtmlParser{}
|
2022-08-24 20:11:39 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-08-24 20:11:39 +00:00
|
|
|
|
2022-09-07 20:06:59 +00:00
|
|
|
err := post.AddIncomingWebmention("https://example.com")
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error adding webmention")
|
2022-08-24 20:11:39 +00:00
|
|
|
|
2022-09-07 20:06:59 +00:00
|
|
|
mentions := post.IncomingWebmentions()
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, mentions, 1)
|
2022-08-24 20:11:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:06:59 +00:00
|
|
|
func TestAddIncomingWebmentionNotOverwritingWebmention(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-11-06 13:17:14 +00:00
|
|
|
repo.HttpClient = &mocks.MockHttpClient{}
|
|
|
|
repo.Parser = &mocks.MockHtmlParser{}
|
2022-08-24 20:11:39 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-08-24 20:11:39 +00:00
|
|
|
|
2022-09-07 20:06:59 +00:00
|
|
|
post.PersistIncomingWebmention(owl.WebmentionIn{
|
|
|
|
Source: "https://example.com",
|
|
|
|
ApprovalStatus: "approved",
|
|
|
|
})
|
2022-08-24 20:11:39 +00:00
|
|
|
|
2022-09-07 20:06:59 +00:00
|
|
|
post.AddIncomingWebmention("https://example.com")
|
2022-08-24 20:11:39 +00:00
|
|
|
|
2022-09-07 20:06:59 +00:00
|
|
|
mentions := post.IncomingWebmentions()
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, mentions, 1)
|
2022-08-24 20:11:39 +00:00
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertEqual(t, mentions[0].ApprovalStatus, "approved")
|
2022-08-24 20:11:39 +00:00
|
|
|
}
|
2022-08-31 18:01:58 +00:00
|
|
|
|
2022-09-09 19:14:49 +00:00
|
|
|
func TestEnrichAddsTitle(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-11-06 13:17:14 +00:00
|
|
|
repo.HttpClient = &mocks.MockHttpClient{}
|
|
|
|
repo.Parser = &mocks.MockHtmlParser{}
|
2022-08-31 18:01:58 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-08-31 18:01:58 +00:00
|
|
|
|
2022-09-07 20:06:59 +00:00
|
|
|
post.AddIncomingWebmention("https://example.com")
|
2022-09-10 12:04:13 +00:00
|
|
|
post.EnrichWebmention(owl.WebmentionIn{Source: "https://example.com"})
|
2022-08-31 18:01:58 +00:00
|
|
|
|
2022-09-07 20:06:59 +00:00
|
|
|
mentions := post.IncomingWebmentions()
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, mentions, 1)
|
|
|
|
assertions.AssertEqual(t, mentions[0].Title, "Mock Title")
|
2022-08-31 18:01:58 +00:00
|
|
|
}
|
2022-09-01 19:53:06 +00:00
|
|
|
|
2022-09-10 11:44:25 +00:00
|
|
|
func TestApprovedIncomingWebmentions(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-09-01 19:53:06 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
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 19:53:06 +00:00
|
|
|
Source: "http://example.com/source",
|
|
|
|
ApprovalStatus: "approved",
|
|
|
|
RetrievedAt: time.Now(),
|
|
|
|
}
|
2022-09-07 20:06:59 +00:00
|
|
|
post.PersistIncomingWebmention(webmention)
|
2022-09-04 13:03:16 +00:00
|
|
|
webmention = owl.WebmentionIn{
|
2022-09-01 19:53:06 +00:00
|
|
|
Source: "http://example.com/source2",
|
|
|
|
ApprovalStatus: "",
|
|
|
|
RetrievedAt: time.Now().Add(time.Hour * -1),
|
|
|
|
}
|
2022-09-07 20:06:59 +00:00
|
|
|
post.PersistIncomingWebmention(webmention)
|
2022-09-04 13:03:16 +00:00
|
|
|
webmention = owl.WebmentionIn{
|
2022-09-01 19:53:06 +00:00
|
|
|
Source: "http://example.com/source3",
|
|
|
|
ApprovalStatus: "approved",
|
|
|
|
RetrievedAt: time.Now().Add(time.Hour * -2),
|
|
|
|
}
|
2022-09-07 20:06:59 +00:00
|
|
|
post.PersistIncomingWebmention(webmention)
|
2022-09-04 13:03:16 +00:00
|
|
|
webmention = owl.WebmentionIn{
|
2022-09-01 19:53:06 +00:00
|
|
|
Source: "http://example.com/source4",
|
|
|
|
ApprovalStatus: "rejected",
|
|
|
|
RetrievedAt: time.Now().Add(time.Hour * -3),
|
|
|
|
}
|
2022-09-07 20:06:59 +00:00
|
|
|
post.PersistIncomingWebmention(webmention)
|
2022-09-01 19:53:06 +00:00
|
|
|
|
2022-09-10 11:44:25 +00:00
|
|
|
webmentions := post.ApprovedIncomingWebmentions()
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, webmentions, 2)
|
2022-09-01 19:53:06 +00:00
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertEqual(t, webmentions[0].Source, "http://example.com/source")
|
|
|
|
assertions.AssertEqual(t, webmentions[1].Source, "http://example.com/source3")
|
2022-09-01 19:53:06 +00:00
|
|
|
|
|
|
|
}
|
2022-09-04 13:32:37 +00:00
|
|
|
|
|
|
|
func TestScanningForLinks(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-09-04 13:32:37 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-09-04 13:32:37 +00:00
|
|
|
|
|
|
|
content := "---\n"
|
|
|
|
content += "title: test\n"
|
|
|
|
content += "date: Wed, 17 Aug 2022 10:50:02 +0000\n"
|
|
|
|
content += "---\n"
|
|
|
|
content += "\n"
|
|
|
|
content += "[Hello](https://example.com/hello)\n"
|
|
|
|
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
|
|
|
|
|
|
|
post.ScanForLinks()
|
|
|
|
webmentions := post.OutgoingWebmentions()
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, webmentions, 1)
|
|
|
|
assertions.AssertEqual(t, webmentions[0].Target, "https://example.com/hello")
|
2022-09-04 13:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestScanningForLinksDoesNotAddDuplicates(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-09-04 13:32:37 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-09-04 13:32:37 +00:00
|
|
|
|
|
|
|
content := "---\n"
|
|
|
|
content += "title: test\n"
|
|
|
|
content += "date: Wed, 17 Aug 2022 10:50:02 +0000\n"
|
|
|
|
content += "---\n"
|
|
|
|
content += "\n"
|
|
|
|
content += "[Hello](https://example.com/hello)\n"
|
|
|
|
content += "[Hello](https://example.com/hello)\n"
|
|
|
|
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
|
|
|
|
|
|
|
post.ScanForLinks()
|
|
|
|
post.ScanForLinks()
|
|
|
|
post.ScanForLinks()
|
|
|
|
webmentions := post.OutgoingWebmentions()
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, webmentions, 1)
|
|
|
|
assertions.AssertEqual(t, webmentions[0].Target, "https://example.com/hello")
|
2022-09-04 13:32:37 +00:00
|
|
|
}
|
2022-09-04 15:10:40 +00:00
|
|
|
|
2022-10-10 19:06:33 +00:00
|
|
|
func TestScanningForLinksDoesAddReplyUrl(t *testing.T) {
|
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-10-10 19:06:33 +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/reply\n"
|
|
|
|
content += "---\n"
|
|
|
|
content += "\n"
|
|
|
|
content += "Hi\n"
|
|
|
|
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
|
|
|
|
|
|
|
post.ScanForLinks()
|
|
|
|
webmentions := post.OutgoingWebmentions()
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, webmentions, 1)
|
|
|
|
assertions.AssertEqual(t, webmentions[0].Target, "https://example.com/reply")
|
2022-10-10 19:06:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 15:10:40 +00:00
|
|
|
func TestCanSendWebmention(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-11-06 13:17:14 +00:00
|
|
|
repo.HttpClient = &mocks.MockHttpClient{}
|
|
|
|
repo.Parser = &mocks.MockHtmlParser{}
|
2022-09-04 15:10:40 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-09-04 15:10:40 +00:00
|
|
|
|
|
|
|
webmention := owl.WebmentionOut{
|
|
|
|
Target: "http://example.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := post.SendWebmention(webmention)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error sending webmention")
|
2022-09-04 15:10:40 +00:00
|
|
|
|
|
|
|
webmentions := post.OutgoingWebmentions()
|
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, webmentions, 1)
|
|
|
|
assertions.AssertEqual(t, webmentions[0].Target, "http://example.com")
|
2022-11-03 18:23:13 +00:00
|
|
|
assertions.AssertEqual(t, webmentions[0].LastSentAt.IsZero(), false)
|
2022-09-04 15:10:40 +00:00
|
|
|
}
|
2022-09-09 19:14:49 +00:00
|
|
|
|
2022-09-10 12:16:22 +00:00
|
|
|
func TestSendWebmentionOnlyScansOncePerWeek(t *testing.T) {
|
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-11-06 13:17:14 +00:00
|
|
|
repo.HttpClient = &mocks.MockHttpClient{}
|
|
|
|
repo.Parser = &mocks.MockHtmlParser{}
|
2022-09-10 12:16:22 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-09-10 12:16:22 +00:00
|
|
|
|
|
|
|
webmention := owl.WebmentionOut{
|
|
|
|
Target: "http://example.com",
|
|
|
|
ScannedAt: time.Now().Add(time.Hour * -24 * 6),
|
|
|
|
}
|
|
|
|
|
|
|
|
post.PersistOutgoingWebmention(&webmention)
|
|
|
|
webmentions := post.OutgoingWebmentions()
|
|
|
|
webmention = webmentions[0]
|
|
|
|
|
|
|
|
err := post.SendWebmention(webmention)
|
2022-11-03 18:23:13 +00:00
|
|
|
assertions.AssertError(t, err, "Expected error, got nil")
|
2022-09-10 12:16:22 +00:00
|
|
|
|
|
|
|
webmentions = post.OutgoingWebmentions()
|
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, webmentions, 1)
|
|
|
|
assertions.AssertEqual(t, webmentions[0].ScannedAt, webmention.ScannedAt)
|
2022-09-10 12:16:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 19:14:49 +00:00
|
|
|
func TestSendingMultipleWebmentions(t *testing.T) {
|
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-11-06 13:17:14 +00:00
|
|
|
repo.HttpClient = &mocks.MockHttpClient{}
|
|
|
|
repo.Parser = &mocks.MockHtmlParser{}
|
2022-09-09 19:14:49 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-09-09 19:14:49 +00:00
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(20)
|
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
go func(k int) {
|
|
|
|
webmention := owl.WebmentionOut{
|
|
|
|
Target: "http://example.com" + strconv.Itoa(k),
|
|
|
|
}
|
|
|
|
post.SendWebmention(webmention)
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
webmentions := post.OutgoingWebmentions()
|
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, webmentions, 20)
|
2022-09-09 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReceivingMultipleWebmentions(t *testing.T) {
|
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-11-06 13:17:14 +00:00
|
|
|
repo.HttpClient = &mocks.MockHttpClient{}
|
|
|
|
repo.Parser = &mocks.MockHtmlParser{}
|
2022-09-09 19:14:49 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-09-09 19:14:49 +00:00
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(20)
|
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
go func(k int) {
|
|
|
|
post.AddIncomingWebmention("http://example.com" + strconv.Itoa(k))
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
webmentions := post.IncomingWebmentions()
|
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, webmentions, 20)
|
2022-09-09 19:14:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendingAndReceivingMultipleWebmentions(t *testing.T) {
|
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-11-06 13:17:14 +00:00
|
|
|
repo.HttpClient = &mocks.MockHttpClient{}
|
|
|
|
repo.Parser = &mocks.MockHtmlParser{}
|
2022-09-09 19:14:49 +00:00
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-09-09 19:14:49 +00:00
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(40)
|
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
go func(k int) {
|
|
|
|
post.AddIncomingWebmention("http://example.com" + strconv.Itoa(k))
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
2022-09-10 11:44:25 +00:00
|
|
|
go func(k int) {
|
|
|
|
webmention := owl.WebmentionOut{
|
|
|
|
Target: "http://example.com" + strconv.Itoa(k),
|
|
|
|
}
|
|
|
|
post.SendWebmention(webmention)
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
ins := post.IncomingWebmentions()
|
|
|
|
outs := post.OutgoingWebmentions()
|
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, ins, 20)
|
|
|
|
assertions.AssertLen(t, outs, 20)
|
2022-09-10 11:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestComplexParallelWebmentions(t *testing.T) {
|
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-11-06 13:17:14 +00:00
|
|
|
repo.HttpClient = &mocks.MockHttpClient{}
|
|
|
|
repo.Parser = &mocks.MockParseLinksHtmlParser{
|
2022-09-10 11:44:25 +00:00
|
|
|
Links: []string{
|
|
|
|
"http://example.com/1",
|
|
|
|
"http://example.com/2",
|
|
|
|
"http://example.com/3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("testpost", false)
|
2022-09-10 11:44:25 +00:00
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(60)
|
|
|
|
|
2022-09-09 19:14:49 +00:00
|
|
|
for i := 0; i < 20; i++ {
|
2022-09-10 11:44:25 +00:00
|
|
|
go func(k int) {
|
|
|
|
post.AddIncomingWebmention("http://example.com/" + strconv.Itoa(k))
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
2022-09-09 19:14:49 +00:00
|
|
|
go func(k int) {
|
|
|
|
webmention := owl.WebmentionOut{
|
2022-09-10 11:44:25 +00:00
|
|
|
Target: "http://example.com/" + strconv.Itoa(k),
|
2022-09-09 19:14:49 +00:00
|
|
|
}
|
|
|
|
post.SendWebmention(webmention)
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
2022-09-10 11:44:25 +00:00
|
|
|
go func() {
|
|
|
|
post.ScanForLinks()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2022-09-09 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
ins := post.IncomingWebmentions()
|
|
|
|
outs := post.OutgoingWebmentions()
|
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertLen(t, ins, 20)
|
|
|
|
assertions.AssertLen(t, outs, 20)
|
2022-09-09 19:14:49 +00:00
|
|
|
}
|
2022-09-11 19:22:59 +00:00
|
|
|
|
2022-12-05 17:46:00 +00:00
|
|
|
func TestPostWithoutContent(t *testing.T) {
|
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
|
|
|
user, _ := repo.CreateUser("testuser")
|
|
|
|
post, _ := user.CreateNewPostFull(owl.PostMeta{}, "")
|
|
|
|
|
|
|
|
result := post.RenderedContent()
|
|
|
|
assertions.AssertEqual(t, result, "")
|
|
|
|
}
|
|
|
|
|
2022-10-07 14:49:26 +00:00
|
|
|
// func TestComplexParallelSimulatedProcessesWebmentions(t *testing.T) {
|
|
|
|
// repoName := testRepoName()
|
|
|
|
// repo, _ := owl.CreateRepository(repoName, owl.RepoConfig{})
|
2022-11-06 13:17:14 +00:00
|
|
|
// repo.HttpClient = &mocks.MockHttpClient{}
|
2022-10-07 14:49:26 +00:00
|
|
|
// repo.Parser = &MockParseLinksHtmlParser{
|
|
|
|
// Links: []string{
|
|
|
|
// "http://example.com/1",
|
|
|
|
// "http://example.com/2",
|
|
|
|
// "http://example.com/3",
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
// user, _ := repo.CreateUser("testuser")
|
2022-10-13 18:33:00 +00:00
|
|
|
// post, _ := user.CreateNewPost("testpost", false)
|
2022-10-07 14:49:26 +00:00
|
|
|
|
|
|
|
// wg := sync.WaitGroup{}
|
|
|
|
// wg.Add(40)
|
|
|
|
|
|
|
|
// for i := 0; i < 20; i++ {
|
|
|
|
// go func(k int) {
|
|
|
|
// defer wg.Done()
|
|
|
|
// fRepo, _ := owl.OpenRepository(repoName)
|
|
|
|
// fUser, _ := fRepo.GetUser("testuser")
|
|
|
|
// fPost, err := fUser.GetPost(post.Id())
|
|
|
|
// if err != nil {
|
|
|
|
// t.Error(err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// fPost.AddIncomingWebmention("http://example.com/" + strconv.Itoa(k))
|
|
|
|
// }(i)
|
|
|
|
// go func(k int) {
|
|
|
|
// defer wg.Done()
|
|
|
|
// fRepo, _ := owl.OpenRepository(repoName)
|
|
|
|
// fUser, _ := fRepo.GetUser("testuser")
|
|
|
|
// fPost, err := fUser.GetPost(post.Id())
|
|
|
|
// if err != nil {
|
|
|
|
// t.Error(err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// webmention := owl.WebmentionOut{
|
|
|
|
// Target: "http://example.com/" + strconv.Itoa(k),
|
|
|
|
// }
|
|
|
|
// fPost.SendWebmention(webmention)
|
|
|
|
// }(i)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// wg.Wait()
|
|
|
|
|
|
|
|
// ins := post.IncomingWebmentions()
|
|
|
|
|
|
|
|
// if len(ins) != 20 {
|
|
|
|
// t.Errorf("Expected 20 webmentions, got %d", len(ins))
|
|
|
|
// }
|
|
|
|
|
|
|
|
// outs := post.OutgoingWebmentions()
|
|
|
|
|
|
|
|
// if len(outs) != 20 {
|
|
|
|
// t.Errorf("Expected 20 webmentions, got %d", len(outs))
|
|
|
|
// }
|
|
|
|
// }
|