-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
57 lines (47 loc) · 1.69 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# !/usr/bin/env python3
import os
import tempfile
import unittest
import unittest.mock
import ftp
from stubserver import FTPStubServer
class FTPTests(unittest.TestCase):
def setUp(self):
self.server = FTPStubServer(0)
self.server.run()
self.port = self.server.server.server_address[1]
self.ftp = ftp.ftp()
self.ftp.connect('127.0.0.1', self.port)
def tearDown(self):
self.ftp.close()
self.server.stop()
def test_connection(self):
serv_ans = self.ftp.ftp_ans()
self.assertEqual(serv_ans, "220 (FtpStubServer 0.1a)\r\n")
def test_login(self):
self.ftp.ftp_ans()
serv_ans = self.ftp.login()
self.assertEqual(serv_ans, "331 Please specify password.\r\n230 You are now logged in.\r\n")
def test_list(self):
self.ftp.ftp_ans()
self.server.add_file("1.txt", "abracarabra")
serv_ans = self.ftp.ls()
self.assertEqual(serv_ans, "1.txt")
def test_download(self):
self.ftp.ftp_ans()
self.server.add_file("1.txt", "abracarabra")
with unittest.mock.patch.object(self.ftp, 'size', return_value=100):
self.ftp.download_file("1.txt")
with open("1.txt", "r") as file:
ans = file.read()
self.assertEqual(ans, "abracarabra")
def test_pasv(self):
reply = self.ftp.ftp_ans()
reply = self.ftp.pasv()
self.assertTrue(reply.startswith("227 Entering Passive Mode."))
def test_cd(self):
dir_name = "new_dir"
self.ftp.cd(dir_name)
self.assertEqual(self.ftp.send_comand("PWD"), '250 OK. Current directory is "' + dir_name + '"\r\n')
if __name__ == '__main__':
unittest.main()