From 5ace87d6c08b95119d7828619a7cffc069347c7c Mon Sep 17 00:00:00 2001
From: Christopher Andre Marcel INSELIN
 <christopher-andre-marcel.inselin@ext.ec.europa.eu>
Date: Mon, 9 Oct 2023 14:23:44 +0200
Subject: [PATCH] Adding test for device creation

---
 tests/e2e/netbox/test_create_device_netbox.py | 160 ++++++++++++++++++
 1 file changed, 160 insertions(+)
 create mode 100644 tests/e2e/netbox/test_create_device_netbox.py

diff --git a/tests/e2e/netbox/test_create_device_netbox.py b/tests/e2e/netbox/test_create_device_netbox.py
new file mode 100644
index 0000000..c40eac2
--- /dev/null
+++ b/tests/e2e/netbox/test_create_device_netbox.py
@@ -0,0 +1,160 @@
+"""Test case for Netbox Device Creation"""
+
+import unittest
+import os
+import requests
+import json
+
+HOST = os.getenv("HOST", default="localhost")
+PORT = os.getenv("PORT", default="8080")
+API_TOKEN = os.getenv("API_TOKEN")
+
+class TestNetboxDeviceCreate(unittest.TestCase):
+    """Test case for device creation"""
+
+    @classmethod
+    def setUpClass(cls) -> None:
+        """Will create a manufacturer, site, device role and device type"""
+        response = requests.post(
+            url=f"http://{HOST}:{PORT}/api/dcim/manufacturers/",
+            headers={"Authorization": f"Token {API_TOKEN}"},
+            json={
+                "name": "Cisco",
+                "slug": "cisco"
+            },
+            timeout=5
+        )
+
+        """Will create a device role """
+
+        response = requests.post(
+            url=f"http://{HOST}:{PORT}/api/dcim/device-roles/",
+            headers={"Authorization": f"Token {API_TOKEN}"},
+            json={
+                "name": "Switch",
+                "slug":"switch"
+            },
+            timeout=5
+        )
+
+        """ Will create a device type """
+        manufacturer_id = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/manufacturers/", headers={"Authorization": f"Token {API_TOKEN}"})
+        id = json.loads(manufacturer_id.content)["results"][0]["id"]
+        response = response = requests.post(
+            url=f"http://{HOST}:{PORT}/api/dcim/device-types/",
+            headers={"Authorization": f"Token {API_TOKEN}"},
+            json={
+                "model": "WS-C2960X",
+                "slug":"ws-c2960x",
+                "manufacturer": id
+            },
+            timeout=5
+        )
+
+        """ will create a site"""
+        response = requests.post(
+            url=f"http://{HOST}:{PORT}/api/dcim/sites/",
+            headers={"Authorization": f"Token {API_TOKEN}"},
+            json={
+                "name": "ISPRA",
+                "slug":"ispra"
+            },
+            timeout=5
+        )
+
+
+    @classmethod
+    def tearDownClass(cls) -> None:
+        """ Deleting all data from NETBOX once the all the tests finish running """
+
+        #deleting the device
+
+        device_id = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/devices/", headers={"Authorization": f"Token {API_TOKEN}"})
+        id = json.loads(device_id.content)["results"][0]["id"]
+        response_device = requests.delete(
+            url= f"http://{HOST}:{PORT}/api/dcim/devices/{id}/",
+            headers={"Authorization": f"Token {API_TOKEN}"},
+            timeout=5
+        )
+
+        #deleting the device type
+
+        device_type_id = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-types/", headers={"Authorization": f"Token {API_TOKEN}"})
+        id = json.loads(device_type_id.content)["results"][0]["id"]
+        response_device_type = requests.delete(
+            url= f"http://{HOST}:{PORT}/api/dcim/device-types/{id}/",
+            headers={"Authorization": f"Token {API_TOKEN}"},
+            timeout=5
+        )
+
+        #deleting the manufacturer
+        manufacturer_id = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/manufacturers/", headers={"Authorization": f"Token {API_TOKEN}"})
+        #id = json.loads(manufacturer_id.content)["id"]
+        #print(json.loads(manufacturer_id.content)["results"][0]["id"])
+        id = json.loads(manufacturer_id.content)["results"][0]["id"]
+        #print(type(json.loads(manufacturer_id.content)["results"][0]["id"]))
+        respose_manufacturer = requests.delete(
+            url= f"http://{HOST}:{PORT}/api/dcim/manufacturers/{id}/",
+            headers={"Authorization": f"Token {API_TOKEN}"},
+            timeout=5
+        )
+
+        # deleting the device role
+        device_role_id = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-roles/", headers={"Authorization": f"Token {API_TOKEN}"})
+        id = json.loads(device_role_id.content)["results"][0]["id"]
+        response_device_role = requests.delete(
+            url= f"http://{HOST}:{PORT}/api/dcim/device-roles/{id}/",
+            headers={"Authorization": f"Token {API_TOKEN}"},
+            timeout=5
+        )
+
+        # deleting the site
+
+        site_id =  requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers={"Authorization": f"Token {API_TOKEN}"})
+        id = json.loads(site_id.content)["results"][0]["id"]
+        response_site = requests.delete(
+            url= f"http://{HOST}:{PORT}/api/dcim/sites/{id}/",
+            headers={"Authorization": f"Token {API_TOKEN}"},
+            timeout=5
+        )
+
+    def tearDown(self) -> None:
+        pass
+    
+    def test_device_creation(self) -> None:
+        """" getting the necessary IDs """
+        device_type = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-types/", headers={"Authorization": f"Token {API_TOKEN}"})
+        device_type_id = json.loads(device_type.content)["results"][0]["id"]
+
+        device_role = requests.get(url=f"http://{HOST}:{PORT}/api/dcim/device-roles/", headers={"Authorization": f"Token {API_TOKEN}"})
+        device_role_id = json.loads(device_role.content)["results"][0]["id"]
+
+        site =  requests.get(url=f"http://{HOST}:{PORT}/api/dcim/sites/", headers={"Authorization": f"Token {API_TOKEN}"})
+        site_id = json.loads(site.content)["results"][0]["id"]
+
+        # Creating the device
+
+        response = response = requests.post(
+            url=f"http://{HOST}:{PORT}/api/dcim/devices/",
+            headers={"Authorization": f"Token {API_TOKEN}"},
+            json={
+                "name": "test",
+                "device_type": device_type_id,
+                "role": device_role_id,
+                "site": site_id,
+                "status": "offline"
+            },
+            timeout=5
+        )
+
+        self.assertEqual(response.status_code,201)
+        self.assertEqual(json.loads(response.content)["name"],"test")
+        self.assertEqual(json.loads(response.content)["device_type"]["display"],"WS-C2960X")
+        self.assertEqual(json.loads(response.content)["role"]["name"],"Switch")
+        self.assertEqual(json.loads(response.content)["site"]["name"],"ISPRA")
+        self.assertEqual(json.loads(response.content)["status"]["value"],"offline")
+
+        
+        
+if __name__ == "__main__":
+    unittest.main()
\ No newline at end of file
-- 
GitLab