def test_files_post_with_bad_md5(self): """ Test upload with bad md5. """ user_relative_upload_filepath = 'testupload/testfile.txt' upload_test_url = SERVER_FILES_API + user_relative_upload_filepath uploaded_filepath = userpath2serverpath(USR, user_relative_upload_filepath) assert not os.path.exists(uploaded_filepath), '"{}" file is existing'.format(uploaded_filepath) # Create temporary file for test test_file, not_used_md5 = _make_temp_file() # Create fake md5 and send it instead the right md5 fake_md5 = 'sent_bad_md5' try: test = self.app.post(upload_test_url, headers=make_basicauth_headers(USR, PW), data={'file': test_file, 'md5': fake_md5}, follow_redirects=True) finally: test_file.close() self.assertEqual(test.status_code, server.HTTP_CONFLICT) self.assertFalse(os.path.isfile(userpath2serverpath(USR, user_relative_upload_filepath))) # check that uploaded path NOT exists in username files dict self.assertNotIn(user_relative_upload_filepath, server.userdata[USR][server.SNAPSHOT])
def test_files_put_with_auth(self): """ Test put. File content and stored md5 must be changed. """ path = 'test_put/file_to_change.txt' _create_file(USR, path, 'I will change') to_modify_filepath = userpath2serverpath(USR, path) old_content = open(to_modify_filepath).read() old_md5 = server.userdata[USR][server.SNAPSHOT][path][1] url = SERVER_FILES_API + path # Create temporary file for test test_file, not_used_md5 = _make_temp_file() # Create fake md5 and send it instead the right md5 fake_md5 = 'sent_bad_md5' try: test = self.app.put(url, headers=make_basicauth_headers(USR, PW), data={'file': test_file, 'md5': fake_md5}, follow_redirects=True) finally: test_file.close() new_content = open(to_modify_filepath).read() self.assertEqual(old_content, new_content) new_md5 = server.userdata[USR][server.SNAPSHOT][path][1] self.assertEqual(old_md5, new_md5) self.assertEqual(test.status_code, server.HTTP_CONFLICT)
def build_tstuser_dir(username): """ Create a directory with files and return its structure in a list. :param username: str :return: tuple """ # md5("foo") = "acbd18db4cc2f85cedef654fccc4a4d8" # md5("bar") = "37b51d194a7513e45b56f6524f2d51f2" # md5("spam") = "e09f6a7593f8ae3994ea57e1117f67ec" file_contents = [ ('spamfile', 'spam', 'e09f6a7593f8ae3994ea57e1117f67ec'), (os.path.join('subdir', 'foofile.txt'), 'foo', 'acbd18db4cc2f85cedef654fccc4a4d8'), (os.path.join('subdir', 'barfile.md'), 'bar', '37b51d194a7513e45b56f6524f2d51f2'), ] user_root = userpath2serverpath(username) # If directory already exists, destroy it if os.path.isdir(user_root): shutil.rmtree(user_root) os.mkdir(user_root) expected_timestamp = None expected_snapshot = {} for user_filepath, content, md5 in file_contents: expected_timestamp = int(_create_file(username, user_filepath, content)) expected_snapshot[user_filepath] = [expected_timestamp, unicode(md5)] return expected_timestamp, expected_snapshot
def create_user_dir(username): """ Create user directory (must not exist) :param username: :return: """ os.makedirs(userpath2serverpath(username))
def test_files_put_with_bad_md5(self): """ Test modify with bad md5. """ path = 'test_put/file_to_change.txt' _create_file(USR, path, 'I will NOT change') to_modify_filepath = userpath2serverpath(USR, path) old_content = open(to_modify_filepath).read() old_md5 = server.userdata[USR][server.SNAPSHOT][path][1] url = SERVER_FILES_API + path # Create temporary file for test test_file, test_md5 = _make_temp_file() try: test = self.app.put(url, headers=make_basicauth_headers(USR, PW), data={'file': test_file, 'md5': test_md5}, follow_redirects=True) finally: test_file.close() new_content = open(to_modify_filepath).read() self.assertNotEqual(old_content, new_content) new_md5 = server.userdata[USR][server.SNAPSHOT][path][1] self.assertNotEqual(old_md5, new_md5) self.assertEqual(test.status_code, server.HTTP_CREATED) # 200 or 201 (OK or created)?
def test_files_post_with_existent_path(self): """ Test the creation of file that already exists. """ path = 'test_put/file_to_change.txt' # path already existent _create_file(USR, path, 'I already exist! Don\'t erase me!') to_created_filepath = userpath2serverpath(USR, path) old_content = open(to_created_filepath).read() old_md5 = server.userdata[USR][server.SNAPSHOT][path][1] url = SERVER_FILES_API + path # Create temporary file for test test_file, test_md5 = _make_temp_file() try: test = self.app.post(url, headers=make_basicauth_headers(USR, PW), data={'file': test_file, 'md5': test_md5}, follow_redirects=True) finally: test_file.close() self.assertEqual(test.status_code, server.HTTP_FORBIDDEN) new_content = open(to_created_filepath).read() self.assertEqual(old_content, new_content) new_md5 = server.userdata[USR][server.SNAPSHOT][path][1] self.assertEqual(old_md5, new_md5)
def test_delete_user(self): """ User deletion. """ # Creating user to delete on-the-fly (TODO: pre-load instead) _manually_create_user(USR, PW) user_dirpath = userpath2serverpath(USR) # Really created? assert USR in server.userdata, 'Utente "{}" non risulta tra i dati'.format(USR) # TODO: translate assert os.path.exists(user_dirpath), 'Directory utente "{}" non trovata'.format(USR) # TODO: translate # Test FORBIDDEN case (removing other users) url = SERVER_API + 'users/' + 'otheruser' test = self.app.delete(url, headers=make_basicauth_headers(USR, PW)) self.assertEqual(test.status_code, server.HTTP_FORBIDDEN) # Test OK case url = SERVER_API + 'users/' + USR test = self.app.delete(url, headers=make_basicauth_headers(USR, PW)) self.assertNotIn(USR, server.userdata) self.assertEqual(test.status_code, server.HTTP_OK) self.assertFalse(os.path.exists(user_dirpath))
def test_consistence_after_actions(self): """ Complex test that do several actions and finally test the consistence. """ # create user user = '******' _manually_create_user(user, 'pass') # post _create_file(user, 'new_file', 'ciao!!!') url = SERVER_FILES_API + 'new_file' self.app.post(url, headers={'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(USR, PW))}) # move move_test_url = SERVER_ACTIONS_API + 'move' src_move_test_file_path = 'test_move_src/testmovesrc.txt' dst_move_test_file_path = 'test_move_dst/testmovedst.txt' #create source file to be moved and its destination _create_file(user, src_move_test_file_path, 'this is the file to be moved') test = self.app.post(move_test_url, headers={'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(user, 'pass'))}, data={'src': src_move_test_file_path, 'dst': dst_move_test_file_path}, follow_redirects=True) # copy copy_test_url = SERVER_FILES_API + 'copy' test = self.app.post(copy_test_url, headers={'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(user, 'pass'))}, data={'src': src_move_test_file_path, 'dst': dst_move_test_file_path}, follow_redirects=True) # intermediate check dic_state, dir_state = get_dic_dir_states() self.assertEqual(dic_state, dir_state) # create other user user, pw = 'pluto', 'pw' _manually_create_user(user, pw) # post a file path = 'dir/dirfile.txt' _create_file(user, path, 'dirfile content...') self.app.post(SERVER_FILES_API + path, headers={'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(user, pw))}) # delete it self.app.post(SERVER_FILES_API + 'delete', headers={'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(user, pw))}, data={'filepath': path}) # final check dic_state, dir_state = get_dic_dir_states() self.assertEqual(dic_state, dir_state) # Now I manually delete a file in the server and must be NOT synchronized! os.remove(userpath2serverpath(user, 'WELCOME')) dic_state, dir_state = get_dic_dir_states() self.assertNotEqual(dic_state, dir_state) # NOT EQUAL
def setUp(self): setup_test_dir() server.reset_userdata() self.app = server.app.test_client() self.app.testing = True self.username = USR self.password = PW self.user_dirpath = userpath2serverpath(self.username)
def test_files_post_with_not_allowed_path(self): """ Test that creating a directory upper than the user root is not allowed. """ user_filepath = '../../../test/myfile2.dat' # path forbidden url = SERVER_FILES_API + user_filepath test = self.app.post(url, headers={'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(USR, PW))}, data=dict(file=(io.BytesIO(b'this is a test'), 'test.pdf'),), follow_redirects=True) self.assertEqual(test.status_code, server.HTTP_FORBIDDEN) self.assertFalse(os.path.isfile(userpath2serverpath(USR, user_filepath)))
def test_unexisting_username(self): """ Not existing username and existing activation_code. """ unexisting_user = '******' test = self.app.put(urlparse.urljoin(SERVER_API, 'users/' + unexisting_user), data={'activation_code': self.activation_code}) self.assertEqual(test.status_code, HTTP_NOT_FOUND) self.assertNotIn(unexisting_user, server.userdata.keys()) self.assertFalse(os.path.exists(userpath2serverpath(unexisting_user)))
def _manually_remove_user(username): # TODO: make this from server module? """ Remove user dictionary from server <userdata>, if exist, and remove its directory from disk, if exist. :param username: str """ if USR in server.userdata: server.userdata.pop(username) # Remove user directory if exists! user_dirpath = userpath2serverpath(USR) if os.path.exists(user_dirpath): shutil.rmtree(user_dirpath) logging.debug('"%s" user directory removed' % user_dirpath)
def get_dic_dir_states(): """ Return a tuple with dictionary state and directory state of all users. NB: Passwords are removed from the dictionary states. :return: tuple """ dic_state = {} dir_state = {} for username in server.userdata: single_user_data = server.userdata[username].copy() single_user_data.pop(server.PASSWORD) # not very beautiful dic_state[username] = single_user_data dir_state[username] = server.compute_dir_state(userpath2serverpath(username)) return dic_state, dir_state
def test_copy_file_path_with_unexisting_destinationfile(self): """ Test the creation of a destination file if this one doesn't exists from the beginning. """ copy_test_url = SERVER_ACTIONS_API + 'copy' src_copy_test_file_path = 'test_copy_src/testcopysrc.txt' dst_copy_test_file_path = 'test_copy_dst/testcopydst.txt' # Create source file to be copied and its destination. src_copy_filepath = userpath2serverpath(USR, src_copy_test_file_path) _create_file(USR, src_copy_test_file_path, 'this is the file to be copied') test = self.app.post(copy_test_url, headers=make_basicauth_headers(USR, PW), data={'src': src_copy_test_file_path, 'dst': dst_copy_test_file_path}, follow_redirects=True) self.assertEqual(test.status_code, server.HTTP_OK)
def test_delete_file_path(self): """ Test if a created file is deleted and assures it doesn't exists anymore with assertFalse """ # create file to be deleted delete_test_url = SERVER_ACTIONS_API + 'delete' delete_test_file_path = 'testdelete/testdeletefile.txt' to_delete_filepath = userpath2serverpath(USR, delete_test_file_path) _create_file(USR, delete_test_file_path, 'this is the file to be deleted') test = self.app.post(delete_test_url, headers=make_basicauth_headers(USR, PW), data={'filepath': delete_test_file_path}, follow_redirects=True) self.assertEqual(test.status_code, server.HTTP_OK) self.assertFalse(os.path.isfile(to_delete_filepath)) self.assertNotIn(delete_test_file_path, server.userdata[USR][server.SNAPSHOT])
def setUp(self): setup_test_dir() server.reset_userdata() self.app = server.app.test_client() self.app.testing = True self.username = USR self.password = PW self.user_dirpath = userpath2serverpath(self.username) assert self.username not in server.userdata assert not os.path.exists(self.user_dirpath) # The Users.post (signup request) is repeatable resp = self.app.post(urlparse.urljoin(SERVER_API, 'users/' + self.username), data={'password': self.password}) # Retrieve the generated activation code self.activation_code = server.userdata[self.username][server.USER_CREATION_DATA]['activation_code']
def test_move_file_path(self): """ Test if a created source file is moved in a new created destination and assures the source file doesn't exists after """ move_test_url = SERVER_ACTIONS_API + 'move' src_move_test_file_path = 'test_move_src/testmovesrc.txt' dst_move_test_file_path = 'test_move_dst/testmovedst.txt' #create source file to be moved and its destination src_move_filepath = userpath2serverpath(USR, src_move_test_file_path) _create_file(USR, src_move_test_file_path, 'this is the file to be moved') test = self.app.post(move_test_url, headers={'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(USR, PW))}, data={'src': src_move_test_file_path, 'dst': dst_move_test_file_path}, follow_redirects=True) self.assertEqual(test.status_code, server.HTTP_OK) self.assertFalse(os.path.isfile(src_move_filepath))
def test_files_post_with_not_allowed_path(self): """ Test that creating a directory upper than the user root is not allowed. """ user_filepath = '../../../test/myfile2.dat' # path forbidden url = SERVER_FILES_API + user_filepath # Create temporary file for test test_file, test_md5 = _make_temp_file() try: test = self.app.post(url, headers=make_basicauth_headers(USR, PW), data={'file': test_file, 'md5': test_md5}, follow_redirects=True) finally: test_file.close() self.assertEqual(test.status_code, server.HTTP_FORBIDDEN) self.assertFalse(os.path.isfile(userpath2serverpath(USR, user_filepath))) # check that uploaded path NOT exists in username files dict self.assertNotIn(user_filepath, server.userdata[USR][server.SNAPSHOT])
def test_files_post_with_auth(self): """ Test for authenticated upload. """ user_relative_upload_filepath = 'testupload/testfile.txt' upload_test_url = SERVER_FILES_API + user_relative_upload_filepath uploaded_filepath = userpath2serverpath(USR, user_relative_upload_filepath) assert not os.path.exists(uploaded_filepath), '"{}" file is existing'.format(uploaded_filepath) test = self.app.post(upload_test_url, headers={'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(USR, PW))}, data=dict(file=(io.BytesIO(b'this is a test'), 'test.pdf'),), follow_redirects=True) self.assertEqual(test.status_code, server.HTTP_CREATED) self.assertTrue(os.path.isfile(uploaded_filepath)) # check that uploaded path exists in username files dict self.assertIn(user_relative_upload_filepath, server.userdata[USR][server.SNAPSHOT]) os.remove(uploaded_filepath) logging.info('"{}" removed'.format(uploaded_filepath))
def test_signup(self): """ Test for registration of a new user. """ test = self.app.post(urlparse.urljoin(SERVER_API, 'signup'), data={'username': USR, 'password': PW}) # test that userdata is actually updated single_user_data = server.userdata[USR] self.assertIn(USR, server.userdata) # test single user data structure (as currently defined) self.assertIsInstance(single_user_data, dict) self.assertIn(server.LAST_SERVER_TIMESTAMP, single_user_data) self.assertIn(server.SNAPSHOT, single_user_data) self.assertIsInstance(single_user_data[server.LAST_SERVER_TIMESTAMP], int) self.assertIsInstance(single_user_data[server.SNAPSHOT], dict) # test that the user directory is created user_dirpath = userpath2serverpath(USR) self.assertTrue(os.path.isdir(user_dirpath)) # test server response self.assertEqual(test.status_code, server.HTTP_CREATED)
def test_files_put_with_auth(self): """ Test put. File content and stored md5 must be changed. """ path = 'test_put/file_to_change.txt' _create_file(USR, path, 'I will change') to_modify_filepath = userpath2serverpath(USR, path) old_content = open(to_modify_filepath).read() old_md5 = server.userdata[USR][server.SNAPSHOT][path][1] url = SERVER_FILES_API + path test = self.app.put(url, headers={'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(USR, PW))}, data=dict(file=(io.BytesIO(b'I have changed'), 'foo.foo')), follow_redirects=True) new_content = open(to_modify_filepath).read() self.assertNotEqual(old_content, new_content) new_md5 = server.userdata[USR][server.SNAPSHOT][path][1] self.assertNotEqual(old_md5, new_md5) self.assertEqual(test.status_code, server.HTTP_CREATED) # 200 or 201 (OK or created)?
def _create_file(username, user_relpath, content, update_userdata=True): """ Create an user file with path <user_relpath> and content <content> and return it's last modification time (== creation time). :param username: str :param user_relpath: str :param content: str :return: float """ filepath = userpath2serverpath(username, user_relpath) dirpath = os.path.dirname(filepath) if not os.path.isdir(dirpath): os.makedirs(dirpath) with open(filepath, 'wb') as fp: fp.write(content) mtime = server.now_timestamp() if update_userdata: server.userdata[username][server.SNAPSHOT][user_relpath] = [mtime, server.calculate_file_md5(open(filepath, 'rb'))] return mtime
def test_files_put_of_not_existing_file(self): """ Test modify of not existing file.. """ path = 'test_put/file_not_existent.txt' # not existent path to_modify_filepath = userpath2serverpath(USR, path) url = SERVER_FILES_API + path # Create temporary file for test test_file, test_md5 = _make_temp_file() try: test = self.app.put(url, headers=make_basicauth_headers(USR, PW), data={'file': test_file, 'md5': test_md5}, follow_redirects=True) finally: test_file.close() self.assertEqual(test.status_code, server.HTTP_NOT_FOUND) self.assertNotIn(to_modify_filepath, server.userdata[USR][server.SNAPSHOT])
def test_copy_file_path(self): """ Test if a created source file is copied in a new created destination and assures the source file still exists """ copy_test_url = SERVER_ACTIONS_API + 'copy' src_copy_test_file_path = 'test_copy_src/testcopysrc.txt' dst_copy_test_file_path = 'test_copy_dst/testcopydst.txt' # Create source file to be copied and its destination. src_copy_filepath = userpath2serverpath(USR, src_copy_test_file_path) _create_file(USR, src_copy_test_file_path, 'this is the file to be copied') _create_file(USR, dst_copy_test_file_path, 'different other content') test = self.app.post(copy_test_url, headers={'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(USR, PW))}, data={'src': src_copy_test_file_path, 'dst': dst_copy_test_file_path}, follow_redirects=True) self.assertEqual(test.status_code, server.HTTP_OK) self.assertTrue(os.path.isfile(src_copy_filepath))
def test_files_post_with_auth(self): """ Test for authenticated upload. """ user_relative_upload_filepath = 'testupload/testfile.txt' upload_test_url = SERVER_FILES_API + user_relative_upload_filepath uploaded_filepath = userpath2serverpath(USR, user_relative_upload_filepath) assert not os.path.exists(uploaded_filepath), '"{}" file is existing'.format(uploaded_filepath) # Create temporary file for test test_file, test_md5 = _make_temp_file() try: test = self.app.post(upload_test_url, headers=make_basicauth_headers(USR, PW), data={'file': test_file, 'md5': test_md5}, follow_redirects=True) finally: test_file.close() self.assertEqual(test.status_code, server.HTTP_CREATED) self.assertTrue(os.path.isfile(uploaded_filepath)) # check that uploaded path exists in username files dict self.assertIn(user_relative_upload_filepath, server.userdata[USR][server.SNAPSHOT]) os.remove(uploaded_filepath) logging.info('"{}" removed'.format(uploaded_filepath))
def tearDown(self): server_filepath = userpath2serverpath(USR, self.USER_RELATIVE_DOWNLOAD_FILEPATH) if os.path.exists(server_filepath): os.remove(server_filepath) _manually_remove_user(USR) tear_down_test_dir()