# Path: sitegate/signin_flows/modern.py
# class ModernSigninForm(ClassicSigninForm):
# 
#     def __init__(self, request=None, *args, **kwargs):
#         super(ModernSigninForm, self).__init__(request, *args, **kwargs)
#         self.fields['username'].label = '%s / %s' % (_('Username'), _('Email'))
# 
#     def clean(self):
#         username = self.cleaned_data.get('username')
#         password = self.cleaned_data.get('password')
# 
#         if username and password and '@' in username:
#             # Let's first try an e-mail auth.
#             result = USER._default_manager.filter(email__iexact=username)
# 
#             if len(result) == 1:
#                 self.cleaned_data['username'] = result[0].username
# 
#             elif len(result) > 1:
#                 raise forms.ValidationError(
#                     _('There is more than one user with this e-mail. Please use your username to log in.'))
# 
#         return super(ModernSigninForm, self).clean()
# 
# Path: sitegate/signup_flows/modern.py
# class ModernSignupForm(SimpleClassicWithEmailSignupForm):
#     """Modernized form with unique e-mail field, without username and second password fields."""
# 
#     def __init__(self, *args, **kwargs):
#         super(ModernSignupForm, self).__init__(*args, **kwargs)
# 
#         if 'username' in self.fields:
#             del self.fields['username']
# 
#     def clean_email(self):
#         email = super(ModernSignupForm, self).clean_email()
# 
#         if USER._default_manager.filter(email__iexact=email):
#             raise forms.ValidationError(_('A user with that e-mail already exists.'))
# 
#         if len(email) > USERNAME_MAX_LEN:
#             raise forms.ValidationError(
#                 _('E-mail length should be no more than %(max_len)s characters long.'),
#                 params={'max_len': USERNAME_MAX_LEN})
# 
#         return email
# 
# class InvitationSignupForm(ModernSignupForm):
#     """Modernized form with invitation code field."""
# 
#     def __init__(self, *args, **kwargs):
#         super(InvitationSignupForm, self).__init__(*args, **kwargs)
# 
#         new_fields = {'code': forms.CharField(label=_('Invitation code'))}
#         new_fields.update(self.fields)
#         self.fields = new_fields
# 
#     def clean_code(self):
#         code = self.cleaned_data['code']
# 
#         if not InvitationCode.is_valid(code):
#             raise forms.ValidationError(_('This invitation code is invalid.'))
# 
#         return code


import pytest
from django.urls import reverse
from sitegate.signin_flows.modern import ModernSigninForm
from sitegate.signup_flows.modern import ModernSignupForm, InvitationSignupForm
    response = user_signup(user)
    assert response['location'].endswith(reverse('ok'))

    # auto_signin = False
    assert not str(response.client.cookies).strip()

    # he can't login yet
    assert not response.client.login(username=user.username, password=user.password_plain)

    # check fields
    db_user = user_model._default_manager.get(username=user.email)
    assert db_user.email == user.email
    assert db_user.username == user.email

    # try to sign up with the same data for a second time
    response = user_signup(user)
    assert response.status_code == 200
    assert b'A user with that e-mail already exists.' in response.content

    # try to signup with email too long
    user.email = '%s@some.com' % ('long' * 100)
    response = user_signup(user)

    assert response.status_code == 200
    assert b'length should be no more than' in response.content


class TestModernSignupForms:

    def test_modern_signup_attrs(self):

# Path: sitegate/signin_flows/modern.py
# class ModernSigninForm(ClassicSigninForm):
# 
#     def __init__(self, request=None, *args, **kwargs):
#         super(ModernSigninForm, self).__init__(request, *args, **kwargs)
#         self.fields['username'].label = '%s / %s' % (_('Username'), _('Email'))
# 
#     def clean(self):
#         username = self.cleaned_data.get('username')
#         password = self.cleaned_data.get('password')
# 
#         if username and password and '@' in username:
#             # Let's first try an e-mail auth.
#             result = USER._default_manager.filter(email__iexact=username)
# 
#             if len(result) == 1:
#                 self.cleaned_data['username'] = result[0].username
# 
#             elif len(result) > 1:
#                 raise forms.ValidationError(
#                     _('There is more than one user with this e-mail. Please use your username to log in.'))
# 
#         return super(ModernSigninForm, self).clean()
# 
# Path: sitegate/signup_flows/modern.py
# class ModernSignupForm(SimpleClassicWithEmailSignupForm):
#     """Modernized form with unique e-mail field, without username and second password fields."""
# 
#     def __init__(self, *args, **kwargs):
#         super(ModernSignupForm, self).__init__(*args, **kwargs)
# 
#         if 'username' in self.fields:
#             del self.fields['username']
# 
#     def clean_email(self):
#         email = super(ModernSignupForm, self).clean_email()
# 
#         if USER._default_manager.filter(email__iexact=email):
#             raise forms.ValidationError(_('A user with that e-mail already exists.'))
# 
#         if len(email) > USERNAME_MAX_LEN:
#             raise forms.ValidationError(
#                 _('E-mail length should be no more than %(max_len)s characters long.'),
#                 params={'max_len': USERNAME_MAX_LEN})
# 
#         return email
# 
# class InvitationSignupForm(ModernSignupForm):
#     """Modernized form with invitation code field."""
# 
#     def __init__(self, *args, **kwargs):
#         super(InvitationSignupForm, self).__init__(*args, **kwargs)
# 
#         new_fields = {'code': forms.CharField(label=_('Invitation code'))}
#         new_fields.update(self.fields)
#         self.fields = new_fields
# 
#     def clean_code(self):
#         code = self.cleaned_data['code']
# 
#         if not InvitationCode.is_valid(code):
#             raise forms.ValidationError(_('This invitation code is invalid.'))
# 
#         return code


# Path: sitegate/tests/test_flow_modern.py
import pytest
from django.urls import reverse
from sitegate.signin_flows.modern import ModernSigninForm
from sitegate.signup_flows.modern import ModernSignupForm, InvitationSignupForm

    response = user_signup(user)
    assert response['location'].endswith(reverse('ok'))

    # auto_signin = False
    assert not str(response.client.cookies).strip()

    # he can't login yet
    assert not response.client.login(username=user.username, password=user.password_plain)

    # check fields
    db_user = user_model._default_manager.get(username=user.email)
    assert db_user.email == user.email
    assert db_user.username == user.email

    # try to sign up with the same data for a second time
    response = user_signup(user)
    assert response.status_code == 200
    assert b'A user with that e-mail already exists.' in response.content

    # try to signup with email too long
    user.email = '%s@some.com' % ('long' * 100)
    response = user_signup(user)

    assert response.status_code == 200
    assert b'length should be no more than' in response.content


class TestModernSignupForms:

    def test_modern_signup_attrs(self):

        f = ModernSignupForm()