From a1cefdbafa7494a8a1298f7a0a704e82533a8bbd Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Tue, 17 Oct 2023 21:00:40 +0200 Subject: [PATCH] Added tests --- internal/webserver/highlights_test.go | 90 ++++++++++++++++++++++ internal/webserver/user_management_test.go | 30 ++++---- 2 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 internal/webserver/highlights_test.go diff --git a/internal/webserver/highlights_test.go b/internal/webserver/highlights_test.go new file mode 100644 index 0000000..ac20a41 --- /dev/null +++ b/internal/webserver/highlights_test.go @@ -0,0 +1,90 @@ +package webserver_test + +import ( + "net/http" + "net/url" + "strings" + "testing" + + "github.com/PuerkitoBio/goquery" + "github.com/gofiber/fiber/v2" + "github.com/svera/coreander/v3/internal/infrastructure" +) + +func TestHighlights(t *testing.T) { + db := infrastructure.Connect("file::memory:", 250) + appFS := loadFilesInMemoryFs([]string{"fixtures/metadata.epub"}) + app := bootstrapApp(db, &infrastructure.NoEmail{}, appFS) + data := url.Values{ + "slug": {"john-doe-test-epub"}, + } + + adminCookie, err := login(app, "admin@example.com", "admin") + if err != nil { + t.Fatalf("Unexpected error: %v", err.Error()) + } + + t.Run("Try to highlight a document without an active session", func(t *testing.T) { + response, err := highlight(&http.Cookie{}, app, strings.NewReader(data.Encode()), fiber.MethodPost) + if err != nil { + t.Fatalf("Unexpected error: %v", err.Error()) + } + + mustReturnForbiddenAndShowLogin(response, t) + }) + + t.Run("Try to highlight and dehighlight a document with an active session", func(t *testing.T) { + response, err := highlight(adminCookie, app, strings.NewReader(data.Encode()), fiber.MethodPost) + if err != nil { + t.Fatalf("Unexpected error: %v", err.Error()) + } + + mustReturnStatus(response, fiber.StatusOK, t) + + assertHighlights(app, t, adminCookie, 1) + + response, err = highlight(adminCookie, app, strings.NewReader(data.Encode()), fiber.MethodDelete) + if err != nil { + t.Fatalf("Unexpected error: %v", err.Error()) + } + + mustReturnStatus(response, fiber.StatusOK, t) + + assertHighlights(app, t, adminCookie, 0) + }) +} + +func highlight(cookie *http.Cookie, app *fiber.App, reader *strings.Reader, method string) (*http.Response, error) { + req, err := http.NewRequest(method, "/highlight", reader) + if err != nil { + return nil, err + } + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + req.AddCookie(cookie) + + return app.Test(req) +} + +func assertHighlights(app *fiber.App, t *testing.T, cookie *http.Cookie, expectedResults int) { + req, err := http.NewRequest(http.MethodGet, "/en/highlights", nil) + req.AddCookie(cookie) + if err != nil { + t.Fatalf("Unexpected error: %v", err.Error()) + } + response, err := app.Test(req) + if err != nil { + t.Fatalf("Unexpected error: %v", err.Error()) + } + if expectedStatus := http.StatusOK; response.StatusCode != expectedStatus { + t.Errorf("Expected status %d, received %d", expectedStatus, response.StatusCode) + } + + doc, err := goquery.NewDocumentFromReader(response.Body) + if err != nil { + t.Fatal(err) + } + + if actualResults := doc.Find(".list-group-item").Length(); actualResults != expectedResults { + t.Errorf("Expected %d results, got %d", expectedResults, actualResults) + } +} diff --git a/internal/webserver/user_management_test.go b/internal/webserver/user_management_test.go index 307e0e5..9b0d78c 100644 --- a/internal/webserver/user_management_test.go +++ b/internal/webserver/user_management_test.go @@ -39,14 +39,14 @@ func TestUserManagement(t *testing.T) { t.Fatalf("Unexpected error: %v", err.Error()) } - mustRedirectToLogin(response, t) + mustReturnForbiddenAndShowLogin(response, t) response, err = addUser(data, &http.Cookie{}, app) if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } - mustRedirectToLogin(response, t) + mustReturnForbiddenAndShowLogin(response, t) }) t.Run("Try to add a user with an admin active session", func(t *testing.T) { @@ -128,14 +128,14 @@ func TestUserManagement(t *testing.T) { t.Fatalf("Unexpected error: %v", err.Error()) } - mustRedirectToLogin(response, t) + mustReturnForbiddenAndShowLogin(response, t) response, err = updateUser(testUser.Uuid, data, &http.Cookie{}, app) if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } - mustRedirectToLogin(response, t) + mustReturnForbiddenAndShowLogin(response, t) }) t.Run("Try to update a user using another, non admin user session", func(t *testing.T) { @@ -228,7 +228,7 @@ func TestUserManagement(t *testing.T) { t.Fatalf("Unexpected error: %v", err.Error()) } - mustRedirectToLogin(response, t) + mustReturnForbiddenAndShowLogin(response, t) }) t.Run("Try to delete a user with a regular user's session", func(t *testing.T) { @@ -281,18 +281,22 @@ func mustRedirectToUsersList(response *http.Response, t *testing.T) { } } -func mustRedirectToLogin(response *http.Response, t *testing.T) { - if response.StatusCode != http.StatusFound { - t.Errorf("Expected status %d, received %d", http.StatusFound, response.StatusCode) +func mustReturnForbiddenAndShowLogin(response *http.Response, t *testing.T) { + if response.StatusCode != http.StatusForbidden { + t.Errorf("Expected status %d, received %d", http.StatusForbidden, response.StatusCode) return } - url, err := response.Location() + + doc, err := goquery.NewDocumentFromReader(response.Body) if err != nil { - t.Error("No location header present") - return + t.Fatal(err) + } + selection, err := doc.Find("head title").First().Html() + if err != nil { + t.Fatal(err) } - if url.Path != "/en/login" { - t.Errorf("Expected location %s, received %s", "/en/login", url.Path) + if selection != "Login" { + t.Errorf("Expected login page, received %s", selection) } }