From f30b79e859667c39f7877678d039a74061252199 Mon Sep 17 00:00:00 2001
From: "MINTS\\dc677" <dc677@medschl.cam.ac.uk>
Date: Wed, 20 Nov 2019 15:08:55 +0000
Subject: [PATCH 1/3] Refactored mock data generation to better align to ipreg
 naming convention and return URL as part of mock data

---
 tests/generate_mock_data.py | 15 +++++++++
 tests/mock_data/ipreg.py    | 64 +++++++++++++++++++++++++++++++++----
 tests/test_api.py           |  8 ++---
 3 files changed, 76 insertions(+), 11 deletions(-)
 create mode 100644 tests/generate_mock_data.py

diff --git a/tests/generate_mock_data.py b/tests/generate_mock_data.py
new file mode 100644
index 0000000..01ab7ad
--- /dev/null
+++ b/tests/generate_mock_data.py
@@ -0,0 +1,15 @@
+import tests.mock_data
+
+mock_ipreg_data = tests.mock_data.ipreg.IPReg()
+
+subnet = '192.168.12.0'
+domain = 'mydomain.test.cam.ac.uk'
+
+data = mock_ipreg_data.list_ops_by_list_subnet(subnet, domain)
+print(data)
+
+data = mock_ipreg_data.xlist_ops_by_list_domain(domain, subnet)
+print(data['text_response'])
+
+# with open('ipreg--mock_list_subnet.txt', 'w') as f:
+#     f.write(text_response)
diff --git a/tests/mock_data/ipreg.py b/tests/mock_data/ipreg.py
index adf6967..5964981 100644
--- a/tests/mock_data/ipreg.py
+++ b/tests/mock_data/ipreg.py
@@ -16,10 +16,21 @@ class IPReg:
             'review_date'
         ]
 
+        self.xlist_box_list_domain_headers = [
+            'name',
+            'equipment',
+            'location',
+            'owner',
+            'sysadmin',
+            'end_user',
+            'remarks',
+            'review_date',
+            'addresses'
+        ]
+
         self.default_line_break = "\r\n"
         self.default_mzone = "MYMZONE"
         self.default_lan = "MYLAN"
-        self.default_domain = "mydomain.cam.ac.uk"
 
         self.ips = [13, 15, 27, 36, 51]
         self.hostnames = ['comp123', 'comp58', 'comp66', 'comp69', 'comp89', 'comp93']
@@ -34,23 +45,61 @@ class IPReg:
             'Sidney Applebaum'
         ]
 
-    def get_all_boxes(self, subnet):
-
+    def get_subnet_base(self, subnet):
         ip_parts = subnet.split(".")
         if len(ip_parts) != 4:
             raise Exception("Invalid IP: " + subnet)
 
         del(ip_parts[3])
         subnet_base = ".".join(ip_parts)
+        return subnet_base
+
+    def xlist_ops_by_list_domain(self, domain, subnet):
+        request_url = '/ipreg/xlist_ops?do_it=list_domain&domain=' + \
+                         domain + \
+                         '&object_type=box&record_separator=CRLF'
+
+        boxes = []
+        for i in range(0, 5):
+            ip = self.get_subnet_base(subnet) + "." + str(self.ips[i])
+            item = [
+                self.hostnames[i] + '.' + domain,
+                self.equipments[i],
+                self.locations[i],
+                self.owners[i],
+                '',
+                self.users[i],
+                '',
+                '',
+                ip
+            ]
+            boxes.append(item)
+
+        text_response = "\t".join(self.xlist_box_list_domain_headers)
+        text_response = text_response + self.default_line_break
+        for box in boxes:
+            text_response = text_response + "\t".join(box)
+            text_response = text_response + self.default_line_break
+
+        return {
+            'url': request_url,
+            'text_response': text_response
+        }
+
+    def list_ops_by_list_subnet(self, subnet, domain):
+
+        request_url = '/ipreg/list_ops?do_it=list_subnet&subnet_base=' + \
+                             subnet + \
+                             '&record_separator=CRLF'
 
         boxes = []
         for i in range(0, 5):
-            ip = subnet_base + "." + str(self.ips[i])
+            ip = self.get_subnet_base(subnet) + "." + str(self.ips[i])
             item = [
                 ip,
                 self.default_mzone,
                 self.default_lan,
-                self.hostnames[i] + '.' + self.default_domain,
+                self.hostnames[i] + '.' + domain,
                 self.equipments[i],
                 self.locations[i],
                 self.owners[i],
@@ -67,4 +116,7 @@ class IPReg:
             text_response = text_response + "\t".join(box)
             text_response = text_response + self.default_line_break
 
-        return text_response
+        return {
+            'url': request_url,
+            'text_response': text_response
+        }
diff --git a/tests/test_api.py b/tests/test_api.py
index 1cf5327..506e648 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -41,15 +41,13 @@ def teardown_module(module):
 
 def test_get_ip_info(requests_mock):
 
+    domain = 'mydomain.test.cam.ac.uk'
     subnet = '192.168.1.0'
-    subnet_request_url = '/ipreg/list_ops?do_it=list_subnet&subnet_base=' + \
-                         subnet + \
-                         '&record_separator=CRLF'
 
     # Register mock data against url
     mock_ipreg_data = tests.mock_data.ipreg.IPReg()  # Generates jackdaw ipreg responses
-    text_response = mock_ipreg_data.get_all_boxes(subnet)
-    requests_mock.get(subnet_request_url, text=text_response)
+    data = mock_ipreg_data.list_ops_by_list_subnet(subnet, domain)
+    requests_mock.get(data['url'], text=data['text_response'])
 
     api_client = ipreg.IPRegApi(cookie_file=pytest.cookie_file_name)
 
-- 
GitLab


From 70ba39f09626ebb02ad8580e42ca4fd05a8f18b8 Mon Sep 17 00:00:00 2001
From: "MINTS\\dc677" <dc677@medschl.cam.ac.uk>
Date: Wed, 20 Nov 2019 15:09:07 +0000
Subject: [PATCH 2/3] Added domain mock test

---
 tests/test_api.py | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/tests/test_api.py b/tests/test_api.py
index 506e648..edcb58f 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -55,3 +55,21 @@ def test_get_ip_info(requests_mock):
     ip_info = api_client.get_ip_info(ip)
 
     assert ip_info['address'] == ip
+
+
+def test_get_domain_info(requests_mock):
+
+    domain = 'mydomain.test.cam.ac.uk'
+    subnet = '192.168.1.0'
+
+    # Register mock data against url
+    mock_ipreg_data = tests.mock_data.ipreg.IPReg()  # Generates jackdaw ipreg responses
+    data = mock_ipreg_data.xlist_ops_by_list_domain(domain, subnet)
+    requests_mock.get(data['url'], text=data['text_response'])
+
+    api_client = ipreg.IPRegApi(cookie_file=pytest.cookie_file_name)
+
+    hostname = "comp66.mydomain.test.cam.ac.uk"
+    hostname_info = api_client.get_hostname_info(hostname)
+
+    assert hostname_info['name'] == hostname
-- 
GitLab


From e278f38d5996709715026e134c7d9233883a247c Mon Sep 17 00:00:00 2001
From: "MINTS\\dc677" <dc677@medschl.cam.ac.uk>
Date: Wed, 20 Nov 2019 15:26:59 +0000
Subject: [PATCH 3/3] Updated documentation and formatting.

---
 README.md                   | 26 ++++++++++++++++++++++++++
 tests/generate_mock_data.py |  2 +-
 tests/mock_data/ipreg.py    |  5 +++++
 tests/test_api.py           |  5 +++++
 4 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 4510f97..d655021 100644
--- a/README.md
+++ b/README.md
@@ -74,3 +74,29 @@ Cookie file - https://www.dns.cam.ac.uk/ipreg/api/
 
 Download cookie - https://jackdaw.cam.ac.uk/ipreg/download-cookie
 
+
+# Usage
+
+```
+>>> import ipreg
+>>> api_client = ipreg.IPRegApi(cookie_file='cookie-jackdaw-dev.txt')
+>>> api_client.get_list_ops_by_subnet('192.168.1.0')
+[{'address': '192.168.1.15', 'mzone': 'MYMZONE', 'lan': 'MYLAND', 'name': 'comp58.mydomain.test.cam.ac.uk' ...},{'address': '192.168.1.27', 'mzone': 'MYMZONE', 'lan': 'MYLAND', 'name': 'comp66.mydomain.test.cam.ac.uk' ...}]
+>>> api_client.get_ip_info('192.168.1.15')
+{'address': '131.111.92.166', 'mzone': 'MYMZONE', 'lan': 'MYLAND', 'name': 'comp58.mydomain.test.cam.ac.uk'...
+```
+
+The previous example shows an interactive session that pulls back a whole subnet info and then a specific
+ip address detail.
+
+```python
+import ipreg
+api_client = ipreg.IPRegApi(cookie_file='/path/to/my/cookie-jackdaw.txt', cache_dir='/path/to/my/cache/')
+
+fqdn = 'comp58.mydomain.test.cam.ac.uk'
+ip = '192.168.1.15'
+
+ip_info = api_client.get_ip_info(ip)
+hostname_info = api_client.get_hostname_info(fqdn)
+```
+
diff --git a/tests/generate_mock_data.py b/tests/generate_mock_data.py
index 01ab7ad..8754c84 100644
--- a/tests/generate_mock_data.py
+++ b/tests/generate_mock_data.py
@@ -6,7 +6,7 @@ subnet = '192.168.12.0'
 domain = 'mydomain.test.cam.ac.uk'
 
 data = mock_ipreg_data.list_ops_by_list_subnet(subnet, domain)
-print(data)
+print(data['text_response'])
 
 data = mock_ipreg_data.xlist_ops_by_list_domain(domain, subnet)
 print(data['text_response'])
diff --git a/tests/mock_data/ipreg.py b/tests/mock_data/ipreg.py
index 5964981..bf3e2a3 100644
--- a/tests/mock_data/ipreg.py
+++ b/tests/mock_data/ipreg.py
@@ -1,3 +1,8 @@
+"""IPReg Mock Data generator
+
+
+"""
+
 
 class IPReg:
 
diff --git a/tests/test_api.py b/tests/test_api.py
index edcb58f..f601329 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -11,12 +11,15 @@ import ipreg
 def setup_module(module):
     """ setup any state specific to the execution of the given module."""
 
+    # The testing cache directory needs to exist
     current_directory = os.getcwd()
     final_directory = os.path.join(current_directory, r'cache-tests')
     pytest.cache_directory = final_directory
     if not os.path.exists(final_directory):
         os.makedirs(final_directory)
 
+    # A fake cookie is needed to ensure that the cookie validation
+    # part of the API will pass.
     pytest.cookie_file_name = 'cookie-jackdaw-gen-test.txt'
     with open(pytest.cookie_file_name, 'w') as f:
         cookie_parts = [
@@ -35,6 +38,8 @@ def teardown_module(module):
     """ teardown any state that was previously setup with a setup_module
     method.
     """
+
+    # Remove the cookies and cache directory
     os.rmdir(pytest.cache_directory)
     os.remove(pytest.cookie_file_name)
 
-- 
GitLab