2022-02-02 09:36:52 +00:00
|
|
|
package gif
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2022-09-27 20:27:20 +00:00
|
|
|
"github.com/status-im/status-go/sqlite"
|
2022-02-02 09:36:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupSQLTestDb(t *testing.T) (*sql.DB, func()) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "local-notifications-tests-")
|
|
|
|
require.NoError(t, err)
|
2022-09-27 20:27:20 +00:00
|
|
|
db, err := appdatabase.InitializeDB(tmpfile.Name(), "local-notifications-tests", sqlite.ReducedKDFIterationsNumber)
|
2022-02-02 09:36:52 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return db, func() {
|
|
|
|
require.NoError(t, os.Remove(tmpfile.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupTestDB(t *testing.T, db *sql.DB) (*accounts.Database, func()) {
|
2022-03-23 18:47:00 +00:00
|
|
|
acc, err := accounts.NewDB(db)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return acc, func() {
|
2022-02-02 09:36:52 +00:00
|
|
|
require.NoError(t, db.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetTenorAPIKey(t *testing.T) {
|
|
|
|
appDB, appStop := setupSQLTestDb(t)
|
|
|
|
defer appStop()
|
|
|
|
|
|
|
|
db, stop := setupTestDB(t, appDB)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
gifAPI := NewGifAPI(db)
|
|
|
|
|
|
|
|
require.NoError(t, gifAPI.SetTenorAPIKey("DU7DWZ27STB2"))
|
|
|
|
require.Equal(t, "DU7DWZ27STB2", tenorAPIKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContentWithRetry(t *testing.T) {
|
|
|
|
appDB, appStop := setupSQLTestDb(t)
|
|
|
|
defer appStop()
|
|
|
|
|
|
|
|
db, stop := setupTestDB(t, appDB)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
gifAPI := NewGifAPI(db)
|
|
|
|
|
|
|
|
require.NoError(t, gifAPI.SetTenorAPIKey(""))
|
|
|
|
require.Equal(t, "", tenorAPIKey)
|
|
|
|
|
|
|
|
gifs, err := gifAPI.GetContentWithRetry("trending?")
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, "", gifs)
|
|
|
|
|
|
|
|
require.NoError(t, gifAPI.SetTenorAPIKey("DU7DWZ27STB2"))
|
|
|
|
require.Equal(t, "DU7DWZ27STB2", tenorAPIKey)
|
|
|
|
|
|
|
|
gifs, err = gifAPI.GetContentWithRetry("trending?")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, "", gifs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFavoriteGifs(t *testing.T) {
|
|
|
|
appDB, appStop := setupSQLTestDb(t)
|
|
|
|
defer appStop()
|
|
|
|
|
|
|
|
db, stop := setupTestDB(t, appDB)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
gifAPI := NewGifAPI(db)
|
|
|
|
|
|
|
|
require.NoError(t, gifAPI.SetTenorAPIKey("DU7DWZ27STB2"))
|
|
|
|
require.Equal(t, "DU7DWZ27STB2", tenorAPIKey)
|
|
|
|
|
|
|
|
recent := map[string]interface{}{
|
|
|
|
"id": "23833142",
|
|
|
|
"title": "",
|
|
|
|
"url": "https://media.tenor.com/images/b845ae14f43883e5cd6283e705f09efb/tenor.gif",
|
|
|
|
"tinyUrl": "https://media.tenor.com/images/2067bdc0375f9606dfb9fb4d2bfaafde/tenor.gif",
|
|
|
|
"height": 498,
|
|
|
|
"isFavorite": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
newRecents := map[string]interface{}{
|
|
|
|
"items": recent,
|
|
|
|
}
|
|
|
|
inputJSON := map[string]interface{}{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "gif_setTenorAPIKey",
|
|
|
|
"params": newRecents,
|
|
|
|
}
|
|
|
|
like, _ := json.Marshal(inputJSON)
|
|
|
|
|
|
|
|
source := (json.RawMessage)(like)
|
|
|
|
|
|
|
|
require.NoError(t, gifAPI.UpdateFavoriteGifs(source))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecentGifs(t *testing.T) {
|
|
|
|
appDB, appStop := setupSQLTestDb(t)
|
|
|
|
defer appStop()
|
|
|
|
|
|
|
|
db, stop := setupTestDB(t, appDB)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
gifAPI := NewGifAPI(db)
|
|
|
|
|
|
|
|
require.NoError(t, gifAPI.SetTenorAPIKey("DU7DWZ27STB2"))
|
|
|
|
require.Equal(t, "DU7DWZ27STB2", tenorAPIKey)
|
|
|
|
|
|
|
|
recent := map[string]interface{}{
|
|
|
|
"id": "23833142",
|
|
|
|
"title": "",
|
|
|
|
"url": "https://media.tenor.com/images/b845ae14f43883e5cd6283e705f09efb/tenor.gif",
|
|
|
|
"tinyUrl": "https://media.tenor.com/images/2067bdc0375f9606dfb9fb4d2bfaafde/tenor.gif",
|
|
|
|
"height": 498,
|
|
|
|
"isFavorite": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
newRecents := map[string]interface{}{
|
|
|
|
"items": recent,
|
|
|
|
}
|
|
|
|
inputJSON := map[string]interface{}{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "gif_setTenorAPIKey",
|
|
|
|
"params": newRecents,
|
|
|
|
}
|
|
|
|
like, _ := json.Marshal(inputJSON)
|
|
|
|
|
|
|
|
source := (json.RawMessage)(like)
|
|
|
|
|
|
|
|
require.NoError(t, gifAPI.UpdateRecentGifs(source))
|
|
|
|
}
|