public void RegistrationDeregistration()
        {
            var notifier = new TwilioNotifier();

            notifier.Save(); // get the id

            notifier = notifier.AsWritable <TwilioNotifier>();
            notifier.TpAccountSid     = "1111";
            notifier.TpAuthToken      = "2222";
            notifier.TpSendingNumber  = "+1234578";
            notifier.TpEnableTestMode = false;

            var tapiMock = new Mock <ISmsProvider>();

            tapiMock.Setup(c => c.RegisterUrlForIncomingSms(notifier.TpAccountSid, notifier.TpAuthToken, notifier.TpSendingNumber, It.Is <long>(v => v == notifier.Id)));
            tapiMock.Setup(c => c.DeregisterUrlForIncomingSms(notifier.TpAccountSid, notifier.TpAuthToken, notifier.TpSendingNumber));

            using (var scope = Factory.Current.BeginLifetimeScope(b =>
            {
                b.Register(c => tapiMock.Object).As <ISmsProvider>().SingleInstance();
            }))
                using (Factory.SetCurrentScope(scope))
                {
                    // Register
                    notifier.Save();

                    // Deregister
                    notifier.AsWritable <TwilioNotifier>();
                    notifier.TpEnableTestMode = true;
                    notifier.Save();
                }

            tapiMock.VerifyAll();
        }
Exemple #2
0
        public void HandleStatusUpdate_MessageDoesNotExist()
        {
            var notifier = new TwilioNotifier();

            notifier.Save();

            var receiver = new TwilioSmsReceiver();

            var handled = receiver.HandleStatusUpdate(notifier.Id, _rand.Next().ToString(), "Sent", null);

            Assert.That(handled, Is.False);
            // Ignored
        }
        public void ReregisterOnAccountChange()
        {
            var notifier = new TwilioNotifier();

            notifier = notifier.AsWritable <TwilioNotifier>();
            notifier.TpAccountSid     = "1111";
            notifier.TpAuthToken      = "2222";
            notifier.TpSendingNumber  = "3333";
            notifier.TpEnableTestMode = false;
            notifier.Save(); // get the id

            var tapiMock = new Mock <ISmsProvider>();

            tapiMock.Setup(c => c.RegisterUrlForIncomingSms("91111", "2222", "3333", It.Is <long>(v => v == notifier.Id)));
            tapiMock.Setup(c => c.RegisterUrlForIncomingSms("91111", "92222", "3333", It.Is <long>(v => v == notifier.Id)));
            tapiMock.Setup(c => c.RegisterUrlForIncomingSms("91111", "92222", "93333", It.Is <long>(v => v == notifier.Id)));

            using (var scope = Factory.Current.BeginLifetimeScope(b =>
            {
                b.Register(c => tapiMock.Object).As <ISmsProvider>().SingleInstance();
            }))
                using (Factory.SetCurrentScope(scope))
                {
                    notifier = notifier.AsWritable <TwilioNotifier>();
                    notifier.TpAccountSid = "91111";
                    notifier.Save();

                    notifier             = notifier.AsWritable <TwilioNotifier>();
                    notifier.TpAuthToken = "92222";
                    notifier.Save();

                    notifier = notifier.AsWritable <TwilioNotifier>();
                    notifier.TpSendingNumber = "93333";
                    notifier.Save();
                }

            tapiMock.VerifyAll();
        }
        public void NotRegisterOnNewWithTestMode()
        {
            var tapiMock = new Mock <ISmsProvider>();

            using (var scope = Factory.Current.BeginLifetimeScope(b =>
            {
                b.Register(c => tapiMock.Object).As <ISmsProvider>().SingleInstance();
            }))
                using (Factory.SetCurrentScope(scope))
                {
                    var notifier = new TwilioNotifier();
                    notifier = notifier.AsWritable <TwilioNotifier>();
                    notifier.TpAccountSid     = "1111";
                    notifier.TpAuthToken      = "2222";
                    notifier.TpSendingNumber  = "3333";
                    notifier.TpEnableTestMode = true;
                    notifier.Save();
                }

            tapiMock.VerifyAll();
        }
Exemple #5
0
        public void HandleStatusUpdate(string status, string errorCode, string expectedStatus, string expectedErrorMessage)
        {
            var notifier = new TwilioNotifier();

            notifier.Save();

            var messageSid = _rand.Next().ToString();
            var receiver   = new TwilioSmsReceiver();

            var send = new SmsSendRecord {
                SsrMessageSid = messageSid
            };

            send.Save();

            bool handled = receiver.HandleStatusUpdate(notifier.Id, messageSid, status, errorCode);

            Assert.That(handled, Is.True);

            var send2 = Entity.Get <SmsSendRecord>(send.Id);

            Assert.That(send2.SsrDeliveryStatus, Is.EqualTo(expectedStatus));
            Assert.That(send2.SrErrorMessage, Is.EqualTo(expectedErrorMessage));
        }
Exemple #6
0
        public void Run(bool testMode, string toNumber, bool waitForReplies, bool linkToRecord, bool setReplyWorkflow)
        {
            TwilioNotifier notifier      = null;
            Notification   nr            = null;
            IEntity        record        = null;
            Workflow       replyWorkflow = null;

            try
            {
                var sendingNumber = _rand.Next().ToString();
                var fromNumber    = _rand.Next().ToString();

                var person = Entity.Create(new EntityRef("core:person"));

                var mobilePhoneField = Factory.ScriptNameResolver.GetInstance("Mobile phone", StringField.StringField_Type.Id);

                person.SetField(mobilePhoneField, toNumber);
                person.Save();
                ToDelete.Add(person.Id);

                notifier = new TwilioNotifier();

                notifier.Name             = "TwilioTest";
                notifier.TpAccountSid     = TwilioTestHelper.TestAccountSid;
                notifier.TpAuthToken      = TwilioTestHelper.TestAuthToken;
                notifier.TpSendingNumber  = sendingNumber;
                notifier.TpEnableTestMode = testMode;

                notifier.Save();

                // add a record
                if (linkToRecord)
                {
                    record = Entity.Create(new EntityRef("test:vehicle"));
                    record.Save();
                }

                string replyWorkflowString = setReplyWorkflow ? "Reply" : null;
                replyWorkflow = CreateLoggingWorkflow();
                replyWorkflow.WorkflowRunAsOwner = true;
                replyWorkflow.SecurityOwner      = Entity.Get <UserAccount>(new EntityRef("core:administratorUserAccount"));
                replyWorkflow.Save();

                nr = RunNotify(person, notifier, waitForReplies, record, replyWorkflowString, replyWorkflow);

                Assert.That(nr.NMessage, Is.EqualTo("Test"));
                Assert.That(nr.SendRecords.Count, Is.EqualTo(1));

                if (linkToRecord)
                {
                    Assert.That(nr.NRelatedRecord?.Id, Is.EqualTo(record.Id));
                }
                else
                {
                    Assert.That(nr.NRelatedRecord, Is.Null);
                }

                if (setReplyWorkflow)
                {
                    Thread.Sleep(1000);      // give the async triggers a chance to complete
                    Assert.That(replyWorkflow.RunningInstances.Count, Is.EqualTo(1));
                }
            }
            finally
            {
                notifier?.Delete();
                if (nr != null)
                {
                    Entity.Delete(nr.Id);
                }
                record?.Delete();
                replyWorkflow?.Delete();
            }
        }
        [RunWithoutTransaction]     // As we are calling back into IIS, this can not run in a transaction
        public void Send(string terminalDigit, string expectedReply)
        {
            Random rand = new Random();

            TwilioNotifier notifier     = null;
            Person         person       = null;
            SendRecord     result       = null;
            Notification   notification = null;

            try
            {
                notifier = new TwilioNotifier()
                {
                    Name             = "LoopbackApiTest.Notify " + DateTime.UtcNow,
                    TpAccountSid     = rand.Next().ToString(),
                    TpAuthToken      = rand.Next().ToString(),
                    TpSendingNumber  = rand.Next().ToString(),
                    TpEnableTestMode = true
                };
                notifier.Save();

                person = new Person {
                    Name = "LoopbackApiTest.Notify " + DateTime.UtcNow
                };

                var mobilePhoneField = Factory.ScriptNameResolver.GetInstance("Mobile phone", StringField.StringField_Type.Id);
                var mobileNumber     = rand.Next().ToString() + terminalDigit;
                person.SetField(mobilePhoneField, mobileNumber);
                person.Save();

                notification = new Notification {
                    NMessage = "TestMessage", NAcceptRepliesUntil = DateTime.UtcNow.AddDays(1)
                };
                notification.Save();

                TwilioRouter.Instance.Send(notifier, notification, person.ToEnumerable(), true);

                var results = Entity.Get <Notification>(notification.Id).SendRecords;
                Assert.That(results, Is.Not.Null);
                Assert.That(results.Count, Is.EqualTo(1));

                result = results.First();
                Assert.That(result.SrErrorMessage, Is.Null);

                // It can take a while for the second thread's update to work its way through.
                SendRecord result2 = null;
                for (int i = 0; i < 50; i++)
                {
                    result2 = Entity.Get <SendRecord>(result.Id);
                    if (result2.SrToReply.Count() == 1)
                    {
                        break;
                    }
                }

                Assert.That(result2.SrToReply.Count(), Is.EqualTo(1));
                Assert.That(result2.SrToReply.First().RrReply, Is.EqualTo(expectedReply));
            }
            finally
            {
                notifier?.Delete();
                person?.Delete();
                notification?.Delete();
            }
        }
        [RunWithoutTransaction]         // Can't be in a transaction as it takes too long
        public void SendMany(int numberToSend)
        {
            Random rand = new Random();

            TwilioNotifier notifier     = null;
            List <Person>  people       = null;
            Notification   notification = null;

            try
            {
                notifier = new TwilioNotifier()
                {
                    Name             = "LoopbackApiTest.Notify " + DateTime.UtcNow,
                    TpAccountSid     = rand.Next().ToString(),
                    TpAuthToken      = rand.Next().ToString(),
                    TpSendingNumber  = rand.Next().ToString(),
                    TpEnableTestMode = true
                };
                notifier.Save();

                people = new List <Person>();

                var mobilePhoneField = Factory.ScriptNameResolver.GetInstance("Mobile phone", StringField.StringField_Type.Id);

                for (int i = 0; i < numberToSend; i++)
                {
                    var person = new Person {
                        Name = "LoopbackApiTest.Notify " + DateTime.UtcNow
                    };

                    var mobileNumber = rand.Next().ToString();
                    person.SetField(mobilePhoneField, mobileNumber);
                    people.Add(person);
                }

                Entity.Save(people);

                notification = new Notification {
                    NMessage = "TestMessage", NAcceptRepliesUntil = DateTime.UtcNow.AddDays(1)
                };
                notification.Save();

                TwilioRouter.Instance.Send(notifier, notification, people, true);

                var results = Entity.Get <Notification>(notification.Id).SendRecords;
                Assert.That(results, Is.Not.Null);
                Assert.That(results.Count, Is.EqualTo(numberToSend));

                foreach (var result in results)
                {
                    Assert.That(result.SrErrorMessage, Is.Null);
                }
            }
            finally
            {
                notifier?.Delete();
                if (people.Any())
                {
                    Entity.Delete(people.Select(p => p.Id));
                }
                notification?.Delete();
            }
        }