Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svera committed Oct 17, 2023
1 parent de1036e commit a1cefdb
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 13 deletions.
90 changes: 90 additions & 0 deletions internal/webserver/highlights_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
30 changes: 17 additions & 13 deletions internal/webserver/user_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit a1cefdb

Please sign in to comment.