Skip to content

Commit e285de6

Browse files
igor-sirotinjrainville
authored andcommitted
feat: FileNameProvider
1 parent bd727c8 commit e285de6

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed

node/backup/controller.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ import (
1414
"github.com/status-im/status-go/signal"
1515
)
1616

17+
//go:generate go tool mockgen -package=mock_backup_controller -source controller.go -destination=mock/mock_backup_controller.go
18+
1719
type BackupConfig struct {
18-
PrivateKey []byte
19-
FileNameGetter func() (string, error)
20-
BackupEnabled bool
21-
Interval time.Duration
20+
PrivateKey []byte
21+
FileNameProvider FilenameProvider
22+
BackupEnabled bool
23+
Interval time.Duration
24+
}
25+
26+
type FilenameProvider interface {
27+
GetBackupFilename() (string, error)
2228
}
2329

2430
type BackupProvider interface {
@@ -52,8 +58,8 @@ func NewController(config BackupConfig, logger *zap.Logger) (*Controller, error)
5258
if len(config.PrivateKey) == 0 {
5359
return nil, errors.New("private key must be provided")
5460
}
55-
if config.FileNameGetter == nil {
56-
return nil, errors.New("filename getter must be provided")
61+
if common.IsNil(config.FileNameProvider) {
62+
return nil, errors.New("filename provider must be provided")
5763
}
5864

5965
return &Controller{
@@ -111,7 +117,7 @@ func (c *Controller) PerformBackup() (string, error) {
111117
return "", err
112118
}
113119

114-
fileName, err := c.config.FileNameGetter()
120+
fileName, err := c.config.FileNameProvider.GetBackupFilename()
115121
if err != nil {
116122
return "", err
117123
}

node/backup/controller_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import (
77
"testing"
88

99
"github.com/brianvoe/gofakeit/v6"
10+
"go.uber.org/mock/gomock"
1011

1112
"go.uber.org/zap"
1213

1314
"github.com/stretchr/testify/require"
15+
16+
mock_backup_controller "github.com/status-im/status-go/node/backup/mock"
1417
)
1518

1619
type Foo struct {
@@ -55,9 +58,14 @@ func TestController(t *testing.T) {
5558
logger, err := zap.NewDevelopment()
5659
require.NoError(t, err)
5760
filename := t.TempDir() + "/test_backup.bak"
61+
62+
ctrl := gomock.NewController(t)
63+
filenameProvider := mock_backup_controller.NewMockFilenameProvider(ctrl)
64+
filenameProvider.EXPECT().GetBackupFilename().Return(filename, nil).AnyTimes()
65+
5866
controller, err := NewController(BackupConfig{
59-
FileNameGetter: func() (string, error) { return filename, nil },
60-
PrivateKey: []byte("0123456789abcdef0123456789abcdef"),
67+
FileNameProvider: filenameProvider,
68+
PrivateKey: []byte("0123456789abcdef0123456789abcdef"),
6169
}, logger)
6270
require.NoError(t, err)
6371

node/get_status_node.go

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -248,31 +248,11 @@ func (n *StatusNode) StartLocalBackup() error {
248248

249249
privateKey := chatAccount.PrivateKey()
250250

251-
filenameGetter := func() (string, error) {
252-
backupPath, err := n.accountsSrvc.GetBackupPath()
253-
if err != nil {
254-
return "", err
255-
}
256-
257-
compressedPubKey, err := utils.SerializePublicKey(crypto.CompressPubkey(&privateKey.PublicKey))
258-
if err != nil {
259-
return "", err
260-
}
261-
262-
if backupPath == "" {
263-
return "", errors.New("backup path is not set")
264-
}
265-
266-
fullPath := filepath.Join(backupPath, fmt.Sprintf("%s_user_data.bkp", compressedPubKey[len(compressedPubKey)-6:]))
267-
268-
return fullPath, nil
269-
}
270-
271251
n.localBackup, err = backup.NewController(backup.BackupConfig{
272-
PrivateKey: crypto.Keccak256(crypto.FromECDSA(privateKey)),
273-
FileNameGetter: filenameGetter,
274-
BackupEnabled: true,
275-
Interval: time.Minute * 30,
252+
PrivateKey: crypto.Keccak256(crypto.FromECDSA(privateKey)),
253+
FileNameProvider: n,
254+
BackupEnabled: true,
255+
Interval: time.Minute * 30,
276256
}, n.logger.Named("LocalBackup"))
277257
if err != nil {
278258
return err
@@ -295,6 +275,33 @@ func (n *StatusNode) StartLocalBackup() error {
295275
return nil
296276
}
297277

278+
func (n *StatusNode) GetBackupFilename() (string, error) {
279+
chatAccount, err := n.gethAccountsManager.SelectedChatAccount()
280+
if err != nil {
281+
return "", err
282+
}
283+
284+
privateKey := chatAccount.PrivateKey()
285+
286+
backupPath, err := n.accountsSrvc.GetBackupPath()
287+
if err != nil {
288+
return "", err
289+
}
290+
291+
compressedPubKey, err := utils.SerializePublicKey(crypto.CompressPubkey(&privateKey.PublicKey))
292+
if err != nil {
293+
return "", err
294+
}
295+
296+
if backupPath == "" {
297+
return "", errors.New("backup path is not set")
298+
}
299+
300+
fullPath := filepath.Join(backupPath, fmt.Sprintf("%s_user_data.bkp", compressedPubKey[len(compressedPubKey)-6:]))
301+
302+
return fullPath, nil
303+
}
304+
298305
func (n *StatusNode) PerformLocalBackup() (string, error) {
299306
return n.localBackup.PerformBackup()
300307
}

0 commit comments

Comments
 (0)