def login_post(command_bus: CommandBus, query: QueryUserByUserName): client_request = request.json validate_client_request(client_request, ['username', 'password']) login_a_user_command = LoginUser( username=UserName(client_request['username']), password=Password(client_request['password']), ) command_bus.handle(login_a_user_command) user = query.execute(UserName(client_request['username'])) return to_single_user_response(user), 200
def test_two_variables_with_same_values_should_be_equal(self): an_id = UserID() a_user = User(username=UserName('Maria'), password=Password('uselesspassword'), about='About Maria', ID=an_id) another_user = User(username=UserName('Maria'), password=Password('uselesspassword'), about='About Maria', ID=an_id) assert a_user == another_user
def test_should_create_a_user(self): user = User(username=UserName('Maria'), password=Password('uselesspassword'), about='About Maria') assert user.username.contents == 'Maria' assert user.password.contents == 'uselesspassword' assert user.about == 'About Maria'
def test_should_raise_an_exception_on_wrong_password(self): command = LoginUser(username=UserName('Maria'), password=Password('a_wrong_password')) user_login_by_username_and_password = UserLoginByUserNameAndPassword( QueryUserByUserName(InMemoryUsersRepository([maria()])), FakeClock(a_perfect_day_and_time())) with self.assertRaises(InvalidCredentials): user_login_by_username_and_password.execute(command)
def test_should_raise_an_exception_on_unkown_username(self): command = LoginUser(username=UserName('IDontExist'), password=Password('a_password')) user_login_by_username_and_password = UserLoginByUserNameAndPassword( QueryUserByUserName(InMemoryUsersRepository([maria()])), FakeClock(a_perfect_day_and_time())) with self.assertRaises(UnknownUser): user_login_by_username_and_password.execute(command)
def registration_post(command_bus: CommandBus): client_request = request.json validate_client_request(client_request, ['username', 'password', 'about']) register_a_user_command = RegisterUser( username=UserName(client_request['username']), password=Password(client_request['password']), about=client_request['about'], ID=UserID()) command_bus.handle(register_a_user_command) return to_single_user_response(register_a_user_command), 201
def test_should_create_a_copy_with_new_values(self): a_user = User(username=UserName('Maria'), password=Password('uselesspassword'), about='About Maria') user_with_password_changed = replace( a_user, password=Password('thispasswordisuselesstoo')) assert a_user.username == user_with_password_changed.username assert a_user.about == user_with_password_changed.about assert user_with_password_changed.password.contents == 'thispasswordisuselesstoo' assert a_user != user_with_password_changed
def test_should_login_a_user(self): try: command = LoginUser(username=UserName('Maria'), password=Password('a_password')) user_login_by_username_and_password = UserLoginByUserNameAndPassword( QueryUserByUserName(InMemoryUsersRepository([maria()])), FakeClock(a_perfect_day_and_time())) entity, events = user_login_by_username_and_password.execute( command) assert entity is None assert len(events) == 1 assert events[0].username == command.username assert events[0].timestamp == a_perfect_day_and_time() except ValueError as err: self.fail("Unexpected exception raised: %s" % err)
def maria() -> User: return User(username=UserName('Maria'), password=Password('a_password'), about='About Maria', ID=UserID('331bfef4-7fe9-47b6-a138-42e81d4a5e14'))
def create_maria() -> RegisterUser: return RegisterUser(ID=UserID(), username=UserName('Maria'), password=Password('a_password'), about='About Maria')
def bob() -> User: return User(username=UserName('Bob'), password=Password('another_password'), about='About Bob', ID=UserID('0d43fe15-8ffa-4845-a818-ddba840501b2'))
def test_should_create_a_username(self): a_username = UserName('maria') assert a_username.contents == 'maria'
def test_should_not_allow_undefined_username(self): with self.assertRaises(ValueError): UserName(None) # noqa
def test_should_not_allow_empty_username(self): with self.assertRaises(ValueError): UserName('')
def test_should_not_allow_username_with_spaces(self): with self.assertRaises(ValueError): UserName('invalid username')