public void ShouldFailIfUserDoesNotExists()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldFailIfUserDoesNotExists));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            var handler    = new GetUserCommandHandler(unitOfWork);

            Assert.ThrowsAsync <UserNotFoundException>(() => handler.Handle(new GetUserCommand("johndoe"), default));
        }
Ejemplo n.º 2
0
        public async Task ShouldFailIfAttemptingToCreateExistingUser()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldFailIfAttemptingToCreateExistingUser));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("johndoe", "Pass@123"));

            var handler = new CreateUserCommandHandler(unitOfWork, InfrastructureFactory.GetMachineDateTime(), InfrastructureFactory.GetSha1Hasher());

            Assert.ThrowsAsync <CreatingExistingUserException>(() => handler.Handle(new CreateUserCommand("johndoe", "Pass@123"), default));
        }
        public async Task ShouldFailAuthenticationIfWrongPassword()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldFailAuthenticationIfWrongPassword));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            var user       = EntityFactory.GetUser("johndoe", "Pass@123");
            await unitOfWork.AddAndSaveAsync(user);

            var handler = new AuthenticateUserCommandHandler(unitOfWork, InfrastructureFactory.GetSha1Hasher(), InfrastructureFactory.GetMachineDateTime());

            Assert.ThrowsAsync <UserAuthenticationFailedException>(() => handler.Handle(new AuthenticateUserCommand("johndoe", "Pass@13", UserAuthConstants.TokenExpirationTime), default));
        }
Ejemplo n.º 4
0
        public async Task ShouldGetEntityFromDbContext()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldGetEntityFromDbContext));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            var user       = EntityFactory.GetUser("johndoe", "Pass@123");
            await unitOfWork.AddAndSaveAsync(user);

            user = await unitOfWork.GetSingleAsync <UserEntity>(u => u.Nickname == "johndoe");

            Assert.NotNull(user);
        }
Ejemplo n.º 5
0
        public async Task ShouldCreateNewUser()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldCreateNewUser));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            var handler    = new CreateUserCommandHandler(unitOfWork, InfrastructureFactory.GetMachineDateTime(), InfrastructureFactory.GetSha1Hasher());

            await handler.Handle(new CreateUserCommand("johndoe", "Pass@123"), default);

            var user = unitOfWork.GetSingleAsync <UserEntity>(u => u.Nickname == "johndoe");

            Assert.NotNull(user);
        }
        public async Task ShouldReturnUser()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldReturnUser));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("johndoe", "Pass@123"));

            var handler = new GetUserCommandHandler(unitOfWork);

            var user = await handler.Handle(new GetUserCommand("johndoe"), default);

            Assert.NotNull(user);
        }
Ejemplo n.º 7
0
        public async Task ShouldAddEntityToDbContext()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldAddEntityToDbContext));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            var user       = EntityFactory.GetUser("johndoe", "Pass@123");
            await unitOfWork.AddAndSaveAsync(user);

            user = await dbContext.Users.FindAsync("johndoe");

            Assert.NotNull(user);
            Assert.AreEqual("johndoe", user.Nickname);
        }
        public async Task ShouldReturnAuthenticatedUser()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldReturnAuthenticatedUser));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            var user       = EntityFactory.GetUser("johndoe", "Pass@123");
            await unitOfWork.AddAndSaveAsync(user);

            var handler        = new AuthenticateUserCommandHandler(unitOfWork, InfrastructureFactory.GetSha1Hasher(), InfrastructureFactory.GetMachineDateTime());
            var authentication = await handler.Handle(new AuthenticateUserCommand("johndoe", "Pass@123", UserAuthConstants.TokenExpirationTime), default);

            Assert.AreEqual("johndoe", authentication.User.Nickname);
            Assert.IsFalse(string.IsNullOrEmpty(authentication.Token));
        }
        public async Task ShouldCreateTokenOnSucesfulAuth()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldChangePasswordSaltAndReHashPasswordOnSuccesfulAuth));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            var user       = EntityFactory.GetUser("johndoe", "Pass@123");
            await unitOfWork.AddAndSaveAsync(user);

            var handler        = new AuthenticateUserCommandHandler(unitOfWork, InfrastructureFactory.GetSha1Hasher(), InfrastructureFactory.GetMachineDateTime());
            var authentication = await handler.Handle(new AuthenticateUserCommand("johndoe", "Pass@123", UserAuthConstants.TokenExpirationTime), default);

            var tokenCount = await dbContext.UserTokens.CountAsync(u => u.Nickname == "johndoe");

            Assert.AreEqual(1, tokenCount);
        }
        public async Task ShouldReturnTrueIfTokenHasNotExpired()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldReturnTrueIfTokenHasNotExpired));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            var dateTime   = new TransientDateTime();

            dateTime.Now = new DateTime(2020, 5, 3);
            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("johndoe", "Pass@123"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetToken("TOKEN", "johndoe", dateTime.Now.AddDays(1)));

            var handler = new CheckAuthorizationCommandHandler(unitOfWork, dateTime);

            Assert.IsTrue(await handler.Handle(new CheckAuthorizationCommand("TOKEN"), default));
        }
Ejemplo n.º 11
0
        public async Task ShouldNotifyChatMessageSent()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldNotifyChatMessageSent));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("johndoe", "Pass@123"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetChat("test", "johndoe"));

            var notifier = InfrastructureFactory.GetNotifierSubstitute();
            var handler  = new ChatMessageSentEventHandler(unitOfWork, InfrastructureFactory.GetMachineDateTime(), notifier);

            await handler.Handle(new ChatMessageSentEvent("Test Message", "johndoe", "test"), default);

            await notifier.Received(1).NotifyAsync("test", ChatConstants.MessageSentActionName, Arg.Any <ChatMessage>());
        }
Ejemplo n.º 12
0
        public async Task ShouldGetAllTheChats()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldGetAllTheChats));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("johndoe", "Pass@123"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetChat("chat1", "johndoe"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetChat("chat2", "johndoe"));

            var handler = new GetAllChatsCommandHandler(unitOfWork);

            var chats = await handler.Handle(new GetAllChatsCommand(), default);

            Assert.AreEqual(2, chats.Count());
        }
        public async Task ShouldOnlyReturnSpecifiedNumberOfMessages()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldOnlyReturnSpecifiedNumberOfMessages));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("johndoe", "Pass@123"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetChat("foo", "johndoe"));

            await unitOfWork.CreateChatMessages("foo", "owner", "Message", 12, new DateTime(2020, 5, 4));

            var handler = new GetChatMessagesCommandHandler(unitOfWork);

            var messages = await handler.Handle(new GetChatMessagesCommand("foo", 10), default);

            Assert.AreEqual(10, messages.Count());
        }
Ejemplo n.º 14
0
        public async Task ShouldUpdateEntityOnDbContext()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldUpdateEntityOnDbContext));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            var user       = EntityFactory.GetUser("johndoe", "Pass@123");
            await unitOfWork.AddAndSaveAsync(user);

            var dateTime = DateTime.Now;

            user.LastLoginDate = dateTime;

            await unitOfWork.UpdateAndSaveAsync(user);

            user = await dbContext.Users.FindAsync("johndoe");

            Assert.AreEqual(dateTime, user.LastLoginDate);
        }
Ejemplo n.º 15
0
        public async Task ShouldSaveMessageSent()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldSaveMessageSent));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("johndoe", "Pass@123"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetChat("test", "johndoe"));

            var handler = new ChatMessageSentEventHandler(unitOfWork, InfrastructureFactory.GetMachineDateTime(), InfrastructureFactory.GetNotifierSubstitute());

            await handler.Handle(new ChatMessageSentEvent("Test Message", "johndoe", "test"), default);

            var message = await unitOfWork.GetSingleAsync <ChatMessageEntity>(m => m.Stock == "test");

            Assert.AreEqual("johndoe", message.FromNickName);
            Assert.AreEqual("Test Message", message.Message);
        }
        public async Task ShouldSortMessagesBySentTime()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldSortMessagesBySentTime));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("johndoe", "Pass@123"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetChat("foo", "johndoe"));

            await unitOfWork.CreateChatMessages("foo", "owner", "Message", 2, new DateTime(2020, 5, 4));

            var handler = new GetChatMessagesCommandHandler(unitOfWork);

            var messages = await handler.Handle(new GetChatMessagesCommand("foo"), default);

            Assert.AreEqual(2, messages.Count());
            Assert.AreEqual(6, messages.First().SentTime.Day);
            Assert.AreEqual(5, messages.Last().SentTime.Day);
        }
        public async Task ShouldChangePasswordSaltAndReHashPasswordOnSuccesfulAuth()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldChangePasswordSaltAndReHashPasswordOnSuccesfulAuth));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            var user       = EntityFactory.GetUser("johndoe", "Pass@123");
            await unitOfWork.AddAndSaveAsync(user);

            var salt          = user.PasswordSalt;
            var passwordHash  = user.PasswordHash;
            var lastLoginDate = user.LastLoginDate;

            var handler        = new AuthenticateUserCommandHandler(unitOfWork, InfrastructureFactory.GetSha1Hasher(), InfrastructureFactory.GetMachineDateTime());
            var authentication = await handler.Handle(new AuthenticateUserCommand("johndoe", "Pass@123", UserAuthConstants.TokenExpirationTime), default);

            user = await unitOfWork.GetSingleAsync <UserEntity>(u => u.Nickname == "johndoe");

            Assert.AreNotEqual(salt, user.PasswordSalt);
            Assert.AreNotEqual(passwordHash, user.PasswordSalt);
            Assert.AreNotEqual(lastLoginDate, user.LastLoginDate);
        }
        public async Task ShouldGetChatMessages()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldGetChatMessages));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("johndoe", "Pass@123"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetChat("foo", "johndoe"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetChatMessage("foo", "johndoe", "Msg 1", DateTime.Now));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetChatMessage("foo", "johndoe", "Msg 2", DateTime.Now));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetChatMessage("foo", "johndoe", "Msg 3", DateTime.Now));

            var handler = new GetChatMessagesCommandHandler(unitOfWork);

            var messages = await handler.Handle(new GetChatMessagesCommand("foo"), default);

            Assert.AreEqual(3, messages.Count());
        }
        public async Task ShouldSortParticipantsAlphabetically()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldSortParticipantsAlphabetically));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("johndoe", "Pass@123"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("janedoe", "Pass@123"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetChat("foo", "johndoe"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetParticipant("foo", "johndoe"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetParticipant("foo", "janedoe"));

            var handler = new GetChatParticipantsCommandHandler(unitOfWork);

            var participants = await handler.Handle(new GetChatParticipantsCommand("foo"), default);

            Assert.AreEqual(2, participants.Count());
            Assert.AreEqual("janedoe", participants.First().Nickname);
            Assert.AreEqual("johndoe", participants.Last().Nickname);
        }
        public async Task ShouldMakeUserParticipantInChat()
        {
            var dbContext  = InfrastructureFactory.GetInMemoryContext(nameof(ShouldMakeUserParticipantInChat));
            var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext);
            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("johndoe", "Pass@123"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("admin", "Pass@123"));

            await unitOfWork.AddAndSaveAsync(EntityFactory.GetChat("foo", "admin"));

            var mediator = InfrastructureFactory.GetMediatorSubstitute();
            var handler  = new JoinChatCommandHandler(unitOfWork, mediator, InfrastructureFactory.GetMachineDateTime());

            await handler.Handle(new JoinChatCommand("johndoe", "foo"), default);

            var pars = await unitOfWork.FetchAsync <ChatParticipantEntity, ChatParticipant>(
                participants =>
                participants.Where(p => p.Stock == "foo"));

            Assert.AreEqual(1, pars.Count());
            Assert.AreEqual("johndoe", pars.First().Nickname);
        }