Skip to content

Commit

Permalink
- adds basic flow integration test for Registrion Flow
Browse files Browse the repository at this point in the history
- replaces print by self.stdout.write within command
- replaces date.today calls by timezone ones in registration_view
- adds test to check for username validation

Ref: #71
  • Loading branch information
oliveratfoodcoopx authored and mk2301 committed Feb 1, 2023
1 parent 98c0d64 commit d891ee9
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 10 deletions.
6 changes: 6 additions & 0 deletions tapir/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ def get_permissions_display(self):
return _("None")
return ", ".join(user_perms)

def save(self, *args, **kwargs):
self.username_validator(self.username)
return super().save(*args, **kwargs)



class UpdateTapirUserLogEntry(UpdateModelLogEntry):
template_name = "accounts/log/update_tapir_user_log_entry.html"
Expand All @@ -216,6 +221,7 @@ class UpdateTapirUserLogEntry(UpdateModelLogEntry):

# The following LDAP-related models were taken from
# https://source.puri.sm/liberty/host/middleware/-/blob/master/ldapregister/models.py
# https://github.com/django-ldapdb/django-ldapdb/blob/master/ldapdb/backends/ldap/base.py
class LdapPerson(ldapdb.models.Model):
"""
Class for representing an LDAP person entry.
Expand Down
34 changes: 34 additions & 0 deletions tapir/accounts/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from unittest import mock
from django.test import TestCase
from django.core.exceptions import ValidationError
from django.contrib.auth import get_user_model

User = get_user_model()

class TapirUserTests(TestCase):

def setUp(self):
super().setUp()
has_ldap_patch = mock.patch("tapir.accounts.models.LdapUser.has_ldap")
self.has_ldap = has_ldap_patch.start()

ldap_save_patch = mock.patch("tapir.accounts.models.LdapPerson.save")
self.ldap_save = ldap_save_patch.start()

self.addCleanup(has_ldap_patch.stop)
self.addCleanup(ldap_save_patch.stop)

def test_model_validates_usernames(self):
self.has_ldap.return_value = 0
self.ldap_save.return_value = None

usernames = [
"asd$",
]
for username in usernames:
try:
User.objects.create_user(username=username)
except ValidationError:
continue
self.fail(f"validation error not raised for username: {username}")

90 changes: 88 additions & 2 deletions tapir/accounts/tests/test_registration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,93 @@
from io import StringIO
from unittest import mock
from django_webtest import WebTest
from django.contrib.auth import get_user_model
from django.urls import reverse
from django.utils import timezone
from django.core.management import call_command
from tapir.wirgarten.models import GrowingPeriod

User = get_user_model()

class WizardTests(WebTest):

def test_bla(self):
a
@classmethod
def setUpTestData(cls):
cls.now = now = timezone.now()

call_command("parameter_definitions", stdout=StringIO())
fixtures = [
"0010_pickup_locations",
"0020_product_types",
"0021_tax_rates",
"0030_products",
"0031_product_prices",
"0040_growing_periods",
"0050_product_capacity",
"0060_pickup_location_capabilities",
]
for fix in fixtures:
call_command("loaddata", f"tapir/wirgarten/fixtures/{fix}", app="wirgarten", stdout=StringIO())

GrowingPeriod.objects.create(
start_date=now.replace(year=now.year - 1, month=3, day=1),
end_date=now.replace(year=now.year + 1, month=2, day=28)
)

@mock.patch('tapir.accounts.models.LdapPerson.save')
@mock.patch('tapir.accounts.models.LdapUser.has_ldap')
def test_simple_signup_flow(self, has_ldap, ldap_save):
has_ldap.return_value = False
ldap_save.return_value = None

form = self.app.get(reverse('wirgarten:draftuser_register')).form
form["Harvest Shares-harvest_shares_s"] = 1
r = form.submit()

form = r.form
form["Cooperative Shares-statute_consent"] = True
r = form.submit()

form = r.form
r = form.submit()

form = r.form
r = form.submit()

form = r.form
r = form.submit()

form = r.form
r = form.submit()

form = r.form
form['Personal Details-first_name'] = 'Firstname'
form['Personal Details-last_name'] = 'Lastname'
form['Personal Details-email'] = 'any@mail.com'
form['Personal Details-phone_number'] = '+12125552368'
form['Personal Details-street'] = 'fake street'
form['Personal Details-street_2'] = ''
form['Personal Details-postcode'] = '12345'
form['Personal Details-city'] = 'Berlin'
form['Personal Details-country'] = 'DE'
form['Personal Details-birthdate'] = ''
r = form.submit()

form = r.form
form['Payment Details-account_owner'] = 'Firstname Lastname'
form['Payment Details-iban'] = 'DE89370400440532013000'
form['Payment Details-bic'] = 'DEUTDE5M'
r = form.submit()

form = r.form
form['Payment Details-sepa_consent'] = True
r = form.submit()

form = r.form
form['Consent-withdrawal_consent'] = True
form['Consent-privacy_consent'] = True
r = form.submit().follow()

user = User.objects.filter(email='any@mail.com').first()
self.assertIsNotNone(user)

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Command(BaseCommand):

@transaction.atomic
def handle(self, *args, **kwargs):
print("Importing parameter definitions:")
self.stdout.write("Importing parameter definitions:")

for cls in TapirParameterDefinitionImporter.__subclasses__():
print(" - " + cls.__module__ + "." + cls.__name__)
self.stdout.write(" - " + cls.__module__ + "." + cls.__name__)
cls.import_definitions(cls)
2 changes: 0 additions & 2 deletions tapir/wirgarten/forms/registration/harvest_shares.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def __init__(self, *args, **kwargs):
): p
for p in harvest_share_products
}

self.field_order = list(self.products.keys()) + ["solidarity_price"]
self.n_columns = len(self.products)
self.colspans = {"solidarity_price": self.n_columns}
Expand All @@ -94,7 +93,6 @@ def __init__(self, *args, **kwargs):
",", "."
)
)

for prod in harvest_share_products:
self.fields[
f"{HARVEST_SHARE_FIELD_PREFIX}{prod.name.lower()}"
Expand Down
1 change: 0 additions & 1 deletion tapir/wirgarten/service/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ def get_free_product_capacity(
),
)
)

return total_capacity - used_capacity


Expand Down
5 changes: 2 additions & 3 deletions tapir/wirgarten/views/registration_view.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from datetime import date
from importlib.resources import _

from django.db import transaction
from django.http import HttpResponseRedirect
from django.urls import reverse_lazy
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views import generic
from django.views.decorators.clickjacking import xframe_options_exempt
Expand Down Expand Up @@ -102,7 +102,7 @@ def save_member(form_dict):
member.bic = form_dict[STEP_PAYMENT_DETAILS].cleaned_data["bic"]
member.is_active = False

now = date.today()
now = timezone.now()
member.sepa_consent = now
member.withdrawal_consent = now
member.privacy_consent = now
Expand Down Expand Up @@ -141,7 +141,6 @@ def init_conditions():
except Exception as e:
print("Could not init registration wizard conditions: ", e)


@method_decorator(xframe_options_exempt, name="dispatch")
@method_decorator(xframe_options_exempt, name="post")
class RegistrationWizardView(CookieWizardView):
Expand Down

0 comments on commit d891ee9

Please sign in to comment.