Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

Commit

Permalink
add password validation to registering user and updating
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal committed Jan 24, 2024
1 parent 3734790 commit 07253b6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
namespace server::application
{
RegisterUserCommandHandlerImpl::RegisterUserCommandHandlerImpl(
std::shared_ptr<domain::UserRepository> userRepositoryInit, std::shared_ptr<HashService> hashServiceInit)
: userRepository{std::move(userRepositoryInit)}, hashService{std::move(hashServiceInit)}
std::shared_ptr<domain::UserRepository> userRepositoryInit, std::shared_ptr<HashService> hashServiceInit,
std::shared_ptr<PasswordValidationService> passwordValidationServiceInit)
: userRepository{std::move(userRepositoryInit)},
hashService{std::move(hashServiceInit)},
passwordValidationService{std::move(passwordValidationServiceInit)}
{
}

Expand All @@ -28,6 +31,8 @@ RegisterUserCommandHandlerImpl::execute(const RegisterUserCommandHandlerPayload&
throw errors::ResourceAlreadyExistsError{fmt::format("User with email \"{}\" already exists.", payload.email)};
}

passwordValidationService->validate(payload.password);

const auto hashedPassword = hashService->hash(payload.password);

std::stringstream uuid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

#include "RegisterUserCommandHandler.h"
#include "server/application/services/hashService/HashService.h"
#include "server/application/services/passwordValidationService/PasswordValidationService.h"
#include "server/domain/repositories/userRepository/UserRepository.h"

namespace server::application
{
class RegisterUserCommandHandlerImpl : public RegisterUserCommandHandler
{
public:
RegisterUserCommandHandlerImpl(std::shared_ptr<domain::UserRepository>, std::shared_ptr<HashService>);
RegisterUserCommandHandlerImpl(std::shared_ptr<domain::UserRepository>, std::shared_ptr<HashService>,
std::shared_ptr<PasswordValidationService>);

RegisterUserCommandHandlerResult execute(const RegisterUserCommandHandlerPayload&) const override;

private:
std::shared_ptr<domain::UserRepository> userRepository;
std::shared_ptr<HashService> hashService;
std::shared_ptr<PasswordValidationService> passwordValidationService;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#include "faker-cxx/Internet.h"
#include "RegisterUserCommandHandlerImpl.h"
#include "server/application/errors/OperationNotValidError.h"
#include "server/application/errors/ResourceAlreadyExistsError.h"
#include "server/application/services/hashService/HashServiceImpl.h"
#include "server/application/services/passwordValidationService/PasswordValidationServiceImpl.h"
#include "server/infrastructure/repositories/userRepository/userMapper/UserMapperImpl.h"
#include "server/infrastructure/repositories/userRepository/UserRepositoryImpl.h"
#include "server/tests/factories/databaseClientTestFactory/DatabaseClientTestFactory.h"
Expand Down Expand Up @@ -39,14 +41,17 @@ class RegisterUserCommandImplIntegrationTest : public Test

std::shared_ptr<HashService> hashService = std::make_shared<HashServiceImpl>();

RegisterUserCommandHandlerImpl registerUserCommandHandler{userRepository, hashService};
std::shared_ptr<PasswordValidationService> passwordValidationService =
std::make_shared<PasswordValidationServiceImpl>();

RegisterUserCommandHandlerImpl registerUserCommandHandler{userRepository, hashService, passwordValidationService};
};

TEST_F(RegisterUserCommandImplIntegrationTest, registerUser)
{
const auto email = faker::Internet::email();
const auto password = "password";
const auto expectedHashPassword = "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8";
const auto password = "Password123";
const auto expectedHashPassword = "b2e98ad6f6eb8508dd6a14cfa704bad7f05f6fb1";
const auto nickname = faker::Internet::username();

const auto [user] = registerUserCommandHandler.execute({email, password, nickname});
Expand All @@ -56,6 +61,15 @@ TEST_F(RegisterUserCommandImplIntegrationTest, registerUser)
ASSERT_EQ(user.getNickname(), nickname);
}

TEST_F(RegisterUserCommandImplIntegrationTest, givenPasswordNotMatchingRequirements_shouldThrow)
{
const auto email = faker::Internet::email();
const auto password = "password";
const auto nickname = faker::Internet::username();

ASSERT_THROW(registerUserCommandHandler.execute({email, password, nickname}), errors::OperationNotValidError);
}

TEST_F(RegisterUserCommandImplIntegrationTest, givenUserWithSameEmail_shouldThrow)
{
const auto user = userTestUtils.createAndPersist();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#include "UpdateUserCommandHandlerImpl.h"

#include <boost/uuid/uuid_io.hpp>

#include "fmt/format.h"
#include "loguru.hpp"
#include "server/application/errors/ResourceAlreadyExistsError.h"
#include "server/application/errors/ResourceNotFoundError.h"

namespace server::application
{
UpdateUserCommandHandlerImpl::UpdateUserCommandHandlerImpl(std::shared_ptr<domain::UserRepository> userRepositoryInit,
std::shared_ptr<HashService> hashServiceInit)
: userRepository{std::move(userRepositoryInit)}, hashService{std::move(hashServiceInit)}
UpdateUserCommandHandlerImpl::UpdateUserCommandHandlerImpl(
std::shared_ptr<domain::UserRepository> userRepositoryInit, std::shared_ptr<HashService> hashServiceInit,
std::shared_ptr<PasswordValidationService> passwordValidationServiceInit)
: userRepository{std::move(userRepositoryInit)},
hashService{std::move(hashServiceInit)},
passwordValidationService{std::move(passwordValidationServiceInit)}
{
}

Expand All @@ -34,6 +35,8 @@ UpdateUserCommandHandlerImpl::execute(const UpdateUserCommandHandlerPayload& pay

if (payload.password)
{
passwordValidationService->validate(*payload.password);

const auto hashedPassword = hashService->hash(*payload.password);

existingUser->get()->setPassword(hashedPassword);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>

#include "server/application/services/hashService/HashService.h"
#include "server/application/services/passwordValidationService/PasswordValidationService.h"
#include "server/domain/repositories/userRepository/UserRepository.h"
#include "UpdateUserCommandHandler.h"

Expand All @@ -11,12 +12,14 @@ namespace server::application
class UpdateUserCommandHandlerImpl : public UpdateUserCommandHandler
{
public:
UpdateUserCommandHandlerImpl(std::shared_ptr<domain::UserRepository>, std::shared_ptr<HashService>);
UpdateUserCommandHandlerImpl(std::shared_ptr<domain::UserRepository>, std::shared_ptr<HashService>,
std::shared_ptr<PasswordValidationService>);

UpdateUserCommandHandlerResult execute(const UpdateUserCommandHandlerPayload&) const override;

private:
std::shared_ptr<domain::UserRepository> userRepository;
std::shared_ptr<HashService> hashService;
std::shared_ptr<PasswordValidationService> passwordValidationService;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "faker-cxx/String.h"
#include "server/application/errors/ResourceNotFoundError.h"
#include "server/application/services/hashService/HashServiceImpl.h"
#include "server/application/services/passwordValidationService/PasswordValidationServiceImpl.h"
#include "server/infrastructure/repositories/userRepository/userMapper/UserMapperImpl.h"
#include "server/infrastructure/repositories/userRepository/UserRepositoryImpl.h"
#include "server/tests/factories/databaseClientTestFactory/DatabaseClientTestFactory.h"
Expand Down Expand Up @@ -40,7 +41,10 @@ class UpdateUserCommandImplIntegrationTest : public Test

std::shared_ptr<HashServiceImpl> hashService = std::make_shared<HashServiceImpl>();

UpdateUserCommandHandlerImpl updateUserCommandHandler{userRepository, hashService};
std::shared_ptr<PasswordValidationService> passwordValidationService =
std::make_shared<PasswordValidationServiceImpl>();

UpdateUserCommandHandlerImpl updateUserCommandHandler{userRepository, hashService, passwordValidationService};
};

TEST_F(UpdateUserCommandImplIntegrationTest, updateNotExistingUser_shouldThrow)
Expand Down
11 changes: 7 additions & 4 deletions apps/server/core/session/MessageRouterFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "server/application/queryHandlers/user/findUserByEmailQueryHandler/FindUserByEmailQueryHandlerImpl.h"
#include "server/application/queryHandlers/user/findUserQueryHandler/FindUserQueryHandlerImpl.h"
#include "server/application/services/hashService/HashServiceImpl.h"
#include "server/application/services/passwordValidationService/PasswordValidationServiceImpl.h"
#include "server/infrastructure/repositories/channelInvitationRepository/channelInvitationMapper/ChannelInvitationMapperImpl.h"
#include "server/infrastructure/repositories/channelInvitationRepository/ChannelInvitationRepositoryImpl.h"
#include "server/infrastructure/repositories/channelRepository/channelMapper/ChannelMapperImpl.h"
Expand Down Expand Up @@ -106,8 +107,10 @@ std::unique_ptr<MessageRouter> MessageRouterFactory::createMessageRouter() const
auto userGroupRepository =
std::make_shared<server::infrastructure::UserGroupRepositoryImpl>(db, userGroupMapper, userMapper, groupMapper);

auto registerUserCommandHandler =
std::make_unique<server::application::RegisterUserCommandHandlerImpl>(userRepository, hashService);
auto passwordValidationService = std::make_shared<application::PasswordValidationServiceImpl>();

auto registerUserCommandHandler = std::make_unique<server::application::RegisterUserCommandHandlerImpl>(
userRepository, hashService, passwordValidationService);

auto loginUserCommandHandler =
std::make_unique<server::application::LoginUserCommandHandlerImpl>(userRepository, hashService, tokenService);
Expand Down Expand Up @@ -160,8 +163,8 @@ std::unique_ptr<MessageRouter> MessageRouterFactory::createMessageRouter() const
auto getUserDataMessageHandler =
std::make_shared<api::GetUserDataMessageHandler>(tokenService, findUserQueryHandler);

auto updateUserCommandHandler =
std::make_unique<server::application::UpdateUserCommandHandlerImpl>(userRepository, hashService);
auto updateUserCommandHandler = std::make_unique<server::application::UpdateUserCommandHandlerImpl>(
userRepository, hashService, passwordValidationService);

auto updateUserMessageHandler =
std::make_shared<api::UpdateUserMessageHandler>(tokenService, std::move(updateUserCommandHandler));
Expand Down

0 comments on commit 07253b6

Please sign in to comment.