Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Ostrovskiy committed Nov 6, 2023
1 parent 88b642c commit 9372a0d
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 159 deletions.
21 changes: 10 additions & 11 deletions internal/client/infrastrucrure/filemanager/filemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,13 @@ func NewFileManager(storageDir, uploadDir, userDir string, meta models.UserMeta,
return nil, err
}

fm := &FileManager{
return &FileManager{
storageDir: storageDir,
uploadDir: uploadDir,
userDir: userDir,
meta: meta,
key: key,
}

return fm, nil

}, nil
}

func (fm *FileManager) AddToStorage(name string, data Data, info models.DataInfo) error {
Expand Down Expand Up @@ -181,19 +178,21 @@ func (fm *FileManager) UpdateInfoByName(name string, info models.DataInfo) error
return errors.New("not found secret")
}

savedInfo.UpdatedAt = time.Now()

if info.Name == "" {
if info.Name != "" && savedInfo.Name != info.Name {
savedInfo.Name = info.Name
delete(fm.meta.Data, name)
}

if info.Description == "" {
if info.Description != "" {
savedInfo.Description = info.Description
}

delete(fm.meta.Data, name)
if info.FileName != nil {
savedInfo.FileName = info.FileName
}

fm.meta.Data[info.Name] = info
savedInfo.UpdatedAt = time.Now()
fm.meta.Data[savedInfo.Name] = savedInfo

return fm.saveMetaData()
}
Expand Down
252 changes: 150 additions & 102 deletions internal/client/infrastrucrure/filemanager/filemanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package filemanager
import (
"bytes"
"encoding/json"
"github.com/kripsy/GophKeeper/internal/models"
"os"
"path/filepath"
"sync"
"testing"
//"time"
// "github.com/google/uuid"

"github.com/kripsy/GophKeeper/internal/models"
)

var (
Expand Down Expand Up @@ -141,61 +140,103 @@ func TestFileManager_AddToStorage(t *testing.T) {
}

func TestFileManager_UpdateInfoByName(t *testing.T) {
info := models.DataInfo{
Name: "NewTestData",
Description: "NewTest data description",
}
defer os.RemoveAll(storageDir)

tests := []struct {
name string
storage FileStorage
data Data
secretName string
newInfo models.DataInfo
wantErr bool
name string
storage *FileManager
data Data
secretName string
newSecretName string
newInfo models.DataInfo
wantInfo models.DataInfo
wantErr bool
}{
{
name: "ok update name",
storage: func() FileStorage {
storage: func() *FileManager {
meta := models.UserMeta{Data: make(models.MetaData)}
meta.Data["testData"] = info
meta.Data["testData"] = models.DataInfo{
Name: "testData",
Description: "test data description",
}
fs, err := NewFileManager(storageDir, userDir, userDir, meta, testKey)
if err != nil {
t.Fatalf("Failed to create FileManager: %v", err)
}

return fs
}(),
data: Note{Text: "test"},
secretName: "testData",
data: Note{Text: "test"},
secretName: "testData",
newSecretName: "NewTestData",
newInfo: models.DataInfo{
Name: "NewTestData",
},
wantInfo: models.DataInfo{
Name: "NewTestData",
Description: "test data description",
},
wantErr: false,
},
{
name: "ok update description",
storage: func() FileStorage {
storage: func() *FileManager {
meta := models.UserMeta{Data: make(models.MetaData)}
meta.Data["testData"] = info
meta.Data["testData"] = models.DataInfo{
Name: "testData",
Description: "test data description",
}
fs, err := NewFileManager(storageDir, userDir, userDir, meta, testKey)
if err != nil {
t.Fatalf("Failed to create FileManager: %v", err)
}

return fs
}(),
data: Note{Text: "test"},
secretName: "testData",
data: Note{Text: "test"},
secretName: "testData",
newSecretName: "testData",
newInfo: models.DataInfo{
Description: "NewTest data description",
},
wantInfo: models.DataInfo{
Name: "testData",
Description: "NewTest data description",
},
wantErr: false,
},
{
name: "ok update filename",
storage: func() *FileManager {
meta := models.UserMeta{Data: make(models.MetaData)}
meta.Data["testData"] = models.DataInfo{
Name: "testData",
Description: "test data description",
}
fs, err := NewFileManager(storageDir, userDir, userDir, meta, testKey)
if err != nil {
t.Fatalf("Failed to create FileManager: %v", err)
}

return fs
}(),
data: Note{Text: "test"},
secretName: "testData",
newSecretName: "testData",
newInfo: models.DataInfo{
FileName: &filename,
},
wantInfo: models.DataInfo{
Name: "testData",
Description: "test data description",
FileName: &filename,
},
wantErr: false,
},
{
name: "failed if secret not exist",
storage: func() FileStorage {
storage: func() *FileManager {
meta := models.UserMeta{Data: make(models.MetaData)}
fs, err := NewFileManager(storageDir, userDir, userDir, meta, testKey)
if err != nil {
Expand All @@ -214,6 +255,93 @@ func TestFileManager_UpdateInfoByName(t *testing.T) {
if err := tt.storage.UpdateInfoByName(tt.secretName, tt.newInfo); (err != nil) != tt.wantErr {
t.Errorf("UpdateInfoByName() error = %v, wantErr %v", err, tt.wantErr)
}
got := tt.storage.meta.Data[tt.newSecretName]
if !tt.wantErr {
if got.Name != tt.wantInfo.Name ||
got.Description != tt.wantInfo.Description ||
got.FileName != tt.wantInfo.FileName {
t.Errorf("UpdateInfoByName() error meta not equal")
}
}
})
}
}

func TestFileManager_UpdateDataByName(t *testing.T) {
defer os.RemoveAll(storageDir)
info := models.DataInfo{
Name: "testData",
Description: "Test data description",
}

tests := []struct {
name string
storage FileStorage
info models.DataInfo
data Data
wantErr bool
}{
{
name: "ok",
storage: func() FileStorage {
meta := models.UserMeta{Data: make(models.MetaData)}
testInfo := info
testInfo.DataID = filename
meta.Data["testData"] = testInfo
fs, err := NewFileManager(storageDir, userDir, userDir, meta, testKey)
if err != nil {
t.Fatalf("Failed to create FileManager: %v", err)
}

if err = os.WriteFile(filepath.Join(storageDir, filename), nil, fileMode); err != nil {
t.Fatalf("Failed to create testFile: %v", err)
}

return fs
}(),
info: info,
data: Note{Text: "test"},
wantErr: false,
},
{
name: "failed if secret not exist",
storage: func() FileStorage {
meta := models.UserMeta{Data: make(models.MetaData)}
fs, err := NewFileManager(storageDir, userDir, userDir, meta, testKey)
if err != nil {
t.Fatalf("Failed to create FileManager: %v", err)
}

return fs
}(),
info: info,
wantErr: true,
},
{
name: "failed with shortness key",
storage: func() FileStorage {
meta := models.UserMeta{Data: make(models.MetaData)}
testInfo := info
testInfo.DataID = filename
meta.Data["testData"] = testInfo
fs, err := NewFileManager(storageDir, userDir, userDir, meta, []byte("testKey"))
if err != nil {
t.Fatalf("Failed to create FileManager: %v", err)
}

return fs
}(),
info: info,
data: Note{Text: "test"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.storage.UpdateDataByName(tt.info.Name, tt.data)
if (err != nil) != tt.wantErr {
t.Errorf("GetByName() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Expand Down Expand Up @@ -535,83 +663,3 @@ func TestFileManager_ReadEncryptedByName1(t *testing.T) {
})
}
}

func TestFileManager_UpdateDataByName(t *testing.T) {
defer os.RemoveAll(storageDir)
info := models.DataInfo{
Name: "testData",
Description: "Test data description",
}

tests := []struct {
name string
storage FileStorage
info models.DataInfo
data Data
wantErr bool
}{
{
name: "ok",
storage: func() FileStorage {
meta := models.UserMeta{Data: make(models.MetaData)}
testInfo := info
testInfo.DataID = filename
meta.Data["testData"] = testInfo
fs, err := NewFileManager(storageDir, userDir, userDir, meta, testKey)
if err != nil {
t.Fatalf("Failed to create FileManager: %v", err)
}

if err = os.WriteFile(filepath.Join(storageDir, filename), nil, fileMode); err != nil {
t.Fatalf("Failed to create testFile: %v", err)
}

return fs
}(),
info: info,
data: Note{Text: "test"},
wantErr: false,
},
{
name: "failed if secret not exist",
storage: func() FileStorage {
meta := models.UserMeta{Data: make(models.MetaData)}
fs, err := NewFileManager(storageDir, userDir, userDir, meta, testKey)
if err != nil {
t.Fatalf("Failed to create FileManager: %v", err)
}

return fs
}(),
info: info,
wantErr: true,
},
{
name: "failed with shortness key",
storage: func() FileStorage {
meta := models.UserMeta{Data: make(models.MetaData)}
testInfo := info
testInfo.DataID = filename
meta.Data["testData"] = testInfo
fs, err := NewFileManager(storageDir, userDir, userDir, meta, []byte("testKey"))
if err != nil {
t.Fatalf("Failed to create FileManager: %v", err)
}

return fs
}(),
info: info,
data: Note{Text: "test"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.storage.UpdateDataByName(tt.info.Name, tt.data)
if (err != nil) != tt.wantErr {
t.Errorf("GetByName() error = %v, wantErr %v", err, tt.wantErr)
}

})
}
}
3 changes: 2 additions & 1 deletion internal/client/infrastrucrure/filemanager/userauth_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package filemanager

import (
"github.com/kripsy/GophKeeper/internal/models"
"os"
"reflect"
"testing"

"github.com/kripsy/GophKeeper/internal/models"
)

const (
Expand Down
7 changes: 7 additions & 0 deletions internal/client/infrastrucrure/ui/cli/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ func (c *CLI) Exit() {
c.Clear()
os.Exit(1)
}

func (c *CLI) PrintErr(error string) {
fmt.Println(promptui.Styler(
promptui.FGRed,
promptui.FGBold,
)(error))
}
1 change: 1 addition & 0 deletions internal/client/infrastrucrure/ui/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ type UserInterface interface {

Sync(stop <-chan struct{})
Clear()
PrintErr(error string)
Exit()
}
Loading

0 comments on commit 9372a0d

Please sign in to comment.