def test_move_as_move_folder(): """ Tries to move one folder into another. Creates one more folder. Tries to move folder into it. Checks for response code: 250 @requires: test_move_as_file to pass """ expected = '250' folder = os.path.join(test_dir, 'second_transfer') if not os.path.exists(folder): os.mkdir(folder) f = OneDirFtpClient(ip, user, pw, test_dir) f.mkd('second_transfer') to_folder = os.path.abspath(folder) f.move('first_transfer', to_folder) actual = f.lastresp nlst = f.nlst('second_transfer') f.quit() f.close() if os.path.split(uploaded_file)[1] in nlst: n_eq(expected, actual, f, 'move') else: print 'nlst:', nlst n_ok(False, f, 'move', message='File not moved')
def test_list_dir_path(): """ Creates a folder and puts a file in it. Checks for file. Checks for response code: 226 @requires: delete_folder to pass @requires: upload to pass """ expected = '226' dir_name = 'ListTest' file_name = 'temp.txt' the_dir = os.path.join(test_dir, dir_name) os.mkdir(the_dir) the_file = os.path.join(the_dir, file_name) with open(the_file, 'w') as w: w.write('words') f = OneDirFtpClient(ip, user, pw, test_dir) f.mkd(dir_name) f.upload(the_file) the_list = f.list_dir(the_dir) actual = f.lastresp f.delete_folder(the_dir) f.quit() f.close() if os.path.exists(the_dir): rmtree(the_dir) if file_name in the_list: n_eq(expected, actual, f, 'list_dir') else: n_ok(False, f, 'list_dir', message='404')
def test_cd(): """ Tests the cd command Checks for response code: 250 Checks the directory name: @required: delete_folder to pass """ expected= '250' dir_name = 'TestCd' the_dir = os.path.join(test_dir, dir_name) os.mkdir(the_dir) f = OneDirFtpClient(ip, user, pw, test_dir) f.mkd(dir_name) f.cd(the_dir) actual = f.lastresp in_dir = f.pwd() f.cwd('..') f.delete_folder(the_dir) f.quit() f.close() if os.path.exists(the_dir): rmtree(the_dir) if in_dir.strip('/') == dir_name: n_eq(expected, actual, f, 'cd') else: print 'Expected:', dir_name, 'Actual:', in_dir n_ok(False, f, 'cd', message="Dir names don't match")
def test_mkdir(): """ Test making a new directory based on local directory. Checks for response code: 257 Checks for folder on server. @requires: list_dir to pass @requires: delete_folder to pass """ expected = '257' dir_name = 'TestMkDir' the_dir = os.path.join(test_dir, dir_name) os.mkdir(the_dir) f = OneDirFtpClient(ip, user, pw, test_dir) f.mkdir(the_dir) actual = f.lastresp the_list = f.list_dir() f.delete_folder(the_dir) f.quit() f.close() if os.path.exists(the_dir): rmtree(the_dir) if dir_name in the_list: n_eq(expected, actual, f, 'mkdir') else: n_ok(False, f, 'mkdir', message='folder not found')
def test_sm_connect_existing_db(): """ Tries to connect to a db that already exists """ s = SqlManager(db_name) s.connect() actual = s.con is None s.disconnect() n_ok(not actual, s, 'connect')
def test_sm_disconnect(): """ Checks that the db is disconnecting properly """ s = SqlManager(db_name) s.connect() s.disconnect() actual = s.con is None n_ok(actual, s, 'disconnect')
def test_ta_not_exists(): """ Checks that it raise a name error if the table does not exists """ table_name = 'def' t = TableAdder(db_name, table_name) actual = t.done del t n_ok(not actual, message='Table not added yet')
def test_tm_exists(): """ Tries to connect to a table that exists """ t = TableManager(db_name, table_name) check_name = [val[0] for val in column_map] check_type = [val[1] for val in column_map] actual = check_name == t.table_col_names and check_type == t.table_col_type n_ok(actual, message='Checks table loaded correctly')
def test_cd(): """ Changers Current working Dir to 'test_fs' """ diff_dir = '/test_fs' before = ftp.pwd() ftp.cwd(diff_dir) after = ftp.pwd() actual = (not before == after) and (after == diff_dir) n_ok(actual)
def test_sm_connect_new_db(): """ Tries to connect to a db that does not exists """ db = 'test_2.db' s = SqlManager(db) actual = os.path.isfile(db) if actual: os.remove(db) del s n_ok(actual, message='test_2.db was not created')
def test_tr_exists(): """ Test it can remove a table that exists """ s = SqlManager(db_name) if not table_name in s.tables: raise Exception('Bad Setup') del s TableRemover(db_name, table_name) s = SqlManager(db_name) actual = table_name in s.tables n_ok(not actual, message='table should be gone')
def test_ta_commit(): """ Checks that the table is added to the database @precondition: SqlManager all test - Pass """ table_name = 'def' t = TableAdder(db_name, table_name) t.add_column('text_column') t.add_column('int_column', 'integer') t.commit() s = SqlManager(db_name) actual = table_name in s.tables n_ok(actual, t, 'commit', message='finally table added')
def test_rename(): """ Renames a file on the server. """ f_name = 'filler.txt' with open(f_name, 'w') as w: w.write('some text for the file') u1_ftp.storlines('STOR %s' % f_name, open(f_name, 'rb')) if os.path.isfile(f_name): os.remove(f_name) u1_ftp.rename(f_name, 'abc.txt') rep = u1_ftp.lastresp x = u1_ftp.nlst() actual = (rep == '250') and ('abc.txt' in x) n_ok(actual)
def test_user_list(): """ Gets a list of all the users Checks for responce code: 226 Checks a line representing admin exists. """ expected = '226' ftp.retrlines('site userlist', callback) actual = ftp.lastresp line = "('admin', 1, 'welcome', 'goodbye')" if line in received: n_eq(expected, actual) else: print received n_ok(False, message='line not recieved')
def test_get_log(): """ Gives the location of log. A normal download command can be used to download it. Checks for responce code: 200 Check the name of the log. """ expected = '200' log = ftp.sendcmd('site getlog') actual = ftp.lastresp log = log.split('/')[-1] if log == 'pyftpd.log': n_eq(expected, actual) else: print log n_ok(False)
def test_list_dir_current(): # NOTE: I am thinking of killing this so it is not allowed, thoughts? """ Lists the files in the folder, Since the testing server runs on 'testing_server.py' it will check for that Checks for response code: 226 """ expected = '226' f = OneDirFtpClient(ip, user, pw, test_dir) files = f.list_dir() actual = f.lastresp f.quit() f.close() if 'testing_server.py' in files: n_eq(expected, actual, f, 'list_dir') else: n_ok(False, f, 'list_dir', message='404')
def test_download(): """ Attempts to download a file """ u1_ftp.cwd('upload_folder') f_name = 'uploaded.txt' download_file = open(f_name, 'wb') u1_ftp.retrlines('RETR %s' % f_name, download_file.write) download_file.close() if os.path.isfile(f_name): os.remove(f_name) else: n_ok(False) actual = u1_ftp.lastresp expected = '226' n_eq(actual, expected)
def test_delete_full_folder(): """ Makes a dir fills it then deletes it while it is full. """ u1_ftp.voidcmd('mkd %s' % 'temp_dir') u1_ftp.cwd('temp_dir') f_name = 'filler.txt' with open(f_name, 'w') as w: w.write('some text for the file') u1_ftp.storlines('STOR %s' % f_name, open(f_name, 'rb')) if os.path.isfile(f_name): os.remove(f_name) u1_ftp.cwd('..') rec_delete(u1_ftp, 'temp_dir') rep = u1_ftp.nlst() actual = not 'temp_dir' in rep n_ok(actual)
def test_user_add(): """ Attempts to create a user @requires: user_list to pass. Checks for responce code: 200 Checks that user is in database. """ expected = '200' ftp.sendcmd('site useradd user_one 0 %s' % password) actual = ftp.lastresp ftp.retrlines('site userlist', callback) line = "('user_one', 0, 'welcome', 'goodbye')" if line in received: n_eq(expected, actual) else: print received n_ok(False, message='User not added')
def test_user_del(): """ Attempts to delete a user. @requires: user_add to pass. Check for responce code: 200 Checks that user is not in database. """ expected = '200' ftp.sendcmd('site userdel user_one') actual = ftp.lastresp line = "('user_one', 0, 'welcome', 'goodbye')" ftp.retrlines('site userlist', callback) if not line in received: n_eq(expected, actual) else: print received n_ok(False, message='user not deleted')
def test_i_am(): """ Sets secondary username. Checks for responce code: 200 Checks that the name was set. @requres: who am i to pass """ expected = '200' ftp.sendcmd('site iam abc') actual = ftp.lastresp x = ftp.sendcmd('site whoami') x = x.split(':') x[0] = x[0].split(' ')[1] if x[0] == 'admin' and x[1] == 'abc': n_eq(expected, actual) else: print x n_ok(False, message='name not set')
def test_admin_change_password(): """ Creates a new users. Changes new user password. Attempts to log in with new password. Checks for responce code: 200 """ expected = '200' ftp.sendcmd('site useradd %s 0 %s' % (usr, password)) ftp.sendcmd('site changepw %s %s' % (usr, new_pw)) actual = ftp.lastresp f = FTP() f.connect(ip) try: f.login(usr, new_pw) n_eq(expected, actual) except: n_ok(False, message='Authentication Failed')
def test_tm_clear_table(): """ @precondition: test_tm_with_enter passed @precondition: test_tm_with_exit passed @precondition: test_tm_with_exit_error passed Tries to clear the table of all data """ with TableManager(db_name, table_name) as t: t.clear_table() s = SqlManager(db_name) command = 'SELECT * FROM ' + table_name + ';' s.connect() data = s._fetch_command(command) s.disconnect() actual = False if len(data) == 0: actual = True n_ok(actual, message=actual)
def test_download_image(): """ Tries to download a image from the server. Checks for response code: 226 Checks that the file has been downloaded. @requires: test_upload_image to pass """ expected = '226' if os.path.isfile(test_image): os.remove(test_image) f = OneDirFtpClient(ip, user, pw, test_dir) f.download(test_image) actual = f.lastresp f.quit() f.close() if os.path.isfile(test_image): n_eq(expected, actual, f, 'download') else: n_ok(False, f, 'download', message='File not downloaded')
def test_delete_full_folder(): """ Tries to delete a folder with items in it. Checks for response code: 250 Check for folder in dir """ expected = '250' f = OneDirFtpClient(ip, user, pw, test_dir) folder = os.path.join(test_dir, 'second_transfer') f.delete_folder(folder) actual = f.lastresp nlst = f.nlst() f.quit() f.close() if not 'second_transfer' in nlst: n_eq(expected, actual, f, 'delete_folder') else: print 'nlst:', nlst n_ok(False, f, 'delete_folder', message='Folder in nlst')
def test_get_time(): """ Tries to get the server time. Checks it length (for formatting) Checks for responce code: 200 Tries to cast time to an int. """ expected = '200' x = ftp.sendcmd('site gettime') actual = ftp.lastresp x = x.split(' ')[1] if len(x) == 20: int(x) global start start = x n_eq(expected, actual) else: print 'length: ', len(x) print 'value', x n_ok(False)
def test_upload_file(): """ Creates a text file, and tries to upload it to the server. Checks for response code: 226 Checks for file on server. """ expected = '226' with open(uploaded_file, 'w') as w: w.write('some text for the file') f = OneDirFtpClient(ip, user, pw, test_dir) f.upload(uploaded_file) actual = f.lastresp nlst = f.nlst() f.quit() f.close() if os.path.split(uploaded_file)[1] in nlst: n_eq(expected, actual, f, 'upload') else: print 'nlst:', nlst n_ok(False, f, 'upload', message='Not in NLST')
def test_delete_file(): """ Tries to delete a file ofter the server. Checks for response code: Checks the file is gone @requires: test_move_as_rename to pass """ expected = '250' f = OneDirFtpClient(ip, user, pw, test_dir) after_name = os.path.join(test_dir, 'after_name.txt') f.delete_file(after_name) actual = f.lastresp nlst = f.nlst() f.quit() f.close() if not 'after_name.txt' in nlst: n_eq(expected, actual, f, 'delete_file') else: print 'nlst:', nlst n_ok(False, f, 'delete_file', message='File still in nlst')
def test_download_file(): """ Tries to download a file from the server. Checks for response code: 226 Checks if file exists. Note: This will downloaded the file into this folder not the testing folder. @requires: test_upload_file to pass. """ expected = '226' if os.path.isfile(uploaded_file): os.remove(uploaded_file) f = OneDirFtpClient(ip, user, pw, test_dir) f.download(uploaded_file) actual = f.lastresp f.quit() f.close() if os.path.isfile(uploaded_file): n_eq(expected, actual, f, 'download') else: n_ok(False, f, 'download', message='File not downloaded')
def test_who_am_i(): """ Ask the server for its secondary username. Checks to see that it is ip adress still. Checks for responce code: 200 """ expected = '200' x = ftp.sendcmd('site whoami') actual = ftp.lastresp x = x.split(':')[-1] x = x.split('.') checker = True for y in x: try: int(y) except: checker = False break if checker: n_eq(expected, actual) else: n_ok(checker, message='not a ip')
def test_delete_empty_folder(): """ Tries to delete an empty folder Checks for response code: 250 Checks that the file is gone """ expected = '250' folder = os.path.join(test_dir, 'delete_this') if not os.path.exists(folder): os.mkdir(folder) f = OneDirFtpClient(ip, user, pw, test_dir) f.mkd('delete_this') f.delete_folder(folder) actual = f.lastresp nlst = f.nlst() f.quit() f.close() if not 'delete_this' in nlst: n_eq(expected, actual, f, 'delete_folder') else: print 'nlst:', nlst n_ok(False, f, 'delete_folder', message='Folder in nlst')
def test_user_change_password(): """ User logs in, and attemps to change their own password. @requires: admin change password to work. User logs out and then attemps to log in with new password. Checks for responce code: 200 """ expected = '200' f = FTP() f.connect(ip) f.login(usr, new_pw) pw_three = 'hij' f.sendcmd('site setpw %s %s' % (new_pw, pw_three)) actual = f.lastresp f.quit() f.close() f.connect(ip) try: f.login(usr, pw_three) n_eq(expected, actual) except: n_ok(False, message='Authentication Failed')
def test_set_flag_one_arg(): """ Set a single arg flag in the database. Checks for responce code: 200 Check for flag in sync. @require: sync to pass. """ expected = '200' ftp.sendcmd('site setflag abc') actual = ftp.lastresp print start ftp.retrlines('site sync %s' % start, callback) check = False for r in reversed(received): x = eval(r) if x[2] == 'FLAG': if x[3] == 'abc' and x[4] == 'abc': check = True break if check: n_eq(expected, actual) else: n_ok(check, message='Flag not found')
def test_move_as_rename(): """ Uploads a file and then tries to rename it Tries to rename a file. Check for response code: Checks ls for new file name @requires: test_upload to pass """ expected = '250' before_name = os.path.join(test_dir, 'before_name.txt') with open(before_name, 'wb') as w: w.write('some text') f = OneDirFtpClient(ip, user, pw, test_dir) f.upload(before_name) after_name = os.path.join(test_dir, 'after_name.txt') f.move(before_name, after_name) actual = f.lastresp nlst = f.nlst() f.quit() f.close() if 'after_name.txt' in nlst and not 'before_name.txt' in nlst: n_eq(expected, actual, f, 'move') else: print "nlst:", nlst n_ok(False, f, 'move', message='File not in nlst')