示例#1
0
def get_contacts():
    email = request.args.get('email', '')
    if email:
        contacts = load_contacts(email=email)
    else:
        contacts = load_contacts()
    return jsonify(contacts)
示例#2
0
def test_purge_old_contacts(test_app):
    with test_app.app_context():
        save_contact(username='******',
                     name='v',
                     surname='viper',
                     emails=['*****@*****.**'])

        # save contact as if it was saved 2 mins ago
        with mock.patch('contacts.db.datetime') as mock_datetime:
            mock_datetime.now.return_value = datetime.now() - timedelta(
                seconds=120)
            save_contact(username='******',
                         name='m',
                         surname='mantis',
                         emails=['*****@*****.**'])

        older_than_timestamp = datetime.now() - timedelta(seconds=60)
        purge_old_contacts(older_than_timestamp)

        contacts = load_contacts()
        assert len(contacts) == 1
        assert dict(username='******',
                    name='v',
                    surname='viper',
                    emails=['*****@*****.**']) in contacts
示例#3
0
def test_add_random_contacts(test_app):
    with test_app.app_context():
        add_random_contacts()

    actual = load_contacts()
    print(actual)
    assert len(actual) == 1
    assert len(actual[0]['name']) == 5
    assert actual[0]['username'] == actual[0]['name'] + actual[0]['surname']
示例#4
0
def add_contact():
    columns = Contact.get_cols()
    if not request.json or any([col not in request.json for col in columns]):
        abort(400)
    contact_attrs = {col: request.json[col] for col in columns}

    save_contact(**contact_attrs)
    all_contacts = load_contacts()

    return jsonify(all_contacts), 201
示例#5
0
def test_delete_contact(test_client_with_populated_db):
    response = test_client_with_populated_db.delete(
        '/contacts_app/contacts/mastershifu')
    assert response.status_code == 204
    expected_remaining_contacts = [{
        'username': '******',
        'emails': ['*****@*****.**'],
        'name': 'm',
        'surname': 'antis'
    }]
    contacts = load_contacts()
    assert expected_remaining_contacts == contacts
示例#6
0
def test_load_contacts(test_app):
    with test_app.app_context():
        save_contact(username='******',
                     name='v',
                     surname='viper',
                     emails=['*****@*****.**'])
        save_contact(username='******',
                     name='m',
                     surname='mantis',
                     emails=['*****@*****.**'])
        contacts = load_contacts()
        assert len(contacts) == 2
        assert dict(username='******',
                    name='v',
                    surname='viper',
                    emails=['*****@*****.**']) in contacts
示例#7
0
def test_add_random_contacts_deletes_contacts_older_than_one_minute(test_app):
    with test_app.app_context():
        # save contact as if it was saved 2 mins ago
        with mock.patch('contacts.db.datetime') as mock_datetime:
            mock_datetime.now.return_value = datetime.now() - timedelta(
                seconds=120)

            save_contact(username='******',
                         name='m',
                         surname='mantis',
                         emails=['*****@*****.**'])
        add_random_contacts()

    actual = load_contacts()
    print(actual)
    assert len(actual) == 1
    assert len(actual[0]['name']) == 5
    assert actual[0]['username'] == actual[0]['name'] + actual[0]['surname']
示例#8
0
def get_contact(username):
    contacts = load_contacts(username=username)
    if not contacts:
        abort(404)
    return jsonify(contacts[0])