summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbuttle <chris@gatopelao.org>2023-06-11 23:26:30 +0200
committerbuttle <chris@gatopelao.org>2023-06-11 23:26:30 +0200
commit952863bf148c56ab8ea6469a7dd139faa17c5750 (patch)
tree211fa792d3d83fdd397467bad3d7d1b09802930b
parent5e7003de466dfae2b2d546808b93939837407487 (diff)
adds test_site_new_user_form_config.pychore/tests
-rw-r--r--liberaforms/models/consent.py9
-rw-r--r--liberaforms/views/site.py2
-rw-r--r--tests/functional/test_site_new_user_form_config.py133
3 files changed, 143 insertions, 1 deletions
diff --git a/liberaforms/models/consent.py b/liberaforms/models/consent.py
index 7bcaf839..983b650a 100644
--- a/liberaforms/models/consent.py
+++ b/liberaforms/models/consent.py
@@ -143,3 +143,12 @@ class Consent(db.Model, CRUD):
def get_copy_count(self) -> int:
return Consent.find_all(template_id=self.id).count()
+
+ def delete(self):
+ if self.site_id:
+ from liberaforms.models.site import Site
+ site = Site.find()
+ if self.id in site.registration_consent:
+ site.registration_consent.remove(self.id)
+ site.save()
+ super().delete()
diff --git a/liberaforms/views/site.py b/liberaforms/views/site.py
index 403703e0..708eca6a 100644
--- a/liberaforms/views/site.py
+++ b/liberaforms/views/site.py
@@ -112,7 +112,7 @@ def new_user_form_enable_consent():
if not ('consent_id' in request.form and 'enable' in request.form):
return jsonify("Not Acceptable"), 406
consent_id = json.loads(request.form['consent_id'])
- enable = json.loads(request.form['enable'])
+ enable = utils.str2bool(request.form['enable'])
consent = Consent.find(id=consent_id, site_id=g.site.id)
if not consent:
return jsonify("Not found"), 404
diff --git a/tests/functional/test_site_new_user_form_config.py b/tests/functional/test_site_new_user_form_config.py
new file mode 100644
index 00000000..4a0bd58c
--- /dev/null
+++ b/tests/functional/test_site_new_user_form_config.py
@@ -0,0 +1,133 @@
+"""
+This file is part of LiberaForms.
+
+# SPDX-FileCopyrightText: 2023 LiberaForms.org
+# SPDX-License-Identifier: AGPL-3.0-or-later
+"""
+
+from flask import url_for, jsonify
+from liberaforms.models.site import Site
+from liberaforms.models.consent import Consent
+from tests.factories import ConsentFactory
+from tests import user_creds
+from tests.utils import login, logout
+
+class TestSiteNewUserFormConfig():
+
+ @classmethod
+ def setup_class(cls):
+ cls.site = Site.find()
+ names = ["New user consent 1", "New user consent 2"]
+ cls.properties = {"names": names}
+
+ for name in names:
+ consent = ConsentFactory(name=name, site_id=cls.site.id)
+ consent.save()
+
+ def test_requirements(self, editor):
+ for name in self.properties["names"]:
+ assert Consent.find(name=name)
+
+ def test_auth(self, client):
+ """Test site_bp.new_user_form_preview
+ site_bp.new_user_form_enable_consent"""
+ logout(client)
+ response = client.get(
+ url_for('site_bp.new_user_form_preview'),
+ follow_redirects=True,
+ )
+ assert response.status_code == 200
+ assert '<!-- site_index_page -->' in response.data.decode()
+ response = client.post(
+ url_for('site_bp.new_user_form_enable_consent'),
+ follow_redirects=False,
+ )
+ assert response.status_code == 401
+
+ login(client, user_creds['editor'])
+ response = client.get(
+ url_for('site_bp.new_user_form_preview'),
+ follow_redirects=True,
+ )
+ assert response.status_code == 200
+ assert '<!-- my_forms_page -->' in response.data.decode()
+ response = client.post(
+ url_for('site_bp.new_user_form_enable_consent'),
+ follow_redirects=False,
+ )
+ assert response.status_code == 401
+
+ login(client, user_creds['admin'])
+
+ def test_preview_form(self, client):
+ response = client.get(
+ url_for('site_bp.new_user_form_preview'),
+ follow_redirects=False,
+ )
+ assert response.status_code == 200
+ assert '<!-- new_user_form_page -->' in response.data.decode()
+
+ def test_add_consent(self, client):
+ name = "New user consent 1"
+ consent = Consent.find(name=name, site_id=self.site.id)
+ assert consent.name == name
+ assert consent.id not in self.site.registration_consent
+ response = client.post(
+ url_for('site_bp.new_user_form_enable_consent'),
+ data={
+ "consent_id": consent.id,
+ "enable": True
+ },
+ follow_redirects=False,
+ )
+ assert self.site.registration_consent[0] == consent.id
+ assert response.status_code == 200
+ assert response.is_json is True
+ enabled_consents = list(response.json["enabled_consents"])
+ assert len(enabled_consents) == 1
+ assert len(enabled_consents) == len(self.site.registration_consent)
+ assert enabled_consents[0]["id"] == consent.id
+
+ name = "New user consent 2"
+ consent = Consent.find(name=name, site_id=self.site.id)
+ assert consent.name == name
+ assert consent.id not in self.site.registration_consent
+ response = client.post(
+ url_for('site_bp.new_user_form_enable_consent'),
+ data={
+ "consent_id": consent.id,
+ "enable": True
+ },
+ follow_redirects=False,
+ )
+ assert self.site.registration_consent[1] == consent.id
+ assert response.status_code == 200
+ assert response.is_json is True
+ enabled_consents = list(response.json["enabled_consents"])
+ assert len(enabled_consents) == 2
+ assert len(enabled_consents) == len(self.site.registration_consent)
+ assert enabled_consents[1]["id"] == consent.id
+
+
+ def test_remove_consent(self, client):
+ name = "New user consent 1"
+ consent = Consent.find(name=name, site_id=self.site.id)
+ assert consent.name == name
+ assert consent.id in self.site.registration_consent
+ response = client.post(
+ url_for('site_bp.new_user_form_enable_consent'),
+ data={
+ "consent_id": consent.id,
+ "enable": False
+ },
+ follow_redirects=False,
+ )
+
+ assert response.status_code == 200
+ assert consent.id not in self.site.registration_consent
+
+ def test_clean_up(self):
+ for name in self.properties["names"]:
+ consent = Consent.find(name=name, site_id=self.site.id)
+ consent.delete()
+ assert len(self.site.registration_consent) == 0