Files
Jonathan Rainville 3fa18a7151 fix(devices): change the delete installation flow to use a field (#7351)
Fixes https://github.com/status-im/status-app/issues/19991

The problem with the deletion of installation is that we sometimes sync or fetch old installations, so it re-adds the deleted installation in the list.

This fix makes it certain that if an installation was deleted, it stays deleted
2026-03-13 09:23:31 -04:00

386 lines
9.1 KiB
Go

package multidevice
import (
"testing"
bindata "github.com/status-im/migrate/v4/source/go_bindata"
"github.com/stretchr/testify/suite"
"github.com/status-im/status-go/internal/testutils"
"github.com/status-im/status-go/pkg/messaging/layers/encryption/migrations"
)
func TestSQLLitePersistenceTestSuite(t *testing.T) {
suite.Run(t, new(SQLLitePersistenceTestSuite))
}
type SQLLitePersistenceTestSuite struct {
suite.Suite
service *SQLitePersistence
}
func (s *SQLLitePersistenceTestSuite) SetupTest() {
db, err := testutils.SetupTestMemorySQLDB(testutils.NewTestDBInitializer([]*bindata.AssetSource{
{
Names: migrations.AssetNames(),
AssetFunc: migrations.Asset,
},
}))
s.Require().NoError(err)
s.service = NewSQLitePersistence(db)
}
func (s *SQLLitePersistenceTestSuite) TestAddInstallations() {
identity := []byte("alice")
installations := []*Installation{
{ID: "alice-1", Version: 1, Enabled: true},
{ID: "alice-2", Version: 2, Enabled: true},
}
addedInstallations, err := s.service.AddInstallations(
identity,
1,
installations,
true,
)
s.Require().NoError(err)
enabledInstallations, err := s.service.GetActiveInstallations(5, identity)
s.Require().NoError(err)
s.Require().Equal(installations, enabledInstallations)
s.Require().Equal(installations, addedInstallations)
}
func (s *SQLLitePersistenceTestSuite) TestAddInstallationVersions() {
identity := []byte("alice")
installations := []*Installation{
{ID: "alice-1", Version: 1, Enabled: true},
}
_, err := s.service.AddInstallations(
identity,
1,
installations,
true,
)
s.Require().NoError(err)
enabledInstallations, err := s.service.GetActiveInstallations(5, identity)
s.Require().NoError(err)
s.Require().Equal(installations, enabledInstallations)
installationsWithDowngradedVersion := []*Installation{
{ID: "alice-1", Version: 0},
}
_, err = s.service.AddInstallations(
identity,
3,
installationsWithDowngradedVersion,
true,
)
s.Require().NoError(err)
enabledInstallations, err = s.service.GetActiveInstallations(5, identity)
s.Require().NoError(err)
s.Require().Equal(installations, enabledInstallations)
}
func (s *SQLLitePersistenceTestSuite) TestAddInstallationsLimit() {
identity := []byte("alice")
installations := []*Installation{
{ID: "alice-1", Version: 1},
{ID: "alice-2", Version: 2},
}
_, err := s.service.AddInstallations(
identity,
1,
installations,
true,
)
s.Require().NoError(err)
installations = []*Installation{
{ID: "alice-1", Version: 1},
{ID: "alice-3", Version: 3},
}
_, err = s.service.AddInstallations(
identity,
2,
installations,
true,
)
s.Require().NoError(err)
installations = []*Installation{
{ID: "alice-2", Version: 2, Enabled: true},
{ID: "alice-3", Version: 3, Enabled: true},
{ID: "alice-4", Version: 4, Enabled: true},
}
_, err = s.service.AddInstallations(
identity,
3,
installations,
true,
)
s.Require().NoError(err)
enabledInstallations, err := s.service.GetActiveInstallations(3, identity)
s.Require().NoError(err)
s.Require().Equal(installations, enabledInstallations)
}
func (s *SQLLitePersistenceTestSuite) TestAddInstallationsDisabled() {
identity := []byte("alice")
installations := []*Installation{
{ID: "alice-1", Version: 1},
{ID: "alice-2", Version: 2},
}
_, err := s.service.AddInstallations(
identity,
1,
installations,
false,
)
s.Require().NoError(err)
actualInstallations, err := s.service.GetActiveInstallations(3, identity)
s.Require().NoError(err)
s.Require().Nil(actualInstallations)
}
func (s *SQLLitePersistenceTestSuite) TestDisableInstallation() {
identity := []byte("alice")
installations := []*Installation{
{ID: "alice-1", Version: 1},
{ID: "alice-2", Version: 2},
}
_, err := s.service.AddInstallations(
identity,
1,
installations,
true,
)
s.Require().NoError(err)
err = s.service.DisableInstallation(identity, "alice-1")
s.Require().NoError(err)
// We add the installations again
installations = []*Installation{
{ID: "alice-1", Version: 1},
{ID: "alice-2", Version: 2},
}
addedInstallations, err := s.service.AddInstallations(
identity,
1,
installations,
true,
)
s.Require().NoError(err)
s.Require().Equal(0, len(addedInstallations))
actualInstallations, err := s.service.GetActiveInstallations(3, identity)
s.Require().NoError(err)
expected := []*Installation{{ID: "alice-2", Version: 2, Enabled: true}}
s.Require().Equal(expected, actualInstallations)
}
func (s *SQLLitePersistenceTestSuite) TestEnableInstallation() {
identity := []byte("alice")
installations := []*Installation{
{ID: "alice-1", Version: 1},
{ID: "alice-2", Version: 2},
}
_, err := s.service.AddInstallations(
identity,
1,
installations,
true,
)
s.Require().NoError(err)
err = s.service.DisableInstallation(identity, "alice-1")
s.Require().NoError(err)
actualInstallations, err := s.service.GetActiveInstallations(3, identity)
s.Require().NoError(err)
expected := []*Installation{{ID: "alice-2", Version: 2, Enabled: true}}
s.Require().Equal(expected, actualInstallations)
err = s.service.EnableInstallation(identity, "alice-1")
s.Require().NoError(err)
actualInstallations, err = s.service.GetActiveInstallations(3, identity)
s.Require().NoError(err)
expected = []*Installation{
{ID: "alice-1", Version: 1, Enabled: true},
{ID: "alice-2", Version: 2, Enabled: true},
}
s.Require().Equal(expected, actualInstallations)
}
func (s *SQLLitePersistenceTestSuite) TestDeleteInstallation() {
identity := []byte("alice")
installations := []*Installation{
{ID: "alice-1", Version: 1},
{ID: "alice-2", Version: 2},
}
_, err := s.service.AddInstallations(
identity,
1,
installations,
true,
)
s.Require().NoError(err)
err = s.service.DeleteInstallation(identity, "alice-1")
s.Require().NoError(err)
actualActiveInstallations, err := s.service.GetActiveInstallations(3, identity)
s.Require().NoError(err)
expectedActive := []*Installation{{ID: "alice-2", Version: 2, Enabled: true}}
s.Require().Equal(expectedActive, actualActiveInstallations)
actualInstallations, err := s.service.GetInstallations(identity)
s.Require().NoError(err)
expectedInstallations := []*Installation{{
ID: "alice-2",
Version: 2,
Timestamp: 1,
Enabled: true,
InstallationMetadata: &InstallationMetadata{},
}}
s.Require().ElementsMatch(expectedInstallations, actualInstallations)
}
func (s *SQLLitePersistenceTestSuite) TestAddInstallationsDoesNotRestoreDeletedInstallation() {
identity := []byte("alice")
installations := []*Installation{
{ID: "alice-1", Version: 1},
{ID: "alice-2", Version: 2},
}
_, err := s.service.AddInstallations(
identity,
1,
installations,
true,
)
s.Require().NoError(err)
err = s.service.DeleteInstallation(identity, "alice-1")
s.Require().NoError(err)
_, err = s.service.AddInstallations(
identity,
2,
[]*Installation{{ID: "alice-1", Version: 10}},
true,
)
s.Require().NoError(err)
actualInstallations, err := s.service.GetInstallations(identity)
s.Require().NoError(err)
expectedInstallations := []*Installation{{
ID: "alice-2",
Version: 2,
Timestamp: 1,
Enabled: true,
InstallationMetadata: &InstallationMetadata{},
}}
s.Require().ElementsMatch(expectedInstallations, actualInstallations)
}
func (s *SQLLitePersistenceTestSuite) TestGetInstallations() {
identity := []byte("alice")
installations := []*Installation{
{ID: "alice-1", Version: 1},
{ID: "alice-2", Version: 2},
}
_, err := s.service.AddInstallations(
identity,
1,
installations,
true,
)
s.Require().NoError(err)
err = s.service.DisableInstallation(identity, "alice-1")
s.Require().NoError(err)
actualInstallations, err := s.service.GetInstallations(identity)
s.Require().NoError(err)
emptyMetadata := &InstallationMetadata{}
expected := []*Installation{
{ID: "alice-1", Version: 1, Timestamp: 1, Enabled: false, InstallationMetadata: emptyMetadata},
{ID: "alice-2", Version: 2, Timestamp: 1, Enabled: true, InstallationMetadata: emptyMetadata},
}
s.Require().Equal(2, len(actualInstallations))
s.Require().ElementsMatch(expected, actualInstallations)
}
func (s *SQLLitePersistenceTestSuite) TestSetMetadata() {
identity := []byte("alice")
installations := []*Installation{
{ID: "alice-1", Version: 1},
{ID: "alice-2", Version: 2},
}
_, err := s.service.AddInstallations(
identity,
1,
installations,
true,
)
s.Require().NoError(err)
err = s.service.DisableInstallation(identity, "alice-1")
s.Require().NoError(err)
emptyMetadata := &InstallationMetadata{}
setMetadata := &InstallationMetadata{
Name: "a",
FCMToken: "b",
DeviceType: "c",
}
err = s.service.SetInstallationMetadata(identity, "alice-2", setMetadata)
s.Require().NoError(err)
actualInstallations, err := s.service.GetInstallations(identity)
s.Require().NoError(err)
expected := []*Installation{
{ID: "alice-1", Version: 1, Timestamp: 1, Enabled: false, InstallationMetadata: emptyMetadata},
{ID: "alice-2", Version: 2, Timestamp: 1, Enabled: true, InstallationMetadata: setMetadata},
}
s.Require().ElementsMatch(expected, actualInstallations)
}