def test_download_logged_data_fail_nodownload(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/dsns/c/properties', json=DEVICE_ATTRIBUTES, status=200) responses.add(responses.GET, 'http://de.mo/file', json=DOWNLOAD_DATA, status=200) # Initialize OwletAPI api = OwletAPI("*****@*****.**", "moped") api.login() # Instantiate the device device = Owlet(api, DEVICE_PAYLOAD) # Update the decice device.update() with pytest.raises(OwletTemporaryCommunicationException) as info: device.download_logged_data() assert 'Download Request failed - no answer' in str(info.value)
def test_download_logged_data_ok(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/dsns/c/properties', json=DEVICE_ATTRIBUTES, status=200) responses.add(responses.GET, 'http://de.mo/file', json=DOWNLOAD_DATA, status=200) responses.add( responses.GET, 'https://ayla-device-field-production-1a2039d9.s3.amazonaws.com/X?AWSAccessKeyId=Y&Expires=1234&Signature=Z', status=200) # Initialize OwletAPI api = OwletAPI("*****@*****.**", "moped") api.login() # Instantiate the device device = Owlet(api, DEVICE_PAYLOAD) # Update the decice device.update() device.download_logged_data()
def test_download_logged_data_fail_noattribute(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) my_device_attributes = copy.deepcopy(DEVICE_ATTRIBUTES) my_device_attributes[0]['property']['name'] = 'DEADBEEF3' my_device_attributes[1]['property']['name'] = 'DEADBEEF3' my_device_attributes[2]['property']['name'] = 'DEADBEEF3' my_device_attributes[3]['property']['name'] = 'DEADBEEF3' responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/dsns/c/properties', json=my_device_attributes, status=200) # Initialize OwletAPI api = OwletAPI("*****@*****.**", "moped") api.login() # Instantiate the device device = Owlet(api, DEVICE_PAYLOAD) # Update the decice device.update() with pytest.raises(OwletNotInitializedException) as info: device.download_logged_data() assert 'Initialize first - missing property' in str(info.value)
def test_reactivate_ok(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/dsns/c/properties', json=DEVICE_ATTRIBUTES, status=200) responses.add( responses.POST, 'https://ads-field.aylanetworks.com/apiv1/properties/42738119/datapoints', status=201) # Initialize OwletAPI api = OwletAPI("*****@*****.**", "moped") api.login() # Instantiate the device device = Owlet(api, DEVICE_PAYLOAD) # Update the decice device.update() device.reactivate()
def test_reactivate_fail_statuscode(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/dsns/c/properties', json=DEVICE_ATTRIBUTES, status=200) responses.add( responses.POST, 'https://ads-field.aylanetworks.com/apiv1/properties/42738119/datapoints', status=500) # Initialize OwletAPI api = OwletAPI("*****@*****.**", "moped") api.login() # Instantiate the device device = Owlet(api, DEVICE_PAYLOAD) # Update the decice device.update() with pytest.raises(OwletTemporaryCommunicationException) as info: device.reactivate() assert 'Server Request failed, return code' in str(info.value)
def test_get_devices_ok(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) api = OwletAPI() api.set_email("*****@*****.**") api.set_password("moped") api.login() responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/devices.json', json=DEVICES_PAYLOAD, status=200) api.get_devices() assert Owlet.__init__.called_once # Check if Owlet has been properly called args, kwargs = Owlet.__init__.call_args instance, arguments = args assert instance is api assert arguments == devices_payload[0]['device'] # When calling get_devices again, no new instances of Owlet should be created api.get_devices() assert Owlet.__init__.called_once
def test_download_logged_data_fail_nodownloadcode(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/dsns/c/properties', json=DEVICE_ATTRIBUTES, status=200) responses.add(responses.GET, 'http://de.mo/file', json=DOWNLOAD_DATA, status=200) responses.add( responses.GET, 'https://ayla-device-field-production-1a2039d9.s3.amazonaws.com/X?AWSAccessKeyId=Y&Expires=1234&Signature=Z', status=500) # Initialize OwletAPI api = OwletAPI("*****@*****.**", "moped") api.login() # Instantiate the device device = Owlet(api, DEVICE_PAYLOAD) # Update the decice device.update() with pytest.raises(OwletTemporaryCommunicationException) as info: device.download_logged_data() assert 'Download Request failed - status code' in str(info.value)
def test_get_auth_token_ok(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) api = OwletAPI() api.set_email("*****@*****.**") api.set_password("moped") api.login() # If no exception occurred, everything seems to be fine assert api.get_auth_token() == "testtoken"
def test_login_fail_noconnection(): api = OwletAPI() api.set_email("*****@*****.**") api.set_password("moped") with pytest.raises(OwletTemporaryCommunicationException) as info: api.login() assert 'Login request failed - no response' in str(info.value) assert api._email == "*****@*****.**" assert api._password == "moped" assert api._auth_token == None assert api.get_auth_token() == None
def test_get_request_headers_ok(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) api = OwletAPI() api.set_email("*****@*****.**") api.set_password("moped") api.login() assert api.get_request_headers()['Content-Type'] == "application/json" assert api.get_request_headers()['Accept'] == "application/json" assert api.get_request_headers()['Authorization'] == "testtoken"
def test_update_devices_fail_noresponse(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) api = OwletAPI() api.set_email("*****@*****.**") api.set_password("moped") api.login() with pytest.raises(OwletTemporaryCommunicationException) as info: api.update_devices() assert 'Server request failed - no response' in str(info.value)
def test_reactivate_fail_noattributes(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) # Initialize OwletAPI api = OwletAPI("*****@*****.**", "moped") api.login() # Instantiate the device device = Owlet(api, DEVICE_PAYLOAD) with pytest.raises(OwletNotInitializedException) as info: device.reactivate() assert 'Initialize first - no properties' in str(info.value)
def test_login_fail_invalidjson(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', body="broken", status=200) api = OwletAPI() api.set_email("*****@*****.**") api.set_password("moped") with pytest.raises(OwletTemporaryCommunicationException) as info: api.login() assert 'Server did not send valid json' in str(info.value) assert api._email == "*****@*****.**" assert api._password == "moped" assert api._auth_token == None assert api.get_auth_token() == None
def test_update_noresponse(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) # Initialize OwletAPI api = OwletAPI("*****@*****.**", "moped") api.login() # Instantiate the device device = Owlet(api, DEVICE_PAYLOAD) # Update the decice with pytest.raises(OwletTemporaryCommunicationException) as info: device.update() assert 'Server Request failed - no response' in str(info.value)
def test_login_fail(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=401) api = OwletAPI() api.set_email("*****@*****.**") api.set_password("moped") with pytest.raises(OwletPermanentCommunicationException) as info: api.login() assert 'Login failed, check username and password' in str(info.value) assert api._email == "*****@*****.**" assert api._password == "moped" assert api._auth_token == None assert api.get_auth_token() == None
def test_get_devices_ok(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) api = OwletAPI() api.set_email("*****@*****.**") api.set_password("moped") api.login() responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/devices.json', json=DEVICES_PAYLOAD, status=200) api.get_devices() assert api.get_update_interval() == 177
def test_login_fail_temporary(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=500) api = OwletAPI() api.set_email("*****@*****.**") api.set_password("moped") with pytest.raises(OwletTemporaryCommunicationException) as info: api.login() assert 'Login request failed - status code' in str(info.value) assert api._email == "*****@*****.**" assert api._password == "moped" assert api._auth_token == None assert api.get_auth_token() == None
def test_update_devices_fail_invalidjson(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) api = OwletAPI() api.set_email("*****@*****.**") api.set_password("moped") api.login() responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/devices.json', body="invalid", status=200) with pytest.raises(OwletTemporaryCommunicationException) as info: api.update_devices() assert 'Server did not send valid json' in str(info.value)
def test_update_repeat(): my_device_attributes = copy.deepcopy(DEVICE_ATTRIBUTES) my_device_attributes[0]['property']['value'] = 'DEADBEEF' my_device_attributes[0]['property'][ 'data_updated_at'] = '2018-12-30T09:43:28Z' # Owlet will pull the properties of this particular device responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/dsns/c/properties', json=DEVICE_ATTRIBUTES, status=200) responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/dsns/c/properties', json=my_device_attributes, status=200) responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) # Initialize OwletAPI api = OwletAPI("*****@*****.**", "moped") api.login() # Instantiate the device device = Owlet(api, DEVICE_PAYLOAD) # Update the decice device.update() device.update() assert device.get_property('AGE_MONTHS_OLD').value == 'DEADBEEF' assert device.get_property('DOES_NOT_EXIST') == None properties = device.get_properties() assert properties['AGE_MONTHS_OLD'].value == 'DEADBEEF' assert device.get_update_interval() == 5
def test_update_invalid_json(): # Owlet will pull the properties of this particular device responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/dsns/c/properties', body="INVALID", status=200) responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) # Initialize OwletAPI api = OwletAPI("*****@*****.**", "moped") api.login() # Instantiate the device device = Owlet(api, DEVICE_PAYLOAD) # Update the decice with pytest.raises(OwletTemporaryCommunicationException) as info: device.update() assert 'Update failed - JSON error' in str(info.value)
def test_get_auth_token_relogin(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) login_payload2 = copy.deepcopy(LOGIN_PAYLOAD) login_payload2['access_token'] = 'newtoken' responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=login_payload2, status=200) api = OwletAPI() api.set_email("*****@*****.**") api.set_password("moped") # Login happens at 2018-12-30 and lasts 1 day with freeze_time("2018-12-30"): api.login() assert api.get_auth_token() == "testtoken" with freeze_time("2019-12-30"): assert api.get_auth_token() == "newtoken"
def test_update_ok(): responses.add(responses.POST, 'https://user-field.aylanetworks.com/users/sign_in.json', json=LOGIN_PAYLOAD, status=200) # Owlet will pull the properties of this particular device responses.add(responses.GET, 'https://ads-field.aylanetworks.com/apiv1/dsns/c/properties', json=DEVICE_ATTRIBUTES, status=200) # Initialize OwletAPI api = OwletAPI("*****@*****.**", "moped") api.login() # Instantiate the device device = Owlet(api, DEVICE_PAYLOAD) # Update the decice device.update() assert device.get_property('AGE_MONTHS_OLD').value == None assert device.get_property('ALRTS_DISABLED').value == None assert device.get_property('APP_ACTIVE').value == 0
def cli(): """Command Line Interface for Owletapi.""" parser = argparse.ArgumentParser( description='Owlet API Command Line Interface') parser.add_argument('email', help='Specify Email Address') parser.add_argument('password', help='Specify Password') parser.add_argument( 'actions', help='Specify the actions', nargs='+', choices=["token", "devices", "attributes", "stream", 'download']) parser.add_argument('--device', dest='device', help='Specify DSN for device filter') parser.add_argument('--stream', dest='attributes', action='append', help='Specify attributes for stream filter') parser.add_argument('--timeout', dest='timeout', help='Specify streaming timeout in seconds') # Parse arguments args = parser.parse_args() if args.timeout: timeout = time.time() + float(args.timeout) else: timeout = None # Initialize Owlet api api = OwletAPI() # Provide Login data api.set_email(args.email) api.set_password(args.password) # Login try: api.login() except OwletPermanentCommunicationException: print("Login failed, username or passwort might be wrong") sys.exit(1) except OwletTemporaryCommunicationException: print("Login failed, server might be down") sys.exit(1) # Print token if "token" in args.actions: print("Token: %s" % api.get_auth_token()) # print Devices if "devices" in args.actions: for device in api.get_devices(): if args.device is None or args.device == device.dsn: print("%15s %-7s %2.4f %2.4f" % (device.dsn, device.connection_status, device.lat, device.lon)) # print Attributes if "attributes" in args.actions: for device in api.get_devices(): if args.device is None or args.device == device.dsn: device.update() for name, myproperty in device.get_properties().items(): print("%-19s %-21s %-20s %s" % (myproperty.name, myproperty.display_name, myproperty.last_update, myproperty.value)) if "download" in args.actions: for device in api.get_devices(): if args.device is None or args.device == device.dsn: device.update() print(device.download_logged_data()) # Stream Attributes if "stream" in args.actions: # If no attributes for streaming have been defined, we stream # everything if args.attributes is None: args.attributes = [] for device in api.get_devices(): if args.device is None or args.device == device.dsn: device.update() for name, myproperty in device.get_properties().items(): args.attributes.append(name) # CSV header header = "TIMESTAMP;DSN;" for attribute in args.attributes: header = header + attribute + ";" print(header) # Stream forever while timeout is None or time.time() < timeout: start = time.time() for device in api.get_devices(): try: device.update() except OwletTemporaryCommunicationException: continue try: device.reactivate() except OwletTemporaryCommunicationException: continue if args.device is None or args.device == device.dsn: line = str(time.time()) + ";" + device.dsn + ";" properties = device.get_properties() for attribute in args.attributes: if attribute in properties: line = line + \ str(properties[attribute].value) + ";" print(line) sys.stdout.flush() wait_time = api.get_update_interval() - (time.time() - start) try: time.sleep(max(0, wait_time)) except (KeyboardInterrupt, SystemExit): sys.exit(0)