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();
        }
예제 #2
0
 public void SetUp()
 {
     using (new TenantAdministratorContext("EDC"))
     {
         _notifier = CreateNotifier();
     }
 }
예제 #3
0
        public void Notify(bool expectReply)
        {
            var provider = new DummyProvider();

            using (var scope = Factory.Current.BeginLifetimeScope(builder =>
            {
                builder.Register(ctx => provider).As <ISmsProvider>();
            }))
                using (Factory.SetCurrentScope(scope))
                {
                    var notifier = new TwilioNotifier()
                    {
                        Name            = "TwilioTest",
                        TpAccountSid    = TwilioTestHelper.TestAccountSid,
                        TpAuthToken     = TwilioTestHelper.TestAuthToken,
                        TpSendingNumber = TwilioTestHelper.TestFromNumber_Valid
                    };

                    var person           = new Person();
                    var mobilePhoneField = Factory.ScriptNameResolver.GetInstance("Mobile phone", StringField.StringField_Type.Id);
                    person.SetField(mobilePhoneField, TwilioTestHelper.TestToNumber_Valid);

                    var notification = new Notification {
                        NMessage = "TestMessage"
                    };

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

                    var result = Entity.Get <Notification>(notification.Id).SendRecords;
                    Assert.That(result, Is.Not.Null);
                    Assert.That(result.Count, Is.EqualTo(1));
                    Assert.That(result.First().SrErrorMessage, Is.Null);
                }
        }
예제 #4
0
        Notification RunNotify(IEntity person, TwilioNotifier notifier, bool waitForReplies, IEntity linkTo = null, string replyWorkflowString = null, Workflow replyWorkflow = null)
        {
            var inputs = new Dictionary <string, object>
            {
                { "People", person.ToEnumerable() },
                { "LinkToRecord", linkTo },
            };

            var wf = new Workflow {
                Name = "RunNotify " + DateTime.UtcNow.ToString()
            };

            wf
            .AddDefaultExitPoint()
            .AddInput <ResourceListArgument>("People", Person.Person_Type)
            .AddInput <ResourceArgument>("LinkToRecord", UserResource.UserResource_Type)
            .AddOutput <ResourceArgument>("NotificationRecord", Notification.Notification_Type)
            .AddNotify(
                "Notify", "People", "'Test'",
                waitForReplies,
                linkToRecordExpression: "LinkToRecord",
                replyMap: replyWorkflowString == null ? null :
                new Dictionary <string, Workflow> {
                { replyWorkflowString, replyWorkflow }
            })
            .AddAssignToVar("Assign", "[Notify_Notification Record]", "NotificationRecord");

            wf.Save();
            ToDelete.Add(wf.Id);

            using (var scope = Factory.Current.BeginLifetimeScope(builder =>
            {
                builder.Register(c => new TenantSmsNotifier(notifier.Id)).As <INotifier>();
            }))
                using (Factory.SetCurrentScope(scope))
                {
                    WorkflowRun run = null;

                    using (new TestWfRunContext())
                    {
                        run = RunWorkflow(wf, inputs);
                        run.Save();
                    }

                    Thread.Sleep(1000);

                    Assert.That(run.WorkflowRunStatus_Enum, Is.EqualTo(WorkflowRunState_Enumeration.WorkflowRunCompleted));

                    var results = run.GetOutput();

                    Assert.That(results.Keys, Has.Member("NotificationRecord"));

                    var entity = (IEntity)results["NotificationRecord"];
                    return(entity.As <Notification>());
                }
        }
예제 #5
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
        }
예제 #6
0
        private void ValidateRequest(TwilioNotifier notifier)
        {
            var httpContext = System.Web.HttpContext.Current;

            if (httpContext == null)    // We are not coming via the http stack so don't validate.
            {
                return;
            }

            // To validate a dev request we need to ignore any ADC transformations that may have occurred.
            var  externalAddress = ConfigurationSettings.GetSiteConfigurationSection().SiteSettings.Address;
            var  alternativeUrl  = $"https://{externalAddress}{httpContext.Request.Url.PathAndQuery}";
            bool isValid         = _validator.IsValidRequest(httpContext, notifier.TpAuthToken, alternativeUrl);

            if (!isValid)
            {
                throw new TwilioValidationException();
            }
        }
        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();
        }
        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();
        }
예제 #9
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));
        }
예제 #10
0
 TwilioSms CreateMatchingReply(TwilioNotifier notifier, SmsSendRecord send, string body = "Unimportant")
 {
     return(CreateSms(accountSid: notifier.TpAccountSid, from: send.SsrTo, to: send.SsrFrom, body: body));
 }
예제 #11
0
 public LoopbackApi(TwilioNotifier notifier, ITwilioSmsReceiver receiver)
 {
     _notifier = notifier;
     _receiver = receiver;
     _replies  = new List <TwilioSms>();
 }
예제 #12
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();
            }
        }
예제 #13
0
        [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();
            }
        }
예제 #14
0
        [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();
            }
        }