Skip to content

Commit

Permalink
Add callsign app view tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elnappo committed Mar 21, 2019
1 parent 55ff09a commit f30bb06
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
92 changes: 92 additions & 0 deletions project_novis/callsign/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.urls import reverse

from ..models import CallSign


class ViewTest(TestCase):
def setUp(self):
self.email = "test@project-novis.org"
self.password = "top_secret"

self.user = get_user_model().objects.create_user(email=self.email, password=self.password)
self.client = Client()

def create_callsigns(self, callsigns):
for callsign in callsigns:
CallSign.objects.create_callsign(callsign, self.user.id)

def test_callsign_workflow(self):
callsign = "DF0HSA"

with self.subTest('GET callsign detail'):
response = self.client.get(reverse('callsign:callsign-html-detail', kwargs={"slug": callsign}))
self.assertEqual(response.status_code, 404)

with self.subTest('GET callsign create'):
response = self.client.get(reverse('callsign:callsign-html-create'))
self.assertEqual(response.status_code, 302)

with self.subTest('User login'):
logged_in = self.client.login(username=self.email, password=self.password)
self.assertTrue(logged_in)

with self.subTest('GET callsign create logged in'):
response = self.client.get(reverse('callsign:callsign-html-create'))
self.assertEqual(response.status_code, 200)

with self.subTest('POST callsign create'):
response = self.client.post(reverse('callsign:callsign-html-create'), data={"name": callsign})
self.assertEqual(response.status_code, 302)
self.assertEqual(response._headers["location"][1],
reverse('callsign:callsign-html-detail', kwargs={"slug": callsign}))
self.assertTrue(CallSign.objects.filter(name=callsign).exists())

with self.subTest('GET callsign detail'):
response = self.client.get(reverse('callsign:callsign-html-detail', kwargs={"slug": callsign}))
self.assertEqual(response.status_code, 200)

with self.subTest('POST callsign claim'):
response = self.client.post(reverse('callsign:callsign-html-claim', kwargs={"slug": callsign}))
self.assertEqual(response.status_code, 302)
self.assertEqual(response._headers["location"][1],
reverse('callsign:callsign-html-detail', kwargs={"slug": callsign}))
self.assertEqual(CallSign.objects.get(name=callsign).owner, self.user)

with self.subTest('GET callsign update'):
response = self.client.get(reverse('callsign:callsign-html-update', kwargs={"slug": callsign}))
self.assertEqual(response.status_code, 200)

with self.subTest('POST callsign update'):
response = self.client.post(reverse('callsign:callsign-html-update', kwargs={"slug": callsign}),
data={"type": "personal",
"location": "{ 'type': 'Point', 'coordinates': [ -12977626.236679835245013, 5053729.738310274668038 ] }",
"cq_zone": 1,
"itu_zone": 1})
self.assertEqual(response.status_code, 302)
self.assertEqual(response._headers["location"][1],
reverse('callsign:callsign-html-detail', kwargs={"slug": callsign}))
self.assertEqual(CallSign.objects.get(name=callsign).type, "personal")
self.assertEqual(CallSign.objects.get(name=callsign).cq_zone, 1)
self.assertEqual(CallSign.objects.get(name=callsign).itu_zone, 1)

with self.subTest('GET callsign detail after update'):
response = self.client.get(reverse('callsign:callsign-html-detail', kwargs={"slug": callsign}))
self.assertEqual(response.status_code, 200)

def test_views_anonymous(self):
callsigns = ("DF0HSA",)
views = (
('callsign:callsign-html-create', dict(), 302),
('callsign:callsign-html-detail', dict(slug="DF0HSA"), 200),
('callsign:callsign-html-update', dict(slug="DF0HSA"), 302),
('callsign:callsign-html-claim', dict(slug="DF0HSA"), 302),
)

self.create_callsigns(callsigns)

for view, kwargs, status_code in views:
with self.subTest(i=view):
response = self.client.get(reverse(view, kwargs=kwargs))
self.assertEqual(response.status_code, status_code)
11 changes: 6 additions & 5 deletions project_novis/callsign/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseForbidden
from django.shortcuts import redirect
from django.views import View
from django.views.generic.detail import DetailView
Expand All @@ -7,15 +8,15 @@
from django_filters import rest_framework
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
from rest_framework_gis.filters import DistanceToPointFilter
from rest_framework import viewsets, generics
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
from rest_framework_gis.filters import DistanceToPointFilter

from .forms import CallsignForm
from .models import Country, DXCCEntry, CallSign, DMRID, CallSignPrefix, Repeater
from .serializers import CountrySerializer, DXCCEntrySerializer, CallsignSerializer,\
from .serializers import CountrySerializer, DXCCEntrySerializer, CallsignSerializer, \
DMRIDSerializer, CallSignPrefixSerializer, RepeaterSerializer, APRSPasscodeSerializer
from .forms import CallsignForm


class DefaultPagination(LimitOffsetPagination):
Expand Down Expand Up @@ -47,9 +48,9 @@ class CallSignUpdate(LoginRequiredMixin, UpdateView):
form_class = CallsignForm
template_name_suffix = '_update_form'

#TODO(elnappo) Replace permission check with django-guardian?
# TODO(elnappo) Replace permission check with django-guardian?
def dispatch(self, request, *args, **kwargs):
if self.get_object().owner == request.user:
if self.get_object().owner == request.user or not request.user.is_authenticated:
return super().dispatch(request, *args, **kwargs)
else:
return HttpResponseForbidden()
Expand Down

0 comments on commit f30bb06

Please sign in to comment.