diff --git a/ihatemoney/api/common.py b/ihatemoney/api/common.py index 923a5391..f9f20165 100644 --- a/ihatemoney/api/common.py +++ b/ihatemoney/api/common.py @@ -50,7 +50,7 @@ def need_auth(f): class ProjectsHandler(Resource): def post(self): - form = ProjectForm(meta={"csrf": False}) + form = ProjectForm(meta={"csrf": False}, bypass_captcha=True) if form.validate() and current_app.config.get("ALLOW_PUBLIC_PROJECT_CREATION"): project = form.save() db.session.add(project) @@ -71,7 +71,7 @@ class ProjectHandler(Resource): return "DELETED" def put(self, project): - form = EditProjectForm(id=project.id, meta={"csrf": False}) + form = EditProjectForm(id=project.id, meta={"csrf": False}, bypass_captcha=True) if form.validate() and current_app.config.get("ALLOW_PUBLIC_PROJECT_CREATION"): form.update(project) db.session.commit() diff --git a/ihatemoney/forms.py b/ihatemoney/forms.py index a55166e8..f7b748f1 100644 --- a/ihatemoney/forms.py +++ b/ihatemoney/forms.py @@ -197,6 +197,10 @@ class ProjectForm(EditProjectForm): password = PasswordField(_("Private code"), validators=[DataRequired()]) submit = SubmitField(_("Create the project")) + def __init__(self, *args, **kwargs): + self.bypass_captcha = kwargs.get('bypass_captcha', False) + super().__init__(*args, **kwargs) + def save(self): """Create a new project with the information given by this form. @@ -232,12 +236,11 @@ class ProjectForm(EditProjectForm): def enable_captcha(cls): captchaField = StringField( _("Which is a real currency: Euro or Petro dollar?"), - validators=[DataRequired()], ) setattr(cls, "captcha", captchaField) def validate_captcha(form, field): - if not field.data.lower() == _("euro"): + if not field.data.lower() == _("euro") and not form.bypass_captcha: message = _("Please, validate the captcha to proceed.") raise ValidationError(Markup(message)) diff --git a/ihatemoney/tests/main_test.py b/ihatemoney/tests/main_test.py index 3efc4bf2..cbcc4742 100644 --- a/ihatemoney/tests/main_test.py +++ b/ihatemoney/tests/main_test.py @@ -286,6 +286,7 @@ class CaptchaTestCase(IhatemoneyTestCase): assert len(models.Project.query.all()) == 1 def test_api_project_creation_does_not_need_captcha(self): + self.client.get('/') resp = self.client.post( "/api/projects", data={