Browse Source

add tests

subDesTagesMitExtraKaese 2 năm trước cách đây
mục cha
commit
81a903e4e9
2 tập tin đã thay đổi với 27 bổ sung18 xóa
  1. 12 2
      tests.py
  2. 15 16
      views.py

+ 12 - 2
tests.py

@@ -1,3 +1,13 @@
-from django.test import TestCase
+from django.test import TestCase, Client
+from django.urls import reverse
 
-# Create your tests here.
+class LoggerTest(TestCase):
+  def test_create_should_not_be_public(self):
+    client = Client(HTTP_HOST="127.0.0.1:8001")
+    response = client.post("/arduino/markers/create", {'abc': []}, content_type="application/json")
+    self.assertEqual(response.status_code, 302)
+
+  def test_censor_should_not_be_public(self):
+    client = Client(HTTP_HOST="127.0.0.1:8001")
+    response = client.get("/arduino/markers/censor")
+    self.assertEqual(response.status_code, 302)

+ 15 - 16
views.py

@@ -18,7 +18,7 @@ def index(request):
 def upload(request):
   return render(request, 'gps_logger/upload.html')
 
-class MarkerCreateView(View, PermissionRequiredMixin):
+class MarkerCreateView(PermissionRequiredMixin, View):
   permission_required = ("gps-logger.markers.change")
 
   def post(self, request):
@@ -54,12 +54,7 @@ class MarkerCreateView(View, PermissionRequiredMixin):
         oldMarkers.delete()
         Marker.objects.bulk_create(objects)
     
-    locations = CensoredLocation.objects.all()
-    for location in locations:
-      censor_count += location.delete_markers()
-
-    if(cache.get('markers')):
-      cache.delete('markers')
+    censor_count = censor()
 
     return JsonResponse({
         'censored': censor_count,
@@ -82,13 +77,17 @@ def marker_view(request):
     cache.set('markers', data, 3600*24)
   return HttpResponse(data, content_type='application/json')
 
-class DeleteCensoredView(View, PermissionRequiredMixin):
-  permission_required = ("gps-logger.markers.change")
+class DeleteCensoredView(PermissionRequiredMixin, View):
+  permission_required = ("gps-logger.markers.change",)
   def get(self, request):
-    counter = 0
-    locations = CensoredLocation.objects.all()
-    for location in locations:
-      counter += location.delete_markers()
-    if(cache.get('markers')):
-      cache.delete('markers')
-    return JsonResponse({'censored': counter})
+    counter = censor()
+    return JsonResponse({'censored': counter})
+
+def censor() -> int:
+  counter = 0
+  locations = CensoredLocation.objects.all()
+  for location in locations:
+    counter += location.delete_markers()
+  if(cache.get('markers')):
+    cache.delete('markers')
+  return counter