github.com/status-im/status-go@v1.1.0/protocol/persistence_profile_showcase_test.go (about)

     1  package protocol
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/suite"
     8  
     9  	"github.com/status-im/status-go/protocol/identity"
    10  )
    11  
    12  func TestProfileShowcasePersistenceSuite(t *testing.T) {
    13  	suite.Run(t, new(TestProfileShowcasePersistence))
    14  }
    15  
    16  type TestProfileShowcasePersistence struct {
    17  	suite.Suite
    18  }
    19  
    20  func (s *TestProfileShowcasePersistence) TestProfileShowcasePreferences() {
    21  	db, err := openTestDB()
    22  	s.Require().NoError(err)
    23  	persistence := newSQLitePersistence(db)
    24  
    25  	preferences := &identity.ProfileShowcasePreferences{
    26  		Communities: []*identity.ProfileShowcaseCommunityPreference{
    27  			&identity.ProfileShowcaseCommunityPreference{
    28  				CommunityID:        "0x32433445133424",
    29  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityEveryone,
    30  				Order:              0,
    31  			},
    32  		},
    33  		Accounts: []*identity.ProfileShowcaseAccountPreference{
    34  			&identity.ProfileShowcaseAccountPreference{
    35  				Address:            "0x0000000000000000000000000032433445133422",
    36  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityEveryone,
    37  				Order:              0,
    38  			},
    39  			&identity.ProfileShowcaseAccountPreference{
    40  				Address:            "0x0000000000000000000000000032433445133424",
    41  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityContacts,
    42  				Order:              1,
    43  			},
    44  		},
    45  		Collectibles: []*identity.ProfileShowcaseCollectiblePreference{
    46  			&identity.ProfileShowcaseCollectiblePreference{
    47  				ContractAddress:    "0x12378534257568678487683576",
    48  				ChainID:            11155111,
    49  				TokenID:            "123213895929994903",
    50  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityEveryone,
    51  				Order:              0,
    52  			},
    53  		},
    54  		VerifiedTokens: []*identity.ProfileShowcaseVerifiedTokenPreference{
    55  			&identity.ProfileShowcaseVerifiedTokenPreference{
    56  				Symbol:             "ETH",
    57  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityEveryone,
    58  				Order:              1,
    59  			},
    60  			&identity.ProfileShowcaseVerifiedTokenPreference{
    61  				Symbol:             "DAI",
    62  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityIDVerifiedContacts,
    63  				Order:              2,
    64  			},
    65  			&identity.ProfileShowcaseVerifiedTokenPreference{
    66  				Symbol:             "SNT",
    67  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityNoOne,
    68  				Order:              3,
    69  			},
    70  		},
    71  		UnverifiedTokens: []*identity.ProfileShowcaseUnverifiedTokenPreference{
    72  			&identity.ProfileShowcaseUnverifiedTokenPreference{
    73  				ContractAddress:    "0x454525452023452",
    74  				ChainID:            1,
    75  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityEveryone,
    76  				Order:              0,
    77  			},
    78  			&identity.ProfileShowcaseUnverifiedTokenPreference{
    79  				ContractAddress:    "0x12312323323233",
    80  				ChainID:            11155111,
    81  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityContacts,
    82  				Order:              1,
    83  			},
    84  		},
    85  		SocialLinks: []*identity.ProfileShowcaseSocialLinkPreference{
    86  			&identity.ProfileShowcaseSocialLinkPreference{
    87  				Text:               identity.TwitterID,
    88  				URL:                "https://twitter.com/ethstatus",
    89  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityContacts,
    90  				Order:              1,
    91  			},
    92  			&identity.ProfileShowcaseSocialLinkPreference{
    93  				Text:               identity.TwitterID,
    94  				URL:                "https://twitter.com/StatusIMBlog",
    95  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityIDVerifiedContacts,
    96  				Order:              2,
    97  			},
    98  			&identity.ProfileShowcaseSocialLinkPreference{
    99  				Text:               identity.GithubID,
   100  				URL:                "https://github.com/status-im",
   101  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityContacts,
   102  				Order:              3,
   103  			},
   104  		},
   105  	}
   106  
   107  	err = persistence.SaveProfileShowcasePreferences(preferences)
   108  	s.Require().NoError(err)
   109  
   110  	preferencesBack, err := persistence.GetProfileShowcasePreferences()
   111  	s.Require().NoError(err)
   112  	s.Require().True(reflect.DeepEqual(preferences, preferencesBack))
   113  
   114  	newPreferences := &identity.ProfileShowcasePreferences{
   115  		Communities: []*identity.ProfileShowcaseCommunityPreference{
   116  			&identity.ProfileShowcaseCommunityPreference{
   117  				CommunityID:        "0x32433445133424",
   118  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityContacts,
   119  				Order:              0,
   120  			},
   121  		},
   122  		Accounts: []*identity.ProfileShowcaseAccountPreference{
   123  			&identity.ProfileShowcaseAccountPreference{
   124  				Address:            "0x0000000000000000000000000032433445133422",
   125  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityContacts,
   126  				Order:              0,
   127  			},
   128  		},
   129  		Collectibles: []*identity.ProfileShowcaseCollectiblePreference{},
   130  		VerifiedTokens: []*identity.ProfileShowcaseVerifiedTokenPreference{
   131  			&identity.ProfileShowcaseVerifiedTokenPreference{
   132  				Symbol:             "ETH",
   133  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityContacts,
   134  				Order:              1,
   135  			},
   136  		},
   137  		UnverifiedTokens: []*identity.ProfileShowcaseUnverifiedTokenPreference{
   138  			&identity.ProfileShowcaseUnverifiedTokenPreference{
   139  				ContractAddress:    "0x12312323323233",
   140  				ChainID:            11155111,
   141  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityEveryone,
   142  				Order:              0,
   143  			},
   144  		},
   145  		SocialLinks: []*identity.ProfileShowcaseSocialLinkPreference{
   146  			&identity.ProfileShowcaseSocialLinkPreference{
   147  				Text:               identity.TwitterID,
   148  				URL:                "https://twitter.com/ethstatus",
   149  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityEveryone,
   150  				Order:              1,
   151  			},
   152  		},
   153  	}
   154  
   155  	err = persistence.SaveProfileShowcasePreferences(newPreferences)
   156  	s.Require().NoError(err)
   157  
   158  	preferencesBack, err = persistence.GetProfileShowcasePreferences()
   159  	s.Require().NoError(err)
   160  	s.Require().True(reflect.DeepEqual(newPreferences, preferencesBack))
   161  }
   162  
   163  func (s *TestProfileShowcasePersistence) TestProfileShowcaseContacts() {
   164  	db, err := openTestDB()
   165  	s.Require().NoError(err)
   166  	persistence := newSQLitePersistence(db)
   167  
   168  	showcase1 := &identity.ProfileShowcase{
   169  		ContactID: "contact_1",
   170  		Communities: []*identity.ProfileShowcaseCommunity{
   171  			&identity.ProfileShowcaseCommunity{
   172  				CommunityID: "0x012312234234234",
   173  				Order:       6,
   174  			},
   175  			&identity.ProfileShowcaseCommunity{
   176  				CommunityID: "0x04523233466753",
   177  				Order:       7,
   178  			},
   179  		},
   180  		Accounts: []*identity.ProfileShowcaseAccount{
   181  			&identity.ProfileShowcaseAccount{
   182  				ContactID: "contact_1",
   183  				Address:   "0x32433445133424",
   184  				Name:      "Status Account",
   185  				ColorID:   "blue",
   186  				Emoji:     "-_-",
   187  				Order:     0,
   188  			},
   189  			&identity.ProfileShowcaseAccount{
   190  				ContactID: "contact_1",
   191  				Address:   "0x0000000000000000000000000032433445133424",
   192  				Name:      "Money Box",
   193  				ColorID:   "red",
   194  				Emoji:     ":o)",
   195  				Order:     1,
   196  			},
   197  		},
   198  		Collectibles: []*identity.ProfileShowcaseCollectible{
   199  			&identity.ProfileShowcaseCollectible{
   200  				ContractAddress: "0x12378534257568678487683576",
   201  				ChainID:         1,
   202  				TokenID:         "123213895929994903",
   203  				Order:           0,
   204  			},
   205  		},
   206  		VerifiedTokens: []*identity.ProfileShowcaseVerifiedToken{
   207  			&identity.ProfileShowcaseVerifiedToken{
   208  				Symbol: "ETH",
   209  				Order:  1,
   210  			},
   211  			&identity.ProfileShowcaseVerifiedToken{
   212  				Symbol: "DAI",
   213  				Order:  2,
   214  			},
   215  			&identity.ProfileShowcaseVerifiedToken{
   216  				Symbol: "SNT",
   217  				Order:  3,
   218  			},
   219  		},
   220  		UnverifiedTokens: []*identity.ProfileShowcaseUnverifiedToken{
   221  			&identity.ProfileShowcaseUnverifiedToken{
   222  				ContractAddress: "0x454525452023452",
   223  				ChainID:         1,
   224  				Order:           0,
   225  			},
   226  			&identity.ProfileShowcaseUnverifiedToken{
   227  				ContractAddress: "0x12312323323233",
   228  				ChainID:         11155111,
   229  				Order:           1,
   230  			},
   231  		},
   232  		SocialLinks: []*identity.ProfileShowcaseSocialLink{
   233  			&identity.ProfileShowcaseSocialLink{
   234  				URL:   "https://status.app/",
   235  				Text:  "Status",
   236  				Order: 1,
   237  			},
   238  			&identity.ProfileShowcaseSocialLink{
   239  				URL:   "https://github.com/status-im",
   240  				Text:  "Github",
   241  				Order: 2,
   242  			},
   243  		},
   244  	}
   245  	err = persistence.SaveProfileShowcaseForContact(showcase1)
   246  	s.Require().NoError(err)
   247  
   248  	showcase2 := &identity.ProfileShowcase{
   249  		ContactID: "contact_2",
   250  		Communities: []*identity.ProfileShowcaseCommunity{
   251  			&identity.ProfileShowcaseCommunity{
   252  				CommunityID: "0x012312234234234", // same id to check query
   253  				Order:       3,
   254  			},
   255  			&identity.ProfileShowcaseCommunity{
   256  				CommunityID: "0x096783478384593",
   257  				Order:       7,
   258  			},
   259  		},
   260  		Collectibles: []*identity.ProfileShowcaseCollectible{
   261  			&identity.ProfileShowcaseCollectible{
   262  				ContractAddress: "0x12378534257568678487683576",
   263  				ChainID:         1,
   264  				TokenID:         "123213895929994903",
   265  				Order:           1,
   266  			},
   267  		},
   268  	}
   269  	err = persistence.SaveProfileShowcaseForContact(showcase2)
   270  	s.Require().NoError(err)
   271  
   272  	showcase1Back, err := persistence.GetProfileShowcaseForContact("contact_1")
   273  	s.Require().NoError(err)
   274  
   275  	s.Require().Equal(len(showcase1.Communities), len(showcase1Back.Communities))
   276  	for i := 0; i < len(showcase1.Communities); i++ {
   277  		s.Require().Equal(*showcase1.Communities[i], *showcase1Back.Communities[i])
   278  	}
   279  	s.Require().Equal(len(showcase1.Accounts), len(showcase1Back.Accounts))
   280  	for i := 0; i < len(showcase1.Accounts); i++ {
   281  		s.Require().Equal(*showcase1.Accounts[i], *showcase1Back.Accounts[i])
   282  	}
   283  	s.Require().Equal(len(showcase1.Collectibles), len(showcase1Back.Collectibles))
   284  	for i := 0; i < len(showcase1.Collectibles); i++ {
   285  		s.Require().Equal(*showcase1.Collectibles[i], *showcase1Back.Collectibles[i])
   286  	}
   287  	s.Require().Equal(len(showcase1.VerifiedTokens), len(showcase1Back.VerifiedTokens))
   288  	for i := 0; i < len(showcase1.VerifiedTokens); i++ {
   289  		s.Require().Equal(*showcase1.VerifiedTokens[i], *showcase1Back.VerifiedTokens[i])
   290  	}
   291  	s.Require().Equal(len(showcase1.UnverifiedTokens), len(showcase1Back.UnverifiedTokens))
   292  	for i := 0; i < len(showcase1.UnverifiedTokens); i++ {
   293  		s.Require().Equal(*showcase1.UnverifiedTokens[i], *showcase1Back.UnverifiedTokens[i])
   294  	}
   295  	s.Require().Equal(len(showcase1.SocialLinks), len(showcase1Back.SocialLinks))
   296  	for i := 0; i < len(showcase1.SocialLinks); i++ {
   297  		s.Require().Equal(*showcase1.SocialLinks[i], *showcase1Back.SocialLinks[i])
   298  	}
   299  
   300  	showcase2Back, err := persistence.GetProfileShowcaseForContact("contact_2")
   301  	s.Require().NoError(err)
   302  
   303  	s.Require().Equal(len(showcase2.Communities), len(showcase2Back.Communities))
   304  	s.Require().Equal(*showcase2.Communities[0], *showcase2Back.Communities[0])
   305  	s.Require().Equal(*showcase2.Communities[1], *showcase2Back.Communities[1])
   306  	s.Require().Equal(len(showcase2.Collectibles), len(showcase2Back.Collectibles))
   307  	s.Require().Equal(*showcase2.Collectibles[0], *showcase2Back.Collectibles[0])
   308  	s.Require().Equal(0, len(showcase2Back.Accounts))
   309  	s.Require().Equal(0, len(showcase2Back.VerifiedTokens))
   310  	s.Require().Equal(0, len(showcase2Back.UnverifiedTokens))
   311  	s.Require().Equal(0, len(showcase2Back.SocialLinks))
   312  }
   313  
   314  func (s *TestProfileShowcasePersistence) TestFetchingProfileShowcaseAccountsByAddress() {
   315  	db, err := openTestDB()
   316  	s.Require().NoError(err)
   317  	persistence := newSQLitePersistence(db)
   318  
   319  	conatacts := []*Contact{
   320  		&Contact{
   321  			ID: "contact_1",
   322  		},
   323  		&Contact{
   324  			ID: "contact_2",
   325  		},
   326  		&Contact{
   327  			ID: "contact_3",
   328  		},
   329  	}
   330  
   331  	err = persistence.SaveContacts(conatacts)
   332  	s.Require().NoError(err)
   333  
   334  	showcase1 := &identity.ProfileShowcase{
   335  		ContactID: "contact_1",
   336  		Accounts: []*identity.ProfileShowcaseAccount{
   337  			&identity.ProfileShowcaseAccount{
   338  				ContactID: "contact_1",
   339  				Address:   "0x0000000000000000000000000000000000000001",
   340  				Name:      "Contact1-Account1",
   341  				ColorID:   "blue",
   342  				Emoji:     "-_-",
   343  				Order:     0,
   344  			},
   345  			&identity.ProfileShowcaseAccount{
   346  				ContactID: "contact_1",
   347  				Address:   "0x0000000000000000000000000000000000000002",
   348  				Name:      "Contact1-Account2",
   349  				ColorID:   "blue",
   350  				Emoji:     "-_-",
   351  				Order:     1,
   352  			},
   353  		},
   354  	}
   355  	showcase2 := &identity.ProfileShowcase{
   356  		ContactID: "contact_2",
   357  		Accounts: []*identity.ProfileShowcaseAccount{
   358  			&identity.ProfileShowcaseAccount{
   359  				ContactID: "contact_2",
   360  				Address:   "0x0000000000000000000000000000000000000001",
   361  				Name:      "Contact2-Account1",
   362  				ColorID:   "blue",
   363  				Emoji:     "-_-",
   364  				Order:     0,
   365  			},
   366  			&identity.ProfileShowcaseAccount{
   367  				ContactID: "contact_2",
   368  				Address:   "0x0000000000000000000000000000000000000002",
   369  				Name:      "Contact2-Account2",
   370  				ColorID:   "blue",
   371  				Emoji:     "-_-",
   372  				Order:     1,
   373  			},
   374  		},
   375  	}
   376  	showcase3 := &identity.ProfileShowcase{
   377  		ContactID: "contact_3",
   378  		Accounts: []*identity.ProfileShowcaseAccount{
   379  			&identity.ProfileShowcaseAccount{
   380  				ContactID: "contact_3",
   381  				Address:   "0x0000000000000000000000000000000000000001",
   382  				Name:      "Contact3-Account1",
   383  				ColorID:   "blue",
   384  				Emoji:     "-_-",
   385  				Order:     0,
   386  			},
   387  		},
   388  	}
   389  
   390  	err = persistence.SaveProfileShowcaseForContact(showcase1)
   391  	s.Require().NoError(err)
   392  	err = persistence.SaveProfileShowcaseForContact(showcase2)
   393  	s.Require().NoError(err)
   394  	err = persistence.SaveProfileShowcaseForContact(showcase3)
   395  	s.Require().NoError(err)
   396  
   397  	showcaseAccounts, err := persistence.GetProfileShowcaseAccountsByAddress(showcase1.Accounts[0].Address)
   398  	s.Require().NoError(err)
   399  
   400  	s.Require().Equal(3, len(showcaseAccounts))
   401  	for i := 0; i < len(showcaseAccounts); i++ {
   402  		if showcaseAccounts[i].ContactID == showcase1.ContactID {
   403  			s.Require().Equal(showcase1.Accounts[0].Address, showcase1.Accounts[0].Address)
   404  		} else if showcaseAccounts[i].ContactID == showcase2.ContactID {
   405  			s.Require().Equal(showcase2.Accounts[0].Address, showcase2.Accounts[0].Address)
   406  		} else if showcaseAccounts[i].ContactID == showcase3.ContactID {
   407  			s.Require().Equal(showcase3.Accounts[0].Address, showcase3.Accounts[0].Address)
   408  		} else {
   409  			s.Require().Fail("unexpected contact id")
   410  		}
   411  	}
   412  
   413  	showcaseAccounts, err = persistence.GetProfileShowcaseAccountsByAddress(showcase1.Accounts[1].Address)
   414  	s.Require().NoError(err)
   415  
   416  	s.Require().Equal(2, len(showcaseAccounts))
   417  	for i := 0; i < len(showcaseAccounts); i++ {
   418  		if showcaseAccounts[i].ContactID == showcase1.ContactID {
   419  			s.Require().Equal(showcase1.Accounts[0].Address, showcase1.Accounts[0].Address)
   420  		} else if showcaseAccounts[i].ContactID == showcase2.ContactID {
   421  			s.Require().Equal(showcase2.Accounts[0].Address, showcase2.Accounts[0].Address)
   422  		} else {
   423  			s.Require().Fail("unexpected contact id")
   424  		}
   425  	}
   426  }
   427  
   428  func (s *TestProfileShowcasePersistence) TestUpdateProfileShowcaseAccountOnWalletAccountChange() {
   429  	db, err := openTestDB()
   430  	s.Require().NoError(err)
   431  	persistence := newSQLitePersistence(db)
   432  
   433  	deleteAccountAddress := "0x0000000000000000000000000033433445133423"
   434  	updateAccountAddress := "0x0000000000000000000000000032433445133424"
   435  
   436  	preferences := &identity.ProfileShowcasePreferences{
   437  		Accounts: []*identity.ProfileShowcaseAccountPreference{
   438  			&identity.ProfileShowcaseAccountPreference{
   439  				Address:            deleteAccountAddress,
   440  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityEveryone,
   441  				Order:              0,
   442  			},
   443  			&identity.ProfileShowcaseAccountPreference{
   444  				Address:            updateAccountAddress,
   445  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityContacts,
   446  				Order:              1,
   447  			},
   448  		},
   449  	}
   450  
   451  	err = persistence.SaveProfileShowcasePreferences(preferences)
   452  	s.Require().NoError(err)
   453  
   454  	account, err := persistence.GetProfileShowcaseAccountPreference(updateAccountAddress)
   455  	s.Require().NoError(err)
   456  	s.Require().NotNil(account)
   457  	s.Require().Equal(*account, *preferences.Accounts[1])
   458  
   459  	account.ShowcaseVisibility = identity.ProfileShowcaseVisibilityIDVerifiedContacts
   460  	account.Order = 7
   461  
   462  	err = persistence.SaveProfileShowcaseAccountPreference(account)
   463  	s.Require().NoError(err)
   464  
   465  	deleted, err := persistence.DeleteProfileShowcaseAccountPreference(deleteAccountAddress)
   466  	s.Require().NoError(err)
   467  	s.Require().True(deleted)
   468  
   469  	// One more time to check correct error handling
   470  	deleted, err = persistence.DeleteProfileShowcaseAccountPreference(deleteAccountAddress)
   471  	s.Require().NoError(err)
   472  	s.Require().False(deleted)
   473  
   474  	preferencesBack, err := persistence.GetProfileShowcasePreferences()
   475  	s.Require().NoError(err)
   476  
   477  	s.Require().Len(preferencesBack.Accounts, 1)
   478  	s.Require().Equal(*preferencesBack.Accounts[0], *account)
   479  }
   480  
   481  func (s *TestProfileShowcasePersistence) TestUpdateProfileShowcaseCommunityOnChange() {
   482  	db, err := openTestDB()
   483  	s.Require().NoError(err)
   484  	persistence := newSQLitePersistence(db)
   485  
   486  	deleteCommunityID := "0x3243344513424"
   487  
   488  	preferences := &identity.ProfileShowcasePreferences{
   489  		Communities: []*identity.ProfileShowcaseCommunityPreference{
   490  			&identity.ProfileShowcaseCommunityPreference{
   491  				CommunityID:        "0x32433445133424",
   492  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityEveryone,
   493  				Order:              0,
   494  			},
   495  			&identity.ProfileShowcaseCommunityPreference{
   496  				CommunityID:        deleteCommunityID,
   497  				ShowcaseVisibility: identity.ProfileShowcaseVisibilityContacts,
   498  				Order:              1,
   499  			},
   500  		},
   501  	}
   502  
   503  	err = persistence.SaveProfileShowcasePreferences(preferences)
   504  	s.Require().NoError(err)
   505  
   506  	deleted, err := persistence.DeleteProfileShowcaseCommunityPreference(deleteCommunityID)
   507  	s.Require().NoError(err)
   508  	s.Require().True(deleted)
   509  
   510  	// One more time to check correct error handling
   511  	deleted, err = persistence.DeleteProfileShowcaseCommunityPreference(deleteCommunityID)
   512  	s.Require().NoError(err)
   513  	s.Require().False(deleted)
   514  
   515  	preferencesBack, err := persistence.GetProfileShowcasePreferences()
   516  	s.Require().NoError(err)
   517  
   518  	s.Require().Len(preferencesBack.Communities, 1)
   519  	s.Require().Equal(*preferencesBack.Communities[0], *preferences.Communities[0])
   520  }