From b9675265449089eb769ce7a5ce4720b9b4f2b087 Mon Sep 17 00:00:00 2001
From: Jan Mach <jan.mach@cesnet.cz>
Date: Tue, 22 Aug 2017 15:00:14 +0200
Subject: [PATCH] Fix: Fixed unit tests.

All unit tests now pass, even though they still need a lot of work and in some case do not do much of a testing. But at least the testing feature can be now enabled on the automated build system. As the unit tests will get improved in time, the deployment process will get more and more reliable. (Redmine issue: #1017,#3387)
---
 lib/mentat/idea/test_internal.py | 12 ++++-----
 lib/mentat/test_whois.py         | 45 +++++++++++++++++++++++++++-----
 2 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/lib/mentat/idea/test_internal.py b/lib/mentat/idea/test_internal.py
index d8826509e..7814c48be 100644
--- a/lib/mentat/idea/test_internal.py
+++ b/lib/mentat/idea/test_internal.py
@@ -123,13 +123,13 @@ class TestMentatIdeaInternal(unittest.TestCase):
                     "195.113.165.128/25"
                  ],
                  "Port" : [
-                    "22"
+                    22
                  ],
-                 "Anonymised" : true
+                 "Anonymised" : True
               }
            ],
            "ID" : "4dd7cf5e-4a95-49f6-8f04-947de998012c",
-           "WinStartTime" : "2016-06-21 11:55:02Z",
+           "WinStartTime" : "2016-06-21T11:55:02Z",
            "ConnCount" : 2,
            "Source" : [
               {
@@ -162,9 +162,9 @@ class TestMentatIdeaInternal(unittest.TestCase):
            "_CESNET" : {
               "StorageTime" : 1466510407
            },
-           "WinEndTime" : "2016-06-21 12:00:02Z",
+           "WinEndTime" : "2016-06-21T12:00:02Z",
            "Format" : "IDEA0",
-           "DetectTime" : "2016-06-21 13:08:27Z"
+           "DetectTime" : "2016-06-21T13:08:27Z"
         }
 
     def test_01_idea_basic(self):
@@ -203,7 +203,7 @@ class TestMentatIdeaInternal(unittest.TestCase):
             print(idea_internal2.to_string())
             #pprint(idea_internal2)
         self.assertEqual(json.dumps(idea_internal2, indent=4, sort_keys=True, default=mentat.idea.internal.json_default), idea_internal2.to_string(indent=4))
-        orig = json.dumps(self.idea_raw, indent=4, sort_keys=True)
+        orig = json.dumps(self.idea_raw2, indent=4, sort_keys=True)
         new  = json.dumps(idea_internal2, indent=4, sort_keys=True, default=mentat.idea.internal.json_default)
         self.assertEqual(orig, new, "\n".join([l for l in difflib.context_diff(orig.split("\n"), new.split("\n"))]))
 
diff --git a/lib/mentat/test_whois.py b/lib/mentat/test_whois.py
index 4232d85f4..bfc833619 100644
--- a/lib/mentat/test_whois.py
+++ b/lib/mentat/test_whois.py
@@ -29,12 +29,41 @@ import mentat.datatype.internal
 import mentat.whois
 
 
+CONTENT_WHOIS_NEGISTRY = """
+{
+  "__negistry_about__": "Dummy Negistry file for unit tests",
+  "78.128.128.0 - 78.128.255.255": {
+    "ip4_start": "78.128.128.0",
+    "ip4_end": "78.128.255.255",
+    "resolved_abuses": [
+      "abuse@cesnet.cz"
+    ]
+  }
+}
+"""
+
+CONTENT_WHOIS_EXCEPTIONS = """
+{
+    "__whois_about__": "Dummy whois exceptions file for unit tests",
+    "__whois_type__": "exceptions",
+    "78.128.214.67": {
+        "network": "78.128.214.67",
+        "resolved_abuses": [
+            "abuse@cuni.cz"
+        ],
+        "type": "ipv4"
+    }
+}
+"""
+
+FILE_WHOIS_NEGISTRY   = '/var/tmp/unittest-whois-negistry.json'
+FILE_WHOIS_EXCEPTIONS = '/var/tmp/unittest-whois-exceptions.json'
+
 #-------------------------------------------------------------------------------
 # NOTE: Sorry for the long lines in this file. They are deliberate, because the
 # assertion permutations are (IMHO) more readable this way.
 #-------------------------------------------------------------------------------
 
-
 class TestMentatWhois(unittest.TestCase):
 
     #
@@ -118,10 +147,14 @@ class TestMentatWhois(unittest.TestCase):
     ]
 
     def setUp(self):
-        pass
+        with open(FILE_WHOIS_NEGISTRY, 'w') as fhnd:
+            fhnd.write(CONTENT_WHOIS_NEGISTRY)
+        with open(FILE_WHOIS_EXCEPTIONS, 'w') as fhnd:
+            fhnd.write(CONTENT_WHOIS_EXCEPTIONS)
 
     def tearDown(self):
-        pass
+        os.unlink(FILE_WHOIS_NEGISTRY)
+        os.unlink(FILE_WHOIS_EXCEPTIONS)
 
     def disabletest_01_basic(self):
         """
@@ -179,7 +212,7 @@ class TestMentatWhois(unittest.TestCase):
         self.maxDiff = None
 
         whois = mentat.whois.WhoisService([
-            mentat.whois.FileWhoisModule('/var/mentat/whois-negistry.json').setup()
+            mentat.whois.FileWhoisModule(FILE_WHOIS_NEGISTRY).setup()
         ])
         #for mod in whois.modules:
             #pprint(mod)
@@ -203,8 +236,8 @@ class TestMentatWhois(unittest.TestCase):
         self.maxDiff = None
 
         whois = mentat.whois.WhoisService([
-            mentat.whois.FileWhoisModule('/var/mentat/whois-exceptions.json').setup(),
-            mentat.whois.FileWhoisModule('/var/mentat/whois-negistry.json').setup()
+            mentat.whois.FileWhoisModule(FILE_WHOIS_EXCEPTIONS).setup(),
+            mentat.whois.FileWhoisModule(FILE_WHOIS_NEGISTRY).setup()
         ])
         #for mod in whois.modules:
             #pprint(mod)
-- 
GitLab