Esempio n. 1
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));
        }
Esempio n. 2
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);
        }
Esempio n. 3
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>());
        }
Esempio n. 4
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 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);
        }
        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 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 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));
        }
        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));
        }