|
1
|
|
|
from django.test import TestCase, Client |
|
2
|
|
|
from django.contrib.auth.models import User |
|
3
|
|
|
from django.template import Context, Template, TemplateSyntaxError |
|
4
|
|
|
from django.core.urlresolvers import reverse |
|
5
|
|
|
from django.test.utils import override_settings |
|
6
|
|
|
|
|
7
|
|
|
from crudbuilder.tests.models import TestModel |
|
8
|
|
|
from crudbuilder.tests.crud import TestModelCrud, TestChildInlineFormset |
|
9
|
|
|
from crudbuilder.tests.forms import TestModelForm |
|
10
|
|
|
from crudbuilder.tests.tables import TestModelTable |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class ViewTestCase(TestCase): |
|
14
|
|
|
def setUp(self): |
|
15
|
|
|
self.client = Client() |
|
16
|
|
|
TestModelCrud.custom_modelform = TestModelForm |
|
17
|
|
|
TestModelCrud.custom_table2 = TestModelTable |
|
18
|
|
|
|
|
19
|
|
|
self.user = User.objects.create_user( |
|
20
|
|
|
username='asdf', |
|
21
|
|
|
password='asdf3452', |
|
22
|
|
|
email='[email protected]') |
|
23
|
|
|
|
|
24
|
|
|
def client_login(self): |
|
25
|
|
|
self.client.login( |
|
26
|
|
|
username=self.user.username, |
|
27
|
|
|
password='asdf3452' |
|
28
|
|
|
) |
|
29
|
|
|
|
|
30
|
|
|
def get_list_view(self): |
|
31
|
|
|
self.client_login() |
|
32
|
|
|
response = self.client.get(reverse('tests-testmodel-list')) |
|
33
|
|
|
self.assertEqual(200, response.status_code) |
|
34
|
|
|
|
|
35
|
|
|
def test_user_not_logged_in(self): |
|
36
|
|
|
response = self.client.get(reverse('tests-testmodel-list')) |
|
37
|
|
|
self.assertEqual(302, response.status_code) |
|
38
|
|
|
|
|
39
|
|
|
def tearDown(self): |
|
40
|
|
|
TestModelCrud.custom_table2 = None |
|
41
|
|
|
TestModelCrud.custom_modelform = None |
|
42
|
|
|
TestModelCrud.createupdate_forms = None |
|
43
|
|
|
|
|
44
|
|
|
def test_user_logged_in(self): |
|
45
|
|
|
self.get_list_view() |
|
46
|
|
|
|
|
47
|
|
|
def test_view_with_default_form(self): |
|
48
|
|
|
TestModelCrud.custom_modelform = None |
|
49
|
|
|
self.get_list_view() |
|
50
|
|
|
|
|
51
|
|
|
def test_view_with_default_tables2(self): |
|
52
|
|
|
TestModelCrud.custom_table2 = None |
|
53
|
|
|
self.get_list_view() |
|
54
|
|
|
|
|
55
|
|
|
def test_invalid_entry_create(self): |
|
56
|
|
|
self.client_login() |
|
57
|
|
|
data = {'name': 'Test text', 'email': 'same.org'} |
|
58
|
|
|
response = self.client.post('/crud/tests/testmodels/create/', data) |
|
59
|
|
|
self.assertEqual(response.status_code, 200) |
|
60
|
|
|
self.assertFormError( |
|
61
|
|
|
response, "form", "email", "Enter a valid email address.") |
|
62
|
|
|
|
|
63
|
|
|
def test_valid_entry_create(self): |
|
64
|
|
|
self.client_login() |
|
65
|
|
|
data = {'name': 'Test text', 'email': '[email protected]'} |
|
66
|
|
|
self.assertEqual(TestModel.objects.count(), 0) |
|
67
|
|
|
|
|
68
|
|
|
response = self.client.post('/crud/tests/testmodels/create/', data) |
|
69
|
|
|
self.assertEqual(response.status_code, 302) |
|
70
|
|
|
self.assertEqual(TestModel.objects.count(), 1) |
|
71
|
|
|
|
|
72
|
|
|
response = self.client.post('/crud/tests/testmodels/1/update/', data) |
|
73
|
|
|
self.assertEqual(response.status_code, 302) |
|
74
|
|
|
|
|
75
|
|
|
@override_settings(PERMISSION_REQUIRED_FOR_CRUD=True) |
|
76
|
|
|
def test_permission_required_enabled(self): |
|
77
|
|
|
self.client_login() |
|
78
|
|
|
response = self.client.get('/crud/tests/testmodels/') |
|
79
|
|
|
self.assertEqual(302, response.status_code) |
|
80
|
|
|
|
|
81
|
|
|
def test_search_list(self): |
|
82
|
|
|
self.client_login() |
|
83
|
|
|
response = self.client.get('/crud/tests/testmodels/?search=some_text') |
|
84
|
|
|
self.assertEqual(200, response.status_code) |
|
85
|
|
|
|
|
86
|
|
|
@override_settings(LOGIN_REQUIRED_FOR_CRUD=False) |
|
87
|
|
|
def test_no_login(self): |
|
88
|
|
|
response = self.client.get(reverse('tests-testmodel-list')) |
|
89
|
|
|
self.assertEqual(200, response.status_code) |
|
90
|
|
|
|
|
91
|
|
|
def test_separate_createupdateform(self): |
|
92
|
|
|
TestModelCrud.custom_modelform = None |
|
93
|
|
|
TestModelCrud.createupdate_forms = dict( |
|
94
|
|
|
create=TestModelForm, |
|
95
|
|
|
update=TestModelForm) |
|
96
|
|
|
|
|
97
|
|
|
self.client_login() |
|
98
|
|
|
data = {'name': 'Test text', 'email': '[email protected]'} |
|
99
|
|
|
self.assertEqual(TestModel.objects.count(), 0) |
|
100
|
|
|
|
|
101
|
|
|
response = self.client.post('/crud/tests/testmodels/create/', data) |
|
102
|
|
|
self.assertEqual(response.status_code, 302) |
|
103
|
|
|
self.assertEqual(TestModel.objects.count(), 1) |
|
104
|
|
|
|
|
105
|
|
|
response = self.client.post('/crud/tests/testmodels/1/update/', data) |
|
106
|
|
|
self.assertEqual(response.status_code, 302) |
|
107
|
|
|
|
|
108
|
|
|
def test_custom_queryset(self): |
|
109
|
|
|
def custom_queryset(self, request, **kwargs): |
|
110
|
|
|
return self.model.objects.all() |
|
111
|
|
|
setattr(TestModelCrud, 'custom_queryset', classmethod(custom_queryset)) |
|
112
|
|
|
self.get_list_view() |
|
113
|
|
|
|
|
114
|
|
|
def test_custom_context(self): |
|
115
|
|
|
def custom_context(self, request, context, **kwargs): |
|
116
|
|
|
context['testcontext'] = 'foo' |
|
117
|
|
|
return context |
|
118
|
|
|
setattr(TestModelCrud, 'custom_context', classmethod(custom_context)) |
|
119
|
|
|
self.get_list_view() |
|
120
|
|
|
|
|
121
|
|
|
def test_iniline_formset(self): |
|
122
|
|
|
TestModelCrud.inlineformset = TestChildInlineFormset |
|
123
|
|
|
self.get_list_view() |
|
124
|
|
|
|
|
125
|
|
|
def test_undertospace_filter(self): |
|
126
|
|
|
model = 'test_model' |
|
127
|
|
|
t = Template('{% load tags %}{{model|undertospaced}}') |
|
128
|
|
|
c = Context({"model": model}) |
|
129
|
|
|
result = t.render(c) |
|
130
|
|
|
self.assertEqual(result, 'Test Model') |
|
131
|
|
|
|
|
132
|
|
|
def test_model_fields_filter(self): |
|
133
|
|
|
obj = TestModel.objects.create(name='object1') |
|
134
|
|
|
t = Template( |
|
135
|
|
|
"{% load tags %}" |
|
136
|
|
|
"{% for field in obj|get_model_fields %}" |
|
137
|
|
|
"{{ field.name}}," |
|
138
|
|
|
"{% endfor %}" |
|
139
|
|
|
) |
|
140
|
|
|
c = Context({"obj": obj}) |
|
141
|
|
|
result = t.render(c) |
|
142
|
|
|
self.assertEqual(result, 'id,name,email,created_at,') |
|
143
|
|
|
|
|
144
|
|
|
def test_get_value_filter(self): |
|
145
|
|
|
obj = TestModel.objects.create(name='object1') |
|
146
|
|
|
t = Template( |
|
147
|
|
|
"{% load tags %}" |
|
148
|
|
|
"{{obj|get_value:'name'}}" |
|
149
|
|
|
) |
|
150
|
|
|
c = Context({"obj": obj}) |
|
151
|
|
|
result = t.render(c) |
|
152
|
|
|
self.assertEqual(result, 'object1') |
|
153
|
|
|
|