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);
        }
        public async Task TestMessagesGetLoadedFromTangle()
        {
            var repository = new InMemoryIotaRepository();

            var messenger = new TangleMessenger(repository);

            var receiver = new Address("GUEOJUOWOWYEXYLZXNQUYMLMETF9OOGASSKUZZWUJNMSHLFLYIDIVKXKLTLZPMNNJCYVSRZABFKCAVVIW");
            var payload  = TryteString.FromUtf8String("Hi. I'm a test").Concat(new TryteString(Constants.End.Value));

            await messenger.SendMessageAsync(new Message(MessageType.RequestContact, payload, receiver));

            var sentMessages = await messenger.GetMessagesByAddressAsync(receiver);

            Assert.AreEqual("Hi. I'm a test", sentMessages[0].Payload.ToUtf8String());
        }
Exemple #3
0
        public async Task TestMessageIsValidShouldSendBundleWithTypeAndPayload()
        {
            var repository = new InMemoryIotaRepository();

            var messenger = new TangleMessenger(repository, new MemoryTransactionCache());
            var receiver  = new Address("GUEOJUOWOWYEXYLZXNQUYMLMETF9OOGASSKUZZWUJNMSHLFLYIDIVKXKLTLZPMNNJCYVSRZABFKCAVVIW");
            var payload   = new TryteString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            await messenger.SendMessageAsync(new Message(payload, receiver));

            Assert.AreEqual(1, repository.SentBundles.Count);

            var sentBundle = repository.SentBundles[0];

            Assert.AreEqual(receiver.Value, sentBundle.Transactions[0].Address.Value);
            Assert.AreEqual(payload.Value, sentBundle.Transactions[0].Fragment.GetChunk(0, payload.TrytesLength).Value);
        }
Exemple #4
0
        public async Task TestTransactionIsCachedShouldMergeWithMessagesFromTangle()
        {
            var repository       = new InMemoryIotaRepository();
            var transactionCache = new MemoryTransactionCache();

            var messenger = new TangleMessenger(repository, transactionCache);

            var receiver = new Address("GUEOJUOWOWYEXYLZXNQUYMLMETF9OOGASSKUZZWUJNMSHLFLYIDIVKXKLTLZPMNNJCYVSRZABFKCAVVIW");
            var payload  = TryteString.FromUtf8String("Hi. I'm a test");

            var messageOne = new Message(payload, receiver);
            var bundle     = new Bundle();

            bundle.AddTransfer(
                new Transfer
            {
                Address = messageOne.Receiver, Message = messageOne.Payload, Tag = Constants.Tag, Timestamp = Timestamp.UnixSecondsTimestamp
            });

            bundle.Finalize();
            bundle.Sign();
            await repository.SendTrytesAsync(bundle.Transactions);

            await transactionCache.SaveTransactionAsync(
                new TransactionCacheItem
            {
                Address           = receiver,
                TransactionHash   = new Hash(Seed.Random().Value),
                TransactionTrytes = new TransactionTrytes(TryteString.FromUtf8String("Hi. I'm a test").Value)
            });

            var sentMessages = await messenger.GetMessagesByAddressAsync(receiver);

            Assert.AreEqual(2, sentMessages.Count);
            Assert.AreEqual("Hi. I'm a test", sentMessages[0].Payload.ToUtf8String());
            Assert.AreEqual("Hi. I'm a test", sentMessages[1].Payload.ToUtf8String());
        }