def test_raises_if_passwords_are_the_same(self, Fred): change = PasswordUpdate("fred", "fred") with pytest.raises(ValidationError) as excinfo: validators.PasswordsMustBeDifferent().validate(Fred, change) assert "New password must be different" in str(excinfo.value)
def test_raises_if_old_password_doesnt_match(self, Fred): change = PasswordUpdate(str(uuid4()), str(uuid4())) with pytest.raises(StopValidation) as excinfo: validators.OldPasswordMustMatch().validate(Fred, change) assert [("old_password", "Old password is wrong") ] == excinfo.value.reasons
def test_transforms_to_expected_change_object(self): data = MultiDict( { "old_password": "******", "new_password": "******", "confirm_new_password": "******", "submit": True, } ) form = forms.ChangePasswordForm(formdata=data) expected = PasswordUpdate(old_password="******", new_password="******") assert form.as_change() == expected
def test_actually_updates_password(self, user, database, plugin_manager, mock): new_password = str(uuid4()) password_change = PasswordUpdate("test", new_password) hook_impl = mock.MagicMock(spec=ChangeSetPostProcessor) plugin_manager.register(self.impl(hook_impl)) handler = DefaultPasswordUpdateHandler(db=database, plugin_manager=plugin_manager, validators=[]) handler.apply_changeset(user, password_change) same_user = User.query.get(user.id) assert same_user.check_password(new_password) hook_impl.post_process_changeset.assert_called_once_with(user=user)
def test_raises_persistence_error_if_save_fails(self, mock, user, plugin_manager): password_change = PasswordUpdate(str(uuid4()), str(uuid4())) db = mock.Mock() db.session.commit.side_effect = Exception("no") hook_impl = mock.MagicMock(spec=ChangeSetPostProcessor) plugin_manager.register(self.impl(hook_impl)) handler = DefaultPasswordUpdateHandler(db=db, plugin_manager=plugin_manager, validators=[]) with pytest.raises(PersistenceError) as excinfo: handler.apply_changeset(user, password_change) assert "Could not update password" in str(excinfo.value) hook_impl.post_process_changeset.assert_not_called()
def test_raises_stop_validation_if_errors_occur(self, mock, user, database, plugin_manager): validator = mock.Mock(spec=ChangeSetValidator) validator.validate.side_effect = ValidationError( "new_password", "Don't use that password") password_change = PasswordUpdate(str(uuid4()), str(uuid4())) hook_impl = mock.MagicMock(spec=ChangeSetPostProcessor) plugin_manager.register(self.impl(hook_impl)) handler = DefaultPasswordUpdateHandler(db=database, plugin_manager=plugin_manager, validators=[validator]) with pytest.raises(StopValidation) as excinfo: handler.apply_changeset(user, password_change) assert excinfo.value.reasons == [("new_password", "Don't use that password")] hook_impl.post_process_changeset.assert_not_called()
def test_updates_user_password_okay(self, user, mock): form = self.produce_form( old_password="******", new_password="******", confirm_new_password="******", ) handler = mock.Mock(spec=ChangeSetHandler) view = ChangePassword(form=form, password_update_handler=handler) result = view.post() flashed = get_flashed_messages(with_categories=True) assert len(flashed) == 1 assert flashed[0] == ("success", "Password updated.") assert result.status_code == 302 assert result.headers["Location"] == url_for("user.change_password") handler.apply_changeset.assert_called_once_with( user, PasswordUpdate(old_password="******", new_password="******") )
def test_doesnt_raise_if_old_passwords_match(self, Fred): change = PasswordUpdate("fred", str(uuid4())) validators.OldPasswordMustMatch().validate(Fred, change)
def test_doesnt_raise_if_passwords_dont_match(self, Fred): change = PasswordUpdate("fred", "actuallycompletelydifferent") validators.PasswordsMustBeDifferent().validate(Fred, change)