Ejemplo n.º 1
0
        public async Task Register_NewUser_ShouldThrowBadRequestException(string exceptionMessage)
        {
            // Arrange
            var users = new List <UserModel> {
                new UserModel {
                    Username  = "******",
                    Password  = "******",
                    FirstName = "Ftest",
                    LastName  = "Ltest"
                }
            };
            var mock = users.AsQueryable().BuildMock();
            Mock <IRepository <UserModel> > userRepo = new Mock <IRepository <UserModel> >();

            userRepo.Setup(repo => repo.FindAll()).Returns(mock.Object);
            userRepo.Setup(repo => repo.AddAsync(It.IsAny <UserModel>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(new UserModel()));
            var mapperMock = new Mock <IMapper>();

            mapperMock.Setup(m => m.Map <UserModel>(It.IsAny <User>()))
            .Returns(new UserModel());
            var handler = new RegisterCommandHandler(userRepo.Object, mapperMock.Object);

            // Act
            var ex = await Assert.ThrowsAsync <BadRequestException>(() => handler.Handle(new RegisterCommand {
                Username  = "******",
                Password  = "******",
                FirstName = "Ftest",
                LastName  = "Ltest"
            }, It.IsAny <CancellationToken>()));

            // Assert
            ex.Message.Should().Be(string.Format(exceptionMessage, "testUsername"));
        }
Ejemplo n.º 2
0
        public async Task Register_NewUser_ShouldPersistNewUser()
        {
            // Arrange
            var users = new List <UserModel>();
            var mock  = users.AsQueryable().BuildMock();
            Mock <IRepository <UserModel> > userRepo = new Mock <IRepository <UserModel> >();

            userRepo.Setup(repo => repo.FindAll()).Returns(mock.Object);
            userRepo.Setup(repo => repo.AddAsync(It.IsAny <UserModel>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(new UserModel()));
            var mapperMock = new Mock <IMapper>();

            mapperMock.Setup(m => m.Map <UserModel>(It.IsAny <User>()))
            .Returns(new UserModel());
            var handler = new RegisterCommandHandler(userRepo.Object, mapperMock.Object);

            // Act
            await handler.Handle(new RegisterCommand {
                Username  = "******",
                Password  = "******",
                FirstName = "Ftest",
                LastName  = "Ltest"
            }, It.IsAny <CancellationToken>());

            // Assert
            userRepo.Verify(x => x.FindAll(), Times.Once);
            userRepo.Verify(x => x.AddAsync(It.IsAny <UserModel>(), It.IsAny <CancellationToken>()), Times.Once);
            mapperMock.Verify(x => x.Map <UserModel>(It.IsAny <User>()), Times.Once);
        }
Ejemplo n.º 3
0
 public RegisterCommandHandlerTests()
 {
     _authConfigurationMock           = new Mock <IAuthConfiguration>();
     _userRepositoryMock              = new Mock <IUserRepository>();
     _encryptorMock                   = new Mock <IEncryptor>();
     _passwordValidatorMock           = new Mock <IPasswordValidator>();
     _emailValidatorMock              = new Mock <IEmailValidator>();
     _emailVerificationRepositoryMock = new Mock <IEmailVerificationRepository>();
     _handler = new RegisterCommandHandler(_authConfigurationMock.Object, _userRepositoryMock.Object, _encryptorMock.Object, _passwordValidatorMock.Object, _emailValidatorMock.Object, _emailVerificationRepositoryMock.Object);
 }
 public RegisterCommandHandlerTest()
 {
     mapper      = new Mock <IMapper>();
     userManager = CustomMock.GetMockUserManager();
     command     = new RegisterCommand {
         UserName = userName, Password = password
     };
     commandHandler = new RegisterCommandHandler(mapper.Object, userManager.Object);
     user           = new User();
 }
Ejemplo n.º 5
0
        public IActionResult Register([FromBody] NewStudentDto dto)
        {
            var handler = new RegisterCommandHandler(_studentService, _courseService);

            return(Ok(handler.Handle(new RegisterCommand(
                                         dto.Name,
                                         dto.Email,
                                         dto.Course1,
                                         dto.Course1Grade,
                                         dto.Course2,
                                         dto.Course2Grade))));
        }
Ejemplo n.º 6
0
 public void NetworkChannelOnMessageReceived(INetworkChannel networkChannel, int messageType, object messageBody)
 {
     if (RegisterCommandHandler.canHandle(messageBody))
     {
         RegisterCommandHandler.Handle(this, messageBody);
     }
     else
     {
         var messageDispatcher = _contexts.session.clientSessionObjects.MessageDispatcher;
         messageDispatcher.SaveDispatch(networkChannel, messageType, messageBody);
     }
 }
Ejemplo n.º 7
0
        public async Task ShouldCreateUser()
        {
            var token          = "token";
            var command        = new RegisterCommand("username", "*****@*****.**", "passwordUsername");
            var presenter      = new RegisterTestPresenter();
            var userRepository = new InMemoryUserRepository(token);
            var hashPassword   = new FakeHashPassword();
            var handler        = new RegisterCommandHandler(presenter, userRepository, hashPassword);

            await handler.Handle(command);

            Assert.Equal(token, presenter.Token);
        }
Ejemplo n.º 8
0
        public async Task ShouldNotCreateUserIfEmailAlreadyExists()
        {
            var command        = new RegisterCommand("username", "*****@*****.**", "passwordUsername");
            var presenter      = new RegisterTestPresenter();
            var userRepository = new InMemoryUserRepository("token", new List <User> {
                new User("login", command.Email, command.Password)
            });
            var hashPassword = new FakeHashPassword();
            var handler      = new RegisterCommandHandler(presenter, userRepository, hashPassword);

            await handler.Handle(command);

            Assert.Null(presenter.Token);
        }
Ejemplo n.º 9
0
        public void RegisterCallbacks(ExecuteCommandHandler delExecuteCommand, GetAccountPrivilegesHandler delGetAccountPrivileges, GetVariableHandler delGetVariable, GetVariableHandler delGetSvVariable, GetMapDefinesHandler delGetMapDefines, TryGetLocalizedHandler delTryGetLocalized, RegisterCommandHandler delRegisterCommand, UnregisterCommandHandler delUnregisterCommand, GetRegisteredCommandsHandler delGetRegisteredCommands, GetWeaponDefinesHandler delGetWeaponDefines, GetSpecializationDefinesHandler delGetSpecializationDefines, GetLoggedInAccountUsernamesHandler delGetLoggedInAccountUsernames, RegisterEventsHandler delRegisterEvents)
        {
            _executeCommandDelegate       = delExecuteCommand;
            _getAccountPrivilegesDelegate = delGetAccountPrivileges;
            _getVariableDelegate          = delGetVariable;
            _getSvVariableDelegate        = delGetSvVariable;
            _getMapDefinesDelegate        = delGetMapDefines;
            _tryGetLocalizedDelegate      = delTryGetLocalized;

            _registerCommandDelegate       = delRegisterCommand;
            _unregisterCommandDelegate     = delUnregisterCommand;
            _getRegisteredCommandsDelegate = delGetRegisteredCommands;

            _getWeaponDefinesDelegate         = delGetWeaponDefines;
            _getSpecializationDefinesDelegate = delGetSpecializationDefines;

            _getLoggedInAccountUsernamesDelegate = delGetLoggedInAccountUsernames;
            _registerEventsDelegate = delRegisterEvents;

            SetupInternalDictionaries();
        }
Ejemplo n.º 10
0
        public async Task RegisterUser_WhenEmailAlreadyExists_ShouldThrowAnException()
        {
            using (var sut = new SystemUnderTest())
            {
                var user = new User(Guid.NewGuid(), "*****@*****.**", false, Enumerable.Empty <Role>(), "someHash", "someSalt", DateTime.Now);
                await sut.UsersRepository.AddAsync(user);

                var command = new RegisterCommand
                {
                    Email           = "*****@*****.**",
                    Password        = "******",
                    ConfirmPassword = "******"
                };

                var handler = new RegisterCommandHandler(sut.UsersRepository, sut.PasswordService);
                var result  = await handler.HandleAsync(command);

                result.IsSuccess.Should().BeFalse();
                result.Message.Should().BeEquivalentTo("Email already exists.");
            }
        }
Ejemplo n.º 11
0
        public void RegisterCallbacks(ExecuteCommandHandler delExecuteCommand, GetAccountPrivilegesHandler delGetAccountPrivileges, GetVariableHandler delGetVariable, GetVariableHandler delGetSvVariable, GetMapDefinesHandler delGetMapDefines, TryGetLocalizedHandler delTryGetLocalized, RegisterCommandHandler delRegisterCommand, UnregisterCommandHandler delUnregisterCommand, GetRegisteredCommandsHandler delGetRegisteredCommands, GetWeaponDefinesHandler delGetWeaponDefines, GetSpecializationDefinesHandler delGetSpecializationDefines, GetLoggedInAccountUsernamesHandler delGetLoggedInAccountUsernames, RegisterEventsHandler delRegisterEvents)
        {
            this.m_delExecuteCommand       = delExecuteCommand;
            this.m_delGetAccountPrivileges = delGetAccountPrivileges;
            this.m_delGetVariable          = delGetVariable;
            this.m_delGetSvVariable        = delGetSvVariable;
            this.m_delGetMapDefines        = delGetMapDefines;
            this.m_delTryGetLocalized      = delTryGetLocalized;

            this.m_delRegisterCommand       = delRegisterCommand;
            this.m_delUnregisterCommand     = delUnregisterCommand;
            this.m_delGetRegisteredCommands = delGetRegisteredCommands;

            this.m_delGetWeaponDefines         = delGetWeaponDefines;
            this.m_delGetSpecializationDefines = delGetSpecializationDefines;

            this.m_delGetLoggedInAccountUsernames = delGetLoggedInAccountUsernames;
            this.m_delRegisterEvents = delRegisterEvents;

            this.SetupInternalDictionaries();
        }
Ejemplo n.º 12
0
        public async Task RegisterUser_WhenEmailDoesNotExists_ShouldCreateCorrectUser()
        {
            using (var sut = new SystemUnderTest())
            {
                var command = new RegisterCommand
                {
                    Email           = "*****@*****.**",
                    Password        = "******",
                    ConfirmPassword = "******"
                };

                var handler = new RegisterCommandHandler(sut.UsersRepository, sut.PasswordService);
                await handler.HandleAsync(command);

                var result = await sut.UsersRepository.GetByEmailAsync(command.Email);

                result.Id.Should().NotBeEmpty();
                result.Email.Should().Be(command.Email);
                result.PasswordHash.Should().BeEquivalentTo($"{command.Password}-hash");
                result.PasswordSalt.Should().NotBe(command.Password);
                result.PasswordSalt.Should().NotBe(result.PasswordHash);
            }
        }