This repository has been archived by the owner on Feb 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.py
143 lines (107 loc) · 3.51 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import uuid
from django.conf import settings
minimal = {
'DATABASES': {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase'
}
},
'CACHES': {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
}
},
'INSTALLED_APPS': ['django.contrib.sessions'],
'DJANGO_PARANOIA_REPORTERS': ['django_paranoia.reporters.log'],
}
if not settings.configured:
settings.configure(**minimal)
from django import forms
from django.db import models
from django.test import TestCase
from django.test.client import RequestFactory
from django.utils.importlib import import_module
import mock
from nose.tools import eq_
from django_paranoia.configure import config
from django_paranoia.forms import ParanoidForm, ParanoidModelForm
from django_paranoia.sessions import SessionStore
from django_paranoia.signals import warning
class SimpleForm(ParanoidForm):
yes = forms.BooleanField()
class SimpleModel(models.Model):
yes = models.BooleanField()
class SimpleModelForm(ParanoidModelForm):
class Meta:
model = SimpleModel
class TestForms(TestCase):
def result(self, *args, **kwargs):
self.called.append((args, kwargs))
def setUp(self):
self.called = []
self.connect = warning.connect(self.result)
def test_fine(self):
SimpleForm()
SimpleForm({'yes': True})
assert not self.called
def test_extra(self):
SimpleForm({'no': 'wat'})
assert self.called
def test_multiple(self):
SimpleForm({'no': 'wat', 'yes': True, 'sql': 'aargh'})
res = self.called[0][1]
eq_(res['values'], ['sql', 'no'])
def test_model_fine(self):
SimpleModelForm()
SimpleModelForm({'yes': True})
assert not self.called
def test_model_extra(self):
SimpleModelForm({'no': 'wat'})
assert self.called
@mock.patch('django_paranoia.configure.warning')
class TestSetup(TestCase):
log = 'django_paranoia.reporters.log'
def test_setup_fails(self, warning):
config([self.log, 'foo'])
eq_(len(warning.connect.call_args_list), 1)
eq_(warning.connect.call_args[1]['dispatch_uid'],
'django-paranoia-%s' % self.log)
class TestLog(TestCase):
# Not sure what to test here.
pass
class TestSession(TestCase):
def setUp(self):
self.session = None
self.uid = 'some:uid'
self.called = []
self.connect = warning.connect(self.result)
def result(self, *args, **kwargs):
self.called.append((args, kwargs))
def request(self, **kwargs):
req = RequestFactory().get('/')
req.META.update(**kwargs)
return req
def get(self, request=None):
session = SessionStore(request=request or self.request(),
session_key=self.uid)
self.uid = session._session_key
return session
def save(self):
self.session.save()
def test_basic(self):
self.session = self.get()
self.session['foo'] = 'bar'
self.save()
eq_(self.get().load()['foo'], 'bar')
def test_request(self):
self.get().save()
res = self.get()
eq_(res.request_data(), {'meta:REMOTE_ADDR': '127.0.0.1'})
assert not self.called
def test_request_changed(self):
ses = self.get()
ses.save()
req = self.request(REMOTE_ADDR='192.168.1.1')
ses.check_request_data(request=req)
assert self.called