def test_unknown_endpoint(self, fix_anonymous): "Test accessing an unknown API endpoint." from relayr import Api from relayr.exceptions import RelayrApiException with pytest.raises(RelayrApiException) as excinfo: url = '%s/this-should-not-exist' % fix_anonymous.testset0['relayrAPI'] resp = Api().perform_request('GET', url) assert str(excinfo.value).startswith('URL could not be routed.')
def show_check(): "Show some key numbers about the current state of the API." # For all this no credentials/token etc. are needed. api = Api() # Get server status. t0 = time.time() try: res = api.get_server_status() ok = res['database'] == 'ok' except: msg = 'Relayr API not reachable, sorry for that!' termcolor.cprint(msg, 'red') sys.exit(-1) t = time.time() - t0 print('server status ok: %s (%.2f s)' % (ok, t)) # Get number of all publishers. t0 = time.time() publishers = list(api.get_public_publishers()) t = time.time() - t0 print('%d publishers (%.2f s)' % (len(publishers), t)) # Get number of all apps. t0 = time.time() apps = list(api.get_public_apps()) t = time.time() - t0 print('%d applications (%.2f s)' % (len(apps), t)) # Get number of all public devices. t0 = time.time() devices = list(api.get_public_devices()) t = time.time() - t0 print('%d public devices (%.2f s)' % (len(devices), t)) # Get number of all device models. t0 = time.time() models = list(api.get_public_device_models()) t = time.time() - t0 print('%d device models (%.2f s)' % (len(models), t)) # Get number of all device model meanings. t0 = time.time() meanings = list(api.get_public_device_model_meanings()) t = time.time() - t0 print('%d device model meanings (%.2f s)' % (len(meanings), t)) # Get a count of all public devices by meaning. counts = {} for device in devices: readings = device['model']['readings'] for reading in readings: meaning = m = reading['meaning'] counts[m] = counts[m] + 1 if counts.get(m, 0) else 1 print('#public devices by meaning: %s' % json.dumps(counts, indent=4))
def test_unknown_endpoint_data(self, fix_anonymous): "Test accessing an unknown API endpoint with body data." from relayr import Api from relayr.exceptions import RelayrApiException with pytest.raises(RelayrApiException) as excinfo: url = '%s/this-should-not-exist' % fix_anonymous.testset0['relayrAPI'] resp = Api().perform_request('POST', url, data={'foo': 42, 'bar': 23}) assert str(excinfo.value).startswith('URL could not be routed.') assert '--data "{\\"foo\\": 42, \\"bar\\": 23}"' in str(excinfo.value) or \ '--data "{\\"bar\\": 23, \\"foo\\": 42}"' in str(excinfo.value)
def test_repeated_creation(self, fix_registered): "Test repeatedly creating channel credentials." from relayr import Api import paho.mqtt.client as mqtt token = fix_registered.testset1['token'] api = Api(token=token) deviceId = fix_registered.testset1['deviceID'] # test clientIds are different creds_1 = api.post_channel(deviceId, 'mqtt') creds_2 = api.post_channel(deviceId, 'mqtt') assert creds_1['credentials']['clientId'] != creds_2['credentials'][ 'clientId'] # test the rest is the same del creds_1['credentials']['clientId'] del creds_2['credentials']['clientId'] assert creds_1 == creds_2 # cleanup, removing created stuff channelId = creds_1['channelId'] api.delete_channel_id(channelId) channels = api.get_device_channels(deviceId)['channels'] channelIds = [ch['channelId'] for ch in channels] assert channelId not in channelIds
def test_repeated_creation(self, fix_registered): "Test repeatedly creating channel credentials." from relayr import Api import paho.mqtt.client as mqtt token = fix_registered.testset1['token'] api = Api(token=token) deviceId = fix_registered.testset1['deviceID'] # test clientIds are different creds_1 = api.post_channel(deviceId, 'mqtt') creds_2 = api.post_channel(deviceId, 'mqtt') assert creds_1['credentials']['clientId'] != creds_2['credentials']['clientId'] # test the rest is the same del creds_1['credentials']['clientId'] del creds_2['credentials']['clientId'] assert creds_1 == creds_2 # cleanup, removing created stuff channelId = creds_1['channelId'] api.delete_channel_id(channelId) channels = api.get_device_channels(deviceId)['channels'] channelIds = [ch['channelId'] for ch in channels] assert channelId not in channelIds
def show_versions(): "Show some versioning info." # Show header lines. api = Api() url = api.host dt = datetime.datetime.now().isoformat() term_width = get_terminal_size()[0] print('.' * term_width) print('Path: %s' % __file__) hash = md5(open(__file__).read().encode('utf8')).hexdigest() print('MD5 hash: %s' % hash) print('API: %s' % url) print('Python Relayr client version: %s' % relayr_version) pyimpl = platform.python_implementation() pyver = platform.python_version() platf = platform.platform() print('Python version: %s %s' % (pyimpl, pyver)) print('Platform: %s' % platf) print('Date/Time: %s' % dt) print('.' * term_width)