WIP clean up tests. Moving checks into assertions functions

This commit is contained in:
Niko Abeler 2022-11-02 22:02:49 +01:00
parent 73def1b477
commit c92ab958a6
13 changed files with 248 additions and 625 deletions

View File

@ -3,6 +3,7 @@ package web_test
import (
"h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/priv/assertions"
"net/http"
"net/http/httptest"
"os"
@ -25,26 +26,14 @@ func TestRedirectOnAliases(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", "/foo/bar", nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
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.StatusMovedPermanently {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusMovedPermanently)
}
assertions.AssertStatus(t, rr, http.StatusMovedPermanently)
// Check that Location header is set correctly
if rr.Header().Get("Location") != post.UrlPath() {
t.Errorf("Location header is not set correctly, expected: %v Got: %v",
post.UrlPath(),
rr.Header().Get("Location"),
)
}
assertions.AssertEqual(t, rr.Header().Get("Location"), post.UrlPath())
}
func TestNoRedirectOnNonExistingAliases(t *testing.T) {
@ -63,18 +52,12 @@ func TestNoRedirectOnNonExistingAliases(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", "/foo/bar2", nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
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)
}
assertions.AssertStatus(t, rr, http.StatusNotFound)
}
@ -94,18 +77,12 @@ func TestNoRedirectIfValidPostUrl(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", post2.UrlPath(), nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
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.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
assertions.AssertStatus(t, rr, http.StatusOK)
}
@ -124,18 +101,12 @@ func TestRedirectIfInvalidPostUrl(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", user.UrlPath()+"posts/not-a-real-post/", nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
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.StatusMovedPermanently {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusMovedPermanently)
}
assertions.AssertStatus(t, rr, http.StatusMovedPermanently)
}
@ -154,18 +125,12 @@ func TestRedirectIfInvalidUserUrl(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", "/user/not-real/", nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
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.StatusMovedPermanently {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusMovedPermanently)
}
assertions.AssertStatus(t, rr, http.StatusMovedPermanently)
}
@ -184,18 +149,12 @@ func TestRedirectIfInvalidMediaUrl(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", post.UrlMediaPath("not-real"), nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
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.StatusMovedPermanently {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusMovedPermanently)
}
assertions.AssertStatus(t, rr, http.StatusMovedPermanently)
}
@ -222,17 +181,11 @@ func TestDeepAliasInSingleUserMode(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", "/2016/09/13/create-tileable-textures-with-gimp/", nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusMovedPermanently {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusMovedPermanently)
}
assertions.AssertStatus(t, rr, http.StatusMovedPermanently)
}

View File

@ -3,12 +3,12 @@ package web_test
import (
"h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/priv/assertions"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"path"
"strings"
"testing"
"time"
)
@ -39,28 +39,16 @@ func TestMultiUserRepoIndexHandler(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
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.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
assertions.AssertStatus(t, rr, http.StatusOK)
// Check the response body contains names of users
if !strings.Contains(rr.Body.String(), "user_1") {
t.Error("user_1 not listed on index page. Got: ")
t.Error(rr.Body.String())
}
if !strings.Contains(rr.Body.String(), "user_2") {
t.Error("user_2 not listed on index page. Got: ")
t.Error(rr.Body.String())
}
assertions.AssertContains(t, rr.Body.String(), "user_1")
assertions.AssertContains(t, rr.Body.String(), "user_2")
}
func TestMultiUserUserIndexHandler(t *testing.T) {
@ -70,24 +58,15 @@ func TestMultiUserUserIndexHandler(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", user.UrlPath(), nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
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.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
assertions.AssertStatus(t, rr, http.StatusOK)
// Check the response body contains names of users
if !strings.Contains(rr.Body.String(), "post-1") {
t.Error("post-1 not listed on index page. Got: ")
t.Error(rr.Body.String())
}
assertions.AssertContains(t, rr.Body.String(), "post-1")
}
func TestMultiUserPostHandler(t *testing.T) {
@ -97,18 +76,12 @@ func TestMultiUserPostHandler(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", post.UrlPath(), nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
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.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
assertions.AssertStatus(t, rr, http.StatusOK)
}
func TestMultiUserPostMediaHandler(t *testing.T) {
@ -119,24 +92,16 @@ func TestMultiUserPostMediaHandler(t *testing.T) {
// Create test media file
path := path.Join(post.MediaDir(), "data.txt")
err := os.WriteFile(path, []byte("test"), 0644)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
// Create Request and Response
req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
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.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
assertions.AssertStatus(t, rr, http.StatusOK)
// Check the response body contains data of media file
if !(rr.Body.String() == "test") {

View File

@ -3,6 +3,7 @@ package web_test
import (
"h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/priv/assertions"
"net/http"
"net/http/httptest"
"os"
@ -24,16 +25,10 @@ func TestPostHandlerReturns404OnDrafts(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", post.UrlPath(), nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
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)
}
assertions.AssertStatus(t, rr, http.StatusNotFound)
}

View File

@ -3,9 +3,9 @@ package web_test
import (
"h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/priv/assertions"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
@ -16,28 +16,16 @@ func TestMultiUserUserRssIndexHandler(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", user.UrlPath()+"index.xml", nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
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.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
assertions.AssertStatus(t, rr, http.StatusOK)
// Check the response Content-Type is what we expect.
if !strings.Contains(rr.Header().Get("Content-Type"), "application/rss+xml") {
t.Errorf("handler returned wrong Content-Type: got %v want %v",
rr.Header().Get("Content-Type"), "application/rss+xml")
}
assertions.AssertContains(t, rr.Header().Get("Content-Type"), "application/rss+xml")
// Check the response body contains names of users
if !strings.Contains(rr.Body.String(), "post-1") {
t.Error("post-1 not listed on index page. Got: ")
t.Error(rr.Body.String())
}
assertions.AssertContains(t, rr.Body.String(), "post-1")
}

View File

@ -3,6 +3,7 @@ package web_test
import (
owl "h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/priv/assertions"
"net/http"
"net/http/httptest"
"os"
@ -23,24 +24,15 @@ func TestSingleUserUserIndexHandler(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", user.UrlPath(), nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
assertions.AssertStatus(t, rr, http.StatusOK)
// Check the response body contains names of users
if !strings.Contains(rr.Body.String(), "post-1") {
t.Error("post-1 not listed on index page. Got: ")
t.Error(rr.Body.String())
}
assertions.AssertContains(t, rr.Body.String(), "post-1")
}
func TestSingleUserPostHandler(t *testing.T) {
@ -49,18 +41,12 @@ func TestSingleUserPostHandler(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", post.UrlPath(), nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
assertions.AssertStatus(t, rr, http.StatusOK)
}
func TestSingleUserPostMediaHandler(t *testing.T) {
@ -70,24 +56,16 @@ func TestSingleUserPostMediaHandler(t *testing.T) {
// Create test media file
path := path.Join(post.MediaDir(), "data.txt")
err := os.WriteFile(path, []byte("test"), 0644)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
// Create Request and Response
req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
assertions.AssertStatus(t, rr, http.StatusOK)
// Check the response body contains data of media file
if !(rr.Body.String() == "test") {
@ -117,9 +95,7 @@ func TestHasNoDraftsInList(t *testing.T) {
// Create Request and Response
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error creating request")
rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req)

View File

@ -3,6 +3,7 @@ package web_test
import (
"h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/priv/assertions"
"net/http"
"net/http/httptest"
"net/url"
@ -34,14 +35,6 @@ func setupWebmentionTest(repo owl.Repository, user owl.User, target string, sour
return rr, nil
}
func assertStatus(t *testing.T, rr *httptest.ResponseRecorder, expStatus int) {
if status := rr.Code; status != expStatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, expStatus)
return
}
}
func TestWebmentionHandleAccepts(t *testing.T) {
repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1")
@ -51,11 +44,9 @@ func TestWebmentionHandleAccepts(t *testing.T) {
source := "https://example.com"
rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error setting up webmention test")
assertStatus(t, rr, http.StatusAccepted)
assertions.AssertStatus(t, rr, http.StatusAccepted)
}
@ -69,16 +60,10 @@ func TestWebmentionWrittenToPost(t *testing.T) {
source := "https://example.com"
rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error setting up webmention test")
// Check the status code is what we expect.
assertStatus(t, rr, http.StatusAccepted)
if len(post.IncomingWebmentions()) != 1 {
t.Errorf("no webmention written to post")
}
assertions.AssertStatus(t, rr, http.StatusAccepted)
assertions.AssertLen(t, post.IncomingWebmentions(), 1)
}
//
@ -98,11 +83,9 @@ func TestWebmentionSourceValidation(t *testing.T) {
source := "ftp://example.com"
rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error setting up webmention test")
assertStatus(t, rr, http.StatusBadRequest)
assertions.AssertStatus(t, rr, http.StatusBadRequest)
}
func TestWebmentionTargetValidation(t *testing.T) {
@ -115,11 +98,9 @@ func TestWebmentionTargetValidation(t *testing.T) {
source := post.FullUrl()
rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error setting up webmention test")
assertStatus(t, rr, http.StatusBadRequest)
assertions.AssertStatus(t, rr, http.StatusBadRequest)
}
// The receiver MUST reject the request if the source URL is the same as the target URL.
@ -134,11 +115,9 @@ func TestWebmentionSameTargetAndSource(t *testing.T) {
source := post.FullUrl()
rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error setting up webmention test")
assertStatus(t, rr, http.StatusBadRequest)
assertions.AssertStatus(t, rr, http.StatusBadRequest)
}
// The receiver SHOULD check that target is a valid resource for which it can accept Webmentions.
@ -154,11 +133,9 @@ func TestValidationOfTarget(t *testing.T) {
source := post.FullUrl()
rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error setting up webmention test")
assertStatus(t, rr, http.StatusBadRequest)
assertions.AssertStatus(t, rr, http.StatusBadRequest)
}
func TestAcceptWebmentionForAlias(t *testing.T) {
@ -179,9 +156,7 @@ func TestAcceptWebmentionForAlias(t *testing.T) {
source := "https://example.com"
rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil {
t.Fatal(err)
}
assertions.AssertNoError(t, err, "Error setting up webmention test")
assertStatus(t, rr, http.StatusAccepted)
assertions.AssertStatus(t, rr, http.StatusAccepted)
}

View File

@ -2,6 +2,7 @@ package owl_test
import (
"h4kor/owl-blogs"
"h4kor/owl-blogs/priv/assertions"
"os"
"path"
"strconv"
@ -121,9 +122,7 @@ func TestRawHTMLIfAllowedByRepo(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644)
html := post.RenderedContent()
html_str := html.String()
if !strings.Contains(html_str, "<script>") {
t.Error("HTML should be allowed")
}
assertions.AssertContains(t, html_str, "<script>")
}
func TestLoadMeta(t *testing.T) {
@ -144,25 +143,13 @@ func TestLoadMeta(t *testing.T) {
err := post.LoadMeta()
if err != nil {
t.Errorf("Got Error: %v", err)
}
assertions.AssertNoError(t, err, "Error loading meta")
if post.Meta().Title != "test" {
t.Errorf("Expected title: %s, got %s", "test", post.Meta().Title)
}
if len(post.Meta().Aliases) != 1 || post.Meta().Aliases[0] != "foo/bar/" {
t.Errorf("Expected title: %v, got %v", []string{"foo/bar/"}, post.Meta().Aliases)
}
if post.Meta().Date.Format(time.RFC1123Z) != "Wed, 17 Aug 2022 10:50:02 +0000" {
t.Errorf("Expected title: %s, got %s", "Wed, 17 Aug 2022 10:50:02 +0000", post.Meta().Title)
}
if post.Meta().Draft != true {
t.Errorf("Expected title: %v, got %v", true, post.Meta().Draft)
}
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)
}
///
@ -177,17 +164,10 @@ func TestPersistIncomingWebmention(t *testing.T) {
Source: "http://example.com/source",
}
err := post.PersistIncomingWebmention(webmention)
if err != nil {
t.Errorf("Got error: %v", err)
}
assertions.AssertNoError(t, err, "Error persisting webmention")
mentions := post.IncomingWebmentions()
if len(mentions) != 1 {
t.Errorf("Expected 1 webmention, got %d", len(mentions))
}
if mentions[0].Source != webmention.Source {
t.Errorf("Expected source: %s, got %s", webmention.Source, mentions[0].Source)
}
assertions.AssertLen(t, mentions, 1)
assertions.AssertEqual(t, mentions[0].Source, webmention.Source)
}
func TestAddIncomingWebmentionCreatesFile(t *testing.T) {
@ -198,14 +178,10 @@ func TestAddIncomingWebmentionCreatesFile(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false)
err := post.AddIncomingWebmention("https://example.com")
if err != nil {
t.Errorf("Got Error: %v", err)
}
assertions.AssertNoError(t, err, "Error adding webmention")
mentions := post.IncomingWebmentions()
if len(mentions) != 1 {
t.Errorf("Expected 1 webmention, got %d", len(mentions))
}
assertions.AssertLen(t, mentions, 1)
}
func TestAddIncomingWebmentionNotOverwritingWebmention(t *testing.T) {
@ -223,13 +199,9 @@ func TestAddIncomingWebmentionNotOverwritingWebmention(t *testing.T) {
post.AddIncomingWebmention("https://example.com")
mentions := post.IncomingWebmentions()
if len(mentions) != 1 {
t.Errorf("Expected 1 webmention, got %d", len(mentions))
}
assertions.AssertLen(t, mentions, 1)
if mentions[0].ApprovalStatus != "approved" {
t.Errorf("Expected approval status: %s, got %s", "approved", mentions[0].ApprovalStatus)
}
assertions.AssertEqual(t, mentions[0].ApprovalStatus, "approved")
}
func TestEnrichAddsTitle(t *testing.T) {
@ -243,13 +215,8 @@ func TestEnrichAddsTitle(t *testing.T) {
post.EnrichWebmention(owl.WebmentionIn{Source: "https://example.com"})
mentions := post.IncomingWebmentions()
if len(mentions) != 1 {
t.Errorf("Expected 1 webmention, got %d", len(mentions))
}
if mentions[0].Title != "Mock Title" {
t.Errorf("Expected title: %s, got %s", "Mock Title", mentions[0].Title)
}
assertions.AssertLen(t, mentions, 1)
assertions.AssertEqual(t, mentions[0].Title, "Mock Title")
}
func TestApprovedIncomingWebmentions(t *testing.T) {
@ -282,16 +249,10 @@ func TestApprovedIncomingWebmentions(t *testing.T) {
post.PersistIncomingWebmention(webmention)
webmentions := post.ApprovedIncomingWebmentions()
if len(webmentions) != 2 {
t.Errorf("Expected 2 webmentions, got %d", len(webmentions))
}
assertions.AssertLen(t, webmentions, 2)
if webmentions[0].Source != "http://example.com/source" {
t.Errorf("Expected source: %s, got %s", "http://example.com/source", webmentions[0].Source)
}
if webmentions[1].Source != "http://example.com/source3" {
t.Errorf("Expected source: %s, got %s", "http://example.com/source3", webmentions[1].Source)
}
assertions.AssertEqual(t, webmentions[0].Source, "http://example.com/source")
assertions.AssertEqual(t, webmentions[1].Source, "http://example.com/source3")
}
@ -310,12 +271,8 @@ func TestScanningForLinks(t *testing.T) {
post.ScanForLinks()
webmentions := post.OutgoingWebmentions()
if len(webmentions) != 1 {
t.Errorf("Expected 1 webmention, got %d", len(webmentions))
}
if webmentions[0].Target != "https://example.com/hello" {
t.Errorf("Expected target: %s, got %s", "https://example.com/hello", webmentions[0].Target)
}
assertions.AssertLen(t, webmentions, 1)
assertions.AssertEqual(t, webmentions[0].Target, "https://example.com/hello")
}
func TestScanningForLinksDoesNotAddDuplicates(t *testing.T) {
@ -336,12 +293,8 @@ func TestScanningForLinksDoesNotAddDuplicates(t *testing.T) {
post.ScanForLinks()
post.ScanForLinks()
webmentions := post.OutgoingWebmentions()
if len(webmentions) != 1 {
t.Errorf("Expected 1 webmention, got %d", len(webmentions))
}
if webmentions[0].Target != "https://example.com/hello" {
t.Errorf("Expected target: %s, got %s", "https://example.com/hello", webmentions[0].Target)
}
assertions.AssertLen(t, webmentions, 1)
assertions.AssertEqual(t, webmentions[0].Target, "https://example.com/hello")
}
func TestScanningForLinksDoesAddReplyUrl(t *testing.T) {
@ -361,12 +314,8 @@ func TestScanningForLinksDoesAddReplyUrl(t *testing.T) {
post.ScanForLinks()
webmentions := post.OutgoingWebmentions()
if len(webmentions) != 1 {
t.Errorf("Expected 1 webmention, got %d", len(webmentions))
}
if webmentions[0].Target != "https://example.com/reply" {
t.Errorf("Expected target: %s, got %s", "https://example.com/reply", webmentions[0].Target)
}
assertions.AssertLen(t, webmentions, 1)
assertions.AssertEqual(t, webmentions[0].Target, "https://example.com/reply")
}
func TestCanSendWebmention(t *testing.T) {
@ -381,20 +330,12 @@ func TestCanSendWebmention(t *testing.T) {
}
err := post.SendWebmention(webmention)
if err != nil {
t.Errorf("Error sending webmention: %v", err)
}
assertions.AssertNoError(t, err, "Error sending webmention")
webmentions := post.OutgoingWebmentions()
if len(webmentions) != 1 {
t.Errorf("Expected 1 webmention, got %d", len(webmentions))
}
if webmentions[0].Target != "http://example.com" {
t.Errorf("Expected target: %s, got %s", "http://example.com", webmentions[0].Target)
}
assertions.AssertLen(t, webmentions, 1)
assertions.AssertEqual(t, webmentions[0].Target, "http://example.com")
if webmentions[0].LastSentAt.IsZero() {
t.Errorf("Expected LastSentAt to be set")
}
@ -423,13 +364,8 @@ func TestSendWebmentionOnlyScansOncePerWeek(t *testing.T) {
webmentions = post.OutgoingWebmentions()
if len(webmentions) != 1 {
t.Errorf("Expected 1 webmention, got %d", len(webmentions))
}
if webmentions[0].ScannedAt != webmention.ScannedAt {
t.Errorf("Expected ScannedAt to be unchanged. Expected: %v, got %v", webmention.ScannedAt, webmentions[0].ScannedAt)
}
assertions.AssertLen(t, webmentions, 1)
assertions.AssertEqual(t, webmentions[0].ScannedAt, webmention.ScannedAt)
}
func TestSendingMultipleWebmentions(t *testing.T) {
@ -456,9 +392,7 @@ func TestSendingMultipleWebmentions(t *testing.T) {
webmentions := post.OutgoingWebmentions()
if len(webmentions) != 20 {
t.Errorf("Expected 20 webmentions, got %d", len(webmentions))
}
assertions.AssertLen(t, webmentions, 20)
}
func TestReceivingMultipleWebmentions(t *testing.T) {
@ -482,9 +416,7 @@ func TestReceivingMultipleWebmentions(t *testing.T) {
webmentions := post.IncomingWebmentions()
if len(webmentions) != 20 {
t.Errorf("Expected 20 webmentions, got %d", len(webmentions))
}
assertions.AssertLen(t, webmentions, 20)
}
@ -515,16 +447,10 @@ func TestSendingAndReceivingMultipleWebmentions(t *testing.T) {
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))
}
assertions.AssertLen(t, ins, 20)
assertions.AssertLen(t, outs, 20)
}
func TestComplexParallelWebmentions(t *testing.T) {
@ -564,16 +490,10 @@ func TestComplexParallelWebmentions(t *testing.T) {
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))
}
assertions.AssertLen(t, ins, 20)
assertions.AssertLen(t, outs, 20)
}
// func TestComplexParallelSimulatedProcessesWebmentions(t *testing.T) {

View File

@ -0,0 +1,50 @@
package assertions
import (
"net/http/httptest"
"strings"
"testing"
)
func AssertContains(t *testing.T, containing string, search string) {
t.Helper()
if !strings.Contains(containing, search) {
t.Errorf("Expected '%s' to contain '%s'", containing, search)
}
}
func AssertNoError(t *testing.T, err error, message string) {
t.Helper()
if err != nil {
t.Errorf(message+": %s", err.Error())
}
}
func AssertLen[T any](t *testing.T, list []T, expected int) {
t.Helper()
if len(list) != expected {
t.Errorf("Expected list to have length %d, got %d", expected, len(list))
}
}
func AssertMapLen[T any, S comparable](t *testing.T, list map[S]T, expected int) {
t.Helper()
if len(list) != expected {
t.Errorf("Expected list to have length %d, got %d", expected, len(list))
}
}
func AssertEqual[T comparable](t *testing.T, actual T, expected T) {
t.Helper()
if actual != expected {
t.Errorf("Expected '%v', got '%v'", expected, actual)
}
}
func AssertStatus(t *testing.T, rr *httptest.ResponseRecorder, expStatus int) {
if status := rr.Code; status != expStatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, expStatus)
return
}
}

View File

@ -2,6 +2,7 @@ package owl_test
import (
"h4kor/owl-blogs"
"h4kor/owl-blogs/priv/assertions"
"os"
"path"
"strings"
@ -14,14 +15,8 @@ func TestCanRenderPost(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false)
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>") {
t.Error("Post title not rendered as h1. Got: " + result)
}
assertions.AssertNoError(t, err, "Error rendering post")
assertions.AssertContains(t, result, "testpost")
}
@ -37,14 +32,8 @@ func TestRenderOneMe(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false)
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)
}
assertions.AssertNoError(t, err, "Error rendering post")
assertions.AssertContains(t, result, "href=\"https://twitter.com/testhandle\" rel=\"me\"")
}
@ -64,17 +53,9 @@ func TestRenderTwoMe(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false)
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)
}
if !strings.Contains(result, "href=\"https://github.com/testhandle\" rel=\"me\"") {
t.Error("Github handle not rendered. Got: " + result)
}
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\"")
}
@ -82,15 +63,9 @@ func TestRenderPostHEntry(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost", false)
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)
}
assertions.AssertContains(t, result, "class=\"h-entry\"")
assertions.AssertContains(t, result, "class=\"p-name\"")
assertions.AssertContains(t, result, "class=\"e-content\"")
}
@ -99,14 +74,8 @@ func TestRendererUsesBaseTemplate(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false)
result, err := owl.RenderPost(post)
if err != nil {
t.Error("Error rendering post: " + err.Error())
return
}
if !strings.Contains(result, "<html") {
t.Error("Base template not used. Got: " + result)
}
assertions.AssertNoError(t, err, "Error rendering post")
assertions.AssertContains(t, result, "<html")
}
func TestCanRenderIndexPage(t *testing.T) {
@ -114,12 +83,8 @@ func TestCanRenderIndexPage(t *testing.T) {
user.CreateNewPost("testpost1", false)
user.CreateNewPost("testpost2", false)
result, _ := owl.RenderIndexPage(user)
if !strings.Contains(result, "testpost1") {
t.Error("Post title not rendered. Got: " + result)
}
if !strings.Contains(result, "testpost2") {
t.Error("Post title not rendered. Got: " + result)
}
assertions.AssertContains(t, result, "testpost1")
assertions.AssertContains(t, result, "testpost2")
}
func TestIndexPageContainsHFeedContainer(t *testing.T) {
@ -127,9 +92,7 @@ func TestIndexPageContainsHFeedContainer(t *testing.T) {
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)
}
assertions.AssertContains(t, result, "<div class=\"h-feed\">")
}
func TestIndexPageContainsHEntryAndUUrl(t *testing.T) {
@ -137,12 +100,8 @@ func TestIndexPageContainsHEntryAndUUrl(t *testing.T) {
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)
}
assertions.AssertContains(t, result, "class=\"h-entry\"")
assertions.AssertContains(t, result, "class=\"u-url\"")
}
@ -165,16 +124,9 @@ func TestRenderUserList(t *testing.T) {
repo.CreateUser("user2")
result, err := owl.RenderUserList(repo)
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)
}
assertions.AssertNoError(t, err, "Error rendering user list")
assertions.AssertContains(t, result, "user1")
assertions.AssertContains(t, result, "user2")
}
func TestRendersHeaderTitle(t *testing.T) {
@ -187,15 +139,9 @@ func TestRendersHeaderTitle(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false)
result, _ := owl.RenderPost(post)
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)
}
assertions.AssertContains(t, result, "Test Title")
assertions.AssertContains(t, result, "Test SubTitle")
assertions.AssertContains(t, result, "#ff1337")
}
func TestRenderPostIncludesRelToWebMention(t *testing.T) {
@ -203,13 +149,9 @@ func TestRenderPostIncludesRelToWebMention(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false)
result, _ := owl.RenderPost(post)
if !strings.Contains(result, "rel=\"webmention\"") {
t.Error("webmention rel not rendered. Got: " + result)
}
assertions.AssertContains(t, result, "rel=\"webmention\"")
if !strings.Contains(result, "href=\""+user.WebmentionUrl()+"\"") {
t.Error("webmention href not rendered. Got: " + result)
}
assertions.AssertContains(t, result, "href=\""+user.WebmentionUrl()+"\"")
}
func TestRenderPostAddsLinksToApprovedWebmention(t *testing.T) {
@ -230,12 +172,8 @@ func TestRenderPostAddsLinksToApprovedWebmention(t *testing.T) {
post.PersistIncomingWebmention(webmention)
result, _ := owl.RenderPost(post)
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)
}
assertions.AssertContains(t, result, "http://example.com/source3")
assertions.AssertContains(t, result, "Test Title")
if strings.Contains(result, "http://example.com/source4") {
t.Error("unapproved webmention rendered. Got: " + result)
}
@ -258,13 +196,8 @@ func TestRenderIncludesFullUrl(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false)
result, _ := owl.RenderPost(post)
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())
}
assertions.AssertContains(t, result, "class=\"u-url\"")
assertions.AssertContains(t, result, post.FullUrl())
}
func TestAddAvatarIfExist(t *testing.T) {
@ -272,9 +205,7 @@ func TestAddAvatarIfExist(t *testing.T) {
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)
}
assertions.AssertContains(t, result, "avatar.png")
}
func TestAuthorNameInPost(t *testing.T) {
@ -288,9 +219,7 @@ func TestAuthorNameInPost(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false)
result, _ := owl.RenderPost(post)
if !strings.Contains(result, "Test Author") {
t.Error("Author Name not included. Got: " + result)
}
assertions.AssertContains(t, result, "Test Author")
}
func TestRenderReplyWithoutText(t *testing.T) {
@ -309,9 +238,7 @@ func TestRenderReplyWithoutText(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644)
result, _ := owl.RenderPost(post)
if !strings.Contains(result, "https://example.com/post") {
t.Error("Reply url not rendered. Got: " + result)
}
assertions.AssertContains(t, result, "https://example.com/post")
}
func TestRenderReplyWithText(t *testing.T) {
@ -330,13 +257,9 @@ func TestRenderReplyWithText(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644)
result, _ := owl.RenderPost(post)
if !strings.Contains(result, "https://example.com/post") {
t.Error("Reply url not rendered. Got: " + result)
}
assertions.AssertContains(t, result, "https://example.com/post")
if !strings.Contains(result, "This is a reply") {
t.Error("Reply text not rendered. Got: " + result)
}
assertions.AssertContains(t, result, "This is a reply")
}
func TestOpenGraphTags(t *testing.T) {
@ -351,28 +274,14 @@ func TestOpenGraphTags(t *testing.T) {
content += "\n"
content += "Hi \n"
err := os.WriteFile(post.ContentFile(), []byte(content), 0644)
if err != nil {
t.Error(err)
}
os.WriteFile(post.ContentFile(), []byte(content), 0644)
post, _ = user.GetPost(post.Id())
result, _ := owl.RenderPost(post)
if !strings.Contains(result, "<meta property=\"og:title\" content=\"The Rock\" />") {
t.Error("incorrent og:title . Got: " + result)
}
if !strings.Contains(result, "<meta property=\"og:description\" content=\"Dwayne Johnson\" />") {
t.Error("incorrent og:description . Got: " + result)
}
if !strings.Contains(result, "<meta property=\"og:type\" content=\"article\" />") {
t.Error("incorrent og:type . Got: " + result)
}
if !strings.Contains(result, "<meta property=\"og:url\" content=\""+post.FullUrl()+"\" />") {
t.Error("incorrent og:url . Got: " + result)
}
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()+"\" />")
}
@ -381,7 +290,5 @@ func TestAddFaviconIfExist(t *testing.T) {
os.WriteFile(path.Join(user.MediaDir(), "favicon.png"), []byte("test"), 0644)
result, _ := owl.RenderIndexPage(user)
if !strings.Contains(result, "favicon.png") {
t.Error("favicon not rendered. Got: " + result)
}
assertions.AssertContains(t, result, "favicon.png")
}

View File

@ -2,6 +2,7 @@ package owl_test
import (
"h4kor/owl-blogs"
"h4kor/owl-blogs/priv/assertions"
"os"
"path"
"testing"
@ -10,9 +11,7 @@ import (
func TestCanCreateRepository(t *testing.T) {
repoName := testRepoName()
_, err := owl.CreateRepository(repoName, owl.RepoConfig{})
if err != nil {
t.Error("Error creating repository: ", err.Error())
}
assertions.AssertNoError(t, err, "Error creating repository: ")
}
@ -88,9 +87,7 @@ func TestCanListRepoUsers(t *testing.T) {
user2, _ := repo.CreateUser(randomUserName())
// Create a new post
users, _ := repo.Users()
if len(users) != 2 {
t.Error("Wrong number of users returned, expected 2, got ", len(users))
}
assertions.AssertLen(t, users, 2)
for _, user := range users {
if user.Name() != user1.Name() && user.Name() != user2.Name() {
t.Error("User found: " + user.Name())
@ -104,9 +101,7 @@ func TestCanOpenRepository(t *testing.T) {
repo, _ := owl.CreateRepository(repoName, owl.RepoConfig{})
// Open the repository
repo2, err := owl.OpenRepository(repoName)
if err != nil {
t.Error("Error opening repository: ", err.Error())
}
assertions.AssertNoError(t, err, "Error opening repository: ")
if repo2.Dir() != repo.Dir() {
t.Error("Repository directories do not match")
}
@ -125,9 +120,7 @@ func TestGetUser(t *testing.T) {
user, _ := repo.CreateUser(randomUserName())
// Get the user
user2, err := repo.GetUser(user.Name())
if err != nil {
t.Error("Error getting user: ", err.Error())
}
assertions.AssertNoError(t, err, "Error getting user: ")
if user2.Name() != user.Name() {
t.Error("User names do not match")
}
@ -171,9 +164,7 @@ func TestNewRepoGetsStaticFilesPicoCSSWithContent(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
file, err := os.Open(path.Join(repo.StaticDir(), "pico.min.css"))
if err != nil {
t.Error("Error opening pico.min.css")
}
assertions.AssertNoError(t, err, "Error opening pico.min.css")
// check that the file has content
stat, _ := file.Stat()
if stat.Size() == 0 {
@ -194,9 +185,7 @@ func TestCanGetRepoTemplate(t *testing.T) {
repo := getTestRepo(owl.RepoConfig{})
// Get the user
template, err := repo.Template()
if err != nil {
t.Error("Error getting template: ", err.Error())
}
assertions.AssertNoError(t, err, "Error getting template: ")
if template == "" {
t.Error("Template not returned")
}
@ -215,9 +204,7 @@ func TestCanOpenRepositoryInSingleUserMode(t *testing.T) {
repo, _ := owl.OpenRepository(repoName)
users, _ := repo.Users()
if len(users) != 1 {
t.Error("Wrong number of users returned, expected 1, got ", len(users))
}
assertions.AssertLen(t, users, 1)
if users[0].Name() != userName {
t.Error("User name does not match")
}
@ -253,19 +240,12 @@ func TestCanGetMapWithAllPostAliases(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts()
if len(posts) != 1 {
t.Error("Wrong number of posts returned, expected 1, got ", len(posts))
}
assertions.AssertLen(t, posts, 1)
var aliases map[string]*owl.Post
aliases, err := repo.PostAliases()
if err != nil {
t.Error("Error getting post aliases: ", err.Error())
}
if len(aliases) != 2 {
t.Error("Wrong number of aliases returned, expected 2, got ", len(aliases))
t.Error("Aliases: ", aliases)
}
assertions.AssertNoError(t, err, "Error getting post aliases: ")
assertions.AssertMapLen(t, aliases, 2)
if aliases["/foo/bar"] == nil {
t.Error("Alias '/foo/bar' not found")
}
@ -298,19 +278,12 @@ func TestAliasesHaveCorrectPost(t *testing.T) {
os.WriteFile(post2.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts()
if len(posts) != 2 {
t.Error("Wrong number of posts returned, expected 1, got ", len(posts))
}
assertions.AssertLen(t, posts, 2)
var aliases map[string]*owl.Post
aliases, err := repo.PostAliases()
if err != nil {
t.Error("Error getting post aliases: ", err.Error())
}
if len(aliases) != 2 {
t.Error("Wrong number of aliases returned, expected 2, got ", len(aliases))
t.Error("Aliases: ", aliases)
}
assertions.AssertNoError(t, err, "Error getting post aliases: ")
assertions.AssertMapLen(t, aliases, 2)
if aliases["/foo/1"].Id() != post1.Id() {
t.Error("Alias '/foo/1' points to wrong post: ", aliases["/foo/1"].Id())
}

View File

@ -2,8 +2,8 @@ package owl_test
import (
"h4kor/owl-blogs"
"h4kor/owl-blogs/priv/assertions"
"os"
"strings"
"testing"
)
@ -14,16 +14,9 @@ func TestRenderRSSFeedMeta(t *testing.T) {
SubTitle: "Test SubTitle",
})
res, err := owl.RenderRSSFeed(user)
if err != nil {
t.Error("Error rendering RSS feed: " + err.Error())
return
}
if !strings.Contains(res, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>") {
t.Error("xml version not rendered. Got: " + res)
}
if !strings.Contains(res, "<rss version=\"2.0\">") {
t.Error("rss version not rendered. Got: " + res)
}
assertions.AssertNoError(t, err, "Error rendering RSS feed")
assertions.AssertContains(t, res, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
assertions.AssertContains(t, res, "<rss version=\"2.0\">")
}
@ -34,19 +27,10 @@ func TestRenderRSSFeedUserData(t *testing.T) {
SubTitle: "Test SubTitle",
})
res, err := owl.RenderRSSFeed(user)
if err != nil {
t.Error("Error rendering RSS feed: " + err.Error())
return
}
if !strings.Contains(res, "Test Title") {
t.Error("Title not rendered. Got: " + res)
}
if !strings.Contains(res, "Test SubTitle") {
t.Error("SubTitle not rendered. Got: " + res)
}
if !strings.Contains(res, "http://localhost:8080/user/") {
t.Error("SubTitle not rendered. Got: " + res)
}
assertions.AssertNoError(t, err, "Error rendering RSS feed")
assertions.AssertContains(t, res, "Test Title")
assertions.AssertContains(t, res, "Test SubTitle")
assertions.AssertContains(t, res, "http://localhost:8080/user/")
}
func TestRenderRSSFeedPostData(t *testing.T) {
@ -61,19 +45,10 @@ func TestRenderRSSFeedPostData(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644)
res, err := owl.RenderRSSFeed(user)
if err != nil {
t.Error("Error rendering RSS feed: " + err.Error())
return
}
if !strings.Contains(res, "Test Post") {
t.Error("Title not rendered. Got: " + res)
}
if !strings.Contains(res, post.FullUrl()) {
t.Error("SubTitle not rendered. Got: " + res)
}
if !strings.Contains(res, "Thu, 01 Jan 2015 00:00:00 +0000") {
t.Error("Date not rendered. Got: " + res)
}
assertions.AssertNoError(t, err, "Error rendering RSS feed")
assertions.AssertContains(t, res, "Test Post")
assertions.AssertContains(t, res, post.FullUrl())
assertions.AssertContains(t, res, "Thu, 01 Jan 2015 00:00:00 +0000")
}
func TestRenderRSSFeedPostDataWithoutDate(t *testing.T) {
@ -87,14 +62,7 @@ func TestRenderRSSFeedPostDataWithoutDate(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644)
res, err := owl.RenderRSSFeed(user)
if err != nil {
t.Error("Error rendering RSS feed: " + err.Error())
return
}
if !strings.Contains(res, "Test Post") {
t.Error("Title not rendered. Got: " + res)
}
if !strings.Contains(res, post.FullUrl()) {
t.Error("SubTitle not rendered. Got: " + res)
}
assertions.AssertNoError(t, err, "Error rendering RSS feed")
assertions.AssertContains(t, res, "Test Post")
assertions.AssertContains(t, res, post.FullUrl())
}

View File

@ -3,6 +3,7 @@ package owl_test
import (
"fmt"
"h4kor/owl-blogs"
"h4kor/owl-blogs/priv/assertions"
"os"
"path"
"testing"
@ -15,9 +16,7 @@ func TestCreateNewPostCreatesEntryInPublic(t *testing.T) {
// Create a new post
user.CreateNewPost("testpost", false)
files, err := os.ReadDir(path.Join(user.Dir(), "public"))
if err != nil {
t.Error("Error reading directory")
}
assertions.AssertNoError(t, err, "Error reading directory")
if len(files) < 1 {
t.Error("Post not created")
}
@ -55,9 +54,7 @@ func TestCreateNewPostMultipleCalls(t *testing.T) {
user.CreateNewPost("testpost", false)
user.CreateNewPost("testpost", false)
files, err := os.ReadDir(path.Join(user.Dir(), "public"))
if err != nil {
t.Error("Error reading directory")
}
assertions.AssertNoError(t, err, "Error reading directory")
if len(files) < 3 {
t.Errorf("Only %d posts created", len(files))
}
@ -72,12 +69,8 @@ func TestCanListUserPosts(t *testing.T) {
user.CreateNewPost("testpost", false)
user.CreateNewPost("testpost", false)
posts, err := user.Posts()
if err != nil {
t.Error("Error reading posts")
}
if len(posts) != 3 {
t.Error("No posts found")
}
assertions.AssertNoError(t, err, "Error reading posts")
assertions.AssertLen(t, posts, 3)
}
func TestCannotListUserPostsInSubdirectories(t *testing.T) {
@ -162,9 +155,7 @@ func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts()
if len(posts) != 0 {
t.Error("Found draft post")
}
assertions.AssertLen(t, posts, 0)
}
func TestListUsersDraftsExcludedRealWorld(t *testing.T) {
@ -190,9 +181,7 @@ func TestListUsersDraftsExcludedRealWorld(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts()
if len(posts) != 0 {
t.Error("Found draft post")
}
assertions.AssertLen(t, posts, 0)
}
func TestCanLoadPost(t *testing.T) {
@ -320,9 +309,7 @@ func TestAvatarSetIfFileExist(t *testing.T) {
func TestPostNameIllegalFileName(t *testing.T) {
user := getTestUser()
_, err := user.CreateNewPost("testpost?///", false)
if err != nil {
t.Error("Should not have failed")
}
assertions.AssertNoError(t, err, "Should not have failed")
}
func TestFaviconIfNotExist(t *testing.T) {

View File

@ -3,6 +3,7 @@ package owl_test
import (
"bytes"
"h4kor/owl-blogs"
"h4kor/owl-blogs/priv/assertions"
"io"
"net/http"
"net/url"
@ -28,12 +29,8 @@ func TestParseValidHEntry(t *testing.T) {
parser := &owl.OwlHtmlParser{}
entry, err := parser.ParseHEntry(&http.Response{Body: io.NopCloser(bytes.NewReader(html))})
if err != nil {
t.Errorf("Unable to parse feed: %v", err)
}
if entry.Title != "Foo" {
t.Errorf("Wrong Title. Expected %v, got %v", "Foo", entry.Title)
}
assertions.AssertNoError(t, err, "Unable to parse feed")
assertions.AssertEqual(t, entry.Title, "Foo")
}
func TestParseValidHEntryWithoutTitle(t *testing.T) {
@ -41,12 +38,8 @@ func TestParseValidHEntryWithoutTitle(t *testing.T) {
parser := &owl.OwlHtmlParser{}
entry, err := parser.ParseHEntry(&http.Response{Body: io.NopCloser(bytes.NewReader(html))})
if err != nil {
t.Errorf("Unable to parse feed: %v", err)
}
if entry.Title != "" {
t.Errorf("Wrong Title. Expected %v, got %v", "Foo", entry.Title)
}
assertions.AssertNoError(t, err, "Unable to parse feed")
assertions.AssertEqual(t, entry.Title, "")
}
func TestGetWebmentionEndpointLink(t *testing.T) {
@ -54,12 +47,9 @@ func TestGetWebmentionEndpointLink(t *testing.T) {
parser := &owl.OwlHtmlParser{}
endpoint, err := parser.GetWebmentionEndpoint(constructResponse(html))
if err != nil {
t.Errorf("Unable to parse feed: %v", err)
}
if endpoint != "http://example.com/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "http://example.com/webmention", endpoint)
}
assertions.AssertNoError(t, err, "Unable to parse feed")
assertions.AssertEqual(t, endpoint, "http://example.com/webmention")
}
func TestGetWebmentionEndpointLinkA(t *testing.T) {
@ -67,12 +57,8 @@ func TestGetWebmentionEndpointLinkA(t *testing.T) {
parser := &owl.OwlHtmlParser{}
endpoint, err := parser.GetWebmentionEndpoint(constructResponse(html))
if err != nil {
t.Errorf("Unable to parse feed: %v", err)
}
if endpoint != "http://example.com/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "http://example.com/webmention", endpoint)
}
assertions.AssertNoError(t, err, "Unable to parse feed")
assertions.AssertEqual(t, endpoint, "http://example.com/webmention")
}
func TestGetWebmentionEndpointLinkAFakeWebmention(t *testing.T) {
@ -80,12 +66,8 @@ func TestGetWebmentionEndpointLinkAFakeWebmention(t *testing.T) {
parser := &owl.OwlHtmlParser{}
endpoint, err := parser.GetWebmentionEndpoint(constructResponse(html))
if err != nil {
t.Errorf("Unable to parse feed: %v", err)
}
if endpoint != "http://example.com/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "http://example.com/webmention", endpoint)
}
assertions.AssertNoError(t, err, "Unable to parse feed")
assertions.AssertEqual(t, endpoint, "http://example.com/webmention")
}
func TestGetWebmentionEndpointLinkHeader(t *testing.T) {
@ -95,12 +77,8 @@ func TestGetWebmentionEndpointLinkHeader(t *testing.T) {
resp.Header = http.Header{"Link": []string{"<http://example.com/webmention>; rel=\"webmention\""}}
endpoint, err := parser.GetWebmentionEndpoint(resp)
if err != nil {
t.Errorf("Unable to parse feed: %v", err)
}
if endpoint != "http://example.com/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "http://example.com/webmention", endpoint)
}
assertions.AssertNoError(t, err, "Unable to parse feed")
assertions.AssertEqual(t, endpoint, "http://example.com/webmention")
}
func TestGetWebmentionEndpointLinkHeaderCommas(t *testing.T) {
@ -112,12 +90,8 @@ func TestGetWebmentionEndpointLinkHeaderCommas(t *testing.T) {
}
endpoint, err := parser.GetWebmentionEndpoint(resp)
if err != nil {
t.Errorf("Unable to parse feed: %v", err)
}
if endpoint != "https://webmention.rocks/test/19/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "https://webmention.rocks/test/19/webmention", endpoint)
}
assertions.AssertNoError(t, err, "Unable to parse feed")
assertions.AssertEqual(t, endpoint, "https://webmention.rocks/test/19/webmention")
}
func TestGetWebmentionEndpointRelativeLink(t *testing.T) {
@ -125,12 +99,8 @@ func TestGetWebmentionEndpointRelativeLink(t *testing.T) {
parser := &owl.OwlHtmlParser{}
endpoint, err := parser.GetWebmentionEndpoint(constructResponse(html))
if err != nil {
t.Errorf("Unable to parse feed: %v", err)
}
if endpoint != "http://example.com/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "http://example.com/webmention", endpoint)
}
assertions.AssertNoError(t, err, "Unable to parse feed")
assertions.AssertEqual(t, endpoint, "http://example.com/webmention")
}
func TestGetWebmentionEndpointRelativeLinkInHeader(t *testing.T) {
@ -140,12 +110,8 @@ func TestGetWebmentionEndpointRelativeLinkInHeader(t *testing.T) {
resp.Header = http.Header{"Link": []string{"</webmention>; rel=\"webmention\""}}
endpoint, err := parser.GetWebmentionEndpoint(resp)
if err != nil {
t.Errorf("Unable to parse feed: %v", err)
}
if endpoint != "http://example.com/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "http://example.com/webmention", endpoint)
}
assertions.AssertNoError(t, err, "Unable to parse feed")
assertions.AssertEqual(t, endpoint, "http://example.com/webmention")
}
// func TestRealWorldWebmention(t *testing.T) {