Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // Grab configuration
            var dynamicsHost = ConfigurationManager.AppSettings["DynamicsHost"].ToString();
            var dynamicsUsername = ConfigurationManager.AppSettings["DynamicsUsername"].ToString();
            var dynamicsPassword = ConfigurationManager.AppSettings["DynamicsPassword"].ToString();

            var govdeliveryHost = ConfigurationManager.AppSettings["GovDeliveryHost"].ToString();
            var govdeliveryKey = ConfigurationManager.AppSettings["GovDeliveryKey"].ToString();

            var dynamics = new DynamicsClient(dynamicsHost, dynamicsUsername, dynamicsPassword);
            var hub = new HubClient(govdeliveryHost, govdeliveryKey, dynamics);

            // Initialize connection information for hub
            hub.RetrieveConnectors().Wait();

            // Ensure that dynamics is properly setup for our use case
            dynamics.EnsureGovdeliveryPublisher();
            dynamics.EnsureEmailGovdeliveryField();
            dynamics.EnsureEmailMetadata();

            // Retrieve and send emails that are in "Pending Send"
            var emails = dynamics.RetrievePendingEmails(hub.GetEmailForPolling());
            hub.SendEmails(emails).Wait();

            // Push analytics data back into dynamics
            hub.UpdateEmailStatuses().Wait();
        }
        public void TestRetrieveConnectorsWithDynamicsEmailConnector()
        {
            var fakeResponseHandler = new FakeHttpMessageHandler(connectorResponse("Connectors::DynamicsEmail"));
            var fakeHttpClient = new HttpClient(fakeResponseHandler);
            fakeHttpClient.BaseAddress = new Uri("https://blah.test.com");

            var mockClient = new Mock<IDynamicsClient>();
            var hubClient = new HubClient("https://blah.test.com", "blah", mockClient.Object);
            var hubConnection = hubClient.GetType().GetField("connection", BindingFlags.NonPublic | BindingFlags.Instance);
            hubConnection.SetValue(hubClient, fakeHttpClient);

            hubClient.RetrieveConnectors().Wait();

            var dynamicsValue = getPrivateValue(typeof(HubClient), hubClient, "dynamics");
            Assert.IsNotNull(dynamicsValue);
        }
        public void TestUpdateEmailStatuses()
        {
            Link link1 = new Link() {
                rel = "actions",
                href = "/connectors/123/actions"
            };

            Event email_sent_event = new Event()
            {
                name = "email_sent",
                external_id = "23a4",
                message_id = 8080
            };

            Event email_status_event = new Event()
            {
                name = "email_status",
                message_id = 8081,
                status = "sent"
            };

            Event invalid_email_status_event = new Event()
            {
                name = "email_status",
                external_id = "dyn54c07-3391-4bc0-a68e-911c2a38ed0e",
                status = "failed"
            };

            Event invalid_event = new Event()
            {
                name = "blah_blah",
                external_id = "zzz",
                message_id = 8083
            };

            Event really_invalid_event = new Event()
            {
                name = "email_status",
            };

            List<Event> events = new List<Event>();
            events.Add(email_sent_event);
            events.Add(email_status_event);
            events.Add(invalid_email_status_event);
            events.Add(invalid_event);
            events.Add(really_invalid_event);

            var connector = new Connector();
            connector.links = new List<Link>();
            connector.links.Add(link1);

            var fakeResponseHandler = new FakeHttpMessageHandler(eventResponse(events, new List<Event>()));
            var fakeHttpClient = new HttpClient(fakeResponseHandler);
            fakeHttpClient.BaseAddress = new Uri("https://blah.test.com");

            var mockClient = new Mock<IDynamicsClient>();
            mockClient.Setup(dyn => dyn.AssociateGovdeliveryEmail(It.IsAny<string>(), It.IsAny<string>()));
            mockClient.Setup(dyn => dyn.UpdateStatus(It.IsAny<string>(), It.IsAny<string>()));
            mockClient.Setup(dyn => dyn.LookupEmailByGovdeliveryId(It.IsAny<string>())).Returns("5cd54c07-3391-4bc0-a68e-911c2a38ed0e");
            var hubClient = new HubClient("https://blah.test.com", "blah", mockClient.Object);

            var hubConnection = hubClient.GetType().GetField("connection", BindingFlags.NonPublic | BindingFlags.Instance);
            hubConnection.SetValue(hubClient, fakeHttpClient);
            var hubDynamics = hubClient.GetType().GetField("dynamics", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            hubDynamics.SetValue(hubClient, connector);

            hubClient.UpdateEmailStatuses().Wait();

            mockClient.Verify(dyn => dyn.AssociateGovdeliveryEmail("23a4", "8080"));
            mockClient.Verify(dyn => dyn.UpdateStatus("5cd54c07-3391-4bc0-a68e-911c2a38ed0e","Sent"));
            mockClient.Verify(dyn => dyn.UpdateStatus(invalid_email_status_event.external_id, "Failed"));
        }
        public void TestSendEmailsSuccess()
        {
            List<Email> emails = new List<Email>()
            { new Email()
                {
                    to = new List<string>() { "*****@*****.**" },
                    subject = "An email subject",
                    body = "Body content of the email",
                    id = "55a"
                }
            };

            var connector = new Connector();
            connector.links = new List<Link>()
            {
                new Link()
                {
                    rel = "actions",
                    href = "/connectors/123/actions"
                }
            };

            var fakeResponseHandler = new FakeHttpMessageHandler(new List<HttpResponseMessage> () {new HttpResponseMessage(HttpStatusCode.OK)});
            var fakeHttpClient = new HttpClient(fakeResponseHandler);
            fakeHttpClient.BaseAddress = new Uri("https://blah.test.com");

            var mockClient = new Mock<IDynamicsClient>();
            mockClient.Setup(dyn => dyn.UpdateStatus(It.IsAny<string>(), It.IsAny<string>()));
            var hubClient = new HubClient("https://prefix.domain.com", "blah", mockClient.Object);

            var hubConnection = hubClient.GetType().GetField("connection", BindingFlags.NonPublic | BindingFlags.Instance);
            hubConnection.SetValue(hubClient, fakeHttpClient);
            var hubDynamics = hubClient.GetType().GetField("dynamics", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            hubDynamics.SetValue(hubClient, connector);

            hubClient.SendEmails(emails).Wait();

            mockClient.Verify(dyn => dyn.UpdateStatus("55a", "Sending"));
        }