Ejemplo n.º 1
0
        public void Details_AuthorizedMessageReceiver_ReturnsViewWithMessageOfGivenId()
        {
            var(context, controller) = CreateTestTools(
                nameof(Details_AuthorizedMessageReceiver_ReturnsViewWithMessageOfGivenId)
                );
            var testUser  = UserDataManager.AuthorizeControllerUser(controller, context);
            var otherUser = UserDataManager.CreateTestUser(context, Role.USER, "*****@*****.**");

            var message = MessageDataManager.CreateMessage(
                context, "Test", "Test", otherUser, new List <User> {
                testUser
            }, new List <User>(), new List <User>()
                );

            MessageDataManager.CreateMessage(
                context, "Test2", "Test2", otherUser, new List <User> {
                testUser
            }, new List <User>(), new List <User>()
                );

            var result = controller.Details(message.MessageId).Result as ViewResult;

            Assert.NotNull(result);

            var modelMessage = result.Model as Message;

            Assert.NotNull(modelMessage);
            Assert.Equal(message.MessageId, modelMessage.Id);
        }
Ejemplo n.º 2
0
        public void SetReadByIdAsync_SetReadByBcc_MessageStateChangedForRightUserOnly()
        {
            var(context, messageService) = CreateTestTools(
                nameof(SetReadByIdAsync_SetReadByBcc_MessageStateChangedForRightUserOnly)
                );
            var sender   = UserDataManager.CreateTestUser(context);
            var receiver = UserDataManager.CreateUser(context, "*****@*****.**");
            var ccUser   = UserDataManager.CreateUser(context, "*****@*****.**");
            var bccUser  = UserDataManager.CreateUser(context, "*****@*****.**");
            var otherBcc = UserDataManager.CreateUser(context, "*****@*****.**");

            var message = MessageDataManager.CreateMessage(
                context, "Test", "Test", sender, new List <Database.POCO.User> {
                receiver
            },
                new List <Database.POCO.User> {
                ccUser
            }, new List <Database.POCO.User> {
                bccUser, otherBcc
            }
                );

            Assert.False(message.MessagesBCCs.First(b => b.User.Equals(bccUser)).IsRead);
            bool receiverRead = message.MessagesReceivers.First(r => r.User.Equals(receiver)).IsRead;
            bool ccRead       = message.MessagesCCs.First(c => c.User.Equals(ccUser)).IsRead;
            bool otherRead    = message.MessagesBCCs.First(b => b.User.Equals(otherBcc)).IsRead;

            messageService.SetReadByIdAsync(message.MessageId, new User(bccUser), true).Wait();
            var updated = context.Messages.Find(message.MessageId);

            Assert.True(updated.MessagesBCCs.First(b => b.User.Equals(bccUser)).IsRead);
            Assert.Equal(receiverRead, message.MessagesReceivers.First(r => r.User.Equals(receiver)).IsRead);
            Assert.Equal(ccRead, message.MessagesCCs.First(c => c.User.Equals(ccUser)).IsRead);
            Assert.Equal(otherRead, message.MessagesBCCs.First(b => b.User.Equals(otherBcc)).IsRead);
        }
Ejemplo n.º 3
0
        public void UpdateAsync_AddingReceivers_UpdatesMessageInDatabase()
        {
            var(context, messageService) = CreateTestTools(
                nameof(UpdateAsync_AddingReceivers_UpdatesMessageInDatabase)
                );
            var testUser  = UserDataManager.CreateTestUser(context);
            var otherUser = UserDataManager.CreateUser(context, "*****@*****.**");
            var msgEntity = MessageDataManager.CreateMessage(
                context, "Test", "Test", testUser, new List <Database.POCO.User> {
                otherUser
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>()
                );

            var message = new Message(msgEntity, new User(msgEntity.Sender));

            message.Receivers = msgEntity.Receivers.Select(r => new User(r)).ToList();
            message.AddReceiver(new User(testUser));

            var task = messageService.UpdateAsync(message);

            task.Wait();

            msgEntity.Receivers.Add(testUser);
            var updatedEntity = context.Messages.Find(msgEntity.MessageId);

            Assert.Equal(msgEntity, updatedEntity);
        }
Ejemplo n.º 4
0
        public void GetByIdAsync_MultipleMessages_ReturnsMessageWithGivenId()
        {
            var(context, messageService) = CreateTestTools(
                nameof(GetByIdAsync_MultipleMessages_ReturnsMessageWithGivenId)
                );
            var testUser = UserDataManager.CreateTestUser(context);

            var message = MessageDataManager.CreateMessage(
                context, "Test 1", "Test", testUser, new List <Database.POCO.User> {
                testUser
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>()
                );

            MessageDataManager.CreateMessage(
                context, "Test 2", "Test", testUser, new List <Database.POCO.User> {
                testUser
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>()
                );

            var found = messageService.GetByIdAsync(message.MessageId).Result;

            Assert.Equal(message.MessageId, found.Id);
        }
Ejemplo n.º 5
0
        public void GetAllToUser_MultipleMessages_ReturnsOnlySentToGivenUser()
        {
            var(context, messageService) = CreateTestTools(
                nameof(GetAllToUser_MultipleMessages_ReturnsOnlySentToGivenUser)
                );
            var sender        = UserDataManager.CreateTestUser(context);
            var receiver      = UserDataManager.CreateUser(context, "*****@*****.**");
            var otherReceiver = UserDataManager.CreateUser(context, "*****@*****.**");

            var recv1 = MessageDataManager.CreateMessage(
                context, "Test 1", "Test", sender, new List <Database.POCO.User> {
                receiver
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>()
                );
            var recv2 = MessageDataManager.CreateMessage(
                context, "Test 2", "Test", sender, new List <Database.POCO.User> {
                receiver, otherReceiver
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>()
                );
            var recv3 = MessageDataManager.CreateMessage(
                context, "Test 3", "Test", sender, new List <Database.POCO.User> {
                otherReceiver
            },
                new List <Database.POCO.User> {
                receiver
            }, new List <Database.POCO.User>()
                );
            var recv4 = MessageDataManager.CreateMessage(
                context, "Test 4", "Test", sender, new List <Database.POCO.User> {
                otherReceiver
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User> {
                receiver
            }
                );
            var notRecv1 = MessageDataManager.CreateMessage(
                context, "Test 5", "Test", sender, new List <Database.POCO.User> {
                otherReceiver
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>()
                );

            var ids = messageService.GetAllToUser(new User(receiver)).Select(m => m.Id).ToList();

            Assert.Contains(recv3.MessageId, ids);
            Assert.Contains(recv4.MessageId, ids);
            Assert.Contains(recv1.MessageId, ids);
            Assert.Contains(recv2.MessageId, ids);
            Assert.DoesNotContain(notRecv1.MessageId, ids);
        }
Ejemplo n.º 6
0
        public void Index_UserAuthorized_ReturnsMessagesReceivedByUser()
        {
            var(context, controller) = CreateTestTools(nameof(Index_UserAuthorized_ReturnsMessagesReceivedByUser));
            var testUser  = UserDataManager.AuthorizeControllerUser(controller, context);
            var otherUser = UserDataManager.CreateTestUser(context, Role.USER, "*****@*****.**");

            var received = MessageDataManager.CreateMessage(
                context, "Test", "Test", otherUser, new List <User> {
                testUser
            }, new List <User>(), new List <User>()
                );
            var cc = MessageDataManager.CreateMessage(
                context, "Test", "Test", otherUser, new List <User>(), new List <User> {
                testUser
            }, new List <User>()
                );
            var bcc = MessageDataManager.CreateMessage(
                context, "Test", "Test", otherUser, new List <User>(), new List <User>(), new List <User> {
                testUser
            }
                );
            var other = MessageDataManager.CreateMessage(
                context, "Test", "Test", testUser, new List <User> {
                otherUser
            }, new List <User> {
                otherUser
            },
                new List <User> {
                otherUser
            }
                );

            var result = controller.Index().Result as ViewResult;

            Assert.NotNull(result);

            var model = result.Model as InboxViewModel;

            Assert.NotNull(model);
            Assert.Equal(testUser.Email, model.User.Email);

            var msgIds = model.Messages.Select(m => m.Id).ToList();

            Assert.DoesNotContain(other.MessageId, msgIds);
            Assert.Contains(received.MessageId, msgIds);
            Assert.Contains(cc.MessageId, msgIds);
            Assert.Contains(bcc.MessageId, msgIds);
        }
Ejemplo n.º 7
0
        public void Details_AuthorizedUserNotReceiver_ReturnsNotFound()
        {
            var(context, controller) = CreateTestTools(nameof(Details_AuthorizedUserNotReceiver_ReturnsNotFound));
            var testUser  = UserDataManager.AuthorizeControllerUser(controller, context);
            var otherUser = UserDataManager.CreateTestUser(context, Role.USER, "*****@*****.**");

            var message = MessageDataManager.CreateMessage(
                context, "Test", "Test", testUser, new List <User> {
                otherUser
            }, new List <User>(), new List <User>()
                );

            var result = controller.Details(message.MessageId).Result;

            Assert.True(result is NotFoundResult);
        }
Ejemplo n.º 8
0
        public void UpdateAsync_AddingReceivers_ReturnsUpdatedMessage()
        {
            var(context, messageService) = CreateTestTools(nameof(UpdateAsync_AddingReceivers_ReturnsUpdatedMessage));
            var testUser  = UserDataManager.CreateTestUser(context);
            var otherUser = UserDataManager.CreateUser(context, "*****@*****.**");
            var msgEntity = MessageDataManager.CreateMessage(
                context, "Test", "Test", testUser, new List <Database.POCO.User> {
                otherUser
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>()
                );

            var message = new Message(context.Messages.Find(msgEntity.MessageId), new User(msgEntity.Sender));

            message.AddReceiver(new User(testUser));

            var updated = messageService.UpdateAsync(message).Result;

            AssertMessagesEqual(message, updated);
        }
Ejemplo n.º 9
0
        public void SetReadByIdAsync_SetReadByReceiver_ReturnsUpdatedMessage()
        {
            var(context, messageService) = CreateTestTools(
                nameof(SetReadByIdAsync_SetReadByReceiver_ReturnsUpdatedMessage)
                );
            var sender   = UserDataManager.CreateTestUser(context);
            var receiver = UserDataManager.CreateUser(context, "*****@*****.**");

            var message = MessageDataManager.CreateMessage(
                context, "Test", "Test", sender, new List <Database.POCO.User> {
                receiver
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>()
                );

            var updated = messageService.SetReadByIdAsync(message.MessageId, new User(receiver), true).Result;

            AssertMessagesEqual(updated, message);
            Assert.True(updated.IsReadBy(new User(receiver)));
        }
Ejemplo n.º 10
0
        public void MarkRead_UserAuthorized_UpdatesMessageInDatabase()
        {
            var(context, controller) = CreateTestTools(nameof(MarkRead_UserAuthorized_UpdatesMessageInDatabase));
            var userEntity = UserDataManager.AuthorizeControllerUser(controller, context);
            var user       = new User(userEntity);
            var otherUser  = UserDataManager.CreateUser(context, "*****@*****.**");

            var message = MessageDataManager.CreateMessage(
                context, "Test", "Test", otherUser, new List <Database.POCO.User> {
                userEntity
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>()
                );

            controller.MarkRead(message.MessageId, user).Wait();

            var updatedMessage = context.Messages.Find(message.MessageId);

            Assert.True(updatedMessage.MessagesReceivers.First(r => r.User.Email.Equals(user.Email)).IsRead);
        }
Ejemplo n.º 11
0
        public void GetNewerThan_MultipleOlderMultipleNewer_ReturnsAllNewer()
        {
            var(context, messageService) = CreateTestTools(
                nameof(GetNewerThan_MultipleOlderMultipleNewer_ReturnsAllNewer)
                );
            var testUser  = UserDataManager.CreateTestUser(context);
            var otherUser = UserDataManager.CreateUser(context, "*****@*****.**");
            var timeSpan  = TimeSpan.FromSeconds(10);

            var older1 = MessageDataManager.CreateMessage(
                context, "Test 1", "Old message", testUser, new List <Database.POCO.User> {
                otherUser
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>(), DateTime.Now - 2 * timeSpan
                );
            var older2 = MessageDataManager.CreateMessage(
                context, "Test 2", "Old message", testUser, new List <Database.POCO.User> {
                otherUser
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>(), DateTime.Now - 2 * timeSpan
                );
            var newer1 = MessageDataManager.CreateMessage(
                context, "Test 3", "New message", testUser, new List <Database.POCO.User> {
                otherUser
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>()
                );
            var newer2 = MessageDataManager.CreateMessage(
                context, "Test 4", "New message", testUser, new List <Database.POCO.User> {
                otherUser
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>()
                );

            var ids = messageService.GetNewerThan(DateTime.Now - timeSpan).Select(m => m.Id).ToList();

            Assert.DoesNotContain(older1.MessageId, ids);
            Assert.DoesNotContain(older2.MessageId, ids);
            Assert.Contains(newer1.MessageId, ids);
            Assert.Contains(newer2.MessageId, ids);
        }
Ejemplo n.º 12
0
        public void MarkUnread_UserAuthorized_ReturnsUpdatedMessage()
        {
            var(context, controller) = CreateTestTools(nameof(MarkUnread_UserAuthorized_ReturnsUpdatedMessage));
            var userEntity = UserDataManager.AuthorizeControllerUser(controller, context);
            var user       = new User(userEntity);
            var otherUser  = UserDataManager.CreateUser(context, "*****@*****.**");

            var message = MessageDataManager.CreateMessage(
                context, "Test", "Test", otherUser, new List <Database.POCO.User> {
                userEntity
            },
                new List <Database.POCO.User>(), new List <Database.POCO.User>()
                );

            var result = controller.MarkUnread(message.MessageId, user).Result as JsonResult;

            Assert.NotNull(result);

            var updatedMessage = result.Value as Message;

            Assert.NotNull(updatedMessage);
            Assert.Equal(message.MessageId, updatedMessage.Id);
            Assert.False(updatedMessage.IsReadBy(user));
        }
Ejemplo n.º 13
0
        public void SendNotifications_MultipleMessagesOldAndNew_GeneratesNotificationsMatchingNewMessages()
        {
            var(context, messageService) = CreateTestTools(
                nameof(SendNotifications_MultipleMessagesOldAndNew_GeneratesNotificationsMatchingNewMessages)
                );
            var user1 = UserDataManager.CreateUser(context, "*****@*****.**");
            var user2 = UserDataManager.CreateUser(context, "*****@*****.**");
            var user3 = UserDataManager.CreateUser(context, "*****@*****.**");
            var user4 = UserDataManager.CreateUser(context, "*****@*****.**");

            var timeSpan = TimeSpan.FromSeconds(10);

            MessageDataManager.CreateMessage(
                context, "Test 1", "Old message", user1, new List <User> {
                user2
            }, new List <User> {
                user3
            },
                new List <User> {
                user4
            }, DateTime.Now - 2 * timeSpan
                );
            MessageDataManager.CreateMessage(
                context, "Test 2", "Old message", user4, new List <User> {
                user1
            }, new List <User> {
                user2
            },
                new List <User> {
                user3
            }, DateTime.Now - 2 * timeSpan
                );
            var newMsg1 = MessageDataManager.CreateMessage(
                context, "Test 3", "New message", user3, new List <User> {
                user4
            }, new List <User> {
                user1
            },
                new List <User> {
                user2
            }
                );
            var newMsg2 = MessageDataManager.CreateMessage(
                context, "Test 4", "New message", user2, new List <User> {
                user3
            }, new List <User> {
                user4
            },
                new List <User> {
                user1
            }
                );

            var messages = new List <Message> {
                newMsg1, newMsg2
            }.Select(MessageDataManager.CreateMessage).ToList();
            var notifyService = new MockNotifyService(messages);

            var notificationSender = new NotificationSender();

            notificationSender.SendNotifications(messageService, notifyService, timeSpan);
            notifyService.AssertNotificationMatchMessages();
        }