Exemple #1
0
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
    
    result = bigmac.search_files("eq")
    assert result == ['reqs']
    
    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
Exemple #3
0
def test_bad_project_names():
    _init_data()
    try:
        badone = get_project(macgyver, macgyver, "..", create=True)
        assert False, "Expected BadValue exception for bad name"
    except BadValue:
        pass
    try:
        badone = get_project(macgyver, macgyver, "foo/bar", create=True)
        assert False, "Expected BadValue exception for bad name"
    except BadValue:
        pass
 def get_user(name):
     user = User.find_user(name)
     if user == None:
         user = User.create_user(name, name, name + "@foo.com")
         session.commit()
         info("Created user called '" + name + "'")
     try:
         filesystem.get_project(user, user, "BespinSettings")
     except:
         settings = filesystem.get_project(user, user, "BespinSettings", create=True)
         settings.install_template('usertemplate')
         info("Created BespinSettings project for '" + name + "'")
     return user
Exemple #5
0
def test_deployment_setup():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    resp = app.put("/project/deploy/bigmac/setup", dumps(dict(
        remoteHost="macgyver.com",
        remoteDirectory="/home/macgyver/knownunknowns",
        connType="sftp",
        kcpass="******",
        authType="ssh",
        username="******")))
    
    bigmac = get_project(macgyver, macgyver, "bigmac")
    pdo = deploy.ProjectDeploymentOptions.get(bigmac)
    assert pdo.remote_host == "macgyver.com"
    assert pdo.remote_directory == "/home/macgyver/knownunknowns"
    kc = deploy.DeploymentKeyChain(macgyver, "sekretkeychain")
    cred = kc.get_credentials_for_project(bigmac)
    assert cred['type'] == "ssh"
    assert cred["username"] == "macman"
    
    resp = app.post("/project/deploy/bigmac/setup", 
        dumps(dict(kcpass="******")))
    assert resp.content_type == "application/json"
    data = loads(resp.body)
    assert data['authType'] == "ssh"
    assert data['username'] == "macman"
    assert data['remoteHost'] == "macgyver.com"
    assert data['remoteDirectory'] == "/home/macgyver/knownunknowns"
    assert data['connType'] == "sftp"

    resp = app.put("/project/deploy/bigmac/setup", dumps(dict(
        remoteHost="macgyver.com",
        remoteDirectory="/home/macgyver/knownunknowns",
        connType="sftp",
        kcpass="******",
        authType="password",
        username="******",
        password="******")))
    
    resp = app.post("/project/deploy/bigmac/setup", 
        dumps(dict(kcpass="******")))
    assert resp.content_type == "application/json"
    data = loads(resp.body)
    assert data['authType'] == "password"
    assert data['username'] == "macman"
    assert data['password'] == "NO ONE WILL EVER GUESS THIS!"
    assert data['remoteHost'] == "macgyver.com"
    assert data['remoteDirectory'] == "/home/macgyver/knownunknowns"
    assert data['connType'] == "sftp"
Exemple #6
0
def test_deployment_runs_with_ssh_key(launch_sync):
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    resp = app.put("/project/deploy/bigmac/setup", dumps(dict(
        remoteHost="macgyver.com",
        remoteDirectory="/home/macgyver/knownunknowns",
        connType="sftp",
        kcpass="******",
        authType="ssh",
        username="******")))

    resp = app.post("/project/deploy/bigmac/", 
        dumps(dict(kcpass="******")))
    
    assert resp.content_type == "application/json"
    data = loads(resp.body)
    
    assert 'jobid' in data
    assert data['jobid'] is not None
    assert launch_sync.called
    desturl = launch_sync.call_args[0][2]
    assert desturl == "sftp://macgyver.com//home/macgyver/knownunknowns"
    options = launch_sync.call_args[0][3]
    assert not options.dry_run
    assert options.username == "macman"
    assert isinstance(options.sshkey, path)
    assert not options.sshkey.exists(), "Key file should be deleted at the end"
    
    
Exemple #7
0
def test_deployment_runs(launch_sync):
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    resp = app.put("/project/deploy/bigmac/setup", dumps(dict(
        remoteHost="macgyver.com",
        remoteDirectory="/home/macgyver/knownunknowns",
        connType="sftp",
        kcpass="******",
        authType="password",
        username="******",
        password="******")))

    resp = app.post("/project/deploy/bigmac/", 
        dumps(dict(kcpass="******", dryRun=True)))
    
    assert resp.content_type == "application/json"
    data = loads(resp.body)
    
    assert 'jobid' in data
    assert data['jobid'] is not None
    assert launch_sync.called
    desturl = launch_sync.call_args[0][2]
    assert desturl == "sftp://macgyver.com//home/macgyver/knownunknowns"
    options = launch_sync.call_args[0][3]
    assert options.dry_run
    assert options.username == "macman"
    assert options.password == "super/pass"
Exemple #8
0
def test_register_and_verify_user():
    config.activate_profile()
    _clear_db()
    s = _get_session()
    app = controllers.make_app()
    app = BespinTestApp(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.find_user("BillBixby")
    sample_project = 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")
    app.post("/file/close/BespinSettings/config")
Exemple #9
0
def _clone_impl(user, source, dest=None, push=None, remoteauth="write",
            authtype=None, username=None, password=None, kcpass="",
            vcs="hg"):
    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 = filesystem.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
    
    space_used = project.scan_files()
    user.amount_used += space_used

    metadata.close()
    
    result = dict(output=str(output), command="clone",
                    project=command.dest)
    return result
Exemple #10
0
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()
Exemple #11
0
def test_hg_diff_on_web(run_command_params):
    _init_data()
    bigmac = get_project(macgyver, macgyver, 'bigmac', create=True)
    bigmac.save_file(".hg/hgrc", "# test rc file\n")
    
    request = simplejson.dumps({'command' : ['diff']})
    resp = app.post("/vcs/command/bigmac/", request)
    
    assert resp.content_type == "application/json"
    output = simplejson.loads(resp.body)
    assert 'jobid' in output
    
    resp = app.post("/messages/")
    messages = simplejson.loads(resp.body)
    assert len(messages) == 1
    output = messages[0]
    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 diff"
    assert working_dir == bigmac.location
    print "output=%s" % (output['output'],)
    print "diff_output=%s" % (diff_output,)
    assert output['output'] == diff_output
Exemple #12
0
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
Exemple #13
0
def get_user_plugin_path(user, include_installed=True, plugin_info=None, project=None):
    if not user:
        return []

    if plugin_info is None:
        plugin_info, project = get_user_plugin_info(user)
    
    if project is None:
        project = get_project(user, user, "BespinSettings", create=True)
    
    path = []
    if plugin_info:
        root = user.get_location()
        root_len = len(root)
        pi_plugins = plugin_info.get("plugins", None)
        # NOTE: it's important to trim leading slashes from these paths
        # because the user can edit the pluginInfo.json file directly.
        if pi_plugins:
            path.extend(dict(name="user", plugin = root / leading_slash.sub("", p), chop=root_len) for p in pi_plugins)
        pi_path = plugin_info.get("path", None)
        if pi_path:
            path.extend(dict(name="user", path=root / leading_slash.sub("", p), chop=root_len) for p in pi_path)
    
    if include_installed:
        path.append(dict(name="user", path=project.location / "plugins", 
            chop=len(user.get_location())))
    return path
Exemple #14
0
def test_hg_push_on_web(run_command_params):
    _init_data()
    kc = vcs.KeyChain(macgyver, "foobar")
    # generate key pair
    kc.get_ssh_key()
    bigmac = get_project(macgyver, macgyver, 'bigmac', create=True)
    kc.set_ssh_for_project(bigmac, vcs.AUTH_WRITE)
    metadata = bigmac.metadata
    metadata['remote_url'] = "http://hg.mozilla.org/labs/bespin"
    metadata['push'] = "ssh://hg.mozilla.org/labs/bespin"
    metadata.close()
    bigmac.save_file(".hg/hgrc", "# test rc file\n")
    
    request = simplejson.dumps({'command' : ['push', '_BESPIN_PUSH'], 
                                'kcpass' : 'foobar'})
    resp = app.post("/vcs/command/bigmac/", request)
    resp = app.post("/messages/")
    
    command, context = run_command_params
    
    assert context.user == "MacGyver"
    
    command_line = command.get_command_line()
    print command_line
    assert command_line[0:3] == ["hg", "push", "--ssh"]
    assert command_line[3].startswith("ssh -i")
    assert command_line[4] == "ssh://hg.mozilla.org/labs/bespin"
Exemple #15
0
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 == []
Exemple #16
0
def test_directory_deletion():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    bigmac.save_file("whiz/bang", "stillmore")
    starting_used = macgyver.amount_used
    bigmac.save_file("foo/bar", "data")
    bigmac.save_file("foo/blorg", "moredata")
    
    files = bigmac.search_files("blorg")
    assert files == ['foo/blorg']
    
    bigmac.delete("foo/")
    
    files = bigmac.search_files("blorg")
    assert files == []
    
    flist = bigmac.list_files()
    assert len(flist) == 1
    assert flist[0].name == 'whiz/'
    file_loc = bigmac.location / "foo/bar"
    assert not file_loc.exists()
    assert macgyver.amount_used == starting_used
    
    bigmac.save_file("newfoo/bar.txt", "just text")
    try:
        bigmac.delete("newfoo")
        assert False, "Expected an exception when deleting a directory without the trailing slash"
    except FSException, e:
        pass
Exemple #17
0
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
Exemple #18
0
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
Exemple #19
0
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()
    
    Base.metadata.drop_all(bind=config.c.dbengine)
    Base.metadata.create_all(bind=config.c.dbengine)
    s = config.c.session_factory()
    
    someone_else = User.create_user("SomeoneElse", "", "*****@*****.**")
    murdoc = User.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.find_user("MacGyver")
Exemple #20
0
def test_can_delete_empty_directory():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    bigmac.create_directory("foo/bar/")
    bigmac.delete("foo/bar/")
    location = bigmac.location / "foo/bar"
    assert not location.exists()
Exemple #21
0
def test_install_single_file_plugin():
    _init_data()
    settings_project = get_project(macgyver, macgyver, "BespinSettings")
    destination = settings_project.location / "plugins"
    path_entry = dict(chop=len(macgyver.get_location()), name="user")
    sfp = plugindir / "single_file_plugin1.js"
    plugin = plugins.install_plugin(open(sfp), "http://somewhere/file.js", 
                                    settings_project, path_entry, "APlugin")
    destfile = destination / "APlugin.js"
    assert destfile.exists()
    desttext = destfile.text()
    assert "someFunction" in desttext
    assert plugin.name == "APlugin"
    assert plugin.location == destfile
    assert plugin.relative_location == "BespinSettings/plugins/APlugin.js"
    metadata = plugin.metadata
    type = metadata['type']
    assert type == "user"
    assert metadata['userLocation'] == "BespinSettings/plugins/APlugin.js"
    
    plugin = plugins.install_plugin(open(sfp), "http://somewhere/Flibber.js",
                                    settings_project, path_entry)
    destfile = destination / "Flibber.js"
    assert destfile.exists()
    assert plugin.name == "Flibber"
Exemple #22
0
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 database, filesystem
    from sqlalchemy.orm.exc import NoResultFound
    
    config.set_profile("dev")
    config.activate_profile()
    session = config.c.session_factory()
    try:
        user = session.query(database.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 = filesystem.get_project(user, user, "BespinSettings", create=True)
        project.install_template('usertemplate')
    info("User %s set up to access directory %s" % (user, location))
Exemple #23
0
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
    
    public_key2 = vcs.KeyChain.get_ssh_public_key(macgyver)
    assert public_key2 == public_key
    
    bigmac = 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
Exemple #24
0
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
Exemple #25
0
def test_retrieve_new_deployment_setup():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    resp = app.post("/project/deploy/bigmac/setup", 
        dumps(dict(kcpass="******")))
    assert resp.content_type == "application/json"
    data = loads(resp.body)
    assert data is None
Exemple #26
0
def test_get_users_vcs_name():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    user = vcs._get_vcs_user(macgyver, bigmac)
    assert user == "MacGyver"
    
    settings = get_project(macgyver, macgyver, "BespinSettings")
    settings.save_file("settings", """
vcsuser Mack Gyver <*****@*****.**>
""")
    user = vcs._get_vcs_user(macgyver, bigmac)
    assert user == "Mack Gyver <*****@*****.**>"
    
    metadata = bigmac.metadata
    metadata['vcsuser'] = "******"
    metadata.close()
    user = vcs._get_vcs_user(macgyver, bigmac)
    assert user == "Big MacGyver <*****@*****.**>"
Exemple #27
0
 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"]
Exemple #28
0
def test_get_users_vcs_name():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    user = vcs._get_vcs_user(macgyver, bigmac)
    assert user == "MacGyver"
    
    settings = get_project(macgyver, macgyver, "BespinSettings")
    settings.save_file("settings", """
vcsuser Mack Gyver <*****@*****.**>
""")
    user = vcs._get_vcs_user(macgyver, bigmac)
    assert user == "Mack Gyver <*****@*****.**>"
    
    metadata = bigmac.metadata
    metadata['vcsuser'] = "******"
    metadata.close()
    user = vcs._get_vcs_user(macgyver, bigmac)
    assert user == "Big MacGyver <*****@*****.**>"
Exemple #29
0
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 FileConflict:
        pass
Exemple #30
0
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 FSException:
        pass
Exemple #31
0
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 FileNotFound:
        pass
Exemple #32
0
class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    uuid = Column(String(36), unique=True)
    username = Column(String(128), unique=True)
    email = Column(String(128))
    password = Column(String(64))
    settings = Column(PickleType())
    quota = Column(Integer, default=10)
    amount_used = Column(Integer, default=0)
    file_location = Column(String(200))
    everyone_viewable = Column(Boolean, default=False)
    messages = relation(Message, order_by=Message.when, backref="user")

    i_follow = relation(Connection,
                        primaryjoin=Connection.following_id==id,
                        secondary=Connection.__table__,
                        secondaryjoin=id==Connection.followed_id)

    following_me = relation(Connection,
                            primaryjoin=Connection.followed_id==id,
                            secondary=Connection.__table__,
                            secondaryjoin=id==Connection.following_id)
                    
                    
    @staticmethod
    def generate_password(password):
        password_hash = sha256()
        password_hash.update(config.c.pw_secret + password)
        return password_hash.hexdigest()

    @classmethod
    def create_user(cls, username, password, email, override_location=None):
        """Adds a new user with the given username and password.
        This raises a ConflictError is the user already
        exists."""
        _check_identifiers("Usernames", username)

        log.debug("Creating user %s", username)
        password = User.generate_password(password)
        
        user = cls(username, password, email)
        if override_location is not None:
            user.file_location = override_location
        _get_session().add(user)
        # flush to ensure that the user is unique
        try:
            _get_session().flush()
        except DBAPIError, e:
            raise ConflictError("Username %s is already in use" % username)

        project = get_project(user, user, "SampleProject", create=True)
        project.install_template()
        config.c.stats.incr("users")
        return user
Exemple #33
0
def test_list_top_level():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    bigmac.save_file("readme.txt", "Hi there!")
    result = bigmac.list_files()
    result_names = [file.name for file in result]
    assert result_names == ["readme.txt"]
    result = macgyver.projects
    result_names = [proj.name for proj in result]
    assert result_names == ["BespinSettings", "SampleProject", "bigmac"]
Exemple #34
0
def test_directory_shortname_computed_to_have_last_dir():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    bigmac.save_file("foo/bar/baz", "biz")
    res = bigmac.list_files("foo/")
    print res
    assert len(res) == 1
    d = res[0]
    shortname = d.short_name
    assert shortname == "bar/"
Exemple #35
0
def test_amount_used_can_be_recomputed():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    bigmac.save_file("secrets", "The password is pa55w0rd!")
    starting_point = macgyver.amount_used
    # open the file, to cause a status file to be created
    bigmac.get_file("secrets")
    macgyver.amount_used = 0
    macgyver.recompute_files()
    assert macgyver.amount_used == starting_point
def test_deployment_fails_when_not_configured():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    resp = app.post("/project/deploy/bigmac/",
                    dumps(dict(kcpass="******")),
                    status=400)
    assert resp.content_type == "application/json"
    data = loads(resp.body)
    assert data['error'] == "Deployment is not yet configured."
    assert data['notConfigured'] == True
Exemple #37
0
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
Exemple #38
0
def test_export_zipfile_from_the_web():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    bigmac.save_file("foo/bar", "INFO!")
    resp = app.get("/project/export/bigmac.zip")
    assert resp.content_type == "application/zip"
    zfile = zipfile.ZipFile(StringIO(resp.body))
    members = zfile.infolist()
    assert len(members) == 1
    assert "bigmac/foo/bar" == members[0].filename
Exemple #39
0
def test_list_all_files():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    bigmac.save_file("foo/bar/baz.txt", "Text file 1\n")
    bigmac.save_file("README.txt", "Another file\n")
    bigmac.save_file("bespin/noodle.py", "# A python file\n")
    resp = app.get("/file/list_all/bigmac/")
    assert resp.content_type == "application/json"
    data = simplejson.loads(resp.body)
    assert len(data) == 3
Exemple #40
0
def test_export_tarball_from_the_web():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    bigmac.save_file("foo/bar", "INFO!")
    resp = app.get("/project/export/bigmac.tgz")
    assert resp.content_type == "application/x-tar-gz"
    tfile = tarfile.open("bigmac.tgz", "r:gz", StringIO(resp.body))
    members = tfile.getmembers()
    assert len(members) == 3
    membersnames = [member.name for member in members]
    assert "bigmac/foo/bar" in membersnames
Exemple #41
0
def test_good_file_operations_from_web():
    _init_data()
    app.put("/file/at/bigmac/reqs", "Chewing gum wrapper")
    bigmac = get_project(macgyver, macgyver, "bigmac")
    fileobj = File(bigmac, "reqs")
    contents = str(fileobj.data)
    assert contents == "Chewing gum wrapper"

    s = _get_session()
    sel = EventLog.select().where(
        and_(EventLog.c.kind == 'filesave', EventLog.c.username == 'MacGyver'))
    result = s.connection().execute(sel).fetchall()
    assert len(result) == 1

    resp = app.get("/file/at/bigmac/reqs")
    assert resp.body == "Chewing gum wrapper"

    resp = app.get("/file/at/bigmac/reqs?mode=r")
    assert resp.body == "Chewing gum wrapper"

    resp = app.get("/file/list/bigmac/")
    data = simplejson.loads(resp.body)
    print data

    resp = app.get("/file/list/")
    data = simplejson.loads(resp.body)
    assert data == [{
        'name': 'BespinSettings/'
    }, {
        'name': 'SampleProject/'
    }, {
        'name': 'bigmac/'
    }]

    resp = app.get("/file/list/SampleProject/")
    data = simplejson.loads(resp.body)
    assert data[1]['name'] == 'index.html'

    resp = app.get("/file/list/bigmac/")
    assert resp.content_type == "application/json"
    data = simplejson.loads(resp.body)
    assert len(data) == 1
    data = data[0]
    assert data['name'] == 'reqs'
    assert data['size'] == 19
    assert data['created'].startswith("20")
    assert 'T' in data['created']
    assert data['modified'].startswith("20")
    assert 'T' in data['modified']

    app.delete("/file/at/bigmac/reqs")
    resp = app.get("/file/list/bigmac/")
    data = simplejson.loads(resp.body)
    assert data == []
def test_deployment_setup_with_illegal_parameters():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    resp = app.put("/project/deploy/bigmac/setup",
                   dumps(
                       dict(remoteHost="macgyver.com",
                            remoteDirectory="/home/macgyver/knownunknowns",
                            connType="file",
                            kcpass="******",
                            authType="ssh",
                            username="******")),
                   status=400)
Exemple #43
0
def test_cannot_save_beyond_quota():
    _init_data()
    old_units = filesystem.QUOTA_UNITS
    filesystem.QUOTA_UNITS = 10
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    try:
        bigmac.save_file("foo", "x" * 11)
        assert False, "Expected an OverQuota exception"
    except OverQuota:
        pass
    finally:
        filesystem.QUOTA_UNITS = old_units
Exemple #44
0
def test_customized_template_installation():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    here = path(__file__).dirname().abspath()
    config.c.template_path.append(here)
    bigmac.install_template("ttemplate", other_vars={'answer': 42})
    flist = bigmac.list_files()
    fnlist = [f.name for f in bigmac.list_files()]
    assert fnlist == ["newfile-bigmac.js"]
    contents = flist[0].data
    print contents
    assert contents == """// newfile-bigmac.js
Exemple #45
0
def test_retrieve_file_obj():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    bigmac.save_file("reqs", "tenletters")
    try:
        bigmac.get_file_object("foo/bar")
        assert False, "expected file not found for missing file"
    except FileNotFound:
        pass

    file_obj = bigmac.get_file_object("reqs")
    assert file_obj.saved_size == 10
Exemple #46
0
def test_rename_project():
    _init_data()
    app.post("/project/rename/bigmac/", "foobar", status=404)
    app.put("/file/at/bigmac/")

    bigmac = get_project(macgyver, macgyver, "bigmac")
    bigmac.metadata['hello'] = "world"

    app.post("/project/rename/bigmac/", "foobar")
    try:
        bigmac = get_project(macgyver, macgyver, "bigmac")
        assert False, "The bigmac project should have been renamed"
    except FileNotFound:
        pass
    foobar = get_project(macgyver, macgyver, "foobar")
    assert foobar.metadata['hello'] == 'world'

    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_install_zipfile_plugin():
    _init_data()
    settings_project = get_project(macgyver, macgyver, "BespinSettings")
    destination = settings_project.location / "plugins"
    path_entry = dict(chop=len(macgyver.get_location()), name="user")
    mfp = path(__file__).dirname() / "plugin1.zip"
    plugin = plugins.install_plugin(open(mfp), "http://somewhere/file.zip", 
                                    settings_project, path_entry, "APlugin")
    
    plugin_info = destination / "APlugin" / "package.json"
    assert plugin_info.exists()
    dep = plugin.metadata['dependencies']
    assert dep['plugin2'] == '0.0'
Exemple #48
0
def test_project_rename_should_be_secure():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    p = path('/tmp/foo')
    try:
        try:
            bigmac.rename("/tmp/foo")
            assert not p.exists()
        except BadValue:
            pass
    finally:
        if p.exists():
            p.rmdir()
Exemple #49
0
def run_command_run(qi):
    """Runs the queued up run_command job."""
    message = qi.message
    s = database._get_session()
    user = User.find_user(message['user'])
    message['user'] = user
    message['project'] = get_project(user, user, message['project'])

    result = _run_command_impl(**message)
    result.update(dict(jobid=qi.id, asyncDone=True))
    retvalue = Message(user_id=user.id, message=simplejson.dumps(result))
    s.add(retvalue)
    config.c.stats.incr('vcs_DATE')
Exemple #50
0
def test_install_template_files():
    _init_data()
    app.put(
        "/file/template/jetpacks/mysidebar.html",
        simplejson.dumps(
            dict(stdtemplate="jetpacks/sidebar.js",
                 values=dict(templateName="mysidebar"))))
    jetpacks = get_project(macgyver, macgyver, "jetpacks")
    datafile = jetpacks.get_file_object("mysidebar.html")

    data = datafile.data
    # add assertions here once this is finalized.
    pass
Exemple #51
0
def test_export_zipfile():
    _init_data()
    handle = open(tarfilename)
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    bigmac.import_tarball(os.path.basename(tarfilename), handle)
    handle.close()
    tempfilename = bigmac.export_zipfile()
    zfile = zipfile.ZipFile(tempfilename.name)
    members = zfile.infolist()
    assert len(members) == 3
    names = set(member.filename for member in members)
    # the extra slash shows up in this context, but does not seem to be a problem
    assert 'bigmac/commands/yourcommands.js' in names
Exemple #52
0
def test_run_a_diff(run_command_params):
    _init_data()
    bigmac = get_project(macgyver, macgyver, 'bigmac', create=True)
    bigmac.save_file(".hg/hgrc", "# test rc file\n")
    cmd = ["diff"]
    output = vcs._run_command_impl(macgyver, bigmac, cmd, None)
    command, context = run_command_params
    
    working_dir = context.working_dir
    
    assert isinstance(command, hg.diff)
    assert working_dir == bigmac.location
    assert output['output'] == diff_output
Exemple #53
0
def test_export_tarfile():
    _init_data()
    handle = open(tarfilename)
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    bigmac.import_tarball(os.path.basename(tarfilename), handle)
    handle.close()
    tempfilename = bigmac.export_tarball()
    tfile = tarfile.open(tempfilename.name)
    members = tfile.getmembers()
    assert len(members) == 6
    names = set(member.name for member in members)
    # the extra slash shows up in this context, but does not seem to be a problem
    assert 'bigmac//' in names
def test_keychain_creation():
    _init_data()
    kc = deploy.DeploymentKeyChain(macgyver, "foobar")
    public_key, private_key = kc.get_ssh_key()

    assert public_key.startswith("ssh-rsa")
    assert "RSA PRIVATE KEY" in private_key

    public_key2 = deploy.DeploymentKeyChain.get_ssh_public_key(macgyver)
    assert public_key2 == public_key

    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)

    kc.set_ssh_for_project(bigmac, "macgyver")

    kcfile = path(macgyver.get_location()) / ".bespin-keychain"
    assert kcfile.exists()

    # make sure the file is encrypted
    text = kcfile.bytes()
    assert "RSA PRIVATE KEY" not in text
    assert "ssh-rsa" not in text

    kc = deploy.DeploymentKeyChain(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"
    assert credentials['username'] == 'macgyver'

    kc.delete_credentials_for_project(bigmac)
    credentials = kc.get_credentials_for_project(bigmac)
    assert credentials is None

    kc.set_credentials_for_project(bigmac, "macG", "coolpass")

    kc = deploy.DeploymentKeyChain(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 = deploy.DeploymentKeyChain(macgyver, "foobar")
    credentials = kc.get_credentials_for_project(bigmac)
    assert credentials is None
Exemple #55
0
def seeddb():
    from bespin import config, filesystem
    from bespin.database import User, Connection
    config.set_profile("dev")
    config.activate_profile()

    def get_user(name):
        user = User.find_user(name)
        if user == None:
            user = User.create_user(name, name, name + "@foo.com")
            session.commit()
            info("Created user called '" + name + "'")
        try:
            filesystem.get_project(user, user, "BespinSettings")
        except:
            settings = filesystem.get_project(user,
                                              user,
                                              "BespinSettings",
                                              create=True)
            settings.install_template('usertemplate')
            info("Created BespinSettings project for '" + name + "'")
        return user

    # Seriously there is something wrong with my ego today ;-)
    bgalbraith = get_user("bgalbraith")
    kdangoor = get_user("kdangoor")
    dalmaer = get_user("d")
    mattb = get_user("mattb")
    zuck = get_user("zuck")
    tom = get_user("tom")
    ev = get_user("ev")
    j = get_user("j")

    bgalbraith.follow(j)
    kdangoor.follow(j)
    dalmaer.follow(j)
    mattb.follow(j)
    zuck.follow(j)
    tom.follow(j)
    ev.follow(j)

    jproject = filesystem.get_project(j, j, "SampleProject", create=True)

    j.add_sharing(jproject, bgalbraith, edit=True)
    j.add_sharing(jproject, kdangoor, edit=True)
    j.add_sharing(jproject, dalmaer, edit=True)
    j.add_sharing(jproject, mattb, edit=False)
    j.add_sharing(jproject, zuck, edit=False)
    j.add_sharing(jproject, tom, edit=False)
    j.add_sharing(jproject, ev, edit=False)
Exemple #56
0
def test_bad_files_and_directories():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    try:
        bigmac.save_file("../foo/bar", "hi")
        assert False, "Expected BadValue exception for bad name"
    except BadValue:
        pass

    bigmac.save_file("/tmp/foo", "hi")
    foopath = path("/tmp/foo")
    assert not foopath.exists()
    location = bigmac.location
    assert (location / "tmp" / "foo").exists()
Exemple #57
0
def test_delete_directories_outside_of_tree():
    _init_data()
    p = path("/tmp/onlydirs/")
    assert not p.exists()
    p.makedirs()
    try:
        (p / "dir2").mkdir()
        (p / "dir3").mkdir()
        bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
        bigmac.save_file("tmp/onlydirs/newfile", "yo")
        files = bigmac.delete(p)
        assert p.exists()
    finally:
        if p.exists():
            p.rmtree()
Exemple #58
0
def test_find_out_remote_auth_info_from_web():
    _init_data()
    bigmac = get_project(macgyver, macgyver, "bigmac", create=True)
    keychain = vcs.KeyChain(macgyver, "foobar")
    keychain.set_ssh_for_project(bigmac, vcs.AUTH_BOTH)
    resp = app.get("/vcs/remoteauth/bigmac/")
    assert resp.body == "both"
    
    keychain.delete_credentials_for_project(bigmac)
    resp = app.get("/vcs/remoteauth/bigmac/")
    assert resp.body == ""
    
    keychain.set_ssh_for_project(bigmac, vcs.AUTH_WRITE)
    resp = app.get("/vcs/remoteauth/bigmac/")
    assert resp.body == "write"
Exemple #59
0
def test_vcs_auth_set_password_on_web():
    _init_data()
    bigmac = get_project(macgyver, macgyver, 'bigmac', create=True)
    resp = app.post("/vcs/setauth/bigmac/", dict(kcpass="******", 
                            type="password", username="******", 
                            password="******",
                            remoteauth="write"))
    kc = vcs.KeyChain(macgyver, "foobar")
    credentials = kc.get_credentials_for_project(bigmac)
    assert credentials['type'] == 'password'
    assert credentials['username'] == 'macG'
    assert credentials['password'] == 'coolpass'
    metadata = bigmac.metadata
    assert metadata[vcs.AUTH_PROPERTY] == vcs.AUTH_WRITE
    metadata.close()
Exemple #60
0
def deploy_impl(qi):
    """Executed via the worker queue to actually deploy the
    project."""
    message = qi.message
    kcpass = message['kcpass']
    options = _OptionHolder(message['options'])

    s = _get_session()

    user = User.find_user(message['user'])
    project = get_project(user, user, message['project'])
    pdo = ProjectDeploymentOptions.get(project)
    keychain = DeploymentKeyChain(user, kcpass)
    credentials = keychain.get_credentials_for_project(project)
    cwd = os.getcwd()

    keyfile = None

    options.username = credentials['username']

    if credentials['type'] == 'ssh':
        keyfile = TempSSHKeyFile()
        keyfile.store(credentials['ssh_public_key'],
                      credentials['ssh_private_key'])
        options.sshkey = keyfile.filename
    else:
        options.password = credentials['password']

    desturl = "sftp://%s/%s" % (quote(pdo.remote_host,
                                      safe=""), quote(pdo.remote_directory))

    try:
        os.chdir(project.location)
        log.debug("Computed destination URL: %s", desturl)
        log.debug("Running with options: %r", options)
        error, output = _launch_sync(qi.id, user.id, desturl, options)

        # there's an extra layer around the output that is
        # expected by the client
        result = dict(output=dict(output=output, error=error))

        result.update(dict(jobid=qi.id, asyncDone=True))
        retvalue = Message(user_id=user.id, message=dumps(result))
        s.add(retvalue)
    finally:
        if keyfile:
            keyfile.delete()
        os.chdir(cwd)