<commit_before># -*- coding: utf-8 -*-
import re

from django.contrib.auth.models import AnonymousUser

from nose.tools import eq_
from pyquery import PyQuery as pq
import waffle

from addons.models import Addon
from addons.tests.test_views import TestPersonas
import amo
import amo.tests
from amo.urlresolvers import reverse
from market.models import PreApprovalUser
from users.helpers import (addon_users_list, emaillink, user_data, user_link,
                           users_list)
from users.models import UserProfile, RequestUser


def test_emaillink():
    email = '<EMAIL>'
    obfuscated = unicode(emaillink(email))

    # remove junk
    m = re.match(r'<a href="#"><span class="emaillink">(.*?)'
                  '<span class="i">null</span>(.*)</span></a>'
                  '<span class="emaillink js-hidden">(.*?)'
                  '<span class="i">null</span>(.*)</span>', obfuscated)
    obfuscated = (''.join((m.group(1), m.group(2)))
                  .replace('&#x0040;', '@').replace('&#x002E;', '.'))[::-1]
    eq_(email, obfuscated)

    title = 'E-mail your question'
    obfuscated = unicode(emaillink(email, title))
    m = re.match(r'<a href="#">(.*)</a>'
                  '<span class="emaillink js-hidden">(.*?)'
                  '<span class="i">null</span>(.*)</span>', obfuscated)
    eq_(title, m.group(1))
    obfuscated = (''.join((m.group(2), m.group(3)))
                  .replace('&#x0040;', '@').replace('&#x002E;', '.'))[::-1]
    eq_(email, obfuscated)


def test_user_link():
    u = UserProfile(username='jconnor', display_name='<NAME>', pk=1)
    eq_(user_link(u), '<a href="%s"><NAME></a>' %
        reverse('users.profile', args=[1]))

    # handle None gracefully
    eq_(user_link(None), '')


def test_user_link_xss():
    u = UserProfile(username='jconnor',
                    display_name='<script>alert(1)</script>', pk=1)
    url = reverse('users.profile', args=[1])
    html = "&lt;script&gt;alert(1)&lt;/script&gt;"
    eq_(user_link(u), '<a href="%s">%s</a>' % (url, html))


def test_users_list():
    u1 = UserProfile(username='jconnor', display_name='<NAME>', pk=1)
    u2 = UserProfile(username='sconnor', display_name='<NAME>', pk=2)
    eq_(users_list([u1, u2]), ', '.join((user_link(u1), user_link(u2))))

    # handle None gracefully
    eq_(user_link(None), '')


def test_short_users_list():
    """Test the option to shortened the users list to a certain size."""
    # short list with 'others'
    u1 = UserProfile(username='oscar', display_name='<NAME>', pk=1)
    u2 = UserProfile(username='grover', display_name='Grover', pk=2)
    u3 = UserProfile(username='cookies!', display_name='Cookie Monster', pk=3)
    shortlist = users_list([u1, u2, u3], size=2)
    eq_(shortlist, ', '.join((user_link(u1), user_link(u2))) + ', others')


def test_user_link_unicode():
    """make sure helper won't choke on unicode input"""
    u = UserProfile(username=u'jmüller', display_name=u'<NAME>', pk=1)
    eq_(user_link(u), u'<a href="%s"><NAME></a>' %
        reverse('users.profile', args=[1]))

    u = UserProfile(username='\xe5\xaf\x92\xe6\x98\x9f', pk=1)
    url = reverse('users.profile', args=[1])
    eq_(user_link(u),
        u'<a href="%s">%s</a>' % (url, u.username))


class TestAddonUsersList(TestPersonas, amo.tests.TestCase):

    def setUp(self):
        self.addon = Addon.objects.get(id=15663)
        self.persona = self.addon.persona
        waffle.models.Switch.objects.create(
            name='personas-migration-completed', active=True)
        self.create_addon_user(self.addon)

    def test_by(self):
        """Test that the by... bit works."""
        content = addon_users_list({'amo': amo}, self.addon)
        eq_(pq(content).text(), 'by %s' % self.addon.authors.all()[0].name)


def test_user_data():
    u = user_data(RequestUser(username='foo', pk=1))
    eq_(u['anonymous'], False)
    eq_(u['pre_auth'], False)


class TestUserData(amo.tests.TestCase):

    def test_user_data_approved(self):
        up = UserProfile.objects.create(email='<EMAIL>', username='foo')
        PreApprovalUser.objects.create(user=up, paypal_key='asd')
        eq_(user_data(RequestUser.objects.get(pk=up.pk)),
            {'anonymous': False, 'pre_auth': True, 'currency': 'USD',
             'email': '<EMAIL>'})

    def test_no_user_data(self):
        eq_(user_data(None),
            {'anonymous': True, 'pre_auth': False, 'currency': 'USD',
             'email': ''})

    def test_anonymous_user_data(self):
        eq_(user_data(AnonymousUser()),
            {'anonymous': True, 'pre_auth': False, 'currency': 'USD',
             'email': ''})

    def test_preapproval_user_data(self):
        up = UserProfile.objects.create(email='<EMAIL>', username='foo')
        PreApprovalUser.objects.create(user=up, paypal_key='asd',
                                       currency='EUR')
        eq_(user_data(RequestUser.objects.get(pk=up.pk)),
            {'anonymous': False, 'pre_auth': True, 'currency': 'EUR',
             'email': 'aq@a'})
<commit_msg>Fixed test to use correct email<commit_after># -*- coding: utf-8 -*-
import re

from django.contrib.auth.models import AnonymousUser

from nose.tools import eq_
from pyquery import PyQuery as pq
import waffle

from addons.models import Addon
from addons.tests.test_views import TestPersonas
import amo
import amo.tests
from amo.urlresolvers import reverse
from market.models import PreApprovalUser
from users.helpers import (addon_users_list, emaillink, user_data, user_link,
                           users_list)
from users.models import UserProfile, RequestUser


def test_emaillink():
    email = '<EMAIL>'
    obfuscated = unicode(emaillink(email))

    # remove junk
    m = re.match(r'<a href="#"><span class="emaillink">(.*?)'
                  '<span class="i">null</span>(.*)</span></a>'
                  '<span class="emaillink js-hidden">(.*?)'
                  '<span class="i">null</span>(.*)</span>', obfuscated)
    obfuscated = (''.join((m.group(1), m.group(2)))
                  .replace('&#x0040;', '@').replace('&#x002E;', '.'))[::-1]
    eq_(email, obfuscated)

    title = 'E-mail your question'
    obfuscated = unicode(emaillink(email, title))
    m = re.match(r'<a href="#">(.*)</a>'
                  '<span class="emaillink js-hidden">(.*?)'
                  '<span class="i">null</span>(.*)</span>', obfuscated)
    eq_(title, m.group(1))
    obfuscated = (''.join((m.group(2), m.group(3)))
                  .replace('&#x0040;', '@').replace('&#x002E;', '.'))[::-1]
    eq_(email, obfuscated)


def test_user_link():
    u = UserProfile(username='jconnor', display_name='<NAME>', pk=1)
    eq_(user_link(u), '<a href="%s"><NAME></a>' %
        reverse('users.profile', args=[1]))

    # handle None gracefully
    eq_(user_link(None), '')


def test_user_link_xss():
    u = UserProfile(username='jconnor',
                    display_name='<script>alert(1)</script>', pk=1)
    url = reverse('users.profile', args=[1])
    html = "&lt;script&gt;alert(1)&lt;/script&gt;"
    eq_(user_link(u), '<a href="%s">%s</a>' % (url, html))


def test_users_list():
    u1 = UserProfile(username='jconnor', display_name='<NAME>', pk=1)
    u2 = UserProfile(username='sconnor', display_name='<NAME>', pk=2)
    eq_(users_list([u1, u2]), ', '.join((user_link(u1), user_link(u2))))

    # handle None gracefully
    eq_(user_link(None), '')


def test_short_users_list():
    """Test the option to shortened the users list to a certain size."""
    # short list with 'others'
    u1 = UserProfile(username='oscar', display_name='<NAME>', pk=1)
    u2 = UserProfile(username='grover', display_name='Grover', pk=2)
    u3 = UserProfile(username='cookies!', display_name='<NAME>', pk=3)
    shortlist = users_list([u1, u2, u3], size=2)
    eq_(shortlist, ', '.join((user_link(u1), user_link(u2))) + ', others')


def test_user_link_unicode():
    """make sure helper won't choke on unicode input"""
    u = UserProfile(username=u'jmüller', display_name=u'<NAME>', pk=1)
    eq_(user_link(u), u'<a href="%s"><NAME></a>' %
        reverse('users.profile', args=[1]))

    u = UserProfile(username='\xe5\xaf\x92\xe6\x98\x9f', pk=1)
    url = reverse('users.profile', args=[1])
    eq_(user_link(u),
        u'<a href="%s">%s</a>' % (url, u.username))


class TestAddonUsersList(TestPersonas, amo.tests.TestCase):

    def setUp(self):
        self.addon = Addon.objects.get(id=15663)
        self.persona = self.addon.persona
        waffle.models.Switch.objects.create(
            name='personas-migration-completed', active=True)
        self.create_addon_user(self.addon)

    def test_by(self):
        """Test that the by... bit works."""
        content = addon_users_list({'amo': amo}, self.addon)
        eq_(pq(content).text(), 'by %s' % self.addon.authors.all()[0].name)


def test_user_data():
    u = user_data(RequestUser(username='foo', pk=1))
    eq_(u['anonymous'], False)
    eq_(u['pre_auth'], False)


class TestUserData(amo.tests.TestCase):

    def test_user_data_approved(self):
        up = UserProfile.objects.create(email='<EMAIL>', username='foo')
        PreApprovalUser.objects.create(user=up, paypal_key='asd')
        eq_(user_data(RequestUser.objects.get(pk=up.pk)),
            {'anonymous': False, 'pre_auth': True, 'currency': 'USD',
             'email': '<EMAIL>'})

    def test_no_user_data(self):
        eq_(user_data(None),
            {'anonymous': True, 'pre_auth': False, 'currency': 'USD',
             'email': ''})

    def test_anonymous_user_data(self):
        eq_(user_data(AnonymousUser()),
            {'anonymous': True, 'pre_auth': False, 'currency': 'USD',
             'email': ''})

    def test_preapproval_user_data(self):
        up = UserProfile.objects.create(email='<EMAIL>', username='foo')
        PreApprovalUser.objects.create(user=up, paypal_key='asd',
                                       currency='EUR')
        eq_(user_data(RequestUser.objects.get(pk=up.pk)),
            {'anonymous': False, 'pre_auth': True, 'currency': 'EUR',
             'email': '<EMAIL>'})

{'fraction_non_alphanumeric': 0.18577231744773606, 'fraction_numerical': 0.013078371148320618, 'mean_word_length': 3.3118325501922254, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4018056', 'n_tokens_mistral': 3569, 'n_tokens_neox': 3469, 'n_words': 644}