<commit_before>from __future__ import absolute_import, unicode_literals

from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils.text import capfirst

from wagtail.contrib.settings.registry import SettingMenuItem
from wagtail.tests.testapp.models import IconSetting, TestSetting
from wagtail.tests.utils import WagtailTestUtils
from wagtail.wagtailcore import hooks
from wagtail.wagtailcore.models import Page, Site


class TestSettingMenu(TestCase, WagtailTestUtils):

    def login_only_admin(self):
        """ Log in with a user that only has permission to access the admin """
        user = get_user_model().objects.create_user(
            username='test', email='<EMAIL>', password='password')
        user.user_permissions.add(Permission.objects.get_by_natural_key(
            codename='access_admin', app_label='wagtailadmin', model='admin'))
        self.client.login(username='test', password='password')
        return user

    def test_menu_item_in_admin(self):
        self.login()
        response = self.client.get(reverse('wagtailadmin_home'))

        self.assertContains(response, capfirst(TestSetting._meta.verbose_name))
        self.assertContains(response, reverse('wagtailsettings:edit', args=('tests', 'testsetting')))

    def test_menu_item_no_permissions(self):
        self.login_only_admin()
        response = self.client.get(reverse('wagtailadmin_home'))

        self.assertNotContains(response, TestSetting._meta.verbose_name)
        self.assertNotContains(response, reverse('wagtailsettings:edit', args=('tests', 'testsetting')))

    def test_menu_item_icon(self):
        menu_item = SettingMenuItem(IconSetting, icon='tag', classnames='test-class')
        classnames = set(menu_item.classnames.split(' '))
        self.assertEqual(classnames, {'icon', 'icon-tag', 'test-class'})


class BaseTestSettingView(TestCase, WagtailTestUtils):
    def get(self, site_pk=1, params={}):
        url = self.edit_url('tests', 'testsetting', site_pk=site_pk)
        return self.client.get(url, params)

    def post(self, site_pk=1, post_data={}):
        url = self.edit_url('tests', 'testsetting', site_pk=site_pk)
        return self.client.post(url, post_data)

    def edit_url(self, app, model, site_pk=1):
        return reverse('wagtailsettings:edit', args=[site_pk, app, model])


class TestSettingCreateView(BaseTestSettingView):
    def setUp(self):
        self.login()

    def test_status_code(self):
        self.assertEqual(self.get().status_code, 200)

    def test_edit_invalid(self):
        response = self.post(post_data={'foo': 'bar'})
        self.assertContains(response, "The setting could not be saved due to errors.")
        self.assertContains(response, "This field is required.")

    def test_edit(self):
        response = self.post(post_data={'title': 'Edited site title',
                                        'email': '<EMAIL>'})
        self.assertEqual(response.status_code, 302)

        default_site = Site.objects.get(is_default_site=True)
        setting = TestSetting.objects.get(site=default_site)
        self.assertEqual(setting.title, 'Edited site title')
        self.assertEqual(setting.email, '<EMAIL>')


class TestSettingEditView(BaseTestSettingView):
    def setUp(self):
        default_site = Site.objects.get(is_default_site=True)

        self.test_setting = TestSetting()
        self.test_setting.title = 'Site title'
        self.test_setting.email = '<EMAIL>'
        self.test_setting.site = default_site
        self.test_setting.save()

        self.login()

    def test_status_code(self):
        self.assertEqual(self.get().status_code, 200)

    def test_non_existant_model(self):
        response = self.client.get(self.edit_url('test', 'foo'))
        self.assertEqual(response.status_code, 404)

    def test_edit_invalid(self):
        response = self.post(post_data={'foo': 'bar'})
        self.assertContains(response, "The setting could not be saved due to errors.")
        self.assertContains(response, "This field is required.")

    def test_edit(self):
        response = self.post(post_data={'title': 'Edited site title',
                                        'email': '<EMAIL>'})
        self.assertEqual(response.status_code, 302)

        default_site = Site.objects.get(is_default_site=True)
        setting = TestSetting.objects.get(site=default_site)
        self.assertEqual(setting.title, 'Edited site title')
        self.assertEqual(setting.email, '<EMAIL>')


class TestMultiSite(BaseTestSettingView):
    def setUp(self):
        self.default_site = Site.objects.get(is_default_site=True)
        self.other_site = Site.objects.create(hostname='example.com', root_page=Page.objects.get(pk=2))
        self.login()

    def test_redirect_to_default(self):
        """
        Should redirect to the setting for the default site.
        """
        start_url = reverse('wagtailsettings:edit', args=[
            'tests', 'testsetting'])
        dest_url = 'http://testserver' + reverse('wagtailsettings:edit', args=[
            self.default_site.pk, 'tests', 'testsetting'])
        response = self.client.get(start_url, follow=True)
        self.assertEqual([(dest_url, 302)], response.redirect_chain)

    def test_redirect_to_current(self):
        """
        Should redirect to the setting for the current site taken from the URL,
        by default
        """
        start_url = reverse('wagtailsettings:edit', args=[
            'tests', 'testsetting'])
        dest_url = 'http://example.com' + reverse('wagtailsettings:edit', args=[
            self.other_site.pk, 'tests', 'testsetting'])
        response = self.client.get(start_url, follow=True, HTTP_HOST=self.other_site.hostname)
        self.assertEqual([(dest_url, 302)], response.redirect_chain)

    def test_with_no_current_site(self):
        """
        Redirection should not break if the current request does not correspond to a site
        """
        self.default_site.is_default_site = False
        self.default_site.save()

        start_url = reverse('wagtailsettings:edit', args=[
            'tests', 'testsetting'])
        response = self.client.get(start_url, follow=True, HTTP_HOST="noneoftheabove.example.com")
        self.assertEqual(302, response.redirect_chain[0][1])

    def test_switcher(self):
        """ Check that the switcher form exists in the page """
        response = self.get()
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'id="settings-site-switch"')

    def test_unknown_site(self):
        """ Check that unknown sites throw a 404 """
        response = self.get(site_pk=3)
        self.assertEqual(response.status_code, 404)<commit_msg>tests(contrib/settings): Use assertRedirects in tests<commit_after>from __future__ import absolute_import, unicode_literals

from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils.text import capfirst

from wagtail.contrib.settings.registry import SettingMenuItem
from wagtail.tests.testapp.models import IconSetting, TestSetting
from wagtail.tests.utils import WagtailTestUtils
from wagtail.wagtailcore import hooks
from wagtail.wagtailcore.models import Page, Site


class TestSettingMenu(TestCase, WagtailTestUtils):

    def login_only_admin(self):
        """ Log in with a user that only has permission to access the admin """
        user = get_user_model().objects.create_user(
            username='test', email='<EMAIL>', password='password')
        user.user_permissions.add(Permission.objects.get_by_natural_key(
            codename='access_admin', app_label='wagtailadmin', model='admin'))
        self.client.login(username='test', password='password')
        return user

    def test_menu_item_in_admin(self):
        self.login()
        response = self.client.get(reverse('wagtailadmin_home'))

        self.assertContains(response, capfirst(TestSetting._meta.verbose_name))
        self.assertContains(response, reverse('wagtailsettings:edit', args=('tests', 'testsetting')))

    def test_menu_item_no_permissions(self):
        self.login_only_admin()
        response = self.client.get(reverse('wagtailadmin_home'))

        self.assertNotContains(response, TestSetting._meta.verbose_name)
        self.assertNotContains(response, reverse('wagtailsettings:edit', args=('tests', 'testsetting')))

    def test_menu_item_icon(self):
        menu_item = SettingMenuItem(IconSetting, icon='tag', classnames='test-class')
        classnames = set(menu_item.classnames.split(' '))
        self.assertEqual(classnames, {'icon', 'icon-tag', 'test-class'})


class BaseTestSettingView(TestCase, WagtailTestUtils):
    def get(self, site_pk=1, params={}):
        url = self.edit_url('tests', 'testsetting', site_pk=site_pk)
        return self.client.get(url, params)

    def post(self, site_pk=1, post_data={}):
        url = self.edit_url('tests', 'testsetting', site_pk=site_pk)
        return self.client.post(url, post_data)

    def edit_url(self, app, model, site_pk=1):
        return reverse('wagtailsettings:edit', args=[site_pk, app, model])


class TestSettingCreateView(BaseTestSettingView):
    def setUp(self):
        self.login()

    def test_status_code(self):
        self.assertEqual(self.get().status_code, 200)

    def test_edit_invalid(self):
        response = self.post(post_data={'foo': 'bar'})
        self.assertContains(response, "The setting could not be saved due to errors.")
        self.assertContains(response, "This field is required.")

    def test_edit(self):
        response = self.post(post_data={'title': 'Edited site title',
                                        'email': '<EMAIL>'})
        self.assertEqual(response.status_code, 302)

        default_site = Site.objects.get(is_default_site=True)
        setting = TestSetting.objects.get(site=default_site)
        self.assertEqual(setting.title, 'Edited site title')
        self.assertEqual(setting.email, '<EMAIL>')


class TestSettingEditView(BaseTestSettingView):
    def setUp(self):
        default_site = Site.objects.get(is_default_site=True)

        self.test_setting = TestSetting()
        self.test_setting.title = 'Site title'
        self.test_setting.email = '<EMAIL>'
        self.test_setting.site = default_site
        self.test_setting.save()

        self.login()

    def test_status_code(self):
        self.assertEqual(self.get().status_code, 200)

    def test_non_existant_model(self):
        response = self.client.get(self.edit_url('test', 'foo'))
        self.assertEqual(response.status_code, 404)

    def test_edit_invalid(self):
        response = self.post(post_data={'foo': 'bar'})
        self.assertContains(response, "The setting could not be saved due to errors.")
        self.assertContains(response, "This field is required.")

    def test_edit(self):
        response = self.post(post_data={'title': 'Edited site title',
                                        'email': '<EMAIL>'})
        self.assertEqual(response.status_code, 302)

        default_site = Site.objects.get(is_default_site=True)
        setting = TestSetting.objects.get(site=default_site)
        self.assertEqual(setting.title, 'Edited site title')
        self.assertEqual(setting.email, '<EMAIL>')


class TestMultiSite(BaseTestSettingView):
    def setUp(self):
        self.default_site = Site.objects.get(is_default_site=True)
        self.other_site = Site.objects.create(hostname='example.com', root_page=Page.objects.get(pk=2))
        self.login()

    def test_redirect_to_default(self):
        """
        Should redirect to the setting for the default site.
        """
        start_url = reverse('wagtailsettings:edit', args=[
            'tests', 'testsetting'])
        dest_url = 'http://testserver' + reverse('wagtailsettings:edit', args=[
            self.default_site.pk, 'tests', 'testsetting'])
        response = self.client.get(start_url, follow=True)
        self.assertRedirects(response, dest_url, status_code=302, fetch_redirect_response=False)

    def test_redirect_to_current(self):
        """
        Should redirect to the setting for the current site taken from the URL,
        by default
        """
        start_url = reverse('wagtailsettings:edit', args=[
            'tests', 'testsetting'])
        dest_url = 'http://example.com' + reverse('wagtailsettings:edit', args=[
            self.other_site.pk, 'tests', 'testsetting'])
        response = self.client.get(start_url, follow=True, HTTP_HOST=self.other_site.hostname)
        self.assertRedirects(response, dest_url, status_code=302, fetch_redirect_response=False)

    def test_with_no_current_site(self):
        """
        Redirection should not break if the current request does not correspond to a site
        """
        self.default_site.is_default_site = False
        self.default_site.save()

        start_url = reverse('wagtailsettings:edit', args=[
            'tests', 'testsetting'])
        response = self.client.get(start_url, follow=True, HTTP_HOST="noneoftheabove.example.com")
        self.assertEqual(302, response.redirect_chain[0][1])

    def test_switcher(self):
        """ Check that the switcher form exists in the page """
        response = self.get()
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'id="settings-site-switch"')

    def test_unknown_site(self):
        """ Check that unknown sites throw a 404 """
        response = self.get(site_pk=3)
        self.assertEqual(response.status_code, 404)
{'fraction_non_alphanumeric': 0.11774076755973932, 'fraction_numerical': 0.005792903692976104, 'mean_word_length': 3.3900190718372536, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1681294', 'n_tokens_mistral': 4066, 'n_tokens_neox': 3950, 'n_words': 807}