diff --git a/cmd/owl/web/aliases_test.go b/cmd/owl/web/aliases_test.go index 7382df7..58ca579 100644 --- a/cmd/owl/web/aliases_test.go +++ b/cmd/owl/web/aliases_test.go @@ -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) } diff --git a/cmd/owl/web/multi_user_test.go b/cmd/owl/web/multi_user_test.go index 97c11ca..58d89c0 100644 --- a/cmd/owl/web/multi_user_test.go +++ b/cmd/owl/web/multi_user_test.go @@ -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") { diff --git a/cmd/owl/web/post_test.go b/cmd/owl/web/post_test.go index 1b41982..2d7a6c9 100644 --- a/cmd/owl/web/post_test.go +++ b/cmd/owl/web/post_test.go @@ -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) } diff --git a/cmd/owl/web/rss_test.go b/cmd/owl/web/rss_test.go index b799acf..2877100 100644 --- a/cmd/owl/web/rss_test.go +++ b/cmd/owl/web/rss_test.go @@ -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") } diff --git a/cmd/owl/web/single_user_test.go b/cmd/owl/web/single_user_test.go index 9734d59..08ef094 100644 --- a/cmd/owl/web/single_user_test.go +++ b/cmd/owl/web/single_user_test.go @@ -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) diff --git a/cmd/owl/web/webmention_test.go b/cmd/owl/web/webmention_test.go index 04c68b8..e2c3105 100644 --- a/cmd/owl/web/webmention_test.go +++ b/cmd/owl/web/webmention_test.go @@ -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) } diff --git a/post_test.go b/post_test.go index cfbb8f4..98ebf3e 100644 --- a/post_test.go +++ b/post_test.go @@ -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, "