Exemplo n.º 1
0
 def test_user_password_forgotten_event_mapper() -> None:
     mapper = UserPasswordForgottenEventMapper()
     msg = UserPasswordForgotten(
         UserPasswordForgotten.Attributes(
             UserIdMother.random(),
             UserEmailMother.random(),
             UserRefreshTokenMother.random(),
             UserRefreshTokenExpirationInMother.random(),
         ))
     assert mapper.belongs_to(msg)
     assert mapper.service_name == 'project.account'
     assert mapper.event_name == 'users.password_forgotten'
     data = {
         'id':
         msg.attributes.id.value(),
         'email':
         msg.attributes.email.value(),
         'refresh_token':
         msg.attributes.refresh_token.value(),
         'refresh_token_expiration_in':
         msg.attributes.refresh_token_expiration_in.value(),
     }
     evt: UserPasswordForgotten = mapper.decode(data)
     assert evt.attributes.id.value() == data['id']
     assert evt.attributes.email.value() == data['email']
     assert evt.attributes.refresh_token.value() == data['refresh_token']
     assert evt.attributes.refresh_token_expiration_in.value(
     ) == data['refresh_token_expiration_in']
     assert mapper.map_attributes(msg.attributes) == data
 def random_with_forgotten_password(cls) -> User:
     return cls.create(
         UserIdMother.random(),
         UserEmailMother.random(),
         UserPasswordMother.random(),
         UserRefreshTokenMother.random(),
         UserRefreshTokenExpirationInMother.random(),
     )
    async def test_generate_and_read() -> None:
        user_id = UserIdMother.random()
        secret = Id.generate().value()

        sut = JWTTokenFactory(lambda: datetime.utcnow(), secret)

        token = await sut.generate(user_id, 1)
        auth_user_id = await sut.read(token)

        assert user_id.value() == auth_user_id.value()
async def test_delete_user_delete_controller() -> None:
    async with APIContext() as ctx:
        cmd = {
            'id': UserIdMother.random().value(),
        }
        ctx.token = await UserContext.an_existent_user_with_id(
            user_id=cmd["id"])
        await ctx.i_send_json_request(method='DELETE',
                                      url=f'/api/protected/users/{cmd["id"]}')
        await ctx.the_response_status_code_should_be(status_code=200)
        await ctx.the_response_content_should_be(data=None)
async def test_find_user_get_controller() -> None:
    async with APIContext() as ctx:
        cmd = {
            'id': UserIdMother.random().value(),
            'email': UserEmailMother.random().value(),
        }
        ctx.token = await UserContext.an_existent_authenticated_user_with(
            cmd=cmd)
        await ctx.i_send_json_request(method='GET',
                                      url=f'/api/protected/users/{cmd["id"]}')
        await ctx.the_response_status_code_should_be(status_code=200)
        await ctx.the_response_content_should_be(data=cmd)
    async def test_authenticate_successfully(self) -> None:
        user_id = UserIdMother.random()
        self._mock_token_factory.read.return_value = user_id

        kwargs = {
            'token': 'token',
        }

        res = await self._sut(**kwargs)

        self._mock_token_factory.read.assert_called_once_with(kwargs['token'])
        assert res.user_id == user_id.value()
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
 def setup(self) -> None:
     self._mock_command_bus = mock(CommandBus, ['dispatch'])
     self._sut = NotifyUserRegisteredOnUserRegisteredEventHandler(
         self._mock_command_bus)
     self._kwargs = {
         'events': [
             UserRegistered(
                 UserRegistered.Attributes(
                     UserIdMother.random(),
                     UserEmailMother.random(),
                 )),
         ],
     }
 def from_password_and_forgotten_password(
     cls,
     user_password: UserPassword,
     user_refresh_token: UserRefreshToken,
     user_refresh_token_expiration_in: UserRefreshTokenExpirationIn,
 ) -> User:
     return cls.create(
         UserIdMother.random(),
         UserEmailMother.random(),
         user_password,
         user_refresh_token,
         user_refresh_token_expiration_in,
     )
Exemplo n.º 10
0
 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
Exemplo n.º 11
0
    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()
Exemplo n.º 12
0
 def test_user_password_changed_event_mapper() -> None:
     mapper = UserPasswordChangedEventMapper()
     msg = UserPasswordChanged(
         UserPasswordChanged.Attributes(UserIdMother.random(),
                                        UserEmailMother.random()))
     assert mapper.belongs_to(msg)
     assert mapper.service_name == 'project.account'
     assert mapper.event_name == 'users.password_changed'
     data = {
         'id': msg.attributes.id.value(),
         'email': msg.attributes.email.value(),
     }
     evt: UserPasswordChanged = mapper.decode(data)
     assert evt.attributes.id.value() == data['id']
     assert evt.attributes.email.value() == data['email']
     assert mapper.map_attributes(msg.attributes) == data
Exemplo n.º 13
0
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_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_notify_user_registered_successfully(self) -> None:
        async with MailContext(user=self._cnf.host_user,
                               ui_host=self._cnf.host,
                               ui_port=self._email_ui_port) as ctx:
            user_id = UserIdMother.random()
            user_email = UserEmailMother.random()
            await self._sut.notify_user_registered(user_id, user_email)

            await ctx.i_send_get_request(user_email=user_email.value())

            def assert_response(headers: Dict[str, Any],
                                body: Optional[str]) -> bool:
                assert headers['X-UserId'][0] == user_id.value()
                assert headers['From'][0] == self._cnf.from_user
                assert headers['Subject'][0] == 'Welcome to Project'
                assert headers['To'][0] == user_email.value()
                assert body == f'Welcome {user_email.value()}'
                return True

            await ctx.the_response_content_should(callback=assert_response)
Exemplo n.º 16
0
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_user_password_forgotten_successfully(self) -> None:
        async with MailContext(user=self._cnf.host_user,
                               ui_host=self._cnf.host,
                               ui_port=self._email_ui_port) as ctx:
            user_id = UserIdMother.random()
            user_email = UserEmailMother.random()
            refresh_token = UserRefreshTokenMother.random()
            await self._sut.notify_user_password_forgotten(
                user_id, user_email, refresh_token)

            await ctx.i_send_get_request(user_email=user_email.value())

            def assert_response(headers: Dict[str, Any],
                                body: Optional[str]) -> bool:
                assert headers['X-UserId'][0] == user_id.value()
                assert headers['From'][0] == self._cnf.from_user
                assert headers['Subject'][
                    0] == 'Project - Here you have your reset password code'
                assert headers['To'][0] == user_email.value()
                assert body == f'Here you have your reset password code: {refresh_token.value()}'
                return True

            await ctx.the_response_content_should(callback=assert_response)
Exemplo n.º 18
0
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',
            }
        )
Exemplo n.º 19
0
 def random(cls) -> User:
     return cls.create(UserIdMother.random(), UserEmailMother.random(),
                       UserPasswordMother.random(), None, None)
Exemplo n.º 20
0
 def from_password(cls, user_password: UserPassword) -> User:
     return cls.create(UserIdMother.random(), UserEmailMother.random(),
                       user_password, None, None)
Exemplo n.º 21
0
 async def _find_full_user(user_id: str) -> UserFullFinderResponse:
     return (await container().get(QueryBus).ask(
         FindFullUserQuery(**{
             'user_id': (UserIdMother.create(user_id)).value(),
         })))
Exemplo n.º 22
0
 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)
Exemplo n.º 23
0
def test_auth_guard_fails_when_user_unauthorized() -> None:
    with pytest.raises(UserUnauthorizedError):
        auth_guard(
            UserIdMother.random().value(),
            UserAuth(**{'id': UserIdMother.random().value(), 'email': UserEmailMother.random().value()}),
        )