Ejemplo n.º 1
0
class FTPTest(TestCase):
    def setUp(self):
        self.random_port = 0
        self.server = FTPStubServer(self.random_port)
        self.server.run()
        self.port = self.server.server.server_address[1]

    def tearDown(self):
        self.server.stop()

    def test_put_test_file(self):
        self.assertFalse(self.server.files("foo.txt"))
        ftp = FTP()
        ftp.set_debuglevel(0)
        ftp.connect('localhost', self.port)
        ftp.login('user1', 'passwd')

        ftp.storlines('STOR foo.txt', StringIO('cant believe its not bitter'))
        ftp.quit()
        ftp.close()
        self.assertTrue(self.server.files("foo.txt"))

    def test_put_2_files_associates_the_correct_content_with_the_correct_filename(self):
        ftp = FTP()
        ftp.connect('localhost', self.port)
        ftp.set_debuglevel(0)
        ftp.login('user2','other_pass')

        ftp.storlines('STOR robot.txt', StringIO("\n".join(["file1 content" for i in range(1024)])))
        ftp.storlines('STOR monster.txt', StringIO("file2 content"))
        ftp.quit()
        ftp.close()
        self.assertEquals("\r\n".join(["file1 content" for i in range(1024)]),
                          self.server.files("robot.txt").strip())
        self.assertEquals("file2 content", self.server.files("monster.txt").strip())

    def test_retrieve_expected_file_returns_file(self):
        expected_content = 'content of my file\nis a complete mystery to me.'
        self.server.add_file('foo.txt', expected_content)
        ftp = FTP()
        ftp.set_debuglevel(2)
        ftp.connect('localhost', self.port)
        ftp.login('chris', 'tarttelin')
        directory_content = []
        ftp.retrlines('LIST', lambda x: directory_content.append(x))
        file_content = []
        ftp.retrlines('RETR foo.txt', lambda x: file_content.append(x))
        ftp.quit()
        ftp.close()
        self.assertTrue('foo.txt' in '\n'.join(directory_content))
        self.assertEquals(expected_content, '\n'.join(file_content))
Ejemplo n.º 2
0
class FTPTest(TestCase):

    def setUp(self):
        self.port = 6666
        self.server = FTPStubServer(self.port)
        self.server.run()

    def tearDown(self):
        self.server.stop()

    def test_put_test_file(self):
        self.assertFalse(self.server.files("foo.txt"))
        ftp = FTP()
        ftp.set_debuglevel(0)
        ftp.connect('localhost', self.port)
        ftp.login('user1', 'passwd')

        ftp.storlines('STOR foo.txt', StringIO('cant believe its not bitter'))
        ftp.quit()
        ftp.close()
        self.assertTrue(self.server.files("foo.txt"))

    def test_put_2_files_associates_the_correct_content_with_the_correct_filename(self):
        ftp = FTP()
        ftp.connect('localhost', self.port)
        ftp.set_debuglevel(0)
        ftp.login('user2','other_pass')

        ftp.storlines('STOR robot.txt', StringIO("file1 content"*1024))
        ftp.storlines('STOR monster.txt', StringIO("file2 content"))
        ftp.quit()
        ftp.close()
        self.assertEquals("file1 content"*1024, self.server.files("robot.txt").strip())
        self.assertEquals("file2 content", self.server.files("monster.txt").strip())
        
    def test_retrieve_expected_file_returns_file(self):
        expected_content = 'content of my file\nis a complete mystery to me.'
        self.server.add_file('foo.txt', expected_content)
        ftp = FTP()
        ftp.set_debuglevel(2)
        ftp.connect('localhost', self.port)
        ftp.login('chris', 'tarttelin')
        directory_content = []
        ftp.retrlines('LIST', lambda x: directory_content.append(x))
        file_content = []
        ftp.retrlines('RETR foo.txt', lambda x: file_content.append(x))
        ftp.quit()
        ftp.close()
        self.assertTrue('foo.txt' in '\n'.join(directory_content))
        self.assertEquals(expected_content, '\n'.join(file_content))
Ejemplo n.º 3
0
class FTPTest(TestCase):
    def setUp(self):
        self.server = FTPStubServer(0)
        self.server.run()
        self.port = self.server.server.server_address[1]
        self.ftp = FTP()
        self.ftp.set_debuglevel(0)
        self.ftp.connect('localhost', self.port)
        self.ftp.login('user1', 'passwd')

    def tearDown(self):
        self.ftp.quit()
        self.ftp.close()
        self.server.stop()

    def test_change_directory(self):
        self.ftp.cwd('newdir')
        self.assertEqual(self.ftp.pwd(), 'newdir')

    def test_make_directory(self):
        prev_dir = self.ftp.pwd()
        self.ftp.mkd('newdir/newdir')
        self.assertEqual(self.ftp.pwd(), prev_dir)

    def test_put_test_file(self):
        self.assertFalse(self.server.files("foo.txt"))
        self.ftp.storlines('STOR foo.txt', BytesIO(b'cant believe its not bitter'))
        self.assertTrue(self.server.files("foo.txt"))

    def test_put_2_files_associates_the_correct_content_with_the_correct_filename(self):
        data = "\n".join(["file1 content" for i in range(1024)])
        self.ftp.storlines('STOR robot.txt', BytesIO(data.encode('utf-8')))
        self.ftp.storlines('STOR monster.txt', BytesIO(b'file2 content'))
        self.assertEqual("\r\n".join(["file1 content" for i in range(1024)]),
                          self.server.files("robot.txt").strip())
        self.assertEqual("file2 content", self.server.files("monster.txt").strip())

    def test_list_2_files(self):
        self.lines = []
        def accumulate(line):
            self.lines.append(line)

        self.ftp.storlines('STOR palladium.csv', BytesIO(b'data'))
        self.ftp.storlines('STOR vanadiyam.pdf', BytesIO(b'more data'))
        self.ftp.retrlines('LIST', accumulate)
        self.assertEqual(sorted(self.lines), sorted(['vanadiyam.pdf', 'palladium.csv']))

    def test_nlst_2_files(self):
        self.lines = []
        def accumulate(line):
            self.lines.append(line)

        self.ftp.storlines('STOR palladium.csv', BytesIO(b'data'))
        self.ftp.storlines('STOR vanadiyam.pdf', BytesIO(b'more data'))
        self.ftp.retrlines('NLST', accumulate)
        self.assertEqual(sorted(self.lines), sorted(['vanadiyam.pdf', 'palladium.csv']))

    def test_retrieve_expected_file_returns_file(self):
        expected_content = 'content of my file\nis a complete mystery to me.'
        self.server.add_file('foo.txt', expected_content)
        directory_content = []
        self.ftp.retrlines('LIST', lambda x: directory_content.append(x))
        file_content = []
        self.ftp.retrlines('RETR foo.txt', lambda x: file_content.append(x))
        self.assertTrue('foo.txt' in '\n'.join(directory_content))
        self.assertEqual(expected_content, '\n'.join(file_content))
Ejemplo n.º 4
0
class FTPTest(TestCase):
    def setUp(self):
        self.server = FTPStubServer(0)
        self.server.run()
        self.port = self.server.server.server_address[1]
        self.ftp = FTP()
        self.ftp.set_debuglevel(0)
        self.ftp.connect('localhost', self.port)
        self.ftp.login('user1', 'passwd')

    def tearDown(self):
        self.ftp.quit()
        self.ftp.close()
        self.server.stop()

    def test_change_directory(self):
        self.ftp.cwd('newdir')
        self.assertEqual(self.ftp.pwd(), 'newdir')

    def test_make_directory(self):
        prev_dir = self.ftp.pwd()
        self.ftp.mkd('newdir/newdir')
        self.assertEqual(self.ftp.pwd(), prev_dir)

    def test_put_test_file(self):
        self.assertFalse(self.server.files("foo.txt"))
        self.ftp.storlines('STOR foo.txt',
                           BytesIO(b'cant believe its not bitter'))
        self.assertTrue(self.server.files("foo.txt"))

    def test_put_2_files_associates_the_correct_content_with_the_correct_filename(
            self):
        data = "\n".join(["file1 content" for i in range(1024)])
        self.ftp.storlines('STOR robot.txt', BytesIO(data.encode('utf-8')))
        self.ftp.storlines('STOR monster.txt', BytesIO(b'file2 content'))
        self.assertEqual("\r\n".join(["file1 content" for i in range(1024)]),
                         self.server.files("robot.txt").strip())
        self.assertEqual("file2 content",
                         self.server.files("monster.txt").strip())

    def test_list_2_files(self):
        self.lines = []

        def accumulate(line):
            self.lines.append(line)

        self.ftp.storlines('STOR palladium.csv', BytesIO(b'data'))
        self.ftp.storlines('STOR vanadiyam.pdf', BytesIO(b'more data'))
        self.ftp.retrlines('LIST', accumulate)
        self.assertEqual(sorted(self.lines),
                         sorted(['vanadiyam.pdf', 'palladium.csv']))

    def test_nlst_2_files(self):
        self.lines = []

        def accumulate(line):
            self.lines.append(line)

        self.ftp.storlines('STOR palladium.csv', BytesIO(b'data'))
        self.ftp.storlines('STOR vanadiyam.pdf', BytesIO(b'more data'))
        self.ftp.retrlines('NLST', accumulate)
        self.assertEqual(sorted(self.lines),
                         sorted(['vanadiyam.pdf', 'palladium.csv']))

    def test_retrieve_expected_file_returns_file(self):
        expected_content = 'content of my file\nis a complete mystery to me.'
        self.server.add_file('foo.txt', expected_content)
        directory_content = []
        self.ftp.retrlines('LIST', lambda x: directory_content.append(x))
        file_content = []
        self.ftp.retrlines('RETR foo.txt', lambda x: file_content.append(x))
        self.assertTrue('foo.txt' in '\n'.join(directory_content))
        self.assertEqual(expected_content, '\n'.join(file_content))
Ejemplo n.º 5
0
class FTPTest(TestCase):
    def setUp(self):
        self.server = FTPStubServer(0)
        self.server.run()
        self.port = self.server.server.server_address[1]
        self.ftp = FTP()
        self.ftp.set_debuglevel(0)
        self.ftp.connect("localhost", self.port)
        self.ftp.login("user1", "passwd")

    def tearDown(self):
        self.ftp.quit()
        self.ftp.close()
        self.server.stop()

    def test_change_directory(self):
        self.ftp.cwd("newdir")
        self.assertEqual(self.ftp.pwd(), "newdir")

    def test_put_test_file(self):
        self.assertFalse(self.server.files("foo.txt"))
        self.ftp.storlines("STOR foo.txt", BytesIO(b"cant believe its not bitter"))
        self.assertTrue(self.server.files("foo.txt"))

    def test_put_2_files_associates_the_correct_content_with_the_correct_filename(self):
        data = "\n".join(["file1 content" for i in range(1024)])
        self.ftp.storlines("STOR robot.txt", BytesIO(data.encode("utf-8")))
        self.ftp.storlines("STOR monster.txt", BytesIO(b"file2 content"))
        self.assertEqual("\r\n".join(["file1 content" for i in range(1024)]), self.server.files("robot.txt").strip())
        self.assertEqual("file2 content", self.server.files("monster.txt").strip())

    def test_list_2_files(self):
        self.lines = []

        def accumulate(line):
            self.lines.append(line)

        self.ftp.storlines("STOR palladium.csv", BytesIO(b"data"))
        self.ftp.storlines("STOR vanadiyam.pdf", BytesIO(b"more data"))
        self.ftp.retrlines("LIST", accumulate)
        self.assertEqual(sorted(self.lines), sorted(["vanadiyam.pdf", "palladium.csv"]))

    def test_nlst_2_files(self):
        self.lines = []

        def accumulate(line):
            self.lines.append(line)

        self.ftp.storlines("STOR palladium.csv", BytesIO(b"data"))
        self.ftp.storlines("STOR vanadiyam.pdf", BytesIO(b"more data"))
        self.ftp.retrlines("NLST", accumulate)
        self.assertEqual(sorted(self.lines), sorted(["vanadiyam.pdf", "palladium.csv"]))

    def test_retrieve_expected_file_returns_file(self):
        expected_content = "content of my file\nis a complete mystery to me."
        self.server.add_file("foo.txt", expected_content)
        directory_content = []
        self.ftp.retrlines("LIST", lambda x: directory_content.append(x))
        file_content = []
        self.ftp.retrlines("RETR foo.txt", lambda x: file_content.append(x))
        self.assertTrue("foo.txt" in "\n".join(directory_content))
        self.assertEqual(expected_content, "\n".join(file_content))