2022-07-05 19:49:44 +00:00
package verification
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
2023-10-02 09:28:42 +00:00
"github.com/status-im/status-go/appdatabase"
2022-07-05 19:49:44 +00:00
"github.com/status-im/status-go/protocol/sqlite"
2023-10-02 09:28:42 +00:00
"github.com/status-im/status-go/t/helpers"
2022-07-05 19:49:44 +00:00
)
func TestPersistenceSuite ( t * testing . T ) {
suite . Run ( t , new ( PersistenceSuite ) )
}
type PersistenceSuite struct {
suite . Suite
db * Persistence
}
func ( s * PersistenceSuite ) SetupTest ( ) {
2023-10-02 09:28:42 +00:00
db , err := helpers . SetupTestMemorySQLDB ( appdatabase . DbInitializer { } )
s . Require ( ) . NoError ( err )
err = sqlite . Migrate ( db )
s . Require ( ) . NoError ( err )
2022-07-05 19:49:44 +00:00
s . db = & Persistence { db : db }
}
func ( s * PersistenceSuite ) TestVerificationRequests ( ) {
request := & Request {
2022-10-24 11:33:47 +00:00
ID : "0xabc" ,
2022-07-05 19:49:44 +00:00
From : "0x01" ,
To : "0x02" ,
Challenge : "ABC" ,
Response : "" ,
RequestedAt : uint64 ( time . Unix ( 1000 , 0 ) . Unix ( ) ) ,
RepliedAt : uint64 ( time . Unix ( 0 , 0 ) . Unix ( ) ) ,
RequestStatus : RequestStatusPENDING ,
}
// Test Insert
err := s . db . SaveVerificationRequest ( request )
2022-10-24 11:33:47 +00:00
s . Require ( ) . NoError ( err )
2022-07-05 19:49:44 +00:00
// Test Found
2022-08-31 14:41:58 +00:00
dbRequest , err := s . db . GetVerificationRequest ( "0xabc" )
2022-10-24 11:33:47 +00:00
s . Require ( ) . NoError ( err )
s . Require ( ) . Equal ( request , dbRequest )
2022-07-05 19:49:44 +00:00
// Test Not Found
2022-10-24 11:33:47 +00:00
dbRequest2 , err := s . db . GetVerificationRequest ( "0xdef" )
s . Require ( ) . NoError ( err )
s . Require ( ) . Nil ( dbRequest2 )
2022-07-05 19:49:44 +00:00
// Test Accept
2022-10-24 11:33:47 +00:00
err = s . db . AcceptContactVerificationRequest ( "0xabc" , "XYZ" )
s . Require ( ) . NoError ( err )
2022-07-05 19:49:44 +00:00
2022-10-24 11:33:47 +00:00
dbRequest , err = s . db . GetVerificationRequest ( "0xabc" )
s . Require ( ) . NoError ( err )
s . Require ( ) . NotNil ( dbRequest )
s . Require ( ) . Equal ( RequestStatusACCEPTED , dbRequest . RequestStatus )
s . Require ( ) . Equal ( "XYZ" , dbRequest . Response )
s . Require ( ) . NotEqual ( time . Unix ( 0 , 0 ) , dbRequest . RepliedAt )
2022-07-05 19:49:44 +00:00
// Test Decline
request = & Request {
2022-10-24 11:33:47 +00:00
ID : "0x01" ,
2022-07-05 19:49:44 +00:00
From : "0x03" ,
To : "0x02" ,
Challenge : "ABC" ,
Response : "" ,
RequestedAt : uint64 ( time . Unix ( 1000 , 0 ) . Unix ( ) ) ,
RepliedAt : uint64 ( time . Unix ( 0 , 0 ) . Unix ( ) ) ,
RequestStatus : RequestStatusPENDING ,
}
err = s . db . SaveVerificationRequest ( request )
s . NoError ( err )
2022-10-24 11:33:47 +00:00
err = s . db . DeclineContactVerificationRequest ( "0x01" )
2022-07-05 19:49:44 +00:00
s . NoError ( err )
2022-10-24 11:33:47 +00:00
dbRequest , err = s . db . GetVerificationRequest ( "0x01" )
2022-07-05 19:49:44 +00:00
s . NoError ( err )
s . Equal ( RequestStatusDECLINED , dbRequest . RequestStatus )
s . NotEqual ( time . Unix ( 0 , 0 ) , dbRequest . RepliedAt )
}
func ( s * PersistenceSuite ) TestTrustStatus ( ) {
err := s . db . SetTrustStatus ( "0x01" , TrustStatusTRUSTED , 1000 )
s . NoError ( err )
err = s . db . SetTrustStatus ( "0x02" , TrustStatusUNKNOWN , 1001 )
s . NoError ( err )
trustStatus , err := s . db . GetTrustStatus ( "0x01" )
s . NoError ( err )
s . Equal ( TrustStatusTRUSTED , trustStatus )
err = s . db . SetTrustStatus ( "0x01" , TrustStatusUNTRUSTWORTHY , 1000 )
s . NoError ( err )
trustStatus , err = s . db . GetTrustStatus ( "0x01" )
s . NoError ( err )
s . Equal ( TrustStatusUNTRUSTWORTHY , trustStatus )
trustStatus , err = s . db . GetTrustStatus ( "0x03" )
s . NoError ( err )
s . Equal ( TrustStatusUNKNOWN , trustStatus )
statuses , err := s . db . GetAllTrustStatus ( )
s . NoError ( err )
s . Len ( statuses , 2 )
s . Equal ( TrustStatusUNTRUSTWORTHY , statuses [ "0x01" ] )
s . Equal ( TrustStatusUNKNOWN , statuses [ "0x02" ] )
// Upsert
success , err := s . db . UpsertTrustStatus ( "0x03" , TrustStatusTRUSTED , 1000 )
s . NoError ( err )
s . True ( success )
trustStatus , err = s . db . GetTrustStatus ( "0x03" )
s . NoError ( err )
s . Equal ( TrustStatusTRUSTED , trustStatus )
success , err = s . db . UpsertTrustStatus ( "0x03" , TrustStatusUNKNOWN , 500 ) // should not be successful without error, because of being older than latest value in the DB
s . NoError ( err )
s . False ( success )
trustStatus , err = s . db . GetTrustStatus ( "0x03" )
s . NoError ( err )
s . Equal ( TrustStatusTRUSTED , trustStatus )
success , err = s . db . UpsertTrustStatus ( "0x03" , TrustStatusUNKNOWN , 1500 )
s . NoError ( err )
s . False ( success )
trustStatus , err = s . db . GetTrustStatus ( "0x03" )
s . NoError ( err )
s . Equal ( TrustStatusUNKNOWN , trustStatus )
}