Skip to content

Commit

Permalink
wait for keys distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Apr 19, 2024
1 parent e4c5582 commit 340656b
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions protocol/communities_messenger_token_permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,15 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestImportDecryptedArchiveMe
return len(sub.Community.TokenPermissions()) == 2
})

waitOnChannelKeyToBeDistributedToOwner := s.waitOnKeyDistribution(func(sub *CommunityAndKeyActions) bool {
action, ok := sub.keyActions.ChannelKeysActions[chat.CommunityChatID()]
if !ok || action.ActionType != communities.EncryptionKeySendToMembers {
return false
}
_, ok = action.Members[common.PubkeyToHex(&s.owner.identity.PublicKey)]
return ok
})

response, err := s.owner.CreateCommunityTokenPermission(communityPermission)
s.Require().NoError(err)
s.Require().NotNil(response)
Expand All @@ -1736,9 +1745,13 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestImportDecryptedArchiveMe
s.Require().NoError(err)
s.Require().True(community.Encrypted())

err = <-waitOnChannelKeyToBeDistributedToOwner
s.Require().NoError(err)

// 2. Owner: Send a message A
messageText1 := RandomLettersString(10)
message1 := s.sendChatMessage(s.owner, chat.ID, messageText1)
s.logger.Debug("message1 sent", zap.Any("id", message1.ID), zap.String("text", message1.Text))

// 2.2. Retrieve own message (to make it stored in the archive later)
_, err = s.owner.RetrieveAll()
Expand Down Expand Up @@ -1769,11 +1782,28 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestImportDecryptedArchiveMe
s.Require().Len(archiveIDs, 1)
s.logger.Debug("archive created", zap.Any("archiveIDs", archiveIDs))

community, err = s.owner.GetCommunityByID(community.ID())
s.Require().NoError(err)

// 4. Bob: join community (satisfying membership, but not channel permissions)
s.makeAddressSatisfyTheCriteria(testChainID1, bobAddress, communityPermission.TokenCriteria[0])
s.advertiseCommunityTo(community, s.bob)
s.joinCommunity(community, s.bob, bobPassword, []string{})

checkCommunity := func(m *Messenger) {
receivedCommunity, err := m.GetCommunityByID(community.ID())
s.Require().NoError(err)
s.Require().NotNil(receivedCommunity)
s.Require().Len(receivedCommunity.Members(), 2)
chatID := chat.CommunityChatID()
receivedChat, ok := receivedCommunity.Chats()[chatID]
s.Require().True(ok)
s.Require().Len(receivedChat.Members, 1)
}

checkCommunity(s.owner)
checkCommunity(s.bob)

// 5. Bob: Import community archive
// The archive is successfully decrypted, but the message inside is not.
// https://github.com/status-im/status-desktop/issues/13105 can be reproduced at this stage
Expand All @@ -1797,6 +1827,13 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestImportDecryptedArchiveMe
err = s.bob.communitiesManager.SaveMessageArchiveID(community.ID(), archiveHash)
s.Require().NoError(err)

ownerCommunity, err := s.owner.GetCommunityByID(community.ID())
s.Require().NoError(err)
bobCommunity, err := s.bob.GetCommunityByID(community.ID())
s.Require().NoError(err)

s.logger.Debug("<<< community before importing", zap.Any("owner", ownerCommunity), zap.Any("bob", bobCommunity))

s.bob.importDelayer.once.Do(func() {
close(s.bob.importDelayer.wait)
})
Expand All @@ -1809,6 +1846,8 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestImportDecryptedArchiveMe
s.Require().Nil(receivedMessage1)
s.Require().Error(err)

//s.bob.persistence.GetHashRatchetMessages()

// Make bob satisfy channel criteria
s.makeAddressSatisfyTheCriteria(testChainID1, bobAddress, channelPermission.TokenCriteria[0])

Expand All @@ -1832,6 +1871,22 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestImportDecryptedArchiveMe
s.Require().NoError(err)

// Finally ensure that the message from archive was retrieved and decrypted

// Message could have been retreived durting previous wait
//receivedMessage1, err = s.bob.MessageByID(message1.ID)
//if err != nil {
// s.Require().NotNil(receivedMessage1)
// return
//}

//_, err = WaitOnMessengerResponse(
// s.bob,
// func(r *MessengerResponse) bool {
// _, ok := r.messages[message1.ID]
// return ok
// },
// "message1 not retrieved",
//)
response, err = s.bob.RetrieveAll()
s.Require().NoError(err)
s.Require().Len(response.Messages(), 1)
Expand Down

0 comments on commit 340656b

Please sign in to comment.