Exemplo n.º 1
0
def get_client_list():
    t = TableManager(
        os.path.expanduser('~') + '/.onedirclient/sync.db', 'local')
    t.connect()
    client_list = t.pull()
    t.clear_table()
    t.disconnect()
    return client_list
Exemplo n.º 2
0
def get_client_list():
    t = TableManager(os.path.expanduser('~') + '/.onedirclient/sync.db', 'local')
    t.connect()
    client_list = t.pull()
    t.clear_table()
    t.disconnect()
    return client_list
Exemplo n.º 3
0
def test_tm_with_enter():
    """
    Checks that with enter is being called
    """
    with patch('Server.sql.sql_manager.TableManager.__enter__') as enter_mock:
        with TableManager(db_name, table_name):
            assert enter_mock.called
Exemplo n.º 4
0
def test_tm_good_quick_push():
    """
    @precondition: test_tm_with_enter passed
    @precondition: test_tm_with_exit passed
    @precondition: test_tm_with_exit_error passed
    Checks that data is being written with a good push
    """
    expected = ['abc', 1]
    with TableManager(db_name, table_name) as t:
        t.quick_push(expected)
    s = SqlManager(db_name)
    s.connect()
    values = s._fetch_command('SELECT * FROM ' + table_name + ';')
    s.disconnect()
    values = values[0]
    actual = []
    expected = ['abc', 1]
    for val in values:
        if type(val) == int:
            val = int(val)
        else:
            val = str(val)
        actual += [val]
    message = 'Expected: ' + str(expected) + '\n' + 'Actual' + str(actual)
    n_eq(expected, actual, message=message)
Exemplo n.º 5
0
def test_tm_with_exit():
    """
    Checks that with exit is called when there are no errors
    """
    with patch('Server.sql.sql_manager.TableManager.__exit__') as exit_mock:
        with TableManager(db_name, table_name):
            pass
        assert exit_mock.called
Exemplo n.º 6
0
def test_tm_with_exit_error():
    """
    Test that with exit will be called even if there is an error
    """
    with patch('Server.sql.sql_manager.TableManager.__exit__') as exit_mock:
        with TableManager(db_name, table_name) as t:
            t.push([i for i in range(10)])
        assert exit_mock.called
Exemplo n.º 7
0
def test_tm_exists():
    """
    Tries to connect to a table that exists
    """
    t = TableManager(db_name, table_name)
    check_name = [val[0] for val in column_map]
    check_type = [val[1] for val in column_map]
    actual = check_name == t.table_col_names and check_type == t.table_col_type
    n_ok(actual, message='Checks table loaded correctly')
Exemplo n.º 8
0
def test_tm_bad_quick_push():
    """
    @precondition: test_tm_with_enter passed
    @precondition: test_tm_with_exit passed
    @precondition: test_tm_with_exit_error passed
    Checks that the push is rejected when the data is wrong
    """
    with TableManager(db_name, table_name) as t:
        t.quick_push([i for i in range(5)])
Exemplo n.º 9
0
def main(ip, port):
    """
    Since pyintofiy says not override its init i made a static class for event handler to use.
    """
    conffile = os.path.expanduser('~') + '/.onedirclient'
    conffile = os.path.join(conffile, 'client.json')
    conffile = os.path.abspath(conffile)
    jd = open(conffile)
    conf = json.load(jd)
    jd.close()
    conf['ip'] = ip
    conf['port'] = port
    ListenerContainer.login = conf
    ListenerContainer.client = OneDirFtpClient(ip, port, conf['username'],
                                               conf['nick'], conf['password'],
                                               conf['root_dir'])
    t = Thread(target=checker, name='checker', args=())
    t.start()
    ListenerContainer.is_syncing = conf['is_syncing']
    ListenerContainer.root_dir = conf['root_dir']
    ListenerContainer.nick = conf['nick']
    notifier = pyinotify.Notifier(ListenerContainer.watch_manager,
                                  EventHandler())
    ListenerContainer.add_watch(conf['root_dir'])
    ListenerContainer.last_sync = conf['last_sync']
    ListenerContainer.add_config(conffile)
    db = os.path.expanduser('~')
    db = db + '/.onedirclient/sync.db'
    ListenerContainer.sync_db = TableManager(db, 'local')
    if not conf['is_syncing']:
        ListenerContainer.sync_db.connect()
    while True:
        try:
            # notifier.process_events()
            # if notifier.check_events():
            #     notifier.read_events()
            notifier.loop()
        except KeyboardInterrupt:
            if ListenerContainer.move_to_folder:
                try:
                    ListenerContainer.client.delete_folder(
                        ListenerContainer.move_to_folder)
                except error_perm:
                    pass  # Nothing to do
            if ListenerContainer.move_to_file:
                try:
                    ListenerContainer.client.delete_file(
                        ListenerContainer.move_to_file)
                except error_perm:
                    pass  # Nothing to do
            ListenerContainer.is_checking = False
            notifier.stop()
            update_last_sync()
            break
        except not KeyboardInterrupt as e:
            reset()
Exemplo n.º 10
0
def test_tm_medium_pull():
    """
    Tries to pull all the columns were there are different types
    """
    push = ['one', 1]
    with TableManager(db_name, table_name) as t:
        t.quick_push(push)
        expected = ('one', 1)
        actual = t.pull()[0]
    n_eq(expected, actual)
Exemplo n.º 11
0
def server_start_testing(is_verbose=False, port=None):
    user_db = 'test_users.db'
    user_table = 'users'
    shares_db = 'test_shares.db'
    username = '******'
    password = '******'
    root = os.getcwd() + '/test_server'
    if os.path.isfile(user_db):
        os.remove(user_db)
    if os.path.isfile(shares_db):
        os.remove(shares_db)
    if os.path.isdir(root):
        rmtree(root)
    os.mkdir(root)
    ta = TableAdder(user_db, user_table)
    cols = ['name', 'status', 'password', 'salt', 'welcome', 'goodbye']
    for col in cols:
        if col == 'status':
            ta.add_column(col, 'integer')
        else:
            ta.add_column(col)
    ta.commit()
    del ta
    with TableManager(user_db, user_table) as tm:
        salt = gen_salt()
        password = gen_hash(password, salt)
        row = [username, 1, password, salt, 'welcome', 'goodbye']
        tm.quick_push(row)
    ta = TableAdder(shares_db, username)
    cols = ['time', 'ip', 'cmd', 'line', 'arg']
    for col in cols:
        ta.add_column(col)
    ta.commit()
    del ta
    container.set_acc_db(user_db, user_table)
    container.set_shares_db(shares_db)
    container.set_root_dir(root)
    container.set_log_file(root + '/pyftpd.log')
    auth = authorizer()
    handle = handler
    handle.user_sendfile = True
    handle.timeout = None
    handle.authorizer = auth
    handle.banner = 'this is the banner'
    if port:  # untested
        address = ('', port)
    else:
        address = ('', 21)
    server = FTPServer(address, handle)
    server.max_cons = 256
    server.maxcons_per_ip = 5
    if not is_verbose:
        logging.basicConfig(filename=container.get_log_file(),
                            level=logging.INFO)
    server.serve_forever()
Exemplo n.º 12
0
def test_tm_push_bad_info():
    """
    @precondition: test_tm_with_enter passed
    @precondition: test_tm_with_exit passed
    @precondition: test_tm_with_exit_error passed
    I am not sure that value error is right
    Tries to push non matching data to the db
    """
    push = [('not a column', 'raise error'), ('text_col', 'one')]
    with TableManager(db_name, table_name) as t:
        t.push(push)
Exemplo n.º 13
0
def test_tm_clear_table():
    """
    @precondition: test_tm_with_enter passed
    @precondition: test_tm_with_exit passed
    @precondition: test_tm_with_exit_error passed
    Tries to clear the table of all data
    """
    with TableManager(db_name, table_name) as t:
        t.clear_table()
    s = SqlManager(db_name)
    command = 'SELECT * FROM ' + table_name + ';'
    s.connect()
    data = s._fetch_command(command)
    s.disconnect()
    actual = False
    if len(data) == 0:
        actual = True
    n_ok(actual, message=actual)
Exemplo n.º 14
0
def test_tm_easy_pull():
    """
    @precondition: test_tm_quick_push passed
    @precondition: test_tm_with_enter passed
    @precondition: test_tm_with_exit passed
    @precondition: test_tm_with_exit_error passed
    Tries to pull all the columns where every type is text
    """
    testing_table = 'def'
    ta = TableAdder(db_name, testing_table)
    ta.add_column('col_one')
    ta.add_column('col_two')
    ta.commit()
    push = ['one', 'two']
    expected = ('one', 'two')
    with TableManager(db_name, testing_table) as t:
        t.quick_push(push)
        actual = t.pull()[0]
    n_eq(expected, actual)
Exemplo n.º 15
0
def test_tm_sorted_good_push():
    """
    @precondition: test_tm_with_enter passed
    @precondition: test_tm_with_exit passed
    @precondition: test_tm_with_exit_error passed
    Tries to push a list where the columns are already in order with good info
    """
    push = [('text_col', 'one'), ('int_col', 1)]
    with TableManager(db_name, table_name) as t:
        t.push(push)
    s = SqlManager(db_name)
    command = 'SELECT * FROM ' + table_name + ';'
    s.connect()
    data = s._fetch_command(command)
    s.disconnect()
    data = data[0]
    expected = [str(val[1]) for val in push]
    actual = [str(val) for val in data]
    n_eq(expected, actual)
Exemplo n.º 16
0
def test_pull_where_string():
    """
    This one test should cover all operators
    @precondition: test_tm_quick_push passed
    @precondition: test_tm_with_enter passed
    @precondition: test_tm_with_exit passed
    @precondition: test_tm_with_exit_error passed
    """
    with TableManager(db_name, table_name) as t:
        t.clear_table()
        for i in range(9):
            text = 'one_%d' % i
            t.quick_push([text, i])
        expected = ('one_5', 5)
        value = column_map[0][0]
        compare_to = 'one_5'
        op = '='
        actual = t.pull_where(value, compare_to, op)[0]
    n_eq(expected, actual)
Exemplo n.º 17
0
def test_tm_unsorted_good_push():
    """
    @precondition: test_tm_with_enter passed
    @precondition: test_tm_with_exit passed
    @precondition: test_tm_with_exit_error passed
    Tries to sort the list of good info and then push it.
    Also does type conversion
    """
    push = [('int_col', '1'), ('text_col', 'one')]
    with TableManager(db_name, table_name) as t:
        t.push(push)
    s = SqlManager(db_name)
    command = 'SELECT * FROM ' + table_name + ';'
    s.connect()
    data = s._fetch_command(command)
    s.disconnect()
    data = data[1]
    expected = [str(val[1]) for val in push][::-1]
    actual = [str(val) for val in data]
    n_eq(expected, actual)
Exemplo n.º 18
0
def test_hard_pull():
    """
    @precondition: test_tm_quick_push passed
    @precondition: test_tm_with_enter passed
    @precondition: test_tm_with_exit passed
    @precondition: test_tm_with_exit_error passed
    Tries to some of the columns and have different types
    """
    testing_table = 'ghi'
    ta = TableAdder(db_name, testing_table)
    ta.add_column('col_one')
    ta.add_column('col_two', 'integer')
    ta.add_column('col_three')
    ta.add_column('col_four')
    ta.commit()
    push = ['one', 2, 'three', 'four']
    cl = ['col_one', 'col_two']
    expected = ('one', 2)
    with TableManager(db_name, testing_table) as t:
        t.quick_push(push)
        actual = t.pull(cl)[0]
    n_eq(expected, actual)
Exemplo n.º 19
0
def server_user_add(username, password, is_admin=False):
    if not os.path.isfile('conf.json'):
        print 'Conf file not found, please run setup.'
        sys.exit(1)
    if is_admin:
        status = 1
    else:
        status = 0
    with open('conf.json') as jd:
        template = json.load(jd)
    with TableManager(str(template['user_db']),
                      str(template['user_table'])) as tm:
        salt = gen_salt()
        password = gen_hash(password, salt)
        row = [username, status, password, salt, 'welcome', 'goodbye']
        tm.quick_push(row)
    ta = TableAdder(template['user_logs'], username)
    cols = ['time', 'ip', 'cmd', 'line', 'arg']
    for col in cols:
        ta.add_column(col)
    ta.commit()
    del ta
    user_dir = '%s/%s' % (template['root'], username)  # Untested
    os.mkdir(user_dir)  # Untested
Exemplo n.º 20
0
def test_tm_not_exists():
    """
    Tries to connect to a table that does not exist
    """
    TableManager(db_name, 'not a table')