144 lines
3.3 KiB
Go
144 lines
3.3 KiB
Go
|
package client_test
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"io/ioutil"
|
||
|
"testing"
|
||
|
|
||
|
"git.kapelle.org/niklas/s3share/internal/client"
|
||
|
"git.kapelle.org/niklas/s3share/internal/db"
|
||
|
"git.kapelle.org/niklas/s3share/internal/s3"
|
||
|
"git.kapelle.org/niklas/s3share/internal/types"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func setup(t *testing.T) (*client.Client, context.Context, *assert.Assertions) {
|
||
|
|
||
|
mockDb := db.NewMock()
|
||
|
mockS3 := s3.NewMockS3()
|
||
|
|
||
|
service := client.NewClient(mockDb, mockS3)
|
||
|
ctx := context.Background()
|
||
|
assert := assert.New(t)
|
||
|
|
||
|
return service, ctx, assert
|
||
|
}
|
||
|
|
||
|
func TestCreateShare(t *testing.T) {
|
||
|
service, ctx, assert := setup(t)
|
||
|
|
||
|
share, err := service.CreateShare(ctx, "test.txt")
|
||
|
assert.NoError(err)
|
||
|
assert.NotNil(share)
|
||
|
assert.NotEmpty(share.Slug)
|
||
|
assert.Equal("test.txt", share.Key)
|
||
|
}
|
||
|
|
||
|
func TestCreateShareInvalidKey(t *testing.T) {
|
||
|
service, ctx, assert := setup(t)
|
||
|
|
||
|
_, err := service.CreateShare(ctx, "not_existing.txt")
|
||
|
assert.Error(err)
|
||
|
}
|
||
|
|
||
|
func TestGetShare(t *testing.T) {
|
||
|
service, ctx, assert := setup(t)
|
||
|
|
||
|
createdShare, err := service.CreateShare(ctx, "test.txt")
|
||
|
|
||
|
share, err := service.GetShare(ctx, createdShare.Slug)
|
||
|
assert.NoError(err)
|
||
|
assert.NotNil(share)
|
||
|
assert.Equal(createdShare.Slug, share.Slug)
|
||
|
assert.Equal("test.txt", share.Key)
|
||
|
}
|
||
|
|
||
|
func TestGetShareNotFound(t *testing.T) {
|
||
|
service, ctx, assert := setup(t)
|
||
|
|
||
|
share, err := service.GetShare(ctx, "123456")
|
||
|
assert.NoError(err)
|
||
|
assert.Nil(share)
|
||
|
}
|
||
|
|
||
|
func TestGetObjFromShare(t *testing.T) {
|
||
|
service, ctx, assert := setup(t)
|
||
|
|
||
|
createdShare, _ := service.CreateShare(ctx, "test.txt")
|
||
|
|
||
|
reader, err := service.GetObjectFromShare(ctx, createdShare)
|
||
|
assert.NoError(err)
|
||
|
assert.NotNil(reader)
|
||
|
|
||
|
content, err := ioutil.ReadAll(reader)
|
||
|
assert.NoError(err)
|
||
|
assert.Equal("test.txt", string(content))
|
||
|
}
|
||
|
|
||
|
func TestGetObjFromShareNotFound(t *testing.T) {
|
||
|
service, ctx, assert := setup(t)
|
||
|
|
||
|
_, err := service.GetObjectFromShare(ctx, &types.Share{Slug: "123456"})
|
||
|
assert.Error(err)
|
||
|
}
|
||
|
|
||
|
func TestDeleteShare(t *testing.T) {
|
||
|
service, ctx, assert := setup(t)
|
||
|
|
||
|
createdShare, _ := service.CreateShare(ctx, "test.txt")
|
||
|
|
||
|
err := service.DeleteShare(ctx, createdShare.Slug)
|
||
|
assert.NoError(err)
|
||
|
|
||
|
share, err := service.GetShare(ctx, createdShare.Slug)
|
||
|
assert.NoError(err)
|
||
|
assert.Nil(share)
|
||
|
}
|
||
|
|
||
|
func TestDeleteShareNotFound(t *testing.T) {
|
||
|
service, ctx, assert := setup(t)
|
||
|
|
||
|
err := service.DeleteShare(ctx, "123456")
|
||
|
assert.Error(err)
|
||
|
}
|
||
|
|
||
|
func TestGetMetadata(t *testing.T) {
|
||
|
service, ctx, assert := setup(t)
|
||
|
|
||
|
metadata, err := service.GetObjectMetadata(ctx, "test.txt")
|
||
|
assert.NoError(err)
|
||
|
assert.NotNil(metadata)
|
||
|
assert.Equal("test.txt", metadata.Filename)
|
||
|
assert.Equal("text/plain", metadata.ContentType)
|
||
|
assert.Equal(int64(8), metadata.Size)
|
||
|
}
|
||
|
|
||
|
func TestGetMetadataNotFound(t *testing.T) {
|
||
|
service, ctx, assert := setup(t)
|
||
|
|
||
|
metadata, err := service.GetObjectMetadata(ctx, "not_existing.txt")
|
||
|
assert.NoError(err)
|
||
|
assert.Nil(metadata)
|
||
|
}
|
||
|
|
||
|
func TestGetAllShares(t *testing.T) {
|
||
|
service, ctx, assert := setup(t)
|
||
|
|
||
|
share, err := service.CreateShare(ctx, "test.txt")
|
||
|
assert.NoError(err)
|
||
|
|
||
|
shares, err := service.GetAllShares(ctx)
|
||
|
assert.NoError(err)
|
||
|
assert.Len(shares, 1)
|
||
|
|
||
|
assert.Equal(share.Slug, shares[0].Slug)
|
||
|
assert.Equal(share.Key, shares[0].Key)
|
||
|
|
||
|
_, err = service.CreateShare(ctx, "dir/test")
|
||
|
assert.NoError(err)
|
||
|
|
||
|
shares, err = service.GetAllShares(ctx)
|
||
|
assert.NoError(err)
|
||
|
assert.Len(shares, 2)
|
||
|
}
|