# Path: brasilcomvc/accounts/forms.py
# class DeleteUserForm(forms.Form):
# 
#     password = forms.CharField(label='Senha', widget=forms.PasswordInput)
# 
#     def __init__(self, user, *args, **kwargs):
#         super(DeleteUserForm, self).__init__(*args, **kwargs)
#         self.user = user
# 
#     def clean_password(self):
#         password = self.cleaned_data['password']
#         if not self.user.check_password(password):
#             self.add_error('password', 'Senha inválida')
#         return password
# 
# class LoginForm(AuthenticationForm):
# 
#     # this is named username so we can use Django's login view
#     username = forms.EmailField(required=True)
# 
# class UserAddressForm(forms.ModelForm):
# 
#     class Meta:
#         model = UserAddress
#         fields = '__all__'
# 
#     def __init__(self, *args, **kwargs):
#         super(UserAddressForm, self).__init__(*args, **kwargs)
# 
#         # Limit regions to available cities
#         states = Region.objects.filter(
#             id__in=set(City.objects.values_list('region_id', flat=True)))
#         self.fields['state'].queryset = states
#         self.fields['state'].choices = states.values_list('id', 'name')
# 
#         # Group cities by region (state)
#         self.fields['city'].choices = self._group_cities()
# 
#     def _group_cities(self):
#         '''
#         Build a choices-like list with all cities grouped by state (region)
#         '''
#         return [
#             (state, [(city.pk, city.name) for city in cities],)
#             for state, cities in groupby(
#                 self.fields['city'].queryset.order_by('region__name', 'name'),
#                 lambda city: city.region.name)]


from django.contrib.auth import get_user_model
from django.contrib.auth.forms import AuthenticationForm
from django.forms import EmailField
from django.test import TestCase
from cities_light.models import City, Region
from ..forms import DeleteUserForm, LoginForm, UserAddressForm

    def test_form_should_have_all_fields(self):
        self.assertEqual(UserAddressForm.Meta.fields, '__all__')

    def test_form_state_field_should_have_used_regions_only(self):
        region = Region.objects.order_by('?')[0]
        City.objects.filter(region=region).delete()
        self.assertEqual(
            set(UserAddressForm().fields['state'].queryset),
            set(Region.objects.exclude(id=region.id)))

    def test_form_group_cities(self):
        expected_choices = [
            (region.name, list(
                region.city_set.order_by('name').values_list('pk', 'name')),)
            for region in Region.objects.order_by('name')
        ]
        self.assertEqual(UserAddressForm()._group_cities(), expected_choices)


class DeleteUserFormTestCase(TestCase):

    def test_clean_password_with_valid_password(self):
        u = User.objects.create_user('wat@wat.net', 'test')
        f = DeleteUserForm(user=u, data={'password': 'test'})
        self.assertTrue(f.is_valid())
        self.assertEqual(f.errors, {})

    def test_clean_password_with_invalid_password(self):
        u = User.objects.create_user('wat@wat.net', 'hardtoguess')

# Path: brasilcomvc/accounts/forms.py
# class DeleteUserForm(forms.Form):
# 
#     password = forms.CharField(label='Senha', widget=forms.PasswordInput)
# 
#     def __init__(self, user, *args, **kwargs):
#         super(DeleteUserForm, self).__init__(*args, **kwargs)
#         self.user = user
# 
#     def clean_password(self):
#         password = self.cleaned_data['password']
#         if not self.user.check_password(password):
#             self.add_error('password', 'Senha inválida')
#         return password
# 
# class LoginForm(AuthenticationForm):
# 
#     # this is named username so we can use Django's login view
#     username = forms.EmailField(required=True)
# 
# class UserAddressForm(forms.ModelForm):
# 
#     class Meta:
#         model = UserAddress
#         fields = '__all__'
# 
#     def __init__(self, *args, **kwargs):
#         super(UserAddressForm, self).__init__(*args, **kwargs)
# 
#         # Limit regions to available cities
#         states = Region.objects.filter(
#             id__in=set(City.objects.values_list('region_id', flat=True)))
#         self.fields['state'].queryset = states
#         self.fields['state'].choices = states.values_list('id', 'name')
# 
#         # Group cities by region (state)
#         self.fields['city'].choices = self._group_cities()
# 
#     def _group_cities(self):
#         '''
#         Build a choices-like list with all cities grouped by state (region)
#         '''
#         return [
#             (state, [(city.pk, city.name) for city in cities],)
#             for state, cities in groupby(
#                 self.fields['city'].queryset.order_by('region__name', 'name'),
#                 lambda city: city.region.name)]


# Path: brasilcomvc/accounts/tests/test_forms.py
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import AuthenticationForm
from django.forms import EmailField
from django.test import TestCase
from cities_light.models import City, Region
from ..forms import DeleteUserForm, LoginForm, UserAddressForm


    def test_form_should_have_all_fields(self):
        self.assertEqual(UserAddressForm.Meta.fields, '__all__')

    def test_form_state_field_should_have_used_regions_only(self):
        region = Region.objects.order_by('?')[0]
        City.objects.filter(region=region).delete()
        self.assertEqual(
            set(UserAddressForm().fields['state'].queryset),
            set(Region.objects.exclude(id=region.id)))

    def test_form_group_cities(self):
        expected_choices = [
            (region.name, list(
                region.city_set.order_by('name').values_list('pk', 'name')),)
            for region in Region.objects.order_by('name')
        ]
        self.assertEqual(UserAddressForm()._group_cities(), expected_choices)


class DeleteUserFormTestCase(TestCase):

    def test_clean_password_with_valid_password(self):
        u = User.objects.create_user('wat@wat.net', 'test')
        f = DeleteUserForm(user=u, data={'password': 'test'})
        self.assertTrue(f.is_valid())
        self.assertEqual(f.errors, {})

    def test_clean_password_with_invalid_password(self):
        u = User.objects.create_user('wat@wat.net', 'hardtoguess')

        f = DeleteUserForm(user=u, data={'password': 'guess'})