<commit_before>import mock
import re

from stronghold import conf
from stronghold.middleware import LoginRequiredMiddleware

from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.test import TestCase
from django.test.client import RequestFactory


class StrongholdMiddlewareTestCase(TestCase):

    @classmethod
    def setUpClass(cls):
        cls.public_url = reverse("public_view")
        cls.private_url = reverse("protected_view")

    def test_public_view_is_public(self):
        response = self.client.get(self.public_url)
        self.assertEqual(response.status_code, 200)

    def test_private_view_is_private(self):
        response = self.client.get(self.private_url)
        self.assertEqual(response.status_code, 302)


class LoginRequiredMiddlewareTests(TestCase):

    def setUp(self):
        self.middleware = LoginRequiredMiddleware()

        self.request = RequestFactory().get('/test-protected-url/')
        self.request.user = mock.Mock()

        self.kwargs = {
            'view_func': HttpResponse,
            'view_args': [],
            'view_kwargs': {},
            'request': self.request,
        }

    def test_redirects_to_login_when_not_authenticated(self):
        self.request.user.is_authenticated.return_value = False

        response = self.middleware.process_view(**self.kwargs)

        self.assertEqual(response.status_code, 302)

    def test_returns_none_when_authenticated(self):
        self.request.user.is_authenticated.return_value = True

        response = self.middleware.process_view(**self.kwargs)

        self.assertEqual(response, None)

    def test_redirects_to_login_when_url_is_not_public(self):
        self.request.user.is_authenticated.return_value = False

        response = self.middleware.process_view(**self.kwargs)

        self.assertEqual(response.status_code, 302)

    def test_returns_none_when_url_is_public(self):
        self.request.user.is_authenticated.return_value = False
        self.middleware.public_view_urls = [re.compile(r'/test-protected-url/')]

        response = self.middleware.process_view(**self.kwargs)

        self.assertEqual(response, None)<commit_msg>refactor public urls test names<commit_after>import mock
import re

from stronghold import conf
from stronghold.middleware import LoginRequiredMiddleware

from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.test import TestCase
from django.test.client import RequestFactory


class StrongholdMiddlewareTestCase(TestCase):

    @classmethod
    def setUpClass(cls):
        cls.public_url = reverse("public_view")
        cls.private_url = reverse("protected_view")

    def test_public_view_is_public(self):
        response = self.client.get(self.public_url)
        self.assertEqual(response.status_code, 200)

    def test_private_view_is_private(self):
        response = self.client.get(self.private_url)
        self.assertEqual(response.status_code, 302)


class LoginRequiredMiddlewareTests(TestCase):

    def setUp(self):
        self.middleware = LoginRequiredMiddleware()

        self.request = RequestFactory().get('/test-protected-url/')
        self.request.user = mock.Mock()

        self.kwargs = {
            'view_func': HttpResponse,
            'view_args': [],
            'view_kwargs': {},
            'request': self.request,
        }

    def test_redirects_to_login_when_not_authenticated(self):
        self.request.user.is_authenticated.return_value = False

        response = self.middleware.process_view(**self.kwargs)

        self.assertEqual(response.status_code, 302)

    def test_returns_none_when_authenticated(self):
        self.request.user.is_authenticated.return_value = True

        response = self.middleware.process_view(**self.kwargs)

        self.assertEqual(response, None)

    def test_redirects_to_login_when_url_is_not_in_public_urls(self):
        self.request.user.is_authenticated.return_value = False

        response = self.middleware.process_view(**self.kwargs)

        self.assertEqual(response.status_code, 302)

    def test_returns_none_when_url_is_in_public_urls(self):
        self.request.user.is_authenticated.return_value = False
        self.middleware.public_view_urls = [re.compile(r'/test-protected-url/')]

        response = self.middleware.process_view(**self.kwargs)

        self.assertEqual(response, None)
{'fraction_non_alphanumeric': 0.09332732191163211, 'fraction_numerical': 0.005410279531109108, 'mean_word_length': 3.486349848331648, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7034463', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1249, 'n_words': 203}