<commit_before>import fudge

from contextlib import contextmanager

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase, Client


def get_users(filter_f=None):
    users = User.objects.all()
    assert len(users) > 0
    if not filter_f:
        return users
    filtered = filter(filter_f, users)
    assert len(filtered) > 0
    return filtered


@contextmanager
def user_with_generated_id(fake_auth, **kwargs):
    users = get_users(lambda u: not u.get_profile().has_chosen_identifier)
    fake_auth.expects_call().returns(users[0])
    yield


@contextmanager
def user_with_chosen_id(fake_auth, **kwargs):
    users = get_users(lambda u: u.get_profile().has_chosen_identifier)
    fake_auth.expects_call().returns(users[0])
    yield


class MiddlewareTests(TestCase):
    """
    Test `ProfileMiddleware`. Should ensure users have at least chosen a
    display name.
    """

    fixtures = ['users.json']

    def _get_and_follow_redirect(self, client, path):
        response = client.get(path)
        if response.status_code != 301:
            return response
        location = response.get('location', None)
        return client.get(location)

    @fudge.patch('django_browserid.auth.BrowserIDBackend.authenticate')
    def test_unsafe_paths(self, fake):
        with user_with_generated_id(fake):
            client = Client()
            client.login()
            response = self._get_and_follow_redirect(client, '/')
            assert response.status_code == 302
            location = response.get('location', None)
            assert location.endswith(reverse('users_edit'))
            response = self._get_and_follow_redirect(client, '/projects/')
            assert response.status_code == 302
            location = response.get('location', None)
            assert location.endswith(reverse('users_edit'))

    @fudge.patch('django_browserid.auth.BrowserIDBackend.authenticate')
    def test_safe_paths(self, fake):
        with user_with_generated_id(fake):
            client = Client()
            client.login()
            expect_ok = (
                self._get_and_follow_redirect(client, reverse('users_edit')),
                self._get_and_follow_redirect(
                    client, reverse('django.views.static.serve', kwargs={
                        'path': '/media/css/innovate/main.css'
                    })
                ),
                self._get_and_follow_redirect(
                    client, reverse('users_profile_add_link')),
                self._get_and_follow_redirect(
                    client, reverse('users_profile_links')),
                self._get_and_follow_redirect(
                    client, reverse('users_profile_delete_link', kwargs={
                        'id': 1,
                    })
                )
            )
            for response in expect_ok:
                self.assertEqual(200, response.status_code)
            # users_signout is expected to 302 regardless.
            response = self._get_and_follow_redirect(
                client, reverse('users_signout'))
            self.assertEqual(302, response.status_code)
            location = response.get('location', None)
            self.assertEqual('http://testserver/', location)

    @fudge.patch('django_browserid.auth.BrowserIDBackend.authenticate')
    def test_user_with_chosen_id(self, fake):
        with user_with_chosen_id(fake):
            client = Client()
            client.login()
            response = self._get_and_follow_redirect(client, '/')
            assert response.status_code == 200
            response = self._get_and_follow_redirect(client, '/projects/')
            assert response.status_code == 200
<commit_msg>Fix test that broke when 404s started working.<commit_after>import fudge

from contextlib import contextmanager

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase, Client

from users.models import Link


def get_users(filter_f=None):
    users = User.objects.all()
    assert len(users) > 0
    if not filter_f:
        return users
    filtered = filter(filter_f, users)
    assert len(filtered) > 0
    return filtered


@contextmanager
def user_with_generated_id(fake_auth, **kwargs):
    users = get_users(lambda u: not u.get_profile().has_chosen_identifier)
    fake_auth.expects_call().returns(users[0])
    yield users[0]


@contextmanager
def user_with_chosen_id(fake_auth, **kwargs):
    users = get_users(lambda u: u.get_profile().has_chosen_identifier)
    fake_auth.expects_call().returns(users[0])
    yield users[0]


class MiddlewareTests(TestCase):
    """
    Test `ProfileMiddleware`. Should ensure users have at least chosen a
    display name.
    """

    fixtures = ['users.json']

    def _get_and_follow_redirect(self, client, path, **kwargs):
        response = client.get(path, **kwargs)
        if response.status_code != 301:
            return response
        location = response.get('location', None)
        return client.get(location, **kwargs)

    @fudge.patch('django_browserid.auth.BrowserIDBackend.authenticate')
    def test_unsafe_paths(self, fake):
        with user_with_generated_id(fake):
            client = Client()
            client.login()
            response = self._get_and_follow_redirect(client, '/')
            assert response.status_code == 302
            location = response.get('location', None)
            assert location.endswith(reverse('users_edit'))
            response = self._get_and_follow_redirect(client, '/projects/')
            assert response.status_code == 302
            location = response.get('location', None)
            assert location.endswith(reverse('users_edit'))

    @fudge.patch('django_browserid.auth.BrowserIDBackend.authenticate')
    def test_safe_paths(self, fake):
        with user_with_generated_id(fake) as user:
            link = Link.objects.create(
                name="Mozilla",
                url="http://www.mozilla.org",
                profile=user.get_profile(),
            )
            client = Client()
            client.login()
            expect_ok = (
                self._get_and_follow_redirect(client, reverse('users_edit')),
                self._get_and_follow_redirect(
                    client, reverse('users_profile_add_link')),
                self._get_and_follow_redirect(
                    client, reverse('users_profile_links'),
                                    HTTP_X_REQUESTED_WITH='XMLHttpRequest'),
                self._get_and_follow_redirect(
                    client, reverse('users_profile_delete_link', kwargs={
                        'id': link.pk,
                    })
                )
            )
            for response in expect_ok:
                self.assertEqual(200, response.status_code)
            # users_signout is expected to 302 regardless.
            response = self._get_and_follow_redirect(
                client, reverse('users_signout'))
            self.assertEqual(302, response.status_code)
            location = response.get('location', None)
            self.assertEqual('http://testserver/', location)

    @fudge.patch('django_browserid.auth.BrowserIDBackend.authenticate')
    def test_user_with_chosen_id(self, fake):
        with user_with_chosen_id(fake):
            client = Client()
            client.login()
            response = self._get_and_follow_redirect(client, '/')
            assert response.status_code == 200
            response = self._get_and_follow_redirect(client, '/projects/')
            assert response.status_code == 200

{'fraction_non_alphanumeric': 0.09828169747461599, 'fraction_numerical': 0.00807081489195522, 'mean_word_length': 2.3712154453707766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1995031', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2049, 'n_words': 408}