From caedf874e4adbbafbea54adfc9c8af1d3688948d Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Wed, 20 May 2020 21:00:08 +0200 Subject: [PATCH] [vpn] add tests --- uncloud/urls.py | 4 +-- uncloud_net/tests.py | 73 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/uncloud/urls.py b/uncloud/urls.py index 723ef45..c75ba95 100644 --- a/uncloud/urls.py +++ b/uncloud/urls.py @@ -52,7 +52,7 @@ router.register(r'v1/service/generic', serviceviews.GenericServiceProductViewSet # Net -router.register(r'v1/net/vpn', netviews.VPNNetworkViewSet, basename='vpnnet') +router.register(r'v1/net/vpn', netviews.VPNNetworkViewSet, basename='vpnnetwork') router.register(r'v1/admin/vpnreservation', netviews.VPNNetworkReservationViewSet, basename='vpnnetreservation') @@ -77,7 +77,7 @@ router.register(r'v1/my/user', authviews.UserViewSet, basename='user') router.register(r'v1/admin/user', authviews.AdminUserViewSet, basename='useradmin') urlpatterns = [ - path('', include(router.urls)), + path(r'api/', include(router.urls)), # web/ = stuff to view in the browser path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), # for login to REST API diff --git a/uncloud_net/tests.py b/uncloud_net/tests.py index 7ce503c..dadaecf 100644 --- a/uncloud_net/tests.py +++ b/uncloud_net/tests.py @@ -1,3 +1,74 @@ from django.test import TestCase +from rest_framework.test import APIRequestFactory, force_authenticate -# Create your tests here. +from rest_framework.reverse import reverse +from django.contrib.auth import get_user_model +from django.core.exceptions import ValidationError + +from .views import * +from .models import * + + +class VPNTests(TestCase): + def setUp(self): + self.user = get_user_model().objects.create_user('django-test-user', 'noreply@ungleich.ch') + self.admin_user = get_user_model().objects.create_user('django-test-adminuser', 'noreply-admin@ungleich.ch') + self.admin_user.is_staff = True + self.admin_user.save() + + self.pool_network = '2001:db8::' + self.pool_network2 = '2001:db8:1::' + self.pool_network_size = '48' + self.pool_subnetwork_size = '64' + self.pool_vpn_hostname = 'vpn.example.org' + self.pool_wireguard_private_key = 'MOz8kk0m4jhNtAXlge0qzexZh1MipIhu4HJwtdvZ2EY=' + + self.vpn_wireguard_public_key = 'B2b78eWBIXPMM1x4DDjkCDZepS0qDgcLN3T3PjcgXkY=' + + self.vpnpool = VPNPool.objects.get_or_create(network=self.pool_network, + network_size=self.pool_network_size, + subnetwork_size=self.pool_subnetwork_size, + vpn_hostname=self.pool_vpn_hostname, + wireguard_private_key=self.pool_wireguard_private_key + ) + + self.factory = APIRequestFactory() + + + def test_create_vpnpool(self): + url = reverse("vpnpool-list") + view = VPNPoolViewSet.as_view({'post': 'create'}) + request = self.factory.post(url, { 'network': self.pool_network2, + 'network_size': self.pool_network_size, + 'subnetwork_size': self.pool_subnetwork_size, + 'vpn_hostname': self.pool_vpn_hostname, + 'wireguard_private_key': self.pool_wireguard_private_key + + }) + force_authenticate(request, user=self.admin_user) + response = view(request) + + # This raises an exception if the request was not successful + # No assert needed + pool = VPNPool.objects.get(network=self.pool_network2) + + def test_create_vpn(self): + url = reverse("vpnnetwork-list") + view = VPNNetworkViewSet.as_view({'post': 'create'}) + request = self.factory.post(url, { 'network_size': self.pool_subnetwork_size, + 'wireguard_public_key': self.vpn_wireguard_public_key + + }) + force_authenticate(request, user=self.user) + + # we don't have a billing address + with self.assertRaises(ValidationError): + response = view(request) + + + #print(response) + + + def tearDown(self): + self.user.delete() + self.admin_user.delete()