# Path: formwizard/storage/cookie.py
# class CookieStorage(storage.BaseStorage):
#     encoder = json.JSONEncoder(separators=(',', ':'))
# 
#     def __init__(self, *args, **kwargs):
#         super(CookieStorage, self).__init__(*args, **kwargs)
#         self.data = self.load_data()
#         if self.data is None:
#             self.init_data()
# 
#     def unsign_cookie_data(self, data):
#         if data is None:
#             return None
# 
#         bits = data.split('$', 1)
#         if len(bits) == 2:
#             if bits[0] == self.get_cookie_hash(bits[1]):
#                 return bits[1]
# 
#         raise SuspiciousOperation('FormWizard cookie manipulated')
# 
#     def load_data(self):
#         data = self.request.COOKIES.get(self.prefix, None)
#         cookie_data = self.unsign_cookie_data(data)
#         if cookie_data is None:
#             return None
#         return json.loads(cookie_data, cls=json.JSONDecoder)
# 
#     def update_response(self, response):
#         if self.data:
#             response.set_cookie(self.prefix, self.create_cookie_data(self.data))
#         else:
#             response.delete_cookie(self.prefix)
#         return response
# 
#     def create_cookie_data(self, data):
#         encoded_data = self.encoder.encode(self.data)
#         cookie_data = '%s$%s' % (self.get_cookie_hash(encoded_data),
#             encoded_data)
#         return cookie_data
# 
#     def get_cookie_hash(self, data):
#         return hmac.new('%s$%s' % (settings.SECRET_KEY, self.prefix),
#             data, sha_hmac).hexdigest()
# 
# Path: formwizard/tests/storagetests.py
# def get_request():
#     request = HttpRequest()
#     engine = import_module(settings.SESSION_ENGINE)
#     request.session = engine.SessionStore(None)
#     return request
# 
# class TestStorage(object):
#     def setUp(self):
#         self.testuser, created = User.objects.get_or_create(username='testuser1')
# 
#     def test_current_step(self):
#         request = get_request()
#         storage = self.get_storage()('wizard1', request, None)
#         my_step = 2
# 
#         self.assertEqual(storage.current_step, None)
# 
#         storage.current_step = my_step
#         self.assertEqual(storage.current_step, my_step)
# 
#         storage.reset()
#         self.assertEqual(storage.current_step, None)
# 
#         storage.current_step = my_step
#         storage2 = self.get_storage()('wizard2', request, None)
#         self.assertEqual(storage2.current_step, None)
# 
#     def test_step_data(self):
#         request = get_request()
#         storage = self.get_storage()('wizard1', request, None)
#         step1 = 'start'
#         step_data1 = {'field1': 'data1',
#                       'field2': 'data2',
#                       'field3': datetime.now(),
#                       'field4': self.testuser}
# 
#         self.assertEqual(storage.get_step_data(step1), None)
# 
#         storage.set_step_data(step1, step_data1)
#         self.assertEqual(storage.get_step_data(step1), step_data1)
# 
#         storage.reset()
#         self.assertEqual(storage.get_step_data(step1), None)
# 
#         storage.set_step_data(step1, step_data1)
#         storage2 = self.get_storage()('wizard2', request, None)
#         self.assertEqual(storage2.get_step_data(step1), None)
# 
#     def test_extra_context(self):
#         request = get_request()
#         storage = self.get_storage()('wizard1', request, None)
#         extra_context = {'key1': 'data1',
#                          'key2': 'data2',
#                          'key3': datetime.now(),
#                          'key4': self.testuser}
# 
#         self.assertEqual(storage.extra_data, {})
# 
#         storage.extra_data = extra_context
#         self.assertEqual(storage.extra_data, extra_context)
# 
#         storage.reset()
#         self.assertEqual(storage.extra_data, {})
# 
#         storage.extra_data = extra_context
#         storage2 = self.get_storage()('wizard2', request, None)
#         self.assertEqual(storage2.extra_data, {})


from django.test import TestCase
from django.core.exceptions import SuspiciousOperation
from django.http import HttpResponse
from formwizard.storage.cookie import CookieStorage
from formwizard.tests.storagetests import get_request, TestStorage



class TestCookieStorage(TestStorage, TestCase):
    def get_storage(self):
        return CookieStorage

    def test_manipulated_cookie(self):
        request = get_request()
        storage = self.get_storage()('wizard1', request, None)

        storage.data = {'key1': 'value1'}
        response = HttpResponse()
        storage.update_response(response)

        signed_cookie_data = storage.create_cookie_data(storage.data)
        self.assertEqual(response.cookies[storage.prefix].value,
            signed_cookie_data)

        storage.request.COOKIES[storage.prefix] = signed_cookie_data

        self.assertEqual(storage.load_data(), {'key1': 'value1'})

        storage.request.COOKIES[storage.prefix] = 'i_am_manipulated'
        self.assertRaises(SuspiciousOperation, storage.load_data)

    def test_reset_cookie(self):

# Path: formwizard/storage/cookie.py
# class CookieStorage(storage.BaseStorage):
#     encoder = json.JSONEncoder(separators=(',', ':'))
# 
#     def __init__(self, *args, **kwargs):
#         super(CookieStorage, self).__init__(*args, **kwargs)
#         self.data = self.load_data()
#         if self.data is None:
#             self.init_data()
# 
#     def unsign_cookie_data(self, data):
#         if data is None:
#             return None
# 
#         bits = data.split('$', 1)
#         if len(bits) == 2:
#             if bits[0] == self.get_cookie_hash(bits[1]):
#                 return bits[1]
# 
#         raise SuspiciousOperation('FormWizard cookie manipulated')
# 
#     def load_data(self):
#         data = self.request.COOKIES.get(self.prefix, None)
#         cookie_data = self.unsign_cookie_data(data)
#         if cookie_data is None:
#             return None
#         return json.loads(cookie_data, cls=json.JSONDecoder)
# 
#     def update_response(self, response):
#         if self.data:
#             response.set_cookie(self.prefix, self.create_cookie_data(self.data))
#         else:
#             response.delete_cookie(self.prefix)
#         return response
# 
#     def create_cookie_data(self, data):
#         encoded_data = self.encoder.encode(self.data)
#         cookie_data = '%s$%s' % (self.get_cookie_hash(encoded_data),
#             encoded_data)
#         return cookie_data
# 
#     def get_cookie_hash(self, data):
#         return hmac.new('%s$%s' % (settings.SECRET_KEY, self.prefix),
#             data, sha_hmac).hexdigest()
# 
# Path: formwizard/tests/storagetests.py
# def get_request():
#     request = HttpRequest()
#     engine = import_module(settings.SESSION_ENGINE)
#     request.session = engine.SessionStore(None)
#     return request
# 
# class TestStorage(object):
#     def setUp(self):
#         self.testuser, created = User.objects.get_or_create(username='testuser1')
# 
#     def test_current_step(self):
#         request = get_request()
#         storage = self.get_storage()('wizard1', request, None)
#         my_step = 2
# 
#         self.assertEqual(storage.current_step, None)
# 
#         storage.current_step = my_step
#         self.assertEqual(storage.current_step, my_step)
# 
#         storage.reset()
#         self.assertEqual(storage.current_step, None)
# 
#         storage.current_step = my_step
#         storage2 = self.get_storage()('wizard2', request, None)
#         self.assertEqual(storage2.current_step, None)
# 
#     def test_step_data(self):
#         request = get_request()
#         storage = self.get_storage()('wizard1', request, None)
#         step1 = 'start'
#         step_data1 = {'field1': 'data1',
#                       'field2': 'data2',
#                       'field3': datetime.now(),
#                       'field4': self.testuser}
# 
#         self.assertEqual(storage.get_step_data(step1), None)
# 
#         storage.set_step_data(step1, step_data1)
#         self.assertEqual(storage.get_step_data(step1), step_data1)
# 
#         storage.reset()
#         self.assertEqual(storage.get_step_data(step1), None)
# 
#         storage.set_step_data(step1, step_data1)
#         storage2 = self.get_storage()('wizard2', request, None)
#         self.assertEqual(storage2.get_step_data(step1), None)
# 
#     def test_extra_context(self):
#         request = get_request()
#         storage = self.get_storage()('wizard1', request, None)
#         extra_context = {'key1': 'data1',
#                          'key2': 'data2',
#                          'key3': datetime.now(),
#                          'key4': self.testuser}
# 
#         self.assertEqual(storage.extra_data, {})
# 
#         storage.extra_data = extra_context
#         self.assertEqual(storage.extra_data, extra_context)
# 
#         storage.reset()
#         self.assertEqual(storage.extra_data, {})
# 
#         storage.extra_data = extra_context
#         storage2 = self.get_storage()('wizard2', request, None)
#         self.assertEqual(storage2.extra_data, {})


# Path: formwizard/tests/cookiestoragetests.py
from django.test import TestCase
from django.core.exceptions import SuspiciousOperation
from django.http import HttpResponse
from formwizard.storage.cookie import CookieStorage
from formwizard.tests.storagetests import get_request, TestStorage




class TestCookieStorage(TestStorage, TestCase):
    def get_storage(self):
        return CookieStorage

    def test_manipulated_cookie(self):
        request = get_request()
        storage = self.get_storage()('wizard1', request, None)

        storage.data = {'key1': 'value1'}
        response = HttpResponse()
        storage.update_response(response)

        signed_cookie_data = storage.create_cookie_data(storage.data)
        self.assertEqual(response.cookies[storage.prefix].value,
            signed_cookie_data)

        storage.request.COOKIES[storage.prefix] = signed_cookie_data

        self.assertEqual(storage.load_data(), {'key1': 'value1'})

        storage.request.COOKIES[storage.prefix] = 'i_am_manipulated'
        self.assertRaises(SuspiciousOperation, storage.load_data)

    def test_reset_cookie(self):

        request = get_request()