Repository: oasis-open/cti-taxii-server

Issue:
save_data_to_file() in test_memory_backend fails on Windows
https://github.com/oasis-open/cti-taxii-server/blob/b9441e14b73eefbd5c3b85c6de7a1c4e4db81a8a/medallion/test/test_memory_backend.py#L396-L403

> save_data_to_file(f.name) attempts to open the temporary file, which has already been opened by NamedTemporaryFile.  That doesn't work (at least on Windows), and causes a permission error.
>
> Python docs say: "Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later)."
>
> https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
>
> I think save_data_to_file() should probably be changed to accept a file-like object rather than a filename, akin to json.dump().

Found by @chisholm.

Discussion:

Patch:
diff --git a/medallion/backends/taxii/memory_backend.py b/medallion/backends/taxii/memory_backend.py
index 85ce27f..d0871ee 100644
--- a/medallion/backends/taxii/memory_backend.py
+++ b/medallion/backends/taxii/memory_backend.py
@@ -5,6 +5,8 @@ import logging
 import os
 import re
 
+from six import string_types
+
 from ...exceptions import ProcessingError
 from ...filters.basic_filter import BasicFilter
 from ...utils.common import (create_bundle, datetime_to_string,
@@ -113,13 +115,19 @@ class MemoryBackend(Backend):
             return json.load(infile)
 
     def load_data_from_file(self, filename):
-        with codecs.open(filename, mode="r", encoding="utf8") as infile:
-            self.data = json.load(infile)
+        if isinstance(filename, string_types):
+            with codecs.open(filename, mode="r", encoding="utf8") as infile:
+                self.data = json.load(infile)
+        else:
+            self.data = json.load(filename)
 
     def save_data_to_file(self, filename, **kwargs):
         """The kwargs are passed to ``json.dump()`` if provided."""
-        with codecs.open(filename, mode="w", encoding="utf8") as outfile:
-            json.dump(self.data, outfile, **kwargs)
+        if isinstance(filename, string_types):
+            with codecs.open(filename, mode="w", encoding="utf8") as outfile:
+                json.dump(self.data, outfile, **kwargs)
+        else:
+            json.dump(self.data, filename, **kwargs)
 
     def _get(self, key):
         for ancestors, item in iterpath(self.data):


Test patch:
diff --git a/medallion/test/test_memory_backend.py b/medallion/test/test_memory_backend.py
index 5cc5c6c..11e6091 100644
--- a/medallion/test/test_memory_backend.py
+++ b/medallion/test/test_memory_backend.py
@@ -393,15 +393,19 @@ class TestTAXIIServerWithMemoryBackend(TaxiiTest):
         post_header["Content-Type"] = MEDIA_TYPE_STIX_V20
         post_header["Accept"] = MEDIA_TYPE_TAXII_V20
 
-        with tempfile.NamedTemporaryFile() as f:
+        with tempfile.NamedTemporaryFile(mode="w+") as f:
             self.client.post(
                 test.ADD_OBJECTS_EP,
                 data=json.dumps(new_bundle),
                 headers=post_header,
             )
-            self.app.medallion_backend.save_data_to_file(f.name)
+            self.app.medallion_backend.save_data_to_file(f)
             assert os.path.isfile(f.name)
 
+            # seek(0) is needed to reset the file pointer so it's correctly
+            # read by the backend again
+            f.seek(0)
+
             configuration = copy.deepcopy(self.configuration)
             configuration["backend"]["filename"] = f.name