private void AssertSentFailed(NotificationTest test, NotificationAccount <Guid> account, PushNotification expectedNotification, NotificationErrorCode expectedErrorCode) { NotificationSendFailure notificationSendFailure = test.RaisedEvents.OfType <NotificationSendFailure>().SingleOrDefault(); notificationSendFailure.Should().NotBeNull(); notificationSendFailure.AccountId.ShouldBeEquivalentTo(account.AccountId.ToString()); notificationSendFailure.Description.Should().NotBeNull(); notificationSendFailure.ErrorCode.ShouldBeEquivalentTo(expectedErrorCode); if (expectedErrorCode == NotificationErrorCode.RouteFailure) { notificationSendFailure.EndpointName.ShouldBeEquivalentTo(Constants.PushNotificationEndPoint); } else { notificationSendFailure.EndpointName.Should().BeNull(); } if (account.MobileDevices != null && account.MobileDevices.Any()) { notificationSendFailure.Destination.ShouldBeEquivalentTo(account.MobileDevices.Single().DeviceId); } else { notificationSendFailure.Destination.Should().BeNull(); } this.AssertNotification((PushNotification)notificationSendFailure.Notification, expectedNotification); }
private void AssertSentSuccessful(NotificationTest test, NotificationAccount <Guid> account, PushNotification expectedNotification) { PushNotification sentNotification = (PushNotification)test.GetSentNotification(); this.AssertNotification(sentNotification, expectedNotification); this.AssertNotificationSentEventRaised(test, account, expectedNotification); }
private bool ValidateDevice <TAccountKey>(NotificationAccount <TAccountKey> account, MobileDevice mobileDevice, PushNotification notification) { if (!mobileDevice.ArePushNotificationsEnabled) { this.OnFailed( notification, NotificationErrorCode.Disabled, account.AccountId.ToString(), "The notification could not be sent because push notifications on the destination device are not enabled.", mobileDevice.DeviceId); return(false); } if (!this.ValidateAppVersion(mobileDevice, notification)) { this.OnFailed( notification, NotificationErrorCode.VersionMismatch, account.AccountId.ToString(), "The app version on the mobile device does not support the notification.", mobileDevice.DeviceId); return(false); } return(true); }
private void AssertNotificationScheduledSuccessful(NotificationTest test, NotificationAccount <Guid> account, Notification expectedNotification, DateTimeOffset expectedDeliveryDateTime) { ScheduledNotification <Guid> scheduledNotification = test.GetScheduledNotification(); scheduledNotification.AccountId.ShouldBeEquivalentTo(account.AccountId); scheduledNotification.DeliveryDateTime.ShouldBeEquivalentTo(expectedDeliveryDateTime); this.AssertNotificationScheduledEventRaised(test, account, expectedNotification, expectedDeliveryDateTime); }
private void AssertNotificationScheduledEventRaised(NotificationTest test, NotificationAccount <Guid> account, Notification expectedNotification, DateTimeOffset expectedDeliveryDateTime) { NotificationScheduled notificationScheduled = test.RaisedEvents.OfType <NotificationScheduled>().SingleOrDefault(); notificationScheduled.Should().NotBeNull(); notificationScheduled.AccountId.ShouldBeEquivalentTo(account.AccountId.ToString()); notificationScheduled.DeliveryDateTime.ShouldBeEquivalentTo(expectedDeliveryDateTime); notificationScheduled.Notification.Should().BeSameAs(expectedNotification); }
/// <summary> /// Adds a notification account to the test. /// </summary> /// <param name="accountId">An account identifier.</param> /// <param name="email">An email for the account.</param> /// <param name="name">A name for the account.</param> /// <param name="timeZone">The time zone for the account.</param> /// <param name="mobileDevice">The mobile device to associate with the account.</param> /// <returns>The account that was added to the repository.</returns> public NotificationAccount <Guid> AddNotificationAccount(Guid accountId, string email, string name, string timeZone, MobileDevice mobileDevice) { NotificationAccount <Guid> account = mobileDevice != null ? new NotificationAccount <Guid>(accountId, email, name, timeZone, new[] { mobileDevice }) : new NotificationAccount <Guid>(accountId, email, name, timeZone, null); this.NotificationAccountRepository.GetAccount(accountId).Returns(account); return(account); }
public void SendVersionedPushNotificationToDeviceWithoutWithInvalidVersionTest() { NotificationTest test = new NotificationTest(); PushNotificationChannel channel = new PushNotificationChannel(this.CreateMobileServiceClient(), test.EventPublisher); PushNotification notification = this.CreateNotification(new Version(1, 0, 0)); NotificationAccount <Guid> account = test.AddNotificationAccount(Guid.NewGuid(), mobileAppVersion: "Invalid"); channel.SendAsync(account, notification).Wait(); this.AssertSentFailed(test, account, notification, NotificationErrorCode.VersionMismatch); }
public void SendPushNotificationForAnOutdatedVersionOfTheApplicationTest() { NotificationTest test = new NotificationTest(); PushNotificationChannel channel = new PushNotificationChannel(this.CreateMobileServiceClient(), test.EventPublisher); PushNotification notification = this.CreateNotification(new Version(1, 0, 0), new Version(1, 0, 2)); NotificationAccount <Guid> account = test.AddNotificationAccount(Guid.NewGuid(), mobileAppVersion: "1.0.3"); channel.SendAsync(account, notification).Wait(); this.AssertSentFailed(test, account, notification, NotificationErrorCode.VersionMismatch); }
public void SendPushNotificationWithOnlyMinimumAppVersionSpecifiedTest() { NotificationTest test = new NotificationTest(); PushNotificationChannel channel = new PushNotificationChannel(this.CreateMobileServiceClient(), test.EventPublisher); PushNotification notification = this.CreateNotification(new Version(1, 0, 0)); NotificationAccount <Guid> account = test.AddNotificationAccount(Guid.NewGuid(), mobileAppVersion: "1.0.3"); channel.SendAsync(account, notification).Wait(); this.AssertSentSuccessful(test, account, notification); }
public void SendPushNotificationForAccountWithoutMobileDeviceTest() { NotificationTest test = new NotificationTest(); PushNotificationChannel channel = new PushNotificationChannel(this.CreateMobileServiceClient(), test.EventPublisher); PushNotification notification = this.CreateNotification(); NotificationAccount <Guid> account = test.AddNotificationAccount(Guid.NewGuid(), "*****@*****.**", "Joe", null, (MobileDevice)null); channel.SendAsync(account, notification).Wait(); this.AssertSentFailed(test, account, notification, NotificationErrorCode.MobileDeviceNotRegistered); }
private IEnumerable <Task> SendToAllDevicesAsync <TAccountKey>(NotificationAccount <TAccountKey> account, PushNotification notification) { List <Task> tasks = new List <Task>(); foreach (MobileDevice mobileDevice in account.MobileDevices) { if (this.ValidateDevice(account, mobileDevice, notification)) { tasks.Add(this.SendAsync(account, mobileDevice, notification)); } } return(tasks); }
public void SendInvalidEndPointPushNotificationTest() { NotificationTest test = new NotificationTest(); IMobilePushNotificationServiceClient mobileServiceClient = this.CreateMobileServiceClient(); PushNotificationChannel channel = new PushNotificationChannel(mobileServiceClient, test.EventPublisher); PushNotification notification = this.CreateNotification(); NotificationAccount <Guid> account = test.AddNotificationAccount(); this.PushNotificationShouldFailAtEndPoint(mobileServiceClient); channel.SendAsync(account, notification).Wait(); this.AssertSentFailed(test, account, notification, NotificationErrorCode.RouteFailure); }
/// <summary> /// Sends a notification to the specified account. /// </summary> /// <param name="account">An account to send the notification to.</param> /// <param name="notification">A notification to send.</param> /// <returns>The task for the operation.</returns> protected override Task DoSendAsync <TAccountKey>(NotificationAccount <TAccountKey> account, PushNotification notification) { if (account.MobileDevices == null || !account.MobileDevices.Any()) { this.OnFailed( notification, NotificationErrorCode.MobileDeviceNotRegistered, account.AccountId.ToString(), "No mobile devices have been registered to receive the push notification."); return(Task.FromResult(false)); } return(Task.WhenAll(this.SendToAllDevicesAsync(account, notification))); }
public void SchedulePushNotificationWithoutTimeZoneTest() { NotificationTest test = new NotificationTest(); NotificationScheduler <Guid> scheduler = this.CreateScheduler(test); PushNotification notification = this.CreateNotification(); NotificationAccount <Guid> account = test.AddNotificationAccount(Guid.NewGuid(), timeZone: null); DateTime deliveryDate = DateTime.UtcNow.AddDays(1); scheduler.Schedule(account.AccountId, notification, deliveryDate); DateTimeOffset expectedDeliveryDate = deliveryDate.ToDateTimeOffset("US/Eastern"); this.AssertNotificationScheduledSuccessful(test, account, notification, expectedDeliveryDate); }
public void SendOverDueNotificationTest() { NotificationTest test = new NotificationTest(); INotificationChannel channel; NotificationScheduler <Guid> scheduler = this.CreateScheduler(test, out channel); DateTime deliveryDate = DateTime.UtcNow; NotificationAccount <Guid> account = test.AddNotificationAccount(Guid.NewGuid(), "US/Pacific"); PushNotification notificationToSend = this.CreateNotification(); test.NotificationRepository.GetScheduledNotifications(Arg.Any <DateTime>()).Returns(new[] { new ScheduledNotification <Guid>(account.AccountId, notificationToSend, DateTime.UtcNow) }); scheduler.SendScheduledNotifications(DateTime.UtcNow.AddDays(1)).Wait(); channel.Received().SendAsync(account, notificationToSend); test.NotificationRepository.Received().DeleteScheduledNotification(notificationToSend.Id); }
private async Task SendAsync <TAccountKey>(NotificationAccount <TAccountKey> account, MobileDevice mobileDevice, PushNotification notification) { PushNotificationResult result = await this.mobileNotificationServiceClient.SendNotification(mobileDevice, notification).ConfigureAwait(false); if (result.WasSuccessful) { this.OnSuccess(notification, account.AccountId.ToString(), mobileDevice.DeviceId, result.EndpointName); } else { this.OnFailed( notification, NotificationErrorCode.RouteFailure, account.AccountId.ToString(), result.ErrorMessage, mobileDevice.DeviceId, result.EndpointName); } }
private void AssertNotificationSentEventRaised(NotificationTest test, NotificationAccount <Guid> account, PushNotification expectedNotification) { NotificationSent notificationSent = test.RaisedEvents.OfType <NotificationSent>().SingleOrDefault(); notificationSent.Should().NotBeNull(); notificationSent.AccountId.ShouldBeEquivalentTo(account.AccountId.ToString()); notificationSent.Notification.NotificationType.ShouldBeEquivalentTo(NotificationType.Push); string expectedDeviceId = null; if (account.MobileDevices != null && account.MobileDevices.Any()) { MobileDevice device = account.MobileDevices.Single(); expectedDeviceId = device.DeviceId; } notificationSent.Destination.ShouldBeEquivalentTo(expectedDeviceId); notificationSent.EndpointName.ShouldBeEquivalentTo(Constants.PushNotificationEndPoint); this.AssertNotification((PushNotification)notificationSent.Notification, expectedNotification); }