2023-11-19 17:29:17 +00:00
|
|
|
package walletconnect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/t/helpers"
|
|
|
|
"github.com/status-im/status-go/walletdatabase"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupTestDB(t *testing.T) (db *sql.DB, close func()) {
|
|
|
|
db, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
return db, func() {
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
// generateTestData generates alternative disconnected and active sessions starting with the active one
|
2023-11-19 17:29:17 +00:00
|
|
|
// timestamps start with 1234567890
|
2023-12-13 14:05:55 +00:00
|
|
|
func generateTestData(count int) []DbSession {
|
|
|
|
res := make([]DbSession, count)
|
|
|
|
j := 0
|
2023-11-19 17:29:17 +00:00
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
strI := strconv.Itoa(i)
|
2023-12-13 14:05:55 +00:00
|
|
|
if i%4 == 0 {
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
strJ := strconv.Itoa(j)
|
|
|
|
res[i] = DbSession{
|
|
|
|
Topic: Topic(strI + "aaaaaa1234567890"),
|
|
|
|
PairingTopic: Topic(strJ + "bbbbbb1234567890"),
|
|
|
|
Expiry: 1234567890 + int64(i),
|
|
|
|
Active: (i % 2) == 0,
|
|
|
|
DappName: "TestApp" + strI,
|
|
|
|
DappURL: "https://test.url/" + strI,
|
|
|
|
DappDescription: "Test Description" + strI,
|
|
|
|
DappIcon: "https://test.icon" + strI,
|
|
|
|
DappVerifyURL: "https://test.verify.url/" + strI,
|
|
|
|
DappPublicKey: strI + "1234567890",
|
2023-11-19 17:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
func insertTestData(t *testing.T, db *sql.DB, entries []DbSession) {
|
2023-11-19 17:29:17 +00:00
|
|
|
for _, entry := range entries {
|
2023-12-13 14:05:55 +00:00
|
|
|
err := UpsertSession(db, entry)
|
2023-11-19 17:29:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
func TestInsertUpdateAndGetSession(t *testing.T) {
|
2023-11-19 17:29:17 +00:00
|
|
|
db, close := setupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
entry := generateTestData(1)[0]
|
2023-12-13 14:05:55 +00:00
|
|
|
err := UpsertSession(db, entry)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
retrievedSession, err := GetSessionByTopic(db, entry.Topic)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, entry, *retrievedSession)
|
|
|
|
|
|
|
|
entry.Active = false
|
|
|
|
entry.Expiry = 1111111111
|
|
|
|
err = UpsertSession(db, entry)
|
|
|
|
|
2023-11-19 17:29:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
retrievedSession, err = GetSessionByTopic(db, entry.Topic)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, entry, *retrievedSession)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInsertAndGetSessionsByPairingTopic(t *testing.T) {
|
|
|
|
db, close := setupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
generatedSessions := generateTestData(10)
|
|
|
|
for _, session := range generatedSessions {
|
|
|
|
err := UpsertSession(db, session)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
retrievedSessions, err := GetSessionsByPairingTopic(db, generatedSessions[4].Topic)
|
2023-11-19 17:29:17 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-13 14:05:55 +00:00
|
|
|
require.Equal(t, 0, len(retrievedSessions))
|
2023-11-19 17:29:17 +00:00
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
retrievedSessions, err = GetSessionsByPairingTopic(db, generatedSessions[4].PairingTopic)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 4, len(retrievedSessions))
|
|
|
|
|
|
|
|
for i := 4; i < 8; i++ {
|
|
|
|
found := false
|
|
|
|
for _, session := range retrievedSessions {
|
|
|
|
if session.Topic == generatedSessions[i].Topic {
|
|
|
|
found = true
|
|
|
|
require.Equal(t, generatedSessions[i], session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.True(t, found)
|
|
|
|
}
|
2023-11-19 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
func TestChangeSessionState(t *testing.T) {
|
2023-11-26 15:50:12 +00:00
|
|
|
db, close := setupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
entry := generateTestData(1)[0]
|
2023-12-13 14:05:55 +00:00
|
|
|
err := UpsertSession(db, entry)
|
2023-11-26 15:50:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
err = ChangeSessionState(db, entry.Topic, false)
|
2023-11-26 15:50:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
retrievedSession, err := GetSessionByTopic(db, entry.Topic)
|
2023-11-26 15:50:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
require.Equal(t, false, retrievedSession.Active)
|
2023-11-26 15:50:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-19 17:29:17 +00:00
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
db, close := setupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
entries := generateTestData(3)
|
|
|
|
insertTestData(t, db, entries)
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
retrievedSession, err := GetSessionByTopic(db, entries[1].Topic)
|
2023-11-19 17:29:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
require.Equal(t, entries[1], *retrievedSession)
|
2023-11-19 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
func TestGetActiveSessions(t *testing.T) {
|
2023-11-19 17:29:17 +00:00
|
|
|
db, close := setupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
// insert two disconnected and three active sessions
|
2023-11-19 17:29:17 +00:00
|
|
|
entries := generateTestData(5)
|
|
|
|
insertTestData(t, db, entries)
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
activeSessions, err := GetActiveSessions(db, 1234567892)
|
2023-11-19 17:29:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
require.Equal(t, 2, len(activeSessions))
|
2023-11-19 17:29:17 +00:00
|
|
|
// Expect newest on top
|
2023-12-13 14:05:55 +00:00
|
|
|
require.Equal(t, entries[4], activeSessions[0])
|
|
|
|
require.Equal(t, entries[2], activeSessions[1])
|
2023-11-19 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
// func TestHasActivePairings(t *testing.T) {
|
|
|
|
// db, close := setupTestDB(t)
|
|
|
|
// defer close()
|
2023-11-19 17:29:17 +00:00
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
// // insert one disconnected and two active pairing
|
|
|
|
// entries := generateTestData(2)
|
|
|
|
// insertTestData(t, db, entries)
|
2023-11-19 17:29:17 +00:00
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
// hasActivePairings, err := HasActivePairings(db, 1234567890)
|
|
|
|
// require.NoError(t, err)
|
|
|
|
// require.True(t, hasActivePairings)
|
2023-11-19 17:29:17 +00:00
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
// hasActivePairings, err = HasActivePairings(db, 1234567891)
|
|
|
|
// require.NoError(t, err)
|
|
|
|
// require.False(t, hasActivePairings)
|
|
|
|
// }
|