-
Notifications
You must be signed in to change notification settings - Fork 538
/
ebook_test.go
171 lines (148 loc) · 4.92 KB
/
ebook_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package core_test
import (
"context"
"os"
fp "path/filepath"
"testing"
"github.com/go-shiori/shiori/internal/core"
"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/assert"
)
func TestGenerateEbook(t *testing.T) {
logger := logrus.New()
_, deps := testutil.GetTestConfigurationAndDependencies(t, context.TODO(), logger)
t.Run("Successful ebook generate", func(t *testing.T) {
t.Run("valid bookmarkId that return HasEbook true", func(t *testing.T) {
// test cae
tempDir := t.TempDir()
dstDir := t.TempDir()
deps.Domains.Storage = domains.NewStorageDomain(deps, afero.NewBasePathFs(afero.NewOsFs(), dstDir))
mockRequest := core.ProcessRequest{
Bookmark: model.BookmarkDTO{
ID: 1,
Title: "Example Bookmark",
HTML: "<html><body>Example HTML</body></html>",
HasEbook: false,
},
DataDir: dstDir,
ContentType: "text/html",
}
bookmark, err := core.GenerateEbook(deps, mockRequest, fp.Join(tempDir, "1"))
assert.True(t, bookmark.HasEbook)
assert.NoError(t, err)
})
t.Run("ebook generate with valid BookmarkID EbookExist ImagePathExist ReturnWithHasEbookTrue", func(t *testing.T) {
dstDir := t.TempDir()
deps.Domains.Storage = domains.NewStorageDomain(deps, afero.NewBasePathFs(afero.NewOsFs(), dstDir))
bookmark := model.BookmarkDTO{
ID: 2,
HasEbook: false,
}
mockRequest := core.ProcessRequest{
Bookmark: bookmark,
DataDir: dstDir,
ContentType: "text/html",
}
// Create the thumbnail file
imagePath := model.GetThumbnailPath(&bookmark)
deps.Domains.Storage.FS().MkdirAll(fp.Dir(imagePath), os.ModePerm)
file, err := deps.Domains.Storage.FS().Create(imagePath)
if err != nil {
t.Fatal(err)
}
defer file.Close()
bookmark, err = core.GenerateEbook(deps, mockRequest, model.GetEbookPath(&bookmark))
expectedImagePath := "/bookmark/2/thumb"
assert.NoError(t, err)
assert.True(t, bookmark.HasEbook)
assert.Equalf(t, expectedImagePath, bookmark.ImageURL, "Expected imageURL %s, but got %s", expectedImagePath, bookmark.ImageURL)
})
t.Run("generate ebook valid BookmarkID EbookExist ReturnHasArchiveTrue", func(t *testing.T) {
tempDir := t.TempDir()
dstDir := t.TempDir()
deps.Domains.Storage = domains.NewStorageDomain(deps, afero.NewBasePathFs(afero.NewOsFs(), dstDir))
bookmark := model.BookmarkDTO{
ID: 3,
HasEbook: false,
}
mockRequest := core.ProcessRequest{
Bookmark: bookmark,
DataDir: dstDir,
ContentType: "text/html",
}
// Create the archive file
archivePath := model.GetArchivePath(&bookmark)
deps.Domains.Storage.FS().MkdirAll(fp.Dir(archivePath), os.ModePerm)
file, err := deps.Domains.Storage.FS().Create(archivePath)
if err != nil {
t.Fatal(err)
}
defer file.Close()
bookmark, err = core.GenerateEbook(deps, mockRequest, fp.Join(tempDir, "1"))
assert.True(t, bookmark.HasArchive)
assert.NoError(t, err)
})
})
t.Run("specific ebook generate case", func(t *testing.T) {
t.Run("invalid bookmarkId that return Error", func(t *testing.T) {
tempDir := t.TempDir()
mockRequest := core.ProcessRequest{
Bookmark: model.BookmarkDTO{
ID: 0,
HasEbook: false,
},
DataDir: tempDir,
ContentType: "text/html",
}
bookmark, err := core.GenerateEbook(deps, mockRequest, tempDir)
assert.Equal(t, model.BookmarkDTO{
ID: 0,
HasEbook: false,
}, bookmark)
assert.Error(t, err)
})
t.Run("ebook exist return HasEbook true", func(t *testing.T) {
dstDir := t.TempDir()
deps.Domains.Storage = domains.NewStorageDomain(deps, afero.NewBasePathFs(afero.NewOsFs(), dstDir))
bookmark := model.BookmarkDTO{
ID: 1,
HasEbook: false,
}
mockRequest := core.ProcessRequest{
Bookmark: bookmark,
DataDir: dstDir,
ContentType: "text/html",
}
// Create the ebook file
ebookFilePath := model.GetEbookPath(&bookmark)
deps.Domains.Storage.FS().MkdirAll(fp.Dir(ebookFilePath), os.ModePerm)
file, err := deps.Domains.Storage.FS().Create(ebookFilePath)
if err != nil {
t.Fatal(err)
}
defer file.Close()
bookmark, err = core.GenerateEbook(deps, mockRequest, ebookFilePath)
assert.True(t, bookmark.HasEbook)
assert.NoError(t, err)
})
t.Run("generate ebook valid BookmarkID RetuenError for PDF file", func(t *testing.T) {
tempDir := t.TempDir()
mockRequest := core.ProcessRequest{
Bookmark: model.BookmarkDTO{
ID: 1,
HasEbook: false,
},
DataDir: tempDir,
ContentType: "application/pdf",
}
bookmark, err := core.GenerateEbook(deps, mockRequest, tempDir)
assert.False(t, bookmark.HasEbook)
assert.Error(t, err)
assert.Contains(t, err.Error(), "can't create ebook for pdf")
})
})
}