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

u"""
.. module:: test_auth
"""
from django.contrib.auth.models import User
from django.test import Client
from django.test import TestCase
from django.test import TransactionTestCase

from volontulo.tests.views.test_usersprofile import TestUsersProfile


class TestRegister(TransactionTestCase):
    u"""Tests for register view."""

    def setUp(self):
        u"""Set up each test."""
        self.client = Client()

    def test_get_method(self):
        u"""Test for get method for view."""
        response = self.client.get('/register')
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'auth/register.html')
        self.assertNotIn('_auth_user_id', self.client.session)

    def test_invalid_form(self):
        u"""Test for post method with invalid form."""
        response = self.client.post('/register', {})
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'auth/register.html')
        self.assertContains(
            response,
            u'Wprowadzono nieprawidłowy email lub hasło',
        )
        self.assertNotIn('_auth_user_id', self.client.session)

    def test_user_already_exists(self):
        u"""Test for attempt of registration for user, that already exists."""
        User.objects.create_user(
            u'<EMAIL>',
            u'<EMAIL>',
            u'1<PASSWORD>'
        )
        response = self.client.post('/register', {
            'email': u'<EMAIL>',
            'password': u'<PASSWORD>',
        })
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'auth/register.html')
        self.assertContains(
            response,
            u'Użytkownik o podanym emailu już istnieje',
        )
        self.assertNotIn('_auth_user_id', self.client.session)
        self.assertEqual(User.objects.all().count(), 1)

    def test_successful_registration(self):
        u"""Test for attempt of registration for new user."""
        response = self.client.post('/register', {
            'email': u'<EMAIL>',
            'password': u'<PASSWORD>',
        }, follow=True)
        self.assertRedirects(response, '/', 302, 200)
        self.assertContains(
            response,
            u'Rejestracja przebiegła pomyślnie',
        )
        self.assertIn('_auth_user_id', self.client.session)
        self.assertEqual(User.objects.all().count(), 1)


class TestAuth(TestCase):
    u"""Class responsible for testing users authentication view."""

    @classmethod
    def setUpTestData(cls):
        # volunteer user
        TestUsersProfile.initialize_empty_volunteer()

    def setUp(self):
        u"""Set up each test."""
        self.client = Client()

    # pylint: disable=invalid-name
    def test__logged_out_anonymous_user(self):
        u"""Testing logout view for anonymous user"""
        response = self.client.get('/logout', follow=True)

        self.assertRedirects(
            response,
            'http://testserver/login?next=/logout',<commit_msg>#223 fixes - replace TestUsersProfile with Common<commit_after># -*- coding: utf-8 -*-

u"""
.. module:: test_auth
"""
from django.contrib.auth.models import User
from django.test import Client
from django.test import TestCase
from django.test import TransactionTestCase

from volontulo.tests.common import Common


class TestRegister(TransactionTestCase):
    u"""Tests for register view."""

    def setUp(self):
        u"""Set up each test."""
        self.client = Client()

    def test_get_method(self):
        u"""Test for get method for view."""
        response = self.client.get('/register')
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'auth/register.html')
        self.assertNotIn('_auth_user_id', self.client.session)

    def test_invalid_form(self):
        u"""Test for post method with invalid form."""
        response = self.client.post('/register', {})
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'auth/register.html')
        self.assertContains(
            response,
            u'Wprowadzono nieprawidłowy email lub hasło',
        )
        self.assertNotIn('_auth_user_id', self.client.session)

    def test_user_already_exists(self):
        u"""Test for attempt of registration for user, that already exists."""
        User.objects.create_user(
            u'<EMAIL>',
            u'<EMAIL>',
            u'<PASSWORD>'
        )
        response = self.client.post('/register', {
            'email': u'<EMAIL>',
            'password': u'<PASSWORD>',
        })
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'auth/register.html')
        self.assertContains(
            response,
            u'Użytkownik o podanym emailu już istnieje',
        )
        self.assertNotIn('_auth_user_id', self.client.session)
        self.assertEqual(User.objects.all().count(), 1)

    def test_successful_registration(self):
        u"""Test for attempt of registration for new user."""
        response = self.client.post('/register', {
            'email': u'<EMAIL>',
            'password': u'<PASSWORD>',
        }, follow=True)
        self.assertRedirects(response, '/', 302, 200)
        self.assertContains(
            response,
            u'Rejestracja przebiegła pomyślnie',
        )
        self.assertIn('_auth_user_id', self.client.session)
        self.assertEqual(User.objects.all().count(), 1)


class TestAuth(TestCase):
    u"""Class responsible for testing users authentication view."""

    @classmethod
    def setUpTestData(cls):
        # volunteer user
        Common.initialize_empty_volunteer()

    def setUp(self):
        u"""Set up each test."""
        self.client = Client()

    # pylint: disable=invalid-name
    def test__logged_out_anonymous_user(self):
        u"""Testing logout view for anonymous user"""
        response = self.client.get('/logout', follow=True)

        self.assertRedirects(
            response,
            'http://testserver/login?next=/logout',
{'fraction_non_alphanumeric': 0.12353896103896105, 'fraction_numerical': 0.006493506493506494, 'mean_word_length': 2.5633314054366685, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '7301361', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1729, 'n_words': 376}