-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
330 lines (308 loc) · 11.9 KB
/
tests.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
from pathlib import Path
from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase, override_settings
from django.urls import reverse
from users.models import User
from .forms import DrawingManualForm, DrawingUpdateForm
from .models import Drawing, Layer
pword = settings.DJANGO_SUPERUSER_PASSWORD
@override_settings(MEDIA_ROOT=Path(settings.MEDIA_ROOT).joinpath("tests"))
class GeoCADViewsTest(TestCase):
@classmethod
def setUpTestData(cls):
User.objects.create_superuser("boss", "[email protected]", pword)
img_path = Path(settings.BASE_DIR).joinpath(
"djeocadengine/static/djeocadengine/tests/nogeo.dxf"
)
with open(img_path, "rb") as f:
content = f.read()
Drawing.objects.create(
title="Drawing 1",
dxf=SimpleUploadedFile("nogeo.dxf", content, "image/x-dxf"),
geom={"type": "Point", "coordinates": [12.0, 42.0]},
epsg=32633,
)
Drawing.objects.create(
title="Drawing 2",
dxf=SimpleUploadedFile("nogeo.dxf", content, "image/x-dxf"),
geom={"type": "Point", "coordinates": [12.1, 42.1]},
epsg=32633,
)
f.close()
def tearDown(self):
"""Checks existing files, then removes them.
Not working for filer paths"""
try:
path = Path(settings.MEDIA_ROOT).joinpath("uploads/djeocad/dxf/")
list = [e for e in path.iterdir() if e.is_file()]
for file in list:
Path(file).unlink()
except FileNotFoundError:
pass
def test_unlogged_list_status_code(self):
response = self.client.get(reverse("djeocadengine:base_list"))
self.assertEqual(response.status_code, 200)
def test_unlogged_detail_status_code(self):
draw = Drawing.objects.first()
response = self.client.get(
reverse("djeocadengine:drawing_detail", kwargs={"pk": draw.id})
)
self.assertEqual(response.status_code, 200)
def test_unlogged_htmx_list_status_code(self):
response = self.client.get(
reverse("djeocadengine:base_list"),
headers={"Hx-Request": "true"},
)
self.assertEqual(response.status_code, 200)
def test_unlogged_htmx_detail_status_code(self):
draw = Drawing.objects.first()
response = self.client.get(
reverse("djeocadengine:drawing_detail", kwargs={"pk": draw.id}),
headers={"Hx-Request": "true"},
)
self.assertEqual(response.status_code, 200)
def test_unlogged_create_status_code(self):
response = self.client.get(reverse("djeocadengine:drawing_create"))
self.assertEqual(response.status_code, 404)
def test_unlogged_update_status_code(self):
draw = Drawing.objects.first()
response = self.client.get(
reverse("djeocadengine:drawing_update", kwargs={"pk": draw.id})
)
self.assertEqual(response.status_code, 404)
def test_unlogged_delete_status_code(self):
draw = Drawing.objects.first()
response = self.client.get(
reverse("djeocadengine:drawing_delete", kwargs={"pk": draw.id})
)
self.assertEqual(response.status_code, 302)
def test_unlogged_htmx_create_status_code(self):
response = self.client.get(
reverse("djeocadengine:drawing_create"), headers={"Hx-Request": "true"}
)
self.assertEqual(response.status_code, 302)
def test_unlogged_htmx_update_status_code(self):
draw = Drawing.objects.first()
response = self.client.get(
reverse("djeocadengine:drawing_update", kwargs={"pk": draw.id}),
headers={"Hx-Request": "true"},
)
self.assertEqual(response.status_code, 302)
def test_logged_delete_status_code(self):
self.client.login(username="boss", password=pword)
draw = Drawing.objects.first()
response = self.client.get(
reverse("djeocadengine:drawing_delete", kwargs={"pk": draw.id})
)
self.assertEqual(response.status_code, 404)
def test_logged_htmx_create_status_code(self):
self.client.login(username="boss", password=pword)
response = self.client.get(
reverse("djeocadengine:drawing_create"), headers={"Hx-Request": "true"}
)
self.assertEqual(response.status_code, 200)
def test_logged_htmx_update_status_code(self):
self.client.login(username="boss", password=pword)
draw = Drawing.objects.first()
response = self.client.get(
reverse("djeocadengine:drawing_update", kwargs={"pk": draw.id}),
headers={"Hx-Request": "true"},
)
self.assertEqual(response.status_code, 200)
def test_logged_htmx_delete_status_code(self):
self.client.login(username="boss", password=pword)
draw = Drawing.objects.first()
response = self.client.get(
reverse("djeocadengine:drawing_delete", kwargs={"pk": draw.id}),
headers={"Hx-Request": "true"},
)
self.assertEqual(response.status_code, 200)
def test_create_drawing_from_georeferenced(self):
dxf_path = Path(settings.BASE_DIR).joinpath(
"djeocadengine/static/djeocadengine/tests/yesgeo.dxf"
)
with open(dxf_path, "rb") as f:
dxf_content = f.read()
self.client.login(username="boss", password=pword)
# add this call to make next one work. why?
self.client.get(reverse("djeocadengine:drawing_create"))
response = self.client.post(
reverse("djeocadengine:drawing_create"),
{
"title": "Georeferenced",
"dxf": SimpleUploadedFile("yesgeo.dxf", dxf_content, "text/dxf"),
},
headers={"HX-Request": "true"},
follow=True,
)
draw = Drawing.objects.get(title="Georeferenced")
self.assertRedirects(
response,
reverse("djeocadengine:drawing_detail", kwargs={"pk": draw.id}),
status_code=302,
target_status_code=200,
)
response = self.client.get(
reverse("djeocadengine:drawing_download", kwargs={"pk": draw.id})
)
self.assertEqual(response.status_code, 200)
response = self.client.get(
reverse("djeocadengine:drawing_csv", kwargs={"pk": draw.id})
)
self.assertEqual(response.status_code, 200)
dxf_path = Path(settings.BASE_DIR).joinpath(
"djeocadengine/static/djeocadengine/tests/nogeo.dxf"
)
with open(dxf_path, "rb") as f:
dxf_content = f.read()
response = self.client.post(
reverse("djeocadengine:drawing_create"),
{
"title": "Not Georeferenced",
"dxf": SimpleUploadedFile("nogeo.dxf", dxf_content, "text/dxf"),
},
headers={"HX-Request": "true"},
follow=True,
)
draw2 = Drawing.objects.get(title="Not Georeferenced")
self.assertRedirects(
response,
reverse("djeocadengine:drawing_geodata", kwargs={"pk": draw2.id}),
status_code=302,
target_status_code=200,
)
response = self.client.post(
reverse("djeocadengine:drawing_geodata", kwargs={"pk": draw2.id}),
{
"parent": draw.id,
},
headers={"HX-Request": "true"},
follow=True,
)
self.assertRedirects(
response,
reverse("djeocadengine:drawing_detail", kwargs={"pk": draw2.id}),
status_code=302,
target_status_code=200,
)
def test_create_drawing_from_not_georeferenced(self):
dxf_path = Path(settings.BASE_DIR).joinpath(
"djeocadengine/static/djeocadengine/tests/nogeo.dxf"
)
img_path = Path(settings.BASE_DIR).joinpath(
"djeocadengine/static/djeocadengine/tests/image.jpg"
)
with open(dxf_path, "rb") as f:
dxf_content = f.read()
with open(img_path, "rb") as i:
img_content = i.read()
self.client.login(username="boss", password=pword)
response = self.client.post(
reverse("djeocadengine:drawing_create"),
{
"title": "Again not Georeferenced",
"dxf": SimpleUploadedFile("nogeo.dxf", dxf_content, "text/dxf"),
"temp_image": SimpleUploadedFile("image.jpg", img_content, "image/jpg"),
},
headers={"HX-Request": "true"},
follow=True,
)
draw = Drawing.objects.get(title="Again not Georeferenced")
self.assertRedirects(
response,
reverse("djeocadengine:drawing_geodata", kwargs={"pk": draw.id}),
status_code=302,
target_status_code=200,
)
response = self.client.get(
reverse("djeocadengine:drawing_manual", kwargs={"pk": draw.id}),
headers={"HX-Request": "true"},
)
self.assertEqual(response.status_code, 200)
response = self.client.post(
reverse("djeocadengine:drawing_manual", kwargs={"pk": draw.id}),
{
"lat": 42,
"long": 12,
"designx": 0,
"designy": 0,
"rotation": 0,
},
headers={"HX-Request": "true"},
follow=True,
)
self.assertRedirects(
response,
reverse("djeocadengine:drawing_detail", kwargs={"pk": draw.id}),
status_code=302,
target_status_code=200,
)
response = self.client.post(
reverse("djeocadengine:drawing_update", kwargs={"pk": draw.id}),
{
"lat": 42,
"long": 11,
"designx": 0,
"designy": 0,
"rotation": 0,
"title": "Again not Georeferenced",
"dxf": "uploads/djeocad/dxf/nogeo.dxf",
"temp_image": SimpleUploadedFile(
"image2.jpg", img_content, "image/jpg"
),
},
headers={"HX-Request": "true"},
follow=True,
)
self.assertRedirects(
response,
reverse("djeocadengine:drawing_detail", kwargs={"pk": draw.id}),
status_code=302,
target_status_code=200,
)
lay = Layer.objects.get(drawing=draw.id, name="one")
response = self.client.post(
reverse("djeocadengine:layer_update", kwargs={"pk": lay.id}),
{
"name": "one",
"color_field": "#f0f0f0",
"linetype": True,
},
headers={"HX-Request": "true"},
follow=True,
)
self.assertRedirects(
response,
reverse("djeocadengine:layer_detail", kwargs={"pk": lay.id}),
status_code=302,
target_status_code=200,
)
response = self.client.get(
reverse("djeocadengine:layer_delete", kwargs={"pk": lay.id}),
headers={"HX-Request": "true"},
)
self.assertEqual(response.status_code, 200)
def test_validation_error(self):
form = DrawingManualForm(
data={
"lat": 142,
"long": 212,
"designx": 0,
"designy": 0,
"rotation": 0,
}
)
self.assertFalse(form.is_valid())
form = DrawingUpdateForm(
data={
"lat": 142,
"long": 211,
"designx": 0,
"designy": 0,
"rotation": 0,
"title": "Again not Georeferenced",
"dxf": "uploads/djeocad/dxf/nogeo.dxf",
}
)
self.assertFalse(form.is_valid())