github.com/status-im/status-go@v1.1.0/protocol/encryption/multidevice/persistence_test.go (about)

     1  package multidevice
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/suite"
     7  
     8  	"github.com/status-im/status-go/appdatabase"
     9  	"github.com/status-im/status-go/protocol/sqlite"
    10  	"github.com/status-im/status-go/t/helpers"
    11  )
    12  
    13  func TestSQLLitePersistenceTestSuite(t *testing.T) {
    14  	suite.Run(t, new(SQLLitePersistenceTestSuite))
    15  }
    16  
    17  type SQLLitePersistenceTestSuite struct {
    18  	suite.Suite
    19  	service *sqlitePersistence
    20  }
    21  
    22  func (s *SQLLitePersistenceTestSuite) SetupTest() {
    23  	db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
    24  	s.Require().NoError(err)
    25  	err = sqlite.Migrate(db)
    26  	s.Require().NoError(err)
    27  
    28  	s.service = newSQLitePersistence(db)
    29  }
    30  
    31  func (s *SQLLitePersistenceTestSuite) TestAddInstallations() {
    32  	identity := []byte("alice")
    33  	installations := []*Installation{
    34  		{ID: "alice-1", Version: 1, Enabled: true},
    35  		{ID: "alice-2", Version: 2, Enabled: true},
    36  	}
    37  	addedInstallations, err := s.service.AddInstallations(
    38  		identity,
    39  		1,
    40  		installations,
    41  		true,
    42  	)
    43  	s.Require().NoError(err)
    44  
    45  	enabledInstallations, err := s.service.GetActiveInstallations(5, identity)
    46  	s.Require().NoError(err)
    47  
    48  	s.Require().Equal(installations, enabledInstallations)
    49  	s.Require().Equal(installations, addedInstallations)
    50  }
    51  
    52  func (s *SQLLitePersistenceTestSuite) TestAddInstallationVersions() {
    53  	identity := []byte("alice")
    54  	installations := []*Installation{
    55  		{ID: "alice-1", Version: 1, Enabled: true},
    56  	}
    57  	_, err := s.service.AddInstallations(
    58  		identity,
    59  		1,
    60  		installations,
    61  		true,
    62  	)
    63  
    64  	s.Require().NoError(err)
    65  
    66  	enabledInstallations, err := s.service.GetActiveInstallations(5, identity)
    67  	s.Require().NoError(err)
    68  
    69  	s.Require().Equal(installations, enabledInstallations)
    70  
    71  	installationsWithDowngradedVersion := []*Installation{
    72  		{ID: "alice-1", Version: 0},
    73  	}
    74  
    75  	_, err = s.service.AddInstallations(
    76  		identity,
    77  		3,
    78  		installationsWithDowngradedVersion,
    79  		true,
    80  	)
    81  	s.Require().NoError(err)
    82  
    83  	enabledInstallations, err = s.service.GetActiveInstallations(5, identity)
    84  	s.Require().NoError(err)
    85  	s.Require().Equal(installations, enabledInstallations)
    86  }
    87  
    88  func (s *SQLLitePersistenceTestSuite) TestAddInstallationsLimit() {
    89  	identity := []byte("alice")
    90  
    91  	installations := []*Installation{
    92  		{ID: "alice-1", Version: 1},
    93  		{ID: "alice-2", Version: 2},
    94  	}
    95  
    96  	_, err := s.service.AddInstallations(
    97  		identity,
    98  		1,
    99  		installations,
   100  		true,
   101  	)
   102  	s.Require().NoError(err)
   103  
   104  	installations = []*Installation{
   105  		{ID: "alice-1", Version: 1},
   106  		{ID: "alice-3", Version: 3},
   107  	}
   108  
   109  	_, err = s.service.AddInstallations(
   110  		identity,
   111  		2,
   112  		installations,
   113  		true,
   114  	)
   115  	s.Require().NoError(err)
   116  
   117  	installations = []*Installation{
   118  		{ID: "alice-2", Version: 2, Enabled: true},
   119  		{ID: "alice-3", Version: 3, Enabled: true},
   120  		{ID: "alice-4", Version: 4, Enabled: true},
   121  	}
   122  
   123  	_, err = s.service.AddInstallations(
   124  		identity,
   125  		3,
   126  		installations,
   127  		true,
   128  	)
   129  	s.Require().NoError(err)
   130  
   131  	enabledInstallations, err := s.service.GetActiveInstallations(3, identity)
   132  	s.Require().NoError(err)
   133  
   134  	s.Require().Equal(installations, enabledInstallations)
   135  }
   136  
   137  func (s *SQLLitePersistenceTestSuite) TestAddInstallationsDisabled() {
   138  	identity := []byte("alice")
   139  
   140  	installations := []*Installation{
   141  		{ID: "alice-1", Version: 1},
   142  		{ID: "alice-2", Version: 2},
   143  	}
   144  
   145  	_, err := s.service.AddInstallations(
   146  		identity,
   147  		1,
   148  		installations,
   149  		false,
   150  	)
   151  	s.Require().NoError(err)
   152  
   153  	actualInstallations, err := s.service.GetActiveInstallations(3, identity)
   154  	s.Require().NoError(err)
   155  
   156  	s.Require().Nil(actualInstallations)
   157  }
   158  
   159  func (s *SQLLitePersistenceTestSuite) TestDisableInstallation() {
   160  	identity := []byte("alice")
   161  
   162  	installations := []*Installation{
   163  		{ID: "alice-1", Version: 1},
   164  		{ID: "alice-2", Version: 2},
   165  	}
   166  
   167  	_, err := s.service.AddInstallations(
   168  		identity,
   169  		1,
   170  		installations,
   171  		true,
   172  	)
   173  	s.Require().NoError(err)
   174  
   175  	err = s.service.DisableInstallation(identity, "alice-1")
   176  	s.Require().NoError(err)
   177  
   178  	// We add the installations again
   179  	installations = []*Installation{
   180  		{ID: "alice-1", Version: 1},
   181  		{ID: "alice-2", Version: 2},
   182  	}
   183  
   184  	addedInstallations, err := s.service.AddInstallations(
   185  		identity,
   186  		1,
   187  		installations,
   188  		true,
   189  	)
   190  	s.Require().NoError(err)
   191  	s.Require().Equal(0, len(addedInstallations))
   192  
   193  	actualInstallations, err := s.service.GetActiveInstallations(3, identity)
   194  	s.Require().NoError(err)
   195  
   196  	expected := []*Installation{{ID: "alice-2", Version: 2, Enabled: true}}
   197  	s.Require().Equal(expected, actualInstallations)
   198  }
   199  
   200  func (s *SQLLitePersistenceTestSuite) TestEnableInstallation() {
   201  	identity := []byte("alice")
   202  
   203  	installations := []*Installation{
   204  		{ID: "alice-1", Version: 1},
   205  		{ID: "alice-2", Version: 2},
   206  	}
   207  
   208  	_, err := s.service.AddInstallations(
   209  		identity,
   210  		1,
   211  		installations,
   212  		true,
   213  	)
   214  	s.Require().NoError(err)
   215  
   216  	err = s.service.DisableInstallation(identity, "alice-1")
   217  	s.Require().NoError(err)
   218  
   219  	actualInstallations, err := s.service.GetActiveInstallations(3, identity)
   220  	s.Require().NoError(err)
   221  
   222  	expected := []*Installation{{ID: "alice-2", Version: 2, Enabled: true}}
   223  	s.Require().Equal(expected, actualInstallations)
   224  
   225  	err = s.service.EnableInstallation(identity, "alice-1")
   226  	s.Require().NoError(err)
   227  
   228  	actualInstallations, err = s.service.GetActiveInstallations(3, identity)
   229  	s.Require().NoError(err)
   230  
   231  	expected = []*Installation{
   232  		{ID: "alice-1", Version: 1, Enabled: true},
   233  		{ID: "alice-2", Version: 2, Enabled: true},
   234  	}
   235  	s.Require().Equal(expected, actualInstallations)
   236  }
   237  
   238  func (s *SQLLitePersistenceTestSuite) TestGetInstallations() {
   239  	identity := []byte("alice")
   240  
   241  	installations := []*Installation{
   242  		{ID: "alice-1", Version: 1},
   243  		{ID: "alice-2", Version: 2},
   244  	}
   245  
   246  	_, err := s.service.AddInstallations(
   247  		identity,
   248  		1,
   249  		installations,
   250  		true,
   251  	)
   252  	s.Require().NoError(err)
   253  
   254  	err = s.service.DisableInstallation(identity, "alice-1")
   255  	s.Require().NoError(err)
   256  
   257  	actualInstallations, err := s.service.GetInstallations(identity)
   258  	s.Require().NoError(err)
   259  
   260  	emptyMetadata := &InstallationMetadata{}
   261  
   262  	expected := []*Installation{
   263  		{ID: "alice-1", Version: 1, Timestamp: 1, Enabled: false, InstallationMetadata: emptyMetadata},
   264  		{ID: "alice-2", Version: 2, Timestamp: 1, Enabled: true, InstallationMetadata: emptyMetadata},
   265  	}
   266  	s.Require().Equal(2, len(actualInstallations))
   267  	s.Require().ElementsMatch(expected, actualInstallations)
   268  }
   269  
   270  func (s *SQLLitePersistenceTestSuite) TestSetMetadata() {
   271  	identity := []byte("alice")
   272  
   273  	installations := []*Installation{
   274  		{ID: "alice-1", Version: 1},
   275  		{ID: "alice-2", Version: 2},
   276  	}
   277  
   278  	_, err := s.service.AddInstallations(
   279  		identity,
   280  		1,
   281  		installations,
   282  		true,
   283  	)
   284  	s.Require().NoError(err)
   285  
   286  	err = s.service.DisableInstallation(identity, "alice-1")
   287  	s.Require().NoError(err)
   288  
   289  	emptyMetadata := &InstallationMetadata{}
   290  	setMetadata := &InstallationMetadata{
   291  		Name:       "a",
   292  		FCMToken:   "b",
   293  		DeviceType: "c",
   294  	}
   295  
   296  	err = s.service.SetInstallationMetadata(identity, "alice-2", setMetadata)
   297  	s.Require().NoError(err)
   298  
   299  	actualInstallations, err := s.service.GetInstallations(identity)
   300  	s.Require().NoError(err)
   301  
   302  	expected := []*Installation{
   303  		{ID: "alice-1", Version: 1, Timestamp: 1, Enabled: false, InstallationMetadata: emptyMetadata},
   304  		{ID: "alice-2", Version: 2, Timestamp: 1, Enabled: true, InstallationMetadata: setMetadata},
   305  	}
   306  	s.Require().ElementsMatch(expected, actualInstallations)
   307  }