2019-07-17 05:28:37 +00:00
|
|
|
package browsers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
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)
|
2019-07-25 05:35:09 +00:00
|
|
|
db, err := appdatabase.InitializeDB(tmpfile.Name(), "browsers-tests")
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func TestBrowsersOrderedNewestFirst(t *testing.T) {
|
|
|
|
api, cancel := setupTestAPI(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
browsers := []*Browser{
|
|
|
|
{
|
|
|
|
ID: "1",
|
|
|
|
Name: "first",
|
|
|
|
Dapp: true,
|
|
|
|
Timestamp: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "2",
|
|
|
|
Name: "second",
|
|
|
|
Dapp: true,
|
|
|
|
Timestamp: 50,
|
|
|
|
},
|
|
|
|
{
|
2019-07-27 05:48:23 +00:00
|
|
|
ID: "3",
|
|
|
|
Name: "third",
|
|
|
|
Dapp: true,
|
|
|
|
Timestamp: 100,
|
|
|
|
HistoryIndex: 0,
|
|
|
|
History: []string{"zero"},
|
2019-07-17 05:28:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for i := 0; i < len(browsers); i++ {
|
|
|
|
require.NoError(t, api.AddBrowser(context.TODO(), *browsers[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(browsers, func(i, j int) bool {
|
|
|
|
return browsers[i].Timestamp > browsers[j].Timestamp
|
|
|
|
})
|
|
|
|
|
|
|
|
rst, err := api.GetBrowsers(context.TODO())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, browsers, rst)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBrowsersHistoryIncluded(t *testing.T) {
|
|
|
|
api, cancel := setupTestAPI(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
browser := &Browser{
|
|
|
|
ID: "1",
|
|
|
|
Name: "first",
|
|
|
|
Dapp: true,
|
|
|
|
Timestamp: 10,
|
|
|
|
HistoryIndex: 1,
|
|
|
|
History: []string{"one", "two"},
|
|
|
|
}
|
|
|
|
require.NoError(t, api.AddBrowser(context.TODO(), *browser))
|
|
|
|
rst, err := api.GetBrowsers(context.TODO())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, rst, 1)
|
|
|
|
require.Equal(t, browser, rst[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBrowsersReplaceOnUpdate(t *testing.T) {
|
|
|
|
api, cancel := setupTestAPI(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
browser := &Browser{
|
|
|
|
ID: "1",
|
|
|
|
Name: "first",
|
|
|
|
Dapp: true,
|
|
|
|
Timestamp: 10,
|
|
|
|
History: []string{"one", "two"},
|
|
|
|
}
|
|
|
|
require.NoError(t, api.AddBrowser(context.TODO(), *browser))
|
|
|
|
browser.Dapp = false
|
|
|
|
browser.History = []string{"one", "three"}
|
|
|
|
browser.Timestamp = 107
|
|
|
|
require.NoError(t, api.AddBrowser(context.TODO(), *browser))
|
|
|
|
rst, err := api.GetBrowsers(context.TODO())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, rst, 1)
|
|
|
|
require.Equal(t, browser, rst[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteBrowser(t *testing.T) {
|
|
|
|
api, cancel := setupTestAPI(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
browser := &Browser{
|
|
|
|
ID: "1",
|
|
|
|
Name: "first",
|
|
|
|
Dapp: true,
|
|
|
|
Timestamp: 10,
|
|
|
|
History: []string{"one", "two"},
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, api.AddBrowser(context.TODO(), *browser))
|
|
|
|
rst, err := api.GetBrowsers(context.TODO())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, rst, 1)
|
|
|
|
|
|
|
|
require.NoError(t, api.DeleteBrowser(context.TODO(), browser.ID))
|
|
|
|
rst, err = api.GetBrowsers(context.TODO())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, rst, 0)
|
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
require.NoError(t, api.DeleteBookmark(context.TODO(), bookmark.URL))
|
|
|
|
rst, err = api.GetBookmarks(context.TODO())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, rst, 0)
|
|
|
|
}
|