📄 test_files.py
字号:
# ***** BEGIN LICENSE BLOCK *****# Version: MPL 1.1# # The contents of this file are subject to the Mozilla Public License # Version# 1.1 (the "License"); you may not use this file except in compliance # with# the License. You may obtain a copy of the License at# http://www.mozilla.org/MPL/# # Software distributed under the License is distributed on an "AS IS" # basis,# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the # License# for the specific language governing rights and limitations under the# License.# # The Original Code is Bespin.# # The Initial Developer of the Original Code is Mozilla.# Portions created by the Initial Developer are Copyright (C) 2009# the Initial Developer. All Rights Reserved.# # Contributor(s):# # ***** END LICENSE BLOCK *****# import osfrom cStringIO import StringIOimport tarfileimport zipfilefrom datetime import datetime, timedeltafrom webtest import TestAppimport simplejsonfrom bespin import config, controllers, modelfrom bespin.model import File, Project, User, FileStatus, Directorytarfilename = os.path.join(os.path.dirname(__file__), "ut.tgz")zipfilename = os.path.join(os.path.dirname(__file__), "ut.zip")otherfilename = os.path.join(os.path.dirname(__file__), "other_import.tgz")with_tabs = os.path.join(os.path.dirname(__file__), "ProjectWithTabs.tgz")app = Nonemacgyver = Nonesomeone_else = Nonemurdoc = Nonedef setup_module(module): global app config.set_profile('test') app = controllers.make_app() app = TestApp(app) def _get_fm(): global macgyver, someone_else, murdoc config.activate_profile() app.reset() model.Base.metadata.drop_all(bind=config.c.dbengine) model.Base.metadata.create_all(bind=config.c.dbengine) s = config.c.sessionmaker(bind=config.c.dbengine) user_manager = model.UserManager(s) file_manager = model.FileManager(s) db = model.DB(user_manager, file_manager) someone_else = user_manager.create_user("SomeoneElse", "", "someone@else.com") murdoc = user_manager.create_user("Murdoc", "", "murdoc@badpeople.bad") otherproject = file_manager.get_project(someone_else, someone_else, "otherproject", create=True) file_manager.save_file(someone_else, otherproject, 'foo', 'Just a file to reserve a project') app.post("/register/new/MacGyver", dict(password="richarddean", email="rich@sg1.com")) macgyver = user_manager.get_user("MacGyver") return file_managerdef test_basic_file_creation(): fm = _get_fm() starting_point = macgyver.amount_used bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "reqs", "Chewing gum wrapper") file_obj = fm.session.query(model.File).filter_by(name="reqs") \ .filter_by(project=bigmac).one() data = str(file_obj.data) assert data == 'Chewing gum wrapper' assert file_obj.saved_size == 19 ending_point = macgyver.amount_used difference = ending_point - starting_point assert difference == 19 now = datetime.now() assert now - file_obj.created < timedelta(seconds=2) file_obj.created = file_obj.created - timedelta(days=1) fm.session.flush() bigmac = fm.get_project(macgyver, macgyver, "bigmac") files = fm.list_files(macgyver, bigmac, "") assert len(files) == 1 assert files[0].name == 'reqs' proj_names = set([proj.name for proj in macgyver.projects]) assert proj_names == set(['bigmac', "SampleProject", "BespinSettings"]) # let's update the contents fm.save_file(macgyver, bigmac, "reqs", "New content") file_obj = fm.session.query(model.File).filter_by(name="reqs") \ .filter_by(project=bigmac).one() assert now - file_obj.created < timedelta(days=1, seconds=2) assert now - file_obj.modified < timedelta(seconds=2) assert file_obj.data == 'New content' fm.session.rollback() def test_changing_file_contents_changes_amount_used(): fm = _get_fm() starting_point = macgyver.amount_used bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "foo", "step 1") assert macgyver.amount_used == starting_point + 6 fm.save_file(macgyver, bigmac, "foo", "step two") assert macgyver.amount_used == starting_point + 8 def test_cannot_save_beyond_quota(): fm = _get_fm() old_units = model.QUOTA_UNITS model.QUOTA_UNITS = 10 try: fm.save_file(macgyver, "bigmac", "foo", "x" * 11) assert False, "Expected an OverQuota exception" except model.OverQuota: pass finally: model.QUOTA_UNITS = old_units def test_amount_used_can_be_recomputed(): fm = _get_fm() starting_point = macgyver.amount_used macgyver.amount_used = 0 fm.recompute_used(macgyver) assert macgyver.amount_used == starting_point def test_retrieve_file_obj(): fm = _get_fm() bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "reqs", "tenletters") try: fm.get_file_object(macgyver, bigmac, "foo/bar") assert False, "expected file not found for missing file" except model.FileNotFound: pass file_obj = fm.get_file_object(macgyver, bigmac, "reqs") assert file_obj.saved_size == 10 def test_error_if_you_try_to_replace_dir_with_file(): fm = _get_fm() bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "foo/bar/baz", "biz") try: fm.save_file(macgyver, bigmac, "foo/bar", "NOT GONNA DO IT!") assert False, "Expected a FileConflict exception" except model.FileConflict: pass def test_get_file_opens_the_file(): fm = _get_fm() bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "foo/bar/baz", "biz") contents = fm.get_file(macgyver, bigmac, "foo/bar/baz") assert contents == "biz" s = fm.session file_obj = s.query(File).filter_by(name="foo/bar/baz") \ .filter_by(project=bigmac).one() files = [f.file for f in macgyver.files] assert file_obj in files open_files = fm.list_open(macgyver) print "OF: ", open_files info = open_files['bigmac']['foo/bar/baz'] assert info['mode'] == "rw" fm.close(macgyver, bigmac, "foo") # does nothing, because we don't have that one open open_files = fm.list_open(macgyver) info = open_files['bigmac']['foo/bar/baz'] assert info['mode'] == "rw" fm.close(macgyver, bigmac, "foo/bar/baz") open_files = fm.list_open(macgyver) assert open_files == {}def test_get_file_raises_exception_if_its_a_directory(): fm = _get_fm() bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "foo/bar/baz", "biz") try: contents = fm.get_file(macgyver, bigmac, "foo/bar/") assert False, "Expected exception for directory" except model.FSException: pass def test_get_file_raises_not_found_exception(): fm = _get_fm() bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "foo/bar/baz", "biz") try: contents = fm.get_file(macgyver, bigmac, "NOTFOUND") assert False, "Expected exception for not found" except model.FileNotFound: pass def test_directory_shortname_computed_to_have_last_dir(): fm = _get_fm() bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "foo/bar/baz", "biz") res = fm.list_files(macgyver, bigmac, "foo/") assert len(res) == 1 d = res[0] shortname = d.short_name assert shortname == "bar/" def test_can_delete_empty_directory(): fm = _get_fm() bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "foo/bar/") fm.delete(macgyver, bigmac, "foo/bar/") s = fm.session flist = s.query(File).filter_by(project=bigmac).all() assert len(flist) == 0 def test_delete_raises_file_not_found(): fm = _get_fm() bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) try: fm.delete(macgyver, bigmac, "DOESNT MATTER") assert False, "Expected not found for missing project" except model.FileNotFound: pass fm.save_file(macgyver, bigmac, "foo/bar/baz", "biz") try: fm.delete(macgyver, bigmac, "STILL DOESNT MATTER") assert False, "Expected not found for missing file" except model.FileNotFound: pass flist = fm.list_files(macgyver, bigmac) assert flist[0].name == "foo/" fm.delete(macgyver, bigmac, "foo/bar/") def test_authorize_other_user(): fm = _get_fm() bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "foo/bar/baz", "biz") fm.authorize_user(macgyver, bigmac, someone_else) b2 = fm.get_project(someone_else, macgyver, "bigmac") assert b2.name == "bigmac" fm.unauthorize_user(macgyver, bigmac, someone_else) try: b2 = fm.get_project(someone_else, macgyver, "bigmac") assert False, "Should have not been authorized any more" except model.NotAuthorized: pass def test_only_owner_can_authorize_user(): fm = _get_fm() bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "foo/bar/baz", "biz") fm.authorize_user(macgyver, bigmac, someone_else) yet_another = fm.db.user_manager.create_user("YetAnother", "", "yet@another.user") try: fm.authorize_user(someone_else, bigmac, yet_another) assert False, "Should not have been allowed to authorize with non-owner" except model.NotAuthorized: pass try: fm.unauthorize_user(someone_else, bigmac, macgyver) assert False, "Should not have been allowed to unauthorize with non-owner" except model.NotAuthorized: pass def test_cannot_delete_file_open_by_someone_else(): fm = _get_fm() bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "foo/bar/baz", "biz") fm.authorize_user(macgyver, bigmac, someone_else) fm.get_file(macgyver, bigmac, "foo/bar/baz") try: fm.delete(someone_else, bigmac, "foo/bar/baz") assert False, "Expected FileConflict exception for deleting open file" except model.FileConflict: pass def test_can_delete_file_open_by_me(): fm = _get_fm() s = fm.session bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "foo/bar/baz", "biz") fm.get_file(macgyver, bigmac, "foo/bar/baz") file_obj_id = s.query(File).filter_by(name="foo/bar/baz") \ .filter_by(project=bigmac).one().id fm.delete(macgyver, bigmac, "foo/bar/baz") fs = fm.session.query(FileStatus).filter_by(file_id=file_obj_id).first() assert fs is None def test_successful_deletion(): fm = _get_fm() starting_used = macgyver.amount_used bigmac = fm.get_project(macgyver, macgyver, "bigmac", create=True) fm.save_file(macgyver, bigmac, "foo/bar/baz", "biz") fm.delete(macgyver, bigmac, "foo/bar/baz") assert macgyver.amount_used == starting_used try: fm.get_file(macgyver, bigmac, "foo/bar/baz") assert False, "Expected FileNotFound because the file is gone" except model.FileNotFound: pass files = fm.list_files(macgyver, bigmac, "foo/bar/")
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -