Пример #1
0
        public async Task TestContactsAreCachedAndOnTangleShouldExcludeRejectedContactsAndSetCache()
        {
            var pubKeyAddress          = Seed.Random().Value;
            var contactRequestAddress  = new Address(Seed.Random().Value);
            var rejectedContactAddress = Seed.Random().Value;
            var storedContactAddress   = Seed.Random().Value;
            var keyPair = InMemoryContactRepository.NtruKeyPair;

            var contactRepository = new InMemoryContactRepository();
            await contactRepository.AddContactAsync(storedContactAddress, true, pubKeyAddress);

            await contactRepository.AddContactAsync(rejectedContactAddress, false, pubKeyAddress);

            var rejectedContactBundle = CreateBundle(
                contactRequestAddress,
                ContactExchange.Create(
                    new Contact {
                ChatAddress = rejectedContactAddress, PublicKeyAddress = rejectedContactAddress, Rejected = true
            },
                    keyPair.PublicKey,
                    keyPair.PublicKey).Payload);

            var approvedContactBundle = CreateBundle(
                contactRequestAddress,
                ContactExchange.Create(
                    new Contact {
                ChatAddress = storedContactAddress, PublicKeyAddress = storedContactAddress
            },
                    keyPair.PublicKey,
                    keyPair.PublicKey).Payload);

            var requestBundle = CreateBundle(
                contactRequestAddress,
                ContactExchange.Create(
                    new Contact {
                ChatAddress = storedContactAddress, PublicKeyAddress = storedContactAddress, Request = true, Name = "Requester"
            },
                    keyPair.PublicKey,
                    keyPair.PublicKey).Payload);

            var messenger = new InMemoryMessenger();

            messenger.SentMessages.Add(new Message(rejectedContactBundle.Transactions.Aggregate(new TryteString(), (current, transaction) => current.Concat(transaction.Fragment)), contactRequestAddress));
            messenger.SentMessages.Add(new Message(approvedContactBundle.Transactions.Aggregate(new TryteString(), (current, transaction) => current.Concat(transaction.Fragment)), contactRequestAddress));
            messenger.SentMessages.Add(new Message(requestBundle.Transactions.Aggregate(new TryteString(), (current, transaction) => current.Concat(transaction.Fragment)), contactRequestAddress));

            var interactor = new GetContactsInteractor(contactRepository, messenger, NtruEncryption.Key);
            var response   = await interactor.ExecuteAsync(
                new GetContactsRequest
            {
                PublicKeyAddress = new Address(pubKeyAddress),
                RequestAddress   = contactRequestAddress,
                KeyPair          = keyPair
            });

            Assert.AreEqual(ResponseCode.Success, response.Code);
            Assert.AreEqual(1, response.ApprovedContacts.Count);
            Assert.AreEqual(1, response.PendingContactRequests.Count);
        }
Пример #2
0
        public async Task TestContactsAreCachedAndOnTangleShouldExcludeRejectedContactsAndSetCache()
        {
            var pubKeyAddress          = Seed.Random().Value;
            var contactRequestAddress  = new Address(Seed.Random().Value);
            var rejectedContactAddress = Seed.Random().Value;
            var storedContactAddress   = Seed.Random().Value;

            var contactRepository = new InMemoryContactRepository();
            await contactRepository.AddContactAsync(storedContactAddress, true, pubKeyAddress);

            await contactRepository.AddContactAsync(rejectedContactAddress, false, pubKeyAddress);

            var approvedContactMessage = TryteString.FromUtf8String(JsonConvert.SerializeObject(new Contact {
                ChatAddress = storedContactAddress
            }));

            var rejectedContactBundle = CreateBundle(
                contactRequestAddress,
                TryteString.FromUtf8String(JsonConvert.SerializeObject(new Contact {
                ChatAddress = rejectedContactAddress, Rejected = true
            })));

            var approvedContactBundle = CreateBundle(contactRequestAddress, approvedContactMessage);

            var iotaRepository = new InMemoryIotaRepository();

            iotaRepository.SentBundles.Add(rejectedContactBundle);
            iotaRepository.SentBundles.Add(approvedContactBundle);
            iotaRepository.SentBundles.Add(
                CreateBundle(
                    contactRequestAddress,
                    TryteString.FromUtf8String(JsonConvert.SerializeObject(new Contact {
                ChatAddress = storedContactAddress, Request = true
            }))));

            var transactionCache = new InMemoryTransactionCache();

            var cacheItem = new TransactionCacheItem
            {
                Address           = contactRequestAddress,
                TransactionHash   = approvedContactBundle.Transactions[0].Hash,
                TransactionTrytes = approvedContactMessage
            };

            transactionCache.Items.Add(cacheItem);

            var interactor = new GetContactsInteractor(contactRepository, transactionCache, iotaRepository);
            var response   = await interactor.ExecuteAsync(
                new GetContactsRequest
            {
                PublicKeyAddress      = new Address(pubKeyAddress),
                ContactRequestAddress = contactRequestAddress
            });

            Assert.AreEqual(1, response.ApprovedContacts.Count);
            Assert.AreEqual(1, response.PendingContactRequests.Count);
            Assert.AreEqual(3, transactionCache.Items.Count);
        }
Пример #3
0
        public async Task TestContactsAreContainedCachedAndApproved()
        {
            var pubKeyAddress         = Seed.Random().Value;
            var contactRequestAddress = new Address(Seed.Random().Value);
            var storedContactAddress  = Seed.Random().Value;
            var keyPair = InMemoryContactRepository.NtruKeyPair;

            var contactRepository = new InMemoryContactRepository();
            var transactionCache  = new MemoryTransactionCache();

            var bundle = CreateBundle(
                contactRequestAddress,
                ContactExchange.Create(new Contact {
                ChatAddress = storedContactAddress, Rejected = true
            }, keyPair.PublicKey, keyPair.PublicKey).Payload);

            bundle.Transactions.ForEach(async t => await transactionCache.SaveTransactionAsync(new TransactionCacheItem
            {
                Address           = contactRequestAddress,
                TransactionHash   = t.Hash,
                TransactionTrytes = t.ToTrytes()
            }));

            await contactRepository.AddContactAsync(storedContactAddress, true, pubKeyAddress);

            var iotaRepositoryMock = new Mock <IIotaRepository>();

            iotaRepositoryMock.Setup(i => i.FindTransactionsByAddressesAsync(It.IsAny <List <Address> >())).ReturnsAsync(
                new TransactionHashList {
                Hashes = new List <Hash>(bundle.Transactions.Select(t => t.Hash))
            });
            iotaRepositoryMock.Setup(i => i.GetTrytesAsync(It.IsAny <List <Hash> >())).ReturnsAsync(new List <TransactionTrytes>());

            var interactor = new GetContactsInteractor(contactRepository, new TangleMessenger(iotaRepositoryMock.Object, transactionCache), NtruEncryption.Key);
            var response   = await interactor.ExecuteAsync(
                new GetContactsRequest
            {
                PublicKeyAddress = new Address(pubKeyAddress),
                RequestAddress   = contactRequestAddress,
                KeyPair          = keyPair
            });

            Assert.AreEqual(1, response.ApprovedContacts.Count);
        }
Пример #4
0
        public async Task TestContactsAreContainedCachedAndApproved()
        {
            var pubKeyAddress         = Seed.Random().Value;
            var contactRequestAddress = new Address(Seed.Random().Value);
            var storedContactAddress  = Seed.Random().Value;

            var contactRepository = new InMemoryContactRepository();
            var transactionCache  = new InMemoryTransactionCache();

            var cacheItem = new TransactionCacheItem
            {
                Address           = contactRequestAddress,
                TransactionHash   = new Hash(Seed.Random().Value),
                TransactionTrytes = TryteString.FromUtf8String(
                    JsonConvert.SerializeObject(
                        new Contact {
                    ChatAddress = storedContactAddress
                }))
            };

            await transactionCache.SaveTransactionAsync(cacheItem);

            await contactRepository.AddContactAsync(storedContactAddress, true, pubKeyAddress);

            var iotaRepositoryMock = new Mock <IIotaRepository>();

            iotaRepositoryMock.Setup(i => i.FindTransactionsByAddressesAsync(It.IsAny <List <Address> >())).ReturnsAsync(
                new TransactionHashList {
                Hashes = new List <Hash> {
                    cacheItem.TransactionHash
                }
            });

            var interactor = new GetContactsInteractor(contactRepository, transactionCache, iotaRepositoryMock.Object);
            var response   = await interactor.ExecuteAsync(
                new GetContactsRequest
            {
                PublicKeyAddress      = new Address(pubKeyAddress),
                ContactRequestAddress = contactRequestAddress
            });

            Assert.AreEqual(1, response.ApprovedContacts.Count);
        }