Exemplo n.º 1
0
    def test_raises_if_email_is_already_registered(self, Fred, user):
        change = EmailUpdate("*****@*****.**", user.email)

        with pytest.raises(ValidationError) as excinfo:
            validators.CantShareEmailValidator(User).validate(Fred, change)

        assert "is already registered" in str(excinfo.value)
Exemplo n.º 2
0
    def test_raises_if_old_email_doesnt_match(self, Fred):
        change = EmailUpdate("*****@*****.**",
                             "*****@*****.**")

        with pytest.raises(StopValidation) as excinfo:
            validators.OldEmailMustMatch().validate(Fred, change)

        assert [("old_email", "Old email does not match")
                ] == excinfo.value.reasons
Exemplo n.º 3
0
    def test_transforms_to_expected_change_object(self, Fred):
        data = MultiDict(
            {
                "old_email": Fred.email,
                "new_email": "*****@*****.**",
                "confirm_new_email": "*****@*****.**",
                "submit": True,
            }
        )

        form = forms.ChangeEmailForm(Fred, formdata=data)
        expected = EmailUpdate(old_email=Fred.email, new_email="*****@*****.**")

        assert form.as_change() == expected
Exemplo n.º 4
0
    def test_actually_updates_email(self, user, database, mocker,
                                    plugin_manager):
        new_email = random_email()
        email_change = EmailUpdate("test", new_email)
        hook_impl = mocker.Mock(spec=ChangeSetPostProcessor)
        plugin_manager.register(self.impl(hook_impl))
        handler = DefaultEmailUpdateHandler(db=database,
                                            validators=[],
                                            plugin_manager=plugin_manager)

        handler.apply_changeset(user, email_change)
        same_user = User.query.get(user.id)
        assert same_user.email == new_email
        hook_impl.post_process_changeset.assert_called_once_with(
            user=user, email_update=email_change)
Exemplo n.º 5
0
    def test_raises_persistence_error_if_save_fails(self, mock, user, plugin_manager):
        email_change = EmailUpdate(user.email, random_email())
        db = mock.Mock()
        db.session.commit.side_effect = Exception("no")
        hook_impl = mock.Mock(spec=ChangeSetPostProcessor)
        plugin_manager.register(self.impl(hook_impl))
        handler = DefaultEmailUpdateHandler(
            db=db, validators=[], plugin_manager=plugin_manager
        )

        with pytest.raises(PersistenceError) as excinfo:
            handler.apply_changeset(user, email_change)

        assert "Could not update email" in str(excinfo.value)
        hook_impl.post_process_changeset.assert_not_called()
Exemplo n.º 6
0
    def test_raises_stop_validation_if_errors_occur(self, mocker, user,
                                                    database, plugin_manager):
        validator = mocker.Mock(spec=ChangeSetValidator)
        validator.validate.side_effect = ValidationError(
            "new_email", "That's not even valid")
        hook_impl = mocker.Mock(spec=ChangeSetPostProcessor)
        plugin_manager.register(self.impl(hook_impl))
        email_change = EmailUpdate(user.email, random_email())
        handler = DefaultEmailUpdateHandler(db=database,
                                            validators=[validator],
                                            plugin_manager=plugin_manager)

        with pytest.raises(StopValidation) as excinfo:
            handler.apply_changeset(user, email_change)

        assert excinfo.value.reasons == [("new_email", "That's not even valid")
                                         ]
        hook_impl.post_process_changeset.assert_not_called()
Exemplo n.º 7
0
    def test_update_user_email_successfully(self, user, mock):
        form = self.produce_form(
            old_email=user.email,
            new_email="*****@*****.**",
            confirm_new_email="*****@*****.**",
        )
        handler = mock.Mock(spec=ChangeSetHandler)
        view = ChangeEmail(form=form, update_email_handler=handler)

        result = view.post()
        flashed = get_flashed_messages(with_categories=True)

        assert flashed == [("success", "Email address updated.")]
        handler.apply_changeset.assert_called_once_with(
            user, EmailUpdate(old_email=user.email, new_email="*****@*****.**")
        )
        assert result.status_code == 302
        assert result.headers["Location"] == url_for("user.change_email")
Exemplo n.º 8
0
    def test_doesnt_raise_if_old_email_matches(self, Fred):
        change = EmailUpdate(Fred.email, "*****@*****.**")

        validators.OldEmailMustMatch().validate(Fred, change)
Exemplo n.º 9
0
    def test_doesnt_raise_if_email_isnt_registered(self, Fred):
        change = EmailUpdate("*****@*****.**", "*****@*****.**")

        validators.CantShareEmailValidator(User).validate(Fred, change)
Exemplo n.º 10
0
    def test_doesnt_raise_if_emails_are_different(self, Fred):
        different_emails = EmailUpdate("*****@*****.**",
                                       "*****@*****.**")

        validators.EmailsMustBeDifferent().validate(Fred, different_emails)
Exemplo n.º 11
0
    def test_raises_if_emails_match(self, Fred):
        matching_emails = EmailUpdate("*****@*****.**", Fred.email)

        with pytest.raises(ValidationError) as excinfo:
            validators.EmailsMustBeDifferent().validate(Fred, matching_emails)
        assert "New email address must be different" in str(excinfo.value)