Ejemplo n.º 1
0
 public void Teardown()
 {
     using (new TenantAdministratorContext("EDC"))
     {
         _notifier?.Delete();
     }
 }
Ejemplo n.º 2
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();
            }
        }
Ejemplo n.º 3
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();
            }
        }
Ejemplo n.º 4
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();
            }
        }