def test_search_mode(client, add_user):
    _ = add_user(role=ROLE_ADMIN, log_him_in=True)

    string = 'common string to find'
    strings = [
        f'some prefix {string}',
        f'{string} some postfix',
        f'prefix {string} postfix',
        string,
    ]

    # Create devices
    ids = set()
    for s in strings:
        location = add_location(address=s)
        ids.add(add_device(location_id=location.id).id)

        contact = add_contact(name=s)
        ids.add(add_device(contact_id=contact.id).id)

        contact = add_contact(tel=s)
        ids.add(add_device(contact_id=contact.id).id)

        ids.add(add_device(comment=s).id)

    # Add some noise
    for s in strings:
        # We do not search by city_name
        location = add_location(city_name=get_random_str())
        add_device(location_id=location.id)

        location = add_location(address=get_random_str())
        add_device(location_id=location.id)

        contact = add_contact(name=get_random_str())
        add_device(contact_id=contact.id)

        contact = add_contact(tel=get_random_str())
        add_device(contact_id=contact.id)

        add_device(comment=get_random_str())

    # Check results
    resp = client.get(
        endpoint=endpoint,
        query=string,
        limit=100  # To get all the results on one page
    )
    assert 'total' in resp
    assert resp['total'] == len(ids)
    assert 'results' in resp
    assert {r['id'] for r in resp['results']} == ids
Beispiel #2
0
def test_not_admin_failure(client, add_user):
    _ = add_user(role=ROLE_USER, log_him_in=True)

    contact = add_contact()
    _ = client.delete(endpoint=endpoint,
                      contact_id=contact.id,
                      check_status=403)
Beispiel #3
0
def test_wrong_id_failure(client, add_user):
    _ = add_user(role=ROLE_ADMIN, log_him_in=True)

    contact = add_contact()
    _ = client.delete(endpoint=endpoint,
                      contact_id=129129129129192,
                      check_status=404)
    assert Contact.query.get(contact.id)
Beispiel #4
0
def test_default(client, add_user):
    _ = add_user(role=ROLE_ADMIN, log_him_in=True)

    contact = add_contact()
    resp = client.delete(
        endpoint=endpoint,
        contact_id=contact.id,
    )
    assert 'id' in resp
    assert resp['id'] == contact.id
    assert not Contact.query.get(resp['id'])
Beispiel #5
0
def test_malformed_params_failure(client, add_user, name, tel, comment):
    _ = add_user(role=ROLE_ADMIN, log_him_in=True)
    contact_id = add_contact().id
    resp = client.put(endpoint=endpoint,
                      contact_id=contact_id,
                      data=dict(
                          name=name,
                          tel=tel,
                          comment=comment,
                      ),
                      check_status=400)
    assert 'errors' in resp
    assert len(resp['errors']) == 1
Beispiel #6
0
def test_not_admin_failure(client, add_user):
    _ = add_user(role=ROLE_USER, log_him_in=True)
    contact_id = add_contact().id

    resp = client.put(endpoint=endpoint,
                      contact_id=contact_id,
                      data=dict(
                          name=get_random_str(),
                          tel=get_random_str(),
                          comment=get_random_str(),
                      ),
                      check_status=403)
    assert 'errors' in resp
    assert len(resp['errors']) == 1
Beispiel #7
0
def test_default(client, add_user):
    _ = add_user(role=ROLE_ADMIN, log_him_in=True)
    contact_id = add_contact().id
    resp = client.put(endpoint=endpoint,
                      contact_id=contact_id,
                      data=dict(
                          name=get_random_str(),
                          tel=get_random_str(),
                          comment=get_random_str(),
                      ))
    assert 'id' in resp
    contact = Contact.query.get(resp['id'])
    assert contact

    assert 'name' in resp
    assert resp['name'] == contact.name
    assert 'tel' in resp
    assert resp['tel'] == contact.tel
    assert 'comment' in resp
    assert resp['comment'] == contact.comment
def test_default(client, add_user, contact, location, comment, status):
    _ = add_user(role=ROLE_ADMIN, log_him_in=True)
    device = add_device()

    contact_id = add_contact().id if contact else None
    location_id = add_location().id if location else None
    resp = client.put(endpoint=endpoint,
                      device_id=device.id,
                      data=dict(
                          status=status,
                          comment=comment,
                          contact_id=contact_id,
                          location_id=location_id,
                      ))
    assert 'id' in resp
    assert resp['id'] == device.id
    updated_device = Device.query.get(resp['id'])
    assert updated_device

    assert 'comment' in resp
    assert updated_device.comment == comment == resp['comment']

    assert 'status' in resp
    if status:
        assert updated_device.status == status == resp['status']
    else:
        assert updated_device.status == resp['status'] == UNAPPROVED

    assert 'location' in resp
    assert updated_device.location_id == location_id
    if location:
        assert 'id' in resp['location']
        assert resp['location']['id'] == location_id

    assert 'contact' in resp
    assert updated_device.contact_id == contact_id
    if contact:
        assert 'id' in resp['contact']
        assert resp['contact']['id'] == contact_id