<commit_before>import logging
import urlparse
import urllib
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import fromstring

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

from mama_cas.models import ServiceTicket
from mama_cas.models import ProxyTicket
from mama_cas.models import ProxyGrantingTicket
from mama_cas.forms import LoginForm


logging.disable(logging.CRITICAL)

XMLNS = '{http://www.yale.edu/tp/cas}'


class LoginViewTests(TestCase):
    """
    Test the ``LoginView`` view.
    """
    user_info = { 'username': 'ellen',
                  'password': '<PASSWORD>',
                  'email': '<EMAIL>' }
    service = { 'service': 'http://www.example.com/' }

    def setUp(self):
        """
        Create a test user for authentication purposes.
        """
        self.user = User.objects.create_user(**self.user_info)

    def test_login_view(self):
        """
        When called with no parameters, a ``GET`` request to the
        view should display the correct template with a login form.
        """
        response = self.client.get(reverse('cas_login'))

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'mama_cas/login.html')
        self.assertTrue(isinstance(response.context['form'], LoginForm))

    def test_login_view_login(self):
        """
        When called with a valid username and password and no service, a
        ``POST`` request to the view should authenticate and login the user,
        and redirect to the ``LoginView`` view.
        """
        response = self.client.post(reverse('cas_login'), self.user_info)

        self.assertEqual(self.client.session['_auth_user_id'], self.user.pk)
        self.assertRedirects(response, reverse('cas_login'))

    def test_login_view_login_service(self):
        """
        When called with a valid username, password and service, a ``POST``
        request to the view should authenticate and login the user, create a
        ``ServiceTicket`` and redirect to the supplied service URL with a
        ticket parameter.
        """
        self.user_info.update(self.service)
        response = self.client.post(reverse('cas_login'), self.user_info)

        self.assertEqual(self.client.session['_auth_user_id'], self.user.pk)
        self.assertEqual(ServiceTicket.objects.count(), 1)
        # Check that the client is redirected properly without actually
        # trying to load the destination page
        parts = list(urlparse.urlparse(response['Location']))
        query = dict(urlparse.parse_qsl(parts[4]))
        destination = "%s://%s%s" % (parts[0], parts[1], parts[2])
        self.assertEqual(response.status_code, 302)
        self.assertEqual(destination, self.user_info['service'])
        self.assertTrue('ticket' in query)

class WarnViewTests(TestCase):
    """
    Test the ``WarnView`` view.
    """
    user_info = { 'username': 'ellen',
                  'password': '<PASSWORD>',
                  'email': '<EMAIL>' }
    valid_service = 'http://www.example.com/'
    form_data = user_info.copy()
    form_data.update({ 'warn': 'true' })

    def setUp(self):
        """
        Create a test user for authentication purposes.
        """
        self.user = User.objects.create_user(**self.user_info)

    def test_warn_view(self):
        """
        When called with no parameters and no logged in user, a ``GET``
        request to the view should simply redirect to the login view.
        """
        response = self.client.get(reverse('cas_warn'))

        self.assertRedirects(response, reverse('cas_login'),
                             status_code=302, target_status_code=200)

    def test_warn_view_post(self):
        """
        A ``POST`` request to the view should return an error that the method
        is not allowed.
        """
        response = self.client.post(reverse('cas_warn'))

        self.assertEqual(response.status_code, 405)

    def test_warn_view_redirect(self):
        """
        When called with a logged in user, a ``ServiceTicket`` request to the
        credential requestor should redirect to the correct view.
        """
        response = self.client.post(reverse('cas_login'), self.form_data)

        self.assertEqual(self.client.session.get('warn'), True)

        query_str = "?service=%s" % urllib.quote(self.valid_service, '')
        response = self.client.get(reverse('cas_login') + query_str)

        self.assertRedirects(response, reverse('cas_warn') + query_str,
                             status_code=302, target_status_code=200)

class LogoutViewTests(TestCase):
    """
    Test the ``LogoutView`` view.
    """<commit_msg>Add additional WarnView test cases<commit_after>import logging
import urlparse
import urllib
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import fromstring

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

from mama_cas.models import ServiceTicket
from mama_cas.models import ProxyTicket
from mama_cas.models import ProxyGrantingTicket
from mama_cas.forms import LoginForm


logging.disable(logging.CRITICAL)

XMLNS = '{http://www.yale.edu/tp/cas}'


class LoginViewTests(TestCase):
    """
    Test the ``LoginView`` view.
    """
    user_info = { 'username': 'ellen',
                  'password': '<PASSWORD>',
                  'email': '<EMAIL>' }
    service = { 'service': 'http://www.example.com/' }

    def setUp(self):
        """
        Create a test user for authentication purposes.
        """
        self.user = User.objects.create_user(**self.user_info)

    def test_login_view(self):
        """
        When called with no parameters, a ``GET`` request to the
        view should display the correct template with a login form.
        """
        response = self.client.get(reverse('cas_login'))

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'mama_cas/login.html')
        self.assertTrue(isinstance(response.context['form'], LoginForm))

    def test_login_view_login(self):
        """
        When called with a valid username and password and no service, a
        ``POST`` request to the view should authenticate and login the user,
        and redirect to the ``LoginView`` view.
        """
        response = self.client.post(reverse('cas_login'), self.user_info)

        self.assertEqual(self.client.session['_auth_user_id'], self.user.pk)
        self.assertRedirects(response, reverse('cas_login'))

    def test_login_view_login_service(self):
        """
        When called with a valid username, password and service, a ``POST``
        request to the view should authenticate and login the user, create a
        ``ServiceTicket`` and redirect to the supplied service URL with a
        ticket parameter.
        """
        self.user_info.update(self.service)
        response = self.client.post(reverse('cas_login'), self.user_info)

        self.assertEqual(self.client.session['_auth_user_id'], self.user.pk)
        self.assertEqual(ServiceTicket.objects.count(), 1)
        # Check that the client is redirected properly without actually
        # trying to load the destination page
        parts = list(urlparse.urlparse(response['Location']))
        query = dict(urlparse.parse_qsl(parts[4]))
        destination = "%s://%s%s" % (parts[0], parts[1], parts[2])
        self.assertEqual(response.status_code, 302)
        self.assertEqual(destination, self.user_info['service'])
        self.assertTrue('ticket' in query)

class WarnViewTests(TestCase):
    """
    Test the ``WarnView`` view.
    """
    user_info = { 'username': 'ellen',
                  'password': '<PASSWORD>',
                  'email': '<EMAIL>' }
    valid_service = 'http://www.example.com/'
    form_data = user_info.copy()
    form_data.update({ 'warn': 'true' })

    def setUp(self):
        """
        Create a test user for authentication purposes.
        """
        self.user = User.objects.create_user(**self.user_info)

    def test_warn_view(self):
        """
        When called with no parameters and no logged in user, a ``GET``
        request to the view should simply redirect to the login view.
        """
        response = self.client.get(reverse('cas_warn'))

        self.assertRedirects(response, reverse('cas_login'),
                             status_code=302, target_status_code=200)

    def test_warn_view_post(self):
        """
        A ``POST`` request to the view should return an error that the method
        is not allowed.
        """
        response = self.client.post(reverse('cas_warn'))

        self.assertEqual(response.status_code, 405)

    def test_warn_view_redirect(self):
        """
        When called with a logged in user, a ``ServiceTicket`` request to the
        credential requestor should redirect to the warn view.
        """
        response = self.client.post(reverse('cas_login'), self.form_data)

        self.assertEqual(self.client.session.get('warn'), True)

        query_str = "?service=%s" % urllib.quote(self.valid_service, '')
        response = self.client.get(reverse('cas_login') + query_str)

        self.assertRedirects(response, reverse('cas_warn') + query_str,
                             status_code=302, target_status_code=200)

    def test_warn_view_no_auth(self):
        """
        When called without a logged in user, a request to the warn view should
        redirect to the login view.
        """
        response = self.client.get(reverse('cas_warn'))

        self.assertRedirects(response, reverse('cas_login'), status_code=302,
                             target_status_code=200)

    def test_warn_view_display(self):
        """
        When called with a logged in user, a request to the warn view should
        display the correct template containing the provided service string.
        """
        response = self.client.post(reverse('cas_login'), self.form_data)
        query_str = "?service=%s" % urllib.quote(self.valid_service, '')
        response = self.client.get(reverse('cas_warn') + query_str)

        self.assertContains(response, self.valid_service, status_code=200)
        self.assertTemplateUsed(response, 'mama_cas/warn.html')

class LogoutViewTests(TestCase):
    """
    Test the ``LogoutView`` view.
    """
{'fraction_non_alphanumeric': 0.11403759900753889, 'fraction_numerical': 0.005821166141807424, 'mean_word_length': 2.764367816091954, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 4}, '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': '3479367', 'n_tokens_mistral': 2976, 'n_tokens_neox': 2883, 'n_words': 758}