<commit_before>import unittest
from django.test import TestCase
from django.test.client import Client
from django.core.management import call_command
from django.contrib.auth.models import User

def decode_cookie(cookie):
    from cookiesessions.middleware import SessionStore
    session = SessionStore(cookie)
    return session.load()
    

class TestCookieSessions(TestCase):
    """Unit tests for cookiesessions"""
    
    def setUp(self):
        from django.contrib.auth.models import User
        user = User.objects.create_user('testuser', '<EMAIL>', 'secret')
        user.is_staff = True
        user.save()
    
    def testLogin(self):
        c = Client()
        # The first request gets the "testcookie"
        r = c.get('/login/')
        r = c.post('/login/', {'username': 'testuser', 'password': '<PASSWORD>'})
        session = decode_cookie(r.cookies.get('sessionid').value)
        
        # Should redirect you somewhere if successful
        self.assertEquals(r.status_code, 302)
        self.assertTrue(session.has_key('_auth_user_id'))
        self.assertTrue(session.has_key('_auth_user_backend'))
        user = User.objects.get(pk=session['_auth_user_id'])
        self.assertTrue(user.username, 'testuser')
        <commit_msg>more tests<commit_after>import sys
from StringIO import StringIO
from django.test import TestCase
from django.test.client import Client
from django.core.management import call_command
from django.conf import settings
from django.contrib.auth.models import User

from cookiesession.middleware import SessionStore
from cookiesession.management.commands.decode_session_cookie import Command as DecodeCommand
from cookiesession.management.commands.encode_session_cookie import Command as EncodeCommand

def decode_cookie(cookie):
    return SessionStore(cookie).load()
    

class TestCookieSessions(TestCase):
    """Unit tests for cookiesessions"""
    
    def setUp(self):
        from django.contrib.auth.models import User
        user = User.objects.create_user('testuser', '<EMAIL>', 'secret')
        user.is_staff = True
        user.save()
    
    def testLogin(self):
        c = Client()
        # The first request gets the "testcookie"
        r = c.get('/login/')
        r = c.post('/login/', {'username': 'testuser', 'password': '<PASSWORD>'})
        session = decode_cookie(r.cookies.get('sessionid').value)
        
        # Should redirect you somewhere if successful
        self.assertEquals(r.status_code, 302)
        self.assertTrue(session.has_key('_auth_user_id'))
        self.assertTrue(session.has_key('_auth_user_backend'))
        user = User.objects.get(pk=session['_auth_user_id'])
        self.assertTrue(user.username, 'testuser')
        
    def test_encode_decode(self):
        testdata = {'foo': 'bar'}
        self.assertEqual(decode_cookie(SessionStore('').encode(testdata)), testdata)
        
        
    def test_management(self):
        out = sys.stdout
        sys.stdout = StringIO()
        
        EncodeCommand().handle('foo=bar')
        self.assertEqual(sys.stdout.getvalue(), 'eJyrVkrLz1eyUlBKSixSqjUzTDM3TzFNNE1LSbM0NEg1TrGwNDczNjdMTE01SU0ytUwzMk40sUgBALJ3D40=\n')

        DecodeCommand().handle(sys.stdout.getvalue())
        self.assertEqual(sys.stdout.getvalue(), "eJyrVkrLz1eyUlBKSixSqjUzTDM3TzFNNE1LSbM0NEg1TrGwNDczNjdMTE01SU0ytUwzMk40sUgBALJ3D40=\n{'foo': 'bar'}\n")

        sys.stdout = out
{'fraction_non_alphanumeric': 0.1117612638970158, 'fraction_numerical': 0.009362200117027502, 'mean_word_length': 3.3721227621483374, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4402476', 'n_tokens_mistral': 1038, 'n_tokens_neox': 1026, 'n_words': 203}