<commit_before>from uuid import uuid4

from django import test

from nose.tools import eq_, ok_
from pyquery import PyQuery as pq
import test_utils

from funfactory.urlresolvers import reverse
from phonebook.tests import LDAPTestCase
from phonebook.views import UNAUTHORIZED_DELETE

# The test data (below in module constants) must matches data in
# directory/mozillians-bulk-test-data.ldif
# You must have run x-rebuild before these tests
MOZILLIAN = dict(email='<EMAIL>', uniq_id='7f3a67u000001')
PENDING = dict(email='<EMAIL>', uniq_id='7f3a67u000003')
OTHER_MOZILLIAN = dict(email='<EMAIL>', uniq_id='7f3a67u000098')
AMANDEEP_NAME = '<NAME>'
AMANDEEP_VOUCHER = '7f3a67u000001'
AMANDA_NAME = '<NAME>'
PASSWORD = '<PASSWORD>'


class TestDeleteUser(LDAPTestCase):
    """Separate test class used to test account deletion flow.

    We create a separate class to delete a user because other tests depend
    on Mozillian users existing.
    """
    def test_confirm_delete(self):
        """Test the account deletion flow, including confirmation.

        A user should not be presented with a form/link that allows them to
        delete their account without a confirmation page. Once they access that
        page, they should be presented with a link to "go back" to their
        profile or to permanently delete their account.

        This test is abstracted away to a generic user deletion flow so
        we can test both non-vouched and vouched user's ability to delete
        their own profile.
        """
        for user in [MOZILLIAN, PENDING]:
            self._delete_flow(user)

    def _delete_flow(self, user):
        """Private method used to walk through account deletion flow."""
        client = _mozillian_client(user['email'])
        uniq_id = user['uniq_id']

        r = client.get(reverse('phonebook.edit_profile', args=[uniq_id]))
        doc = pq(r.content)

        # Make sure there's a link to a confirm deletion page, and nothing
        # pointing directly to the delete URL.
        eq_(reverse('confirm_delete'), doc('#delete-profile').attr('href'),
            'We see a link to a confirmation page.')
        self.assertFalse(any((reverse('phonebook.delete_profile') in el.action)
                              for el in doc('#main form')),
            "We don't see a form posting to the account delete URL.")

        # Follow the link to the deletion confirmation page.
        r = client.get(doc('#delete-profile').attr('href'))

        # Test that we can go back (i.e. cancel account deletion).
        doc = pq(r.content)
        eq_(reverse('phonebook.edit_profile', args=[uniq_id]),
            doc('#cancel-action').attr('href'))

        # Test that account deletion works.
        delete_url = doc('#delete-action').closest('form').attr('action')
        r = client.post(delete_url, {'unique_id': uniq_id}, follow=True)
        eq_(200, r.status_code)
        self.assertFalse(_logged_in_html(r))

        # Make sure the user can't login anymore
        client = test.Client()
        data = dict(username=user['email'], password=PASSWORD)
        r = client.post(reverse('login'), data, follow=True)
        self.assertFalse(_logged_in_html(r))


class TestViews(LDAPTestCase):
    def setUp(self):
        """Create multiple test clients of varying authorization levels."""
        self.anon_client = self.client
        self.pending_client = _mozillian_client(PENDING['email'])
        self.mozillian_client = _mozillian_client(MOZILLIAN['email'])

    def test_anonymous_home(self):
        r = self.anon_client.get('/', follow=True)
        self.assertEquals(200, r.status_code)
        doc = pq(r.content)
        login = reverse('login')
        eq_(doc('a#login').attr('href'), login, 'We see a link to login')
        self.assertFalse(_logged_in_html(r))

    def test_pending_home(self):
        r = self.pending_client.get('/', follow=True)
        self.assertEquals(200, r.status_code)
        self.assertTrue(_logged_in_html(r))
        doc = pq(r.content)
        profile = reverse('profile', args=[PENDING['uniq_id']])
        eq_(profile, doc('a#profile').attr('href'),
            'We see a link to our profile')

    def test_mozillian_home(self):
        r = self.mozillian_client.get('/', follow=True)
        self.assertEquals(200, r.status_code)
        self.assertTrue(_logged_in_html(r))
        doc = pq(r.content)
        profile = reverse('profile', args=[MOZILLIAN['uniq_id']])
        eq_(profile, doc('a#profile').attr('href'),
            'We see a link to our profile')

    def test_anonymous_or_pending_search(self):
        search = reverse('phonebook.search')
        for client in [self.anon_client, self.pending_client]:
            r = client.get(search, dict(q='Am'), follow=True)
            peeps = r.context['people']
            eq_(0, len(peeps),
                'Search should fail for interlopers')

    def test_mozillian_search(self):
        url = reverse('phonebook.search')
        r = self.mozillian_client.get(url, dict(q='Am'))
        peeps = r.context['people']
        saw_amandeep = saw_amanda = False

        for person in peeps:
            if person.full_name == AMANDEEP_NAME:
                eq_(AMANDEEP_VOUCHER,
                    person.voucher_unique_id,
                    'Amandeep is a Mozillian')
                saw_amandeep = True
            elif person.full_name == AMANDA_NAME:<commit_msg>Anon and Pending have two different behaviors on search now.<commit_after>from uuid import uuid4

from django import test

from nose.tools import eq_, ok_
from pyquery import PyQuery as pq
import test_utils

from funfactory.urlresolvers import reverse
from phonebook.tests import LDAPTestCase
from phonebook.views import UNAUTHORIZED_DELETE

# The test data (below in module constants) must matches data in
# directory/mozillians-bulk-test-data.ldif
# You must have run x-rebuild before these tests
MOZILLIAN = dict(email='<EMAIL>', uniq_id='7f3a67u000001')
PENDING = dict(email='<EMAIL>', uniq_id='7f3a67u000003')
OTHER_MOZILLIAN = dict(email='<EMAIL>', uniq_id='7f3a67u000098')
AMANDEEP_NAME = '<NAME>'
AMANDEEP_VOUCHER = '7f3a67u000001'
AMANDA_NAME = '<NAME>'
PASSWORD = '<PASSWORD>'


class TestDeleteUser(LDAPTestCase):
    """Separate test class used to test account deletion flow.

    We create a separate class to delete a user because other tests depend
    on Mozillian users existing.
    """
    def test_confirm_delete(self):
        """Test the account deletion flow, including confirmation.

        A user should not be presented with a form/link that allows them to
        delete their account without a confirmation page. Once they access that
        page, they should be presented with a link to "go back" to their
        profile or to permanently delete their account.

        This test is abstracted away to a generic user deletion flow so
        we can test both non-vouched and vouched user's ability to delete
        their own profile.
        """
        for user in [MOZILLIAN, PENDING]:
            self._delete_flow(user)

    def _delete_flow(self, user):
        """Private method used to walk through account deletion flow."""
        client = _mozillian_client(user['email'])
        uniq_id = user['uniq_id']

        r = client.get(reverse('phonebook.edit_profile', args=[uniq_id]))
        doc = pq(r.content)

        # Make sure there's a link to a confirm deletion page, and nothing
        # pointing directly to the delete URL.
        eq_(reverse('confirm_delete'), doc('#delete-profile').attr('href'),
            'We see a link to a confirmation page.')
        self.assertFalse(any((reverse('phonebook.delete_profile') in el.action)
                              for el in doc('#main form')),
            "We don't see a form posting to the account delete URL.")

        # Follow the link to the deletion confirmation page.
        r = client.get(doc('#delete-profile').attr('href'))

        # Test that we can go back (i.e. cancel account deletion).
        doc = pq(r.content)
        eq_(reverse('phonebook.edit_profile', args=[uniq_id]),
            doc('#cancel-action').attr('href'))

        # Test that account deletion works.
        delete_url = doc('#delete-action').closest('form').attr('action')
        r = client.post(delete_url, {'unique_id': uniq_id}, follow=True)
        eq_(200, r.status_code)
        self.assertFalse(_logged_in_html(r))

        # Make sure the user can't login anymore
        client = test.Client()
        data = dict(username=user['email'], password=PASSWORD)
        r = client.post(reverse('login'), data, follow=True)
        self.assertFalse(_logged_in_html(r))


class TestViews(LDAPTestCase):
    def setUp(self):
        """Create multiple test clients of varying authorization levels."""
        self.anon_client = self.client
        self.pending_client = _mozillian_client(PENDING['email'])
        self.mozillian_client = _mozillian_client(MOZILLIAN['email'])

    def test_anonymous_home(self):
        r = self.anon_client.get('/', follow=True)
        self.assertEquals(200, r.status_code)
        doc = pq(r.content)
        login = reverse('login')
        eq_(doc('a#login').attr('href'), login, 'We see a link to login')
        self.assertFalse(_logged_in_html(r))

    def test_pending_home(self):
        r = self.pending_client.get('/', follow=True)
        self.assertEquals(200, r.status_code)
        self.assertTrue(_logged_in_html(r))
        doc = pq(r.content)
        profile = reverse('profile', args=[PENDING['uniq_id']])
        eq_(profile, doc('a#profile').attr('href'),
            'We see a link to our profile')

    def test_mozillian_home(self):
        r = self.mozillian_client.get('/', follow=True)
        self.assertEquals(200, r.status_code)
        self.assertTrue(_logged_in_html(r))
        doc = pq(r.content)
        profile = reverse('profile', args=[MOZILLIAN['uniq_id']])
        eq_(profile, doc('a#profile').attr('href'),
            'We see a link to our profile')

    def test_anonymous_or_pending_search(self):
        search = reverse('phonebook.search')

        r = self.anon_client.get(search, dict(q='Am'), follow=True)
        self.assertFalse('people' in r.context)

        r = self.pending_client.get(search, dict(q='Am'), follow=True)
        eq_([], r.context['people'])

    def test_mozillian_search(self):
        url = reverse('phonebook.search')
        r = self.mozillian_client.get(url, dict(q='Am'))
        peeps = r.context['people']
        saw_amandeep = saw_amanda = False

        for person in peeps:
            if person.full_name == AMANDEEP_NAME:
                eq_(AMANDEEP_VOUCHER,
                    person.voucher_unique_id,
                    'Amandeep is a Mozillian')
                saw_amandeep = True
            elif person.full_name == AMANDA_NAME:
{'fraction_non_alphanumeric': 0.11154022988505748, 'fraction_numerical': 0.009839080459770114, 'mean_word_length': 2.8745992162451017, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5880348', 'n_tokens_mistral': 3335, 'n_tokens_neox': 3180, 'n_words': 887}