def test_basic_file_creation(): _init_data() starting_point = macgyver.amount_used bigmac = get_project(macgyver, macgyver, "bigmac", create=True) bigmac.save_file("reqs", "Chewing gum wrapper") file_obj = File(bigmac, "reqs") data = str(file_obj.data) assert data == 'Chewing gum wrapper' ending_point = macgyver.amount_used difference = ending_point - starting_point assert difference == 19 now = datetime.now() assert now - file_obj.created < timedelta(seconds=2) assert now - file_obj.modified < timedelta(seconds=2) bigmac = get_project(macgyver, macgyver, "bigmac") files = bigmac.list_files("") assert len(files) == 1 assert files[0].short_name == 'reqs' proj_names = set([proj.name for proj in macgyver.projects]) assert proj_names == set(['bigmac', "SampleProject", "BespinSettings"]) # let's update the contents bigmac.save_file("reqs", "New content") file_obj = File(bigmac, "reqs") assert file_obj.data == 'New content'
def test_basic_project_metadata_use(): _init_data() bigmac = get_project(macgyver, macgyver, "bigmac", create=True) metadata = bigmac.metadata try: value = metadata['remote_auth'] assert False, "should have gotten key error for unset value" except KeyError: pass metadata['remote_auth'] = "both" metadata.close() bigmac = get_project(macgyver, macgyver, "bigmac") metadata = bigmac.metadata # this should re-open the database and retrieve the value value = metadata['remote_auth'] assert value == "both" metadata['remote_auth'] = "write" metadata.close() bigmac = get_project(macgyver, macgyver, "bigmac") metadata = bigmac.metadata # this should re-open the database and retrieve the value value = metadata['remote_auth'] assert value == "write" del metadata['remote_auth'] try: value = metadata['remote_auth'] assert False, "expected key to be gone from DB" except KeyError: pass
def test_bad_project_names(): _init_data() try: badone = get_project(macgyver, macgyver, "..", create=True) assert False, "Expected BadValue exception for bad name" except model.BadValue: pass try: badone = get_project(macgyver, macgyver, "foo/bar", create=True) assert False, "Expected BadValue exception for bad name" except model.BadValue: pass
def test_rename_project(): _init_data() app.post("/project/rename/bigmac/", "foobar", status=404) app.put("/file/at/bigmac/") app.post("/project/rename/bigmac/", "foobar") try: bigmac = get_project(macgyver, macgyver, "bigmac") assert False, "The bigmac project should have been renamed" except model.FileNotFound: pass foobar = get_project(macgyver, macgyver, "foobar") app.put("/file/at/bigmac/") # should get a conflict error if you try to rename to a project # that exists app.post("/project/rename/foobar/", "bigmac", status=409)
def test_can_delete_file_open_by_me(): _init_data() bigmac = get_project(macgyver, macgyver, "bigmac", create=True) bigmac.save_file("foo/bar/baz", "biz") bigmac.get_file("foo/bar/baz") bigmac.delete("foo/bar/baz") assert not macgyver.files
def test_can_delete_empty_directory(): _init_data() bigmac = get_project(macgyver, macgyver, "bigmac", create=True) bigmac.save_file("foo/bar/") bigmac.delete("foo/bar/") location = bigmac.location / "foo/bar" assert not location.exists()
def test_top_level_deletion(): _init_data() bigmac = get_project(macgyver, macgyver, "bigmac", create=True) bigmac.save_file("foo", "data") bigmac.delete("foo") flist = bigmac.list_files() assert flist == []
def listfiles(request, response): user = request.user path = request.kwargs['path'] if not path: files = user.projects else: try: project, path = _split_path(request) except BadRequest: project = path path = '' if project: project = model.get_project(user, user, project) files = project.list_files(path) result = [] for item in files: f = {'name' : item.short_name} _populate_stats(item, f) result.append(f) response.content_type = "application/json" response.body = simplejson.dumps(result) return response()
def test_hg_clone_on_web(run_command_params): _init_data() resp = app.post("/vcs/clone/", dict(source="http://hg.mozilla.org/labs/bespin", dest="bigmac", push="ssh://hg.mozilla.org/labs/bespin", remoteauth="both", authtype="password", username="******", password="******", kcpass="******" )) assert resp.content_type == "application/json" output = simplejson.loads(resp.body) assert 'output' in output output = output['output'] command, context = run_command_params working_dir = context.working_dir command_line = " ".join(command.get_command_line()) assert command_line == "hg clone http://someuser:[email protected]/labs/bespin bigmac" assert working_dir == macgyver.get_location() assert output == clone_output bigmac = model.get_project(macgyver, macgyver, "bigmac") metadata = bigmac.metadata assert metadata['remote_auth'] == vcs.AUTH_BOTH assert metadata['push'] == "ssh://hg.mozilla.org/labs/bespin" metadata.close()
def test_register_and_verify_user(): config.activate_profile() _clear_db() s, user_manager = _get_user_manager() app = controllers.make_app() app = TestApp(app) resp = app.post('/register/new/BillBixby', dict(email="*****@*****.**", password="******")) assert resp.content_type == "application/json" data = simplejson.loads(resp.body) assert data == {} assert resp.cookies_set['auth_tkt'] assert app.cookies billbixby = user_manager.get_user("BillBixby") sample_project = model.get_project(billbixby, billbixby, "SampleProject") files = [file.name for file in sample_project.list_files()] assert "readme.txt" in files # should be able to run again without an exception appearing resp = app.post('/register/new/BillBixby', dict(email="*****@*****.**", password="******"), status=409) # with the cookie set, we should be able to retrieve the # logged in name resp = app.get('/register/userinfo/') assert resp.content_type == 'application/json' data = simplejson.loads(resp.body) assert data['username'] == 'BillBixby' assert 'quota' in data assert data['quota'] == 15728640 assert 'amountUsed' in data resp = app.get("/file/at/BespinSettings/config.js") app.post("/file/close/BespinSettings/config.js")
def editbespin(options): """Use Bespin to edit Bespin. This will change the given user's file location to the directory above Bespin, allowing you to edit Bespin (and any other projects you have in that directory).""" if not 'editbespin' in options or not options.editbespin.user: raise BuildFailure("You must specify a user with -u for this task.") user = options.editbespin.user from bespin import config from bespin import model from sqlalchemy.orm.exc import NoResultFound config.set_profile("dev") config.activate_profile() session = config.c.sessionmaker(bind=config.c.dbengine) try: user = session.query(model.User).filter_by(username=user).one() except NoResultFound: raise BuildFailure("I couldn't find %s in the database. Sorry!" % (user)) location = path.getcwd().parent.abspath() user.file_location = location user.recompute_files() session.commit() bespinsettings_loc = location / "BespinSettings" if not bespinsettings_loc.exists(): project = model.get_project(user, user, "BespinSettings", create=True) project.install_template('usertemplate') info("User %s set up to access directory %s" % (user, location))
def test_project_deletion(): _init_data() bigmac = get_project(macgyver, macgyver, "bigmac", create=True) bigmac.save_file("foo/bar/baz", "biz") bigmac.delete() flist = macgyver.projects assert "bigmac" not in flist
def test_delete_project_from_the_web(): global macgyver _init_data() bigmac = get_project(macgyver, macgyver, "bigmac", create=True) bigmac.save_file("README.txt", "This is the readme file.") resp = app.delete("/file/at/bigmac/") assert len(macgyver.projects) == 2
def _init_data(): global macgyver, someone_else, murdoc config.activate_profile() fsroot = config.c.fsroot if fsroot.exists() and fsroot.basename() == "testfiles": fsroot.rmtree() fsroot.makedirs() 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) someone_else = user_manager.create_user("SomeoneElse", "", "*****@*****.**") murdoc = user_manager.create_user("Murdoc", "", "*****@*****.**") otherproject = get_project(someone_else, someone_else, "otherproject", create=True) otherproject.save_file('foo', 'Just a file to reserve a project') app.post("/register/new/MacGyver", dict(password="******", email="*****@*****.**")) macgyver = user_manager.get_user("MacGyver")
def test_hg_clone_on_web_with_ssh(run_command_params): _init_data() resp = app.post("/vcs/clone/", dict(source="http://hg.mozilla.org/labs/bespin", push="ssh://hg.mozilla.org/labs/bespin", remoteauth="both", authtype="ssh", kcpass="******" )) assert resp.content_type == "application/json" output = simplejson.loads(resp.body) assert 'output' in output output = output['output'] command, context = run_command_params working_dir = context.working_dir command_line = command.get_command_line() assert command_line[0:3] == ["hg", "clone", "-e"] assert command_line[3].startswith("ssh -i") assert command_line[4] == "http://hg.mozilla.org/labs/bespin" assert command_line[5] == "bespin" assert working_dir == macgyver.get_location() assert output == clone_output bespin = model.get_project(macgyver, macgyver, "bespin") metadata = bespin.metadata assert metadata['remote_auth'] == vcs.AUTH_BOTH assert metadata['push'] == "ssh://hg.mozilla.org/labs/bespin" metadata.close()
def test_create_a_project_from_the_web(): _init_data() app.put("/file/at/bigmac/") project_names = [project.name for project in macgyver.projects] assert 'bigmac' in project_names bigmac = get_project(macgyver, macgyver, 'bigmac') assert not bigmac.list_files()
def test_changing_file_contents_changes_amount_used(): _init_data() starting_point = macgyver.amount_used bigmac = get_project(macgyver, macgyver, "bigmac", create=True) bigmac.save_file("foo", "step 1") assert macgyver.amount_used == starting_point + 6 bigmac.save_file("foo", "step two") assert macgyver.amount_used == starting_point + 8
def deletefile(request, response): user = request.user project, path = _split_path(request) project = model.get_project(user, user, project) project.delete(path) return response()
def test_get_file_raises_not_found_exception(): _init_data() bigmac = get_project(macgyver, macgyver, "bigmac", create=True) bigmac.save_file("foo/bar/baz", "biz") try: contents = bigmac.get_file("NOTFOUND") assert False, "Expected exception for not found" except model.FileNotFound: pass
def test_error_if_you_try_to_replace_dir_with_file(): _init_data() bigmac = get_project(macgyver, macgyver, "bigmac", create=True) bigmac.save_file("foo/bar/baz", "biz") try: bigmac.save_file("foo/bar", "NOT GONNA DO IT!") assert False, "Expected a FileConflict exception" except model.FileConflict: pass
def run_one(func, f): global macgyver print "Testing %s" % (func) handle = open(f) _init_data() bigmac = get_project(macgyver, macgyver, "bigmac", create=True) getattr(bigmac, func)(os.path.basename(f), handle) handle.close() flist = bigmac.list_files() flist = [item.name for item in flist] assert flist == ["commands/", "config.js", "scratchpad/"] handle = open(otherfilename) bigmac = get_project(macgyver, macgyver, "bigmac", clean=True) bigmac.import_tarball(os.path.basename(f), handle) flist = bigmac.list_files() flist = [item.name for item in flist] assert flist == ["README"]
def test_get_file_raises_exception_if_its_a_directory(): _init_data() bigmac = get_project(macgyver, macgyver, "bigmac", create=True) bigmac.save_file("foo/bar/baz", "biz") try: contents = bigmac.get_file("foo/bar/") assert False, "Expected exception for directory" except model.FSException: pass
def rename_project(request, response): user = request.user project_name = request.kwargs['project_name'] project = model.get_project(user, user, project_name) project.rename(request.body) response.body = "" response.content_type = "text/plain" return response()
def install_template(request, response): user = request.user project_name = request.kwargs['project_name'] template_name = request.body project = model.get_project(user, user, project_name, create=True) project.install_template(template_name) response.content_type = "text/plain" response.body = "" return response()
def __decomposeName(self, name): from bespin import config, model from bespin.config import c session = c.sessionmaker(bind=c.dbengine) user_manager = model.UserManager(session) (username, projectname, path) = name.split("/", 2) user = user_manager.get_user(username) project = model.get_project(user, user, projectname) return (project, path)
def putfile(request, response): user = request.user project, path = _split_path(request) project = model.get_project(user, user, project, create=True) if path: project.save_file(path, request.body) return response()
def file_list_all(request, response): user = request.user project_name = request.kwargs['project_name'] project = model.get_project(user, user, project_name) files = project._search_cache.lines(retain=False) response.content_type = "application/json" response.body = simplejson.dumps(files) return response()
def clone(user, source, dest=None, push=None, remoteauth="write", authtype=None, username=None, password=None, kcpass="", vcs="hg"): """Clones or checks out the repository using the command provided.""" working_dir = user.get_location() args = ["clone", source] if dest: args.append(dest) auth = {} if username: auth['username'] = username if password: auth['password'] = password keychain = KeyChain(user, kcpass) keyfile = None if vcs: dialect = main.get_dialect(vcs) else: dialect = None if authtype: auth['type'] = authtype if authtype == "ssh": public_key, private_key = keychain.get_ssh_key() keyfile = TempSSHKeyFile() keyfile.store(public_key, private_key) auth['key'] = keyfile.filename try: context = main.SecureContext(working_dir, auth) command = main.convert(context, args, dialect) output = main.run_command(command, context) finally: if keyfile: keyfile.delete() project = model.get_project(user, user, command.dest) if authtype == "ssh": keychain.set_ssh_for_project(project, remoteauth) elif authtype == "password": keychain.set_credentials_for_project(project, remoteauth, username, password) metadata = project.metadata metadata['remote_url'] = source if push: metadata['push'] = push metadata.close() return str(output)
def test_keychain_creation(): _init_data() kc = vcs.KeyChain(macgyver, "foobar") public_key, private_key = kc.get_ssh_key() assert public_key.startswith("ssh-rsa") assert "RSA PRIVATE KEY" in private_key bigmac = model.get_project(macgyver, macgyver, "bigmac", create=True) kc.set_ssh_for_project(bigmac, vcs.AUTH_BOTH) kcfile = path(macgyver.get_location()) / ".bespin-keychain" assert kcfile.exists() metadata = bigmac.metadata assert metadata['remote_auth'] == vcs.AUTH_BOTH metadata.close() # make sure the file is encrypted text = kcfile.bytes() assert "RSA PRIVATE KEY" not in text assert "ssh-rsa" not in text kc = vcs.KeyChain(macgyver, "foobar") public_key2, private_key2 = kc.get_ssh_key() assert public_key2 == public_key assert private_key2 == private_key credentials = kc.get_credentials_for_project(bigmac) assert "RSA PRIVATE KEY" in credentials['ssh_private_key'] assert credentials['type'] == "ssh" kc.delete_credentials_for_project(bigmac) credentials = kc.get_credentials_for_project(bigmac) assert credentials is None metadata = bigmac.metadata try: value = metadata['remote_auth'] assert False, "expected remote_auth key to be removed from project" except KeyError: pass metadata.close() kc.set_credentials_for_project(bigmac, vcs.AUTH_WRITE, "macG", "coolpass") kc = vcs.KeyChain(macgyver, "foobar") credentials = kc.get_credentials_for_project(bigmac) assert credentials['type'] == 'password' assert credentials['username'] == 'macG' assert credentials['password'] == 'coolpass' kc.delete_credentials_for_project(bigmac) kc = vcs.KeyChain(macgyver, "foobar") credentials = kc.get_credentials_for_project(bigmac) assert credentials is None
def test_template_installation(): _init_data() bigmac = get_project(macgyver, macgyver, "bigmac", create=True) bigmac.install_template() data = bigmac.get_file("readme.txt") bigmac.close("readme.txt") assert "Welcome to Bespin" in data result = bigmac.list_files() result_names = [file.name for file in result] assert 'readme.txt' in result_names