Skip to content

Commit

Permalink
changed lorawan connection view to modelviewset
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoLozCoding committed Dec 8, 2023
1 parent cebafc6 commit 80176f9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 122 deletions.
48 changes: 24 additions & 24 deletions manifests/tests/test_lorawan_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def test_retrieve_existing_lorawan_connection(self):
LorawanConnection.objects.create(node=self.nodedata, lorawan_device=self.device)

# Create a request to retrieve the LorawanConnection
url = reverse('manifests:retrieve_lorawan_connection',kwargs={'node_vsn': self.nodedata.vsn,'lorawan_deveui': self.device.deveui})
url = reverse('manifests:URD_lorawan_connection',kwargs={'node_vsn': self.nodedata.vsn,'lorawan_deveui': self.device.deveui})
request = self.factory.get(url)

# Use the LorawanConnectionView to handle the request
lorawan_connection_view = LorawanConnectionView.as_view()
lorawan_connection_view = LorawanConnectionView.as_view({'get': 'retrieve'})
response = lorawan_connection_view(request, node_vsn=self.nodedata.vsn, lorawan_deveui=self.device.deveui)

# Check the response status code and data
Expand All @@ -56,11 +56,11 @@ def test_retrieve_existing_lorawan_connection(self):
def test_retrieve_nonexistent_lorawan_connection(self):
"""Test lorawan connection view for retrieving records sad path"""
# Attempt to retrieve a nonexistent LorawanConnection
url = reverse('manifests:retrieve_lorawan_connection',kwargs={'node_vsn': "nonexistent_vsn",'lorawan_deveui': "nonexistent_deveui"})
url = reverse('manifests:URD_lorawan_connection',kwargs={'node_vsn': "nonexistent_vsn",'lorawan_deveui': "nonexistent_deveui"})
request = self.factory.get(url)

# Use the LorawanConnectionView to handle the request
lorawan_connection_view = LorawanConnectionView.as_view()
lorawan_connection_view = LorawanConnectionView.as_view({'get': 'retrieve'})
response = lorawan_connection_view(request, node_vsn="nonexistent_vsn", lorawan_deveui="nonexistent_deveui")

self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Expand All @@ -69,7 +69,7 @@ def test_retrieve_nonexistent_lorawan_connection(self):
def test_create_lorawan_connection_success(self):
"""Test correctly creating a lorawan connection"""
# Create a request to create a LorawanConnection
url = reverse('manifests:create_lorawan_connection')
url = reverse('manifests:C_lorawan_connection')
data = {
"node": self.nodedata.vsn,
"lorawan_device": self.device.deveui,
Expand All @@ -78,7 +78,7 @@ def test_create_lorawan_connection_success(self):
request = self.factory.post(url, data, format="json")

# Use the LorawanConnectionView to handle the request
lorawan_connection_view = LorawanConnectionView.as_view()
lorawan_connection_view = LorawanConnectionView.as_view({'post': 'create'})
response = lorawan_connection_view(request)

# Check the response status code and data
Expand All @@ -93,7 +93,7 @@ def test_create_lorawan_connection_invalid_node(self):
"""Test creating a lorawan connection with invalid node"""
# Create a request to create a LorawanConnection with an invalid node
nonexistent_vsn = 'nonexistent_vsn'
url = reverse('manifests:create_lorawan_connection')
url = reverse('manifests:C_lorawan_connection')
data = {
"node": nonexistent_vsn,
"lorawan_device": self.device.deveui,
Expand All @@ -102,12 +102,12 @@ def test_create_lorawan_connection_invalid_node(self):
request = self.factory.post(url, data, format="json")

# Use the LorawanConnectionView to handle the request
lorawan_connection_view = LorawanConnectionView.as_view()
lorawan_connection_view = LorawanConnectionView.as_view({'post': 'create'})
response = lorawan_connection_view(request)

# Check the response status code and error message
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data, {"message": f"Node with vsn {nonexistent_vsn} does not exist"})
self.assertEqual(response.data, {"node":[f"Invalid vsn \"{nonexistent_vsn}\" - object does not exist."]})

# Check that no LorawanConnection is not created in the database
with self.assertRaises(LorawanConnection.DoesNotExist):
Expand All @@ -118,7 +118,7 @@ def test_create_lorawan_connection_invalid_device(self):
"""Test creating a lorawan connection with invalid device"""
# Create a request to create a LorawanConnection with an invalid node
nonexistent_deveui = '121456984'
url = reverse('manifests:create_lorawan_connection')
url = reverse('manifests:C_lorawan_connection')
data = {
"node": self.nodedata.vsn,
"lorawan_device": nonexistent_deveui,
Expand All @@ -127,12 +127,12 @@ def test_create_lorawan_connection_invalid_device(self):
request = self.factory.post(url, data, format="json")

# Use the LorawanConnectionView to handle the request
lorawan_connection_view = LorawanConnectionView.as_view()
lorawan_connection_view = LorawanConnectionView.as_view({'post': 'create'})
response = lorawan_connection_view(request)

# Check the response status code and error message
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data, {"message": f"Lorawan Device with deveui {nonexistent_deveui} does not exist"})
self.assertEqual(response.data, {"lorawan_device":[f"Invalid pk \"{nonexistent_deveui}\" - object does not exist."]})

# Check that no LorawanConnection is not created in the database
with self.assertRaises(LorawanConnection.DoesNotExist):
Expand All @@ -142,7 +142,7 @@ def test_create_lorawan_connection_invalid_device(self):
def test_create_lorawan_connection_serializer_error(self):
"""Test for getting a serializer error when creating a lorawan connection"""
# Create a request to create a LorawanConnection
url = reverse('manifests:create_lorawan_connection')
url = reverse('manifests:C_lorawan_connection')
data = {
"node": self.nodedata.vsn,
"lorawan_device": self.device.deveui,
Expand All @@ -151,7 +151,7 @@ def test_create_lorawan_connection_serializer_error(self):
request = self.factory.post(url, data, format="json")

# Use the LorawanConnectionView to handle the request
lorawan_connection_view = LorawanConnectionView.as_view()
lorawan_connection_view = LorawanConnectionView.as_view({'post': 'create'})
response = lorawan_connection_view(request)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Expand All @@ -167,12 +167,12 @@ def test_update_lorawan_connection_success(self):
lorawan_connection = LorawanConnection.objects.create(node=self.nodedata, lorawan_device=self.device, connection_type="ABP")

#request
url = reverse('manifests:update_lorawan_connection',kwargs={'node_vsn': self.nodedata.vsn,'lorawan_deveui': self.device.deveui})
url = reverse('manifests:URD_lorawan_connection',kwargs={'node_vsn': self.nodedata.vsn,'lorawan_deveui': self.device.deveui})
data = {"connection_type": "OTAA"}
request = self.factory.patch(url, data, format="json")

# Use the LorawanConnectionView to handle the request
lorawan_connection_view = LorawanConnectionView.as_view()
lorawan_connection_view = LorawanConnectionView.as_view({'patch': 'partial_update'})
response = lorawan_connection_view(request, node_vsn=self.nodedata.vsn, lorawan_deveui=self.device.deveui)

# Check the response status code and data
Expand All @@ -190,17 +190,17 @@ def test_update_lorawan_connection_invalid_node(self):

#request
nonexistent_vsn = 'nonexistent_vsn'
url = reverse('manifests:update_lorawan_connection',kwargs={'node_vsn': self.nodedata.vsn,'lorawan_deveui': self.device.deveui})
url = reverse('manifests:URD_lorawan_connection',kwargs={'node_vsn': self.nodedata.vsn,'lorawan_deveui': self.device.deveui})
data = {"node": nonexistent_vsn, "connection_type": "OTAA"}
request = self.factory.patch(url, data, format="json")

# Use the LorawanConnectionView to handle the request
lorawan_connection_view = LorawanConnectionView.as_view()
lorawan_connection_view = LorawanConnectionView.as_view({'patch': 'partial_update'})
response = lorawan_connection_view(request, node_vsn=self.nodedata.vsn, lorawan_deveui=self.device.deveui)

# Check the response status code and error message
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data, {"message": f"Node with vsn {nonexistent_vsn} does not exist"})
self.assertEqual(response.data,{"node":[f"Invalid vsn \"{nonexistent_vsn}\" - object does not exist."]})

# Check if the LorawanConnection is not updated in the database
lorawan_connection = LorawanConnection.objects.get(node=self.nodedata, lorawan_device=self.device)
Expand All @@ -214,17 +214,17 @@ def test_update_lorawan_connection_invalid_device(self):

#request
nonexistent_deveui = 'nonexistent_deveui'
url = reverse('manifests:update_lorawan_connection',kwargs={'node_vsn': self.nodedata.vsn,'lorawan_deveui': self.device.deveui})
url = reverse('manifests:URD_lorawan_connection',kwargs={'node_vsn': self.nodedata.vsn,'lorawan_deveui': self.device.deveui})
data = {"lorawan_device": nonexistent_deveui, "connection_type": "OTAA"}
request = self.factory.patch(url, data, format="json")

# Use the LorawanConnectionView to handle the request
lorawan_connection_view = LorawanConnectionView.as_view()
lorawan_connection_view = LorawanConnectionView.as_view({'patch': 'partial_update'})
response = lorawan_connection_view(request, node_vsn=self.nodedata.vsn, lorawan_deveui=self.device.deveui)

# Check the response status code and error message
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data, {"message": f"Lorawan Device with deveui {nonexistent_deveui} does not exist"})
self.assertEqual(response.data, {"lorawan_device":[f"Invalid pk \"{nonexistent_deveui}\" - object does not exist."]})

# Check if the LorawanConnection is not updated in the database
lorawan_connection = LorawanConnection.objects.get(node=self.nodedata, lorawan_device=self.device)
Expand All @@ -237,12 +237,12 @@ def test_update_lorawan_connection_serializer_error(self):
lorawan_connection = LorawanConnection.objects.create(node=self.nodedata, lorawan_device=self.device, connection_type="ABP")

# Create a request to update a LorawanConnection
url = reverse('manifests:update_lorawan_connection',kwargs={'node_vsn': self.nodedata.vsn,'lorawan_deveui': self.device.deveui})
url = reverse('manifests:URD_lorawan_connection',kwargs={'node_vsn': self.nodedata.vsn,'lorawan_deveui': self.device.deveui})
data = {"connection_type": "error"}
request = self.factory.patch(url, data, format="json")

# Use the LorawanConnectionView to handle the request
lorawan_connection_view = LorawanConnectionView.as_view()
lorawan_connection_view = LorawanConnectionView.as_view({'patch': 'partial_update'})
response = lorawan_connection_view(request, node_vsn=self.nodedata.vsn, lorawan_deveui=self.device.deveui)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Expand Down
14 changes: 5 additions & 9 deletions manifests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@
router.register(r"sensors", SensorHardwareViewSet)
router.register(r"node-builds", NodeBuildViewSet)
router.register(r"lorawandevices", LorawanDeviceView, basename="lorawandevices")
router.register(r"lorawanconnections", LorawanConnectionView, basename="lorawanconnections")

urlpatterns = [
path("", include(router.urls)),
path(
"lorawanconnections/",
LorawanConnectionView.as_view(),
name="create_lorawan_connection",
LorawanConnectionView.as_view({'post': 'create'}),
name="C_lorawan_connection",
),
path(
"lorawanconnections/<str:node_vsn>/<str:lorawan_deveui>/",
LorawanConnectionView.as_view(),
name="update_lorawan_connection",
),
path(
"lorawanconnections/<str:node_vsn>/<str:lorawan_deveui>/",
LorawanConnectionView.as_view(),
name="retrieve_lorawan_connection",
LorawanConnectionView.as_view({'patch': 'partial_update', 'put': 'update', 'get': 'retrieve', 'delete':'destroy'}),
name="URD_lorawan_connection",
),
path(
"lorawankeys/",
Expand Down
88 changes: 1 addition & 87 deletions manifests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class LorawanDeviceView(NodeAuthMixin, ModelViewSet):
queryset = LorawanDevice.objects.all()
lookup_field = "deveui"

class LorawanConnectionView(NodeOwnedObjectsMixin, CreateAPIView, UpdateAPIView, RetrieveAPIView):
class LorawanConnectionView(NodeOwnedObjectsMixin, ModelViewSet):
serializer_class = LorawanConnectionSerializer
vsn_field = "node__vsn"

Expand All @@ -100,92 +100,6 @@ def get_object(self):
except LorawanConnection.DoesNotExist:
raise Http404

def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
#retrieve node
if "node" in serializer.validated_data:
vsn = serializer.validated_data["node"]["vsn"]
try:
node = NodeData.objects.get(vsn=vsn)
except NodeData.DoesNotExist:
return Response(
{"message": f"Node with vsn {vsn} does not exist"},
status=status.HTTP_400_BAD_REQUEST,
)
else:
serializer.validated_data["node"] = node
#retrieve ld
if "lorawan_device" in serializer.validated_data:
deveui = serializer.validated_data["lorawan_device"]["deveui"]
try:
device = LorawanDevice.objects.get(deveui=deveui)
except LorawanDevice.DoesNotExist:
return Response(
{
"message": f"Lorawan Device with deveui {deveui} does not exist"
},
status=status.HTTP_400_BAD_REQUEST,
)
else:
serializer.validated_data["lorawan_device"] = device

try:
LorawanConnection.objects.create(**serializer.validated_data)
except IntegrityError as e:
error_message = f"IntegrityError: {e}"
return Response(
{"message": error_message},
status=status.HTTP_400_BAD_REQUEST,
)

# Return a response
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def update(self, request, *args, **kwargs):
partial = kwargs.pop("partial", False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)

if serializer.is_valid():
#retrieve node
if "node" in serializer.validated_data:
vsn = serializer.validated_data["node"]["vsn"]
try:
node = NodeData.objects.get(vsn=vsn)
except NodeData.DoesNotExist:
return Response(
{"message": f"Node with vsn {vsn} does not exist"},
status=status.HTTP_400_BAD_REQUEST,
)
else:
serializer.validated_data["node"] = node
#retrieve ld
if "lorawan_device" in serializer.validated_data:

deveui = serializer.validated_data["lorawan_device"]["deveui"]
try:
device = LorawanDevice.objects.get(deveui=deveui)
except LorawanDevice.DoesNotExist:
return Response(
{
"message": f"Lorawan Device with deveui {deveui} does not exist"
},
status=status.HTTP_400_BAD_REQUEST,
)
else:
serializer.validated_data["lorawan_device"] = device

serializer.save(**serializer.validated_data)

# Return a response
return Response(serializer.data)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class LorawanKeysView(NodeOwnedObjectsMixin, CreateAPIView, UpdateAPIView, RetrieveAPIView):
serializer_class = LorawanKeysSerializer
lookup_field = "lorawan_connection"
Expand Down
4 changes: 2 additions & 2 deletions node_auth/tests/test_node_auth_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def test_correct_OnlyCreateToSelf_auth(self):
self.csrf_client.credentials(HTTP_AUTHORIZATION=self.auth_header)

# Use reverse to dynamically generate the URL
url = reverse('manifests:create_lorawan_connection')
url = reverse('manifests:C_lorawan_connection')
response = self.csrf_client.post(url, data, format='json')

self.assertEqual(response.status_code, status.HTTP_201_CREATED, "Expected status code 201, object created")
Expand All @@ -195,7 +195,7 @@ def test_wrong_OnlyCreateToSelf_auth(self):
self.csrf_client.credentials(HTTP_AUTHORIZATION=self.auth_header)

# Use reverse to dynamically generate the URL
url = reverse('manifests:create_lorawan_connection')
url = reverse('manifests:C_lorawan_connection')
response = self.csrf_client.post(url, data, format='json')

self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, "Expected status code 403, Forbidden")
Expand Down

0 comments on commit 80176f9

Please sign in to comment.