Esempio n. 1
0
def test_registers_user_if_no_errors_occurs(mocker):
    repo = mocker.Mock(UserRepository)
    service = registration.RegistrationService([], repo)
    user_info = UserRegistrationInfo(
        username='******',
        password='******',
        email='*****@*****.**',
        language='fredspeak',
        group=4
    )
    service.register(user_info)
    repo.add.assert_called_with(user_info)
Esempio n. 2
0
def test_raises_if_username_in_blacklist():
    requirements = UsernameRequirements(min=1, max=100, blacklist=set(['no']))
    validator = UsernameValidator(requirements)

    registration = UserRegistrationInfo(
        username='******', password='******', email='*****@*****.**', group=4, language='no'
    )

    with pytest.raises(ValidationError) as excinfo:
        validator(registration)

    assert excinfo.value.attribute == 'username'
    assert 'forbidden username' in excinfo.value.reason
Esempio n. 3
0
def test_raises_if_username_too_long():
    requirements = UsernameRequirements(min=0, max=1, blacklist=set())
    validator = UsernameValidator(requirements)

    registration = UserRegistrationInfo(
        username='******', password='******', email='*****@*****.**', group=4, language='no'
    )

    with pytest.raises(ValidationError) as excinfo:
        validator(registration)

    assert excinfo.value.attribute == 'username'
    assert 'must be between' in excinfo.value.reason
Esempio n. 4
0
def test_raises_if_user_email_already_registered(Fred):
    validator = EmailUniquenessValidator(User)
    registration = UserRegistrationInfo(username='******',
                                        email='*****@*****.**',
                                        language='fred',
                                        group=4,
                                        password='******')

    with pytest.raises(ValidationError) as excinfo:
        validator(registration)

    assert excinfo.value.attribute == 'email'
    assert 'already registered' in excinfo.value.reason
Esempio n. 5
0
def test_doesnt_register_user_if_validator_fails_with_ValidationError(mocker):
    repo = mocker.Mock(UserRepository)
    service = registration.RegistrationService([RaisingValidator()], repo)

    with pytest.raises(StopValidation):
        service.register(
            UserRegistrationInfo(
                username='******',
                password='******',
                email='*****@*****.**',
                language='fredspeak',
                group=4
            )
        )

    repo.add.assert_not_called()
Esempio n. 6
0
def test_gathers_up_all_errors_during_registration(mocker):
    repo = mocker.Mock(UserRepository)
    service = registration.RegistrationService([
        RaisingValidator(), RaisingValidator()
    ], repo)

    with pytest.raises(StopValidation) as excinfo:
        service.register(
            UserRegistrationInfo(
                username='******',
                password='******',
                email='*****@*****.**',
                language='fredspeak',
                group=4
            )
        )

    repo.add.assert_not_called()
    assert len(excinfo.value.reasons) == 2
    assert all(('test', 'just a little whoopsie-diddle') == r
               for r in excinfo.value.reasons)
Esempio n. 7
0
class TestRegistrationService(object):
    fred = UserRegistrationInfo(
        username="******",
        password="******",
        email="*****@*****.**",
        language="fred",
        group=4,
    )

    def test_raises_stop_validation_if_validators_fail(self, plugin_manager,
                                                       database):
        service = self._get_service(plugin_manager, database)
        plugin_manager.register(self.impls(validator=RaisingValidator()))

        with pytest.raises(StopValidation) as excinfo:
            service.register(self.fred)

        assert ("username", "nope") in excinfo.value.reasons

    def test_calls_failure_handlers_if_validation_fails(
            self, plugin_manager, database, mocker):
        service = self._get_service(plugin_manager, database)
        failure = mocker.MagicMock(spec=RegistrationFailureHandler)
        plugin_manager.register(
            self.impls(validator=RaisingValidator(), failure=failure))

        with pytest.raises(StopValidation) as excinfo:
            service.register(self.fred)

        failure.assert_called_once_with(self.fred, excinfo.value.reasons)

    def test_registers_user_if_everything_is_good(self, database,
                                                  plugin_manager):
        service = self._get_service(plugin_manager, database)

        service.register(self.fred)

        actual_fred = User.query.filter(User.username == "Fred").one()

        assert actual_fred.id is not None

    def test_calls_post_processors_if_user_registration_works(
            self, database, plugin_manager, mocker):
        service = self._get_service(plugin_manager, database)
        post_process = mocker.MagicMock(spec=RegistrationPostProcessor)
        plugin_manager.register(self.impls(post_process=post_process))

        fred = service.register(self.fred)

        post_process.assert_called_once_with(fred)

    def test_raises_persistenceerror_if_saving_user_goes_wrong(
            self, database, plugin_manager, Fred):
        service = self._get_service(plugin_manager, database)

        with pytest.raises(PersistenceError):
            service.register(self.fred)

    @staticmethod
    def _get_service(plugin_manager, db):
        return RegistrationService(plugins=plugin_manager, users=User, db=db)

    @staticmethod
    def impls(validator=None, failure=None, post_process=None):
        impl = HookimplMarker("flaskbb")

        class Impls:
            if validator is not None:

                @impl
                def flaskbb_gather_registration_validators(self):
                    return [validator]

            if failure is not None:

                @impl
                def flaskbb_registration_failure_handler(
                        self, user_info, failures):
                    failure(user_info, failures)

            if post_process is not None:

                @impl
                def flaskbb_registration_post_processor(self, user):
                    post_process(user)

        return Impls()