Exemplo n.º 1
0
def _create_repo_common(request, repo_name, repo_desc, encryption,
                        uuid, magic_str, encrypted_file_key):
    """Common logic for creating repo.

    Returns:
        newly created repo id. Or ``None`` if error raised.
    """
    username = request.user.username
    try:
        if not encryption:
            if is_org_context(request):
                org_id = request.user.org.org_id
                repo_id = seafile_api.create_org_repo(repo_name, repo_desc,
                                                      username, None, org_id)
            else:
                repo_id = seafile_api.create_repo(repo_name, repo_desc,
                                                  username, None)
        else:
            if is_org_context(request):
                org_id = request.user.org.org_id
                repo_id = seafile_api.create_org_enc_repo(
                    uuid, repo_name, repo_desc, username, magic_str,
                    encrypted_file_key, enc_version=2, org_id=org_id)
            else:
                repo_id = seafile_api.create_enc_repo(
                    uuid, repo_name, repo_desc, username,
                    magic_str, encrypted_file_key, enc_version=2)
    except SearpcError as e:
        logger.error(e)
        repo_id = None

    return repo_id
Exemplo n.º 2
0
def test_encrypted_repo(rpc, enc_version):
    test_repo_name = 'test_enc_repo'
    test_repo_desc = 'test_enc_repo'
    test_repo_passwd = 'test_enc_repo'
    if rpc == 'create_repo':
        repo_id = api.create_repo(test_repo_name, test_repo_desc, USER,
                                  test_repo_passwd, enc_version)
        assert repo_id
    else:
        if enc_version == 2:
            repo_id = 'd17bf8ca-3019-40ee-8fdb-0258c89fb762'
        elif enc_version == 3:
            repo_id = 'd17bf8ca-3019-40ee-8fdb-0258c89fb763'
        else:
            repo_id = 'd17bf8ca-3019-40ee-8fdb-0258c89fb764'
        enc_info = api.generate_magic_and_random_key(enc_version, repo_id,
                                                     test_repo_passwd)
        assert enc_info
        ret_repo_id = api.create_enc_repo(repo_id, test_repo_name,
                                          test_repo_desc, USER, enc_info.magic,
                                          enc_info.random_key, enc_info.salt,
                                          enc_version)
        assert ret_repo_id == repo_id

    repo = api.get_repo(repo_id)
    assert repo
    assert repo.enc_version == enc_version
    assert len(repo.magic) == 64
    assert len(repo.random_key) == 96
    if enc_version == 3 or enc_version == 4:
        assert len(repo.salt) == 64

    new_passwd = 'new password'

    assert api.set_passwd(repo.id, USER, test_repo_passwd) == 0
    assert api.get_decrypt_key(repo.id, USER)
    api.change_repo_passwd(repo.repo_id, test_repo_passwd, new_passwd,
                           USER) == 0
    assert api.set_passwd(repo.id, USER, new_passwd) == 0

    assert api.is_password_set(repo.id, USER)
    assert api.unset_passwd(repo.id, USER) == 0
    assert api.is_password_set(repo.id, USER) == 0

    api.remove_repo(repo_id)
Exemplo n.º 3
0
        try:
            seafile_api.add_org_group_repo(repo_id, org_id, group.id, username,
                                           permission)
        except SearpcError, e:
            logger.error(e)
            return json_error(_(u'Failed to create: internal error.'))
        else:
            return HttpResponse(json.dumps({'success': True}),
                                content_type=content_type)
    else:
        try:
            if encryption:
                repo_id = seafile_api.create_enc_repo(uuid,
                                                      repo_name,
                                                      repo_desc,
                                                      username,
                                                      magic_str,
                                                      encrypted_file_key,
                                                      enc_version=2)
            else:
                repo_id = seafile_api.create_repo(repo_name, repo_desc,
                                                  username, None)
        except SearpcError, e:
            logger.error(e)
            return json_error(_(u'Failed to create'))

        try:
            seafile_api.set_group_repo(repo_id, group.id, username, permission)
        except SearpcError, e:
            logger.error(e)
            return json_error(_(u'Failed to create: internal error.'))
Exemplo n.º 4
0
        except SearpcError, e:
            logger.error(e)
            return json_error(_(u"Failed to create"))

        try:
            seafile_api.add_org_group_repo(repo_id, org_id, group.id, username, permission)
        except SearpcError, e:
            logger.error(e)
            return json_error(_(u"Failed to create: internal error."))
        else:
            return HttpResponse(json.dumps({"success": True}), content_type=content_type)
    else:
        try:
            if encryption:
                repo_id = seafile_api.create_enc_repo(
                    uuid, repo_name, repo_desc, username, magic_str, encrypted_file_key, enc_version=2
                )
            else:
                repo_id = seafile_api.create_repo(repo_name, repo_desc, username, None)
        except SearpcError, e:
            logger.error(e)
            return json_error(_(u"Failed to create"))

        try:
            seafile_api.set_group_repo(repo_id, group.id, username, permission)
        except SearpcError, e:
            logger.error(e)
            return json_error(_(u"Failed to create: internal error."))
        else:
            return HttpResponse(json.dumps({"success": True}), content_type=content_type)
Exemplo n.º 5
0
def test_repo_manipulation():

    #test get_system_default_repo_id
    t_default_repo_id = api.get_system_default_repo_id()
    assert t_default_repo_id

    #test create_repo
    t_repo_id = api.create_repo('test_repo_manipulation',
                                '',
                                USER,
                                passwd=None)
    assert t_repo_id

    #test counts_repo
    t_repo_count = 0
    t_repo_count = api.count_repos()
    assert t_repo_count != 0

    #test get_repo ,edit_repo
    t_new_name = 'n_name'
    t_new_desc = 'n_desc'
    t_repo_version = 1
    t_repo = api.get_repo(t_repo_id)
    assert t_repo

    api.edit_repo(t_repo_id, t_new_name, t_new_desc, USER)
    t_repo = api.get_repo(t_repo_id)
    assert t_repo.name == t_new_name and t_repo.desc == t_new_desc

    #test revert_repo and get_commit
    t_commit_id_before_changing = t_repo.head_cmmt_id

    api.post_dir(t_repo_id, '/', 'dir1', USER)
    t_repo = api.get_repo(t_repo_id)

    api.revert_repo(t_repo_id, t_commit_id_before_changing, USER)

    t_repo = api.get_repo(t_repo_id)
    t_commit_id_after_revert = t_repo.head_cmmt_id

    t_commit_before_changing = api.get_commit(t_repo_id, t_repo_version,
                                              t_commit_id_before_changing)
    t_commit_after_revert = api.get_commit(t_repo_id, t_repo_version,
                                           t_commit_id_after_revert)
    assert t_commit_before_changing.root_id == t_commit_after_revert.root_id

    #test is_repo_owner
    assert api.is_repo_owner(USER, t_repo_id)
    assert api.is_repo_owner(USER2, t_repo_id) == 0

    #test get_repo_owner
    owner_get = api.get_repo_owner(t_repo_id)
    assert owner_get == USER

    #test set_repo_owner
    api.set_repo_owner(t_repo_id, USER2)
    assert api.is_repo_owner(USER2, t_repo_id)

    #test create_enc_repo
    t_enc_repo_id = '826d1b7b-f110-46f2-8d5e-7b5ac3e11f4d'
    t_enc_version = 2
    t_passwd = '123'
    magic_and_random_key = api.generate_magic_and_random_key(
        t_enc_version, t_enc_repo_id, t_passwd)
    t_magic = magic_and_random_key.magic
    t_random_key = magic_and_random_key.random_key
    t_enc_repo_id = api.create_enc_repo(t_enc_repo_id, 'test_encrypted_repo',
                                        '', USER, t_magic, t_random_key,
                                        t_enc_version)
    assert t_enc_repo_id == '826d1b7b-f110-46f2-8d5e-7b5ac3e11f4d'

    #test get_repo_list
    t_start = -1
    t_limit = -1
    t_repo_list = api.get_repo_list(t_start, t_limit)
    assert t_repo_list and len(t_repo_list)

    t_start = 1
    t_limit = 1
    t_repo_list = api.get_repo_list(t_start, t_limit)
    assert t_repo_list and len(t_repo_list) == 1

    #test get_owned_repo_list
    t_repo_list = api.get_owned_repo_list(USER2)
    assert t_repo_list and len(t_repo_list)

    #test get_commit_list
    t_offset = 0
    t_limit = 0
    t_commit_list = api.get_commit_list(t_repo_id, t_offset, t_limit)
    assert t_commit_list and len(t_commit_list) == 4

    t_offset = 1
    t_limit = 1
    t_commit_list = api.get_commit_list(t_repo_id, t_offset, t_limit)
    assert t_commit_list and len(t_commit_list) == 1

    #test remove_repo
    api.remove_repo(t_repo_id)
    t_repo = api.get_repo(t_repo_id)
    assert t_repo == None