status-go/services/gif/gif_test.go

143 lines
3.4 KiB
Go
Raw Normal View History

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"
"github.com/status-im/status-go/sqlite"
)
func setupSQLTestDb(t *testing.T) (*sql.DB, func()) {
tmpfile, err := ioutil.TempFile("", "local-notifications-tests-")
require.NoError(t, err)
db, err := appdatabase.InitializeDB(tmpfile.Name(), "local-notifications-tests", sqlite.ReducedKDFIterationsNumber)
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()) {
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
acc, err := accounts.NewDB(db)
require.NoError(t, err)
return acc, func() {
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))
}