<commit_before>'''
Created on Dec 10, 2010

@author: <NAME> <<EMAIL>>
'''


from __future__ import with_statement
from cms.models.pagemodel import Page
from cms.models.titlemodels import Title
from cms.tests import base
from cms.tests.base import CMSTestCase
from django.conf import settings
from django.contrib.auth.models import User, Permission
from django.contrib.contenttypes.models import ContentType

class AdminTestCase(CMSTestCase):
    
    def _get_normal_guy(self):
        normal_guy = User.objects.create_user('test', '<EMAIL>', 'test')
        normal_guy.is_active = True
        normal_guy.is_staff = True
        normal_guy.save()
        
        normal_guy.user_permissions = Permission.objects.filter(
                                            codename__in=['change_page',
                                                          'add_page',
                                                          'delete_page',
                                                          'add_title',
                                                          'change_title',
                                                          'delete_title',
                                                          ])
        self.assertTrue(normal_guy.has_perm('cms.change_page'))
        return normal_guy
    
    def _get_admin(self):
        admin = User(username="admin", is_staff = True, is_active = True, is_superuser = True)
        admin.set_password("<PASSWORD>")
        admin.save()
        return admin
    
    def test_01_edit_does_not_reset_page_adv_fields(self):
        
        NEW_PAGE_NAME = 'Test page 2'
        REVERSE_ID = 'Test'
        
        admin = self._get_admin()
        normal_guy = self._get_normal_guy()
        
        # The admin creates the page
        page = self.create_page(None, admin, 1, 'Test Page')
        page.reverse_id = REVERSE_ID
        page.save()
        
        # The user edits the page (change the page name for ex.)
        page_data = {
            'title':NEW_PAGE_NAME, 
            'slug':page.get_slug(), 
            'language':settings.LANGUAGES[0][0],
            'site':page.site.pk, 
            'template':page.template
        }
        # required only if user haves can_change_permission
        page_data['pagepermission_set-TOTAL_FORMS'] = 0
        page_data['pagepermission_set-INITIAL_FORMS'] = 0
        page_data['pagepermission_set-MAX_NUM_FORMS'] = 0
        
        self.login_user(normal_guy)
        resp = self.client.post(base.URL_CMS_PAGE_CHANGE % page.pk, page_data, 
                                follow=True)
        self.assertEqual(resp.status_code, 200)
        self.assertTemplateNotUsed(resp, 'admin/login.html')
        page = Page.objects.get(pk=page.pk)
        
        self.assertEqual(page.get_title(), NEW_PAGE_NAME)
        self.assertEqual(page.reverse_id, REVERSE_ID)
        
        
        <commit_msg>Issue 516 seems to be legit<commit_after>'''
Created on Dec 10, 2010

@author: <NAME> <<EMAIL>>
'''


from __future__ import with_statement
from cms.models.pagemodel import Page
from cms.tests import base
from cms.tests.base import CMSTestCase
from cms.tests.util.settings_contextmanager import SettingsOverride
from django.conf import settings
from django.contrib.auth.models import User

class AdminTestCase(CMSTestCase):
    
    def _get_guys(self):
        admin = User(username="admin", is_staff = True, is_active = True, is_superuser = True)
        admin.set_password("<PASSWORD>")
        admin.save()
        self.login_user(admin)
        USERNAME = 'test'
        data = {
            'username': USERNAME, 
            'password1': <PASSWORD>,
            'password2': <PASSWORD>, 
            'can_add_page': True, 
            'can_change_page': True, 
            'can_delete_page': True, 
            'can_recover_page': True, 
            'can_add_pageuser': False, 
            'can_change_pageuser': False, 
            'can_delete_pageuser': False, 
            'can_add_pagepermission': False, 
            'can_change_pagepermission': False, 
            'can_delete_pagepermission': False,
        }
        response = self.client.post('/admin/cms/pageuser/add/', data)
        self.assertRedirects(response, '/admin/cms/pageuser/')
        
        normal_guy = User.objects.get(username=USERNAME)
        normal_guy.is_staff = True
        normal_guy.is_active = True
        normal_guy.save()
        return admin, normal_guy
    
    def test_01_edit_does_not_reset_page_adv_fields(self):
        
        NEW_PAGE_NAME = 'Test page 2'
        REVERSE_ID = 'Test'
        
        admin, normal_guy = self._get_guys()
        
        # The admin creates the page
        page = self.create_page(None, admin, 1, 'Test Page')
        page.reverse_id = REVERSE_ID
        page.save()
        
        # The user edits the page (change the page name for ex.)
        page_data = {
            'title':NEW_PAGE_NAME, 
            'slug':page.get_slug(), 
            'language':settings.LANGUAGES[0][0],
            'site':page.site.pk, 
            'template':page.template
        }
        # required only if user haves can_change_permission
        page_data['pagepermission_set-TOTAL_FORMS'] = 0
        page_data['pagepermission_set-INITIAL_FORMS'] = 0
        page_data['pagepermission_set-MAX_NUM_FORMS'] = 0
        
        self.login_user(normal_guy)
        with SettingsOverride(CMS_PERMISSION=False):
            resp = self.client.post(base.URL_CMS_PAGE_CHANGE % page.pk, page_data, 
                                    follow=True)
        self.assertEqual(resp.status_code, 200)
        self.assertTemplateNotUsed(resp, 'admin/login.html')
        page = Page.objects.get(pk=page.pk)
        
        self.assertEqual(page.get_title(), NEW_PAGE_NAME)
        self.assertEqual(page.reverse_id, REVERSE_ID)
{'fraction_non_alphanumeric': 0.09394714407502132, 'fraction_numerical': 0.006990622335890878, 'mean_word_length': 1.9068384539147671, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '464143', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1657, 'n_words': 324}