<commit_before>"""
Tests using nose for the accounts application
"""

from django.forms import ValidationError
from django.contrib.auth.models import User
from django.test.client import Client
import nose.tools

from accounts.models import Profile
import accounts.forms
import accounts.models
import accounts.templatetags.filters

 # forms
def test_profile_form_zip():
    """Tests profile form zip code validation"""

    form = accounts.forms.ProfileForm()
    form.cleaned_data = {}
    form.cleaned_data['zip_code'] = u'01234'
    nose.tools.eq_(form.clean_zip_code(), u'01234', 'Good zip code should return unchanged')
    form.cleaned_data['zip_code'] = u'98765'
    nose.tools.eq_(form.clean_zip_code(), u'98765', 'Good zip code should return unchanged')
    # short zip code
    form.cleaned_data['zip_code'] = u'1234'
    nose.tools.assert_raises(ValidationError, form.clean_zip_code)
    # long zip code
    form.cleaned_data['zip_code'] = u'012345'
    nose.tools.assert_raises(ValidationError, form.clean_zip_code)
    # bad digits zip code
    form.cleaned_data['zip_code'] = u'01a23'
    nose.tools.assert_raises(ValidationError, form.clean_zip_code)

def test_profile_form_phone():
    """Tests profile form zip code validation"""

    form = accounts.forms.ProfileForm()
    form.cleaned_data = {}
    form.cleaned_data['phone'] = u'5551234567'
    nose.tools.eq_(form.clean_phone(), u'5551234567', 'Simple phone number')
    form.cleaned_data['phone'] = u'555-123-4567'
    nose.tools.eq_(form.clean_phone(), u'5551234567', 'Phone with punctuation')
    form.cleaned_data['phone'] = u'(555) 123-4567'
    nose.tools.eq_(form.clean_phone(), u'5551234567', 'Phone with punctuation and white space')
    form.cleaned_data['phone'] = u'1 (555) 123-4567'
    nose.tools.eq_(form.clean_phone(), u'5551234567', 'Phone with leading one')
    # short phone
    form.cleaned_data['phone'] = u'1234567890'
    nose.tools.assert_raises(ValidationError, form.clean_phone)
    form.cleaned_data['phone'] = u'(234) 567-890'
    nose.tools.assert_raises(ValidationError, form.clean_phone)
    # long phone
    form.cleaned_data['phone'] = u'(234) 567-89011'
    nose.tools.assert_raises(ValidationError, form.clean_phone)
    # bad phone
    form.cleaned_data['phone'] = u'1 (800) CALL-ATT'
    nose.tools.assert_raises(ValidationError, form.clean_phone)

def test_user_change_form_email():
    """Tests user change form email validation"""

    User.objects.create(username='test1', email='<EMAIL>')
    user2 = User.objects.create(username='test2', email='<EMAIL>')
    Profile.objects.create(user=user2)

    form = accounts.forms.UserChangeForm(instance=user2.get_profile())
    form.cleaned_data = {}
    form.cleaned_data['email'] = u'<EMAIL>'
    nose.tools.eq_(form.clean_email(), u'<EMAIL>', 'Non conflicting email')
    form.cleaned_data['email'] = u'<EMAIL>'
    nose.tools.eq_(form.clean_email(), u'<EMAIL>', 'Update without changing')
    form.cleaned_data['email'] = u'<EMAIL>'
    # conflicting email
    nose.tools.assert_raises(ValidationError, form.clean_email)

 # models
def test_profile_model_unicode():
    """Test profile model's __unicode__ method"""

    user3 = User.objects.create(username='test3', email='<EMAIL>')
    profile = accounts.models.Profile(user=user3)
    nose.tools.eq_(unicode(profile), u"Test3's Profile")

 # template filters
def test_format_phone():
    """Test the format phone template filter"""

    nose.tools.eq_(accounts.templatetags.filters.format_phone('5551234567'), '(555) 123-4567')

 # views
def test_views():
    """Test views and urls"""

    client = Client()

    def get_allowed(url, template):
        """Test a get on a url that is allowed with the users current credntials"""
        response = client.get(url)
        nose.tools.eq_(response.status_code, 200)
        nose.tools.eq_([t.name for t in response.template], [template, 'base.html'])

    def post_allowed(url, data, redirect):
        """Test an allowed post with the given data and redirect location"""
        response = client.post(url, data)
        nose.tools.eq_(response.status_code, 302)
        nose.tools.eq_(response['Location'], redirect)

    def post_allowed_bad(url, template):
        """Test an allowed post with bad data"""
        response = client.post(url, {'data': 'bad'})
        nose.tools.eq_(response.status_code, 200)
        nose.tools.eq_([t.name for t in response.template], [template, 'base.html'])

    def get_post_unallowed(url):
        """Test an unauthenticated get and post on a url that is allowed<commit_msg>clean up accounts tests<commit_after>"""
Tests using nose for the accounts application
"""

from django.forms import ValidationError
from django.contrib.auth.models import User
from django.test.client import Client
import nose.tools

from accounts.models import Profile
from accounts.templatetags.filters import format_phone
import accounts.forms

 # forms
def test_profile_form_zip():
    """Tests profile form zip code validation"""

    form = accounts.forms.ProfileForm()
    form.cleaned_data = {}
    form.cleaned_data['zip_code'] = u'01234'
    nose.tools.eq_(form.clean_zip_code(), u'01234', 'Good zip code should return unchanged')
    form.cleaned_data['zip_code'] = u'98765'
    nose.tools.eq_(form.clean_zip_code(), u'98765', 'Good zip code should return unchanged')
    # short zip code
    form.cleaned_data['zip_code'] = u'1234'
    nose.tools.assert_raises(ValidationError, form.clean_zip_code)
    # long zip code
    form.cleaned_data['zip_code'] = u'012345'
    nose.tools.assert_raises(ValidationError, form.clean_zip_code)
    # bad digits zip code
    form.cleaned_data['zip_code'] = u'01a23'
    nose.tools.assert_raises(ValidationError, form.clean_zip_code)

def test_profile_form_phone():
    """Tests profile form zip code validation"""

    form = accounts.forms.ProfileForm()
    form.cleaned_data = {}
    form.cleaned_data['phone'] = u'5551234567'
    nose.tools.eq_(form.clean_phone(), u'5551234567', 'Simple phone number')
    form.cleaned_data['phone'] = u'555-123-4567'
    nose.tools.eq_(form.clean_phone(), u'5551234567', 'Phone with punctuation')
    form.cleaned_data['phone'] = u'(555) 123-4567'
    nose.tools.eq_(form.clean_phone(), u'5551234567', 'Phone with punctuation and white space')
    form.cleaned_data['phone'] = u'1 (555) 123-4567'
    nose.tools.eq_(form.clean_phone(), u'5551234567', 'Phone with leading one')
    # short phone
    form.cleaned_data['phone'] = u'1234567890'
    nose.tools.assert_raises(ValidationError, form.clean_phone)
    form.cleaned_data['phone'] = u'(234) 567-890'
    nose.tools.assert_raises(ValidationError, form.clean_phone)
    # long phone
    form.cleaned_data['phone'] = u'(234) 567-89011'
    nose.tools.assert_raises(ValidationError, form.clean_phone)
    # bad phone
    form.cleaned_data['phone'] = u'1 (800) CALL-ATT'
    nose.tools.assert_raises(ValidationError, form.clean_phone)

def test_user_change_form_email():
    """Tests user change form email validation"""

    User.objects.create(username='test1', email='<EMAIL>')
    user2 = User.objects.create(username='test2', email='<EMAIL>')
    Profile.objects.create(user=user2)

    form = accounts.forms.UserChangeForm(instance=user2.get_profile())
    form.cleaned_data = {}
    form.cleaned_data['email'] = u'<EMAIL>'
    nose.tools.eq_(form.clean_email(), u'<EMAIL>', 'Non conflicting email')
    form.cleaned_data['email'] = u'<EMAIL>'
    nose.tools.eq_(form.clean_email(), u'<EMAIL>', 'Update without changing')
    form.cleaned_data['email'] = u'<EMAIL>'
    # conflicting email
    nose.tools.assert_raises(ValidationError, form.clean_email)

 # models
def test_profile_model_unicode():
    """Test profile model's __unicode__ method"""

    user3 = User.objects.create(username='test3', email='<EMAIL>')
    profile = Profile(user=user3)
    nose.tools.eq_(unicode(profile), u"Test3's Profile")

 # template filters
def test_format_phone():
    """Test the format phone template filter"""

    nose.tools.eq_(format_phone('5551234567'), '(555) 123-4567')

 # views
def test_views():
    """Test views and urls"""

    client = Client()

    def get_allowed(url, template):
        """Test a get on a url that is allowed with the users current credntials"""
        response = client.get(url)
        nose.tools.eq_(response.status_code, 200)
        nose.tools.eq_([t.name for t in response.template], [template, 'base.html'])

    def post_allowed(url, data, redirect):
        """Test an allowed post with the given data and redirect location"""
        response = client.post(url, data)
        nose.tools.eq_(response.status_code, 302)
        nose.tools.eq_(response['Location'], redirect)

    def post_allowed_bad(url, template):
        """Test an allowed post with bad data"""
        response = client.post(url, {'data': 'bad'})
        nose.tools.eq_(response.status_code, 200)
        nose.tools.eq_([t.name for t in response.template], [template, 'base.html'])

    def get_post_unallowed(url):
        """Test an unauthenticated get and post on a url that is allowed
{'fraction_non_alphanumeric': 0.1358200767964893, 'fraction_numerical': 0.0410312671420735, 'mean_word_length': 4.203196347031963, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5875921', 'n_tokens_mistral': 3172, 'n_tokens_neox': 2887, 'n_words': 642}