-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed crash in highlights, missing recommendations
- Loading branch information
Showing
7 changed files
with
90 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,16 +11,32 @@ import ( | |
"github.com/gofiber/fiber/v2" | ||
"github.com/svera/coreander/v3/internal/webserver/infrastructure" | ||
"github.com/svera/coreander/v3/internal/webserver/model" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func TestHighlights(t *testing.T) { | ||
db := infrastructure.Connect("file::memory:", 250) | ||
var ( | ||
db *gorm.DB | ||
app *fiber.App | ||
adminCookie *http.Cookie | ||
data url.Values | ||
adminUser model.User | ||
) | ||
|
||
func setup(t *testing.T) { | ||
var err error | ||
|
||
db = infrastructure.Connect("file::memory:", 250) | ||
appFS := loadFilesInMemoryFs([]string{"fixtures/library/metadata.epub"}) | ||
app := bootstrapApp(db, &infrastructure.NoEmail{}, appFS) | ||
data := url.Values{ | ||
app = bootstrapApp(db, &infrastructure.NoEmail{}, appFS) | ||
adminCookie, err = login(app, "[email protected]", "admin") | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err.Error()) | ||
} | ||
|
||
data = url.Values{ | ||
"slug": {"john-doe-test-epub"}, | ||
} | ||
adminUser := model.User{} | ||
adminUser = model.User{} | ||
db.Where("email = ?", "[email protected]").First(&adminUser) | ||
|
||
regularUserData := url.Values{ | ||
|
@@ -32,17 +48,15 @@ func TestHighlights(t *testing.T) { | |
"words-per-minute": {"250"}, | ||
} | ||
|
||
adminCookie, err := login(app, "[email protected]", "admin") | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err.Error()) | ||
} | ||
|
||
response, err := postRequest(regularUserData, adminCookie, app, "/en/users/new") | ||
if response == nil { | ||
t.Fatalf("Unexpected error: %v", err.Error()) | ||
} | ||
} | ||
|
||
func TestHighlights(t *testing.T) { | ||
t.Run("Try to highlight a document without an active session", func(t *testing.T) { | ||
setup(t) | ||
response, err := highlight(&http.Cookie{}, app, strings.NewReader(data.Encode()), fiber.MethodPost) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err.Error()) | ||
|
@@ -52,6 +66,7 @@ func TestHighlights(t *testing.T) { | |
}) | ||
|
||
t.Run("Try to highlight and dehighlight a document with an active session", func(t *testing.T) { | ||
setup(t) | ||
response, err := highlight(adminCookie, app, strings.NewReader(data.Encode()), fiber.MethodPost) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err.Error()) | ||
|
@@ -71,7 +86,45 @@ func TestHighlights(t *testing.T) { | |
assertHighlights(app, t, adminCookie, adminUser.Uuid, 0) | ||
}) | ||
|
||
t.Run("Deleting a document also removes it from the highlights of all users", func(t *testing.T) { | ||
setup(t) | ||
regularUser := model.User{} | ||
db.Where("email = ?", "[email protected]").First(®ularUser) | ||
|
||
regularUserCookie, err := login(app, "[email protected]", "test") | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err.Error()) | ||
} | ||
|
||
response, err := highlight(regularUserCookie, 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, regularUserCookie, regularUser.Uuid, 1) | ||
|
||
adminCookie, err = login(app, "[email protected]", "admin") | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err.Error()) | ||
} | ||
|
||
_, err = postRequest(data, adminCookie, app, "/delete") | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err.Error()) | ||
} | ||
|
||
var total int64 | ||
db.Table("highlights").Where("user_id = ?", regularUser.ID).Count(&total) | ||
if total != 0 { | ||
t.Errorf("Expected no highlights in DB for user, got %d", total) | ||
} | ||
assertHighlights(app, t, adminCookie, regularUser.Uuid, 0) | ||
}) | ||
|
||
t.Run("Deleting a user also remove his/her highlights", func(t *testing.T) { | ||
setup(t) | ||
regularUser := model.User{} | ||
db.Where("email = ?", "[email protected]").First(®ularUser) | ||
|
||
|
@@ -94,7 +147,7 @@ func TestHighlights(t *testing.T) { | |
t.Fatalf("Unexpected error: %v", err.Error()) | ||
} | ||
|
||
data = url.Values{ | ||
data := url.Values{ | ||
"uuid": {regularUser.Uuid}, | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters