def test_search_mode(client, add_user): _ = add_user(role=ROLE_ADMIN, log_him_in=True) common_string = 'FGHJ#$%^&12' ids = { add_location(address=f'prefixXXX{common_string}').id, add_location(address=f'prefixXXX{common_string}postfix123').id, add_location(address=f'{common_string}postfix123').id, add_location(address=common_string).id, add_location(city_name=f'prefixXXX{common_string}').id, add_location(city_name=f'prefixXXX{common_string}postfix123').id, add_location(city_name=f'{common_string}postfix123').id, add_location(city_name=common_string).id, } # Add some noise for _ in range(10): add_location() # Search resp = client.get(endpoint=endpoint, query=common_string) assert 'total' in resp assert resp['total'] == 8 assert 'results' in resp assert len(resp['results']) == 8 assert {r['id'] for r in resp['results']} == ids
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
def test_not_admin_failure(client, add_user): _ = add_user(role=ROLE_USER, log_him_in=True) location = add_location() _ = client.delete(endpoint=endpoint, location_id=location.id, check_status=403)
def test_wrong_id_failure(client, add_user): _ = add_user(role=ROLE_ADMIN, log_him_in=True) location = add_location() _ = client.delete(endpoint=endpoint, location_id=129129129129192, check_status=404) assert Location.query.get(location.id)
def test_default(client, add_user): _ = add_user(role=ROLE_ADMIN, log_him_in=True) location = add_location() 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 resp['results'][0]['id'] == location.id location2 = add_location() resp = client.get(endpoint=endpoint) assert 'total' in resp assert resp['total'] == 2 assert 'results' in resp assert len(resp['results']) == 2 assert {r['id'] for r in resp['results']} == {location.id, location2.id}
def test_malformed_params_failure(client, add_user, address): _ = add_user(role=ROLE_ADMIN, log_him_in=True) location = add_location() resp = client.put(endpoint=endpoint, location_id=location.id, data=dict(address=address, city_id=location.city.id), check_status=400) assert 'errors' in resp assert len(resp['errors']) == 1
def test_default(client, add_user): _ = add_user(role=ROLE_ADMIN, log_him_in=True) location = add_location() resp = client.delete( endpoint=endpoint, location_id=location.id, ) assert 'id' in resp assert resp['id'] == location.id assert not Location.query.get(resp['id'])
def test_default(client, add_user, address): _ = add_user(role=ROLE_ADMIN, log_him_in=True) location = add_location() resp = client.put(endpoint=endpoint, location_id=location.id, data=dict(address=address, city_id=location.city.id)) assert 'id' in resp location = Location.query.get(resp['id']) assert location assert 'address' in resp assert resp['address'] == location.address == address assert 'city' in resp assert 'id' in resp['city'] assert resp['city']['id'] == location.city.id assert 'name' in resp['city'] assert resp['city']['name'] == location.city.name
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