shiori/internal/domains/bookmarks_test.go
Felipe Martin cc7c75116d
refactor: migrate bookmark static pages to new http server (#775)
* migrate bookmark content route to new http server

* new archive page

* remove unused go generate comment

* database mock

* utils cleanup

* unused var

* domains refactor and tests

* fixed secret key type

* redirect to login on ui errors

* fixed archive folder with storage domain

* webroot documentation

* some bookmark route tests

* fixed error in bookmark domain for non existant bookmarks

* centralice errors

* add coverage data to unittests

* added tests, refactor storage to use afero

* removed mock to avoid increasing complexity

* using deps to copy files around

* remove config usage (to deps)

* remove handler-ui file
2023-12-28 18:18:32 +01:00

82 lines
2.3 KiB
Go

package domains_test
import (
"context"
"testing"
"github.com/go-shiori/shiori/internal/config"
"github.com/go-shiori/shiori/internal/database"
"github.com/go-shiori/shiori/internal/dependencies"
"github.com/go-shiori/shiori/internal/domains"
"github.com/go-shiori/shiori/internal/model"
"github.com/go-shiori/shiori/internal/testutil"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
)
func TestBookmarkDomain(t *testing.T) {
fs := afero.NewMemMapFs()
db, err := database.OpenSQLiteDatabase(context.TODO(), ":memory:")
require.NoError(t, err)
require.NoError(t, db.Migrate())
deps := &dependencies.Dependencies{
Database: db,
Config: config.ParseServerConfiguration(context.TODO(), logrus.New()),
Log: logrus.New(),
Domains: &dependencies.Domains{},
}
deps.Domains.Storage = domains.NewStorageDomain(deps, fs)
fs.MkdirAll("thumb", 0755)
fs.Create("thumb/1")
fs.MkdirAll("ebook", 0755)
fs.Create("ebook/1.epub")
fs.MkdirAll("archive", 0755)
// TODO: write a valid archive file
fs.Create("archive/1")
domain := domains.NewBookmarksDomain(deps)
t.Run("HasEbook", func(t *testing.T) {
t.Run("Yes", func(t *testing.T) {
require.True(t, domain.HasEbook(&model.BookmarkDTO{ID: 1}))
})
t.Run("No", func(t *testing.T) {
require.False(t, domain.HasEbook(&model.BookmarkDTO{ID: 2}))
})
})
t.Run("HasArchive", func(t *testing.T) {
t.Run("Yes", func(t *testing.T) {
require.True(t, domain.HasArchive(&model.BookmarkDTO{ID: 1}))
})
t.Run("No", func(t *testing.T) {
require.False(t, domain.HasArchive(&model.BookmarkDTO{ID: 2}))
})
})
t.Run("HasThumbnail", func(t *testing.T) {
t.Run("Yes", func(t *testing.T) {
require.True(t, domain.HasThumbnail(&model.BookmarkDTO{ID: 1}))
})
t.Run("No", func(t *testing.T) {
require.False(t, domain.HasThumbnail(&model.BookmarkDTO{ID: 2}))
})
})
t.Run("GetBookmark", func(t *testing.T) {
t.Run("Success", func(t *testing.T) {
_, err := deps.Database.SaveBookmarks(context.TODO(), true, *testutil.GetValidBookmark())
require.NoError(t, err)
bookmark, err := domain.GetBookmark(context.Background(), 1)
require.NoError(t, err)
require.Equal(t, 1, bookmark.ID)
// Check DTO attributes
require.True(t, bookmark.HasEbook)
require.True(t, bookmark.HasArchive)
})
})
}