示例#1
0
 def setUp(self):
     self.config = MSDSConfig()
     self.server = DataSyncServer(self.config)
示例#2
0
 def setUp(self):
     self.config = MSDSConfig()
     self.server = DataSyncServer(self.config)
示例#3
0
class DataSyncServerTests(unittest.TestCase):
    """
    These are unit tests for the `DataSyncServer` class.
    """
    def setUp(self):
        self.config = MSDSConfig()
        self.server = DataSyncServer(self.config)

    @staticmethod
    def fake_urlopen(json, my_dingus=None):
        my_dingus = my_dingus or dingus.Dingus()
        urlopen = dingus.Dingus(return_value=StringIO(json))
        return betterpatch("urllib2.urlopen", urlopen)

    def test_handshake(self):
        """Try the "handshake" API call."""
        with self.fake_urlopen('{ "success": true, "details": { "host": "testhost", "flags": "testflags", "username": "******", "rootdir": "rootdir", "rules": ["a", "b", "c"] } }'):
            details = self.server.handshake()

            self.assertEqual(details["host"], "testhost")
            self.assertListEqual(details["rules"], ["a", "b", "c"])

    def test_handshake_weird_json(self):
        """Test handshaking when server returns unexpected json."""
        with self.assertRaises(KeyError):
            with self.fake_urlopen("{ 'lah': 'hello' }"):
                details = self.server.handshake()

    def test_handshake_not_json(self):
        """
        Test handshaking when server returns something other than json.
        This test case expects `KeyError`, which isn't ideal.
        """
        with self.assertRaises(KeyError):
            with self.fake_urlopen("  *8>--O===3  this is not json; it's an emu  "):
                details = self.server.handshake()

    def test_requestsync(self):
        """
        Tests calling the /sync/requestsync/ view. The fake server
        requests no files.
        """
        with self.fake_urlopen('{ "success": true, "details": { "host": "testhost", "flags": "testflags", "username": "******", "rootdir": "rootdir", "rules": ["a", "b", "c"] } }') as d:
            result = self.server.requestsync()
            self.assertTrue(result["success"])
            self.assertEqual(len(d.calls), 1)
            self.assertEqual(len(d.calls("()")), 1)
            self.assertTrue(d.calls("()", dingus.DontCare,
                                    "sync_completed=False&version=%s" % VERSION,
                                    dingus.DontCare).once())

    def test_requestsync_expected_files(self):
        """
        Tests calling /sync/requestsync, with 1 file requested by the
        fake server.
        """
        # fixme: this is the wrong place to test such things
        with self.fake_urlopen('{ "success": true, "details": {}, "files": { "1": { "test.txt": [1, 2, "relpath", false] } } }') as d:
            result = self.server.requestsync()
            logger.info(repr(d.calls))
            self.assertTrue(result["success"])

    def test_requestsync_baseurl(self):
        "tests requestsync when configured sync url has no trailing slash"
        self.config["synchub"] = "http://test"
        self.config["sitename"] = "b"
        self.config["stationname"] = "c"
        self.config["organisation"] = "a"
        my_dingus = dingus.Dingus()
        with self.fake_urlopen('{ "success": true, "details": {} }') as d:
            with dingus.patch("urllib2.Request", my_dingus):
                result = self.server.requestsync()
                self.assertTrue(result["success"])
                self.assertTrue(d.calls("()").once())
            self.assertEqual(len(my_dingus.calls), 1)
            self.assertEqual(my_dingus.calls[0].args[0], "http://test/requestsync/a/b/c/")

    def test_requestsync_urlopen_error(self):
        "test requestsync when urlopen raises an exception"
        with dingus.patch("urllib2.urlopen", dingus.exception_raiser(IOError("you fail"))):
            result = self.server.requestsync()
            self.assertFalse(result["success"])
            self.assertEqual(result["message"], "you fail")

    def test_requestsync_spaces(self):
        self.config["synchub"] = "http://test"
        self.config["sitename"] = "An Interesting Site"
        self.config["stationname"] = "A Station Name"
        self.config["organisation"] = "The Organisation"

        my_dingus = dingus.Dingus()
        with self.fake_urlopen('{ "success": true, "details": {} }') as d:
            with dingus.patch("urllib2.Request", my_dingus):
                result = self.server.requestsync()
                self.assertTrue(result["success"])
                self.assertTrue(d.calls("()").once())
            self.assertEqual(len(my_dingus.calls), 1)
            self.assertEqual(my_dingus.calls[0].args[0], "http://test/requestsync/The%20Organisation/An%20Interesting%20Site/A%20Station%20Name/")
示例#4
0
class DataSyncServerTests(unittest.TestCase):
    """
    These are unit tests for the `DataSyncServer` class.
    """
    def setUp(self):
        self.config = MSDSConfig()
        self.server = DataSyncServer(self.config)

    @staticmethod
    def fake_urlopen(json, my_dingus=None):
        my_dingus = my_dingus or dingus.Dingus()
        urlopen = dingus.Dingus(return_value=StringIO(json))
        return betterpatch("urllib2.urlopen", urlopen)

    def test_handshake(self):
        """Try the "handshake" API call."""
        with self.fake_urlopen(
                '{ "success": true, "details": { "host": "testhost", "flags": "testflags", "username": "******", "rootdir": "rootdir", "rules": ["a", "b", "c"] } }'
        ):
            details = self.server.handshake()

            self.assertEqual(details["host"], "testhost")
            self.assertListEqual(details["rules"], ["a", "b", "c"])

    def test_handshake_weird_json(self):
        """Test handshaking when server returns unexpected json."""
        with self.assertRaises(KeyError):
            with self.fake_urlopen("{ 'lah': 'hello' }"):
                details = self.server.handshake()

    def test_handshake_not_json(self):
        """
        Test handshaking when server returns something other than json.
        This test case expects `KeyError`, which isn't ideal.
        """
        with self.assertRaises(KeyError):
            with self.fake_urlopen(
                    "  *8>--O===3  this is not json; it's an emu  "):
                details = self.server.handshake()

    def test_requestsync(self):
        """
        Tests calling the /sync/requestsync/ view. The fake server
        requests no files.
        """
        with self.fake_urlopen(
                '{ "success": true, "details": { "host": "testhost", "flags": "testflags", "username": "******", "rootdir": "rootdir", "rules": ["a", "b", "c"] } }'
        ) as d:
            result = self.server.requestsync()
            self.assertTrue(result["success"])
            self.assertEqual(len(d.calls), 1)
            self.assertEqual(len(d.calls("()")), 1)
            self.assertTrue(
                d.calls("()", dingus.DontCare,
                        "sync_completed=False&version=%s" % VERSION,
                        dingus.DontCare).once())

    def test_requestsync_expected_files(self):
        """
        Tests calling /sync/requestsync, with 1 file requested by the
        fake server.
        """
        # fixme: this is the wrong place to test such things
        with self.fake_urlopen(
                '{ "success": true, "details": {}, "files": { "1": { "test.txt": [1, 2, "relpath", false] } } }'
        ) as d:
            result = self.server.requestsync()
            logger.info(repr(d.calls))
            self.assertTrue(result["success"])

    def test_requestsync_baseurl(self):
        "tests requestsync when configured sync url has no trailing slash"
        self.config["synchub"] = "http://test"
        self.config["sitename"] = "b"
        self.config["stationname"] = "c"
        self.config["organisation"] = "a"
        my_dingus = dingus.Dingus()
        with self.fake_urlopen('{ "success": true, "details": {} }') as d:
            with dingus.patch("urllib2.Request", my_dingus):
                result = self.server.requestsync()
                self.assertTrue(result["success"])
                self.assertTrue(d.calls("()").once())
            self.assertEqual(len(my_dingus.calls), 1)
            self.assertEqual(my_dingus.calls[0].args[0],
                             "http://test/requestsync/a/b/c/")

    def test_requestsync_urlopen_error(self):
        "test requestsync when urlopen raises an exception"
        with dingus.patch("urllib2.urlopen",
                          dingus.exception_raiser(IOError("you fail"))):
            result = self.server.requestsync()
            self.assertFalse(result["success"])
            self.assertEqual(result["message"], "you fail")

    def test_requestsync_spaces(self):
        self.config["synchub"] = "http://test"
        self.config["sitename"] = "An Interesting Site"
        self.config["stationname"] = "A Station Name"
        self.config["organisation"] = "The Organisation"

        my_dingus = dingus.Dingus()
        with self.fake_urlopen('{ "success": true, "details": {} }') as d:
            with dingus.patch("urllib2.Request", my_dingus):
                result = self.server.requestsync()
                self.assertTrue(result["success"])
                self.assertTrue(d.calls("()").once())
            self.assertEqual(len(my_dingus.calls), 1)
            self.assertEqual(
                my_dingus.calls[0].args[0],
                "http://test/requestsync/The%20Organisation/An%20Interesting%20Site/A%20Station%20Name/"
            )