public void CallTrackingMetricWebhookThrowsExceptionTest()
        {
            var request           = new Mock <HttpRequestBase>();
            var serverTime        = new Mock <IServerTime>();
            var xDataProvider     = new Mock <IExternalDataProvider>();
            var contactAutoRating = new Mock <IContactAutoRating>();

            string sampleInvalidJsonData =
                "{\"invalid\":Data\"}";
            var serializeObject =
                new MemoryStream(
                    Encoding.UTF8.GetBytes(
                        sampleInvalidJsonData));

            request.SetupGet(x => x.InputStream).Returns(serializeObject);

            var context = new Mock <HttpContextBase>();

            context.SetupGet(x => x.Request).Returns(request.Object);

            var leadsRepository  = new Mock <IRepository <Contact> >();
            var clientRepository = new Mock <IRepository <Client> >();
            var contactService   = new Mock <IContactService>();

            var controller = new WebhookController(leadsRepository.Object, clientRepository.Object, contactService.Object, serverTime.Object, xDataProvider.Object, contactAutoRating.Object);

            controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
            controller.CallTrackingMetricWebhook();

            contactAutoRating.Verify(ar => ar.SetAutoRating(It.IsAny <Contact>()), Times.Never);

            contactService.Verify(cs => cs.NotifyClientsForNewContactWithEmail(It.IsAny <int>()), Times.Never);
            contactService.Verify(cs => cs.NotifyClientsForNewContactWithPhoneNotification(It.IsAny <int>()), Times.Never);
            contactService.Verify(cs => cs.NotifyClientsForNewContactWithSmsNotification(It.IsAny <int>()), Times.Never);
        }
        public void CallTrackingMetricWebhookCreatesLeadWithValidData()
        {
            var serverTime = new Mock <IServerTime>();

            serverTime.Setup(st => st.RequestStarted).Returns(new DateTime(2013, 01, 01, 8, 20, 0));
            var xDataProvider     = new Mock <IExternalDataProvider>();
            var contactAutoRating = new Mock <IContactAutoRating>();

            var request = new Mock <HttpRequestBase>();

            string sampleJsonData =
                "{\"id\":3158334,\"account_id\":9724,\"name\":\"Restricted\",\"search\":null,\"referrer\":null,\"location\":null,\"source\":\"Website\",\"source_id\":14932,\"tgid\":28622,\"likelihood\":null,\"duration\":107,\"talk_time\":101,\"ring_time\":6,\"parent_id\":null,\"email\":null,\"street\":null,\"city\":\"\",\"state\":\"Stockholm\",\"country\":\"SE\",\"postal_code\":\"\",\"called_at\":\"2013-10-01 10:17 AM +02:00\",\"tracking_number_id\":28938,\"tracking_number\":\"+46844680390\",\"tracking_label\":null,\"business_number\":\"+4687541040\",\"business_label\":\"Winassist Växel\",\"receiving_number_id\":14464,\"dial_status\":\"completed\",\"billed_amount\":1080.0,\"billed_at\":\"2013-10-01T08:19:09\",\"caller_number_split\":[\"46\",\"8\",\"890443\"],\"excluded\":false,\"tracking_number_format\":\"+46-8-44680390\",\"business_number_format\":\"+46-8-7541040\",\"caller_number_format\":\"(+46) 889-0443\",\"alternative_number\":\"+468890443\",\"caller_number_complete\":\"+468890443\",\"caller_number\":\"+468890443\",\"visitor\":false,\"audio\":\"https://ct4.s3.amazonaws.com/accounts/AC31fe87364641848e5d08c041cac16c84/recordings/REef08b869be099e8c7aeb0d85b04a99d2\",\"tag_list\":[],\"notes\":null,\"latitude\":59.3323,\"longitude\":18.0629,\"extended_lookup_on\":false}";

            var serializeObject =
                new MemoryStream(
                    Encoding.UTF8.GetBytes(
                        sampleJsonData));

            request.SetupGet(x => x.InputStream).Returns(serializeObject);

            var context = new Mock <HttpContextBase>();

            context.SetupGet(x => x.Request).Returns(request.Object);

            var leadsRepository = new LocalRepository <Contact>();

            var clientRepository = new LocalRepository <Client>();
            var client           = ModelHelper.TestClient1AllDataNoReferences;

            client.CallTrackingMetricId = 9724;
            clientRepository.Add(client);

            var contactService = new Mock <IContactService>();

            var returnContact = new Contact()
            {
                Id   = 1,
                Date = new DateTime(2013, 01, 01, 08, 17, 0)
            };

            xDataProvider.Setup(x => x.MapPhoneDataToContact(It.Is <CallTrackingMetricsWebhookData>(ctm => ctm.id == 3158334)))
            .Returns(returnContact);

            var controller = new WebhookController(leadsRepository, clientRepository, contactService.Object, serverTime.Object, xDataProvider.Object, contactAutoRating.Object);

            controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
            controller.CallTrackingMetricWebhook();

            Assert.AreEqual(1, leadsRepository.All().Count());

            contactAutoRating.Verify(ar => ar.SetAutoRating(It.Is <Contact>(c => c.Id == returnContact.Id)), Times.Once());

            contactService.Verify(cs => cs.NotifyClientsForNewContactWithEmail(It.IsAny <int>()), Times.Once());
            contactService.Verify(cs => cs.NotifyClientsForNewContactWithPhoneNotification(It.IsAny <int>()), Times.Once());
            contactService.Verify(cs => cs.NotifyClientsForNewContactWithSmsNotification(It.IsAny <int>()), Times.Once());
        }
        public void CallTrackingMetricLeadsOlderThanTenMinutesSaveLeadInDataBaseAndDontSendNotifications()
        {
            var request           = new Mock <HttpRequestBase>();
            var serverTime        = new Mock <IServerTime>();
            var xDataProvider     = new Mock <IExternalDataProvider>();
            var contactAutoRating = new Mock <IContactAutoRating>();

            serverTime.Setup(st => st.RequestStarted).Returns(new DateTime(2013, 01, 01, 8, 30, 0));
            string sampleJsonData =
                "{\"id\":1,\"account_id\":1,\"caller_number\":\"08123456\", \"called_at\":\"2013-01-01 10:00 AM +02:00\"}";

            var serializeObject =
                new MemoryStream(
                    Encoding.UTF8.GetBytes(
                        sampleJsonData));

            request.SetupGet(x => x.InputStream).Returns(serializeObject);

            var context = new Mock <HttpContextBase>();

            context.SetupGet(x => x.Request).Returns(request.Object);

            var leadsRepository = new Mock <IRepository <Contact> >();

            var clientRepository = new LocalRepository <Client>();
            var client           = ModelHelper.TestClient1AllDataNoReferences;

            clientRepository.Add(client);

            var contactService = new Mock <IContactService>();


            var returnContact = new Contact()
            {
                Id   = 1,
                Date = new DateTime(2013, 01, 01, 08, 0, 0)
            };

            xDataProvider.Setup(x => x.MapPhoneDataToContact(It.Is <CallTrackingMetricsWebhookData>(ctm => ctm.id == 1)))
            .Returns(returnContact);

            var controller = new WebhookController(leadsRepository.Object, clientRepository, contactService.Object, serverTime.Object, xDataProvider.Object, contactAutoRating.Object);

            controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
            controller.CallTrackingMetricWebhook();

            leadsRepository.Verify(pc => pc.Add(It.IsAny <Contact>()), Times.Once());
            leadsRepository.Verify(pc => pc.SaveChanges(), Times.Once());
            contactAutoRating.Verify(ar => ar.SetAutoRating(It.Is <Contact>(c => c.Id == returnContact.Id)), Times.Once());

            contactService.Verify(cs => cs.NotifyClientsForNewContactWithEmail(It.IsAny <int>()), Times.Never);
            contactService.Verify(cs => cs.NotifyClientsForNewContactWithPhoneNotification(It.IsAny <int>()), Times.Never);
            contactService.Verify(cs => cs.NotifyClientsForNewContactWithSmsNotification(It.IsAny <int>()), Times.Never);
        }