mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-03 16:20:27 +00:00
fix test cases
This commit is contained in:
parent
d7e3e1d326
commit
8f9054888e
5 changed files with 77 additions and 55 deletions
|
@ -11,6 +11,13 @@ type AllServices struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServices(repos *repo.AllRepos, root string) *AllServices {
|
func NewServices(repos *repo.AllRepos, root string) *AllServices {
|
||||||
|
if repos == nil {
|
||||||
|
panic("repos cannot be nil")
|
||||||
|
}
|
||||||
|
if root == "" {
|
||||||
|
panic("root cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
return &AllServices{
|
return &AllServices{
|
||||||
User: &UserService{repos},
|
User: &UserService{repos},
|
||||||
Admin: &AdminService{repos},
|
Admin: &AdminService{repos},
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
var (
|
var (
|
||||||
fk = faker.NewFaker()
|
fk = faker.NewFaker()
|
||||||
|
|
||||||
|
tCtx = Context{}
|
||||||
tClient *ent.Client
|
tClient *ent.Client
|
||||||
tRepos *repo.AllRepos
|
tRepos *repo.AllRepos
|
||||||
tUser *ent.User
|
tUser *ent.User
|
||||||
|
@ -66,7 +67,13 @@ func TestMain(m *testing.M) {
|
||||||
tSvc = NewServices(tRepos, "/tmp/homebox")
|
tSvc = NewServices(tRepos, "/tmp/homebox")
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
|
|
||||||
bootstrap()
|
bootstrap()
|
||||||
|
tCtx = Context{
|
||||||
|
Context: context.Background(),
|
||||||
|
GID: tGroup.ID,
|
||||||
|
UID: tUser.ID,
|
||||||
|
}
|
||||||
|
|
||||||
os.Exit(m.Run())
|
os.Exit(m.Run())
|
||||||
}
|
}
|
||||||
|
|
62
backend/internal/services/service_items_attachments_test.go
Normal file
62
backend/internal/services/service_items_attachments_test.go
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hay-kot/homebox/backend/internal/types"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestItemService_AddAttachment(t *testing.T) {
|
||||||
|
temp := os.TempDir()
|
||||||
|
|
||||||
|
svc := &ItemService{
|
||||||
|
repo: tRepos,
|
||||||
|
filepath: temp,
|
||||||
|
}
|
||||||
|
|
||||||
|
loc, err := tSvc.Location.Create(context.Background(), tGroup.ID, types.LocationCreate{
|
||||||
|
Description: "test",
|
||||||
|
Name: "test",
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, loc)
|
||||||
|
|
||||||
|
itmC := types.ItemCreate{
|
||||||
|
Name: fk.Str(10),
|
||||||
|
Description: fk.Str(10),
|
||||||
|
LocationID: loc.ID,
|
||||||
|
}
|
||||||
|
|
||||||
|
itm, err := svc.Create(context.Background(), tGroup.ID, itmC)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, itm)
|
||||||
|
t.Cleanup(func() {
|
||||||
|
err := svc.repo.Items.Delete(context.Background(), itm.ID)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
contents := fk.Str(1000)
|
||||||
|
reader := strings.NewReader(contents)
|
||||||
|
|
||||||
|
// Setup
|
||||||
|
afterAttachment, err := svc.AttachmentAdd(tCtx, itm.ID, "testfile.txt", "attachment", reader)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, afterAttachment)
|
||||||
|
|
||||||
|
// Check that the file exists
|
||||||
|
storedPath := afterAttachment.Attachments[0].Document.Path
|
||||||
|
|
||||||
|
// {root}/{group}/{item}/{attachment}
|
||||||
|
assert.Equal(t, path.Join(temp, tGroup.ID.String(), itm.ID.String(), "testfile.txt"), storedPath)
|
||||||
|
|
||||||
|
// Check that the file contents are correct
|
||||||
|
bts, err := os.ReadFile(storedPath)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, contents, string(bts))
|
||||||
|
|
||||||
|
}
|
|
@ -2,13 +2,9 @@ package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/hay-kot/homebox/backend/internal/types"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -92,52 +88,3 @@ func TestItemService_CsvImport(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestItemService_AddAttachment(t *testing.T) {
|
|
||||||
temp := os.TempDir()
|
|
||||||
|
|
||||||
svc := &ItemService{
|
|
||||||
repo: tRepos,
|
|
||||||
}
|
|
||||||
|
|
||||||
loc, err := tSvc.Location.Create(context.Background(), tGroup.ID, types.LocationCreate{
|
|
||||||
Description: "test",
|
|
||||||
Name: "test",
|
|
||||||
})
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotNil(t, loc)
|
|
||||||
|
|
||||||
itmC := types.ItemCreate{
|
|
||||||
Name: fk.Str(10),
|
|
||||||
Description: fk.Str(10),
|
|
||||||
LocationID: loc.ID,
|
|
||||||
}
|
|
||||||
|
|
||||||
itm, err := svc.Create(context.Background(), tGroup.ID, itmC)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotNil(t, itm)
|
|
||||||
t.Cleanup(func() {
|
|
||||||
err := svc.repo.Items.Delete(context.Background(), itm.ID)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
contents := fk.Str(1000)
|
|
||||||
reader := strings.NewReader(contents)
|
|
||||||
|
|
||||||
// Setup
|
|
||||||
afterAttachment, err := svc.AttachmentAdd(Context{Context: context.Background(), GID: tGroup.ID}, itm.ID, "testfile.txt", "attachment", reader)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotNil(t, afterAttachment)
|
|
||||||
|
|
||||||
// Check that the file exists
|
|
||||||
storedPath := afterAttachment.Attachments[0].Document.Path
|
|
||||||
|
|
||||||
// {root}/{group}/{item}/{attachment}
|
|
||||||
assert.Equal(t, path.Join(temp, tGroup.ID.String(), itm.ID.String(), "testfile.txt"), storedPath)
|
|
||||||
|
|
||||||
// Check that the file contents are correct
|
|
||||||
bts, err := os.ReadFile(storedPath)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, contents, string(bts))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -50,9 +50,8 @@ func Test_hasConflict(t *testing.T) {
|
||||||
|
|
||||||
func TestSafePath(t *testing.T) {
|
func TestSafePath(t *testing.T) {
|
||||||
// override dirReader
|
// override dirReader
|
||||||
|
|
||||||
dirReader = func(name string) []string {
|
dirReader = func(name string) []string {
|
||||||
return []string{"/foo/bar.pdf", "/foo/bar (1).pdf", "/foo/bar (2).pdf"}
|
return []string{"bar.pdf", "bar (1).pdf", "bar (2).pdf"}
|
||||||
}
|
}
|
||||||
|
|
||||||
type args struct {
|
type args struct {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue