diff --git a/backend/internal/services/all.go b/backend/internal/services/all.go index 2b85c63..e255e28 100644 --- a/backend/internal/services/all.go +++ b/backend/internal/services/all.go @@ -11,6 +11,13 @@ type AllServices struct { } 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{ User: &UserService{repos}, Admin: &AdminService{repos}, diff --git a/backend/internal/services/main_test.go b/backend/internal/services/main_test.go index f010ecb..df0369f 100644 --- a/backend/internal/services/main_test.go +++ b/backend/internal/services/main_test.go @@ -18,6 +18,7 @@ import ( var ( fk = faker.NewFaker() + tCtx = Context{} tClient *ent.Client tRepos *repo.AllRepos tUser *ent.User @@ -66,7 +67,13 @@ func TestMain(m *testing.M) { tSvc = NewServices(tRepos, "/tmp/homebox") defer client.Close() + bootstrap() + tCtx = Context{ + Context: context.Background(), + GID: tGroup.ID, + UID: tUser.ID, + } os.Exit(m.Run()) } diff --git a/backend/internal/services/service_items_attachments_test.go b/backend/internal/services/service_items_attachments_test.go new file mode 100644 index 0000000..71f9b60 --- /dev/null +++ b/backend/internal/services/service_items_attachments_test.go @@ -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)) + +} diff --git a/backend/internal/services/service_items_test.go b/backend/internal/services/service_items_test.go index 0b7a787..5a21508 100644 --- a/backend/internal/services/service_items_test.go +++ b/backend/internal/services/service_items_test.go @@ -2,13 +2,9 @@ package services import ( "context" - "os" - "path" - "strings" "testing" "github.com/google/uuid" - "github.com/hay-kot/homebox/backend/internal/types" "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)) - -} diff --git a/backend/pkgs/pathlib/pathlib_test.go b/backend/pkgs/pathlib/pathlib_test.go index eb991ff..193c4ab 100644 --- a/backend/pkgs/pathlib/pathlib_test.go +++ b/backend/pkgs/pathlib/pathlib_test.go @@ -50,9 +50,8 @@ func Test_hasConflict(t *testing.T) { func TestSafePath(t *testing.T) { // override dirReader - 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 {