Example #1
0
def handle_data():
    """
    App route for handling form data.

    :return: stream of generated logins and passwords with headers consisting of HTTP status 200,
        the MIME type of file determined by the content_type() function and filename determined by
        _generate_output_name() function.
    """

    if 'same_form' in request.form:
        form = SameForm()
        print('SAME found')
        form.validate_on_submit()
        print('POST request valid')
    elif 'different_form' in request.form:
        form = DifferentForm()
        print('DIFFERENT found')
        form.validate_on_submit()
        print('post request valid')

    constraints = create_dictionaries()
    extension = file_format()
    output = _replace_boolean(generate_logins_and_passwords(constraints))
    output_text = StringIO('')
    _file_writer(output_text, extension, output, headers())
    name = _generate_output_name(extension)
    saving_file(name, constraints, output)

    return output_text.getvalue(), 200, {
        'content-type': content_type(),
        'content-disposition': 'attachment; filename={}'.format(name),
    }
Example #2
0
def saving_file(name, constraints, output):
    """
    Function creates file in 'web_app/static/files' directory on server side.
    If user is not authenticated, file is not saved. If user is authenticated,
    opens session, and saves file information to database.

    :param name: name of file, generated based on current time
    :param constraints: dictionaries of constraints
    :param output: data generated by generate_logins_and_passwords function
    :return:
    """

    if current_user.is_authenticated:
        extension = file_format()
        header = 'yes' if headers() else 'no'
        db_session = db_sessionmaker()
        db_session.add(
            FileHistory(user_id=current_user.user_id,
                        records_no=constraints[0]['list_length'],
                        ratio=constraints[0]['positive_percentage'],
                        headers=header,
                        login_min_length=constraints[0]['min_length'],
                        login_max_length=constraints[0]['max_length'],
                        login_textarea_custom=constraints[0]['characters'],
                        pass_min_length=constraints[1]['min_length'],
                        pass_max_length=constraints[1]['max_length'],
                        pass_textarea_custom=constraints[1]['characters'],
                        file_path='files/%s' % name,
                        file_extension=extension,
                        created_at=time.time()))
        db_session.commit()
        db_session.close()
        with open('web_app/static/files/' + name, 'a', newline='') as file:
            _file_writer(file, extension, output, header)
Example #3
0
def test_file_writer_txt_no_headers():
    test_data = [['first_element_first_row', 'second_element_first_row'],
                 ['first_element_second_row', 'second_element_second_row']]
    mock_file = io.StringIO()
    _file_writer(mock_file, 'txt', test_data, False)
    mock_file.seek(0)
    assert mock_file.readlines() == [
        'first_element_first_row second_element_first_row\r\n',
        'first_element_second_row second_element_second_row\r\n'
    ]
Example #4
0
def test_file_writer_csv_no_headers():
    test_data = [['First column first row', 'Second column first row'],
                 ['first column second row', 'second column second row']]
    mock_file = io.StringIO()
    _file_writer(mock_file, 'csv', test_data, False)
    mock_file.seek(0)
    assert mock_file.readlines() == [
        'First column first row,Second column first row\r\n',
        'first column second row,second column second row\r\n'
    ]
Example #5
0
def test_file_writer_txt_with_headers():
    test_data = [['first_element_first_row', 'second_element_first_row'],
                 ['first_element_second_row', 'second_element_second_row']]
    mock_file = io.StringIO()
    _file_writer(mock_file, 'txt', test_data, True)
    mock_file.seek(0)
    assert mock_file.readlines() == [
        'Login P/N Password P/N\r\n',
        'first_element_first_row second_element_first_row\r\n',
        'first_element_second_row second_element_second_row\r\n'
    ]
Example #6
0
def test_file_writer_csv_with_headers():
    test_data = [['First column first row', 'Second column first row'],
                 ['first column second row', 'second column second row']]
    mock_file = io.StringIO()
    _file_writer(mock_file, 'csv', test_data, True)
    mock_file.seek(0)
    assert mock_file.readlines() == [
        'Login,P/N,Password,P/N\r\n',
        'First column first row,Second column first row\r\n',
        'first column second row,second column second row\r\n'
    ]