From 3fe828d31dfad467ce7b43e4b2439b32c59bff9e Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Tue, 29 Nov 2022 21:22:39 +0100 Subject: [PATCH] tests for editor --- cmd/owl/web/editor_test.go | 153 +++++++++++++++++++++++++++++++++++++ renderer_test.go | 18 +++++ test/assertions/asserts.go | 7 ++ user.go | 2 +- 4 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 cmd/owl/web/editor_test.go diff --git a/cmd/owl/web/editor_test.go b/cmd/owl/web/editor_test.go new file mode 100644 index 0000000..171e18c --- /dev/null +++ b/cmd/owl/web/editor_test.go @@ -0,0 +1,153 @@ +package web_test + +import ( + main "h4kor/owl-blogs/cmd/owl/web" + "h4kor/owl-blogs/test/assertions" + "net/http" + "net/http/httptest" + "net/url" + "strconv" + "strings" + "testing" +) + +func TestLoginWrongPassword(t *testing.T) { + repo, user := getSingleUserTestRepo() + user.ResetPassword("testpassword") + + csrfToken := "test_csrf_token" + + // Create Request and Response + form := url.Values{} + form.Add("password", "wrongpassword") + form.Add("csrf_token", csrfToken) + + req, err := http.NewRequest("POST", user.EditorLoginUrl(), strings.NewReader(form.Encode())) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + req.Header.Add("Content-Length", strconv.Itoa(len(form.Encode()))) + req.AddCookie(&http.Cookie{Name: "csrf_token", Value: csrfToken}) + assertions.AssertNoError(t, err, "Error creating request") + rr := httptest.NewRecorder() + router := main.SingleUserRouter(&repo) + router.ServeHTTP(rr, req) + + // check redirect to login page + + assertions.AssertNotEqual(t, rr.Header().Get("Location"), user.EditorUrl()) +} + +func TestLoginCorrectPassword(t *testing.T) { + repo, user := getSingleUserTestRepo() + user.ResetPassword("testpassword") + + csrfToken := "test_csrf_token" + + // Create Request and Response + form := url.Values{} + form.Add("password", "testpassword") + form.Add("csrf_token", csrfToken) + + req, err := http.NewRequest("POST", user.EditorLoginUrl(), strings.NewReader(form.Encode())) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + req.Header.Add("Content-Length", strconv.Itoa(len(form.Encode()))) + req.AddCookie(&http.Cookie{Name: "csrf_token", Value: csrfToken}) + assertions.AssertNoError(t, err, "Error creating request") + rr := httptest.NewRecorder() + router := main.SingleUserRouter(&repo) + router.ServeHTTP(rr, req) + + // check redirect to login page + assertions.AssertStatus(t, rr, http.StatusFound) + assertions.AssertEqual(t, rr.Header().Get("Location"), user.EditorUrl()) +} + +func TestEditorWithoutSession(t *testing.T) { + repo, user := getSingleUserTestRepo() + user.ResetPassword("testpassword") + user.CreateNewSession() + + req, err := http.NewRequest("GET", user.EditorUrl(), nil) + // req.AddCookie(&http.Cookie{Name: "session", Value: sessionId}) + assertions.AssertNoError(t, err, "Error creating request") + rr := httptest.NewRecorder() + router := main.SingleUserRouter(&repo) + router.ServeHTTP(rr, req) + + assertions.AssertStatus(t, rr, http.StatusFound) + assertions.AssertEqual(t, rr.Header().Get("Location"), user.EditorLoginUrl()) + +} + +func TestEditorWithSession(t *testing.T) { + repo, user := getSingleUserTestRepo() + user.ResetPassword("testpassword") + sessionId := user.CreateNewSession() + + req, err := http.NewRequest("GET", user.EditorUrl(), nil) + req.AddCookie(&http.Cookie{Name: "session", Value: sessionId}) + assertions.AssertNoError(t, err, "Error creating request") + rr := httptest.NewRecorder() + router := main.SingleUserRouter(&repo) + router.ServeHTTP(rr, req) + + assertions.AssertStatus(t, rr, http.StatusOK) +} + +func TestEditorPostWithoutSession(t *testing.T) { + repo, user := getSingleUserTestRepo() + user.ResetPassword("testpassword") + user.CreateNewSession() + + csrfToken := "test_csrf_token" + + // Create Request and Response + form := url.Values{} + form.Add("type", "article") + form.Add("title", "testtitle") + form.Add("content", "testcontent") + form.Add("csrf_token", csrfToken) + + req, err := http.NewRequest("POST", user.EditorUrl(), strings.NewReader(form.Encode())) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + req.Header.Add("Content-Length", strconv.Itoa(len(form.Encode()))) + req.AddCookie(&http.Cookie{Name: "csrf_token", Value: csrfToken}) + assertions.AssertNoError(t, err, "Error creating request") + rr := httptest.NewRecorder() + router := main.SingleUserRouter(&repo) + router.ServeHTTP(rr, req) + + assertions.AssertStatus(t, rr, http.StatusFound) + assertions.AssertEqual(t, rr.Header().Get("Location"), user.EditorLoginUrl()) +} + +func TestEditorPostWithSession(t *testing.T) { + repo, user := getSingleUserTestRepo() + user.ResetPassword("testpassword") + sessionId := user.CreateNewSession() + + csrfToken := "test_csrf_token" + + // Create Request and Response + form := url.Values{} + form.Add("type", "article") + form.Add("title", "testtitle") + form.Add("content", "testcontent") + form.Add("csrf_token", csrfToken) + + req, err := http.NewRequest("POST", user.EditorUrl(), strings.NewReader(form.Encode())) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + req.Header.Add("Content-Length", strconv.Itoa(len(form.Encode()))) + req.AddCookie(&http.Cookie{Name: "csrf_token", Value: csrfToken}) + req.AddCookie(&http.Cookie{Name: "session", Value: sessionId}) + assertions.AssertNoError(t, err, "Error creating request") + rr := httptest.NewRecorder() + router := main.SingleUserRouter(&repo) + router.ServeHTTP(rr, req) + + posts, _ := user.AllPosts() + assertions.AssertEqual(t, len(posts), 1) + post := posts[0] + + assertions.AssertStatus(t, rr, http.StatusFound) + assertions.AssertEqual(t, rr.Header().Get("Location"), post.FullUrl()) +} diff --git a/renderer_test.go b/renderer_test.go index 459216f..f199f02 100644 --- a/renderer_test.go +++ b/renderer_test.go @@ -112,6 +112,24 @@ func TestIndexPageContainsHEntryAndUUrl(t *testing.T) { } +func TestIndexPageDoesNotContainsArticle(t *testing.T) { + user := getTestUser() + user.CreateNewPostFull(owl.PostMeta{Type: "article"}, "hi") + + result, _ := owl.RenderIndexPage(user) + assertions.AssertContains(t, result, "class=\"h-entry\"") + assertions.AssertContains(t, result, "class=\"u-url\"") +} + +func TestIndexPageDoesNotContainsReply(t *testing.T) { + user := getTestUser() + user.CreateNewPostFull(owl.PostMeta{Type: "reply", Reply: owl.Reply{Url: "https://example.com/post"}}, "hi") + + result, _ := owl.RenderIndexPage(user) + assertions.AssertContains(t, result, "class=\"h-entry\"") + assertions.AssertContains(t, result, "class=\"u-url\"") +} + func TestRenderIndexPageWithBrokenBaseTemplate(t *testing.T) { user := getTestUser() user.CreateNewPost("testpost1", false) diff --git a/test/assertions/asserts.go b/test/assertions/asserts.go index 613a6bc..a49d542 100644 --- a/test/assertions/asserts.go +++ b/test/assertions/asserts.go @@ -79,6 +79,13 @@ func AssertEqual[T comparable](t *testing.T, actual T, expected T) { } } +func AssertNotEqual[T comparable](t *testing.T, actual T, expected T) { + t.Helper() + if actual == expected { + t.Errorf("Expected '%v' to not be '%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", diff --git a/user.go b/user.go index d8a65c6..f101b99 100644 --- a/user.go +++ b/user.go @@ -225,7 +225,7 @@ func (user User) PrimaryFeedPosts() ([]*Post, error) { n := 0 for _, post := range posts { meta := post.Meta() - if meta.Type == "article" { + if meta.Type == "article" || meta.Type == "reply" { posts[n] = post n++ }