diff --git a/Gopkg.lock b/Gopkg.lock index 395335e..1dd3e1d 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -350,6 +350,7 @@ "github.com/spf13/viper", "github.com/stretchr/testify/assert", "github.com/stretchr/testify/mock", + "github.com/stretchr/testify/require", "github.com/stretchr/testify/suite", "github.com/tevino/abool", "github.com/thedevsaddam/govalidator", diff --git a/db/fs/fs.go b/db/fs/fs.go index 646e199..b3c5369 100644 --- a/db/fs/fs.go +++ b/db/fs/fs.go @@ -17,10 +17,6 @@ type Filesystem struct { } func (f *Filesystem) FindCapeByUsername(username string) (*model.Cape, error) { - if username == "" { - return nil, nil - } - capePath := path.Join(f.path, strings.ToLower(username)+".png") file, err := os.Open(capePath) if err != nil { diff --git a/db/fs/fs_integration_test.go b/db/fs/fs_integration_test.go new file mode 100644 index 0000000..573567e --- /dev/null +++ b/db/fs/fs_integration_test.go @@ -0,0 +1,56 @@ +package fs + +import ( + "fmt" + "io/ioutil" + "os" + "path" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestNew(t *testing.T) { + fs, err := New("base/path") + require.Nil(t, err) + require.Equal(t, "base/path", fs.path) +} + +func TestFilesystem(t *testing.T) { + t.Run("FindCapeByUsername", func(t *testing.T) { + dir, err := ioutil.TempDir("", "capes") + if err != nil { + panic(fmt.Errorf("cannot crete temp directory for tests: %w", err)) + } + defer os.RemoveAll(dir) + + t.Run("exists cape", func(t *testing.T) { + file, err := os.Create(path.Join(dir, "username.png")) + if err != nil { + panic(fmt.Errorf("cannot create temp skin for tests: %w", err)) + } + defer os.Remove(file.Name()) + + fs, _ := New(dir) + cape, err := fs.FindCapeByUsername("username") + require.Nil(t, err) + require.NotNil(t, cape) + capeFile, _ := cape.File.(*os.File) + require.Equal(t, file.Name(), capeFile.Name()) + }) + + t.Run("not exists cape", func(t *testing.T) { + fs, _ := New(dir) + cape, err := fs.FindCapeByUsername("username") + require.Nil(t, err) + require.Nil(t, cape) + }) + + t.Run("empty username", func(t *testing.T) { + fs, _ := New(dir) + cape, err := fs.FindCapeByUsername("") + require.Nil(t, err) + require.Nil(t, cape) + }) + }) +}