def test_update_user_details_successfully_updates(self, user, mock): form = self.produce_form( birthday="25 04 2000", gender="awesome", location="here", website="http://web.site", avatar="http://web.site/avatar.png", signature="use a cursive font", notes="got 'em", ) handler = mock.Mock(spec=ChangeSetHandler) view = ChangeUserDetails(form=form, details_update_handler=handler) result = view.post() flashed = get_flashed_messages(with_categories=True) assert flashed == [("success", "User details updated.")] assert result.status_code == 302 assert result.headers["Location"] == url_for("user.change_user_details") handler.apply_changeset.assert_called_once_with( user, UserDetailsChange( birthday=date(2000, 4, 25), gender="awesome", location="here", website="http://web.site", avatar="http://web.site/avatar.png", signature="use a cursive font", notes="got 'em", ), )
def test_transforms_to_expected_change_object(self): data = MultiDict( dict( submit=True, birthday="25 06 2000", gender="awesome", location="here", website="http://flaskbb.org", avatar="https://totally.real/image.img", signature="test often", notes="testy mctest face", ) ) form = forms.ChangeUserDetailsForm(formdata=data) expected = UserDetailsChange( birthday=date(2000, 6, 25), gender="awesome", location="here", website="http://flaskbb.org", avatar="https://totally.real/image.img", signature="test often", notes="testy mctest face", ) assert form.as_change() == expected
def test_raises_if_image_doesnt_pass_checks(self, image_too_tall, Fred, responses): change = UserDetailsChange(avatar=image_too_tall.url) responses.add(image_too_tall) with pytest.raises(ValidationError) as excinfo: validators.ValidateAvatarURL().validate(Fred, change) assert "too high" in excinfo.value.reason
def test_raises_if_check_raises_requests_error(self, Fred, responses): url = "http://notfake.example/image.png" change = UserDetailsChange(avatar=url) responses.add(responses.GET, url=url, body=RequestException()) with pytest.raises(ValidationError) as excinfo: validators.ValidateAvatarURL().validate(Fred, change) assert excinfo.value.attribute == "avatar" assert excinfo.value.reason == "Could not retrieve avatar"
def test_actually_updates_users_details(self, user, database, plugin_manager, mock): location = str(uuid4()) details = UserDetailsChange(location=location) hook_impl = mock.Mock(spec=ChangeSetPostProcessor) plugin_manager.register(self.impl(hook_impl)) handler = DefaultDetailsUpdateHandler(db=database, plugin_manager=plugin_manager) handler.apply_changeset(user, details) same_user = User.query.get(user.id) assert same_user.location == location hook_impl.post_process_changeset.assert_called_once_with( user=user, details_update=details)
def test_raises_persistence_error_if_save_fails(self, mock, user, plugin_manager): details = UserDetailsChange() db = mock.Mock() db.session.commit.side_effect = Exception("no") hook_impl = mock.Mock(spec=ChangeSetPostProcessor) plugin_manager.register(self.impl(hook_impl)) handler = DefaultDetailsUpdateHandler(validators=[], db=db, plugin_manager=plugin_manager) with pytest.raises(PersistenceError) as excinfo: handler.apply_changeset(user, details) assert "Could not update details" 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( "location", "Dont be from there") details = UserDetailsChange() hook_impl = mock.Mock(spec=ChangeSetPostProcessor) plugin_manager.register(self.impl(hook_impl)) handler = DefaultDetailsUpdateHandler(validators=[validator], db=database, plugin_manager=plugin_manager) with pytest.raises(StopValidation) as excinfo: handler.apply_changeset(user, details) assert excinfo.value.reasons == [("location", "Dont be from there")] hook_impl.post_process_changeset.assert_not_called()
def test_passes_if_avatar_url_is_none(self, Fred): change = UserDetailsChange() validators.ValidateAvatarURL().validate(Fred, change)
def tests_passes_if_image_is_just_right(self, image_just_right, Fred, responses): change = UserDetailsChange(avatar=image_just_right.url) responses.add(image_just_right) validators.ValidateAvatarURL().validate(Fred, change)