def test_change_password() -> None: old_plain_password = '******' old_password = UserPasswordMother.create(old_plain_password, False) user = UserMother.from_password(old_password) new_password = UserPasswordMother.random() user.change_password(old_plain_password, new_password) assert user.password() == new_password events = user.pull_aggregate_events() assert len(events) == 1 assert isinstance(events[0], UserPasswordChanged)
def test_reset_password() -> None: user = UserMother.from_password_and_forgotten_password( UserPasswordMother.create(value='secret123456', hashed=False), UserRefreshTokenMother.random(), UserRefreshTokenExpirationInMother.random(), ) new_password = UserPasswordMother.random() user.reset_password(new_password) assert user.password() == new_password events = user.pull_aggregate_events() assert len(events) == 1 assert isinstance(events[0], UserPasswordResetted)
async def test_change_password_successfully(self) -> None: old_password = '******' user = UserMother.from_password(UserPasswordMother.create(value=old_password, hashed=False)) self._mock_user_repository.find.return_value = user self._mock_user_repository.save_and_publish.return_value = None kwargs = { 'user_id': user.id(), 'old_password': old_password, 'new_password': UserPasswordMother.random(), } await self._sut(**kwargs) self._mock_user_repository.find.assert_called_once_with(kwargs['user_id']) self._mock_user_repository.save_and_publish.assert_called_once_with(user)
def random_with_forgotten_password(cls) -> User: return cls.create( UserIdMother.random(), UserEmailMother.random(), UserPasswordMother.random(), UserRefreshTokenMother.random(), UserRefreshTokenExpirationInMother.random(), )
def test_forget_password() -> None: user = UserMother.from_password( UserPasswordMother.create(value='secret123456', hashed=False)) assert user.refresh_token() is None assert user.refresh_token_expiration_in() is None user.forget_password() assert user.refresh_token() is not None assert user.refresh_token_expiration_in() is not None events = user.pull_aggregate_events() assert len(events) == 1 assert isinstance(events[0], UserPasswordForgotten)
async def test_change_user_password_post_controller() -> None: async with APIContext() as ctx: cmd = { 'id': UserIdMother.random().value(), 'email': UserEmailMother.random().value(), 'password': UserPasswordMother.random().value(), } ctx.token = await UserContext.an_existent_authenticated_user_with( cmd=cmd) await ctx.i_send_json_request( method='POST', url='/api/protected/users/change-password', data={ 'id': cmd['id'], 'old_password': cmd['password'], 'new_password': UserPasswordMother.random().value(), }, ) await ctx.the_response_status_code_should_be(status_code=200) await ctx.the_response_content_should_be(data=None)
async def test_reset_user_password_post_controller() -> None: async with APIContext() as ctx: cmd = { 'id': UserIdMother.random().value(), 'email': UserEmailMother.random().value(), 'password': UserPasswordMother.random().value(), } ctx.token = await UserContext.an_existent_authenticated_user_with(cmd=cmd) await UserContext.forget_user_password_with_email(cmd['email']) full_user = await UserContext.find_full_user_with_id(cmd['id']) await ctx.i_send_json_request( method='POST', url='/api/public/users/reset-password', data={ 'email': cmd['email'], 'refresh_token': full_user.refresh_token, 'new_password': UserPasswordMother.random().value(), }, ) await ctx.the_response_status_code_should_be(status_code=200) await ctx.the_response_content_should_be(data=None)
async def test_register_user_put_controller() -> None: async with APIContext() as ctx: await ctx.i_send_json_request( method='PUT', url='/api/public/users', data={ 'id': UserIdMother.random().value(), 'email': UserEmailMother.random().value(), 'password': UserPasswordMother.random().value(), }, ) await ctx.the_response_status_code_should_be(status_code=201) await ctx.the_response_content_should_be(data=None)
async def test_register_successfully(self) -> None: self._mock_user_repository.search.return_value = None self._mock_user_repository.save_and_publish.return_value = None kwargs = { 'user_id': UserIdMother.random(), 'email': UserEmailMother.random(), 'password': UserPasswordMother.random(), } await self._sut(**kwargs) self._mock_user_repository.search.assert_called_once_with( kwargs['user_id']) self._mock_user_repository.save_and_publish.assert_called_once()
async def test_reset_password_successfully(self) -> None: user = UserMother.from_password_and_forgotten_password( UserPasswordMother.create(value='secret123456', hashed=False), UserRefreshTokenMother.random(), UserRefreshTokenExpirationInMother.random(), ) self._mock_user_repository.find_email_and_refresh_token.return_value = user self._mock_user_notifier.notify_user_password_resetted.return_value = None self._mock_user_repository.save_and_publish.return_value = None kwargs = { 'user_email': user.email(), 'user_refresh_token': user.refresh_token(), 'new_password': UserPasswordMother.random(), } await self._sut(**kwargs) self._mock_user_repository.find_email_and_refresh_token.assert_called_once_with( kwargs['user_email'], kwargs['user_refresh_token']) self._mock_user_notifier.notify_user_password_resetted.assert_called_once( ) self._mock_user_repository.save_and_publish.assert_called_once_with( user)
async def test_reset_password_successfully(self) -> None: user = UserMother.from_password( UserPasswordMother.create(value='secret123456', hashed=False), ) self._mock_user_repository.find_email.return_value = user self._mock_user_notifier.notify_user_password_forgotten.return_value = None self._mock_user_repository.save_and_publish.return_value = None kwargs = { 'user_email': user.email(), } await self._sut(**kwargs) self._mock_user_repository.find_email.assert_called_once_with(kwargs['user_email']) self._mock_user_notifier.notify_user_password_forgotten.assert_called_once() self._mock_user_repository.save_and_publish.assert_called_once_with(user)
async def test_forget_user_password_post_controller() -> None: async with APIContext() as ctx: cmd = { 'id': UserIdMother.random().value(), 'email': UserEmailMother.random().value(), 'password': UserPasswordMother.random().value(), } await UserContext.an_existent_user_with(cmd=cmd) await ctx.i_send_json_request( method='POST', url='/api/public/users/forget-password', data={ 'email': cmd['email'], }, ) await ctx.the_response_status_code_should_be(status_code=200) await ctx.the_response_content_should_be(data=None)
async def test_generate_token_successfully(self) -> None: password = '******' user = UserMother.from_password( UserPasswordMother.create(value=password, hashed=False)) access_token = 'token' self._mock_auth_repository.find.return_value = user self._mock_token_factory.generate.return_value = access_token kwargs = { 'email': user.email(), 'password': password, } res = await self._sut(**kwargs) self._mock_auth_repository.find.assert_called_once_with( kwargs['email']) self._mock_token_factory.generate.assert_called_once_with( user.id(), self._expiration_days) assert res.user_id == user.id().value() assert res.access_token == access_token assert res.token_type == self._token_type
async def _register_user(cls, cmd: Dict[str, Any], do_authorize: bool) -> Optional[str]: plain_email = (UserEmailMother.create(cmd['email']) if 'email' in cmd else UserEmailMother.random()).value() plain_password = cmd[ 'password'] if 'password' in cmd else UserPasswordMother.random( ).value() await container().get(CommandBus).dispatch( RegisterUserCommand( **{ 'id': (UserIdMother.create(cmd['id']) if 'id' in cmd else UserIdMother.random()).value(), 'email': plain_email, 'password': plain_password, })) if do_authorize: return await cls._authorize_user(cmd={ 'email': plain_email, 'password': plain_password }) return None
async def test_token_auth_post_controller() -> None: async with APIContext() as ctx: cmd = { 'id': UserIdMother.random().value(), 'email': UserEmailMother.random().value(), 'password': UserPasswordMother.random().value(), } await UserContext.an_existent_user_with(cmd=cmd) await ctx.i_send_x_www_form_urlencoded_request( method='POST', url='/api/public/users/auth', data={ 'grant_type': 'password', 'username': cmd['email'], 'password': cmd['password'], }, ) await ctx.the_response_status_code_should_be(status_code=200) await ctx.the_response_content_should_be_contains( data={ 'user_id': cmd['id'], 'token_type': 'Bearer', } )
def from_id(cls, user_id: UserId) -> User: return cls.create(user_id, UserEmailMother.random(), UserPasswordMother.random(), None, None)
def random(cls) -> User: return cls.create(UserIdMother.random(), UserEmailMother.random(), UserPasswordMother.random(), None, None)
def test_validate_password() -> None: plain_password = '******' user = UserMother.from_password( UserPasswordMother.create(plain_password, False)) user.validate_password(plain_password)
def test_register() -> None: user = User.register(UserIdMother.random(), UserEmailMother.random(), UserPasswordMother.random()) events = user.pull_aggregate_events() assert len(events) == 1 assert isinstance(events[0], UserRegistered)