2019-07-17 05:28:37 +00:00
|
|
|
package browsers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
2022-09-27 20:27:20 +00:00
|
|
|
"github.com/status-im/status-go/sqlite"
|
2019-07-17 05:28:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupTestDB(t *testing.T) (*Database, func()) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "browsers-tests-")
|
|
|
|
require.NoError(t, err)
|
2022-09-27 20:27:20 +00:00
|
|
|
db, err := appdatabase.InitializeDB(tmpfile.Name(), "browsers-tests", sqlite.ReducedKDFIterationsNumber)
|
2019-07-17 05:28:37 +00:00
|
|
|
require.NoError(t, err)
|
2019-07-25 05:35:09 +00:00
|
|
|
return NewDB(db), func() {
|
2019-07-17 05:28:37 +00:00
|
|
|
require.NoError(t, db.Close())
|
|
|
|
require.NoError(t, os.Remove(tmpfile.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupTestAPI(t *testing.T) (*API, func()) {
|
|
|
|
db, cancel := setupTestDB(t)
|
2019-07-25 05:35:09 +00:00
|
|
|
return &API{db: db}, cancel
|
2019-07-17 05:28:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 18:31:34 +00:00
|
|
|
func TestBookmarks(t *testing.T) {
|
|
|
|
api, cancel := setupTestAPI(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
bookmark := &Bookmark{
|
|
|
|
Name: "MyBookmark",
|
|
|
|
URL: "https://status.im",
|
|
|
|
ImageURL: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := api.StoreBookmark(context.TODO(), *bookmark)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rst, err := api.GetBookmarks(context.TODO())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, rst, 1)
|
|
|
|
|
2022-01-17 03:42:11 +00:00
|
|
|
err = api.RemoveBookmark(context.TODO(), bookmark.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
rst, err = api.GetBookmarks(context.TODO())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, rst, 1)
|
|
|
|
require.Equal(t, rst[0].Removed, true)
|
|
|
|
|
2020-11-16 18:31:34 +00:00
|
|
|
require.NoError(t, api.DeleteBookmark(context.TODO(), bookmark.URL))
|
|
|
|
rst, err = api.GetBookmarks(context.TODO())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, rst, 0)
|
2022-01-17 03:42:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldSyncBookmark(t *testing.T) {
|
|
|
|
api, cancel := setupTestAPI(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
bookmark := &Bookmark{
|
|
|
|
Name: "MyBookmark",
|
|
|
|
URL: "https://status.im",
|
|
|
|
ImageURL: "",
|
|
|
|
Clock: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := api.StoreBookmark(context.TODO(), *bookmark)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
bookmark.Clock = 2
|
|
|
|
shouldSync, err := api.db.shouldSyncBookmark(bookmark, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, shouldSync)
|
|
|
|
|
|
|
|
bookmark.Clock = 0
|
|
|
|
shouldSync, err = api.db.shouldSyncBookmark(bookmark, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, shouldSync)
|
2020-11-16 18:31:34 +00:00
|
|
|
}
|