From d6d84f4e0b8128d7947c93e55004cbd781dcdd63 Mon Sep 17 00:00:00 2001 From: sneakypete81 Date: Thu, 19 Dec 2013 18:50:12 +0000 Subject: [PATCH] Add tests for unicode filename uploads --- tests/data/test_ünicode_photo.jpg | Bin 0 -> 1657 bytes tests/functional/test_photos.py | 21 +++++++++++++ tests/unit/data/ünicode_test_file.txt | 1 + tests/unit/test_cli.py | 41 ++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 tests/data/test_ünicode_photo.jpg create mode 100644 tests/unit/data/ünicode_test_file.txt diff --git a/tests/data/test_ünicode_photo.jpg b/tests/data/test_ünicode_photo.jpg new file mode 100644 index 0000000000000000000000000000000000000000..bc469e3216330372c736425b3235087452058d9c GIT binary patch literal 1657 zcmb7Ec~FyA5dXd}2_%GsFOU$Fa6~~1f)b7tuz=hI)Dbz#R1vwXfDs}B1%ZKx2L*(1 zIovl5fsBG&(tsR-H3B1uEtf)yC=8&YDC&sm2P18#)3MWi@1M7C-`m~a?*62s(rEy8 zc3?RGAP53Z$O5DjfGvQ6p!JOi8rc{u27^XpQPZj%3gIeKG54Aj!N_jsB7STO=a{GF}|4%V!M?i4vmI6$Czv5BS#tLF)532UuS zgF}{bRX(;H7YuvF=HDg7c#Zni`&M{rTbenD6;rR&H0%#4a!1&=c`$E*#UB>FxNaF%h@gW6kmt|vk!{S&-H8_^@FS%Lmv zFn~4S9vebB|*XGN$_uyt%_f>(K;(tL-!IYn8o zRN0XsrF>L2@ksxff`Mo^w+@?+X2&OIEE)fz%B6L1yEJOo#PPJMB$2zi4eM=-*F>v( zujzI5=q6*axqG?(`@r+zsqgv@J!6_+Q|NKY9xbbhQyH40S9k{uM~_~bG>40|9`u{B z*?MK33zQ98m!J{tzFgxPIV$?_WOwx+md~(YIox;;Q zBjk8Hmpm#?{a*N>UJ@SPmTcgc5_Tt=I6t~v-`+Zllv5i<`VvCRAk(o~EfPS7HLYD4 zzMRCem-SuCyv_EtqVr92_Ouj>Tk%5USl6N-2`9~I^J!mg*Y`8rooTn$Xfx&c8W&KA z8*$HzsQ|Ev;}^Qbny37^M@f-B9`D5Gw*3%gwd+v7(~}IAPip0?F1YeA@`Ex!qeXsA{JS$=q1c7~FEs*nzAjs*4&n zqnvNJxUMo?oaxl5<}KWrv`%#YLV5ba?dNl|`8aADE z;mAE?-6!1))s+Hmr{Ai*o36GJT{gaQzm|YzMqCPK+;G7gwBX{VzOC@rPfwSQ{Q=4V BUjG09 literal 0 HcmV?d00001 diff --git a/tests/functional/test_photos.py b/tests/functional/test_photos.py index 1fe409b..0f5c867 100644 --- a/tests/functional/test_photos.py +++ b/tests/functional/test_photos.py @@ -121,6 +121,27 @@ class TestPhotos(test_base.TestBase): photos[1].delete() self.photos[0].update(permission=False) + # Unicode filename upload not working due to frontend bug 1433 + @unittest.expectedFailure + def test_upload_unicode_filename(self): + """Test that a photo with a unicode filename can be uploaded""" + ret_val = self.client.photo.upload(u"tests/data/test_\xfcnicode_photo.jpg", + title=self.TEST_TITLE) + # Check that there are now four photos + self.photos = self.client.photos.list() + self.assertEqual(len(self.photos), 4) + + # Check that the upload return value was correct + pathOriginals = [photo.pathOriginal for photo in self.photos] + self.assertIn(ret_val.pathOriginal, pathOriginals) + + # Delete the photo + ret_val.delete() + + # Check that it's gone + self.photos = self.client.photos.list() + self.assertEqual(len(self.photos), 3) + def test_update(self): """ Update a photo by editing the title """ title = "\xfcmlaut" # umlauted umlaut diff --git a/tests/unit/data/ünicode_test_file.txt b/tests/unit/data/ünicode_test_file.txt new file mode 100644 index 0000000..4fff881 --- /dev/null +++ b/tests/unit/data/ünicode_test_file.txt @@ -0,0 +1 @@ +Test File diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index 22daa00..08f81aa 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -23,6 +23,8 @@ def raise_exception(_): class TestCli(unittest.TestCase): test_file = os.path.join("tests", "unit", "data", "test_file.txt") + test_unicode_file = os.path.join("tests", "unit", "data", + "\xfcnicode_test_file.txt") @mock.patch.object(trovebox.main.trovebox, "Trovebox") @mock.patch('sys.stdout', new_callable=io.StringIO) @@ -107,6 +109,45 @@ class TestCli(unittest.TestCase): with self.assertRaises(IOError): main(["-X", "POST", "-F", "photo=@%s.missing" % self.test_file]) + @mock.patch.object(trovebox.main.trovebox, "Trovebox") + @mock.patch('sys.stdout', new_callable=io.StringIO) + def test_post_unicode_files(self, _, mock_trovebox): + """Check that unicode filenames are posted correctly""" + post = mock_trovebox.return_value.post + + # Python 2.x provides encoded commandline arguments + file_param = "photo=@%s" % self.test_unicode_file + if sys.version < '3': + file_param = file_param.encode(sys.getfilesystemencoding()) + + main(["-X", "POST", "-F", "photo=@%s" % self.test_unicode_file]) + # It's not possible to directly compare the file object, + # so check it manually + files = post.call_args[1]["files"] + self.assertEqual(list(files.keys()), ["photo"]) + self.assertEqual(files["photo"].name, self.test_unicode_file) + + @unittest.skipIf(sys.version >= '3', + "Python3 only uses unicode commandline arguments") + @mock.patch('trovebox.main.sys.getfilesystemencoding') + @mock.patch.object(trovebox.main.trovebox, "Trovebox") + @mock.patch('sys.stdout', new_callable=io.StringIO) + def test_post_utf8_files(self, _, mock_trovebox, mock_getfilesystemencoding): + """Check that utf-8 encoded filenames are posted correctly""" + post = mock_trovebox.return_value.post + # Make the system think its filesystemencoding is utf-8 + mock_getfilesystemencoding.return_value = "utf-8" + + file_param = "photo=@%s" % self.test_unicode_file + file_param = file_param.encode("utf-8") + + main(["-X", "POST", "-F", file_param]) + # It's not possible to directly compare the file object, + # so check it manually + files = post.call_args[1]["files"] + self.assertEqual(list(files.keys()), ["photo"]) + self.assertEqual(files["photo"].name, self.test_unicode_file) + @mock.patch.object(sys, "exit", raise_exception) @mock.patch('sys.stderr', new_callable=io.StringIO) def test_unknown_arg(self, mock_stderr):