public async Task Create_ReturnsNewNotification()
        {
            string usersJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"Users.json");
            var    user      = JsonConvert.DeserializeObject <List <User> >(usersJson).First(u => u.Id == TESTING_USER_ID);

            const string title   = "Test title";
            const string content = "Test content";

            var userNotification = new UserNotification
            {
                Title   = title,
                Content = content,
                IsRead  = false
            };

            UserRepository.Setup(u => u.GetByKey(TESTING_USER_ID)).Returns(user);

            UserNotificationRepository.Setup(n => n.Create(userNotification)).ReturnsAsync(userNotification);


            var resultUserNotification = await UserNotificationService.Create(userNotification);


            Assert.NotNull(resultUserNotification);
            Assert.NotNull(resultUserNotification.User);
            Assert.Equal(title, resultUserNotification.Title);
            Assert.Equal(content, resultUserNotification.Content);
            Assert.Equal(DateTime.Today, resultUserNotification.CreatedDate);
            Assert.False(resultUserNotification.IsRead);
        }
        public async Task Update_ReturnsUpdatedNotification()
        {
            string userNotificationsJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"UserNotifications.json");
            var    notification          = JsonConvert.DeserializeObject <List <UserNotification> >(userNotificationsJson).First(u => u.User.Id == TESTING_USER_ID);

            const string newTitle   = "Test title";
            const string newContent = "Test content";

            var userNotification = new UserNotification
            {
                Id      = notification.Id,
                Title   = newTitle,
                Content = newContent,
                IsRead  = true
            };

            UserNotificationRepository.Setup(u => u.GetByKey(userNotification.Id)).Returns(notification);

            UserNotificationRepository.Setup(n => n.Update(notification)).ReturnsAsync(notification);


            var resultUserNotification = await UserNotificationService.Update(userNotification);


            Assert.NotNull(resultUserNotification);
            Assert.Equal(newTitle, resultUserNotification.Title);
            Assert.Equal(newContent, resultUserNotification.Content);
            Assert.Equal(notification.CreatedDate, resultUserNotification.CreatedDate);
            Assert.True(resultUserNotification.IsRead);
        }
Example #3
0
        public async Task GenerateNotificationsAsync_With1EventFor1UsersWhoHasNotAlreadyReceivedTheEmail_Sends1Email()
        {
            // Arrange
            List <Event> events = GetEventList1();
            List <User>  users  = GetUserList1();

            // The user is attending all available events
            EventAttendeeRepository.Setup(ea => ea.GetEventsUserIsAttending(It.IsAny <Guid>(), It.IsAny <bool>(), It.IsAny <CancellationToken>())).ReturnsAsync(events);
            EventRepository.Setup(e => e.GetActiveEvents(It.IsAny <CancellationToken>())).ReturnsAsync(events);
            UserRepository.Setup(u => u.GetAllUsers(It.IsAny <CancellationToken>())).ReturnsAsync(users);

            var userNotifications = new List <UserNotification>();

            UserNotificationRepository.Setup(ea => ea.GetUserNotifications(It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(userNotifications);

            // Act
            await Engine.GenerateNotificationsAsync().ConfigureAwait(false);

            // Assert
            UserRepository.Verify(_ => _.GetAllUsers(It.IsAny <CancellationToken>()), Times.Once);
            UserNotificationPreferenceRepository.Verify(_ => _.GetUserNotificationPreferences(It.IsAny <Guid>(), It.IsAny <CancellationToken>()), Times.Once);
            EventRepository.Verify(_ => _.GetActiveEvents(It.IsAny <CancellationToken>()), Times.Once);
            EventAttendeeRepository.Verify(_ => _.GetEventsUserIsAttending(It.IsAny <Guid>(), It.IsAny <bool>(), It.IsAny <CancellationToken>()), Times.Once);
            UserNotificationRepository.Verify(_ => _.AddUserNotification(It.IsAny <UserNotification>()), Times.Once);
            EmailSender.Verify(_ => _.SendEmailAsync(It.IsAny <Email>(), It.IsAny <CancellationToken>()), Times.Once);
        }
        public void GetUnreadCount_ReturnsCount()
        {
            string userNotificationsJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"UserNotifications.json");
            var    notifications         = JsonConvert.DeserializeObject <List <UserNotification> >(userNotificationsJson)
                                           .Where(n => n.User.Id == TESTING_USER_ID && !n.IsRead);

            UserNotificationRepository.Setup(n => n.GetUnread()).Returns(notifications);


            int unreadNotificationsCount = UserNotificationService.GetUnreadCount();


            Assert.Equal(notifications.Count(), unreadNotificationsCount);
        }
        public async Task Delete_ReturnsDeletedNotification()
        {
            string userNotificationsJson = File.ReadAllText(TestConfiguration.MockDataFolderPath + @"UserNotifications.json");
            var    notification          = JsonConvert.DeserializeObject <List <UserNotification> >(userNotificationsJson).First(u => u.User.Id == TESTING_USER_ID);

            UserNotificationRepository.Setup(u => u.GetByKey(notification.Id)).Returns(notification);

            UserNotificationRepository.Setup(n => n.Delete(notification.Id)).ReturnsAsync(notification);


            var resultUserNotification = await UserNotificationService.Delete(notification.Id);


            Assert.NotNull(resultUserNotification);
            Assert.Equal(notification.Title, resultUserNotification.Title);
            Assert.Equal(notification.Content, resultUserNotification.Content);
            Assert.Equal(notification.IsRead, resultUserNotification.IsRead);
            Assert.Equal(notification.CreatedDate, resultUserNotification.CreatedDate);
        }
        public async Task GenerateNotificationsAsync_With1EventFor1UsersWhoHasAlreadyReceivedTheEmail_SendsNoEmail()
        {
            // Arrange
            List <Event> events = GetEventList1();
            List <User>  users  = GetUserList1();

            events[0].CreatedByUserId = users[0].Id;

            // The user is attending all available events
            EventAttendeeRepository.Setup(ea => ea.GetEventsUserIsAttending(It.IsAny <Guid>(), It.IsAny <bool>(), It.IsAny <CancellationToken>())).ReturnsAsync(events);
            EventRepository.Setup(e => e.GetActiveEvents(It.IsAny <CancellationToken>())).ReturnsAsync(events);
            UserRepository.Setup(u => u.GetAllUsers(It.IsAny <CancellationToken>())).ReturnsAsync(users);

            // The user has already received notifications for all events
            var userNotification = new UserNotification()
            {
                EventId  = events[0].Id,
                UserId   = users[0].Id,
                SentDate = DateTimeOffset.UtcNow.AddDays(-1),
                UserNotificationTypeId = (int)NotificationType,
            };

            var userNotifications = new List <UserNotification>()
            {
                userNotification
            };

            UserNotificationRepository.Setup(ea => ea.GetUserNotifications(It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(userNotifications);

            // Act
            await Engine.GenerateNotificationsAsync().ConfigureAwait(false);

            // Assert
            UserRepository.Verify(_ => _.GetAllUsers(It.IsAny <CancellationToken>()), Times.Once);
            UserNotificationPreferenceRepository.Verify(_ => _.GetUserNotificationPreferences(It.IsAny <Guid>(), It.IsAny <CancellationToken>()), Times.Once);
            EventRepository.Verify(_ => _.GetActiveEvents(It.IsAny <CancellationToken>()), Times.Once);
            UserNotificationRepository.Verify(_ => _.AddUserNotification(It.IsAny <UserNotification>()), Times.Never);
            EmailSender.Verify(_ => _.SendEmailAsync(It.IsAny <Email>(), It.IsAny <CancellationToken>()), Times.Never);
        }