def test_wrong_id_failure(client, add_user):
    _ = add_user(role=ROLE_USER, log_him_in=True)

    device = add_device()
    _ = client.get(endpoint=endpoint,
                   device_id=device.id + 100,
                   check_status=404)
def test_filter_by_date_range(client, add_user):
    _ = add_user(log_him_in=True)
    device = add_device(uid_token=str(uuid4()))
    date_time_now = datetime.utcnow()
    date_time_future = date_time_now + timedelta(days=5)
    date_time_past = date_time_now - timedelta(days=5)
    device_health_future = add_device_health(device_id=device.id,
                                             created_at=date_time_future)
    device_health_now = add_device_health(device_id=device.id,
                                          created_at=date_time_now)
    device_health_past = add_device_health(device_id=device.id,
                                           created_at=date_time_past)
    resp = client.get(endpoint=endpoint,
                      start_date_time=date_time_now,
                      end_date_time=date_time_future)

    assert 'total' in resp
    assert resp['total'] > 0
    assert 'results' in resp
    date_time_now_str = datetime.strftime(date_time_now,
                                          '%Y-%m-%dT%H:%M:%S.%f')
    date_time_future_str = datetime.strftime(date_time_future,
                                             '%Y-%m-%dT%H:%M:%S.%f')
    assert all([r['created_at'] >= date_time_now_str for r in resp['results']])
    assert all(
        [r['created_at'] <= date_time_future_str for r in resp['results']])
    assert any([
        r['id'] == device_health_now.id or r['id'] == device_health_future.id
        for r in resp['results']
    ])
    assert not any([r['id'] == device_health_past.id for r in resp['results']])
def test_not_admin_failure(client, add_user):
    _ = add_user(role=ROLE_USER, log_him_in=True)

    device = add_device()
    _ = client.delete(
        endpoint=endpoint,
        device_id=device.id,
        check_status=403
    )
Exemple #4
0
def test_default(client):
    device = add_device(uid_token=str(uuid4()))
    resp = client.post(
        endpoint=endpoint,
        headers={
            AUTH_TOKEN_HEADER_NAME: f'{device.id}:{device.access_token}',
        },
    )
    assert resp == 'ok'
def test_default(client, add_user):
    _ = add_user(log_him_in=True)
    device = add_device(uid_token=str(uuid4()))
    device_health = add_device_health(device_id=device.id)

    resp = client.get(endpoint=endpoint)
    assert 'total' in resp
    assert resp['total'] > 0
    assert 'results' in resp
    assert any([r['id'] == device_health.id for r in resp['results']])
def test_default(client):
    device = add_device(uid_token=str(uuid4()))
    resp = client.get(
        endpoint=endpoint,
        headers={
            AUTH_TOKEN_HEADER_NAME: f'{device.id}:{device.access_token}',
        },
    )
    assert 'results' in resp
    assert isinstance(resp['results'], list)
def test_wrong_id_failure(client, add_user):
    _ = add_user(role=ROLE_ADMIN, log_him_in=True)

    device = add_device()
    _ = client.delete(
        endpoint=endpoint,
        device_id=129129129129192,
        check_status=404
    )
    assert Device.query.get(device.id)
def test_default(client, add_user):
    _ = add_user(role=ROLE_ADMIN, log_him_in=True)

    device = add_device()
    resp = client.delete(
        endpoint=endpoint,
        device_id=device.id,
    )
    assert 'id' in resp
    assert resp['id'] == device.id
    assert not Device.query.get(resp['id'])
def test_not_admin_failure(client, add_user):
    _ = add_user(role=ROLE_USER, log_him_in=True)

    device = add_device()
    _ = client.put(endpoint=endpoint,
                   device_id=device.id,
                   check_status=403,
                   data=dict(
                       status=ACTIVE,
                       comment=get_random_str(),
                   ))
def test_filter_by_id(client, add_user):
    _ = add_user(log_him_in=True)
    device1 = add_device(uid_token=str(uuid4()))
    device2 = add_device(uid_token=str(uuid4()))
    device_health_first = add_device_health(device_id=device1.id)
    device_health_second = add_device_health(device_id=device2.id)

    resp = client.get(endpoint=endpoint,
                      device_id=device_health_first.device_id)

    assert 'total' in resp
    assert resp['total'] > 0
    assert 'results' in resp
    assert all([
        r['device']['id'] == device_health_first.device_id
        for r in resp['results']
    ])
    assert all([
        r['device']['id'] != device_health_second.device_id
        for r in resp['results']
    ])
def test_default(client, add_user):
    _ = add_user(role=ROLE_USER, log_him_in=True)

    device = add_device()
    resp = client.get(
        endpoint=endpoint,
        device_id=device.id,
    )
    assert 'id' in resp
    assert resp['id'] == device.id
    assert 'access_token' not in resp
    assert 'uid_token' not in resp
Exemple #12
0
def test_default(client, add_user):
    user = add_user(role=ROLE_MANAGER)
    content = add_content(created_by=user.id, publisher_id=user.publisher_id)
    device = add_device()
    resp = client.post(
        endpoint=endpoint,
        file_id=content.id,
        headers={
            AUTH_TOKEN_HEADER_NAME: f'{device.id}:{device.access_token}',
        },
    )
    assert resp == 'ok'
Exemple #13
0
def test_malformed_params_failure(client, software_version, param_name):
    device = add_device(uid_token=str(uuid4()))
    resp = client.post(
        endpoint=endpoint,
        data=dict(software_version=software_version, ),
        headers={
            AUTH_TOKEN_HEADER_NAME: f'{device.id}:{device.access_token}',
        },
        check_status=400,
    )
    assert 'errors' in resp
    assert len(resp['errors']) == 1
    assert param_name in resp['errors'][0].lower()
Exemple #14
0
def test_default(client, software_version):
    device = add_device(uid_token=str(uuid4()))
    resp = client.post(
        endpoint=endpoint,
        data=dict(software_version=software_version, ),
        headers={
            AUTH_TOKEN_HEADER_NAME: f'{device.id}:{device.access_token}',
        },
    )
    assert 'id' in resp
    device_health = DeviceHealth.query.filter_by(id=resp['id']).one_or_none()
    assert device_health

    assert device_health.device_id == device.id
    assert device_health.software_version == software_version
def test_default(client, add_user):
    _ = add_user(role=ROLE_USER, log_him_in=True)

    device = add_device()
    resp = client.get(endpoint=endpoint)
    assert 'total' in resp
    assert resp['total'] == 1

    assert 'results' in resp
    assert len(resp['results']) == 1
    assert 'id' in resp['results'][0]
    assert device.id == resp['results'][0]['id']

    assert 'access_token' not in resp['results'][0]
    assert 'uid_token' not in resp['results'][0]
def test_malformed_params_failure(client, add_user, contact_id, location_id,
                                  comment, status):
    _ = add_user(role=ROLE_ADMIN, log_him_in=True)

    device = add_device()
    resp = client.put(endpoint=endpoint,
                      device_id=device.id,
                      check_status=400,
                      data=dict(
                          status=status,
                          comment=comment,
                          contact_id=contact_id,
                          location_id=location_id,
                      ))
    assert 'errors' in resp
    assert len(resp['errors']) == 1
def test_default(client, add_user):
    user = add_user(role=ROLE_MANAGER)
    _ = add_content(created_by=user.id, publisher_id=user.publisher_id)
    device = add_device()

    resp = client.get(
        endpoint=endpoint,
        headers={
            AUTH_TOKEN_HEADER_NAME: f'{device.id}:{device.access_token}',
        },
    )

    assert 'results' in resp
    assert resp['results']
    for file in resp['results']:
        assert 'id' in file
        assert 'src' in file
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
def test_not_auth_failure(client):
    device = add_device()
    _ = client.get(endpoint=endpoint, device_id=device.id, check_status=403)
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